1
0

base.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /** @file base.c
  2. * @brief Base faux functions.
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "faux/faux.h"
  7. /** Portable implementation of free() function.
  8. *
  9. * The POSIX standard says the free() must check pointer for NULL value
  10. * and must not try to free NULL pointer. I.e. it do nothing in a case of NULL.
  11. * The Linux glibc do so. But some non-fully POSIX compatible systems don't.
  12. * For these systems our implementation of free() must check for NULL itself.
  13. *
  14. * For now function simply call free() but it can be changed while working on
  15. * portability.
  16. *
  17. * @param [in] ptr Memory pointer to free.
  18. * @sa free()
  19. */
  20. void faux_free(void *ptr) {
  21. #if 0
  22. if (ptr)
  23. #endif
  24. free(ptr);
  25. }
  26. /** Portable implementation of malloc() function.
  27. *
  28. * The faux library implements its own free() function (called faux_free()) so
  29. * it's suitable to implement their own complementary malloc() function. May be
  30. * somedays it will be more portable than standard malloc().
  31. *
  32. * @param [in] size Memory size to allocate.
  33. * @return Allocated memory or NULL on error.
  34. * @sa malloc()
  35. */
  36. void *faux_malloc(size_t size) {
  37. return malloc(size);
  38. }
  39. /** Portable implementation of bzero().
  40. *
  41. * The POSIX standard says the bzero() is legacy now. It recommends to use
  42. * memset() instead it. But bzero() is easier to use. So bzero() can be
  43. * implemented using memset().
  44. *
  45. * @param [in] ptr Pointer
  46. * @param [in] size Size of memory (in bytes) to zero it.
  47. * @sa bzero()
  48. */
  49. void faux_bzero(void *ptr, size_t size) {
  50. memset(ptr, '\0', size);
  51. }
  52. /** The malloc() implementation with writing zeroes to allocated buffer.
  53. *
  54. * The POSIX defines calloc() function to allocate memory and write zero bytes
  55. * to the whole buffer. But calloc() has strange set of arguments. This function
  56. * is simply combination of malloc() and bzero().
  57. *
  58. * @param [in] size Memory size to allocate.
  59. * @return Allocated zeroed memory or NULL on error.
  60. */
  61. void *faux_zmalloc(size_t size) {
  62. void *ptr = NULL;
  63. ptr = faux_malloc(size);
  64. if (ptr)
  65. faux_bzero(ptr, size);
  66. return ptr;
  67. }