MCPcopy Create free account
hub / github.com/acl-dev/acl / mymalloc

Function mymalloc

test/libmm/memalloc.cpp:88–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86}
87
88void *mymalloc(size_t size)
89{
90 size_t total_size;
91 void *block;
92 header_t *header;
93 if (!size)
94 return NULL;
95 pthread_mutex_lock(&global_malloc_lock);
96 header = get_free_block(size);
97 if (header) {
98 /* Woah, found a free block to accomodate requested memory. */
99 header->s.is_free = 0;
100 pthread_mutex_unlock(&global_malloc_lock);
101 return (void*)(header + 1);
102 }
103 /* We need to get memory to fit in the requested block and header from OS. */
104 total_size = sizeof(header_t) + size;
105 block = sbrk(total_size);
106 if (block == (void*) -1) {
107 pthread_mutex_unlock(&global_malloc_lock);
108 return NULL;
109 }
110 header = (header_t*) block;
111 header->s.size = size;
112 header->s.is_free = 0;
113 header->s.next = NULL;
114 if (!head)
115 head = header;
116 if (tail)
117 tail->s.next = header;
118 tail = header;
119 pthread_mutex_unlock(&global_malloc_lock);
120 return (void*)(header + 1);
121}
122
123void *calloc(size_t num, size_t nsize)
124{

Callers 3

callocFunction · 0.85
reallocFunction · 0.85
mallocFunction · 0.85

Calls 3

pthread_mutex_lockFunction · 0.85
get_free_blockFunction · 0.85
pthread_mutex_unlockFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…