blob: 0100ba5b966bcacb9aa0e1bd3bad3e0f1e1755ae [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
xia.jin383142c2024-06-11 06:30:53 +000036#ifdef CONFIG_ARMV8_MULTIENTRY
37#include <spinlock.h>
38static spin_lock_t malloc_lock;
39#endif
Bo Lv96a66d02023-05-12 19:18:22 +080040
Wolfgang Denkd87080b2006-03-31 18:32:53 +020041DECLARE_GLOBAL_DATA_PTR;
42
wdenk217c9da2002-10-25 20:35:49 +000043/*
44 Emulation of sbrk for WIN32
45 All code within the ifdef WIN32 is untested by me.
46
47 Thanks to Martin Fong and others for supplying this.
48*/
49
50
51#ifdef WIN32
52
53#define AlignPage(add) (((add) + (malloc_getpagesize-1)) & \
54~(malloc_getpagesize-1))
55#define AlignPage64K(add) (((add) + (0x10000 - 1)) & ~(0x10000 - 1))
56
57/* resrve 64MB to insure large contiguous space */
58#define RESERVED_SIZE (1024*1024*64)
59#define NEXT_SIZE (2048*1024)
60#define TOP_MEMORY ((unsigned long)2*1024*1024*1024)
61
62struct GmListElement;
63typedef struct GmListElement GmListElement;
64
65struct GmListElement
66{
67 GmListElement* next;
68 void* base;
69};
70
71static GmListElement* head = 0;
72static unsigned int gNextAddress = 0;
73static unsigned int gAddressBase = 0;
74static unsigned int gAllocatedSize = 0;
75
76static
77GmListElement* makeGmListElement (void* bas)
78{
79 GmListElement* this;
80 this = (GmListElement*)(void*)LocalAlloc (0, sizeof (GmListElement));
81 assert (this);
82 if (this)
83 {
84 this->base = bas;
85 this->next = head;
86 head = this;
87 }
88 return this;
89}
90
91void gcleanup ()
92{
93 BOOL rval;
94 assert ( (head == NULL) || (head->base == (void*)gAddressBase));
95 if (gAddressBase && (gNextAddress - gAddressBase))
96 {
97 rval = VirtualFree ((void*)gAddressBase,
98 gNextAddress - gAddressBase,
99 MEM_DECOMMIT);
wdenk8bde7f72003-06-27 21:31:46 +0000100 assert (rval);
wdenk217c9da2002-10-25 20:35:49 +0000101 }
102 while (head)
103 {
104 GmListElement* next = head->next;
105 rval = VirtualFree (head->base, 0, MEM_RELEASE);
106 assert (rval);
107 LocalFree (head);
108 head = next;
109 }
110}
111
112static
113void* findRegion (void* start_address, unsigned long size)
114{
115 MEMORY_BASIC_INFORMATION info;
116 if (size >= TOP_MEMORY) return NULL;
117
118 while ((unsigned long)start_address + size < TOP_MEMORY)
119 {
120 VirtualQuery (start_address, &info, sizeof (info));
121 if ((info.State == MEM_FREE) && (info.RegionSize >= size))
122 return start_address;
123 else
124 {
wdenk8bde7f72003-06-27 21:31:46 +0000125 /* Requested region is not available so see if the */
126 /* next region is available. Set 'start_address' */
127 /* to the next region and call 'VirtualQuery()' */
128 /* again. */
wdenk217c9da2002-10-25 20:35:49 +0000129
130 start_address = (char*)info.BaseAddress + info.RegionSize;
131
wdenk8bde7f72003-06-27 21:31:46 +0000132 /* Make sure we start looking for the next region */
133 /* on the *next* 64K boundary. Otherwise, even if */
134 /* the new region is free according to */
135 /* 'VirtualQuery()', the subsequent call to */
136 /* 'VirtualAlloc()' (which follows the call to */
137 /* this routine in 'wsbrk()') will round *down* */
138 /* the requested address to a 64K boundary which */
139 /* we already know is an address in the */
140 /* unavailable region. Thus, the subsequent call */
141 /* to 'VirtualAlloc()' will fail and bring us back */
142 /* here, causing us to go into an infinite loop. */
wdenk217c9da2002-10-25 20:35:49 +0000143
144 start_address =
145 (void *) AlignPage64K((unsigned long) start_address);
146 }
147 }
148 return NULL;
149
150}
151
152
153void* wsbrk (long size)
154{
155 void* tmp;
156 if (size > 0)
157 {
158 if (gAddressBase == 0)
159 {
160 gAllocatedSize = max (RESERVED_SIZE, AlignPage (size));
161 gNextAddress = gAddressBase =
162 (unsigned int)VirtualAlloc (NULL, gAllocatedSize,
163 MEM_RESERVE, PAGE_NOACCESS);
164 } else if (AlignPage (gNextAddress + size) > (gAddressBase +
165gAllocatedSize))
166 {
167 long new_size = max (NEXT_SIZE, AlignPage (size));
168 void* new_address = (void*)(gAddressBase+gAllocatedSize);
169 do
170 {
171 new_address = findRegion (new_address, new_size);
172
Heinrich Schuchardta874cac2017-11-10 21:46:34 +0100173 if (!new_address)
wdenk217c9da2002-10-25 20:35:49 +0000174 return (void*)-1;
175
176 gAddressBase = gNextAddress =
177 (unsigned int)VirtualAlloc (new_address, new_size,
178 MEM_RESERVE, PAGE_NOACCESS);
wdenk8bde7f72003-06-27 21:31:46 +0000179 /* repeat in case of race condition */
180 /* The region that we found has been snagged */
181 /* by another thread */
wdenk217c9da2002-10-25 20:35:49 +0000182 }
183 while (gAddressBase == 0);
184
185 assert (new_address == (void*)gAddressBase);
186
187 gAllocatedSize = new_size;
188
189 if (!makeGmListElement ((void*)gAddressBase))
190 return (void*)-1;
191 }
192 if ((size + gNextAddress) > AlignPage (gNextAddress))
193 {
194 void* res;
195 res = VirtualAlloc ((void*)AlignPage (gNextAddress),
196 (size + gNextAddress -
197 AlignPage (gNextAddress)),
198 MEM_COMMIT, PAGE_READWRITE);
Heinrich Schuchardta874cac2017-11-10 21:46:34 +0100199 if (!res)
wdenk217c9da2002-10-25 20:35:49 +0000200 return (void*)-1;
201 }
202 tmp = (void*)gNextAddress;
203 gNextAddress = (unsigned int)tmp + size;
204 return tmp;
205 }
206 else if (size < 0)
207 {
208 unsigned int alignedGoal = AlignPage (gNextAddress + size);
209 /* Trim by releasing the virtual memory */
210 if (alignedGoal >= gAddressBase)
211 {
212 VirtualFree ((void*)alignedGoal, gNextAddress - alignedGoal,
213 MEM_DECOMMIT);
214 gNextAddress = gNextAddress + size;
215 return (void*)gNextAddress;
216 }
217 else
218 {
219 VirtualFree ((void*)gAddressBase, gNextAddress - gAddressBase,
220 MEM_DECOMMIT);
221 gNextAddress = gAddressBase;
222 return (void*)-1;
223 }
224 }
225 else
226 {
227 return (void*)gNextAddress;
228 }
229}
230
231#endif
232
Simon Glassd93041a2014-07-10 22:23:25 -0600233
wdenk217c9da2002-10-25 20:35:49 +0000234
235/*
236 Type declarations
237*/
238
239
240struct malloc_chunk
241{
242 INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
243 INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
244 struct malloc_chunk* fd; /* double links -- used only if free. */
245 struct malloc_chunk* bk;
Joakim Tjernlund1ba91ba2010-10-14 08:51:34 +0200246} __attribute__((__may_alias__)) ;
wdenk217c9da2002-10-25 20:35:49 +0000247
248typedef struct malloc_chunk* mchunkptr;
249
250/*
251
252 malloc_chunk details:
253
254 (The following includes lightly edited explanations by Colin Plumb.)
255
256 Chunks of memory are maintained using a `boundary tag' method as
257 described in e.g., Knuth or Standish. (See the paper by Paul
258 Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
259 survey of such techniques.) Sizes of free chunks are stored both
260 in the front of each chunk and at the end. This makes
261 consolidating fragmented chunks into bigger chunks very fast. The
262 size fields also hold bits representing whether chunks are free or
263 in use.
264
265 An allocated chunk looks like this:
266
267
268 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000269 | Size of previous chunk, if allocated | |
270 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
271 | Size of chunk, in bytes |P|
wdenk217c9da2002-10-25 20:35:49 +0000272 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000273 | User data starts here... .
274 . .
275 . (malloc_usable_space() bytes) .
276 . |
wdenk217c9da2002-10-25 20:35:49 +0000277nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000278 | Size of chunk |
279 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk217c9da2002-10-25 20:35:49 +0000280
281
282 Where "chunk" is the front of the chunk for the purpose of most of
283 the malloc code, but "mem" is the pointer that is returned to the
284 user. "Nextchunk" is the beginning of the next contiguous chunk.
285
286 Chunks always begin on even word boundries, so the mem portion
287 (which is returned to the user) is also on an even word boundary, and
288 thus double-word aligned.
289
290 Free chunks are stored in circular doubly-linked lists, and look like this:
291
292 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000293 | Size of previous chunk |
294 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk217c9da2002-10-25 20:35:49 +0000295 `head:' | Size of chunk, in bytes |P|
296 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk8bde7f72003-06-27 21:31:46 +0000297 | Forward pointer to next chunk in list |
298 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
299 | Back pointer to previous chunk in list |
300 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
301 | Unused space (may be 0 bytes long) .
302 . .
303 . |
Marek Bykowski9297e362020-04-29 18:23:07 +0200304
wdenk217c9da2002-10-25 20:35:49 +0000305nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
306 `foot:' | Size of chunk, in bytes |
wdenk8bde7f72003-06-27 21:31:46 +0000307 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
wdenk217c9da2002-10-25 20:35:49 +0000308
309 The P (PREV_INUSE) bit, stored in the unused low-order bit of the
310 chunk size (which is always a multiple of two words), is an in-use
311 bit for the *previous* chunk. If that bit is *clear*, then the
312 word before the current chunk size contains the previous chunk
313 size, and can be used to find the front of the previous chunk.
314 (The very first chunk allocated always has this bit set,
315 preventing access to non-existent (or non-owned) memory.)
316
317 Note that the `foot' of the current chunk is actually represented
318 as the prev_size of the NEXT chunk. (This makes it easier to
319 deal with alignments etc).
320
321 The two exceptions to all this are
322
323 1. The special chunk `top', which doesn't bother using the
wdenk8bde7f72003-06-27 21:31:46 +0000324 trailing size field since there is no
325 next contiguous chunk that would have to index off it. (After
326 initialization, `top' is forced to always exist. If it would
327 become less than MINSIZE bytes long, it is replenished via
328 malloc_extend_top.)
wdenk217c9da2002-10-25 20:35:49 +0000329
330 2. Chunks allocated via mmap, which have the second-lowest-order
wdenk8bde7f72003-06-27 21:31:46 +0000331 bit (IS_MMAPPED) set in their size fields. Because they are
332 never merged or traversed from any other chunk, they have no
333 foot size or inuse information.
wdenk217c9da2002-10-25 20:35:49 +0000334
335 Available chunks are kept in any of several places (all declared below):
336
337 * `av': An array of chunks serving as bin headers for consolidated
338 chunks. Each bin is doubly linked. The bins are approximately
339 proportionally (log) spaced. There are a lot of these bins
340 (128). This may look excessive, but works very well in
341 practice. All procedures maintain the invariant that no
342 consolidated chunk physically borders another one. Chunks in
343 bins are kept in size order, with ties going to the
344 approximately least recently used chunk.
345
346 The chunks in each bin are maintained in decreasing sorted order by
347 size. This is irrelevant for the small bins, which all contain
348 the same-sized chunks, but facilitates best-fit allocation for
349 larger chunks. (These lists are just sequential. Keeping them in
350 order almost never requires enough traversal to warrant using
351 fancier ordered data structures.) Chunks of the same size are
352 linked with the most recently freed at the front, and allocations
353 are taken from the back. This results in LRU or FIFO allocation
354 order, which tends to give each chunk an equal opportunity to be
355 consolidated with adjacent freed chunks, resulting in larger free
356 chunks and less fragmentation.
357
358 * `top': The top-most available chunk (i.e., the one bordering the
359 end of available memory) is treated specially. It is never
360 included in any bin, is used only if no other chunk is
361 available, and is released back to the system if it is very
362 large (see M_TRIM_THRESHOLD).
363
364 * `last_remainder': A bin holding only the remainder of the
365 most recently split (non-top) chunk. This bin is checked
366 before other non-fitting chunks, so as to provide better
367 locality for runs of sequentially allocated chunks.
368
369 * Implicitly, through the host system's memory mapping tables.
370 If supported, requests greater than a threshold are usually
371 serviced via calls to mmap, and then later released via munmap.
372
373*/
Simon Glassd93041a2014-07-10 22:23:25 -0600374
wdenk217c9da2002-10-25 20:35:49 +0000375/* sizes, alignments */
376
377#define SIZE_SZ (sizeof(INTERNAL_SIZE_T))
378#define MALLOC_ALIGNMENT (SIZE_SZ + SIZE_SZ)
379#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
380#define MINSIZE (sizeof(struct malloc_chunk))
381
382/* conversion from malloc headers to user pointers, and back */
383
Bo Lv96a66d02023-05-12 19:18:22 +0800384#ifdef CONFIG_AML_UASAN
385
386#define chunk2mem(p) ((Void_t *)((char *)(p) + \
387 (2 * SIZE_SZ + UASAN_ALLOCA_REDZONE_SIZE)))
388#define mem2chunk(mem) ((mchunkptr)((char *)(mem) - \
389 (2 * SIZE_SZ + UASAN_ALLOCA_REDZONE_SIZE)))
390
391/* insert red zone when get alloc size */
392#define request2size(req) \
393 ((((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \
394 (long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \
395 (((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK))) + \
396 UASAN_ALLOCA_REDZONE_SIZE * 2)
397
398#else /* CONFIG_AML_UASAN */
399
wdenk217c9da2002-10-25 20:35:49 +0000400#define chunk2mem(p) ((Void_t*)((char*)(p) + 2*SIZE_SZ))
401#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
402
403/* pad request bytes into a usable size */
404
405#define request2size(req) \
406 (((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \
407 (long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \
408 (((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK)))
Bo Lv96a66d02023-05-12 19:18:22 +0800409#endif /* CONFIG_AML_UASAN */
wdenk217c9da2002-10-25 20:35:49 +0000410
411/* Check if m has acceptable alignment */
412
413#define aligned_OK(m) (((unsigned long)((m)) & (MALLOC_ALIGN_MASK)) == 0)
414
415
Simon Glassd93041a2014-07-10 22:23:25 -0600416
wdenk217c9da2002-10-25 20:35:49 +0000417
418/*
419 Physical chunk operations
420*/
421
422
423/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
424
425#define PREV_INUSE 0x1
426
427/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
428
429#define IS_MMAPPED 0x2
430
431/* Bits to mask off when extracting size */
432
433#define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
434
435
436/* Ptr to next physical malloc_chunk. */
437
438#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
439
440/* Ptr to previous physical malloc_chunk */
441
442#define prev_chunk(p)\
443 ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
444
445
446/* Treat space at ptr + offset as a chunk */
447
448#define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s)))
449
450
Simon Glassd93041a2014-07-10 22:23:25 -0600451
wdenk217c9da2002-10-25 20:35:49 +0000452
453/*
454 Dealing with use bits
455*/
456
457/* extract p's inuse bit */
458
459#define inuse(p)\
460((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
461
462/* extract inuse bit of previous chunk */
463
464#define prev_inuse(p) ((p)->size & PREV_INUSE)
465
466/* check for mmap()'ed chunk */
467
468#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
469
470/* set/clear chunk as in use without otherwise disturbing */
471
472#define set_inuse(p)\
473((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
474
475#define clear_inuse(p)\
476((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
477
478/* check/set/clear inuse bits in known places */
479
480#define inuse_bit_at_offset(p, s)\
481 (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
482
483#define set_inuse_bit_at_offset(p, s)\
484 (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
485
486#define clear_inuse_bit_at_offset(p, s)\
487 (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
488
489
Simon Glassd93041a2014-07-10 22:23:25 -0600490
wdenk217c9da2002-10-25 20:35:49 +0000491
492/*
493 Dealing with size fields
494*/
495
496/* Get size, ignoring use bits */
497
498#define chunksize(p) ((p)->size & ~(SIZE_BITS))
499
500/* Set size at head, without disturbing its use bit */
501
502#define set_head_size(p, s) ((p)->size = (((p)->size & PREV_INUSE) | (s)))
503
504/* Set size/use ignoring previous bits in header */
505
506#define set_head(p, s) ((p)->size = (s))
507
508/* Set size at footer (only when chunk is not in use) */
509
510#define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
511
512
Simon Glassd93041a2014-07-10 22:23:25 -0600513
wdenk217c9da2002-10-25 20:35:49 +0000514
515
516/*
517 Bins
518
519 The bins, `av_' are an array of pairs of pointers serving as the
520 heads of (initially empty) doubly-linked lists of chunks, laid out
521 in a way so that each pair can be treated as if it were in a
522 malloc_chunk. (This way, the fd/bk offsets for linking bin heads
523 and chunks are the same).
524
525 Bins for sizes < 512 bytes contain chunks of all the same size, spaced
526 8 bytes apart. Larger bins are approximately logarithmically
527 spaced. (See the table below.) The `av_' array is never mentioned
528 directly in the code, but instead via bin access macros.
529
530 Bin layout:
531
532 64 bins of size 8
533 32 bins of size 64
534 16 bins of size 512
535 8 bins of size 4096
536 4 bins of size 32768
537 2 bins of size 262144
538 1 bin of size what's left
539
540 There is actually a little bit of slop in the numbers in bin_index
541 for the sake of speed. This makes no difference elsewhere.
542
543 The special chunks `top' and `last_remainder' get their own bins,
544 (this is implemented via yet more trickery with the av_ array),
545 although `top' is never properly linked to its bin since it is
546 always handled specially.
547
548*/
549
550#define NAV 128 /* number of bins */
551
552typedef struct malloc_chunk* mbinptr;
553
554/* access macros */
555
556#define bin_at(i) ((mbinptr)((char*)&(av_[2*(i) + 2]) - 2*SIZE_SZ))
557#define next_bin(b) ((mbinptr)((char*)(b) + 2 * sizeof(mbinptr)))
558#define prev_bin(b) ((mbinptr)((char*)(b) - 2 * sizeof(mbinptr)))
559
560/*
561 The first 2 bins are never indexed. The corresponding av_ cells are instead
562 used for bookkeeping. This is not to save space, but to simplify
563 indexing, maintain locality, and avoid some initialization tests.
564*/
565
Stefan Roesef2302d42008-08-06 14:05:38 +0200566#define top (av_[2]) /* The topmost chunk */
wdenk217c9da2002-10-25 20:35:49 +0000567#define last_remainder (bin_at(1)) /* remainder from last split */
568
569
570/*
571 Because top initially points to its own bin with initial
572 zero size, thus forcing extension on the first malloc request,
573 we avoid having any special code in malloc to check whether
574 it even exists yet. But we still need to in malloc_extend_top.
575*/
576
577#define initial_top ((mchunkptr)(bin_at(0)))
578
579/* Helper macro to initialize bins */
580
581#define IAV(i) bin_at(i), bin_at(i)
582
583static mbinptr av_[NAV * 2 + 2] = {
Kim Phillips199adb62012-10-29 13:34:32 +0000584 NULL, NULL,
wdenk217c9da2002-10-25 20:35:49 +0000585 IAV(0), IAV(1), IAV(2), IAV(3), IAV(4), IAV(5), IAV(6), IAV(7),
586 IAV(8), IAV(9), IAV(10), IAV(11), IAV(12), IAV(13), IAV(14), IAV(15),
587 IAV(16), IAV(17), IAV(18), IAV(19), IAV(20), IAV(21), IAV(22), IAV(23),
588 IAV(24), IAV(25), IAV(26), IAV(27), IAV(28), IAV(29), IAV(30), IAV(31),
589 IAV(32), IAV(33), IAV(34), IAV(35), IAV(36), IAV(37), IAV(38), IAV(39),
590 IAV(40), IAV(41), IAV(42), IAV(43), IAV(44), IAV(45), IAV(46), IAV(47),
591 IAV(48), IAV(49), IAV(50), IAV(51), IAV(52), IAV(53), IAV(54), IAV(55),
592 IAV(56), IAV(57), IAV(58), IAV(59), IAV(60), IAV(61), IAV(62), IAV(63),
593 IAV(64), IAV(65), IAV(66), IAV(67), IAV(68), IAV(69), IAV(70), IAV(71),
594 IAV(72), IAV(73), IAV(74), IAV(75), IAV(76), IAV(77), IAV(78), IAV(79),
595 IAV(80), IAV(81), IAV(82), IAV(83), IAV(84), IAV(85), IAV(86), IAV(87),
596 IAV(88), IAV(89), IAV(90), IAV(91), IAV(92), IAV(93), IAV(94), IAV(95),
597 IAV(96), IAV(97), IAV(98), IAV(99), IAV(100), IAV(101), IAV(102), IAV(103),
598 IAV(104), IAV(105), IAV(106), IAV(107), IAV(108), IAV(109), IAV(110), IAV(111),
599 IAV(112), IAV(113), IAV(114), IAV(115), IAV(116), IAV(117), IAV(118), IAV(119),
600 IAV(120), IAV(121), IAV(122), IAV(123), IAV(124), IAV(125), IAV(126), IAV(127)
601};
602
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200603#ifdef CONFIG_NEEDS_MANUAL_RELOC
Gabor Juhos7b395232013-01-21 21:10:38 +0000604static void malloc_bin_reloc(void)
wdenk217c9da2002-10-25 20:35:49 +0000605{
Simon Glass93691842012-09-04 11:31:07 +0000606 mbinptr *p = &av_[2];
607 size_t i;
608
609 for (i = 2; i < ARRAY_SIZE(av_); ++i, ++p)
610 *p = (mbinptr)((ulong)*p + gd->reloc_off);
wdenk217c9da2002-10-25 20:35:49 +0000611}
Gabor Juhos7b395232013-01-21 21:10:38 +0000612#else
613static inline void malloc_bin_reloc(void) {}
Peter Tyser521af042009-09-21 11:20:36 -0500614#endif
Peter Tyser5e93bd12009-08-21 23:05:19 -0500615
Marek Bykowski9297e362020-04-29 18:23:07 +0200616#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
617static void malloc_init(void);
618#endif
619
Peter Tyser5e93bd12009-08-21 23:05:19 -0500620ulong mem_malloc_start = 0;
621ulong mem_malloc_end = 0;
622ulong mem_malloc_brk = 0;
623
Simon Glass62d63832022-09-06 20:27:00 -0600624static bool malloc_testing; /* enable test mode */
625static int malloc_max_allocs; /* return NULL after this many calls to malloc() */
626
Peter Tyser5e93bd12009-08-21 23:05:19 -0500627void *sbrk(ptrdiff_t increment)
628{
629 ulong old = mem_malloc_brk;
630 ulong new = old + increment;
631
Kumar Gala6163f5b2010-11-15 18:41:43 -0600632 /*
633 * if we are giving memory back make sure we clear it out since
634 * we set MORECORE_CLEARS to 1
635 */
636 if (increment < 0)
637 memset((void *)new, 0, -increment);
638
Peter Tyser5e93bd12009-08-21 23:05:19 -0500639 if ((new < mem_malloc_start) || (new > mem_malloc_end))
karl.beldan@gmail.comae30b8c2010-04-06 22:18:08 +0200640 return (void *)MORECORE_FAILURE;
Peter Tyser5e93bd12009-08-21 23:05:19 -0500641
642 mem_malloc_brk = new;
643
644 return (void *)old;
645}
wdenk217c9da2002-10-25 20:35:49 +0000646
Peter Tyserd4e8ada2009-08-21 23:05:21 -0500647void mem_malloc_init(ulong start, ulong size)
648{
649 mem_malloc_start = start;
650 mem_malloc_end = start + size;
651 mem_malloc_brk = start;
652
Marek Bykowski9297e362020-04-29 18:23:07 +0200653#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
654 malloc_init();
655#endif
656
Thierry Reding868de512014-08-26 17:34:22 +0200657 debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
658 mem_malloc_end);
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +0100659#ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
660 memset((void *)mem_malloc_start, 0x0, size);
661#endif
Gabor Juhos7b395232013-01-21 21:10:38 +0000662 malloc_bin_reloc();
xia.jin383142c2024-06-11 06:30:53 +0000663#ifdef CONFIG_ARMV8_MULTIENTRY
664 spin_lock_init(&malloc_lock);
665#endif
Peter Tyserd4e8ada2009-08-21 23:05:21 -0500666}
Peter Tyserd4e8ada2009-08-21 23:05:21 -0500667
wdenk217c9da2002-10-25 20:35:49 +0000668/* field-extraction macros */
669
670#define first(b) ((b)->fd)
671#define last(b) ((b)->bk)
672
673/*
674 Indexing into bins
675*/
676
677#define bin_index(sz) \
678(((((unsigned long)(sz)) >> 9) == 0) ? (((unsigned long)(sz)) >> 3): \
679 ((((unsigned long)(sz)) >> 9) <= 4) ? 56 + (((unsigned long)(sz)) >> 6): \
680 ((((unsigned long)(sz)) >> 9) <= 20) ? 91 + (((unsigned long)(sz)) >> 9): \
681 ((((unsigned long)(sz)) >> 9) <= 84) ? 110 + (((unsigned long)(sz)) >> 12): \
682 ((((unsigned long)(sz)) >> 9) <= 340) ? 119 + (((unsigned long)(sz)) >> 15): \
683 ((((unsigned long)(sz)) >> 9) <= 1364) ? 124 + (((unsigned long)(sz)) >> 18): \
wdenk8bde7f72003-06-27 21:31:46 +0000684 126)
wdenk217c9da2002-10-25 20:35:49 +0000685/*
686 bins for chunks < 512 are all spaced 8 bytes apart, and hold
687 identically sized chunks. This is exploited in malloc.
688*/
689
690#define MAX_SMALLBIN 63
691#define MAX_SMALLBIN_SIZE 512
692#define SMALLBIN_WIDTH 8
693
694#define smallbin_index(sz) (((unsigned long)(sz)) >> 3)
695
696/*
697 Requests are `small' if both the corresponding and the next bin are small
698*/
699
700#define is_small_request(nb) (nb < MAX_SMALLBIN_SIZE - SMALLBIN_WIDTH)
701
Simon Glassd93041a2014-07-10 22:23:25 -0600702
wdenk217c9da2002-10-25 20:35:49 +0000703
704/*
705 To help compensate for the large number of bins, a one-level index
706 structure is used for bin-by-bin searching. `binblocks' is a
707 one-word bitvector recording whether groups of BINBLOCKWIDTH bins
708 have any (possibly) non-empty bins, so they can be skipped over
709 all at once during during traversals. The bits are NOT always
710 cleared as soon as all bins in a block are empty, but instead only
711 when all are noticed to be empty during traversal in malloc.
712*/
713
714#define BINBLOCKWIDTH 4 /* bins per block */
715
Stefan Roesef2302d42008-08-06 14:05:38 +0200716#define binblocks_r ((INTERNAL_SIZE_T)av_[1]) /* bitvector of nonempty blocks */
717#define binblocks_w (av_[1])
wdenk217c9da2002-10-25 20:35:49 +0000718
719/* bin<->block macros */
720
721#define idx2binblock(ix) ((unsigned)1 << (ix / BINBLOCKWIDTH))
Stefan Roesef2302d42008-08-06 14:05:38 +0200722#define mark_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r | idx2binblock(ii)))
723#define clear_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r & ~(idx2binblock(ii))))
wdenk217c9da2002-10-25 20:35:49 +0000724
725
Simon Glassd93041a2014-07-10 22:23:25 -0600726
wdenk217c9da2002-10-25 20:35:49 +0000727
728
729/* Other static bookkeeping data */
730
731/* variables holding tunable values */
732
733static unsigned long trim_threshold = DEFAULT_TRIM_THRESHOLD;
734static unsigned long top_pad = DEFAULT_TOP_PAD;
735static unsigned int n_mmaps_max = DEFAULT_MMAP_MAX;
736static unsigned long mmap_threshold = DEFAULT_MMAP_THRESHOLD;
737
738/* The first value returned from sbrk */
739static char* sbrk_base = (char*)(-1);
740
741/* The maximum memory obtained from system via sbrk */
742static unsigned long max_sbrked_mem = 0;
743
744/* The maximum via either sbrk or mmap */
745static unsigned long max_total_mem = 0;
746
747/* internal working copy of mallinfo */
748static struct mallinfo current_mallinfo = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
749
750/* The total memory obtained from system via sbrk */
751#define sbrked_mem (current_mallinfo.arena)
752
753/* Tracking mmaps */
754
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200755#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +0000756static unsigned int n_mmaps = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200757#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +0000758static unsigned long mmapped_mem = 0;
759#if HAVE_MMAP
760static unsigned int max_n_mmaps = 0;
761static unsigned long max_mmapped_mem = 0;
762#endif
763
Marek Bykowski9297e362020-04-29 18:23:07 +0200764#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
765static void malloc_init(void)
766{
767 int i, j;
Simon Glassd93041a2014-07-10 22:23:25 -0600768
Marek Bykowski9297e362020-04-29 18:23:07 +0200769 debug("bins (av_ array) are at %p\n", (void *)av_);
770
771 av_[0] = NULL; av_[1] = NULL;
772 for (i = 2, j = 2; i < NAV * 2 + 2; i += 2, j++) {
773 av_[i] = bin_at(j - 2);
774 av_[i + 1] = bin_at(j - 2);
775
776 /* Just print the first few bins so that
777 * we can see there are alright.
778 */
779 if (i < 10)
780 debug("av_[%d]=%lx av_[%d]=%lx\n",
781 i, (ulong)av_[i],
782 i + 1, (ulong)av_[i + 1]);
783 }
784
785 /* Init the static bookkeeping as well */
786 sbrk_base = (char *)(-1);
787 max_sbrked_mem = 0;
788 max_total_mem = 0;
789#ifdef DEBUG
790 memset((void *)&current_mallinfo, 0, sizeof(struct mallinfo));
791#endif
792}
793#endif
wdenk217c9da2002-10-25 20:35:49 +0000794
795/*
796 Debugging support
797*/
798
799#ifdef DEBUG
800
801
802/*
803 These routines make a number of assertions about the states
804 of data structures that should be true at all times. If any
805 are not true, it's very likely that a user program has somehow
806 trashed memory. (It's also possible that there is a coding error
807 in malloc. In which case, please report it!)
808*/
809
810#if __STD_C
811static void do_check_chunk(mchunkptr p)
812#else
813static void do_check_chunk(p) mchunkptr p;
814#endif
815{
wdenk217c9da2002-10-25 20:35:49 +0000816 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +0000817
818 /* No checkable chunk is mmapped */
819 assert(!chunk_is_mmapped(p));
820
821 /* Check for legal address ... */
822 assert((char*)p >= sbrk_base);
823 if (p != top)
824 assert((char*)p + sz <= (char*)top);
825 else
826 assert((char*)p + sz <= sbrk_base + sbrked_mem);
827
828}
829
830
831#if __STD_C
832static void do_check_free_chunk(mchunkptr p)
833#else
834static void do_check_free_chunk(p) mchunkptr p;
835#endif
836{
837 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +0000838 mchunkptr next = chunk_at_offset(p, sz);
wdenk217c9da2002-10-25 20:35:49 +0000839
840 do_check_chunk(p);
841
842 /* Check whether it claims to be free ... */
843 assert(!inuse(p));
844
845 /* Unless a special marker, must have OK fields */
846 if ((long)sz >= (long)MINSIZE)
847 {
848 assert((sz & MALLOC_ALIGN_MASK) == 0);
849 assert(aligned_OK(chunk2mem(p)));
850 /* ... matching footer field */
851 assert(next->prev_size == sz);
852 /* ... and is fully consolidated */
853 assert(prev_inuse(p));
854 assert (next == top || inuse(next));
855
856 /* ... and has minimally sane links */
857 assert(p->fd->bk == p);
858 assert(p->bk->fd == p);
859 }
860 else /* markers are always of size SIZE_SZ */
861 assert(sz == SIZE_SZ);
862}
863
864#if __STD_C
865static void do_check_inuse_chunk(mchunkptr p)
866#else
867static void do_check_inuse_chunk(p) mchunkptr p;
868#endif
869{
870 mchunkptr next = next_chunk(p);
871 do_check_chunk(p);
872
873 /* Check whether it claims to be in use ... */
874 assert(inuse(p));
875
876 /* ... and is surrounded by OK chunks.
877 Since more things can be checked with free chunks than inuse ones,
878 if an inuse chunk borders them and debug is on, it's worth doing them.
879 */
880 if (!prev_inuse(p))
881 {
882 mchunkptr prv = prev_chunk(p);
883 assert(next_chunk(prv) == p);
884 do_check_free_chunk(prv);
885 }
886 if (next == top)
887 {
888 assert(prev_inuse(next));
889 assert(chunksize(next) >= MINSIZE);
890 }
891 else if (!inuse(next))
892 do_check_free_chunk(next);
893
894}
895
896#if __STD_C
897static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s)
898#else
899static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s;
900#endif
901{
wdenk217c9da2002-10-25 20:35:49 +0000902 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
903 long room = sz - s;
wdenk217c9da2002-10-25 20:35:49 +0000904
905 do_check_inuse_chunk(p);
906
907 /* Legal size ... */
908 assert((long)sz >= (long)MINSIZE);
909 assert((sz & MALLOC_ALIGN_MASK) == 0);
910 assert(room >= 0);
911 assert(room < (long)MINSIZE);
912
913 /* ... and alignment */
914 assert(aligned_OK(chunk2mem(p)));
915
916
917 /* ... and was allocated at front of an available chunk */
918 assert(prev_inuse(p));
919
920}
921
922
923#define check_free_chunk(P) do_check_free_chunk(P)
924#define check_inuse_chunk(P) do_check_inuse_chunk(P)
925#define check_chunk(P) do_check_chunk(P)
926#define check_malloced_chunk(P,N) do_check_malloced_chunk(P,N)
927#else
928#define check_free_chunk(P)
929#define check_inuse_chunk(P)
930#define check_chunk(P)
931#define check_malloced_chunk(P,N)
932#endif
933
Simon Glassd93041a2014-07-10 22:23:25 -0600934
wdenk217c9da2002-10-25 20:35:49 +0000935
936/*
937 Macro-based internal utilities
938*/
939
940
941/*
942 Linking chunks in bin lists.
943 Call these only with variables, not arbitrary expressions, as arguments.
944*/
945
946/*
947 Place chunk p of size s in its bin, in size order,
948 putting it ahead of others of same size.
949*/
950
951
952#define frontlink(P, S, IDX, BK, FD) \
953{ \
954 if (S < MAX_SMALLBIN_SIZE) \
955 { \
956 IDX = smallbin_index(S); \
957 mark_binblock(IDX); \
958 BK = bin_at(IDX); \
959 FD = BK->fd; \
960 P->bk = BK; \
961 P->fd = FD; \
962 FD->bk = BK->fd = P; \
963 } \
964 else \
965 { \
966 IDX = bin_index(S); \
967 BK = bin_at(IDX); \
968 FD = BK->fd; \
969 if (FD == BK) mark_binblock(IDX); \
970 else \
971 { \
972 while (FD != BK && S < chunksize(FD)) FD = FD->fd; \
973 BK = FD->bk; \
974 } \
975 P->bk = BK; \
976 P->fd = FD; \
977 FD->bk = BK->fd = P; \
978 } \
979}
980
981
982/* take a chunk off a list */
983
984#define unlink(P, BK, FD) \
985{ \
986 BK = P->bk; \
987 FD = P->fd; \
988 FD->bk = BK; \
989 BK->fd = FD; \
990} \
991
992/* Place p as the last remainder */
993
994#define link_last_remainder(P) \
995{ \
996 last_remainder->fd = last_remainder->bk = P; \
997 P->fd = P->bk = last_remainder; \
998}
999
1000/* Clear the last_remainder bin */
1001
1002#define clear_last_remainder \
1003 (last_remainder->fd = last_remainder->bk = last_remainder)
1004
1005
Simon Glassd93041a2014-07-10 22:23:25 -06001006
wdenk217c9da2002-10-25 20:35:49 +00001007
1008
1009/* Routines dealing with mmap(). */
1010
1011#if HAVE_MMAP
1012
1013#if __STD_C
1014static mchunkptr mmap_chunk(size_t size)
1015#else
1016static mchunkptr mmap_chunk(size) size_t size;
1017#endif
1018{
1019 size_t page_mask = malloc_getpagesize - 1;
1020 mchunkptr p;
1021
1022#ifndef MAP_ANONYMOUS
1023 static int fd = -1;
1024#endif
1025
1026 if(n_mmaps >= n_mmaps_max) return 0; /* too many regions */
1027
1028 /* For mmapped chunks, the overhead is one SIZE_SZ unit larger, because
1029 * there is no following chunk whose prev_size field could be used.
1030 */
1031 size = (size + SIZE_SZ + page_mask) & ~page_mask;
1032
1033#ifdef MAP_ANONYMOUS
1034 p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE,
1035 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1036#else /* !MAP_ANONYMOUS */
1037 if (fd < 0)
1038 {
1039 fd = open("/dev/zero", O_RDWR);
1040 if(fd < 0) return 0;
1041 }
1042 p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1043#endif
1044
1045 if(p == (mchunkptr)-1) return 0;
1046
1047 n_mmaps++;
1048 if (n_mmaps > max_n_mmaps) max_n_mmaps = n_mmaps;
1049
1050 /* We demand that eight bytes into a page must be 8-byte aligned. */
1051 assert(aligned_OK(chunk2mem(p)));
1052
1053 /* The offset to the start of the mmapped region is stored
1054 * in the prev_size field of the chunk; normally it is zero,
1055 * but that can be changed in memalign().
1056 */
1057 p->prev_size = 0;
1058 set_head(p, size|IS_MMAPPED);
1059
1060 mmapped_mem += size;
1061 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
1062 max_mmapped_mem = mmapped_mem;
1063 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1064 max_total_mem = mmapped_mem + sbrked_mem;
1065 return p;
1066}
1067
1068#if __STD_C
1069static void munmap_chunk(mchunkptr p)
1070#else
1071static void munmap_chunk(p) mchunkptr p;
1072#endif
1073{
1074 INTERNAL_SIZE_T size = chunksize(p);
1075 int ret;
1076
1077 assert (chunk_is_mmapped(p));
1078 assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
1079 assert((n_mmaps > 0));
1080 assert(((p->prev_size + size) & (malloc_getpagesize-1)) == 0);
1081
1082 n_mmaps--;
1083 mmapped_mem -= (size + p->prev_size);
1084
1085 ret = munmap((char *)p - p->prev_size, size + p->prev_size);
1086
1087 /* munmap returns non-zero on failure */
1088 assert(ret == 0);
1089}
1090
1091#if HAVE_MREMAP
1092
1093#if __STD_C
1094static mchunkptr mremap_chunk(mchunkptr p, size_t new_size)
1095#else
1096static mchunkptr mremap_chunk(p, new_size) mchunkptr p; size_t new_size;
1097#endif
1098{
1099 size_t page_mask = malloc_getpagesize - 1;
1100 INTERNAL_SIZE_T offset = p->prev_size;
1101 INTERNAL_SIZE_T size = chunksize(p);
1102 char *cp;
1103
1104 assert (chunk_is_mmapped(p));
1105 assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
1106 assert((n_mmaps > 0));
1107 assert(((size + offset) & (malloc_getpagesize-1)) == 0);
1108
1109 /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
1110 new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask;
1111
1112 cp = (char *)mremap((char *)p - offset, size + offset, new_size, 1);
1113
1114 if (cp == (char *)-1) return 0;
1115
1116 p = (mchunkptr)(cp + offset);
1117
1118 assert(aligned_OK(chunk2mem(p)));
1119
1120 assert((p->prev_size == offset));
1121 set_head(p, (new_size - offset)|IS_MMAPPED);
1122
1123 mmapped_mem -= size + offset;
1124 mmapped_mem += new_size;
1125 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
1126 max_mmapped_mem = mmapped_mem;
1127 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1128 max_total_mem = mmapped_mem + sbrked_mem;
1129 return p;
1130}
1131
1132#endif /* HAVE_MREMAP */
1133
1134#endif /* HAVE_MMAP */
1135
wdenk217c9da2002-10-25 20:35:49 +00001136/*
1137 Extend the top-most chunk by obtaining memory from system.
1138 Main interface to sbrk (but see also malloc_trim).
1139*/
1140
1141#if __STD_C
1142static void malloc_extend_top(INTERNAL_SIZE_T nb)
1143#else
1144static void malloc_extend_top(nb) INTERNAL_SIZE_T nb;
1145#endif
1146{
1147 char* brk; /* return value from sbrk */
1148 INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of sbrked space */
1149 INTERNAL_SIZE_T correction; /* bytes for 2nd sbrk call */
1150 char* new_brk; /* return of 2nd sbrk call */
1151 INTERNAL_SIZE_T top_size; /* new size of top chunk */
1152
1153 mchunkptr old_top = top; /* Record state of old top */
1154 INTERNAL_SIZE_T old_top_size = chunksize(old_top);
1155 char* old_end = (char*)(chunk_at_offset(old_top, old_top_size));
1156
1157 /* Pad request with top_pad plus minimal overhead */
1158
1159 INTERNAL_SIZE_T sbrk_size = nb + top_pad + MINSIZE;
1160 unsigned long pagesz = malloc_getpagesize;
1161
1162 /* If not the first time through, round to preserve page boundary */
1163 /* Otherwise, we need to correct to a page size below anyway. */
1164 /* (We also correct below if an intervening foreign sbrk call.) */
1165
1166 if (sbrk_base != (char*)(-1))
1167 sbrk_size = (sbrk_size + (pagesz - 1)) & ~(pagesz - 1);
1168
1169 brk = (char*)(MORECORE (sbrk_size));
1170
1171 /* Fail if sbrk failed or if a foreign sbrk call killed our space */
1172 if (brk == (char*)(MORECORE_FAILURE) ||
1173 (brk < old_end && old_top != initial_top))
1174 return;
1175
1176 sbrked_mem += sbrk_size;
1177
1178 if (brk == old_end) /* can just add bytes to current top */
1179 {
1180 top_size = sbrk_size + old_top_size;
1181 set_head(top, top_size | PREV_INUSE);
1182 }
1183 else
1184 {
1185 if (sbrk_base == (char*)(-1)) /* First time through. Record base */
1186 sbrk_base = brk;
1187 else /* Someone else called sbrk(). Count those bytes as sbrked_mem. */
1188 sbrked_mem += brk - (char*)old_end;
1189
1190 /* Guarantee alignment of first new chunk made from this space */
1191 front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK;
1192 if (front_misalign > 0)
1193 {
1194 correction = (MALLOC_ALIGNMENT) - front_misalign;
1195 brk += correction;
1196 }
1197 else
1198 correction = 0;
1199
1200 /* Guarantee the next brk will be at a page boundary */
1201
1202 correction += ((((unsigned long)(brk + sbrk_size))+(pagesz-1)) &
wdenk8bde7f72003-06-27 21:31:46 +00001203 ~(pagesz - 1)) - ((unsigned long)(brk + sbrk_size));
wdenk217c9da2002-10-25 20:35:49 +00001204
1205 /* Allocate correction */
1206 new_brk = (char*)(MORECORE (correction));
1207 if (new_brk == (char*)(MORECORE_FAILURE)) return;
1208
1209 sbrked_mem += correction;
1210
1211 top = (mchunkptr)brk;
1212 top_size = new_brk - brk + correction;
1213 set_head(top, top_size | PREV_INUSE);
1214
1215 if (old_top != initial_top)
1216 {
1217
1218 /* There must have been an intervening foreign sbrk call. */
1219 /* A double fencepost is necessary to prevent consolidation */
1220
1221 /* If not enough space to do this, then user did something very wrong */
1222 if (old_top_size < MINSIZE)
1223 {
wdenk8bde7f72003-06-27 21:31:46 +00001224 set_head(top, PREV_INUSE); /* will force null return from malloc */
1225 return;
wdenk217c9da2002-10-25 20:35:49 +00001226 }
1227
1228 /* Also keep size a multiple of MALLOC_ALIGNMENT */
1229 old_top_size = (old_top_size - 3*SIZE_SZ) & ~MALLOC_ALIGN_MASK;
1230 set_head_size(old_top, old_top_size);
1231 chunk_at_offset(old_top, old_top_size )->size =
wdenk8bde7f72003-06-27 21:31:46 +00001232 SIZE_SZ|PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +00001233 chunk_at_offset(old_top, old_top_size + SIZE_SZ)->size =
wdenk8bde7f72003-06-27 21:31:46 +00001234 SIZE_SZ|PREV_INUSE;
wdenk217c9da2002-10-25 20:35:49 +00001235 /* If possible, release the rest. */
1236 if (old_top_size >= MINSIZE)
wdenk8bde7f72003-06-27 21:31:46 +00001237 fREe(chunk2mem(old_top));
wdenk217c9da2002-10-25 20:35:49 +00001238 }
1239 }
1240
1241 if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem)
1242 max_sbrked_mem = sbrked_mem;
1243 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1244 max_total_mem = mmapped_mem + sbrked_mem;
1245
1246 /* We always land on a page boundary */
1247 assert(((unsigned long)((char*)top + top_size) & (pagesz - 1)) == 0);
1248}
1249
1250
Simon Glassd93041a2014-07-10 22:23:25 -06001251
wdenk217c9da2002-10-25 20:35:49 +00001252
1253/* Main public routines */
1254
1255
1256/*
1257 Malloc Algorthim:
1258
1259 The requested size is first converted into a usable form, `nb'.
1260 This currently means to add 4 bytes overhead plus possibly more to
1261 obtain 8-byte alignment and/or to obtain a size of at least
1262 MINSIZE (currently 16 bytes), the smallest allocatable size.
1263 (All fits are considered `exact' if they are within MINSIZE bytes.)
1264
1265 From there, the first successful of the following steps is taken:
1266
1267 1. The bin corresponding to the request size is scanned, and if
wdenk8bde7f72003-06-27 21:31:46 +00001268 a chunk of exactly the right size is found, it is taken.
wdenk217c9da2002-10-25 20:35:49 +00001269
1270 2. The most recently remaindered chunk is used if it is big
wdenk8bde7f72003-06-27 21:31:46 +00001271 enough. This is a form of (roving) first fit, used only in
1272 the absence of exact fits. Runs of consecutive requests use
1273 the remainder of the chunk used for the previous such request
1274 whenever possible. This limited use of a first-fit style
1275 allocation strategy tends to give contiguous chunks
1276 coextensive lifetimes, which improves locality and can reduce
1277 fragmentation in the long run.
wdenk217c9da2002-10-25 20:35:49 +00001278
1279 3. Other bins are scanned in increasing size order, using a
wdenk8bde7f72003-06-27 21:31:46 +00001280 chunk big enough to fulfill the request, and splitting off
1281 any remainder. This search is strictly by best-fit; i.e.,
1282 the smallest (with ties going to approximately the least
1283 recently used) chunk that fits is selected.
wdenk217c9da2002-10-25 20:35:49 +00001284
1285 4. If large enough, the chunk bordering the end of memory
wdenk8bde7f72003-06-27 21:31:46 +00001286 (`top') is split off. (This use of `top' is in accord with
1287 the best-fit search rule. In effect, `top' is treated as
1288 larger (and thus less well fitting) than any other available
1289 chunk since it can be extended to be as large as necessary
1290 (up to system limitations).
wdenk217c9da2002-10-25 20:35:49 +00001291
1292 5. If the request size meets the mmap threshold and the
wdenk8bde7f72003-06-27 21:31:46 +00001293 system supports mmap, and there are few enough currently
1294 allocated mmapped regions, and a call to mmap succeeds,
1295 the request is allocated via direct memory mapping.
wdenk217c9da2002-10-25 20:35:49 +00001296
1297 6. Otherwise, the top of memory is extended by
wdenk8bde7f72003-06-27 21:31:46 +00001298 obtaining more space from the system (normally using sbrk,
1299 but definable to anything else via the MORECORE macro).
1300 Memory is gathered from the system (in system page-sized
1301 units) in a way that allows chunks obtained across different
1302 sbrk calls to be consolidated, but does not require
1303 contiguous memory. Thus, it should be safe to intersperse
1304 mallocs with other sbrk calls.
wdenk217c9da2002-10-25 20:35:49 +00001305
1306
1307 All allocations are made from the the `lowest' part of any found
1308 chunk. (The implementation invariant is that prev_inuse is
1309 always true of any allocated chunk; i.e., that each allocated
1310 chunk borders either a previously allocated and still in-use chunk,
1311 or the base of its memory arena.)
1312
1313*/
1314
1315#if __STD_C
1316Void_t* mALLOc(size_t bytes)
1317#else
1318Void_t* mALLOc(bytes) size_t bytes;
1319#endif
1320{
1321 mchunkptr victim; /* inspected/selected chunk */
1322 INTERNAL_SIZE_T victim_size; /* its size */
1323 int idx; /* index for bin traversal */
1324 mbinptr bin; /* associated bin */
1325 mchunkptr remainder; /* remainder from a split */
1326 long remainder_size; /* its size */
1327 int remainder_index; /* its bin index */
1328 unsigned long block; /* block traverser bit */
1329 int startidx; /* first bin of a traversed block */
1330 mchunkptr fwd; /* misc temp for linking */
1331 mchunkptr bck; /* misc temp for linking */
1332 mbinptr q; /* misc temp */
1333
1334 INTERNAL_SIZE_T nb;
1335
Andy Yanf1896c42017-07-24 17:43:34 +08001336#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Stephen Warrendeff6fb2016-03-05 10:30:53 -07001337 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
Simon Glassc9356be2014-11-10 17:16:43 -07001338 return malloc_simple(bytes);
Simon Glassd59476b2014-07-10 22:23:28 -06001339#endif
1340
Simon Glass62d63832022-09-06 20:27:00 -06001341 if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) {
1342 if (--malloc_max_allocs < 0)
1343 return NULL;
1344 }
1345
Wolfgang Denk27405442010-01-15 11:20:10 +01001346 /* check if mem_malloc_init() was run */
1347 if ((mem_malloc_start == 0) && (mem_malloc_end == 0)) {
1348 /* not initialized yet */
Kim Phillips199adb62012-10-29 13:34:32 +00001349 return NULL;
Wolfgang Denk27405442010-01-15 11:20:10 +01001350 }
1351
Kim Phillips199adb62012-10-29 13:34:32 +00001352 if ((long)bytes < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00001353
xia.jin383142c2024-06-11 06:30:53 +00001354#ifdef CONFIG_ARMV8_MULTIENTRY
1355 spin_lock(&malloc_lock);
1356#endif
wdenk217c9da2002-10-25 20:35:49 +00001357 nb = request2size(bytes); /* padded request size; */
1358
1359 /* Check for exact match in a bin */
1360
1361 if (is_small_request(nb)) /* Faster version for small requests */
1362 {
1363 idx = smallbin_index(nb);
1364
1365 /* No traversal or size check necessary for small bins. */
1366
1367 q = bin_at(idx);
1368 victim = last(q);
1369
1370 /* Also scan the next one, since it would have a remainder < MINSIZE */
1371 if (victim == q)
1372 {
1373 q = next_bin(q);
1374 victim = last(q);
1375 }
1376 if (victim != q)
1377 {
1378 victim_size = chunksize(victim);
1379 unlink(victim, bck, fwd);
1380 set_inuse_bit_at_offset(victim, victim_size);
1381 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001382 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
xia.jin383142c2024-06-11 06:30:53 +00001383#ifdef CONFIG_ARMV8_MULTIENTRY
1384 spin_unlock(&malloc_lock);
1385#endif
Bo Lv96a66d02023-05-12 19:18:22 +08001386#ifdef CONFIG_AML_UASAN
1387 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001388 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001389#else
1390 return chunk2mem(victim);
1391#endif
wdenk217c9da2002-10-25 20:35:49 +00001392 }
1393
1394 idx += 2; /* Set for bin scan below. We've already scanned 2 bins. */
1395
1396 }
1397 else
1398 {
1399 idx = bin_index(nb);
1400 bin = bin_at(idx);
1401
1402 for (victim = last(bin); victim != bin; victim = victim->bk)
1403 {
1404 victim_size = chunksize(victim);
1405 remainder_size = victim_size - nb;
1406
1407 if (remainder_size >= (long)MINSIZE) /* too big */
1408 {
wdenk8bde7f72003-06-27 21:31:46 +00001409 --idx; /* adjust to rescan below after checking last remainder */
1410 break;
wdenk217c9da2002-10-25 20:35:49 +00001411 }
1412
1413 else if (remainder_size >= 0) /* exact fit */
1414 {
wdenk8bde7f72003-06-27 21:31:46 +00001415 unlink(victim, bck, fwd);
1416 set_inuse_bit_at_offset(victim, victim_size);
1417 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001418 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
xia.jin383142c2024-06-11 06:30:53 +00001419#ifdef CONFIG_ARMV8_MULTIENTRY
1420 spin_unlock(&malloc_lock);
1421 #endif
Bo Lv96a66d02023-05-12 19:18:22 +08001422#ifdef CONFIG_AML_UASAN
1423 uasan_alloc(victim, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001424 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001425#else
1426 return chunk2mem(victim);
1427#endif
wdenk217c9da2002-10-25 20:35:49 +00001428 }
1429 }
1430
1431 ++idx;
1432
1433 }
1434
1435 /* Try to use the last split-off remainder */
1436
1437 if ( (victim = last_remainder->fd) != last_remainder)
1438 {
1439 victim_size = chunksize(victim);
1440 remainder_size = victim_size - nb;
1441
1442 if (remainder_size >= (long)MINSIZE) /* re-split */
1443 {
1444 remainder = chunk_at_offset(victim, nb);
1445 set_head(victim, nb | PREV_INUSE);
1446 link_last_remainder(remainder);
1447 set_head(remainder, remainder_size | PREV_INUSE);
1448 set_foot(remainder, remainder_size);
1449 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001450 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
xia.jin383142c2024-06-11 06:30:53 +00001451#ifdef CONFIG_ARMV8_MULTIENTRY
1452 spin_unlock(&malloc_lock);
1453#endif
Bo Lv96a66d02023-05-12 19:18:22 +08001454#ifdef CONFIG_AML_UASAN
1455 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001456 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001457#else
1458 return chunk2mem(victim);
1459#endif
wdenk217c9da2002-10-25 20:35:49 +00001460 }
1461
1462 clear_last_remainder;
1463
1464 if (remainder_size >= 0) /* exhaust */
1465 {
1466 set_inuse_bit_at_offset(victim, victim_size);
1467 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001468 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
xia.jin383142c2024-06-11 06:30:53 +00001469#ifdef CONFIG_ARMV8_MULTIENTRY
1470 spin_unlock(&malloc_lock);
1471#endif
Bo Lv96a66d02023-05-12 19:18:22 +08001472#ifdef CONFIG_AML_UASAN
1473 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001474 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001475#else
1476 return chunk2mem(victim);
1477#endif
wdenk217c9da2002-10-25 20:35:49 +00001478 }
1479
1480 /* Else place in bin */
1481
1482 frontlink(victim, victim_size, remainder_index, bck, fwd);
1483 }
1484
1485 /*
1486 If there are any possibly nonempty big-enough blocks,
1487 search for best fitting chunk by scanning bins in blockwidth units.
1488 */
1489
Stefan Roesef2302d42008-08-06 14:05:38 +02001490 if ( (block = idx2binblock(idx)) <= binblocks_r)
wdenk217c9da2002-10-25 20:35:49 +00001491 {
1492
1493 /* Get to the first marked block */
1494
Stefan Roesef2302d42008-08-06 14:05:38 +02001495 if ( (block & binblocks_r) == 0)
wdenk217c9da2002-10-25 20:35:49 +00001496 {
1497 /* force to an even block boundary */
1498 idx = (idx & ~(BINBLOCKWIDTH - 1)) + BINBLOCKWIDTH;
1499 block <<= 1;
Stefan Roesef2302d42008-08-06 14:05:38 +02001500 while ((block & binblocks_r) == 0)
wdenk217c9da2002-10-25 20:35:49 +00001501 {
wdenk8bde7f72003-06-27 21:31:46 +00001502 idx += BINBLOCKWIDTH;
1503 block <<= 1;
wdenk217c9da2002-10-25 20:35:49 +00001504 }
1505 }
1506
1507 /* For each possibly nonempty block ... */
1508 for (;;)
1509 {
1510 startidx = idx; /* (track incomplete blocks) */
1511 q = bin = bin_at(idx);
1512
1513 /* For each bin in this block ... */
1514 do
1515 {
wdenk8bde7f72003-06-27 21:31:46 +00001516 /* Find and use first big enough chunk ... */
wdenk217c9da2002-10-25 20:35:49 +00001517
wdenk8bde7f72003-06-27 21:31:46 +00001518 for (victim = last(bin); victim != bin; victim = victim->bk)
1519 {
1520 victim_size = chunksize(victim);
1521 remainder_size = victim_size - nb;
wdenk217c9da2002-10-25 20:35:49 +00001522
wdenk8bde7f72003-06-27 21:31:46 +00001523 if (remainder_size >= (long)MINSIZE) /* split */
1524 {
1525 remainder = chunk_at_offset(victim, nb);
1526 set_head(victim, nb | PREV_INUSE);
1527 unlink(victim, bck, fwd);
1528 link_last_remainder(remainder);
1529 set_head(remainder, remainder_size | PREV_INUSE);
1530 set_foot(remainder, remainder_size);
1531 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001532 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
xia.jin383142c2024-06-11 06:30:53 +00001533#ifdef CONFIG_ARMV8_MULTIENTRY
1534 spin_unlock(&malloc_lock);
1535#endif
Bo Lv96a66d02023-05-12 19:18:22 +08001536#ifdef CONFIG_AML_UASAN
1537 uasan_alloc(victim, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001538 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001539#else
1540 return chunk2mem(victim);
1541#endif
wdenk8bde7f72003-06-27 21:31:46 +00001542 }
wdenk217c9da2002-10-25 20:35:49 +00001543
wdenk8bde7f72003-06-27 21:31:46 +00001544 else if (remainder_size >= 0) /* take */
1545 {
1546 set_inuse_bit_at_offset(victim, victim_size);
1547 unlink(victim, bck, fwd);
1548 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001549 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
xia.jin383142c2024-06-11 06:30:53 +00001550#ifdef CONFIG_ARMV8_MULTIENTRY
1551 spin_unlock(&malloc_lock);
1552#endif
Bo Lv96a66d02023-05-12 19:18:22 +08001553#ifdef CONFIG_AML_UASAN
1554 uasan_alloc(victim, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001555 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001556#else
1557 return chunk2mem(victim);
1558#endif
wdenk8bde7f72003-06-27 21:31:46 +00001559 }
wdenk217c9da2002-10-25 20:35:49 +00001560
wdenk8bde7f72003-06-27 21:31:46 +00001561 }
wdenk217c9da2002-10-25 20:35:49 +00001562
1563 bin = next_bin(bin);
1564
1565 } while ((++idx & (BINBLOCKWIDTH - 1)) != 0);
1566
1567 /* Clear out the block bit. */
1568
1569 do /* Possibly backtrack to try to clear a partial block */
1570 {
wdenk8bde7f72003-06-27 21:31:46 +00001571 if ((startidx & (BINBLOCKWIDTH - 1)) == 0)
1572 {
Stefan Roesef2302d42008-08-06 14:05:38 +02001573 av_[1] = (mbinptr)(binblocks_r & ~block);
wdenk8bde7f72003-06-27 21:31:46 +00001574 break;
1575 }
1576 --startidx;
wdenk217c9da2002-10-25 20:35:49 +00001577 q = prev_bin(q);
1578 } while (first(q) == q);
1579
1580 /* Get to the next possibly nonempty block */
1581
Stefan Roesef2302d42008-08-06 14:05:38 +02001582 if ( (block <<= 1) <= binblocks_r && (block != 0) )
wdenk217c9da2002-10-25 20:35:49 +00001583 {
Stefan Roesef2302d42008-08-06 14:05:38 +02001584 while ((block & binblocks_r) == 0)
wdenk8bde7f72003-06-27 21:31:46 +00001585 {
1586 idx += BINBLOCKWIDTH;
1587 block <<= 1;
1588 }
wdenk217c9da2002-10-25 20:35:49 +00001589 }
1590 else
wdenk8bde7f72003-06-27 21:31:46 +00001591 break;
wdenk217c9da2002-10-25 20:35:49 +00001592 }
1593 }
1594
1595
1596 /* Try to use top chunk */
1597
1598 /* Require that there be a remainder, ensuring top always exists */
1599 if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE)
1600 {
1601
1602#if HAVE_MMAP
1603 /* If big and would otherwise need to extend, try to use mmap instead */
1604 if ((unsigned long)nb >= (unsigned long)mmap_threshold &&
Heinrich Schuchardta874cac2017-11-10 21:46:34 +01001605 (victim = mmap_chunk(nb)))
Sean Andersonbdaeea12022-03-23 14:04:49 -04001606 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
wdenk217c9da2002-10-25 20:35:49 +00001607 return chunk2mem(victim);
1608#endif
1609
1610 /* Try to extend */
1611 malloc_extend_top(nb);
xia.jin383142c2024-06-11 06:30:53 +00001612#ifdef CONFIG_ARMV8_MULTIENTRY
1613 if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE) {
1614 spin_unlock(&malloc_lock);
1615 return NULL; /* propagate failure */
1616 }
1617#else
wdenk217c9da2002-10-25 20:35:49 +00001618 if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE)
Kim Phillips199adb62012-10-29 13:34:32 +00001619 return NULL; /* propagate failure */
xia.jin383142c2024-06-11 06:30:53 +00001620#endif
wdenk217c9da2002-10-25 20:35:49 +00001621 }
1622
1623 victim = top;
1624 set_head(victim, nb | PREV_INUSE);
1625 top = chunk_at_offset(victim, nb);
1626 set_head(top, remainder_size | PREV_INUSE);
1627 check_malloced_chunk(victim, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001628 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
xia.jin383142c2024-06-11 06:30:53 +00001629#ifdef CONFIG_ARMV8_MULTIENTRY
1630 spin_unlock(&malloc_lock);
1631#endif
Bo Lv96a66d02023-05-12 19:18:22 +08001632#ifdef CONFIG_AML_UASAN
1633 uasan_alloc(victim, bytes);
wdenk217c9da2002-10-25 20:35:49 +00001634 return chunk2mem(victim);
Bo Lv96a66d02023-05-12 19:18:22 +08001635#else
1636 return chunk2mem(victim);
1637#endif
wdenk217c9da2002-10-25 20:35:49 +00001638
1639}
1640
1641
Simon Glassd93041a2014-07-10 22:23:25 -06001642
wdenk217c9da2002-10-25 20:35:49 +00001643
1644/*
1645
1646 free() algorithm :
1647
1648 cases:
1649
1650 1. free(0) has no effect.
1651
1652 2. If the chunk was allocated via mmap, it is release via munmap().
1653
1654 3. If a returned chunk borders the current high end of memory,
wdenk8bde7f72003-06-27 21:31:46 +00001655 it is consolidated into the top, and if the total unused
1656 topmost memory exceeds the trim threshold, malloc_trim is
1657 called.
wdenk217c9da2002-10-25 20:35:49 +00001658
1659 4. Other chunks are consolidated as they arrive, and
wdenk8bde7f72003-06-27 21:31:46 +00001660 placed in corresponding bins. (This includes the case of
1661 consolidating with the current `last_remainder').
wdenk217c9da2002-10-25 20:35:49 +00001662
1663*/
1664
1665
1666#if __STD_C
1667void fREe(Void_t* mem)
1668#else
1669void fREe(mem) Void_t* mem;
1670#endif
1671{
1672 mchunkptr p; /* chunk corresponding to mem */
1673 INTERNAL_SIZE_T hd; /* its head field */
1674 INTERNAL_SIZE_T sz; /* its size */
1675 int idx; /* its bin index */
1676 mchunkptr next; /* next contiguous chunk */
1677 INTERNAL_SIZE_T nextsz; /* its size */
1678 INTERNAL_SIZE_T prevsz; /* size of previous contiguous chunk */
1679 mchunkptr bck; /* misc temp for linking */
1680 mchunkptr fwd; /* misc temp for linking */
1681 int islr; /* track whether merging with last_remainder */
1682
Andy Yanf1896c42017-07-24 17:43:34 +08001683#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassd59476b2014-07-10 22:23:28 -06001684 /* free() is a no-op - all the memory will be freed on relocation */
Sean Andersonbdaeea12022-03-23 14:04:49 -04001685 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
1686 VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ);
Simon Glassd59476b2014-07-10 22:23:28 -06001687 return;
Sean Andersonbdaeea12022-03-23 14:04:49 -04001688 }
Simon Glassd59476b2014-07-10 22:23:28 -06001689#endif
1690
Kim Phillips199adb62012-10-29 13:34:32 +00001691 if (mem == NULL) /* free(0) has no effect */
wdenk217c9da2002-10-25 20:35:49 +00001692 return;
1693
1694 p = mem2chunk(mem);
1695 hd = p->size;
1696
1697#if HAVE_MMAP
1698 if (hd & IS_MMAPPED) /* release mmapped memory. */
1699 {
1700 munmap_chunk(p);
1701 return;
1702 }
1703#endif
1704
xia.jin383142c2024-06-11 06:30:53 +00001705#ifdef CONFIG_ARMV8_MULTIENTRY
1706 spin_lock(&malloc_lock);
1707#endif
wdenk217c9da2002-10-25 20:35:49 +00001708 check_inuse_chunk(p);
1709
1710 sz = hd & ~PREV_INUSE;
Bo Lv96a66d02023-05-12 19:18:22 +08001711
1712#ifdef CONFIG_AML_UASAN
1713 uasan_free(p, sz);
1714#endif
1715
wdenk217c9da2002-10-25 20:35:49 +00001716 next = chunk_at_offset(p, sz);
1717 nextsz = chunksize(next);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001718 VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ);
wdenk217c9da2002-10-25 20:35:49 +00001719
1720 if (next == top) /* merge with top */
1721 {
1722 sz += nextsz;
1723
1724 if (!(hd & PREV_INUSE)) /* consolidate backward */
1725 {
1726 prevsz = p->prev_size;
1727 p = chunk_at_offset(p, -((long) prevsz));
1728 sz += prevsz;
1729 unlink(p, bck, fwd);
1730 }
1731
1732 set_head(p, sz | PREV_INUSE);
1733 top = p;
1734 if ((unsigned long)(sz) >= (unsigned long)trim_threshold)
1735 malloc_trim(top_pad);
xia.jin383142c2024-06-11 06:30:53 +00001736 #ifdef CONFIG_ARMV8_MULTIENTRY
1737 spin_unlock(&malloc_lock);
1738 #endif
wdenk217c9da2002-10-25 20:35:49 +00001739 return;
1740 }
1741
1742 set_head(next, nextsz); /* clear inuse bit */
1743
1744 islr = 0;
1745
1746 if (!(hd & PREV_INUSE)) /* consolidate backward */
1747 {
1748 prevsz = p->prev_size;
1749 p = chunk_at_offset(p, -((long) prevsz));
1750 sz += prevsz;
1751
1752 if (p->fd == last_remainder) /* keep as last_remainder */
1753 islr = 1;
1754 else
1755 unlink(p, bck, fwd);
1756 }
1757
1758 if (!(inuse_bit_at_offset(next, nextsz))) /* consolidate forward */
1759 {
1760 sz += nextsz;
1761
1762 if (!islr && next->fd == last_remainder) /* re-insert last_remainder */
1763 {
1764 islr = 1;
1765 link_last_remainder(p);
1766 }
1767 else
1768 unlink(next, bck, fwd);
1769 }
1770
1771
1772 set_head(p, sz | PREV_INUSE);
1773 set_foot(p, sz);
1774 if (!islr)
1775 frontlink(p, sz, idx, bck, fwd);
xia.jin383142c2024-06-11 06:30:53 +00001776#ifdef CONFIG_ARMV8_MULTIENTRY
1777 spin_unlock(&malloc_lock);
1778#endif
wdenk217c9da2002-10-25 20:35:49 +00001779}
1780
1781
wdenk217c9da2002-10-25 20:35:49 +00001782/*
1783
1784 Realloc algorithm:
1785
1786 Chunks that were obtained via mmap cannot be extended or shrunk
1787 unless HAVE_MREMAP is defined, in which case mremap is used.
1788 Otherwise, if their reallocation is for additional space, they are
1789 copied. If for less, they are just left alone.
1790
1791 Otherwise, if the reallocation is for additional space, and the
1792 chunk can be extended, it is, else a malloc-copy-free sequence is
1793 taken. There are several different ways that a chunk could be
1794 extended. All are tried:
1795
1796 * Extending forward into following adjacent free chunk.
1797 * Shifting backwards, joining preceding adjacent space
1798 * Both shifting backwards and extending forward.
1799 * Extending into newly sbrked space
1800
1801 Unless the #define REALLOC_ZERO_BYTES_FREES is set, realloc with a
1802 size argument of zero (re)allocates a minimum-sized chunk.
1803
1804 If the reallocation is for less space, and the new request is for
1805 a `small' (<512 bytes) size, then the newly unused space is lopped
1806 off and freed.
1807
1808 The old unix realloc convention of allowing the last-free'd chunk
1809 to be used as an argument to realloc is no longer supported.
1810 I don't know of any programs still relying on this feature,
1811 and allowing it would also allow too many other incorrect
1812 usages of realloc to be sensible.
1813
1814
1815*/
1816
xia.jin383142c2024-06-11 06:30:53 +00001817#ifdef CONFIG_ARMV8_MULTIENTRY
1818Void_t* rEALLOc(Void_t* oldmem, size_t bytes)
1819{
1820 void *new;
1821 mchunkptr oldp;
1822 size_t oldsize;
wdenk217c9da2002-10-25 20:35:49 +00001823
xia.jin383142c2024-06-11 06:30:53 +00001824 if (bytes <= 0)
1825 return NULL;
1826 if (!oldmem)
1827 return mALLOc(bytes);
1828
1829 oldp = mem2chunk(oldmem);
1830 oldsize = chunksize(oldp);
1831
1832 new = mALLOc(bytes);
1833 if (!new)
1834 return NULL;
1835 if (oldsize > bytes)
1836 memcpy(new, oldmem, bytes);
1837 else
1838 memcpy(new, oldmem, oldsize);
1839 fREe(oldmem);
1840 return new;
1841}
1842#else
wdenk217c9da2002-10-25 20:35:49 +00001843#if __STD_C
1844Void_t* rEALLOc(Void_t* oldmem, size_t bytes)
1845#else
1846Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
1847#endif
1848{
1849 INTERNAL_SIZE_T nb; /* padded request size */
1850
1851 mchunkptr oldp; /* chunk corresponding to oldmem */
1852 INTERNAL_SIZE_T oldsize; /* its size */
1853
1854 mchunkptr newp; /* chunk to return */
1855 INTERNAL_SIZE_T newsize; /* its size */
1856 Void_t* newmem; /* corresponding user mem */
1857
1858 mchunkptr next; /* next contiguous chunk after oldp */
1859 INTERNAL_SIZE_T nextsize; /* its size */
1860
1861 mchunkptr prev; /* previous contiguous chunk before oldp */
1862 INTERNAL_SIZE_T prevsize; /* its size */
1863
1864 mchunkptr remainder; /* holds split off extra space from newp */
1865 INTERNAL_SIZE_T remainder_size; /* its size */
1866
1867 mchunkptr bck; /* misc temp for linking */
1868 mchunkptr fwd; /* misc temp for linking */
1869
1870#ifdef REALLOC_ZERO_BYTES_FREES
Heinrich Schuchardta874cac2017-11-10 21:46:34 +01001871 if (!bytes) {
1872 fREe(oldmem);
1873 return NULL;
1874 }
wdenk217c9da2002-10-25 20:35:49 +00001875#endif
1876
Kim Phillips199adb62012-10-29 13:34:32 +00001877 if ((long)bytes < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00001878
1879 /* realloc of null is supposed to be same as malloc */
Kim Phillips199adb62012-10-29 13:34:32 +00001880 if (oldmem == NULL) return mALLOc(bytes);
wdenk217c9da2002-10-25 20:35:49 +00001881
Andy Yanf1896c42017-07-24 17:43:34 +08001882#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassc9356be2014-11-10 17:16:43 -07001883 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
Simon Glassd59476b2014-07-10 22:23:28 -06001884 /* This is harder to support and should not be needed */
1885 panic("pre-reloc realloc() is not supported");
1886 }
1887#endif
1888
wdenk217c9da2002-10-25 20:35:49 +00001889 newp = oldp = mem2chunk(oldmem);
1890 newsize = oldsize = chunksize(oldp);
1891
1892
1893 nb = request2size(bytes);
1894
1895#if HAVE_MMAP
1896 if (chunk_is_mmapped(oldp))
1897 {
1898#if HAVE_MREMAP
1899 newp = mremap_chunk(oldp, nb);
1900 if(newp) return chunk2mem(newp);
1901#endif
1902 /* Note the extra SIZE_SZ overhead. */
1903 if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */
1904 /* Must alloc, copy, free. */
1905 newmem = mALLOc(bytes);
Heinrich Schuchardta874cac2017-11-10 21:46:34 +01001906 if (!newmem)
1907 return NULL; /* propagate failure */
wdenk217c9da2002-10-25 20:35:49 +00001908 MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
1909 munmap_chunk(oldp);
1910 return newmem;
1911 }
1912#endif
1913
1914 check_inuse_chunk(oldp);
1915
1916 if ((long)(oldsize) < (long)(nb))
1917 {
1918
1919 /* Try expanding forward */
1920
1921 next = chunk_at_offset(oldp, oldsize);
1922 if (next == top || !inuse(next))
1923 {
1924 nextsize = chunksize(next);
1925
1926 /* Forward into top only if a remainder */
1927 if (next == top)
1928 {
wdenk8bde7f72003-06-27 21:31:46 +00001929 if ((long)(nextsize + newsize) >= (long)(nb + MINSIZE))
1930 {
1931 newsize += nextsize;
1932 top = chunk_at_offset(oldp, nb);
1933 set_head(top, (newsize - nb) | PREV_INUSE);
1934 set_head_size(oldp, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001935 VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ);
1936 VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes);
Bo Lv96a66d02023-05-12 19:18:22 +08001937#ifdef CONFIG_AML_UASAN
1938 uasan_alloc(oldp, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001939 return chunk2mem(oldp);
Bo Lv96a66d02023-05-12 19:18:22 +08001940#else
1941 return chunk2mem(oldp);
1942#endif
wdenk8bde7f72003-06-27 21:31:46 +00001943 }
wdenk217c9da2002-10-25 20:35:49 +00001944 }
1945
1946 /* Forward into next chunk */
1947 else if (((long)(nextsize + newsize) >= (long)(nb)))
1948 {
wdenk8bde7f72003-06-27 21:31:46 +00001949 unlink(next, bck, fwd);
1950 newsize += nextsize;
Sean Andersonbdaeea12022-03-23 14:04:49 -04001951 VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ);
1952 VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001953 goto split;
wdenk217c9da2002-10-25 20:35:49 +00001954 }
1955 }
1956 else
1957 {
Kim Phillips199adb62012-10-29 13:34:32 +00001958 next = NULL;
wdenk217c9da2002-10-25 20:35:49 +00001959 nextsize = 0;
1960 }
1961
1962 /* Try shifting backwards. */
1963
1964 if (!prev_inuse(oldp))
1965 {
1966 prev = prev_chunk(oldp);
1967 prevsize = chunksize(prev);
1968
1969 /* try forward + backward first to save a later consolidation */
1970
Kim Phillips199adb62012-10-29 13:34:32 +00001971 if (next != NULL)
wdenk217c9da2002-10-25 20:35:49 +00001972 {
wdenk8bde7f72003-06-27 21:31:46 +00001973 /* into top */
1974 if (next == top)
1975 {
1976 if ((long)(nextsize + prevsize + newsize) >= (long)(nb + MINSIZE))
1977 {
1978 unlink(prev, bck, fwd);
1979 newp = prev;
1980 newsize += prevsize + nextsize;
1981 newmem = chunk2mem(newp);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001982 VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08001983#ifdef CONFIG_AML_UASAN
1984 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
1985 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
1986 oldsize - SIZE_SZ);
1987#else
wdenk8bde7f72003-06-27 21:31:46 +00001988 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08001989#endif
wdenk8bde7f72003-06-27 21:31:46 +00001990 top = chunk_at_offset(newp, nb);
1991 set_head(top, (newsize - nb) | PREV_INUSE);
1992 set_head_size(newp, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04001993 VALGRIND_FREELIKE_BLOCK(oldmem, SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08001994#ifdef CONFIG_AML_UASAN
1995 uasan_alloc(newp, bytes);
wdenk8bde7f72003-06-27 21:31:46 +00001996 return newmem;
Bo Lv96a66d02023-05-12 19:18:22 +08001997#else
1998 return newmem;
1999#endif
wdenk8bde7f72003-06-27 21:31:46 +00002000 }
2001 }
wdenk217c9da2002-10-25 20:35:49 +00002002
wdenk8bde7f72003-06-27 21:31:46 +00002003 /* into next chunk */
2004 else if (((long)(nextsize + prevsize + newsize) >= (long)(nb)))
2005 {
2006 unlink(next, bck, fwd);
2007 unlink(prev, bck, fwd);
2008 newp = prev;
2009 newsize += nextsize + prevsize;
2010 newmem = chunk2mem(newp);
Sean Andersonbdaeea12022-03-23 14:04:49 -04002011 VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08002012#ifdef CONFIG_AML_UASAN
2013 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
2014 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
2015 oldsize - SIZE_SZ);
2016#else
wdenk8bde7f72003-06-27 21:31:46 +00002017 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08002018#endif
wdenk8bde7f72003-06-27 21:31:46 +00002019 goto split;
2020 }
wdenk217c9da2002-10-25 20:35:49 +00002021 }
2022
2023 /* backward only */
Kim Phillips199adb62012-10-29 13:34:32 +00002024 if (prev != NULL && (long)(prevsize + newsize) >= (long)nb)
wdenk217c9da2002-10-25 20:35:49 +00002025 {
wdenk8bde7f72003-06-27 21:31:46 +00002026 unlink(prev, bck, fwd);
2027 newp = prev;
2028 newsize += prevsize;
2029 newmem = chunk2mem(newp);
Sean Andersonbdaeea12022-03-23 14:04:49 -04002030 VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
Bo Lv96a66d02023-05-12 19:18:22 +08002031#ifdef CONFIG_AML_UASAN
2032 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
2033 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
2034 oldsize - SIZE_SZ);
2035#else
wdenk8bde7f72003-06-27 21:31:46 +00002036 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08002037#endif
wdenk8bde7f72003-06-27 21:31:46 +00002038 goto split;
wdenk217c9da2002-10-25 20:35:49 +00002039 }
2040 }
2041
2042 /* Must allocate */
2043
2044 newmem = mALLOc (bytes);
2045
Kim Phillips199adb62012-10-29 13:34:32 +00002046 if (newmem == NULL) /* propagate failure */
2047 return NULL;
wdenk217c9da2002-10-25 20:35:49 +00002048
2049 /* Avoid copy if newp is next chunk after oldp. */
2050 /* (This can only happen when new chunk is sbrk'ed.) */
2051
2052 if ( (newp = mem2chunk(newmem)) == next_chunk(oldp))
2053 {
2054 newsize += chunksize(newp);
2055 newp = oldp;
2056 goto split;
2057 }
2058
2059 /* Otherwise copy, free, and exit */
Bo Lv96a66d02023-05-12 19:18:22 +08002060#ifdef CONFIG_AML_UASAN
2061 MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE,
2062 oldmem - UASAN_ALLOCA_REDZONE_SIZE,
2063 oldsize - SIZE_SZ);
2064#else
wdenk217c9da2002-10-25 20:35:49 +00002065 MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08002066#endif
wdenk217c9da2002-10-25 20:35:49 +00002067 fREe(oldmem);
2068 return newmem;
Sean Andersonbdaeea12022-03-23 14:04:49 -04002069 } else {
2070 VALGRIND_RESIZEINPLACE_BLOCK(oldmem, 0, bytes, SIZE_SZ);
2071 VALGRIND_MAKE_MEM_DEFINED(oldmem, bytes);
wdenk217c9da2002-10-25 20:35:49 +00002072 }
2073
2074
2075 split: /* split off extra room in old or expanded chunk */
2076
2077 if (newsize - nb >= MINSIZE) /* split off remainder */
2078 {
2079 remainder = chunk_at_offset(newp, nb);
2080 remainder_size = newsize - nb;
2081 set_head_size(newp, nb);
2082 set_head(remainder, remainder_size | PREV_INUSE);
2083 set_inuse_bit_at_offset(remainder, remainder_size);
Sean Andersonbdaeea12022-03-23 14:04:49 -04002084 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ,
2085 false);
wdenk217c9da2002-10-25 20:35:49 +00002086 fREe(chunk2mem(remainder)); /* let free() deal with it */
2087 }
2088 else
2089 {
2090 set_head_size(newp, newsize);
2091 set_inuse_bit_at_offset(newp, newsize);
2092 }
2093
2094 check_inuse_chunk(newp);
Bo Lv96a66d02023-05-12 19:18:22 +08002095#ifdef CONFIG_AML_UASAN
2096 uasan_alloc(newp, bytes);
wdenk217c9da2002-10-25 20:35:49 +00002097 return chunk2mem(newp);
Bo Lv96a66d02023-05-12 19:18:22 +08002098#else
2099 return chunk2mem(newp);
2100#endif
wdenk217c9da2002-10-25 20:35:49 +00002101}
xia.jin383142c2024-06-11 06:30:53 +00002102#endif
wdenk217c9da2002-10-25 20:35:49 +00002103
2104
Simon Glassd93041a2014-07-10 22:23:25 -06002105
wdenk217c9da2002-10-25 20:35:49 +00002106
2107/*
2108
2109 memalign algorithm:
2110
2111 memalign requests more than enough space from malloc, finds a spot
2112 within that chunk that meets the alignment request, and then
2113 possibly frees the leading and trailing space.
2114
2115 The alignment argument must be a power of two. This property is not
2116 checked by memalign, so misuse may result in random runtime errors.
2117
2118 8-byte alignment is guaranteed by normal malloc calls, so don't
2119 bother calling memalign with an argument of 8 or less.
2120
2121 Overreliance on memalign is a sure way to fragment space.
2122
2123*/
2124
2125
2126#if __STD_C
2127Void_t* mEMALIGn(size_t alignment, size_t bytes)
2128#else
2129Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
2130#endif
2131{
2132 INTERNAL_SIZE_T nb; /* padded request size */
2133 char* m; /* memory returned by malloc call */
2134 mchunkptr p; /* corresponding chunk */
2135 char* brk; /* alignment point within p */
2136 mchunkptr newp; /* chunk to return */
2137 INTERNAL_SIZE_T newsize; /* its size */
2138 INTERNAL_SIZE_T leadsize; /* leading space befor alignment point */
2139 mchunkptr remainder; /* spare room at end to split off */
2140 long remainder_size; /* its size */
2141
Kim Phillips199adb62012-10-29 13:34:32 +00002142 if ((long)bytes < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00002143
Ley Foon Tanee038c52018-05-18 18:03:12 +08002144#if CONFIG_VAL(SYS_MALLOC_F_LEN)
2145 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
Andreas Dannenberg4c6be012019-03-27 13:17:26 -05002146 return memalign_simple(alignment, bytes);
Ley Foon Tanee038c52018-05-18 18:03:12 +08002147 }
2148#endif
2149
wdenk217c9da2002-10-25 20:35:49 +00002150 /* If need less alignment than we give anyway, just relay to malloc */
2151
2152 if (alignment <= MALLOC_ALIGNMENT) return mALLOc(bytes);
2153
2154 /* Otherwise, ensure that it is at least a minimum chunk size */
2155
2156 if (alignment < MINSIZE) alignment = MINSIZE;
2157
2158 /* Call malloc with worst case padding to hit alignment. */
2159
2160 nb = request2size(bytes);
2161 m = (char*)(mALLOc(nb + alignment + MINSIZE));
2162
Stephen Warren4f144a42016-01-25 14:03:42 -07002163 /*
2164 * The attempt to over-allocate (with a size large enough to guarantee the
2165 * ability to find an aligned region within allocated memory) failed.
2166 *
2167 * Try again, this time only allocating exactly the size the user wants. If
2168 * the allocation now succeeds and just happens to be aligned, we can still
2169 * fulfill the user's request.
2170 */
2171 if (m == NULL) {
Stephen Warren034eda82016-04-25 15:55:42 -06002172 size_t extra, extra2;
Stephen Warren4f144a42016-01-25 14:03:42 -07002173 /*
2174 * Use bytes not nb, since mALLOc internally calls request2size too, and
2175 * each call increases the size to allocate, to account for the header.
2176 */
2177 m = (char*)(mALLOc(bytes));
2178 /* Aligned -> return it */
2179 if ((((unsigned long)(m)) % alignment) == 0)
2180 return m;
Stephen Warren034eda82016-04-25 15:55:42 -06002181 /*
2182 * Otherwise, try again, requesting enough extra space to be able to
2183 * acquire alignment.
2184 */
Stephen Warren4f144a42016-01-25 14:03:42 -07002185 fREe(m);
Stephen Warren034eda82016-04-25 15:55:42 -06002186 /* Add in extra bytes to match misalignment of unexpanded allocation */
2187 extra = alignment - (((unsigned long)(m)) % alignment);
2188 m = (char*)(mALLOc(bytes + extra));
2189 /*
2190 * m might not be the same as before. Validate that the previous value of
2191 * extra still works for the current value of m.
2192 * If (!m), extra2=alignment so
2193 */
2194 if (m) {
2195 extra2 = alignment - (((unsigned long)(m)) % alignment);
2196 if (extra2 > extra) {
2197 fREe(m);
2198 m = NULL;
2199 }
2200 }
2201 /* Fall through to original NULL check and chunk splitting logic */
Stephen Warren4f144a42016-01-25 14:03:42 -07002202 }
2203
Kim Phillips199adb62012-10-29 13:34:32 +00002204 if (m == NULL) return NULL; /* propagate failure */
wdenk217c9da2002-10-25 20:35:49 +00002205
2206 p = mem2chunk(m);
2207
2208 if ((((unsigned long)(m)) % alignment) == 0) /* aligned */
2209 {
2210#if HAVE_MMAP
2211 if(chunk_is_mmapped(p))
2212 return chunk2mem(p); /* nothing more to do */
2213#endif
2214 }
2215 else /* misaligned */
2216 {
2217 /*
2218 Find an aligned spot inside chunk.
2219 Since we need to give back leading space in a chunk of at
2220 least MINSIZE, if the first calculation places us at
2221 a spot with less than MINSIZE leader, we can move to the
2222 next aligned spot -- we've allocated enough total room so that
2223 this is always possible.
2224 */
2225
2226 brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) & -((signed) alignment));
2227 if ((long)(brk - (char*)(p)) < MINSIZE) brk = brk + alignment;
2228
2229 newp = (mchunkptr)brk;
2230 leadsize = brk - (char*)(p);
2231 newsize = chunksize(p) - leadsize;
2232
2233#if HAVE_MMAP
2234 if(chunk_is_mmapped(p))
2235 {
2236 newp->prev_size = p->prev_size + leadsize;
2237 set_head(newp, newsize|IS_MMAPPED);
2238 return chunk2mem(newp);
2239 }
2240#endif
2241
2242 /* give back leader, use the rest */
2243
2244 set_head(newp, newsize | PREV_INUSE);
2245 set_inuse_bit_at_offset(newp, newsize);
2246 set_head_size(p, leadsize);
2247 fREe(chunk2mem(p));
2248 p = newp;
Sean Andersonbdaeea12022-03-23 14:04:49 -04002249 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(p), bytes, SIZE_SZ, false);
wdenk217c9da2002-10-25 20:35:49 +00002250
2251 assert (newsize >= nb && (((unsigned long)(chunk2mem(p))) % alignment) == 0);
2252 }
2253
2254 /* Also give back spare room at the end */
2255
2256 remainder_size = chunksize(p) - nb;
2257
2258 if (remainder_size >= (long)MINSIZE)
2259 {
2260 remainder = chunk_at_offset(p, nb);
2261 set_head(remainder, remainder_size | PREV_INUSE);
2262 set_head_size(p, nb);
Sean Andersonbdaeea12022-03-23 14:04:49 -04002263 VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ,
2264 false);
wdenk217c9da2002-10-25 20:35:49 +00002265 fREe(chunk2mem(remainder));
2266 }
2267
2268 check_inuse_chunk(p);
Bo Lv96a66d02023-05-12 19:18:22 +08002269#ifdef CONFIG_AML_UASAN
2270 uasan_alloc(p, bytes);
2271#endif
wdenk217c9da2002-10-25 20:35:49 +00002272 return chunk2mem(p);
2273
2274}
2275
Simon Glassd93041a2014-07-10 22:23:25 -06002276
wdenk217c9da2002-10-25 20:35:49 +00002277
2278
2279/*
2280 valloc just invokes memalign with alignment argument equal
2281 to the page size of the system (or as near to this as can
2282 be figured out from all the includes/defines above.)
2283*/
2284
2285#if __STD_C
2286Void_t* vALLOc(size_t bytes)
2287#else
2288Void_t* vALLOc(bytes) size_t bytes;
2289#endif
2290{
2291 return mEMALIGn (malloc_getpagesize, bytes);
2292}
2293
2294/*
2295 pvalloc just invokes valloc for the nearest pagesize
2296 that will accommodate request
2297*/
2298
2299
2300#if __STD_C
2301Void_t* pvALLOc(size_t bytes)
2302#else
2303Void_t* pvALLOc(bytes) size_t bytes;
2304#endif
2305{
2306 size_t pagesize = malloc_getpagesize;
2307 return mEMALIGn (pagesize, (bytes + pagesize - 1) & ~(pagesize - 1));
2308}
2309
2310/*
2311
2312 calloc calls malloc, then zeroes out the allocated chunk.
2313
2314*/
2315
2316#if __STD_C
2317Void_t* cALLOc(size_t n, size_t elem_size)
2318#else
2319Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size;
2320#endif
2321{
2322 mchunkptr p;
2323 INTERNAL_SIZE_T csz;
2324
2325 INTERNAL_SIZE_T sz = n * elem_size;
2326
2327
2328 /* check if expand_top called, in which case don't need to clear */
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002329#ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
wdenk217c9da2002-10-25 20:35:49 +00002330#if MORECORE_CLEARS
2331 mchunkptr oldtop = top;
2332 INTERNAL_SIZE_T oldtopsize = chunksize(top);
2333#endif
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002334#endif
wdenk217c9da2002-10-25 20:35:49 +00002335 Void_t* mem = mALLOc (sz);
2336
Kim Phillips199adb62012-10-29 13:34:32 +00002337 if ((long)n < 0) return NULL;
wdenk217c9da2002-10-25 20:35:49 +00002338
Kim Phillips199adb62012-10-29 13:34:32 +00002339 if (mem == NULL)
2340 return NULL;
wdenk217c9da2002-10-25 20:35:49 +00002341 else
2342 {
Andy Yanf1896c42017-07-24 17:43:34 +08002343#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassc9356be2014-11-10 17:16:43 -07002344 if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
Simon Goldschmidtbb71a2d2019-10-25 21:23:35 +02002345 memset(mem, 0, sz);
Simon Glassd59476b2014-07-10 22:23:28 -06002346 return mem;
2347 }
2348#endif
wdenk217c9da2002-10-25 20:35:49 +00002349 p = mem2chunk(mem);
2350
2351 /* Two optional cases in which clearing not necessary */
2352
2353
2354#if HAVE_MMAP
2355 if (chunk_is_mmapped(p)) return mem;
2356#endif
2357
2358 csz = chunksize(p);
2359
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002360#ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
wdenk217c9da2002-10-25 20:35:49 +00002361#if MORECORE_CLEARS
Bo Lv96a66d02023-05-12 19:18:22 +08002362 if (p == oldtop && csz > oldtopsize && oldtopsize > sz)
wdenk217c9da2002-10-25 20:35:49 +00002363 {
2364 /* clear only the bytes from non-freshly-sbrked memory */
2365 csz = oldtopsize;
2366 }
2367#endif
Przemyslaw Marczak0aa8a4a2015-03-04 14:01:24 +01002368#endif
wdenk217c9da2002-10-25 20:35:49 +00002369
Bo Lv96a66d02023-05-12 19:18:22 +08002370#ifdef CONFIG_AML_UASAN
2371 /* avoid overwrite */
2372 MALLOC_ZERO(mem, csz - SIZE_SZ - UASAN_ALLOCA_REDZONE_SIZE);
2373#else
wdenk217c9da2002-10-25 20:35:49 +00002374 MALLOC_ZERO(mem, csz - SIZE_SZ);
Bo Lv96a66d02023-05-12 19:18:22 +08002375#endif
Sean Andersonbdaeea12022-03-23 14:04:49 -04002376 VALGRIND_MAKE_MEM_DEFINED(mem, sz);
wdenk217c9da2002-10-25 20:35:49 +00002377 return mem;
2378 }
2379}
2380
2381/*
2382
2383 cfree just calls free. It is needed/defined on some systems
2384 that pair it with calloc, presumably for odd historical reasons.
2385
2386*/
2387
2388#if !defined(INTERNAL_LINUX_C_LIB) || !defined(__ELF__)
2389#if __STD_C
2390void cfree(Void_t *mem)
2391#else
2392void cfree(mem) Void_t *mem;
2393#endif
2394{
2395 fREe(mem);
2396}
2397#endif
2398
Simon Glassd93041a2014-07-10 22:23:25 -06002399
wdenk217c9da2002-10-25 20:35:49 +00002400
2401/*
2402
2403 Malloc_trim gives memory back to the system (via negative
2404 arguments to sbrk) if there is unused memory at the `high' end of
2405 the malloc pool. You can call this after freeing large blocks of
2406 memory to potentially reduce the system-level memory requirements
2407 of a program. However, it cannot guarantee to reduce memory. Under
2408 some allocation patterns, some large free blocks of memory will be
2409 locked between two used chunks, so they cannot be given back to
2410 the system.
2411
2412 The `pad' argument to malloc_trim represents the amount of free
2413 trailing space to leave untrimmed. If this argument is zero,
2414 only the minimum amount of memory to maintain internal data
2415 structures will be left (one page or less). Non-zero arguments
2416 can be supplied to maintain enough trailing space to service
2417 future expected allocations without having to re-obtain memory
2418 from the system.
2419
2420 Malloc_trim returns 1 if it actually released any memory, else 0.
2421
2422*/
2423
2424#if __STD_C
2425int malloc_trim(size_t pad)
2426#else
2427int malloc_trim(pad) size_t pad;
2428#endif
2429{
2430 long top_size; /* Amount of top-most memory */
2431 long extra; /* Amount to release */
2432 char* current_brk; /* address returned by pre-check sbrk call */
2433 char* new_brk; /* address returned by negative sbrk call */
2434
2435 unsigned long pagesz = malloc_getpagesize;
2436
2437 top_size = chunksize(top);
2438 extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
2439
2440 if (extra < (long)pagesz) /* Not enough memory to release */
2441 return 0;
2442
2443 else
2444 {
2445 /* Test to make sure no one else called sbrk */
2446 current_brk = (char*)(MORECORE (0));
2447 if (current_brk != (char*)(top) + top_size)
2448 return 0; /* Apparently we don't own memory; must fail */
2449
2450 else
2451 {
2452 new_brk = (char*)(MORECORE (-extra));
2453
2454 if (new_brk == (char*)(MORECORE_FAILURE)) /* sbrk failed? */
2455 {
wdenk8bde7f72003-06-27 21:31:46 +00002456 /* Try to figure out what we have */
2457 current_brk = (char*)(MORECORE (0));
2458 top_size = current_brk - (char*)top;
2459 if (top_size >= (long)MINSIZE) /* if not, we are very very dead! */
2460 {
2461 sbrked_mem = current_brk - sbrk_base;
2462 set_head(top, top_size | PREV_INUSE);
2463 }
2464 check_chunk(top);
2465 return 0;
wdenk217c9da2002-10-25 20:35:49 +00002466 }
2467
2468 else
2469 {
wdenk8bde7f72003-06-27 21:31:46 +00002470 /* Success. Adjust top accordingly. */
2471 set_head(top, (top_size - extra) | PREV_INUSE);
2472 sbrked_mem -= extra;
2473 check_chunk(top);
2474 return 1;
wdenk217c9da2002-10-25 20:35:49 +00002475 }
2476 }
2477 }
2478}
2479
Simon Glassd93041a2014-07-10 22:23:25 -06002480
wdenk217c9da2002-10-25 20:35:49 +00002481
2482/*
2483 malloc_usable_size:
2484
2485 This routine tells you how many bytes you can actually use in an
2486 allocated chunk, which may be more than you requested (although
2487 often not). You can use this many bytes without worrying about
2488 overwriting other allocated objects. Not a particularly great
2489 programming practice, but still sometimes useful.
2490
2491*/
2492
2493#if __STD_C
2494size_t malloc_usable_size(Void_t* mem)
2495#else
2496size_t malloc_usable_size(mem) Void_t* mem;
2497#endif
2498{
2499 mchunkptr p;
Kim Phillips199adb62012-10-29 13:34:32 +00002500 if (mem == NULL)
wdenk217c9da2002-10-25 20:35:49 +00002501 return 0;
2502 else
2503 {
2504 p = mem2chunk(mem);
2505 if(!chunk_is_mmapped(p))
2506 {
2507 if (!inuse(p)) return 0;
2508 check_inuse_chunk(p);
2509 return chunksize(p) - SIZE_SZ;
2510 }
2511 return chunksize(p) - 2*SIZE_SZ;
2512 }
2513}
2514
2515
Simon Glassd93041a2014-07-10 22:23:25 -06002516
wdenk217c9da2002-10-25 20:35:49 +00002517
2518/* Utility to update current_mallinfo for malloc_stats and mallinfo() */
2519
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002520#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +00002521static void malloc_update_mallinfo()
2522{
2523 int i;
2524 mbinptr b;
2525 mchunkptr p;
2526#ifdef DEBUG
2527 mchunkptr q;
2528#endif
2529
2530 INTERNAL_SIZE_T avail = chunksize(top);
2531 int navail = ((long)(avail) >= (long)MINSIZE)? 1 : 0;
2532
2533 for (i = 1; i < NAV; ++i)
2534 {
2535 b = bin_at(i);
2536 for (p = last(b); p != b; p = p->bk)
2537 {
2538#ifdef DEBUG
2539 check_free_chunk(p);
2540 for (q = next_chunk(p);
wdenk8bde7f72003-06-27 21:31:46 +00002541 q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE;
2542 q = next_chunk(q))
2543 check_inuse_chunk(q);
wdenk217c9da2002-10-25 20:35:49 +00002544#endif
2545 avail += chunksize(p);
2546 navail++;
2547 }
2548 }
2549
2550 current_mallinfo.ordblks = navail;
2551 current_mallinfo.uordblks = sbrked_mem - avail;
2552 current_mallinfo.fordblks = avail;
2553 current_mallinfo.hblks = n_mmaps;
2554 current_mallinfo.hblkhd = mmapped_mem;
2555 current_mallinfo.keepcost = chunksize(top);
2556
2557}
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002558#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +00002559
Simon Glassd93041a2014-07-10 22:23:25 -06002560
wdenk217c9da2002-10-25 20:35:49 +00002561
2562/*
2563
2564 malloc_stats:
2565
2566 Prints on the amount of space obtain from the system (both
2567 via sbrk and mmap), the maximum amount (which may be more than
2568 current if malloc_trim and/or munmap got called), the maximum
2569 number of simultaneous mmap regions used, and the current number
2570 of bytes allocated via malloc (or realloc, etc) but not yet
2571 freed. (Note that this is the number of bytes allocated, not the
2572 number requested. It will be larger than the number requested
2573 because of alignment and bookkeeping overhead.)
2574
2575*/
2576
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002577#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +00002578void malloc_stats()
2579{
2580 malloc_update_mallinfo();
2581 printf("max system bytes = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002582 (unsigned int)(max_total_mem));
wdenk217c9da2002-10-25 20:35:49 +00002583 printf("system bytes = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002584 (unsigned int)(sbrked_mem + mmapped_mem));
wdenk217c9da2002-10-25 20:35:49 +00002585 printf("in use bytes = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002586 (unsigned int)(current_mallinfo.uordblks + mmapped_mem));
wdenk217c9da2002-10-25 20:35:49 +00002587#if HAVE_MMAP
2588 printf("max mmap regions = %10u\n",
wdenk8bde7f72003-06-27 21:31:46 +00002589 (unsigned int)max_n_mmaps);
wdenk217c9da2002-10-25 20:35:49 +00002590#endif
2591}
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002592#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +00002593
2594/*
2595 mallinfo returns a copy of updated current mallinfo.
2596*/
2597
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002598#ifdef DEBUG
wdenk217c9da2002-10-25 20:35:49 +00002599struct mallinfo mALLINFo()
2600{
2601 malloc_update_mallinfo();
2602 return current_mallinfo;
2603}
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002604#endif /* DEBUG */
wdenk217c9da2002-10-25 20:35:49 +00002605
2606
Simon Glassd93041a2014-07-10 22:23:25 -06002607
wdenk217c9da2002-10-25 20:35:49 +00002608
2609/*
2610 mallopt:
2611
2612 mallopt is the general SVID/XPG interface to tunable parameters.
2613 The format is to provide a (parameter-number, parameter-value) pair.
2614 mallopt then sets the corresponding parameter to the argument
2615 value if it can (i.e., so long as the value is meaningful),
2616 and returns 1 if successful else 0.
2617
2618 See descriptions of tunable parameters above.
2619
2620*/
2621
2622#if __STD_C
2623int mALLOPt(int param_number, int value)
2624#else
2625int mALLOPt(param_number, value) int param_number; int value;
2626#endif
2627{
2628 switch(param_number)
2629 {
2630 case M_TRIM_THRESHOLD:
2631 trim_threshold = value; return 1;
2632 case M_TOP_PAD:
2633 top_pad = value; return 1;
2634 case M_MMAP_THRESHOLD:
2635 mmap_threshold = value; return 1;
2636 case M_MMAP_MAX:
2637#if HAVE_MMAP
2638 n_mmaps_max = value; return 1;
2639#else
2640 if (value != 0) return 0; else n_mmaps_max = value; return 1;
2641#endif
2642
2643 default:
2644 return 0;
2645 }
2646}
2647
Simon Glassfb5cf7f2015-02-27 22:06:36 -07002648int initf_malloc(void)
2649{
Andy Yanf1896c42017-07-24 17:43:34 +08002650#if CONFIG_VAL(SYS_MALLOC_F_LEN)
Simon Glassfb5cf7f2015-02-27 22:06:36 -07002651 assert(gd->malloc_base); /* Set up by crt0.S */
Andy Yanf1896c42017-07-24 17:43:34 +08002652 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
Simon Glassfb5cf7f2015-02-27 22:06:36 -07002653 gd->malloc_ptr = 0;
2654#endif
2655
2656 return 0;
2657}
2658
Simon Glass62d63832022-09-06 20:27:00 -06002659void malloc_enable_testing(int max_allocs)
2660{
2661 malloc_testing = true;
2662 malloc_max_allocs = max_allocs;
2663}
2664
2665void malloc_disable_testing(void)
2666{
2667 malloc_testing = false;
2668}
2669
wdenk217c9da2002-10-25 20:35:49 +00002670/*
2671
2672History:
2673
2674 V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee)
2675 * return null for negative arguments
2676 * Added Several WIN32 cleanups from Martin C. Fong <mcfong@yahoo.com>
wdenk8bde7f72003-06-27 21:31:46 +00002677 * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h'
2678 (e.g. WIN32 platforms)
2679 * Cleanup up header file inclusion for WIN32 platforms
2680 * Cleanup code to avoid Microsoft Visual C++ compiler complaints
2681 * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing
2682 memory allocation routines
2683 * Set 'malloc_getpagesize' for WIN32 platforms (needs more work)
2684 * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to
wdenk217c9da2002-10-25 20:35:49 +00002685 usage of 'assert' in non-WIN32 code
wdenk8bde7f72003-06-27 21:31:46 +00002686 * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to
2687 avoid infinite loop
wdenk217c9da2002-10-25 20:35:49 +00002688 * Always call 'fREe()' rather than 'free()'
2689
2690 V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee)
2691 * Fixed ordering problem with boundary-stamping
2692
2693 V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee)
2694 * Added pvalloc, as recommended by H.J. Liu
2695 * Added 64bit pointer support mainly from Wolfram Gloger
2696 * Added anonymously donated WIN32 sbrk emulation
2697 * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
2698 * malloc_extend_top: fix mask error that caused wastage after
wdenk8bde7f72003-06-27 21:31:46 +00002699 foreign sbrks
wdenk217c9da2002-10-25 20:35:49 +00002700 * Add linux mremap support code from HJ Liu
2701
2702 V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee)
2703 * Integrated most documentation with the code.
2704 * Add support for mmap, with help from
wdenk8bde7f72003-06-27 21:31:46 +00002705 Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
wdenk217c9da2002-10-25 20:35:49 +00002706 * Use last_remainder in more cases.
2707 * Pack bins using idea from colin@nyx10.cs.du.edu
2708 * Use ordered bins instead of best-fit threshhold
2709 * Eliminate block-local decls to simplify tracing and debugging.
2710 * Support another case of realloc via move into top
2711 * Fix error occuring when initial sbrk_base not word-aligned.
2712 * Rely on page size for units instead of SBRK_UNIT to
wdenk8bde7f72003-06-27 21:31:46 +00002713 avoid surprises about sbrk alignment conventions.
wdenk217c9da2002-10-25 20:35:49 +00002714 * Add mallinfo, mallopt. Thanks to Raymond Nijssen
wdenk8bde7f72003-06-27 21:31:46 +00002715 (raymond@es.ele.tue.nl) for the suggestion.
wdenk217c9da2002-10-25 20:35:49 +00002716 * Add `pad' argument to malloc_trim and top_pad mallopt parameter.
2717 * More precautions for cases where other routines call sbrk,
wdenk8bde7f72003-06-27 21:31:46 +00002718 courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
wdenk217c9da2002-10-25 20:35:49 +00002719 * Added macros etc., allowing use in linux libc from
wdenk8bde7f72003-06-27 21:31:46 +00002720 H.J. Lu (hjl@gnu.ai.mit.edu)
wdenk217c9da2002-10-25 20:35:49 +00002721 * Inverted this history list
2722
2723 V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee)
2724 * Re-tuned and fixed to behave more nicely with V2.6.0 changes.
2725 * Removed all preallocation code since under current scheme
wdenk8bde7f72003-06-27 21:31:46 +00002726 the work required to undo bad preallocations exceeds
2727 the work saved in good cases for most test programs.
wdenk217c9da2002-10-25 20:35:49 +00002728 * No longer use return list or unconsolidated bins since
wdenk8bde7f72003-06-27 21:31:46 +00002729 no scheme using them consistently outperforms those that don't
2730 given above changes.
wdenk217c9da2002-10-25 20:35:49 +00002731 * Use best fit for very large chunks to prevent some worst-cases.
2732 * Added some support for debugging
2733
2734 V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee)
2735 * Removed footers when chunks are in use. Thanks to
wdenk8bde7f72003-06-27 21:31:46 +00002736 Paul Wilson (wilson@cs.texas.edu) for the suggestion.
wdenk217c9da2002-10-25 20:35:49 +00002737
2738 V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee)
2739 * Added malloc_trim, with help from Wolfram Gloger
wdenk8bde7f72003-06-27 21:31:46 +00002740 (wmglo@Dent.MED.Uni-Muenchen.DE).
wdenk217c9da2002-10-25 20:35:49 +00002741
2742 V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g)
2743
2744 V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g)
2745 * realloc: try to expand in both directions
2746 * malloc: swap order of clean-bin strategy;
2747 * realloc: only conditionally expand backwards
2748 * Try not to scavenge used bins
2749 * Use bin counts as a guide to preallocation
2750 * Occasionally bin return list chunks in first scan
2751 * Add a few optimizations from colin@nyx10.cs.du.edu
2752
2753 V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g)
2754 * faster bin computation & slightly different binning
2755 * merged all consolidations to one part of malloc proper
wdenk8bde7f72003-06-27 21:31:46 +00002756 (eliminating old malloc_find_space & malloc_clean_bin)
wdenk217c9da2002-10-25 20:35:49 +00002757 * Scan 2 returns chunks (not just 1)
2758 * Propagate failure in realloc if malloc returns 0
2759 * Add stuff to allow compilation on non-ANSI compilers
wdenk8bde7f72003-06-27 21:31:46 +00002760 from kpv@research.att.com
wdenk217c9da2002-10-25 20:35:49 +00002761
2762 V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu)
2763 * removed potential for odd address access in prev_chunk
2764 * removed dependency on getpagesize.h
2765 * misc cosmetics and a bit more internal documentation
2766 * anticosmetics: mangled names in macros to evade debugger strangeness
2767 * tested on sparc, hp-700, dec-mips, rs6000
wdenk8bde7f72003-06-27 21:31:46 +00002768 with gcc & native cc (hp, dec only) allowing
2769 Detlefs & Zorn comparison study (in SIGPLAN Notices.)
wdenk217c9da2002-10-25 20:35:49 +00002770
2771 Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu)
2772 * Based loosely on libg++-1.2X malloc. (It retains some of the overall
wdenk8bde7f72003-06-27 21:31:46 +00002773 structure of old version, but most details differ.)
wdenk217c9da2002-10-25 20:35:49 +00002774
2775*/