1 /* Memory allocator `malloc'. */
3 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 /* Copyright 1990, 1991, 1992 Free Software Foundation
10 Written May 1989 by Mike Haertel.
11 Heavily modified Mar 1992 by Fred Fish for mmap'd version. */
13 #include <string.h> /* Prototypes for memcpy, memmove, memset, etc */
15 #include "mmprivate.h"
17 /* Prototypes for local functions */
19 static void initialize(xbt_mheap_t mdp);
20 static void *register_morecore(xbt_mheap_t mdp, size_t size);
21 static void* mmalloc_aligned(xbt_mheap_t mdp, size_t size);
23 /* Allocation aligned on block boundary.
25 * It never returns NULL, but dies verbosely on error.
27 static void* mmalloc_aligned(struct mdesc* mdp, size_t size)
30 unsigned long int adj;
32 result = mmorecore(mdp, size);
34 /* if this reservation does not fill up the last block of our resa,
35 * complete the reservation by also asking for the full latest block.
37 * Also, the returned block is aligned to the end of block (but I've
38 * no fucking idea of why, actually -- http://abstrusegoose.com/432 --
39 * but not doing so seems to lead to issues).
41 adj = RESIDUAL(result, BLOCKSIZE);
43 adj = BLOCKSIZE - adj;
45 result = (char *) result + adj;
50 /** Initialize heapinfo about the heapinfo pages :) */
51 static void initialize_heapinfo_heapinfo(const s_xbt_mheap_t* mdp)
53 // Update heapinfo about the heapinfo pages (!):
54 mmalloc_assert((uintptr_t)mdp->heapinfo % BLOCKSIZE == 0, "Failed assert in initialize_heapinfo_heapinfo()");
55 size_t block = BLOCK(mdp->heapinfo);
56 size_t nblocks = mdp->heapsize * sizeof(malloc_info) / BLOCKSIZE;
58 for (size_t j=0; j!=nblocks; ++j) {
59 mdp->heapinfo[block+j].type = MMALLOC_TYPE_FREE;
60 mdp->heapinfo[block+j].free_block.size = 0;
61 mdp->heapinfo[block+j].free_block.next = 0;
62 mdp->heapinfo[block+j].free_block.prev = 0;
64 mdp->heapinfo[block].free_block.size = nblocks;
67 /* Finish the initialization of the mheap. If we want to inline it
68 * properly, we need to make the mmalloc_aligned function publicly visible, too */
69 static void initialize(xbt_mheap_t mdp)
71 // Update mdp meta-data:
72 mdp->heapsize = HEAP / BLOCKSIZE;
73 mdp->heapinfo = (malloc_info*)mmalloc_aligned(mdp, mdp->heapsize * sizeof(malloc_info));
74 mdp->heapbase = (void *) mdp->heapinfo;
75 mdp->flags |= MMALLOC_INITIALIZED;
77 // Update root heapinfo:
78 memset((void *) mdp->heapinfo, 0, mdp->heapsize * sizeof(malloc_info));
79 mdp->heapinfo[0].type = MMALLOC_TYPE_FREE;
80 mdp->heapinfo[0].free_block.size = 0;
81 mdp->heapinfo[0].free_block.next = mdp->heapinfo[0].free_block.prev = 0;
84 initialize_heapinfo_heapinfo(mdp);
86 for (int i = 0; i < BLOCKLOG; i++) {
87 malloc_info mi; /* to compute the offset of the swag hook */
88 xbt_swag_init(&(mdp->fraghead[i]), xbt_swag_offset(mi, freehook));
92 static inline void update_hook(void **a, size_t offset)
95 *a = (char*)*a + offset;
98 /* Get neatly aligned memory from the low level layers, and register it
99 * into the heap info table as necessary. */
100 static void *register_morecore(struct mdesc *mdp, size_t size)
102 void* result = mmalloc_aligned(mdp, size); // Never returns NULL
104 /* Check if we need to grow the info table (in a multiplicative manner) */
105 if (BLOCK((char*)result + size) > mdp->heapsize) {
106 size_t newsize = mdp->heapsize;
107 while (BLOCK((char*)result + size) > newsize)
110 /* Copy old info into new location */
111 malloc_info* oldinfo = mdp->heapinfo;
112 malloc_info* newinfo = (malloc_info*)mmalloc_aligned(mdp, newsize * sizeof(malloc_info));
113 memcpy(newinfo, oldinfo, mdp->heapsize * sizeof(malloc_info));
115 /* Initialize the new blockinfo : */
116 memset((char*) newinfo + mdp->heapsize * sizeof(malloc_info), 0,
117 (newsize - mdp->heapsize)* sizeof(malloc_info));
119 /* Update the swag of busy blocks containing free fragments by applying the offset to all swag_hooks. Yeah. My hand is right in the fan and I still type */
120 size_t offset=((char*)newinfo)-((char*)oldinfo);
122 for (size_t i = 1 /*first element of heapinfo describes the mdesc area*/; i < mdp->heaplimit; i++) {
123 update_hook(&newinfo[i].freehook.next, offset);
124 update_hook(&newinfo[i].freehook.prev, offset);
126 // also update the starting points of the swag
127 for (int i = 0; i < BLOCKLOG; i++) {
128 update_hook(&mdp->fraghead[i].head, offset);
129 update_hook(&mdp->fraghead[i].tail, offset);
131 mdp->heapinfo = newinfo;
133 /* mark the space previously occupied by the block info as free by first marking it
134 * as occupied in the regular way, and then freing it */
135 for (size_t it = 0; it < BLOCKIFY(mdp->heapsize * sizeof(malloc_info)); it++) {
136 newinfo[BLOCK(oldinfo)+it].type = MMALLOC_TYPE_UNFRAGMENTED;
137 newinfo[BLOCK(oldinfo)+it].busy_block.ignore = 0;
140 newinfo[BLOCK(oldinfo)].busy_block.size = BLOCKIFY(mdp->heapsize * sizeof(malloc_info));
141 newinfo[BLOCK(oldinfo)].busy_block.busy_size = size;
142 mfree(mdp, (void *) oldinfo);
143 mdp->heapsize = newsize;
145 initialize_heapinfo_heapinfo(mdp);
148 mdp->heaplimit = BLOCK((char *) result + size);
152 /* Allocate memory from the heap. */
153 void *mmalloc(xbt_mheap_t mdp, size_t size) {
154 void *res= mmalloc_no_memset(mdp,size);
155 if (mdp->options & XBT_MHEAP_OPTION_MEMSET) {
161 static void mmalloc_mark_used(xbt_mheap_t mdp, size_t block, size_t nblocks, size_t requested_size)
163 for (size_t it = 0; it < nblocks; it++) {
164 mdp->heapinfo[block + it].type = MMALLOC_TYPE_UNFRAGMENTED;
165 mdp->heapinfo[block + it].busy_block.busy_size = 0;
166 mdp->heapinfo[block + it].busy_block.ignore = 0;
167 mdp->heapinfo[block + it].busy_block.size = 0;
169 mdp->heapinfo[block].busy_block.size = nblocks;
170 mdp->heapinfo[block].busy_block.busy_size = requested_size;
171 mdp->heapstats.chunks_used++;
172 mdp->heapstats.bytes_used += nblocks * BLOCKSIZE;
175 /* Splitting mmalloc this way is mandated by a trick in mrealloc, that gives
176 back the memory of big blocks to the system before reallocating them: we don't
177 want to loose the beginning of the area when this happens */
178 void *mmalloc_no_memset(xbt_mheap_t mdp, size_t size)
183 size_t requested_size = size; // The amount of memory requested by user, for real
185 /* Work even if the user was stupid enough to ask a ridiculously small block (even 0-length),
186 * ie return a valid block that can be realloced and freed.
187 * glibc malloc does not use this trick but return a constant pointer, but we need to enlist the free fragments later on.
189 if (size < SMALLEST_POSSIBLE_MALLOC)
190 size = SMALLEST_POSSIBLE_MALLOC;
192 if (!(mdp->flags & MMALLOC_INITIALIZED))
195 /* Determine the allocation policy based on the request size. */
196 if (size <= BLOCKSIZE / 2) {
197 /* Small allocation to receive a fragment of a block.
198 Determine the logarithm to base two of the fragment size. */
201 while ((size /= 2) != 0) {
205 /* Look in the fragment lists for a free fragment of the desired size. */
206 if (xbt_swag_size(&mdp->fraghead[log])>0) {
207 /* There are free fragments of this size; Get one of them and prepare to return it.
208 Update the block's nfree and if no other free fragment, get out of the swag. */
210 /* search a fragment that I could return as a result */
211 malloc_info *candidate_info = xbt_swag_getFirst(&mdp->fraghead[log]);
212 size_t candidate_block = (candidate_info - &(mdp->heapinfo[0]));
213 size_t candidate_frag;
214 for (candidate_frag=0;candidate_frag<(size_t) (BLOCKSIZE >> log);candidate_frag++)
215 if (candidate_info->busy_frag.frag_size[candidate_frag] == -1)
217 mmalloc_assert(candidate_frag < (size_t)(BLOCKSIZE >> log),
218 "Block %zu was registered as containing free fragments of type %zu, but I can't find any",
219 candidate_block, log);
221 result = (void*) (((char*)ADDRESS(candidate_block)) + (candidate_frag << log));
223 /* Remove this fragment from the list of free guys */
224 candidate_info->busy_frag.nfree--;
225 if (candidate_info->busy_frag.nfree == 0) {
226 xbt_swag_remove(candidate_info,&mdp->fraghead[log]);
229 /* Update our metadata about this fragment */
230 candidate_info->busy_frag.frag_size[candidate_frag] = requested_size;
231 candidate_info->busy_frag.ignore[candidate_frag] = 0;
233 /* Update the statistics. */
234 mdp -> heapstats.chunks_used++;
235 mdp -> heapstats.bytes_used += 1 << log;
236 mdp -> heapstats.chunks_free--;
237 mdp -> heapstats.bytes_free -= 1 << log;
240 /* No free fragments of the desired size, so get a new block
241 and break it into fragments, returning the first. */
243 result = mmalloc(mdp, BLOCKSIZE); // does not return NULL
244 block = BLOCK(result);
246 mdp->heapinfo[block].type = (int)log;
247 /* Link all fragments but the first as free, and add the block to the swag of blocks containing free frags */
249 for (i = 1; i < (size_t) (BLOCKSIZE >> log); ++i) {
250 mdp->heapinfo[block].busy_frag.frag_size[i] = -1;
251 mdp->heapinfo[block].busy_frag.ignore[i] = 0;
253 mdp->heapinfo[block].busy_frag.nfree = i - 1;
254 mdp->heapinfo[block].freehook.prev = NULL;
255 mdp->heapinfo[block].freehook.next = NULL;
257 xbt_swag_insert(&mdp->heapinfo[block], &(mdp->fraghead[log]));
259 /* mark the fragment returned as busy */
260 mdp->heapinfo[block].busy_frag.frag_size[0] = requested_size;
261 mdp->heapinfo[block].busy_frag.ignore[0] = 0;
264 mdp -> heapstats.chunks_free += (BLOCKSIZE >> log) - 1;
265 mdp -> heapstats.bytes_free += BLOCKSIZE - (1 << log);
266 mdp -> heapstats.bytes_used -= BLOCKSIZE - (1 << log);
269 /* Large allocation to receive one or more blocks.
270 Search the free list in a circle starting at the last place visited.
271 If we loop completely around without finding a large enough
272 space we will have to get more memory from the system. */
273 size_t blocks = BLOCKIFY(size);
274 size_t start = MALLOC_SEARCH_START;
275 block = MALLOC_SEARCH_START;
276 while (mdp->heapinfo[block].free_block.size < blocks) {
277 if (mdp->heapinfo[block].type >=0) { // Don't trust xbt_die and friends in malloc-level library, you fool!
279 "Internal error: found a free block not marked as such (block=%zu type=%d). Please report this bug.\n",
280 block, mdp->heapinfo[block].type);
284 block = mdp->heapinfo[block].free_block.next;
285 if (block == start) {
286 /* Need to get more from the system. Check to see if
287 the new core will be contiguous with the final free
288 block; if so we don't need to get as much. */
289 block = mdp->heapinfo[0].free_block.prev;
290 size_t lastblocks = mdp->heapinfo[block].free_block.size;
291 if (mdp->heaplimit != 0 &&
292 block + lastblocks == mdp->heaplimit &&
293 mmorecore(mdp, 0) == ADDRESS(block + lastblocks) &&
294 (register_morecore(mdp, (blocks - lastblocks) * BLOCKSIZE)) != NULL) {
295 /* Which block we are extending (the `final free
296 block' referred to above) might have changed, if
297 it got combined with a freed info table. */
298 block = mdp->heapinfo[0].free_block.prev;
300 mdp->heapinfo[block].free_block.size += (blocks - lastblocks);
303 result = register_morecore(mdp, blocks * BLOCKSIZE);
305 block = BLOCK(result);
306 mmalloc_mark_used(mdp, block, blocks, requested_size);
310 /* Need large block(s), but found some in the existing heap */
313 /* At this point we have found a suitable free list entry.
314 Figure out how to remove what we need from the list. */
315 result = ADDRESS(block);
316 if (mdp->heapinfo[block].free_block.size > blocks) {
317 /* The block we found has a bit left over,
318 so relink the tail end back into the free list. */
319 mdp->heapinfo[block + blocks].free_block.size
320 = mdp->heapinfo[block].free_block.size - blocks;
321 mdp->heapinfo[block + blocks].free_block.next
322 = mdp->heapinfo[block].free_block.next;
323 mdp->heapinfo[block + blocks].free_block.prev
324 = mdp->heapinfo[block].free_block.prev;
325 mdp->heapinfo[mdp->heapinfo[block].free_block.prev].free_block.next
326 = mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev
327 = mdp->heapindex = block + blocks;
329 /* The block exactly matches our requirements,
330 so just remove it from the list. */
331 mdp->heapinfo[mdp->heapinfo[block].free_block.next].free_block.prev
332 = mdp->heapinfo[block].free_block.prev;
333 mdp->heapinfo[mdp->heapinfo[block].free_block.prev].free_block.next
334 = mdp->heapindex = mdp->heapinfo[block].free_block.next;
337 mmalloc_mark_used(mdp, block, blocks, requested_size);
338 mdp -> heapstats.bytes_free -= blocks * BLOCKSIZE;