| 86 | } |
| 87 | |
| 88 | void *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 | |
| 123 | void *calloc(size_t num, size_t nsize) |
| 124 | { |
no test coverage detected
searching dependent graphs…