Libecoli 0.5.0
Extensible COmmand LIne library
Loading...
Searching...
No Matches
ecoli_malloc.h
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3 */
4
14
15#ifndef ECOLI_MALLOC_
16#define ECOLI_MALLOC_
17
18#include <sys/types.h>
19#include <stdlib.h>
20#include <string.h>
21
38typedef void *(*ec_malloc_t)(size_t size, const char *file, unsigned int line);
39
53typedef void (*ec_free_t)(void *ptr, const char *file, unsigned int line);
54
71typedef void *(*ec_realloc_t)(void *ptr, size_t size, const char *file,
72 unsigned int line);
73
94int ec_malloc_register(ec_malloc_t usr_malloc, ec_free_t usr_free,
95 ec_realloc_t usr_realloc);
96
98 ec_malloc_t malloc;
99 ec_free_t free;
100 ec_realloc_t realloc;
101};
102
104
117#define ec_malloc(size) ({ \
118 void *ret_; \
119 if (ec_malloc_handler.malloc == NULL) \
120 ret_ = malloc(size); \
121 else \
122 ret_ = __ec_malloc(size, __FILE__, __LINE__); \
123 ret_; \
124 })
125
132void *ec_malloc_func(size_t size);
133
144#define ec_free(ptr) ({ \
145 if (ec_malloc_handler.free == NULL) \
146 free(ptr); \
147 else \
148 __ec_free(ptr, __FILE__, __LINE__); \
149 })
150
157void ec_free_func(void *ptr);
158
170#define ec_realloc(ptr, size) ({ \
171 void *ret_; \
172 if (ec_malloc_handler.realloc == NULL) \
173 ret_ = realloc(ptr, size); \
174 else \
175 ret_ = __ec_realloc(ptr, size, __FILE__, __LINE__); \
176 ret_; \
177 })
178
185void ec_realloc_func(void *ptr, size_t size);
186
197#define ec_calloc(n, size) ({ \
198 void *ret_; \
199 if (ec_malloc_handler.malloc == NULL) \
200 ret_ = calloc(n, size); \
201 else \
202 ret_ = __ec_calloc(n, size, __FILE__, __LINE__); \
203 ret_; \
204 })
205
217#define ec_strdup(s) ({ \
218 void *ret_; \
219 if (ec_malloc_handler.malloc == NULL) \
220 ret_ = strdup(s); \
221 else \
222 ret_ = __ec_strdup(s, __FILE__, __LINE__); \
223 ret_; \
224 })
225
240#define ec_strndup(s, n) ({ \
241 void *ret_; \
242 if (ec_malloc_handler.malloc == NULL) \
243 ret_ = strndup(s, n); \
244 else \
245 ret_ = __ec_strndup(s, n, __FILE__, __LINE__); \
246 ret_; \
247 })
248
249/* internal */
250void *__ec_malloc(size_t size, const char *file, unsigned int line);
251void __ec_free(void *ptr, const char *file, unsigned int line);
252void *__ec_calloc(size_t nmemb, size_t size, const char *file,
253 unsigned int line);
254void *__ec_realloc(void *ptr, size_t size, const char *file, unsigned int line);
255char *__ec_strdup(const char *s, const char *file, unsigned int line);
256char *__ec_strndup(const char *s, size_t n, const char *file,
257 unsigned int line);
258
259
260#endif
261
int ec_malloc_register(ec_malloc_t usr_malloc, ec_free_t usr_free, ec_realloc_t usr_realloc)
void ec_free_func(void *ptr)
void(* ec_free_t)(void *ptr, const char *file, unsigned int line)
void *(* ec_malloc_t)(size_t size, const char *file, unsigned int line)
void * ec_malloc_func(size_t size)
void *(* ec_realloc_t)(void *ptr, size_t size, const char *file, unsigned int line)
void ec_realloc_func(void *ptr, size_t size)