-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
57 lines (45 loc) · 1.33 KB
/
memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#if !defined(NN2_MEMORY)
#define NN2_MEMORY
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "logger.h"
/*
* memory allocation wrappers
* These are lightweight and should catch the common allocation problems
* -- One could add pretty easily hooks to a debugging malloc in here
* -- or a garbage collector. ;)
*/
static __inline void *mem_alloc(size_t size) {
register void *ptr;
if ((ptr = malloc(size)) == NULL)
Log(LOG_ERROR, "%s:malloc %d:%s", __FUNCTION__, errno, strerror(errno));
memset(ptr, 0, size);
return ptr;
}
static __inline void *mem_realloc(void *ptr, size_t size) {
register void *nptr;
if ((nptr = realloc(ptr, size)) == NULL)
Log(LOG_ERROR, "%s:realloc %d:%s", __FUNCTION__, errno, strerror(errno));
return nptr;
}
static __inline void mem_free(void *ptr) {
/*
* Force a crash if double free or a NULL ptr, should aid debugging
*/
assert(ptr != NULL);
free(ptr);
ptr = NULL;
return;
}
static __inline void *mem_calloc(size_t nmemb, size_t size) {
register void *p;
assert(nmemb != 0);
assert(size != 0);
if ((p = calloc(nmemb, size)) == NULL)
Log(LOG_ERROR, "%s:calloc %d:%s", __FUNCTION__, errno, strerror(errno));
return p;
}
#endif /* !defined(NN2_MEMORY) */