Skip to content

Commit 27420df

Browse files
committed
mem: add malloc function to abort for failure
The __malloc_hook deprecated and malloc function is weak function. Then we can add malloc function to abort when failed to allocate. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent d16e8d8 commit 27420df

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

util/mem.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <unistd.h>
44
#include <malloc.h>
55
#include <string.h>
6+
#include <dlfcn.h>
67
#include <sys/mman.h>
78

89
#include "mem.h"
@@ -107,3 +108,20 @@ void nvme_free_huge(struct nvme_mem_huge *mh)
107108
mh->len = 0;
108109
mh->p = NULL;
109110
}
111+
112+
void *malloc(size_t size)
113+
{
114+
static void *(*malloc_sym)(size_t size);
115+
void *result = NULL;
116+
117+
if (!malloc_sym)
118+
malloc_sym = dlsym(RTLD_NEXT, "malloc");
119+
120+
if (malloc_sym)
121+
result = malloc_sym(size);
122+
123+
if (!result)
124+
abort();
125+
126+
return result;
127+
}

0 commit comments

Comments
 (0)