1 #ifdef HOOK_ENABLE
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include "musl_malloc.h"
5 #include <malloc.h>
6 #include "musl_malloc_dispatch_table.h"
7 #include "common_def.h"
8 #include "musl_preinit_common.h"
9
malloc(size_t bytes)10 void* malloc(size_t bytes)
11 {
12 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
13 if (__predict_false(dispatch_table != NULL)) {
14 void*ret = dispatch_table->malloc(bytes);
15 return ret;
16 }
17 void* result = MuslMalloc(malloc)(bytes);
18 if (__predict_false(result == NULL)) {
19 //__musl_log(__MUSL_LOG_WARN, "malloc(%zu) failed: returning null pointer\n", bytes);
20 }
21 return result;
22 }
23
free(void * mem)24 void free(void* mem)
25 {
26 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
27 if (__predict_false(dispatch_table != NULL)) {
28 dispatch_table->free(mem);
29 } else {
30 MuslMalloc(free)(mem);
31 }
32 }
33
mmap(void * addr,size_t length,int prot,int flags,int fd,off_t offset)34 void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
35 {
36 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
37 if (__predict_false(dispatch_table != NULL)) {
38 return dispatch_table->mmap(addr, length, prot, flags, fd, offset);
39 } else {
40 return MuslMalloc(mmap)(addr, length, prot, flags, fd, offset);
41 }
42 }
43
munmap(void * addr,size_t length)44 int munmap(void* addr, size_t length)
45 {
46 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
47 if (__predict_false(dispatch_table != NULL)) {
48 return dispatch_table->munmap(addr, length);
49 } else {
50 return MuslMalloc(munmap)(addr, length);
51 }
52 }
53
calloc(size_t m,size_t n)54 void* calloc(size_t m, size_t n)
55 {
56 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
57 if (__predict_false(dispatch_table != NULL)) {
58 return dispatch_table->calloc(m, n);
59 } else {
60 return MuslMalloc(calloc)(m, n);
61 }
62 }
63
realloc(void * p,size_t n)64 void* realloc(void *p, size_t n)
65 {
66 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
67 if (__predict_false(dispatch_table != NULL)) {
68 return dispatch_table->realloc(p, n);
69 } else {
70 return MuslMalloc(realloc)(p, n);
71 }
72 }
73
malloc_usable_size(void * addr)74 size_t malloc_usable_size(void* addr)
75 {
76 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
77 if (__predict_false(dispatch_table != NULL)) {
78 return dispatch_table->malloc_usable_size(addr);
79 } else {
80 return MuslMalloc(malloc_usable_size)(addr);
81 }
82 }
83
mallopt(int param,int value)84 int mallopt(int param, int value)
85 {
86 volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
87 if (__predict_false(dispatch_table != NULL)) {
88 return dispatch_table->mallopt(param, value);
89 } else {
90 return MuslMalloc(mallopt)(param, value);
91 }
92 }
93 #endif
94