blob: da0eab3a4c2a8d6aaf3d691698efb0915760e8b9 [file] [log] [blame]
Heinrich Schuchardt5ad92202021-05-29 13:18:00 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * This code is based on a version (aka dlmalloc) of malloc/free/realloc written
4 * by Doug Lea and released to the public domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/-
6 *
7 * The original code is available at http://gee.cs.oswego.edu/pub/misc/
8 * as file malloc-2.6.6.c.
9 */
10
Kumar Gala81673e92008-05-13 19:01:54 -050011#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Kumar Gala81673e92008-05-13 19:01:54 -050014
Heinrich Schuchardtbe621c12020-04-15 18:46:23 +020015#if CONFIG_IS_ENABLED(UNIT_TEST)
Simon Glass6d7601e2014-07-10 22:23:33 -060016#define DEBUG
17#endif
18
wdenk217c9da2002-10-25 20:35:49 +000019#include <malloc.h>
Simon Glassd59476b2014-07-10 22:23:28 -060020#include <asm/io.h>
Sean Andersonbdaeea12022-03-23 14:04:49 -040021#include <valgrind/memcheck.h>
Simon Glassd59476b2014-07-10 22:23:28 -060022
Wolfgang Denkea882ba2010-06-20 23:33:59 +020023#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +000024#if __STD_C
25static void malloc_update_mallinfo (void);
26void malloc_stats (void);
27#else
28static void malloc_update_mallinfo ();
29void malloc_stats();
30#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020031#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +000032
Bo Lv96a66d02023-05-12 19:18:22 +080033#ifdef CONFIG_AML_UASAN
34#include <amlogic/uasan.h>
35#endif
36
Wolfgang Denkd87080b2006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
wdenk217c9da2002-10-25 20:35:49 +000039/*
40 Emulation of sbrk for WIN32
41 All code within the ifdef WIN32 is untested by me.
42
43 Thanks to Martin Fong and others for supplying this.
44*/
45
46
47#ifdef WIN32
48
49#define AlignPage(add) (((add) + (malloc_getpagesize-1)) & \
50~(malloc_getpagesize-1))
51#define AlignPage64K(add) (((add) + (0x10000 - 1)) & ~(0x10000 - 1))
52
53/* resrve 64MB to insure large contiguous space */
54#define RESERVED_SIZE (1024*1024*64)
55#define NEXT_SIZE (2048*1024)
56#define TOP_MEMORY ((unsigned long)2*1024*1024*1024)
57
58struct GmListElement;
59typedef struct GmListElement GmListElement;
60
61struct GmListElement
62{
63 GmListElement* next;
64 void* base;
65};
66
67static GmListElement* head = 0;
68static unsigned int gNextAddress = 0;
69static unsigned int gAddressBase = 0;
70static unsigned int gAllocatedSize = 0;
71
72static
73GmListElement* makeGmListElement (void* bas)
74{
75 GmListElement* this;
76 this = (GmListElement*)(void*)LocalAlloc (0, sizeof (GmListElement));
77 assert (this);
78 if (this)
79 {
80 this->base = bas;
81 this->next = head;
82 head = this;
83 }
84 return this;
85}
86
87void gcleanup ()
88{
89 BOOL rval;
90 assert ( (head == NULL) || (head->base == (void*)gAddressBase));
91 if (gAddressBase && (gNextAddress - gAddressBase))
92 {
93 rval = VirtualFree ((void*)gAddressBase,
94 gNextAddress - gAddressBase,
95 MEM_DECOMMIT);
wdenk8bde7f72003-06-27 21:31:46 +000096 assert (rval);
wdenk217c9da2002-10-25 20:35:49 +000097 }
98 while (head)
99 {
100 GmListElement* next = head->next;
101 rval = VirtualFree (head->base, 0, MEM_RELEASE);
102 assert (rval);
103 LocalFree (head);
104 head = next;
105 }
106}
107
108static
109void* findRegion (void* start_address, unsigned long size)
110{
111 MEMORY_BASIC_INFORMATION info;
112 if (size >= TOP_MEMORY) return NULL;
113
114 while ((unsigned long)start_address + size < TOP_MEMORY)
115 {
116 VirtualQuery (start_address, &info, sizeof (info));
117 if ((info.State == MEM_FREE) && (info.RegionSize >= size))
118 return start_address;
119 else
120 {
wdenk8bde7f72003-06-27 21:31:46 +0000121 /* Requested region is not available so see if the */
122 /* next region is available. Set 'start_address' */
123 /* to the next region and call 'VirtualQuery()' */
124 /* again. */
wdenk217c9da2002-10-25 20:35:49 +0000125
126 start_address = (char*)info.BaseAddress + info.RegionSize;
127
wdenk8bde7f72003-06-27 21:31:46 +0000128 /* Make sure we start looking for the next region */
129 /* on the *next* 64K boundary. Otherwise, even if */
130 /* the new region is free according to */
131 /* 'VirtualQuery()', the subsequent call to */
132 /* 'VirtualAlloc()' (which follows the call to */
133 /* this routine in 'wsbrk()') will round *down* */
134 /* the requested address to a 64K boundary which */
135 /* we already know is an address in the */
136 /* unavailable region. Thus, the subsequent call */
137 /* to 'VirtualAlloc()' will fail and bring us back */
138 /* here, causing us to go into an infinite loop. */
wdenk217c9da2002-10-25 20:35:49 +0000139
140 start_address =
141 (void *) AlignPage64K((unsigned long) start_address);
142 }
143 }
144 return NULL;
145
146}
147
148
149void* wsbrk (long size)
150{
151 void* tmp;
152 if (size > 0)
153 {
154 if (gAddressBase == 0)
155 {
156 gAllocatedSize = max (RESERVED_SIZE, AlignPage (size));
157 gNextAddress = gAddressBase =
158 (unsigned int)VirtualAlloc (NULL, gAllocatedSize,
159 MEM_RESERVE, PAGE_NOACCESS);
160 } else if (AlignPage (gNextAddress + size) > (gAddressBase +
161gAllocatedSize))
162 {
163 long new_size = max (NEXT_SIZE, AlignPage (size));
164 void* new_address = (void*)(gAddressBase+gAllocatedSize);
165 do
166 {
167 new_address = findRegion (new_address, new_size);
168
Heinrich Schuchardta874cac2017-11-10 21:46:34 +0100169 if (!new_address)
wdenk217c9da2002-10-25 20:35:49 +0000170 return (void*)-1;
171
172 gAddressBase = gNextAddress =
173 (unsigned int)VirtualAlloc (new_address, new_size,
174 MEM_RESERVE, PAGE_NOACCESS);
wdenk8bde7f72003-06-27 21:31:46 +0000175 /* repeat in case of race condition */
176 /* The region that we found has been snagged */
177 /* by another thread */
wdenk217c9da2002-10-25 20:35:49 +0000178 }
179 while (gAddressBase == 0);
180
181 assert (new_address == (void*)gAddressBase);
182
183 gAllocatedSize = new_size;
184
185 if (!makeGmListElement ((void*)gAddressBase))
186 return (void*)-1;
187 }
188 if ((size + gNextAddress) > AlignPage (gNextAddress))
189 {
190 void* res;
191 res = VirtualAlloc ((void*)AlignPage (gNextAddress),
192 (size + gNextAddress -
193 AlignPage (gNextAddress)),
194 MEM_COMMIT, PAGE_READWRITE);
Heinrich Schuchardta874cac2017-11-10 21:46:34 +0100195 if (!res)
wdenk217c9da2002-10-25 20:35:49 +0000196 return (void*)-1;
197 }
198 tmp = (void*)gNextAddress;
199 gNextAddress = (unsigned int)tmp + size;
200 return tmp;
201 }
202 else if (size < 0)
203 {
204 unsigned int alignedGoal = AlignPage (gNextAddress + size);
205 /* Trim by releasing the virtual memory */
206 if (alignedGoal >= gAddressBase)
207 {
208 VirtualFree ((void*)alignedGoal, gNextAddress - alignedGoal,
209 MEM_DECOMMIT);
210 gNextAddress = gNextAddress + size;
211 return (void*)gNextAddress;
212 }
213 else
214 {
215 VirtualFree ((void*)gAddressBase, gNextAddress - gAddressBase,
216 MEM_DECOMMIT);
217 gNextAddress = gAddressBase;
218 return (void*)-1;
219 }
220 }
221 else
222 {
223 return (void*)gNextAddress;
224 }
225}
226
227#endif
228
Simon Glassd93041a2014-07-10 22:23:25 -0600229
wdenk217c9da2002-10-25 20:35:49 +0000230
231/*
232 Type declarations
233*/
234
235
236struct malloc_chunk
237{
238 INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
239 INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
240 struct malloc_chunk* fd; /* double links -- used only if free. */
241 struct malloc_chunk* bk;
Joakim Tjernlund1ba91ba2010-10-14 08:51:34 +0200242} __attribute__((__may_alias__)) ;
wdenk217c9da2002-10-25 20:35:49 +0000243
244typedef struct malloc_chunk* mchunkptr;
245
246/*
247
248 malloc_chunk details:
249
250 (The following includes lightly edited explanations by Colin Plumb.)
251
252 Chunks of memory are maintained using a `boundary tag' method as
253 described in e.g., Knuth or Standish. (See the paper by Paul
254 Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
255 survey of such techniques.) Sizes of free chunks are stored both
256 in the front of each chunk and at the end. This makes
257 consolidating fragmented chunks into bigger chunks very fast. The
258 size fields also hold bits representing whether chunks are free or
259 in use.
260
261 An allocated chunk looks like this:
262
263
264 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000265 | Size of previous chunk, if allocated | |
266 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
267 | Size of chunk, in bytes |P|
wdenk217c9da2002-10-25 20:35:49 +0000268 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000269 | User data starts here... .
270 . .
271 . (malloc_usable_space() bytes) .
272 . |
wdenk217c9da2002-10-25 20:35:49 +0000273nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000274 | Size of chunk |
275 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk217c9da2002-10-25 20:35:49 +0000276
277
278 Where "chunk" is the front of the chunk for the purpose of most of
279 the malloc code, but "mem" is the pointer that is returned to the
280 user. "Nextchunk" is the beginning of the next contiguous chunk.
281
282 Chunks always begin on even word boundries, so the mem portion
283 (which is returned to the user) is also on an even word boundary, and
284 thus double-word aligned.
285
286 Free chunks are stored in circular doubly-linked lists, and look like this:
287
288 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000289 | Size of previous chunk |
290 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk217c9da2002-10-25 20:35:49 +0000291 `head:' | Size of chunk, in bytes |P|
292 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000293 | Forward pointer to next chunk in list |
294 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
295 | Back pointer to previous chunk in list |
296 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
297 | Unused space (may be 0 bytes long) .
298 . .
299 . |
Marek Bykowski9297e362020-04-29 18:23:07 +0200300
wdenk217c9da2002-10-25 20:35:49 +0000301nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
302 `foot:' | Size of chunk, in bytes |
wdenk8bde7f72003-06-27 21:31:46 +0000303 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk217c9da2002-10-25 20:35:49 +0000304
305 The P (PREV_INUSE) bit, stored in the unused low-order bit of the
306 chunk size (which is always a multiple of two words), is an in-use
307 bit for the *previous* chunk. If that bit is *clear*, then the
308 word before the current chunk size contains the previous chunk
309 size, and can be used to find the front of the previous chunk.
310 (The very first chunk allocated always has this bit set,
311 preventing access to non-existent (or non-owned) memory.)
312
313 Note that the `foot' of the current chunk is actually represented
314 as the prev_size of the NEXT chunk. (This makes it easier to
315 deal with alignments etc).
316
317 The two exceptions to all this are
318
319 1. The special chunk `top', which doesn't bother using the
wdenk8bde7f72003-06-27 21:31:46 +0000320 trailing size field since there is no
321 next contiguous chunk that would have to index off it. (After
322 initialization, `top' is forced to always exist. If it would
323 become less than MINSIZE bytes long, it is replenished via
324 malloc_extend_top.)
wdenk217c9da2002-10-25 20:35:49 +0000325
326 2. Chunks allocated via mmap, which have the second-lowest-order
wdenk8bde7f72003-06-27 21:31:46 +0000327 bit (IS_MMAPPED) set in their size fields. Because they are
328 never merged or traversed from any other chunk, they have no
329 foot size or inuse information.
wdenk217c9da2002-10-25 20:35:49 +0000330
331 Available chunks are kept in any of several places (all declared below):
332
333 * `av': An array of chunks serving as bin headers for consolidated
334 chunks. Each bin is doubly linked. The bins are approximately
335 proportionally (log) spaced. There are a lot of these bins
336 (128). This may look excessive, but works very well in
337 practice. All procedures maintain the invariant that no
338 consolidated chunk physically borders another one. Chunks in
339 bins are kept in size order, with ties going to the
340 approximately least recently used chunk.
341
342 The chunks in each bin are maintained in decreasing sorted order by
343 size. This is irrelevant for the small bins, which all contain
344 the same-sized chunks, but facilitates best-fit allocation for
345 larger chunks. (These lists are just sequential. Keeping them in
346 order almost never requires enough traversal to warrant using
347 fancier ordered data structures.) Chunks of the same size are
348 linked with the most recently freed at the front, and allocations
349 are taken from the back. This results in LRU or FIFO allocation
350 order, which tends to give each chunk an equal opportunity to be
351 consolidated with adjacent freed chunks, resulting in larger free
352 chunks and less fragmentation.
353
354 * `top': The top-most available chunk (i.e., the one bordering the
355 end of available memory) is treated specially. It is never
356 included in any bin, is used only if no other chunk is
357 available, and is released back to the system if it is very
358 large (see M_TRIM_THRESHOLD).
359
360 * `last_remainder': A bin holding only the remainder of the
361 most recently split (non-top) chunk. This bin is checked
362 before other non-fitting chunks, so as to provide better
363 locality for runs of sequentially allocated chunks.
364
365 * Implicitly, through the host system's memory mapping tables.
366 If supported, requests greater than a threshold are usually
367 serviced via calls to mmap, and then later released via munmap.
368
369*/
Simon Glassd93041a2014-07-10 22:23:25 -0600370
wdenk217c9da2002-10-25 20:35:49 +0000371/* sizes, alignments */
372
373#define SIZE_SZ (sizeof(INTERNAL_SIZE_T))
374#define MALLOC_ALIGNMENT (SIZE_SZ + SIZE_SZ)
375#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
376#define MINSIZE (sizeof(struct malloc_chunk))
377
378/* conversion from malloc headers to user pointers, and back */
379
Bo Lv96a66d02023-05-12 19:18:22 +0800380#ifdef CONFIG_AML_UASAN
381
382#define chunk2mem(p) ((Void_t *)((char *)(p) + \
383 (2 * SIZE_SZ + UASAN_ALLOCA_REDZONE_SIZE)))
384#define mem2chunk(mem) ((mchunkptr)((char *)(mem) - \
385 (2 * SIZE_SZ + UASAN_ALLOCA_REDZONE_SIZE)))
386
387/* insert red zone when get alloc size */
388#define request2size(req) \
389 ((((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \
390 (long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \
391 (((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK))) + \
392 UASAN_ALLOCA_REDZONE_SIZE * 2)
393
394#else /* CONFIG_AML_UASAN */
395
wdenk217c9da2002-10-25 20:35:49 +0000396#define chunk2mem(p) ((Void_t*)((char*)(p) + 2*SIZE_SZ))
397#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
398
399/* pad request bytes into a usable size */
400
401#define request2size(req) \
402 (((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \
403 (long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \
404 (((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK)))
Bo Lv96a66d02023-05-12 19:18:22 +0800405#endif /* CONFIG_AML_UASAN */
wdenk217c9da2002-10-25 20:35:49 +0000406
407/* Check if m has acceptable alignment */
408
409#define aligned_OK(m) (((unsigned long)((m)) & (MALLOC_ALIGN_MASK)) == 0)
410
411
Simon Glassd93041a2014-07-10 22:23:25 -0600412
wdenk217c9da2002-10-25 20:35:49 +0000413
414/*
415 Physical chunk operations
416*/
417
418
419/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
420
421#define PREV_INUSE 0x1
422
423/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
424
425#define IS_MMAPPED 0x2
426
427/* Bits to mask off when extracting size */
428
429#define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
430
431
432/* Ptr to next physical malloc_chunk. */
433
434#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
435
436/* Ptr to previous physical malloc_chunk */
437
438#define prev_chunk(p)\
439 ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
440
441
442/* Treat space at ptr + offset as a chunk */
443
444#define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s)))
445
446
Simon Glassd93041a2014-07-10 22:23:25 -0600447
wdenk217c9da2002-10-25 20:35:49 +0000448
449/*
450 Dealing with use bits
451*/
452
453/* extract p's inuse bit */
454
455#define inuse(p)\
456((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
457
458/* extract inuse bit of previous chunk */
459
460#define prev_inuse(p) ((p)->size & PREV_INUSE)
461
462/* check for mmap()'ed chunk */
463
464#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
465
466/* set/clear chunk as in use without otherwise disturbing */
467
468#define set_inuse(p)\
469((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
470
471#define clear_inuse(p)\
472((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
473
474/* check/set/clear inuse bits in known places */
475
476#define inuse_bit_at_offset(p, s)\
477 (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
478
479#define set_inuse_bit_at_offset(p, s)\
480 (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
481
482#define clear_inuse_bit_at_offset(p, s)\
483 (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
484
485
Simon Glassd93041a2014-07-10 22:23:25 -0600486
wdenk217c9da2002-10-25 20:35:49 +0000487
488/*
489 Dealing with size fields
490*/
491
492/* Get size, ignoring use bits */
493
494#define chunksize(p) ((p)->size & ~(SIZE_BITS))
495
496/* Set size at head, without disturbing its use bit */
497
498#define set_head_size(p, s) ((p)->size = (((p)->size & PREV_INUSE) | (s)))
499
500/* Set size/use ignoring previous bits in header */
501
502#define set_head(p, s) ((p)->size = (s))
503
504/* Set size at footer (only when chunk is not in use) */
505
506#define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
507
508
Simon Glassd93041a2014-07-10 22:23:25 -0600509
wdenk217c9da2002-10-25 20:35:49 +0000510
511
512/*
513 Bins
514
515 The bins, `av_' are an array of pairs of pointers serving as the
516 heads of (initially empty) doubly-linked lists of chunks, laid out
517 in a way so that each pair can be treated as if it were in a
518 malloc_chunk. (This way, the fd/bk offsets for linking bin heads
519 and chunks are the same).
520
521 Bins for sizes < 512 bytes contain chunks of all the same size, spaced
522 8 bytes apart. Larger bins are approximately logarithmically
523 spaced. (See the table below.) The `av_' array is never mentioned
524 directly in the code, but instead via bin access macros.
525
526 Bin layout:
527
528 64 bins of size 8
529 32 bins of size 64
530 16 bins of size 512
531 8 bins of size 4096
532 4 bins of size 32768
533 2 bins of size 262144
534 1 bin of size what's left
535
536 There is actually a little bit of slop in the numbers in bin_index
537 for the sake of speed. This makes no difference elsewhere.
538
539 The special chunks `top' and `last_remainder' get their own bins,
540 (this is implemented via yet more trickery with the av_ array),
541 although `top' is never properly linked to its bin since it is
542 always handled specially.
543
544*/
545
546#define NAV 128 /* number of bins */
547
548typedef struct malloc_chunk* mbinptr;
549
550/* access macros */
551
552#define bin_at(i) ((mbinptr)((char*)&(av_[2*(i) + 2]) - 2*SIZE_SZ))
553#define next_bin(b) ((mbinptr)((char*)(b) + 2 * sizeof(mbinptr)))
554#define prev_bin(b) ((mbinptr)((char*)(b) - 2 * sizeof(mbinptr)))
555
556/*
557 The first 2 bins are never indexed. The corresponding av_ cells are instead
558 used for bookkeeping. This is not to save space, but to simplify
559 indexing, maintain locality, and avoid some initialization tests.
560*/
561
Stefan Roesef2302d42008-08-06 14:05:38 +0200562#define top (av_[2]) /* The topmost chunk */
wdenk217c9da2002-10-25 20:35:49 +0000563#define last_remainder (bin_at(1)) /* remainder from last split */
564
565
566/*
567 Because top initially points to its own bin with initial
568 zero size, thus forcing extension on the first malloc request,
569 we avoid having any special code in malloc to check whether
570 it even exists yet. But we still need to in malloc_extend_top.
571*/
572
573#define initial_top ((mchunkptr)(bin_at(0)))
574
575/* Helper macro to initialize bins */
576
577#define IAV(i) bin_at(i), bin_at(i)
578
579static mbinptr av_[NAV * 2 + 2] = {
Kim Phillips199adb62012-10-29 13:34:32 +0000580 NULL, NULL,
wdenk217c9da2002-10-25 20:35:49 +0000581 IAV(0), IAV(1), IAV(2), IAV(3), IAV(4), IAV(5), IAV(6), IAV(7),
582 IAV(8), IAV(9), IAV(10), IAV(11), IAV(12), IAV(13), IAV(14), IAV(15),
583 IAV(16), IAV(17), IAV(18), IAV(19), IAV(20), IAV(21), IAV(22), IAV(23),
584 IAV(24), IAV(25), IAV(26), IAV(27), IAV(28), IAV(29), IAV(30), IAV(31),
585 IAV(32), IAV(33), IAV(34), IAV(35), IAV(36), IAV(37), IAV(38), IAV(39),
586 IAV(40), IAV(41), IAV(42), IAV(43), IAV(44), IAV(45), IAV(46), IAV(47),
587 IAV(48), IAV(49), IAV(50), IAV(51), IAV(52), IAV(53), IAV(54), IAV(55),
588 IAV(56), IAV(57), IAV(58), IAV(59), IAV(60), IAV(61), IAV(62), IAV(63),
589 IAV(64), IAV(65), IAV(66), IAV(67), IAV(68), IAV(69), IAV(70), IAV(71),
590 IAV(72), IAV(73), IAV(74), IAV(75), IAV(76), IAV(77), IAV(78), IAV(79),
591 IAV(80), IAV(81), IAV(82), IAV(83), IAV(84), IAV(85), IAV(86), IAV(87),
592 IAV(88), IAV(89), IAV(90), IAV(91), IAV(92), IAV(93), IAV(94), IAV(95),
593 IAV(96), IAV(97), IAV(98), IAV(99), IAV(100), IAV(101), IAV(102), IAV(103),
594 IAV(104), IAV(105), IAV(106), IAV(107), IAV(108), IAV(109), IAV(110), IAV(111),
595 IAV(112), IAV(113), IAV(114), IAV(115), IAV(116), IAV(117), IAV(118), IAV(119),
596 IAV(120), IAV(121), IAV(122), IAV(123), IAV(124), IAV(125), IAV(126), IAV(127)
597};
598
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200599#ifdef CONFIG_NEEDS_MANUAL_RELOC
Gabor Juhos7b395232013-01-21 21:10:38 +0000600static void malloc_bin_reloc(void)
wdenk217c9da2002-10-25 20:35:49 +0000601{
Simon Glass93691842012-09-04 11:31:07 +0000602 mbinptr *p = &av_[2];
603 size_t i;
604
605 for (i = 2; i < ARRAY_SIZE(av_); ++i, ++p)
606 *p = (mbinptr)((ulong)*p + gd->reloc_off);
wdenk217c9da2002-10-25 20:35:49 +0000607}
Gabor Juhos7b395232013-01-21 21:10:38 +0000608#else
609static inline void malloc_bin_reloc(void) {}
Peter Tyser521af042009-09-21 11:20:36 -0500610#endif
Peter Tyser5e93bd12009-08-21 23:05:19 -0500611
Marek Bykowski9297e362020-04-29 18:23:07 +0200612#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
613static void malloc_init(void);
614#endif
615
Peter Tyser5e93bd12009-08-21 23:05:19 -0500616ulong mem_malloc_start = 0;
617ulong mem_malloc_end = 0;
618ulong mem_malloc_brk = 0;
619
Simon Glass62d63832022-09-06 20:27:00 -0600620static bool malloc_testing; /* enable test mode */
621static int malloc_max_allocs; /* return NULL after this many calls to malloc() */
622
Peter Tyser5e93bd12009-08-21 23:05:19 -0500623void *sbrk(ptrdiff_t increment)
624{
625 ulong old = mem_malloc_brk;
626 ulong new = old + increment;
627
Kumar Gala6163f5b2010-11-15 18:41:43 -0600628 /*
629 * if we are giving memory back make sure we clear it out since
630 * we set MORECORE_CLEARS to 1
631 */
632 if (increment < 0)
633 memset((void *)new, 0, -increment);
634
Peter Tyser5e93bd12009-08-21 23:05:19 -0500635 if ((new < mem_malloc_start) || (new > mem_malloc_end))
karl.beldan@gmail.comae30b8c2010-04-06 22:18:08 +0200636 return (void *)MORECORE_FAILURE;
Peter Tyser5e93bd12009-08-21 23:05:19 -0500637
638 mem_malloc_brk = new;
639
640 return (void *)old;
641}
wdenk217c9da2002-10-25 20:35:49 +0000642
Peter Tyserd4e8ada2009-08-21 23:05:21 -0500643void mem_malloc_init(ulong start, ulong size)
644{
645 mem_malloc_start = start;
646 mem_malloc_end = start + size;
647 mem_malloc_brk = start;
648
Marek Bykowski9297e362020-04-29 18:23:07 +0200649#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
650 malloc_init();
651#endif
652
Thierry Reding868de512014-08-26 17:34:22 +0200653 debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
654 mem_malloc_end);
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +0100655#ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
656 memset((void *)mem_malloc_start, 0x0, size);
657#endif
Gabor Juhos7b395232013-01-21 21:10:38 +0000658 malloc_bin_reloc();
Peter Tyserd4e8ada2009-08-21 23:05:21 -0500659}
Peter Tyserd4e8ada2009-08-21 23:05:21 -0500660
wdenk217c9da2002-10-25 20:35:49 +0000661/* field-extraction macros */
662
663#define first(b) ((b)->fd)
664#define last(b) ((b)->bk)
665
666/*
667 Indexing into bins
668*/
669
670#define bin_index(sz) \
671(((((unsigned long)(sz)) >> 9) == 0) ? (((unsigned long)(sz)) >> 3): \
672 ((((unsigned long)(sz)) >> 9) <= 4) ? 56 + (((unsigned long)(sz)) >> 6): \
673 ((((unsigned long)(sz)) >> 9) <= 20) ? 91 + (((unsigned long)(sz)) >> 9): \
674 ((((unsigned long)(sz)) >> 9) <= 84) ? 110 + (((unsigned long)(sz)) >> 12): \
675 ((((unsigned long)(sz)) >> 9) <= 340) ? 119 + (((unsigned long)(sz)) >> 15): \
676 ((((unsigned long)(sz)) >> 9) <= 1364) ? 124 + (((unsigned long)(sz)) >> 18): \
wdenk8bde7f72003-06-27 21:31:46 +0000677 126)
wdenk217c9da2002-10-25 20:35:49 +0000678/*
679 bins for chunks < 512 are all spaced 8 bytes apart, and hold
680 identically sized chunks. This is exploited in malloc.
681*/
682
683#define MAX_SMALLBIN 63
684#define MAX_SMALLBIN_SIZE 512
685#define SMALLBIN_WIDTH 8
686
687#define smallbin_index(sz) (((unsigned long)(sz)) >> 3)
688
689/*
690 Requests are `small' if both the corresponding and the next bin are small
691*/
692
693#define is_small_request(nb) (nb < MAX_SMALLBIN_SIZE - SMALLBIN_WIDTH)
694
Simon Glassd93041a2014-07-10 22:23:25 -0600695
wdenk217c9da2002-10-25 20:35:49 +0000696
697/*
698 To help compensate for the large number of bins, a one-level index
699 structure is used for bin-by-bin searching. `binblocks' is a
700 one-word bitvector recording whether groups of BINBLOCKWIDTH bins
701 have any (possibly) non-empty bins, so they can be skipped over
702 all at once during during traversals. The bits are NOT always
703 cleared as soon as all bins in a block are empty, but instead only
704 when all are noticed to be empty during traversal in malloc.
705*/
706
707#define BINBLOCKWIDTH 4 /* bins per block */
708
Stefan Roesef2302d42008-08-06 14:05:38 +0200709#define binblocks_r ((INTERNAL_SIZE_T)av_[1]) /* bitvector of nonempty blocks */
710#define binblocks_w (av_[1])
wdenk217c9da2002-10-25 20:35:49 +0000711
712/* bin<->block macros */
713
714#define idx2binblock(ix) ((unsigned)1 << (ix / BINBLOCKWIDTH))
Stefan Roesef2302d42008-08-06 14:05:38 +0200715#define mark_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r | idx2binblock(ii)))
716#define clear_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r & ~(idx2binblock(ii))))
wdenk217c9da2002-10-25 20:35:49 +0000717
718
Simon Glassd93041a2014-07-10 22:23:25 -0600719
wdenk217c9da2002-10-25 20:35:49 +0000720
721
722/* Other static bookkeeping data */
723
724/* variables holding tunable values */
725
726static unsigned long trim_threshold = DEFAULT_TRIM_THRESHOLD;
727static unsigned long top_pad = DEFAULT_TOP_PAD;
728static unsigned int n_mmaps_max = DEFAULT_MMAP_MAX;
729static unsigned long mmap_threshold = DEFAULT_MMAP_THRESHOLD;
730
731/* The first value returned from sbrk */
732static char* sbrk_base = (char*)(-1);
733
734/* The maximum memory obtained from system via sbrk */
735static unsigned long max_sbrked_mem = 0;
736
737/* The maximum via either sbrk or mmap */
738static unsigned long max_total_mem = 0;
739
740/* internal working copy of mallinfo */
741static struct mallinfo current_mallinfo = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
742
743/* The total memory obtained from system via sbrk */
744#define sbrked_mem (current_mallinfo.arena)
745
746/* Tracking mmaps */
747
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200748#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +0000749static unsigned int n_mmaps = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200750#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +0000751static unsigned long mmapped_mem = 0;
752#if HAVE_MMAP
753static unsigned int max_n_mmaps = 0;
754static unsigned long max_mmapped_mem = 0;
755#endif
756
Marek Bykowski9297e362020-04-29 18:23:07 +0200757#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
758static void malloc_init(void)
759{
760 int i, j;
Simon Glassd93041a2014-07-10 22:23:25 -0600761
Marek Bykowski9297e362020-04-29 18:23:07 +0200762 debug("bins (av_ array) are at %p\n", (void *)av_);
763
764 av_[0] = NULL; av_[1] = NULL;
765 for (i = 2, j = 2; i < NAV * 2 + 2; i += 2, j++) {
766 av_[i] = bin_at(j - 2);
767 av_[i + 1] = bin_at(j - 2);
768
769 /* Just print the first few bins so that
770 * we can see there are alright.
771 */
772 if (i < 10)
773 debug("av_[%d]=%lx av_[%d]=%lx\n",
774 i, (ulong)av_[i],
775 i + 1, (ulong)av_[i + 1]);
776 }
777
778 /* Init the static bookkeeping as well */
779 sbrk_base = (char *)(-1);
780 max_sbrked_mem = 0;
781 max_total_mem = 0;
782#ifdef DEBUG
783 memset((void *)&current_mallinfo, 0, sizeof(struct mallinfo));
784#endif
785}
786#endif
wdenk217c9da2002-10-25 20:35:49 +0000787
788/*
789 Debugging support
790*/
791
792#ifdef DEBUG
793
794
795/*
796 These routines make a number of assertions about the states
797 of data structures that should be true at all times. If any
798 are not true, it's very likely that a user program has somehow
799 trashed memory. (It's also possible that there is a coding error
800 in malloc. In which case, please report it!)
801*/
802
803#if __STD_C
804static void do_check_chunk(mchunkptr p)
805#else
806static void do_check_chunk(p) mchunkptr p;
807#endif
808{
wdenk217c9da2002-10-25 20:35:49 +0000809 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +0000810
811 /* No checkable chunk is mmapped */
812 assert(!chunk_is_mmapped(p));
813
814 /* Check for legal address ... */
815 assert((char*)p >= sbrk_base);
816 if (p != top)
817 assert((char*)p + sz <= (char*)top);
818 else
819 assert((char*)p + sz <= sbrk_base + sbrked_mem);
820
821}
822
823
824#if __STD_C
825static void do_check_free_chunk(mchunkptr p)
826#else
827static void do_check_free_chunk(p) mchunkptr p;
828#endif
829{
830 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +0000831 mchunkptr next = chunk_at_offset(p, sz);
wdenk217c9da2002-10-25 20:35:49 +0000832
833 do_check_chunk(p);
834
835 /* Check whether it claims to be free ... */
836 assert(!inuse(p));
837
838 /* Unless a special marker, must have OK fields */
839 if ((long)sz >= (long)MINSIZE)
840 {
841 assert((sz & MALLOC_ALIGN_MASK) == 0);
842 assert(aligned_OK(chunk2mem(p)));
843 /* ... matching footer field */
844 assert(next->prev_size == sz);
845 /* ... and is fully consolidated */
846 assert(prev_inuse(p));
847 assert (next == top || inuse(next));
848
849 /* ... and has minimally sane links */
850 assert(p->fd->bk == p);
851 assert(p->bk->fd == p);
852 }
853 else /* markers are always of size SIZE_SZ */
854 assert(sz == SIZE_SZ);
855}
856
857#if __STD_C
858static void do_check_inuse_chunk(mchunkptr p)
859#else
860static void do_check_inuse_chunk(p) mchunkptr p;
861#endif
862{
863 mchunkptr next = next_chunk(p);
864 do_check_chunk(p);
865
866 /* Check whether it claims to be in use ... */
867 assert(inuse(p));
868
869 /* ... and is surrounded by OK chunks.
870 Since more things can be checked with free chunks than inuse ones,
871 if an inuse chunk borders them and debug is on, it's worth doing them.
872 */
873 if (!prev_inuse(p))
874 {
875 mchunkptr prv = prev_chunk(p);
876 assert(next_chunk(prv) == p);
877 do_check_free_chunk(prv);
878 }
879 if (next == top)
880 {
881 assert(prev_inuse(next));
882 assert(chunksize(next) >= MINSIZE);
883 }
884 else if (!inuse(next))
885 do_check_free_chunk(next);
886
887}
888
889#if __STD_C
890static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
891#else
892static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
893#endif
894{
wdenk217c9da2002-10-25 20:35:49 +0000895 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
896 long room = sz - s;
wdenk217c9da2002-10-25 20:35:49 +0000897
898 do_check_inuse_chunk(p);
899
900 /* Legal size ... */
901 assert((long)sz >= (long)MINSIZE);
902 assert((sz & MALLOC_ALIGN_MASK) == 0);
903 assert(room >= 0);
904 assert(room < (long)MINSIZE);
905
906 /* ... and alignment */
907 assert(aligned_OK(chunk2mem(p)));
908
909
910 /* ... and was allocated at front of an available chunk */
911 assert(prev_inuse(p));
912
913}
914
915
916#define check_free_chunk(P) do_check_free_chunk(P)
917#define check_inuse_chunk(P) do_check_inuse_chunk(P)
918#define check_chunk(P) do_check_chunk(P)
919#define check_malloced_chunk(P,N) do_check_malloced_chunk(P,N)
920#else
921#define check_free_chunk(P)
922#define check_inuse_chunk(P)
923#define check_chunk(P)
924#define check_malloced_chunk(P,N)
925#endif
926
Simon Glassd93041a2014-07-10 22:23:25 -0600927
wdenk217c9da2002-10-25 20:35:49 +0000928
929/*
930 Macro-based internal utilities
931*/
932
933
934/*
935 Linking chunks in bin lists.
936 Call these only with variables, not arbitrary expressions, as arguments.
937*/
938
939/*
940 Place chunk p of size s in its bin, in size order,
941 putting it ahead of others of same size.
942*/
943
944
945#define frontlink(P, S, IDX, BK, FD) \
946{ \
947 if (S < MAX_SMALLBIN_SIZE) \
948 { \
949 IDX = smallbin_index(S); \
950 mark_binblock(IDX); \
951 BK = bin_at(IDX); \
952 FD = BK->fd; \
953 P->bk = BK; \
954 P->fd = FD; \
955 FD->bk = BK->fd = P; \
956 } \
957 else \
958 { \
959 IDX = bin_index(S); \
960 BK = bin_at(IDX); \
961 FD = BK->fd; \
962 if (FD == BK) mark_binblock(IDX); \
963 else \
964 { \
965 while (FD != BK && S < chunksize(FD)) FD = FD->fd; \
966 BK = FD->bk; \
967 } \
968 P->bk = BK; \
969 P->fd = FD; \
970 FD->bk = BK->fd = P; \
971 } \
972}
973
974
975/* take a chunk off a list */
976
977#define unlink(P, BK, FD) \
978{ \
979 BK = P->bk; \
980 FD = P->fd; \
981 FD->bk = BK; \
982 BK->fd = FD; \
983} \
984
985/* Place p as the last remainder */
986
987#define link_last_remainder(P) \
988{ \
989 last_remainder->fd = last_remainder->bk = P; \
990 P->fd = P->bk = last_remainder; \
991}
992
993/* Clear the last_remainder bin */
994
995#define clear_last_remainder \
996 (last_remainder->fd = last_remainder->bk = last_remainder)
997
998
Simon Glassd93041a2014-07-10 22:23:25 -0600999
wdenk217c9da2002-10-25 20:35:49 +00001000
1001
1002/* Routines dealing with mmap(). */
1003
1004#if HAVE_MMAP
1005
1006#if __STD_C
1007static mchunkptr mmap_chunk(size_t size)
1008#else
1009static mchunkptr mmap_chunk(size) size_t size;
1010#endif
1011{
1012 size_t page_mask = malloc_getpagesize - 1;
1013 mchunkptr p;
1014
1015#ifndef MAP_ANONYMOUS
1016 static int fd = -1;
1017#endif
1018
1019 if(n_mmaps >= n_mmaps_max) return 0; /* too many regions */
1020
1021 /* For mmapped chunks, the overhead is one SIZE_SZ unit larger, because
1022 * there is no following chunk whose prev_size field could be used.
1023 */
1024 size = (size + SIZE_SZ + page_mask) & ~page_mask;
1025
1026#ifdef MAP_ANONYMOUS
1027 p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE,
1028 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1029#else /* !MAP_ANONYMOUS */
1030 if (fd < 0)
1031 {
1032 fd = open("/dev/zero", O_RDWR);
1033 if(fd < 0) return 0;
1034 }
1035 p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1036#endif
1037
1038 if(p == (mchunkptr)-1) return 0;
1039
1040 n_mmaps++;
1041 if (n_mmaps > max_n_mmaps) max_n_mmaps = n_mmaps;
1042
1043 /* We demand that eight bytes into a page must be 8-byte aligned. */
1044 assert(aligned_OK(chunk2mem(p)));
1045
1046 /* The offset to the start of the mmapped region is stored
1047 * in the prev_size field of the chunk; normally it is zero,
1048 * but that can be changed in memalign().
1049 */
1050 p->prev_size = 0;
1051 set_head(p, size|IS_MMAPPED);
1052
1053 mmapped_mem += size;
1054 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
1055 max_mmapped_mem = mmapped_mem;
1056 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1057 max_total_mem = mmapped_mem + sbrked_mem;
1058 return p;
1059}
1060
1061#if __STD_C
1062static void munmap_chunk(mchunkptr p)
1063#else
1064static void munmap_chunk(p) mchunkptr p;
1065#endif
1066{
1067 INTERNAL_SIZE_T size = chunksize(p);
1068 int ret;
1069
1070 assert (chunk_is_mmapped(p));
1071 assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
1072 assert((n_mmaps > 0));
1073 assert(((p->prev_size + size) & (malloc_getpagesize-1)) == 0);
1074
1075 n_mmaps--;
1076 mmapped_mem -= (size + p->prev_size);
1077
1078 ret = munmap((char *)p - p->prev_size, size + p->prev_size);
1079
1080 /* munmap returns non-zero on failure */
1081 assert(ret == 0);
1082}
1083
1084#if HAVE_MREMAP
1085
1086#if __STD_C
1087static mchunkptr mremap_chunk(mchunkptr p, size_t new_size)
1088#else
1089static mchunkptr mremap_chunk(p, new_size) mchunkptr p; size_t new_size;
1090#endif
1091{
1092 size_t page_mask = malloc_getpagesize - 1;
1093 INTERNAL_SIZE_T offset = p->prev_size;
1094 INTERNAL_SIZE_T size = chunksize(p);
1095 char *cp;
1096
1097 assert (chunk_is_mmapped(p));
1098 assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
1099 assert((n_mmaps > 0));
1100 assert(((size + offset) & (malloc_getpagesize-1)) == 0);
1101
1102 /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
1103 new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask;
1104
1105 cp = (char *)mremap((char *)p - offset, size + offset, new_size, 1);
1106
1107 if (cp == (char *)-1) return 0;
1108
1109 p = (mchunkptr)(cp + offset);
1110
1111 assert(aligned_OK(chunk2mem(p)));
1112
1113 assert((p->prev_size == offset));
1114 set_head(p, (new_size - offset)|IS_MMAPPED);
1115
1116 mmapped_mem -= size + offset;
1117 mmapped_mem += new_size;
1118 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
1119 max_mmapped_mem = mmapped_mem;
1120 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1121 max_total_mem = mmapped_mem + sbrked_mem;
1122 return p;
1123}
1124
1125#endif /* HAVE_MREMAP */
1126
1127#endif /* HAVE_MMAP */
1128
wdenk217c9da2002-10-25 20:35:49 +00001129/*
1130 Extend the top-most chunk by obtaining memory from system.
1131 Main interface to sbrk (but see also malloc_trim).
1132*/
1133
1134#if __STD_C
1135static void malloc_extend_top(INTERNAL_SIZE_T nb)
1136#else
1137static void malloc_extend_top(nb) INTERNAL_SIZE_T nb;
1138#endif
1139{
1140 char* brk; /* return value from sbrk */
1141 INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of sbrked space */
1142 INTERNAL_SIZE_T correction; /* bytes for 2nd sbrk call */
1143 char* new_brk; /* return of 2nd sbrk call */
1144 INTERNAL_SIZE_T top_size; /* new size of top chunk */
1145
1146 mchunkptr old_top = top; /* Record state of old top */
1147 INTERNAL_SIZE_T old_top_size = chunksize(old_top);
1148 char* old_end = (char*)(chunk_at_offset(old_top, old_top_size));
1149
1150 /* Pad request with top_pad plus minimal overhead */
1151
1152 INTERNAL_SIZE_T sbrk_size = nb + top_pad + MINSIZE;
1153 unsigned long pagesz = malloc_getpagesize;
1154
1155 /* If not the first time through, round to preserve page boundary */
1156 /* Otherwise, we need to correct to a page size below anyway. */
1157 /* (We also correct below if an intervening foreign sbrk call.) */
1158
1159 if (sbrk_base != (char*)(-1))
1160 sbrk_size = (sbrk_size + (pagesz - 1)) & ~(pagesz - 1);
1161
1162 brk = (char*)(MORECORE (sbrk_size));
1163
1164 /* Fail if sbrk failed or if a foreign sbrk call killed our space */
1165 if (brk == (char*)(MORECORE_FAILURE) ||
1166 (brk < old_end && old_top != initial_top))
1167 return;
1168
1169 sbrked_mem += sbrk_size;
1170
1171 if (brk == old_end) /* can just add bytes to current top */
1172 {
1173 top_size = sbrk_size + old_top_size;
1174 set_head(top, top_size | PREV_INUSE);
1175 }
1176 else
1177 {
1178 if (sbrk_base == (char*)(-1)) /* First time through. Record base */
1179 sbrk_base = brk;
1180 else /* Someone else called sbrk(). Count those bytes as sbrked_mem. */
1181 sbrked_mem += brk - (char*)old_end;
1182
1183 /* Guarantee alignment of first new chunk made from this space */
1184 front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK;
1185 if (front_misalign > 0)
1186 {
1187 correction = (MALLOC_ALIGNMENT) - front_misalign;
1188 brk += correction;
1189 }
1190 else
1191 correction = 0;
1192
1193 /* Guarantee the next brk will be at a page boundary */
1194
1195 correction += ((((unsigned long)(brk + sbrk_size))+(pagesz-1)) &
wdenk8bde7f72003-06-27 21:31:46 +00001196 ~(pagesz - 1)) - ((unsigned long)(brk + sbrk_size));
wdenk217c9da2002-10-25 20:35:49 +00001197
1198 /* Allocate correction */
1199 new_brk = (char*)(MORECORE (correction));
1200 if (new_brk == (char*)(MORECORE_FAILURE)) return;
1201
1202 sbrked_mem += correction;
1203
1204 top = (mchunkptr)brk;
1205 top_size = new_brk - brk + correction;
1206 set_head(top, top_size | PREV_INUSE);
1207
1208 if (old_top != initial_top)
1209 {
1210
1211 /* There must have been an intervening foreign sbrk call. */
1212 /* A double fencepost is necessary to prevent consolidation */
1213
1214 /* If not enough space to do this, then user did something very wrong */
1215 if (old_top_size < MINSIZE)
1216 {
wdenk8bde7f72003-06-27 21:31:46 +00001217 set_head(top, PREV_INUSE); /* will force null return from malloc */
1218 return;
wdenk217c9da2002-10-25 20:35:49 +00001219 }
1220
1221 /* Also keep size a multiple of MALLOC_ALIGNMENT */
1222 old_top_size = (old_top_size - 3*SIZE_SZ) & ~MALLOC_ALIGN_MASK;
1223 set_head_size(old_top, old_top_size);
1224 chunk_at_offset(old_top, old_top_size )->size =
wdenk8bde7f72003-06-27 21:31:46 +00001225 SIZE_SZ|PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +00001226 chunk_at_offset(old_top, old_top_size + SIZE_SZ)->size =
wdenk8bde7f72003-06-27 21:31:46 +00001227 SIZE_SZ|PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +00001228 /* If possible, release the rest. */
1229 if (old_top_size >= MINSIZE)
wdenk8bde7f72003-06-27 21:31:46 +00001230 fREe(chunk2mem(old_top));
wdenk217c9da2002-10-25 20:35:49 +00001231 }
1232 }
1233
1234 if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem)
1235 max_sbrked_mem = sbrked_mem;
1236 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1237 max_total_mem = mmapped_mem + sbrked_mem;
1238
1239 /* We always land on a page boundary */
1240 assert(((unsigned long)((char*)top + top_size) & (pagesz - 1)) == 0);
1241}
1242
1243
Simon Glassd93041a2014-07-10 22:23:25 -06001244
wdenk217c9da2002-10-25 20:35:49 +00001245
1246/* Main public routines */
1247
1248
1249/*
1250 Malloc Algorthim:
1251
1252 The requested size is first converted into a usable form, `nb'.
1253 This currently means to add 4 bytes overhead plus possibly more to
1254 obtain 8-byte alignment and/or to obtain a size of at least
1255 MINSIZE (currently 16 bytes), the smallest allocatable size.
1256 (All fits are considered `exact' if they are within MINSIZE bytes.)
1257
1258 From there, the first successful of the following steps is taken:
1259
1260 1. The bin corresponding to the request size is scanned, and if
wdenk8bde7f72003-06-27 21:31:46 +00001261 a chunk of exactly the right size is found, it is taken.
wdenk217c9da2002-10-25 20:35:49 +00001262
1263 2. The most recently remaindered chunk is used if it is big
wdenk8bde7f72003-06-27 21:31:46 +00001264 enough. This is a form of (roving) first fit, used only in
1265 the absence of exact fits. Runs of consecutive requests use
1266 the remainder of the chunk used for the previous such request
1267 whenever possible. This limited use of a first-fit style
1268 allocation strategy tends to give contiguous chunks
1269 coextensive lifetimes, which improves locality and can reduce
1270 fragmentation in the long run.
wdenk217c9da2002-10-25 20:35:49 +00001271
1272 3. Other bins are scanned in increasing size order, using a
wdenk8bde7f72003-06-27 21:31:46 +00001273 chunk big enough to fulfill the request, and splitting off
1274 any remainder. This search is strictly by best-fit; i.e.,
1275 the smallest (with ties going to approximately the least
1276 recently used) chunk that fits is selected.
wdenk217c9da2002-10-25 20:35:49 +00001277
1278 4. If large enough, the chunk bordering the end of memory
wdenk8bde7f72003-06-27 21:31:46 +00001279 (`top') is split off. (This use of `top' is in accord with
1280 the best-fit search rule. In effect, `top' is treated as
1281 larger (and thus less well fitting) than any other available
1282 chunk since it can be extended to be as large as necessary
1283 (up to system limitations).
wdenk217c9da2002-10-25 20:35:49 +00001284
1285 5. If the request size meets the mmap threshold and the
wdenk8bde7f72003-06-27 21:31:46 +00001286 system supports mmap, and there are few enough currently
1287 allocated mmapped regions, and a call to mmap succeeds,
1288 the request is allocated via direct memory mapping.
wdenk217c9da2002-10-25 20:35:49 +00001289
1290 6. Otherwise, the top of memory is extended by
wdenk8bde7f72003-06-27 21:31:46 +00001291 obtaining more space from the system (normally using sbrk,
1292 but definable to anything else via the MORECORE macro).
1293 Memory is gathered from the system (in system page-sized
1294 units) in a way that allows chunks obtained across different
1295 sbrk calls to be consolidated, but does not require
1296 contiguous memory. Thus, it should be safe to intersperse
1297 mallocs with other sbrk calls.
wdenk217c9da2002-10-25 20:35:49 +00001298
1299
1300 All allocations are made from the the `lowest' part of any found
1301 chunk. (The implementation invariant is that prev_inuse is
1302 always true of any allocated chunk; i.e., that each allocated
1303 chunk borders either a previously allocated and still in-use chunk,
1304 or the base of its memory arena.)
1305
1306*/
1307
1308#if __STD_C
1309Void_t* mALLOc(size_t bytes)
1310#else
1311Void_t* mALLOc(bytes) size_t bytes;
1312#endif
1313{
1314 mchunkptr victim; /* inspected/selected chunk */
1315 INTERNAL_SIZE_T victim_size; /* its size */
1316 int idx; /* index for bin traversal */
1317 mbinptr bin; /* associated bin */
1318 mchunkptr remainder; /* remainder from a split */
1319 long remainder_size; /* its size */
1320 int remainder_index; /* its bin index */
1321 unsigned long block; /* block traverser bit */
1322 int startidx; /* first bin of a traversed block */
1323 mchunkptr fwd; /* misc temp for linking */
1324 mchunkptr bck; /* misc temp for linking */
1325 mbinptr q; /* misc temp */
1326
1327 INTERNAL_SIZE_T nb;
1328
Andy Yanf1896c42017-07-24 17:43:34 +08001329#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Stephen Warrendeff6fb2016-03-05 10:30:53 -07001330 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
Simon Glassc9356be2014-11-10 17:16:43 -07001331 return malloc_simple(bytes);
Simon Glassd59476b2014-07-10 22:23:28 -06001332#endif
1333
Simon Glass62d63832022-09-06 20:27:00 -06001334 if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) {
1335 if (--malloc_max_allocs < 0)
1336 return NULL;
1337 }
1338
Wolfgang Denk27405442010-01-15 11:20:10 +01001339 /* check if mem_malloc_init() was run */
1340 if ((mem_malloc_start == 0) && (mem_malloc_end == 0)) {
1341 /* not initialized yet */
Kim Phillips199adb62012-10-29 13:34:32 +00001342 return NULL;
Wolfgang Denk27405442010-01-15 11:20:10 +01001343 }
1344
Kim Phillips199adb62012-10-29 13:34:32 +00001345 if ((long)bytes < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00001346
1347 nb = request2size(bytes); /* padded request size; */
1348
1349 /* Check for exact match in a bin */
1350
1351 if (is_small_request(nb)) /* Faster version for small requests */
1352 {
1353 idx = smallbin_index(nb);
1354
1355 /* No traversal or size check necessary for small bins. */
1356
1357 q = bin_at(idx);
1358 victim = last(q);
1359
1360 /* Also scan the next one, since it would have a remainder < MINSIZE */
1361 if (victim == q)
1362 {
1363 q = next_bin(q);
1364 victim = last(q);
1365 }
1366 if (victim != q)
1367 {
1368 victim_size = chunksize(victim);
1369 unlink(victim, bck, fwd);
1370 set_inuse_bit_at_offset(victim, victim_size);
1371 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001372 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001373#ifdef CONFIG_AML_UASAN
1374 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001375 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001376#else
1377 return chunk2mem(victim);
1378#endif
wdenk217c9da2002-10-25 20:35:49 +00001379 }
1380
1381 idx += 2; /* Set for bin scan below. We've already scanned 2 bins. */
1382
1383 }
1384 else
1385 {
1386 idx = bin_index(nb);
1387 bin = bin_at(idx);
1388
1389 for (victim = last(bin); victim != bin; victim = victim->bk)
1390 {
1391 victim_size = chunksize(victim);
1392 remainder_size = victim_size - nb;
1393
1394 if (remainder_size >= (long)MINSIZE) /* too big */
1395 {
wdenk8bde7f72003-06-27 21:31:46 +00001396 --idx; /* adjust to rescan below after checking last remainder */
1397 break;
wdenk217c9da2002-10-25 20:35:49 +00001398 }
1399
1400 else if (remainder_size >= 0) /* exact fit */
1401 {
wdenk8bde7f72003-06-27 21:31:46 +00001402 unlink(victim, bck, fwd);
1403 set_inuse_bit_at_offset(victim, victim_size);
1404 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001405 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001406
1407#ifdef CONFIG_AML_UASAN
1408 uasan_alloc(victim, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001409 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001410#else
1411 return chunk2mem(victim);
1412#endif
wdenk217c9da2002-10-25 20:35:49 +00001413 }
1414 }
1415
1416 ++idx;
1417
1418 }
1419
1420 /* Try to use the last split-off remainder */
1421
1422 if ( (victim = last_remainder->fd) != last_remainder)
1423 {
1424 victim_size = chunksize(victim);
1425 remainder_size = victim_size - nb;
1426
1427 if (remainder_size >= (long)MINSIZE) /* re-split */
1428 {
1429 remainder = chunk_at_offset(victim, nb);
1430 set_head(victim, nb | PREV_INUSE);
1431 link_last_remainder(remainder);
1432 set_head(remainder, remainder_size | PREV_INUSE);
1433 set_foot(remainder, remainder_size);
1434 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001435 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001436#ifdef CONFIG_AML_UASAN
1437 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001438 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001439#else
1440 return chunk2mem(victim);
1441#endif
wdenk217c9da2002-10-25 20:35:49 +00001442 }
1443
1444 clear_last_remainder;
1445
1446 if (remainder_size >= 0) /* exhaust */
1447 {
1448 set_inuse_bit_at_offset(victim, victim_size);
1449 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001450 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001451#ifdef CONFIG_AML_UASAN
1452 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001453 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001454#else
1455 return chunk2mem(victim);
1456#endif
wdenk217c9da2002-10-25 20:35:49 +00001457 }
1458
1459 /* Else place in bin */
1460
1461 frontlink(victim, victim_size, remainder_index, bck, fwd);
1462 }
1463
1464 /*
1465 If there are any possibly nonempty big-enough blocks,
1466 search for best fitting chunk by scanning bins in blockwidth units.
1467 */
1468
Stefan Roesef2302d42008-08-06 14:05:38 +02001469 if ( (block = idx2binblock(idx)) <= binblocks_r)
wdenk217c9da2002-10-25 20:35:49 +00001470 {
1471
1472 /* Get to the first marked block */
1473
Stefan Roesef2302d42008-08-06 14:05:38 +02001474 if ( (block & binblocks_r) == 0)
wdenk217c9da2002-10-25 20:35:49 +00001475 {
1476 /* force to an even block boundary */
1477 idx = (idx & ~(BINBLOCKWIDTH - 1)) + BINBLOCKWIDTH;
1478 block <<= 1;
Stefan Roesef2302d42008-08-06 14:05:38 +02001479 while ((block & binblocks_r) == 0)
wdenk217c9da2002-10-25 20:35:49 +00001480 {
wdenk8bde7f72003-06-27 21:31:46 +00001481 idx += BINBLOCKWIDTH;
1482 block <<= 1;
wdenk217c9da2002-10-25 20:35:49 +00001483 }
1484 }
1485
1486 /* For each possibly nonempty block ... */
1487 for (;;)
1488 {
1489 startidx = idx; /* (track incomplete blocks) */
1490 q = bin = bin_at(idx);
1491
1492 /* For each bin in this block ... */
1493 do
1494 {
wdenk8bde7f72003-06-27 21:31:46 +00001495 /* Find and use first big enough chunk ... */
wdenk217c9da2002-10-25 20:35:49 +00001496
wdenk8bde7f72003-06-27 21:31:46 +00001497 for (victim = last(bin); victim != bin; victim = victim->bk)
1498 {
1499 victim_size = chunksize(victim);
1500 remainder_size = victim_size - nb;
wdenk217c9da2002-10-25 20:35:49 +00001501
wdenk8bde7f72003-06-27 21:31:46 +00001502 if (remainder_size >= (long)MINSIZE) /* split */
1503 {
1504 remainder = chunk_at_offset(victim, nb);
1505 set_head(victim, nb | PREV_INUSE);
1506 unlink(victim, bck, fwd);
1507 link_last_remainder(remainder);
1508 set_head(remainder, remainder_size | PREV_INUSE);
1509 set_foot(remainder, remainder_size);
1510 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001511 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001512#ifdef CONFIG_AML_UASAN
1513 uasan_alloc(victim, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001514 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001515#else
1516 return chunk2mem(victim);
1517#endif
wdenk8bde7f72003-06-27 21:31:46 +00001518 }
wdenk217c9da2002-10-25 20:35:49 +00001519
wdenk8bde7f72003-06-27 21:31:46 +00001520 else if (remainder_size >= 0) /* take */
1521 {
1522 set_inuse_bit_at_offset(victim, victim_size);
1523 unlink(victim, bck, fwd);
1524 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001525 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001526#ifdef CONFIG_AML_UASAN
1527 uasan_alloc(victim, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001528 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001529#else
1530 return chunk2mem(victim);
1531#endif
wdenk8bde7f72003-06-27 21:31:46 +00001532 }
wdenk217c9da2002-10-25 20:35:49 +00001533
wdenk8bde7f72003-06-27 21:31:46 +00001534 }
wdenk217c9da2002-10-25 20:35:49 +00001535
1536 bin = next_bin(bin);
1537
1538 } while ((++idx & (BINBLOCKWIDTH - 1)) != 0);
1539
1540 /* Clear out the block bit. */
1541
1542 do /* Possibly backtrack to try to clear a partial block */
1543 {
wdenk8bde7f72003-06-27 21:31:46 +00001544 if ((startidx & (BINBLOCKWIDTH - 1)) == 0)
1545 {
Stefan Roesef2302d42008-08-06 14:05:38 +02001546 av_[1] = (mbinptr)(binblocks_r & ~block);
wdenk8bde7f72003-06-27 21:31:46 +00001547 break;
1548 }
1549 --startidx;
wdenk217c9da2002-10-25 20:35:49 +00001550 q = prev_bin(q);
1551 } while (first(q) == q);
1552
1553 /* Get to the next possibly nonempty block */
1554
Stefan Roesef2302d42008-08-06 14:05:38 +02001555 if ( (block <<= 1) <= binblocks_r && (block != 0) )
wdenk217c9da2002-10-25 20:35:49 +00001556 {
Stefan Roesef2302d42008-08-06 14:05:38 +02001557 while ((block & binblocks_r) == 0)
wdenk8bde7f72003-06-27 21:31:46 +00001558 {
1559 idx += BINBLOCKWIDTH;
1560 block <<= 1;
1561 }
wdenk217c9da2002-10-25 20:35:49 +00001562 }
1563 else
wdenk8bde7f72003-06-27 21:31:46 +00001564 break;
wdenk217c9da2002-10-25 20:35:49 +00001565 }
1566 }
1567
1568
1569 /* Try to use top chunk */
1570
1571 /* Require that there be a remainder, ensuring top always exists */
1572 if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE)
1573 {
1574
1575#if HAVE_MMAP
1576 /* If big and would otherwise need to extend, try to use mmap instead */
1577 if ((unsigned long)nb >= (unsigned long)mmap_threshold &&
Heinrich Schuchardta874cac2017-11-10 21:46:34 +01001578 (victim = mmap_chunk(nb)))
Sean Andersonbdaeea12022-03-23 14:04:49 -04001579 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
wdenk217c9da2002-10-25 20:35:49 +00001580 return chunk2mem(victim);
1581#endif
1582
1583 /* Try to extend */
1584 malloc_extend_top(nb);
1585 if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE)
Kim Phillips199adb62012-10-29 13:34:32 +00001586 return NULL; /* propagate failure */
wdenk217c9da2002-10-25 20:35:49 +00001587 }
1588
1589 victim = top;
1590 set_head(victim, nb | PREV_INUSE);
1591 top = chunk_at_offset(victim, nb);
1592 set_head(top, remainder_size | PREV_INUSE);
1593 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001594 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001595#ifdef CONFIG_AML_UASAN
1596 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001597 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001598#else
1599 return chunk2mem(victim);
1600#endif
wdenk217c9da2002-10-25 20:35:49 +00001601
1602}
1603
1604
Simon Glassd93041a2014-07-10 22:23:25 -06001605
wdenk217c9da2002-10-25 20:35:49 +00001606
1607/*
1608
1609 free() algorithm :
1610
1611 cases:
1612
1613 1. free(0) has no effect.
1614
1615 2. If the chunk was allocated via mmap, it is release via munmap().
1616
1617 3. If a returned chunk borders the current high end of memory,
wdenk8bde7f72003-06-27 21:31:46 +00001618 it is consolidated into the top, and if the total unused
1619 topmost memory exceeds the trim threshold, malloc_trim is
1620 called.
wdenk217c9da2002-10-25 20:35:49 +00001621
1622 4. Other chunks are consolidated as they arrive, and
wdenk8bde7f72003-06-27 21:31:46 +00001623 placed in corresponding bins. (This includes the case of
1624 consolidating with the current `last_remainder').
wdenk217c9da2002-10-25 20:35:49 +00001625
1626*/
1627
1628
1629#if __STD_C
1630void fREe(Void_t* mem)
1631#else
1632void fREe(mem) Void_t* mem;
1633#endif
1634{
1635 mchunkptr p; /* chunk corresponding to mem */
1636 INTERNAL_SIZE_T hd; /* its head field */
1637 INTERNAL_SIZE_T sz; /* its size */
1638 int idx; /* its bin index */
1639 mchunkptr next; /* next contiguous chunk */
1640 INTERNAL_SIZE_T nextsz; /* its size */
1641 INTERNAL_SIZE_T prevsz; /* size of previous contiguous chunk */
1642 mchunkptr bck; /* misc temp for linking */
1643 mchunkptr fwd; /* misc temp for linking */
1644 int islr; /* track whether merging with last_remainder */
1645
Andy Yanf1896c42017-07-24 17:43:34 +08001646#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassd59476b2014-07-10 22:23:28 -06001647 /* free() is a no-op - all the memory will be freed on relocation */
Sean Andersonbdaeea12022-03-23 14:04:49 -04001648 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
1649 VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ);
Simon Glassd59476b2014-07-10 22:23:28 -06001650 return;
Sean Andersonbdaeea12022-03-23 14:04:49 -04001651 }
Simon Glassd59476b2014-07-10 22:23:28 -06001652#endif
1653
Kim Phillips199adb62012-10-29 13:34:32 +00001654 if (mem == NULL) /* free(0) has no effect */
wdenk217c9da2002-10-25 20:35:49 +00001655 return;
1656
1657 p = mem2chunk(mem);
1658 hd = p->size;
1659
1660#if HAVE_MMAP
1661 if (hd & IS_MMAPPED) /* release mmapped memory. */
1662 {
1663 munmap_chunk(p);
1664 return;
1665 }
1666#endif
1667
1668 check_inuse_chunk(p);
1669
1670 sz = hd & ~PREV_INUSE;
Bo Lv96a66d02023-05-12 19:18:22 +08001671
1672#ifdef CONFIG_AML_UASAN
1673 uasan_free(p, sz);
1674#endif
1675
wdenk217c9da2002-10-25 20:35:49 +00001676 next = chunk_at_offset(p, sz);
1677 nextsz = chunksize(next);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001678 VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ);
wdenk217c9da2002-10-25 20:35:49 +00001679
1680 if (next == top) /* merge with top */
1681 {
1682 sz += nextsz;
1683
1684 if (!(hd & PREV_INUSE)) /* consolidate backward */
1685 {
1686 prevsz = p->prev_size;
1687 p = chunk_at_offset(p, -((long) prevsz));
1688 sz += prevsz;
1689 unlink(p, bck, fwd);
1690 }
1691
1692 set_head(p, sz | PREV_INUSE);
1693 top = p;
1694 if ((unsigned long)(sz) >= (unsigned long)trim_threshold)
1695 malloc_trim(top_pad);
1696 return;
1697 }
1698
1699 set_head(next, nextsz); /* clear inuse bit */
1700
1701 islr = 0;
1702
1703 if (!(hd & PREV_INUSE)) /* consolidate backward */
1704 {
1705 prevsz = p->prev_size;
1706 p = chunk_at_offset(p, -((long) prevsz));
1707 sz += prevsz;
1708
1709 if (p->fd == last_remainder) /* keep as last_remainder */
1710 islr = 1;
1711 else
1712 unlink(p, bck, fwd);
1713 }
1714
1715 if (!(inuse_bit_at_offset(next, nextsz))) /* consolidate forward */
1716 {
1717 sz += nextsz;
1718
1719 if (!islr && next->fd == last_remainder) /* re-insert last_remainder */
1720 {
1721 islr = 1;
1722 link_last_remainder(p);
1723 }
1724 else
1725 unlink(next, bck, fwd);
1726 }
1727
1728
1729 set_head(p, sz | PREV_INUSE);
1730 set_foot(p, sz);
1731 if (!islr)
1732 frontlink(p, sz, idx, bck, fwd);
1733}
1734
1735
Simon Glassd93041a2014-07-10 22:23:25 -06001736
wdenk217c9da2002-10-25 20:35:49 +00001737
1738
1739/*
1740
1741 Realloc algorithm:
1742
1743 Chunks that were obtained via mmap cannot be extended or shrunk
1744 unless HAVE_MREMAP is defined, in which case mremap is used.
1745 Otherwise, if their reallocation is for additional space, they are
1746 copied. If for less, they are just left alone.
1747
1748 Otherwise, if the reallocation is for additional space, and the
1749 chunk can be extended, it is, else a malloc-copy-free sequence is
1750 taken. There are several different ways that a chunk could be
1751 extended. All are tried:
1752
1753 * Extending forward into following adjacent free chunk.
1754 * Shifting backwards, joining preceding adjacent space
1755 * Both shifting backwards and extending forward.
1756 * Extending into newly sbrked space
1757
1758 Unless the #define REALLOC_ZERO_BYTES_FREES is set, realloc with a
1759 size argument of zero (re)allocates a minimum-sized chunk.
1760
1761 If the reallocation is for less space, and the new request is for
1762 a `small' (<512 bytes) size, then the newly unused space is lopped
1763 off and freed.
1764
1765 The old unix realloc convention of allowing the last-free'd chunk
1766 to be used as an argument to realloc is no longer supported.
1767 I don't know of any programs still relying on this feature,
1768 and allowing it would also allow too many other incorrect
1769 usages of realloc to be sensible.
1770
1771
1772*/
1773
1774
1775#if __STD_C
1776Void_t* rEALLOc(Void_t* oldmem, size_t bytes)
1777#else
1778Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
1779#endif
1780{
1781 INTERNAL_SIZE_T nb; /* padded request size */
1782
1783 mchunkptr oldp; /* chunk corresponding to oldmem */
1784 INTERNAL_SIZE_T oldsize; /* its size */
1785
1786 mchunkptr newp; /* chunk to return */
1787 INTERNAL_SIZE_T newsize; /* its size */
1788 Void_t* newmem; /* corresponding user mem */
1789
1790 mchunkptr next; /* next contiguous chunk after oldp */
1791 INTERNAL_SIZE_T nextsize; /* its size */
1792
1793 mchunkptr prev; /* previous contiguous chunk before oldp */
1794 INTERNAL_SIZE_T prevsize; /* its size */
1795
1796 mchunkptr remainder; /* holds split off extra space from newp */
1797 INTERNAL_SIZE_T remainder_size; /* its size */
1798
1799 mchunkptr bck; /* misc temp for linking */
1800 mchunkptr fwd; /* misc temp for linking */
1801
1802#ifdef REALLOC_ZERO_BYTES_FREES
Heinrich Schuchardta874cac2017-11-10 21:46:34 +01001803 if (!bytes) {
1804 fREe(oldmem);
1805 return NULL;
1806 }
wdenk217c9da2002-10-25 20:35:49 +00001807#endif
1808
Kim Phillips199adb62012-10-29 13:34:32 +00001809 if ((long)bytes < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00001810
1811 /* realloc of null is supposed to be same as malloc */
Kim Phillips199adb62012-10-29 13:34:32 +00001812 if (oldmem == NULL) return mALLOc(bytes);
wdenk217c9da2002-10-25 20:35:49 +00001813
Andy Yanf1896c42017-07-24 17:43:34 +08001814#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassc9356be2014-11-10 17:16:43 -07001815 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
Simon Glassd59476b2014-07-10 22:23:28 -06001816 /* This is harder to support and should not be needed */
1817 panic("pre-reloc realloc() is not supported");
1818 }
1819#endif
1820
wdenk217c9da2002-10-25 20:35:49 +00001821 newp = oldp = mem2chunk(oldmem);
1822 newsize = oldsize = chunksize(oldp);
1823
1824
1825 nb = request2size(bytes);
1826
1827#if HAVE_MMAP
1828 if (chunk_is_mmapped(oldp))
1829 {
1830#if HAVE_MREMAP
1831 newp = mremap_chunk(oldp, nb);
1832 if(newp) return chunk2mem(newp);
1833#endif
1834 /* Note the extra SIZE_SZ overhead. */
1835 if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */
1836 /* Must alloc, copy, free. */
1837 newmem = mALLOc(bytes);
Heinrich Schuchardta874cac2017-11-10 21:46:34 +01001838 if (!newmem)
1839 return NULL; /* propagate failure */
wdenk217c9da2002-10-25 20:35:49 +00001840 MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
1841 munmap_chunk(oldp);
1842 return newmem;
1843 }
1844#endif
1845
1846 check_inuse_chunk(oldp);
1847
1848 if ((long)(oldsize) < (long)(nb))
1849 {
1850
1851 /* Try expanding forward */
1852
1853 next = chunk_at_offset(oldp, oldsize);
1854 if (next == top || !inuse(next))
1855 {
1856 nextsize = chunksize(next);
1857
1858 /* Forward into top only if a remainder */
1859 if (next == top)
1860 {
wdenk8bde7f72003-06-27 21:31:46 +00001861 if ((long)(nextsize + newsize) >= (long)(nb + MINSIZE))
1862 {
1863 newsize += nextsize;
1864 top = chunk_at_offset(oldp, nb);
1865 set_head(top, (newsize - nb) | PREV_INUSE);
1866 set_head_size(oldp, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001867 VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ);
1868 VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes);
Bo Lv96a66d02023-05-12 19:18:22 +08001869#ifdef CONFIG_AML_UASAN
1870 uasan_alloc(oldp, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001871 return chunk2mem(oldp);
Bo Lv96a66d02023-05-12 19:18:22 +08001872#else
1873 return chunk2mem(oldp);
1874#endif
wdenk8bde7f72003-06-27 21:31:46 +00001875 }
wdenk217c9da2002-10-25 20:35:49 +00001876 }
1877
1878 /* Forward into next chunk */
1879 else if (((long)(nextsize + newsize) >= (long)(nb)))
1880 {
wdenk8bde7f72003-06-27 21:31:46 +00001881 unlink(next, bck, fwd);
1882 newsize += nextsize;
Sean Andersonbdaeea12022-03-23 14:04:49 -04001883 VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ);
1884 VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001885 goto split;
wdenk217c9da2002-10-25 20:35:49 +00001886 }
1887 }
1888 else
1889 {
Kim Phillips199adb62012-10-29 13:34:32 +00001890 next = NULL;
wdenk217c9da2002-10-25 20:35:49 +00001891 nextsize = 0;
1892 }
1893
1894 /* Try shifting backwards. */
1895
1896 if (!prev_inuse(oldp))
1897 {
1898 prev = prev_chunk(oldp);
1899 prevsize = chunksize(prev);
1900
1901 /* try forward + backward first to save a later consolidation */
1902
Kim Phillips199adb62012-10-29 13:34:32 +00001903 if (next != NULL)
wdenk217c9da2002-10-25 20:35:49 +00001904 {
wdenk8bde7f72003-06-27 21:31:46 +00001905 /* into top */
1906 if (next == top)
1907 {
1908 if ((long)(nextsize + prevsize + newsize) >= (long)(nb + MINSIZE))
1909 {
1910 unlink(prev, bck, fwd);
1911 newp = prev;
1912 newsize += prevsize + nextsize;
1913 newmem = chunk2mem(newp);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001914 VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001915#ifdef CONFIG_AML_UASAN
1916 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
1917 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
1918 oldsize - SIZE_SZ);
1919#else
wdenk8bde7f72003-06-27 21:31:46 +00001920 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08001921#endif
wdenk8bde7f72003-06-27 21:31:46 +00001922 top = chunk_at_offset(newp, nb);
1923 set_head(top, (newsize - nb) | PREV_INUSE);
1924 set_head_size(newp, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001925 VALGRIND_FREELIKE_BLOCK(oldmem, SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08001926#ifdef CONFIG_AML_UASAN
1927 uasan_alloc(newp, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001928 return newmem;
Bo Lv96a66d02023-05-12 19:18:22 +08001929#else
1930 return newmem;
1931#endif
wdenk8bde7f72003-06-27 21:31:46 +00001932 }
1933 }
wdenk217c9da2002-10-25 20:35:49 +00001934
wdenk8bde7f72003-06-27 21:31:46 +00001935 /* into next chunk */
1936 else if (((long)(nextsize + prevsize + newsize) >= (long)(nb)))
1937 {
1938 unlink(next, bck, fwd);
1939 unlink(prev, bck, fwd);
1940 newp = prev;
1941 newsize += nextsize + prevsize;
1942 newmem = chunk2mem(newp);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001943 VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001944#ifdef CONFIG_AML_UASAN
1945 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
1946 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
1947 oldsize - SIZE_SZ);
1948#else
wdenk8bde7f72003-06-27 21:31:46 +00001949 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08001950#endif
wdenk8bde7f72003-06-27 21:31:46 +00001951 goto split;
1952 }
wdenk217c9da2002-10-25 20:35:49 +00001953 }
1954
1955 /* backward only */
Kim Phillips199adb62012-10-29 13:34:32 +00001956 if (prev != NULL && (long)(prevsize + newsize) >= (long)nb)
wdenk217c9da2002-10-25 20:35:49 +00001957 {
wdenk8bde7f72003-06-27 21:31:46 +00001958 unlink(prev, bck, fwd);
1959 newp = prev;
1960 newsize += prevsize;
1961 newmem = chunk2mem(newp);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001962 VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001963#ifdef CONFIG_AML_UASAN
1964 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
1965 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
1966 oldsize - SIZE_SZ);
1967#else
wdenk8bde7f72003-06-27 21:31:46 +00001968 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08001969#endif
wdenk8bde7f72003-06-27 21:31:46 +00001970 goto split;
wdenk217c9da2002-10-25 20:35:49 +00001971 }
1972 }
1973
1974 /* Must allocate */
1975
1976 newmem = mALLOc (bytes);
1977
Kim Phillips199adb62012-10-29 13:34:32 +00001978 if (newmem == NULL) /* propagate failure */
1979 return NULL;
wdenk217c9da2002-10-25 20:35:49 +00001980
1981 /* Avoid copy if newp is next chunk after oldp. */
1982 /* (This can only happen when new chunk is sbrk'ed.) */
1983
1984 if ( (newp = mem2chunk(newmem)) == next_chunk(oldp))
1985 {
1986 newsize += chunksize(newp);
1987 newp = oldp;
1988 goto split;
1989 }
1990
1991 /* Otherwise copy, free, and exit */
Bo Lv96a66d02023-05-12 19:18:22 +08001992#ifdef CONFIG_AML_UASAN
1993 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
1994 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
1995 oldsize - SIZE_SZ);
1996#else
wdenk217c9da2002-10-25 20:35:49 +00001997 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08001998#endif
wdenk217c9da2002-10-25 20:35:49 +00001999 fREe(oldmem);
2000 return newmem;
Sean Andersonbdaeea12022-03-23 14:04:49 -04002001 } else {
2002 VALGRIND_RESIZEINPLACE_BLOCK(oldmem, 0, bytes, SIZE_SZ);
2003 VALGRIND_MAKE_MEM_DEFINED(oldmem, bytes);
wdenk217c9da2002-10-25 20:35:49 +00002004 }
2005
2006
2007 split: /* split off extra room in old or expanded chunk */
2008
2009 if (newsize - nb >= MINSIZE) /* split off remainder */
2010 {
2011 remainder = chunk_at_offset(newp, nb);
2012 remainder_size = newsize - nb;
2013 set_head_size(newp, nb);
2014 set_head(remainder, remainder_size | PREV_INUSE);
2015 set_inuse_bit_at_offset(remainder, remainder_size);
Sean Andersonbdaeea12022-03-23 14:04:49 -04002016 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ,
2017 false);
wdenk217c9da2002-10-25 20:35:49 +00002018 fREe(chunk2mem(remainder)); /* let free() deal with it */
2019 }
2020 else
2021 {
2022 set_head_size(newp, newsize);
2023 set_inuse_bit_at_offset(newp, newsize);
2024 }
2025
2026 check_inuse_chunk(newp);
Bo Lv96a66d02023-05-12 19:18:22 +08002027#ifdef CONFIG_AML_UASAN
2028 uasan_alloc(newp, bytes);
wdenk217c9da2002-10-25 20:35:49 +00002029 return chunk2mem(newp);
Bo Lv96a66d02023-05-12 19:18:22 +08002030#else
2031 return chunk2mem(newp);
2032#endif
wdenk217c9da2002-10-25 20:35:49 +00002033}
2034
2035
Simon Glassd93041a2014-07-10 22:23:25 -06002036
wdenk217c9da2002-10-25 20:35:49 +00002037
2038/*
2039
2040 memalign algorithm:
2041
2042 memalign requests more than enough space from malloc, finds a spot
2043 within that chunk that meets the alignment request, and then
2044 possibly frees the leading and trailing space.
2045
2046 The alignment argument must be a power of two. This property is not
2047 checked by memalign, so misuse may result in random runtime errors.
2048
2049 8-byte alignment is guaranteed by normal malloc calls, so don't
2050 bother calling memalign with an argument of 8 or less.
2051
2052 Overreliance on memalign is a sure way to fragment space.
2053
2054*/
2055
2056
2057#if __STD_C
2058Void_t* mEMALIGn(size_t alignment, size_t bytes)
2059#else
2060Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
2061#endif
2062{
2063 INTERNAL_SIZE_T nb; /* padded request size */
2064 char* m; /* memory returned by malloc call */
2065 mchunkptr p; /* corresponding chunk */
2066 char* brk; /* alignment point within p */
2067 mchunkptr newp; /* chunk to return */
2068 INTERNAL_SIZE_T newsize; /* its size */
2069 INTERNAL_SIZE_T leadsize; /* leading space befor alignment point */
2070 mchunkptr remainder; /* spare room at end to split off */
2071 long remainder_size; /* its size */
2072
Kim Phillips199adb62012-10-29 13:34:32 +00002073 if ((long)bytes < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00002074
Ley Foon Tanee038c52018-05-18 18:03:12 +08002075#if CONFIG_VAL(SYS_MALLOC_F_LEN)
2076 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
Andreas Dannenberg4c6be012019-03-27 13:17:26 -05002077 return memalign_simple(alignment, bytes);
Ley Foon Tanee038c52018-05-18 18:03:12 +08002078 }
2079#endif
2080
wdenk217c9da2002-10-25 20:35:49 +00002081 /* If need less alignment than we give anyway, just relay to malloc */
2082
2083 if (alignment <= MALLOC_ALIGNMENT) return mALLOc(bytes);
2084
2085 /* Otherwise, ensure that it is at least a minimum chunk size */
2086
2087 if (alignment < MINSIZE) alignment = MINSIZE;
2088
2089 /* Call malloc with worst case padding to hit alignment. */
2090
2091 nb = request2size(bytes);
2092 m = (char*)(mALLOc(nb + alignment + MINSIZE));
2093
Stephen Warren4f144a42016-01-25 14:03:42 -07002094 /*
2095 * The attempt to over-allocate (with a size large enough to guarantee the
2096 * ability to find an aligned region within allocated memory) failed.
2097 *
2098 * Try again, this time only allocating exactly the size the user wants. If
2099 * the allocation now succeeds and just happens to be aligned, we can still
2100 * fulfill the user's request.
2101 */
2102 if (m == NULL) {
Stephen Warren034eda82016-04-25 15:55:42 -06002103 size_t extra, extra2;
Stephen Warren4f144a42016-01-25 14:03:42 -07002104 /*
2105 * Use bytes not nb, since mALLOc internally calls request2size too, and
2106 * each call increases the size to allocate, to account for the header.
2107 */
2108 m = (char*)(mALLOc(bytes));
2109 /* Aligned -> return it */
2110 if ((((unsigned long)(m)) % alignment) == 0)
2111 return m;
Stephen Warren034eda82016-04-25 15:55:42 -06002112 /*
2113 * Otherwise, try again, requesting enough extra space to be able to
2114 * acquire alignment.
2115 */
Stephen Warren4f144a42016-01-25 14:03:42 -07002116 fREe(m);
Stephen Warren034eda82016-04-25 15:55:42 -06002117 /* Add in extra bytes to match misalignment of unexpanded allocation */
2118 extra = alignment - (((unsigned long)(m)) % alignment);
2119 m = (char*)(mALLOc(bytes + extra));
2120 /*
2121 * m might not be the same as before. Validate that the previous value of
2122 * extra still works for the current value of m.
2123 * If (!m), extra2=alignment so
2124 */
2125 if (m) {
2126 extra2 = alignment - (((unsigned long)(m)) % alignment);
2127 if (extra2 > extra) {
2128 fREe(m);
2129 m = NULL;
2130 }
2131 }
2132 /* Fall through to original NULL check and chunk splitting logic */
Stephen Warren4f144a42016-01-25 14:03:42 -07002133 }
2134
Kim Phillips199adb62012-10-29 13:34:32 +00002135 if (m == NULL) return NULL; /* propagate failure */
wdenk217c9da2002-10-25 20:35:49 +00002136
2137 p = mem2chunk(m);
2138
2139 if ((((unsigned long)(m)) % alignment) == 0) /* aligned */
2140 {
2141#if HAVE_MMAP
2142 if(chunk_is_mmapped(p))
2143 return chunk2mem(p); /* nothing more to do */
2144#endif
2145 }
2146 else /* misaligned */
2147 {
2148 /*
2149 Find an aligned spot inside chunk.
2150 Since we need to give back leading space in a chunk of at
2151 least MINSIZE, if the first calculation places us at
2152 a spot with less than MINSIZE leader, we can move to the
2153 next aligned spot -- we've allocated enough total room so that
2154 this is always possible.
2155 */
2156
2157 brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) & -((signed) alignment));
2158 if ((long)(brk - (char*)(p)) < MINSIZE) brk = brk + alignment;
2159
2160 newp = (mchunkptr)brk;
2161 leadsize = brk - (char*)(p);
2162 newsize = chunksize(p) - leadsize;
2163
2164#if HAVE_MMAP
2165 if(chunk_is_mmapped(p))
2166 {
2167 newp->prev_size = p->prev_size + leadsize;
2168 set_head(newp, newsize|IS_MMAPPED);
2169 return chunk2mem(newp);
2170 }
2171#endif
2172
2173 /* give back leader, use the rest */
2174
2175 set_head(newp, newsize | PREV_INUSE);
2176 set_inuse_bit_at_offset(newp, newsize);
2177 set_head_size(p, leadsize);
2178 fREe(chunk2mem(p));
2179 p = newp;
Sean Andersonbdaeea12022-03-23 14:04:49 -04002180 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(p), bytes, SIZE_SZ, false);
wdenk217c9da2002-10-25 20:35:49 +00002181
2182 assert (newsize >= nb && (((unsigned long)(chunk2mem(p))) % alignment) == 0);
2183 }
2184
2185 /* Also give back spare room at the end */
2186
2187 remainder_size = chunksize(p) - nb;
2188
2189 if (remainder_size >= (long)MINSIZE)
2190 {
2191 remainder = chunk_at_offset(p, nb);
2192 set_head(remainder, remainder_size | PREV_INUSE);
2193 set_head_size(p, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04002194 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ,
2195 false);
wdenk217c9da2002-10-25 20:35:49 +00002196 fREe(chunk2mem(remainder));
2197 }
2198
2199 check_inuse_chunk(p);
Bo Lv96a66d02023-05-12 19:18:22 +08002200#ifdef CONFIG_AML_UASAN
2201 uasan_alloc(p, bytes);
2202#endif
wdenk217c9da2002-10-25 20:35:49 +00002203 return chunk2mem(p);
2204
2205}
2206
Simon Glassd93041a2014-07-10 22:23:25 -06002207
wdenk217c9da2002-10-25 20:35:49 +00002208
2209
2210/*
2211 valloc just invokes memalign with alignment argument equal
2212 to the page size of the system (or as near to this as can
2213 be figured out from all the includes/defines above.)
2214*/
2215
2216#if __STD_C
2217Void_t* vALLOc(size_t bytes)
2218#else
2219Void_t* vALLOc(bytes) size_t bytes;
2220#endif
2221{
2222 return mEMALIGn (malloc_getpagesize, bytes);
2223}
2224
2225/*
2226 pvalloc just invokes valloc for the nearest pagesize
2227 that will accommodate request
2228*/
2229
2230
2231#if __STD_C
2232Void_t* pvALLOc(size_t bytes)
2233#else
2234Void_t* pvALLOc(bytes) size_t bytes;
2235#endif
2236{
2237 size_t pagesize = malloc_getpagesize;
2238 return mEMALIGn (pagesize, (bytes + pagesize - 1) & ~(pagesize - 1));
2239}
2240
2241/*
2242
2243 calloc calls malloc, then zeroes out the allocated chunk.
2244
2245*/
2246
2247#if __STD_C
2248Void_t* cALLOc(size_t n, size_t elem_size)
2249#else
2250Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size;
2251#endif
2252{
2253 mchunkptr p;
2254 INTERNAL_SIZE_T csz;
2255
2256 INTERNAL_SIZE_T sz = n * elem_size;
2257
2258
2259 /* check if expand_top called, in which case don't need to clear */
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002260#ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
wdenk217c9da2002-10-25 20:35:49 +00002261#if MORECORE_CLEARS
2262 mchunkptr oldtop = top;
2263 INTERNAL_SIZE_T oldtopsize = chunksize(top);
2264#endif
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002265#endif
wdenk217c9da2002-10-25 20:35:49 +00002266 Void_t* mem = mALLOc (sz);
2267
Kim Phillips199adb62012-10-29 13:34:32 +00002268 if ((long)n < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00002269
Kim Phillips199adb62012-10-29 13:34:32 +00002270 if (mem == NULL)
2271 return NULL;
wdenk217c9da2002-10-25 20:35:49 +00002272 else
2273 {
Andy Yanf1896c42017-07-24 17:43:34 +08002274#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassc9356be2014-11-10 17:16:43 -07002275 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
Simon Goldschmidtbb71a2d2019-10-25 21:23:35 +02002276 memset(mem, 0, sz);
Simon Glassd59476b2014-07-10 22:23:28 -06002277 return mem;
2278 }
2279#endif
wdenk217c9da2002-10-25 20:35:49 +00002280 p = mem2chunk(mem);
2281
2282 /* Two optional cases in which clearing not necessary */
2283
2284
2285#if HAVE_MMAP
2286 if (chunk_is_mmapped(p)) return mem;
2287#endif
2288
2289 csz = chunksize(p);
2290
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002291#ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
wdenk217c9da2002-10-25 20:35:49 +00002292#if MORECORE_CLEARS
Bo Lv96a66d02023-05-12 19:18:22 +08002293 if (p == oldtop && csz > oldtopsize && oldtopsize > sz)
wdenk217c9da2002-10-25 20:35:49 +00002294 {
2295 /* clear only the bytes from non-freshly-sbrked memory */
2296 csz = oldtopsize;
2297 }
2298#endif
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002299#endif
wdenk217c9da2002-10-25 20:35:49 +00002300
Bo Lv96a66d02023-05-12 19:18:22 +08002301#ifdef CONFIG_AML_UASAN
2302 /* avoid overwrite */
2303 MALLOC_ZERO(mem, csz - SIZE_SZ - UASAN_ALLOCA_REDZONE_SIZE);
2304#else
wdenk217c9da2002-10-25 20:35:49 +00002305 MALLOC_ZERO(mem, csz - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08002306#endif
Sean Andersonbdaeea12022-03-23 14:04:49 -04002307 VALGRIND_MAKE_MEM_DEFINED(mem, sz);
wdenk217c9da2002-10-25 20:35:49 +00002308 return mem;
2309 }
2310}
2311
2312/*
2313
2314 cfree just calls free. It is needed/defined on some systems
2315 that pair it with calloc, presumably for odd historical reasons.
2316
2317*/
2318
2319#if !defined(INTERNAL_LINUX_C_LIB) || !defined(__ELF__)
2320#if __STD_C
2321void cfree(Void_t *mem)
2322#else
2323void cfree(mem) Void_t *mem;
2324#endif
2325{
2326 fREe(mem);
2327}
2328#endif
2329
Simon Glassd93041a2014-07-10 22:23:25 -06002330
wdenk217c9da2002-10-25 20:35:49 +00002331
2332/*
2333
2334 Malloc_trim gives memory back to the system (via negative
2335 arguments to sbrk) if there is unused memory at the `high' end of
2336 the malloc pool. You can call this after freeing large blocks of
2337 memory to potentially reduce the system-level memory requirements
2338 of a program. However, it cannot guarantee to reduce memory. Under
2339 some allocation patterns, some large free blocks of memory will be
2340 locked between two used chunks, so they cannot be given back to
2341 the system.
2342
2343 The `pad' argument to malloc_trim represents the amount of free
2344 trailing space to leave untrimmed. If this argument is zero,
2345 only the minimum amount of memory to maintain internal data
2346 structures will be left (one page or less). Non-zero arguments
2347 can be supplied to maintain enough trailing space to service
2348 future expected allocations without having to re-obtain memory
2349 from the system.
2350
2351 Malloc_trim returns 1 if it actually released any memory, else 0.
2352
2353*/
2354
2355#if __STD_C
2356int malloc_trim(size_t pad)
2357#else
2358int malloc_trim(pad) size_t pad;
2359#endif
2360{
2361 long top_size; /* Amount of top-most memory */
2362 long extra; /* Amount to release */
2363 char* current_brk; /* address returned by pre-check sbrk call */
2364 char* new_brk; /* address returned by negative sbrk call */
2365
2366 unsigned long pagesz = malloc_getpagesize;
2367
2368 top_size = chunksize(top);
2369 extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
2370
2371 if (extra < (long)pagesz) /* Not enough memory to release */
2372 return 0;
2373
2374 else
2375 {
2376 /* Test to make sure no one else called sbrk */
2377 current_brk = (char*)(MORECORE (0));
2378 if (current_brk != (char*)(top) + top_size)
2379 return 0; /* Apparently we don't own memory; must fail */
2380
2381 else
2382 {
2383 new_brk = (char*)(MORECORE (-extra));
2384
2385 if (new_brk == (char*)(MORECORE_FAILURE)) /* sbrk failed? */
2386 {
wdenk8bde7f72003-06-27 21:31:46 +00002387 /* Try to figure out what we have */
2388 current_brk = (char*)(MORECORE (0));
2389 top_size = current_brk - (char*)top;
2390 if (top_size >= (long)MINSIZE) /* if not, we are very very dead! */
2391 {
2392 sbrked_mem = current_brk - sbrk_base;
2393 set_head(top, top_size | PREV_INUSE);
2394 }
2395 check_chunk(top);
2396 return 0;
wdenk217c9da2002-10-25 20:35:49 +00002397 }
2398
2399 else
2400 {
wdenk8bde7f72003-06-27 21:31:46 +00002401 /* Success. Adjust top accordingly. */
2402 set_head(top, (top_size - extra) | PREV_INUSE);
2403 sbrked_mem -= extra;
2404 check_chunk(top);
2405 return 1;
wdenk217c9da2002-10-25 20:35:49 +00002406 }
2407 }
2408 }
2409}
2410
Simon Glassd93041a2014-07-10 22:23:25 -06002411
wdenk217c9da2002-10-25 20:35:49 +00002412
2413/*
2414 malloc_usable_size:
2415
2416 This routine tells you how many bytes you can actually use in an
2417 allocated chunk, which may be more than you requested (although
2418 often not). You can use this many bytes without worrying about
2419 overwriting other allocated objects. Not a particularly great
2420 programming practice, but still sometimes useful.
2421
2422*/
2423
2424#if __STD_C
2425size_t malloc_usable_size(Void_t* mem)
2426#else
2427size_t malloc_usable_size(mem) Void_t* mem;
2428#endif
2429{
2430 mchunkptr p;
Kim Phillips199adb62012-10-29 13:34:32 +00002431 if (mem == NULL)
wdenk217c9da2002-10-25 20:35:49 +00002432 return 0;
2433 else
2434 {
2435 p = mem2chunk(mem);
2436 if(!chunk_is_mmapped(p))
2437 {
2438 if (!inuse(p)) return 0;
2439 check_inuse_chunk(p);
2440 return chunksize(p) - SIZE_SZ;
2441 }
2442 return chunksize(p) - 2*SIZE_SZ;
2443 }
2444}
2445
2446
Simon Glassd93041a2014-07-10 22:23:25 -06002447
wdenk217c9da2002-10-25 20:35:49 +00002448
2449/* Utility to update current_mallinfo for malloc_stats and mallinfo() */
2450
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002451#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +00002452static void malloc_update_mallinfo()
2453{
2454 int i;
2455 mbinptr b;
2456 mchunkptr p;
2457#ifdef DEBUG
2458 mchunkptr q;
2459#endif
2460
2461 INTERNAL_SIZE_T avail = chunksize(top);
2462 int navail = ((long)(avail) >= (long)MINSIZE)? 1 : 0;
2463
2464 for (i = 1; i < NAV; ++i)
2465 {
2466 b = bin_at(i);
2467 for (p = last(b); p != b; p = p->bk)
2468 {
2469#ifdef DEBUG
2470 check_free_chunk(p);
2471 for (q = next_chunk(p);
wdenk8bde7f72003-06-27 21:31:46 +00002472 q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE;
2473 q = next_chunk(q))
2474 check_inuse_chunk(q);
wdenk217c9da2002-10-25 20:35:49 +00002475#endif
2476 avail += chunksize(p);
2477 navail++;
2478 }
2479 }
2480
2481 current_mallinfo.ordblks = navail;
2482 current_mallinfo.uordblks = sbrked_mem - avail;
2483 current_mallinfo.fordblks = avail;
2484 current_mallinfo.hblks = n_mmaps;
2485 current_mallinfo.hblkhd = mmapped_mem;
2486 current_mallinfo.keepcost = chunksize(top);
2487
2488}
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002489#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +00002490
Simon Glassd93041a2014-07-10 22:23:25 -06002491
wdenk217c9da2002-10-25 20:35:49 +00002492
2493/*
2494
2495 malloc_stats:
2496
2497 Prints on the amount of space obtain from the system (both
2498 via sbrk and mmap), the maximum amount (which may be more than
2499 current if malloc_trim and/or munmap got called), the maximum
2500 number of simultaneous mmap regions used, and the current number
2501 of bytes allocated via malloc (or realloc, etc) but not yet
2502 freed. (Note that this is the number of bytes allocated, not the
2503 number requested. It will be larger than the number requested
2504 because of alignment and bookkeeping overhead.)
2505
2506*/
2507
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002508#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +00002509void malloc_stats()
2510{
2511 malloc_update_mallinfo();
2512 printf("max system bytes = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002513 (unsigned int)(max_total_mem));
wdenk217c9da2002-10-25 20:35:49 +00002514 printf("system bytes = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002515 (unsigned int)(sbrked_mem + mmapped_mem));
wdenk217c9da2002-10-25 20:35:49 +00002516 printf("in use bytes = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002517 (unsigned int)(current_mallinfo.uordblks + mmapped_mem));
wdenk217c9da2002-10-25 20:35:49 +00002518#if HAVE_MMAP
2519 printf("max mmap regions = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002520 (unsigned int)max_n_mmaps);
wdenk217c9da2002-10-25 20:35:49 +00002521#endif
2522}
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002523#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +00002524
2525/*
2526 mallinfo returns a copy of updated current mallinfo.
2527*/
2528
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002529#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +00002530struct mallinfo mALLINFo()
2531{
2532 malloc_update_mallinfo();
2533 return current_mallinfo;
2534}
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002535#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +00002536
2537
Simon Glassd93041a2014-07-10 22:23:25 -06002538
wdenk217c9da2002-10-25 20:35:49 +00002539
2540/*
2541 mallopt:
2542
2543 mallopt is the general SVID/XPG interface to tunable parameters.
2544 The format is to provide a (parameter-number, parameter-value) pair.
2545 mallopt then sets the corresponding parameter to the argument
2546 value if it can (i.e., so long as the value is meaningful),
2547 and returns 1 if successful else 0.
2548
2549 See descriptions of tunable parameters above.
2550
2551*/
2552
2553#if __STD_C
2554int mALLOPt(int param_number, int value)
2555#else
2556int mALLOPt(param_number, value) int param_number; int value;
2557#endif
2558{
2559 switch(param_number)
2560 {
2561 case M_TRIM_THRESHOLD:
2562 trim_threshold = value; return 1;
2563 case M_TOP_PAD:
2564 top_pad = value; return 1;
2565 case M_MMAP_THRESHOLD:
2566 mmap_threshold = value; return 1;
2567 case M_MMAP_MAX:
2568#if HAVE_MMAP
2569 n_mmaps_max = value; return 1;
2570#else
2571 if (value != 0) return 0; else n_mmaps_max = value; return 1;
2572#endif
2573
2574 default:
2575 return 0;
2576 }
2577}
2578
Simon Glassfb5cf7f2015-02-27 22:06:36 -07002579int initf_malloc(void)
2580{
Andy Yanf1896c42017-07-24 17:43:34 +08002581#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassfb5cf7f2015-02-27 22:06:36 -07002582 assert(gd->malloc_base); /* Set up by crt0.S */
Andy Yanf1896c42017-07-24 17:43:34 +08002583 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
Simon Glassfb5cf7f2015-02-27 22:06:36 -07002584 gd->malloc_ptr = 0;
2585#endif
2586
2587 return 0;
2588}
2589
Simon Glass62d63832022-09-06 20:27:00 -06002590void malloc_enable_testing(int max_allocs)
2591{
2592 malloc_testing = true;
2593 malloc_max_allocs = max_allocs;
2594}
2595
2596void malloc_disable_testing(void)
2597{
2598 malloc_testing = false;
2599}
2600
wdenk217c9da2002-10-25 20:35:49 +00002601/*
2602
2603History:
2604
2605 V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee)
2606 * return null for negative arguments
2607 * Added Several WIN32 cleanups from Martin C. Fong <mcfong@yahoo.com>
wdenk8bde7f72003-06-27 21:31:46 +00002608 * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h'
2609 (e.g. WIN32 platforms)
2610 * Cleanup up header file inclusion for WIN32 platforms
2611 * Cleanup code to avoid Microsoft Visual C++ compiler complaints
2612 * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing
2613 memory allocation routines
2614 * Set 'malloc_getpagesize' for WIN32 platforms (needs more work)
2615 * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to
wdenk217c9da2002-10-25 20:35:49 +00002616 usage of 'assert' in non-WIN32 code
wdenk8bde7f72003-06-27 21:31:46 +00002617 * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to
2618 avoid infinite loop
wdenk217c9da2002-10-25 20:35:49 +00002619 * Always call 'fREe()' rather than 'free()'
2620
2621 V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee)
2622 * Fixed ordering problem with boundary-stamping
2623
2624 V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee)
2625 * Added pvalloc, as recommended by H.J. Liu
2626 * Added 64bit pointer support mainly from Wolfram Gloger
2627 * Added anonymously donated WIN32 sbrk emulation
2628 * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
2629 * malloc_extend_top: fix mask error that caused wastage after
wdenk8bde7f72003-06-27 21:31:46 +00002630 foreign sbrks
wdenk217c9da2002-10-25 20:35:49 +00002631 * Add linux mremap support code from HJ Liu
2632
2633 V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee)
2634 * Integrated most documentation with the code.
2635 * Add support for mmap, with help from
wdenk8bde7f72003-06-27 21:31:46 +00002636 Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
wdenk217c9da2002-10-25 20:35:49 +00002637 * Use last_remainder in more cases.
2638 * Pack bins using idea from colin@nyx10.cs.du.edu
2639 * Use ordered bins instead of best-fit threshhold
2640 * Eliminate block-local decls to simplify tracing and debugging.
2641 * Support another case of realloc via move into top
2642 * Fix error occuring when initial sbrk_base not word-aligned.
2643 * Rely on page size for units instead of SBRK_UNIT to
wdenk8bde7f72003-06-27 21:31:46 +00002644 avoid surprises about sbrk alignment conventions.
wdenk217c9da2002-10-25 20:35:49 +00002645 * Add mallinfo, mallopt. Thanks to Raymond Nijssen
wdenk8bde7f72003-06-27 21:31:46 +00002646 (raymond@es.ele.tue.nl) for the suggestion.
wdenk217c9da2002-10-25 20:35:49 +00002647 * Add `pad' argument to malloc_trim and top_pad mallopt parameter.
2648 * More precautions for cases where other routines call sbrk,
wdenk8bde7f72003-06-27 21:31:46 +00002649 courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
wdenk217c9da2002-10-25 20:35:49 +00002650 * Added macros etc., allowing use in linux libc from
wdenk8bde7f72003-06-27 21:31:46 +00002651 H.J. Lu (hjl@gnu.ai.mit.edu)
wdenk217c9da2002-10-25 20:35:49 +00002652 * Inverted this history list
2653
2654 V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee)
2655 * Re-tuned and fixed to behave more nicely with V2.6.0 changes.
2656 * Removed all preallocation code since under current scheme
wdenk8bde7f72003-06-27 21:31:46 +00002657 the work required to undo bad preallocations exceeds
2658 the work saved in good cases for most test programs.
wdenk217c9da2002-10-25 20:35:49 +00002659 * No longer use return list or unconsolidated bins since
wdenk8bde7f72003-06-27 21:31:46 +00002660 no scheme using them consistently outperforms those that don't
2661 given above changes.
wdenk217c9da2002-10-25 20:35:49 +00002662 * Use best fit for very large chunks to prevent some worst-cases.
2663 * Added some support for debugging
2664
2665 V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee)
2666 * Removed footers when chunks are in use. Thanks to
wdenk8bde7f72003-06-27 21:31:46 +00002667 Paul Wilson (wilson@cs.texas.edu) for the suggestion.
wdenk217c9da2002-10-25 20:35:49 +00002668
2669 V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee)
2670 * Added malloc_trim, with help from Wolfram Gloger
wdenk8bde7f72003-06-27 21:31:46 +00002671 (wmglo@Dent.MED.Uni-Muenchen.DE).
wdenk217c9da2002-10-25 20:35:49 +00002672
2673 V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g)
2674
2675 V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g)
2676 * realloc: try to expand in both directions
2677 * malloc: swap order of clean-bin strategy;
2678 * realloc: only conditionally expand backwards
2679 * Try not to scavenge used bins
2680 * Use bin counts as a guide to preallocation
2681 * Occasionally bin return list chunks in first scan
2682 * Add a few optimizations from colin@nyx10.cs.du.edu
2683
2684 V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g)
2685 * faster bin computation & slightly different binning
2686 * merged all consolidations to one part of malloc proper
wdenk8bde7f72003-06-27 21:31:46 +00002687 (eliminating old malloc_find_space & malloc_clean_bin)
wdenk217c9da2002-10-25 20:35:49 +00002688 * Scan 2 returns chunks (not just 1)
2689 * Propagate failure in realloc if malloc returns 0
2690 * Add stuff to allow compilation on non-ANSI compilers
wdenk8bde7f72003-06-27 21:31:46 +00002691 from kpv@research.att.com
wdenk217c9da2002-10-25 20:35:49 +00002692
2693 V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu)
2694 * removed potential for odd address access in prev_chunk
2695 * removed dependency on getpagesize.h
2696 * misc cosmetics and a bit more internal documentation
2697 * anticosmetics: mangled names in macros to evade debugger strangeness
2698 * tested on sparc, hp-700, dec-mips, rs6000
wdenk8bde7f72003-06-27 21:31:46 +00002699 with gcc & native cc (hp, dec only) allowing
2700 Detlefs & Zorn comparison study (in SIGPLAN Notices.)
wdenk217c9da2002-10-25 20:35:49 +00002701
2702 Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu)
2703 * Based loosely on libg++-1.2X malloc. (It retains some of the overall
wdenk8bde7f72003-06-27 21:31:46 +00002704 structure of old version, but most details differ.)
wdenk217c9da2002-10-25 20:35:49 +00002705
2706*/