Heinrich Schuchardt | 5ad9220 | 2021-05-29 13:18:00 +0200 | [diff] [blame] | 1 | // 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 Gala | 81673e9 | 2008-05-13 19:01:54 -0500 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Kumar Gala | 81673e9 | 2008-05-13 19:01:54 -0500 | [diff] [blame] | 14 | |
Heinrich Schuchardt | be621c1 | 2020-04-15 18:46:23 +0200 | [diff] [blame] | 15 | #if CONFIG_IS_ENABLED(UNIT_TEST) |
Simon Glass | 6d7601e | 2014-07-10 22:23:33 -0600 | [diff] [blame] | 16 | #define DEBUG |
| 17 | #endif |
| 18 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 19 | #include <malloc.h> |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 20 | #include <asm/io.h> |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 21 | #include <valgrind/memcheck.h> |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 22 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 23 | #ifdef DEBUG |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 24 | #if __STD_C |
| 25 | static void malloc_update_mallinfo (void); |
| 26 | void malloc_stats (void); |
| 27 | #else |
| 28 | static void malloc_update_mallinfo (); |
| 29 | void malloc_stats(); |
| 30 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 31 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 32 | |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 33 | #ifdef CONFIG_AML_UASAN |
| 34 | #include <amlogic/uasan.h> |
| 35 | #endif |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 36 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 37 | #include <spinlock.h> |
| 38 | static spin_lock_t malloc_lock; |
| 39 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 40 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 43 | /* |
| 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 | |
| 62 | struct GmListElement; |
| 63 | typedef struct GmListElement GmListElement; |
| 64 | |
| 65 | struct GmListElement |
| 66 | { |
| 67 | GmListElement* next; |
| 68 | void* base; |
| 69 | }; |
| 70 | |
| 71 | static GmListElement* head = 0; |
| 72 | static unsigned int gNextAddress = 0; |
| 73 | static unsigned int gAddressBase = 0; |
| 74 | static unsigned int gAllocatedSize = 0; |
| 75 | |
| 76 | static |
| 77 | GmListElement* 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 | |
| 91 | void 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); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 100 | assert (rval); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 101 | } |
| 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 | |
| 112 | static |
| 113 | void* 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 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 125 | /* 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. */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 129 | |
| 130 | start_address = (char*)info.BaseAddress + info.RegionSize; |
| 131 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | /* 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. */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 143 | |
| 144 | start_address = |
| 145 | (void *) AlignPage64K((unsigned long) start_address); |
| 146 | } |
| 147 | } |
| 148 | return NULL; |
| 149 | |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void* 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 + |
| 165 | gAllocatedSize)) |
| 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 Schuchardt | a874cac | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 173 | if (!new_address) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 174 | return (void*)-1; |
| 175 | |
| 176 | gAddressBase = gNextAddress = |
| 177 | (unsigned int)VirtualAlloc (new_address, new_size, |
| 178 | MEM_RESERVE, PAGE_NOACCESS); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 179 | /* repeat in case of race condition */ |
| 180 | /* The region that we found has been snagged */ |
| 181 | /* by another thread */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 182 | } |
| 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 Schuchardt | a874cac | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 199 | if (!res) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 200 | 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 233 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | Type declarations |
| 237 | */ |
| 238 | |
| 239 | |
| 240 | struct 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 Tjernlund | 1ba91ba | 2010-10-14 08:51:34 +0200 | [diff] [blame] | 246 | } __attribute__((__may_alias__)) ; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 247 | |
| 248 | typedef 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-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 269 | | Size of previous chunk, if allocated | | |
| 270 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 271 | | Size of chunk, in bytes |P| |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 272 | mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 273 | | User data starts here... . |
| 274 | . . |
| 275 | . (malloc_usable_space() bytes) . |
| 276 | . | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 277 | nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 278 | | Size of chunk | |
| 279 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 280 | |
| 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-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 293 | | Size of previous chunk | |
| 294 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 295 | `head:' | Size of chunk, in bytes |P| |
| 296 | mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 297 | | 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 Bykowski | 9297e36 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 304 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 305 | nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 306 | `foot:' | Size of chunk, in bytes | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 307 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 308 | |
| 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 324 | 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.) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 329 | |
| 330 | 2. Chunks allocated via mmap, which have the second-lowest-order |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 331 | 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. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 334 | |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 374 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 375 | /* 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 Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 384 | #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 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 400 | #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 Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 409 | #endif /* CONFIG_AML_UASAN */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 410 | |
| 411 | /* Check if m has acceptable alignment */ |
| 412 | |
| 413 | #define aligned_OK(m) (((unsigned long)((m)) & (MALLOC_ALIGN_MASK)) == 0) |
| 414 | |
| 415 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 416 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 417 | |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 451 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 452 | |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 490 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 491 | |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 513 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 514 | |
| 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 | |
| 552 | typedef 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 Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 566 | #define top (av_[2]) /* The topmost chunk */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 567 | #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 | |
| 583 | static mbinptr av_[NAV * 2 + 2] = { |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 584 | NULL, NULL, |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 585 | 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 Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 603 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Gabor Juhos | 7b39523 | 2013-01-21 21:10:38 +0000 | [diff] [blame] | 604 | static void malloc_bin_reloc(void) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 605 | { |
Simon Glass | 9369184 | 2012-09-04 11:31:07 +0000 | [diff] [blame] | 606 | 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); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 611 | } |
Gabor Juhos | 7b39523 | 2013-01-21 21:10:38 +0000 | [diff] [blame] | 612 | #else |
| 613 | static inline void malloc_bin_reloc(void) {} |
Peter Tyser | 521af04 | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 614 | #endif |
Peter Tyser | 5e93bd1 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 615 | |
Marek Bykowski | 9297e36 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 616 | #ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT |
| 617 | static void malloc_init(void); |
| 618 | #endif |
| 619 | |
Peter Tyser | 5e93bd1 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 620 | ulong mem_malloc_start = 0; |
| 621 | ulong mem_malloc_end = 0; |
| 622 | ulong mem_malloc_brk = 0; |
| 623 | |
Simon Glass | 62d6383 | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 624 | static bool malloc_testing; /* enable test mode */ |
| 625 | static int malloc_max_allocs; /* return NULL after this many calls to malloc() */ |
| 626 | |
Peter Tyser | 5e93bd1 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 627 | void *sbrk(ptrdiff_t increment) |
| 628 | { |
| 629 | ulong old = mem_malloc_brk; |
| 630 | ulong new = old + increment; |
| 631 | |
Kumar Gala | 6163f5b | 2010-11-15 18:41:43 -0600 | [diff] [blame] | 632 | /* |
| 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 Tyser | 5e93bd1 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 639 | if ((new < mem_malloc_start) || (new > mem_malloc_end)) |
karl.beldan@gmail.com | ae30b8c | 2010-04-06 22:18:08 +0200 | [diff] [blame] | 640 | return (void *)MORECORE_FAILURE; |
Peter Tyser | 5e93bd1 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 641 | |
| 642 | mem_malloc_brk = new; |
| 643 | |
| 644 | return (void *)old; |
| 645 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 646 | |
Peter Tyser | d4e8ada | 2009-08-21 23:05:21 -0500 | [diff] [blame] | 647 | void 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 Bykowski | 9297e36 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 653 | #ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT |
| 654 | malloc_init(); |
| 655 | #endif |
| 656 | |
Thierry Reding | 868de51 | 2014-08-26 17:34:22 +0200 | [diff] [blame] | 657 | debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start, |
| 658 | mem_malloc_end); |
Przemyslaw Marczak | 0aa8a4a | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 659 | #ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT |
| 660 | memset((void *)mem_malloc_start, 0x0, size); |
| 661 | #endif |
Gabor Juhos | 7b39523 | 2013-01-21 21:10:38 +0000 | [diff] [blame] | 662 | malloc_bin_reloc(); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 663 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 664 | spin_lock_init(&malloc_lock); |
| 665 | #endif |
Peter Tyser | d4e8ada | 2009-08-21 23:05:21 -0500 | [diff] [blame] | 666 | } |
Peter Tyser | d4e8ada | 2009-08-21 23:05:21 -0500 | [diff] [blame] | 667 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 668 | /* 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): \ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 684 | 126) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 685 | /* |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 702 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 703 | |
| 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 Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 716 | #define binblocks_r ((INTERNAL_SIZE_T)av_[1]) /* bitvector of nonempty blocks */ |
| 717 | #define binblocks_w (av_[1]) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 718 | |
| 719 | /* bin<->block macros */ |
| 720 | |
| 721 | #define idx2binblock(ix) ((unsigned)1 << (ix / BINBLOCKWIDTH)) |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 722 | #define mark_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r | idx2binblock(ii))) |
| 723 | #define clear_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r & ~(idx2binblock(ii)))) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 724 | |
| 725 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 726 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 727 | |
| 728 | |
| 729 | /* Other static bookkeeping data */ |
| 730 | |
| 731 | /* variables holding tunable values */ |
| 732 | |
| 733 | static unsigned long trim_threshold = DEFAULT_TRIM_THRESHOLD; |
| 734 | static unsigned long top_pad = DEFAULT_TOP_PAD; |
| 735 | static unsigned int n_mmaps_max = DEFAULT_MMAP_MAX; |
| 736 | static unsigned long mmap_threshold = DEFAULT_MMAP_THRESHOLD; |
| 737 | |
| 738 | /* The first value returned from sbrk */ |
| 739 | static char* sbrk_base = (char*)(-1); |
| 740 | |
| 741 | /* The maximum memory obtained from system via sbrk */ |
| 742 | static unsigned long max_sbrked_mem = 0; |
| 743 | |
| 744 | /* The maximum via either sbrk or mmap */ |
| 745 | static unsigned long max_total_mem = 0; |
| 746 | |
| 747 | /* internal working copy of mallinfo */ |
| 748 | static 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 Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 755 | #ifdef DEBUG |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 756 | static unsigned int n_mmaps = 0; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 757 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 758 | static unsigned long mmapped_mem = 0; |
| 759 | #if HAVE_MMAP |
| 760 | static unsigned int max_n_mmaps = 0; |
| 761 | static unsigned long max_mmapped_mem = 0; |
| 762 | #endif |
| 763 | |
Marek Bykowski | 9297e36 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 764 | #ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT |
| 765 | static void malloc_init(void) |
| 766 | { |
| 767 | int i, j; |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 768 | |
Marek Bykowski | 9297e36 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 769 | 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 *)¤t_mallinfo, 0, sizeof(struct mallinfo)); |
| 791 | #endif |
| 792 | } |
| 793 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 794 | |
| 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 |
| 811 | static void do_check_chunk(mchunkptr p) |
| 812 | #else |
| 813 | static void do_check_chunk(p) mchunkptr p; |
| 814 | #endif |
| 815 | { |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 816 | INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 817 | |
| 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 |
| 832 | static void do_check_free_chunk(mchunkptr p) |
| 833 | #else |
| 834 | static void do_check_free_chunk(p) mchunkptr p; |
| 835 | #endif |
| 836 | { |
| 837 | INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 838 | mchunkptr next = chunk_at_offset(p, sz); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 839 | |
| 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 |
| 865 | static void do_check_inuse_chunk(mchunkptr p) |
| 866 | #else |
| 867 | static 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 |
| 897 | static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s) |
| 898 | #else |
| 899 | static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s; |
| 900 | #endif |
| 901 | { |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 902 | INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; |
| 903 | long room = sz - s; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 904 | |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 934 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 935 | |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 1006 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1007 | |
| 1008 | |
| 1009 | /* Routines dealing with mmap(). */ |
| 1010 | |
| 1011 | #if HAVE_MMAP |
| 1012 | |
| 1013 | #if __STD_C |
| 1014 | static mchunkptr mmap_chunk(size_t size) |
| 1015 | #else |
| 1016 | static 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 |
| 1069 | static void munmap_chunk(mchunkptr p) |
| 1070 | #else |
| 1071 | static 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 |
| 1094 | static mchunkptr mremap_chunk(mchunkptr p, size_t new_size) |
| 1095 | #else |
| 1096 | static 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 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1136 | /* |
| 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 |
| 1142 | static void malloc_extend_top(INTERNAL_SIZE_T nb) |
| 1143 | #else |
| 1144 | static 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)) & |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1203 | ~(pagesz - 1)) - ((unsigned long)(brk + sbrk_size)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1204 | |
| 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 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1224 | set_head(top, PREV_INUSE); /* will force null return from malloc */ |
| 1225 | return; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1226 | } |
| 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 = |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1232 | SIZE_SZ|PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1233 | chunk_at_offset(old_top, old_top_size + SIZE_SZ)->size = |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1234 | SIZE_SZ|PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1235 | /* If possible, release the rest. */ |
| 1236 | if (old_top_size >= MINSIZE) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1237 | fREe(chunk2mem(old_top)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1238 | } |
| 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 1251 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1252 | |
| 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1268 | a chunk of exactly the right size is found, it is taken. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1269 | |
| 1270 | 2. The most recently remaindered chunk is used if it is big |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1271 | 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. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1278 | |
| 1279 | 3. Other bins are scanned in increasing size order, using a |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1280 | 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. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1284 | |
| 1285 | 4. If large enough, the chunk bordering the end of memory |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1286 | (`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). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1291 | |
| 1292 | 5. If the request size meets the mmap threshold and the |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1293 | 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. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1296 | |
| 1297 | 6. Otherwise, the top of memory is extended by |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1298 | 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. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1305 | |
| 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 |
| 1316 | Void_t* mALLOc(size_t bytes) |
| 1317 | #else |
| 1318 | Void_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 Yan | f1896c4 | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 1336 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
Stephen Warren | deff6fb | 2016-03-05 10:30:53 -0700 | [diff] [blame] | 1337 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 1338 | return malloc_simple(bytes); |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1339 | #endif |
| 1340 | |
Simon Glass | 62d6383 | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 1341 | if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) { |
| 1342 | if (--malloc_max_allocs < 0) |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | |
Wolfgang Denk | 2740544 | 2010-01-15 11:20:10 +0100 | [diff] [blame] | 1346 | /* check if mem_malloc_init() was run */ |
| 1347 | if ((mem_malloc_start == 0) && (mem_malloc_end == 0)) { |
| 1348 | /* not initialized yet */ |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1349 | return NULL; |
Wolfgang Denk | 2740544 | 2010-01-15 11:20:10 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1352 | if ((long)bytes < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1353 | |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1354 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1355 | spin_lock(&malloc_lock); |
| 1356 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1357 | 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1382 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1383 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1384 | spin_unlock(&malloc_lock); |
| 1385 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1386 | #ifdef CONFIG_AML_UASAN |
| 1387 | uasan_alloc(victim, bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1388 | return chunk2mem(victim); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1389 | #else |
| 1390 | return chunk2mem(victim); |
| 1391 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1392 | } |
| 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 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1409 | --idx; /* adjust to rescan below after checking last remainder */ |
| 1410 | break; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | else if (remainder_size >= 0) /* exact fit */ |
| 1414 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1415 | unlink(victim, bck, fwd); |
| 1416 | set_inuse_bit_at_offset(victim, victim_size); |
| 1417 | check_malloced_chunk(victim, nb); |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1418 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1419 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1420 | spin_unlock(&malloc_lock); |
| 1421 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1422 | #ifdef CONFIG_AML_UASAN |
| 1423 | uasan_alloc(victim, bytes); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1424 | return chunk2mem(victim); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1425 | #else |
| 1426 | return chunk2mem(victim); |
| 1427 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1428 | } |
| 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1450 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1451 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1452 | spin_unlock(&malloc_lock); |
| 1453 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1454 | #ifdef CONFIG_AML_UASAN |
| 1455 | uasan_alloc(victim, bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1456 | return chunk2mem(victim); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1457 | #else |
| 1458 | return chunk2mem(victim); |
| 1459 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1460 | } |
| 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1468 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1469 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1470 | spin_unlock(&malloc_lock); |
| 1471 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1472 | #ifdef CONFIG_AML_UASAN |
| 1473 | uasan_alloc(victim, bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1474 | return chunk2mem(victim); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1475 | #else |
| 1476 | return chunk2mem(victim); |
| 1477 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1478 | } |
| 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 Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1490 | if ( (block = idx2binblock(idx)) <= binblocks_r) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1491 | { |
| 1492 | |
| 1493 | /* Get to the first marked block */ |
| 1494 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1495 | if ( (block & binblocks_r) == 0) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1496 | { |
| 1497 | /* force to an even block boundary */ |
| 1498 | idx = (idx & ~(BINBLOCKWIDTH - 1)) + BINBLOCKWIDTH; |
| 1499 | block <<= 1; |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1500 | while ((block & binblocks_r) == 0) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1501 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1502 | idx += BINBLOCKWIDTH; |
| 1503 | block <<= 1; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1504 | } |
| 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 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1516 | /* Find and use first big enough chunk ... */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1517 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1518 | for (victim = last(bin); victim != bin; victim = victim->bk) |
| 1519 | { |
| 1520 | victim_size = chunksize(victim); |
| 1521 | remainder_size = victim_size - nb; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1522 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1523 | 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1532 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1533 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1534 | spin_unlock(&malloc_lock); |
| 1535 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1536 | #ifdef CONFIG_AML_UASAN |
| 1537 | uasan_alloc(victim, bytes); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1538 | return chunk2mem(victim); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1539 | #else |
| 1540 | return chunk2mem(victim); |
| 1541 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1542 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1543 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1544 | 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1549 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1550 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1551 | spin_unlock(&malloc_lock); |
| 1552 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1553 | #ifdef CONFIG_AML_UASAN |
| 1554 | uasan_alloc(victim, bytes); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1555 | return chunk2mem(victim); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1556 | #else |
| 1557 | return chunk2mem(victim); |
| 1558 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1559 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1560 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1561 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1562 | |
| 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 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1571 | if ((startidx & (BINBLOCKWIDTH - 1)) == 0) |
| 1572 | { |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1573 | av_[1] = (mbinptr)(binblocks_r & ~block); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1574 | break; |
| 1575 | } |
| 1576 | --startidx; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1577 | q = prev_bin(q); |
| 1578 | } while (first(q) == q); |
| 1579 | |
| 1580 | /* Get to the next possibly nonempty block */ |
| 1581 | |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1582 | if ( (block <<= 1) <= binblocks_r && (block != 0) ) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1583 | { |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1584 | while ((block & binblocks_r) == 0) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1585 | { |
| 1586 | idx += BINBLOCKWIDTH; |
| 1587 | block <<= 1; |
| 1588 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1589 | } |
| 1590 | else |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1591 | break; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1592 | } |
| 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 Schuchardt | a874cac | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 1605 | (victim = mmap_chunk(nb))) |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1606 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1607 | return chunk2mem(victim); |
| 1608 | #endif |
| 1609 | |
| 1610 | /* Try to extend */ |
| 1611 | malloc_extend_top(nb); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1612 | #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 |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1618 | if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE) |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1619 | return NULL; /* propagate failure */ |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1620 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1621 | } |
| 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1628 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1629 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1630 | spin_unlock(&malloc_lock); |
| 1631 | #endif |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1632 | #ifdef CONFIG_AML_UASAN |
| 1633 | uasan_alloc(victim, bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1634 | return chunk2mem(victim); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1635 | #else |
| 1636 | return chunk2mem(victim); |
| 1637 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1638 | |
| 1639 | } |
| 1640 | |
| 1641 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 1642 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1643 | |
| 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, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1655 | it is consolidated into the top, and if the total unused |
| 1656 | topmost memory exceeds the trim threshold, malloc_trim is |
| 1657 | called. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1658 | |
| 1659 | 4. Other chunks are consolidated as they arrive, and |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1660 | placed in corresponding bins. (This includes the case of |
| 1661 | consolidating with the current `last_remainder'). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1662 | |
| 1663 | */ |
| 1664 | |
| 1665 | |
| 1666 | #if __STD_C |
| 1667 | void fREe(Void_t* mem) |
| 1668 | #else |
| 1669 | void 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 Yan | f1896c4 | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 1683 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1684 | /* free() is a no-op - all the memory will be freed on relocation */ |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1685 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
| 1686 | VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ); |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1687 | return; |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1688 | } |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1689 | #endif |
| 1690 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1691 | if (mem == NULL) /* free(0) has no effect */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1692 | 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.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1705 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1706 | spin_lock(&malloc_lock); |
| 1707 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1708 | check_inuse_chunk(p); |
| 1709 | |
| 1710 | sz = hd & ~PREV_INUSE; |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1711 | |
| 1712 | #ifdef CONFIG_AML_UASAN |
| 1713 | uasan_free(p, sz); |
| 1714 | #endif |
| 1715 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1716 | next = chunk_at_offset(p, sz); |
| 1717 | nextsz = chunksize(next); |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1718 | VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1719 | |
| 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.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1736 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1737 | spin_unlock(&malloc_lock); |
| 1738 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1739 | 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.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1776 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1777 | spin_unlock(&malloc_lock); |
| 1778 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1782 | /* |
| 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.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1817 | #ifdef CONFIG_ARMV8_MULTIENTRY |
| 1818 | Void_t* rEALLOc(Void_t* oldmem, size_t bytes) |
| 1819 | { |
| 1820 | void *new; |
| 1821 | mchunkptr oldp; |
| 1822 | size_t oldsize; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1823 | |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 1824 | 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 |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1843 | #if __STD_C |
| 1844 | Void_t* rEALLOc(Void_t* oldmem, size_t bytes) |
| 1845 | #else |
| 1846 | Void_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 Schuchardt | a874cac | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 1871 | if (!bytes) { |
| 1872 | fREe(oldmem); |
| 1873 | return NULL; |
| 1874 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1875 | #endif |
| 1876 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1877 | if ((long)bytes < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1878 | |
| 1879 | /* realloc of null is supposed to be same as malloc */ |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1880 | if (oldmem == NULL) return mALLOc(bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1881 | |
Andy Yan | f1896c4 | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 1882 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 1883 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1884 | /* This is harder to support and should not be needed */ |
| 1885 | panic("pre-reloc realloc() is not supported"); |
| 1886 | } |
| 1887 | #endif |
| 1888 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1889 | 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 Schuchardt | a874cac | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 1906 | if (!newmem) |
| 1907 | return NULL; /* propagate failure */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1908 | 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 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1929 | 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1935 | VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ); |
| 1936 | VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1937 | #ifdef CONFIG_AML_UASAN |
| 1938 | uasan_alloc(oldp, bytes); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1939 | return chunk2mem(oldp); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1940 | #else |
| 1941 | return chunk2mem(oldp); |
| 1942 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1943 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | /* Forward into next chunk */ |
| 1947 | else if (((long)(nextsize + newsize) >= (long)(nb))) |
| 1948 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1949 | unlink(next, bck, fwd); |
| 1950 | newsize += nextsize; |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1951 | VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ); |
| 1952 | VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1953 | goto split; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | else |
| 1957 | { |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1958 | next = NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1959 | 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 Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1971 | if (next != NULL) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1972 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1973 | /* 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1982 | VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1983 | #ifdef CONFIG_AML_UASAN |
| 1984 | MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 1985 | oldmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 1986 | oldsize - SIZE_SZ); |
| 1987 | #else |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1988 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1989 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1990 | top = chunk_at_offset(newp, nb); |
| 1991 | set_head(top, (newsize - nb) | PREV_INUSE); |
| 1992 | set_head_size(newp, nb); |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1993 | VALGRIND_FREELIKE_BLOCK(oldmem, SIZE_SZ); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1994 | #ifdef CONFIG_AML_UASAN |
| 1995 | uasan_alloc(newp, bytes); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1996 | return newmem; |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 1997 | #else |
| 1998 | return newmem; |
| 1999 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2002 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2003 | /* 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2011 | VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2012 | #ifdef CONFIG_AML_UASAN |
| 2013 | MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 2014 | oldmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 2015 | oldsize - SIZE_SZ); |
| 2016 | #else |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2017 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2018 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2019 | goto split; |
| 2020 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | /* backward only */ |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2024 | if (prev != NULL && (long)(prevsize + newsize) >= (long)nb) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2025 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2026 | unlink(prev, bck, fwd); |
| 2027 | newp = prev; |
| 2028 | newsize += prevsize; |
| 2029 | newmem = chunk2mem(newp); |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2030 | VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2031 | #ifdef CONFIG_AML_UASAN |
| 2032 | MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 2033 | oldmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 2034 | oldsize - SIZE_SZ); |
| 2035 | #else |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2036 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2037 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2038 | goto split; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | /* Must allocate */ |
| 2043 | |
| 2044 | newmem = mALLOc (bytes); |
| 2045 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2046 | if (newmem == NULL) /* propagate failure */ |
| 2047 | return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2048 | |
| 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 Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2060 | #ifdef CONFIG_AML_UASAN |
| 2061 | MALLOC_COPY(newmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 2062 | oldmem - UASAN_ALLOCA_REDZONE_SIZE, |
| 2063 | oldsize - SIZE_SZ); |
| 2064 | #else |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2065 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2066 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2067 | fREe(oldmem); |
| 2068 | return newmem; |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2069 | } else { |
| 2070 | VALGRIND_RESIZEINPLACE_BLOCK(oldmem, 0, bytes, SIZE_SZ); |
| 2071 | VALGRIND_MAKE_MEM_DEFINED(oldmem, bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2072 | } |
| 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2084 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ, |
| 2085 | false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2086 | 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 Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2095 | #ifdef CONFIG_AML_UASAN |
| 2096 | uasan_alloc(newp, bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2097 | return chunk2mem(newp); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2098 | #else |
| 2099 | return chunk2mem(newp); |
| 2100 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2101 | } |
xia.jin | 383142c | 2024-06-11 06:30:53 +0000 | [diff] [blame^] | 2102 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2103 | |
| 2104 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2105 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2106 | |
| 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 |
| 2127 | Void_t* mEMALIGn(size_t alignment, size_t bytes) |
| 2128 | #else |
| 2129 | Void_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 Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2142 | if ((long)bytes < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2143 | |
Ley Foon Tan | ee038c5 | 2018-05-18 18:03:12 +0800 | [diff] [blame] | 2144 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
| 2145 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
Andreas Dannenberg | 4c6be01 | 2019-03-27 13:17:26 -0500 | [diff] [blame] | 2146 | return memalign_simple(alignment, bytes); |
Ley Foon Tan | ee038c5 | 2018-05-18 18:03:12 +0800 | [diff] [blame] | 2147 | } |
| 2148 | #endif |
| 2149 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2150 | /* 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 Warren | 4f144a4 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 2163 | /* |
| 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 Warren | 034eda8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2172 | size_t extra, extra2; |
Stephen Warren | 4f144a4 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 2173 | /* |
| 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 Warren | 034eda8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2181 | /* |
| 2182 | * Otherwise, try again, requesting enough extra space to be able to |
| 2183 | * acquire alignment. |
| 2184 | */ |
Stephen Warren | 4f144a4 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 2185 | fREe(m); |
Stephen Warren | 034eda8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2186 | /* 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 Warren | 4f144a4 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2204 | if (m == NULL) return NULL; /* propagate failure */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2205 | |
| 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2249 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(p), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2250 | |
| 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 Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2263 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ, |
| 2264 | false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2265 | fREe(chunk2mem(remainder)); |
| 2266 | } |
| 2267 | |
| 2268 | check_inuse_chunk(p); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2269 | #ifdef CONFIG_AML_UASAN |
| 2270 | uasan_alloc(p, bytes); |
| 2271 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2272 | return chunk2mem(p); |
| 2273 | |
| 2274 | } |
| 2275 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2276 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2277 | |
| 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 |
| 2286 | Void_t* vALLOc(size_t bytes) |
| 2287 | #else |
| 2288 | Void_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 |
| 2301 | Void_t* pvALLOc(size_t bytes) |
| 2302 | #else |
| 2303 | Void_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 |
| 2317 | Void_t* cALLOc(size_t n, size_t elem_size) |
| 2318 | #else |
| 2319 | Void_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 Marczak | 0aa8a4a | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 2329 | #ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2330 | #if MORECORE_CLEARS |
| 2331 | mchunkptr oldtop = top; |
| 2332 | INTERNAL_SIZE_T oldtopsize = chunksize(top); |
| 2333 | #endif |
Przemyslaw Marczak | 0aa8a4a | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 2334 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2335 | Void_t* mem = mALLOc (sz); |
| 2336 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2337 | if ((long)n < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2338 | |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2339 | if (mem == NULL) |
| 2340 | return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2341 | else |
| 2342 | { |
Andy Yan | f1896c4 | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 2343 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
Simon Glass | c9356be | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 2344 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
Simon Goldschmidt | bb71a2d | 2019-10-25 21:23:35 +0200 | [diff] [blame] | 2345 | memset(mem, 0, sz); |
Simon Glass | d59476b | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 2346 | return mem; |
| 2347 | } |
| 2348 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2349 | 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 Marczak | 0aa8a4a | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 2360 | #ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2361 | #if MORECORE_CLEARS |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2362 | if (p == oldtop && csz > oldtopsize && oldtopsize > sz) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2363 | { |
| 2364 | /* clear only the bytes from non-freshly-sbrked memory */ |
| 2365 | csz = oldtopsize; |
| 2366 | } |
| 2367 | #endif |
Przemyslaw Marczak | 0aa8a4a | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 2368 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2369 | |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2370 | #ifdef CONFIG_AML_UASAN |
| 2371 | /* avoid overwrite */ |
| 2372 | MALLOC_ZERO(mem, csz - SIZE_SZ - UASAN_ALLOCA_REDZONE_SIZE); |
| 2373 | #else |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2374 | MALLOC_ZERO(mem, csz - SIZE_SZ); |
Bo Lv | 96a66d0 | 2023-05-12 19:18:22 +0800 | [diff] [blame] | 2375 | #endif |
Sean Anderson | bdaeea1 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2376 | VALGRIND_MAKE_MEM_DEFINED(mem, sz); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2377 | 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 |
| 2390 | void cfree(Void_t *mem) |
| 2391 | #else |
| 2392 | void cfree(mem) Void_t *mem; |
| 2393 | #endif |
| 2394 | { |
| 2395 | fREe(mem); |
| 2396 | } |
| 2397 | #endif |
| 2398 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2399 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2400 | |
| 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 |
| 2425 | int malloc_trim(size_t pad) |
| 2426 | #else |
| 2427 | int 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 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2456 | /* 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; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | else |
| 2469 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2470 | /* Success. Adjust top accordingly. */ |
| 2471 | set_head(top, (top_size - extra) | PREV_INUSE); |
| 2472 | sbrked_mem -= extra; |
| 2473 | check_chunk(top); |
| 2474 | return 1; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2480 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2481 | |
| 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 |
| 2494 | size_t malloc_usable_size(Void_t* mem) |
| 2495 | #else |
| 2496 | size_t malloc_usable_size(mem) Void_t* mem; |
| 2497 | #endif |
| 2498 | { |
| 2499 | mchunkptr p; |
Kim Phillips | 199adb6 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2500 | if (mem == NULL) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2501 | 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 Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2516 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2517 | |
| 2518 | /* Utility to update current_mallinfo for malloc_stats and mallinfo() */ |
| 2519 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2520 | #ifdef DEBUG |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2521 | static 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); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2541 | q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE; |
| 2542 | q = next_chunk(q)) |
| 2543 | check_inuse_chunk(q); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2544 | #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 Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2558 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2559 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2560 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2561 | |
| 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 Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2577 | #ifdef DEBUG |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2578 | void malloc_stats() |
| 2579 | { |
| 2580 | malloc_update_mallinfo(); |
| 2581 | printf("max system bytes = %10u\n", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2582 | (unsigned int)(max_total_mem)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2583 | printf("system bytes = %10u\n", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2584 | (unsigned int)(sbrked_mem + mmapped_mem)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2585 | printf("in use bytes = %10u\n", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2586 | (unsigned int)(current_mallinfo.uordblks + mmapped_mem)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2587 | #if HAVE_MMAP |
| 2588 | printf("max mmap regions = %10u\n", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2589 | (unsigned int)max_n_mmaps); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2590 | #endif |
| 2591 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2592 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2593 | |
| 2594 | /* |
| 2595 | mallinfo returns a copy of updated current mallinfo. |
| 2596 | */ |
| 2597 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2598 | #ifdef DEBUG |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2599 | struct mallinfo mALLINFo() |
| 2600 | { |
| 2601 | malloc_update_mallinfo(); |
| 2602 | return current_mallinfo; |
| 2603 | } |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2604 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2605 | |
| 2606 | |
Simon Glass | d93041a | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2607 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2608 | |
| 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 |
| 2623 | int mALLOPt(int param_number, int value) |
| 2624 | #else |
| 2625 | int 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 Glass | fb5cf7f | 2015-02-27 22:06:36 -0700 | [diff] [blame] | 2648 | int initf_malloc(void) |
| 2649 | { |
Andy Yan | f1896c4 | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 2650 | #if CONFIG_VAL(SYS_MALLOC_F_LEN) |
Simon Glass | fb5cf7f | 2015-02-27 22:06:36 -0700 | [diff] [blame] | 2651 | assert(gd->malloc_base); /* Set up by crt0.S */ |
Andy Yan | f1896c4 | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 2652 | gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN); |
Simon Glass | fb5cf7f | 2015-02-27 22:06:36 -0700 | [diff] [blame] | 2653 | gd->malloc_ptr = 0; |
| 2654 | #endif |
| 2655 | |
| 2656 | return 0; |
| 2657 | } |
| 2658 | |
Simon Glass | 62d6383 | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 2659 | void malloc_enable_testing(int max_allocs) |
| 2660 | { |
| 2661 | malloc_testing = true; |
| 2662 | malloc_max_allocs = max_allocs; |
| 2663 | } |
| 2664 | |
| 2665 | void malloc_disable_testing(void) |
| 2666 | { |
| 2667 | malloc_testing = false; |
| 2668 | } |
| 2669 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2670 | /* |
| 2671 | |
| 2672 | History: |
| 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> |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2677 | * 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 |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2685 | usage of 'assert' in non-WIN32 code |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2686 | * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to |
| 2687 | avoid infinite loop |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2688 | * 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2699 | foreign sbrks |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2700 | * 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2705 | Wolfram Gloger (Gloger@lrz.uni-muenchen.de). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2706 | * 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2713 | avoid surprises about sbrk alignment conventions. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2714 | * Add mallinfo, mallopt. Thanks to Raymond Nijssen |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2715 | (raymond@es.ele.tue.nl) for the suggestion. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2716 | * Add `pad' argument to malloc_trim and top_pad mallopt parameter. |
| 2717 | * More precautions for cases where other routines call sbrk, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2718 | courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2719 | * Added macros etc., allowing use in linux libc from |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2720 | H.J. Lu (hjl@gnu.ai.mit.edu) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2721 | * 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2726 | the work required to undo bad preallocations exceeds |
| 2727 | the work saved in good cases for most test programs. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2728 | * No longer use return list or unconsolidated bins since |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2729 | no scheme using them consistently outperforms those that don't |
| 2730 | given above changes. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2731 | * 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2736 | Paul Wilson (wilson@cs.texas.edu) for the suggestion. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2737 | |
| 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2740 | (wmglo@Dent.MED.Uni-Muenchen.DE). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2741 | |
| 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2756 | (eliminating old malloc_find_space & malloc_clean_bin) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2757 | * 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2760 | from kpv@research.att.com |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2761 | |
| 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2768 | with gcc & native cc (hp, dec only) allowing |
| 2769 | Detlefs & Zorn comparison study (in SIGPLAN Notices.) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2770 | |
| 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 |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2773 | structure of old version, but most details differ.) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2774 | |
| 2775 | */ |