20#ifndef _ACF_UTILS_SAFE_ALLOC_H_
21#define _ACF_UTILS_SAFE_ALLOC_H_
31#if defined(__cplusplus) && __cplusplus >= 201703L
58 void *p = malloc(size);
60 VERIFY_MSG(p != NULL,
"Cannot allocate %lu bytes: "
61 "out of memory", (
long unsigned)size);
73 void *p = calloc(nmemb, size);
74 if (nmemb > 0 && size > 0) {
75 VERIFY_MSG(p != NULL,
"Cannot allocate %lu bytes: "
76 "out of memory", (
long unsigned)(nmemb * size));
88 void *p = realloc(oldptr, size);
90 VERIFY_MSG(p != NULL,
"Cannot allocate %lu bytes: "
91 "out of memory", (
long unsigned)size);
96#if !APL || __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
97#if IBM || __STDC_VERSION__ >= 201112L || __cplusplus >= 201703L || \
98 _POSIX_C_SOURCE >= 200112L || defined(__DOXYGEN__)
123 ASSERT3U(alignment, >=,
sizeof (
void *));
124 ASSERT0(alignment ^ (1 << highbit64(alignment)));
126 p = _aligned_malloc(size, alignment);
127 if (size > 0 && p == NULL)
129#elif __STDC_VERSION__ >= 201112L
130 p = aligned_alloc(alignment, size);
131 if (size > 0 && p == NULL)
133#elif __cplusplus >= 201703L
134 p = std::aligned_alloc(alignment, size);
135 if (size > 0 && p == NULL)
137#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
138 err = posix_memalign(&p, alignment, size);
142 VERIFY_MSG(err == 0,
"Cannot allocate %lu bytes (align %lu): %s",
143 (
long unsigned)size, (
long unsigned)alignment, strerror(err));
208 memcpy(str, str2, l);
237 newbuf = (
char *)
safe_realloc(buf, strlen(buf) + strlen(str) + 1);
238 memcpy(&newbuf[strlen(newbuf)], str, strlen(str) + 1);
253#define ZERO_FREE(ptr) \
255 NOT_TYPE_ASSERT(ptr, void *); \
256 NOT_TYPE_ASSERT(ptr, char *); \
258 memset((ptr), 0, sizeof (*(ptr))); \
264#define DESTROY_FREE(ptr) \
275#define ZERO_FREE_N(ptr, num) \
277 NOT_TYPE_ASSERT(ptr, void *); \
279 memset((ptr), 0, sizeof (*(ptr)) * (num)); \
285#define DESTROY_FREE_N(ptr, num) \
287 ZERO_FREE_N((ptr), (num)); \
296#define BZERO(data) memset((data), 0, sizeof (*(data)))
301#define SAFE_BZERO(data) \
303 if ((data) != NULL) \
#define ASSERT3U(x, op, y)
#define VERIFY_MSG(x, fmt,...)
static char * safe_append_realloc(char *buf, const char *str)
static void aligned_free(void *ptr)
static void * safe_realloc(void *oldptr, size_t size)
static void * safe_aligned_calloc(size_t alignment, size_t nmemb, size_t size)
static void * safe_aligned_malloc(size_t alignment, size_t size)
static char * safe_strdup(const char *str2)
static void * safe_calloc(size_t nmemb, size_t size)
static void * safe_malloc(size_t size)