Skip to content

Commit a3a494a

Browse files
authored
ensure harmonized memory management functions (#87)
Allocation, reallocation, and deallocation functions must not belong to different libraries. Ensure that either all or none of them are overridden.
1 parent 49692ef commit a3a494a

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

cvector.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,27 @@
1010
/* in case C library malloc() needs extra protection,
1111
* allow these defines to be overridden.
1212
*/
13-
#ifndef cvector_clib_free
14-
#include <stdlib.h> /* for free */
15-
#define cvector_clib_free free
13+
/* functions for allocation and deallocation need to correspond to each other, fall back to C library functions if not all are overridden */
14+
#if !defined(cvector_clib_free) || !defined(cvector_clib_malloc) || !defined(cvector_clib_calloc) || !defined(cvector_clib_realloc)
15+
#ifdef cvector_clib_free
16+
#undef cvector_clib_free
1617
#endif
17-
#ifndef cvector_clib_malloc
18-
#include <stdlib.h> /* for malloc */
19-
#define cvector_clib_malloc malloc
18+
#ifdef cvector_clib_malloc
19+
#undef cvector_clib_malloc
2020
#endif
21-
#ifndef cvector_clib_calloc
22-
#include <stdlib.h> /* for calloc */
23-
#define cvector_clib_calloc calloc
21+
#ifdef cvector_clib_calloc
22+
#undef cvector_clib_calloc
23+
#endif
24+
#ifdef cvector_clib_realloc
25+
#undef cvector_clib_realloc
2426
#endif
25-
#ifndef cvector_clib_realloc
26-
#include <stdlib.h> /* for realloc */
27+
#include <stdlib.h>
28+
#define cvector_clib_free free
29+
#define cvector_clib_malloc malloc
30+
#define cvector_clib_calloc calloc
2731
#define cvector_clib_realloc realloc
2832
#endif
33+
/* functions independent of memory allocation */
2934
#ifndef cvector_clib_assert
3035
#include <assert.h> /* for assert */
3136
#define cvector_clib_assert assert

0 commit comments

Comments
 (0)