eteran / c-vector

A dynamic array implementation in C similar to the one found in standard C++
MIT License
737 stars 109 forks source link

Custom memory management compile protection #27

Closed marekmosna closed 2 years ago

eteran commented 2 years ago

I actually think the best solution is simpler. Remove the CVECTOR_CUSTOM_MALLOC macro and just make it so users can override the malloc family. If they don't, they'll just get the default.

eteran commented 2 years ago

In essense, I would change your PR from this:

#ifndef CVECTOR_CUSTOM_MALLOC
    #include <stdlib.h> /* for malloc/realloc/free */

    /* cvector heap implemented using C library malloc() */

    /* in case C library malloc() needs extra protection,
    * allow these defines to be overridden.
    */
    #ifndef cvector_clib_free
    #define cvector_clib_free free
    #endif

    #ifndef cvector_clib_malloc
    #define cvector_clib_malloc malloc
    #endif

    #ifndef cvector_clib_calloc
    #define cvector_clib_calloc calloc
    #endif

    #ifndef cvector_clib_realloc
    #define cvector_clib_realloc realloc
    #endif
#else
    #ifndef cvector_clib_free
    #error "custom cvector_clib_free aquired but not defined"
    #endif

    #ifndef cvector_clib_malloc
    #error "custom cvector_clib_malloc aquired but not defined"
    #endif

    #ifndef cvector_clib_calloc
    #error "custom cvector_clib_calloc aquired but not defined"
    #endif

    #ifndef cvector_clib_realloc
    #error "custom cvector_clib_realloc aquired but not defined"
    #endif
#endif /* #ifndef CVECTOR_CUSTOM_MALLOC */

to just this:

  #include <stdlib.h> /* for malloc/realloc/free */

  /* cvector heap implemented using C library malloc() */

  /* in case C library malloc() needs extra protection,
  * allow these defines to be overridden.
  */
  #ifndef cvector_clib_free
  #define cvector_clib_free free
  #endif

  #ifndef cvector_clib_malloc
  #define cvector_clib_malloc malloc
  #endif

  #ifndef cvector_clib_calloc
  #define cvector_clib_calloc calloc
  #endif

  #ifndef cvector_clib_realloc
  #define cvector_clib_realloc realloc
  #endif