Heinrich Schuchardt | 862dd21 | 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 | |
Heinrich Schuchardt | 28105f4 | 2020-04-15 18:46:23 +0200 | [diff] [blame] | 11 | #if CONFIG_IS_ENABLED(UNIT_TEST) |
Simon Glass | 05c8600 | 2014-07-10 22:23:33 -0600 | [diff] [blame] | 12 | #define DEBUG |
| 13 | #endif |
| 14 | |
Sean Anderson | 8b2a481 | 2023-10-07 22:01:56 -0400 | [diff] [blame] | 15 | #include <log.h> |
| 16 | #include <asm/global_data.h> |
| 17 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 18 | #include <malloc.h> |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 19 | #include <asm/io.h> |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 20 | #include <valgrind/memcheck.h> |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 21 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 22 | #ifdef DEBUG |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 23 | #if __STD_C |
| 24 | static void malloc_update_mallinfo (void); |
| 25 | void malloc_stats (void); |
| 26 | #else |
| 27 | static void malloc_update_mallinfo (); |
| 28 | void malloc_stats(); |
| 29 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 30 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 31 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 34 | #ifdef MCHECK_HEAP_PROTECTION |
| 35 | #define STATIC_IF_MCHECK static |
Eugene Uriev | 5a1d968 | 2024-03-31 23:03:20 +0300 | [diff] [blame] | 36 | #undef MALLOC_COPY |
| 37 | #undef MALLOC_ZERO |
| 38 | static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); } |
| 39 | static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(dest, src, sz); } |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 40 | #else |
| 41 | #define STATIC_IF_MCHECK |
| 42 | #define mALLOc_impl mALLOc |
| 43 | #define fREe_impl fREe |
| 44 | #define rEALLOc_impl rEALLOc |
| 45 | #define mEMALIGn_impl mEMALIGn |
| 46 | #define cALLOc_impl cALLOc |
| 47 | #endif |
| 48 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 49 | /* |
| 50 | Emulation of sbrk for WIN32 |
| 51 | All code within the ifdef WIN32 is untested by me. |
| 52 | |
| 53 | Thanks to Martin Fong and others for supplying this. |
| 54 | */ |
| 55 | |
| 56 | |
| 57 | #ifdef WIN32 |
| 58 | |
| 59 | #define AlignPage(add) (((add) + (malloc_getpagesize-1)) & \ |
| 60 | ~(malloc_getpagesize-1)) |
| 61 | #define AlignPage64K(add) (((add) + (0x10000 - 1)) & ~(0x10000 - 1)) |
| 62 | |
| 63 | /* resrve 64MB to insure large contiguous space */ |
| 64 | #define RESERVED_SIZE (1024*1024*64) |
| 65 | #define NEXT_SIZE (2048*1024) |
| 66 | #define TOP_MEMORY ((unsigned long)2*1024*1024*1024) |
| 67 | |
| 68 | struct GmListElement; |
| 69 | typedef struct GmListElement GmListElement; |
| 70 | |
| 71 | struct GmListElement |
| 72 | { |
| 73 | GmListElement* next; |
| 74 | void* base; |
| 75 | }; |
| 76 | |
| 77 | static GmListElement* head = 0; |
| 78 | static unsigned int gNextAddress = 0; |
| 79 | static unsigned int gAddressBase = 0; |
| 80 | static unsigned int gAllocatedSize = 0; |
| 81 | |
| 82 | static |
| 83 | GmListElement* makeGmListElement (void* bas) |
| 84 | { |
| 85 | GmListElement* this; |
| 86 | this = (GmListElement*)(void*)LocalAlloc (0, sizeof (GmListElement)); |
| 87 | assert (this); |
| 88 | if (this) |
| 89 | { |
| 90 | this->base = bas; |
| 91 | this->next = head; |
| 92 | head = this; |
| 93 | } |
| 94 | return this; |
| 95 | } |
| 96 | |
Tom Rini | 03787a9 | 2023-02-27 17:08:34 -0500 | [diff] [blame] | 97 | void gcleanup (void) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 98 | { |
| 99 | BOOL rval; |
| 100 | assert ( (head == NULL) || (head->base == (void*)gAddressBase)); |
| 101 | if (gAddressBase && (gNextAddress - gAddressBase)) |
| 102 | { |
| 103 | rval = VirtualFree ((void*)gAddressBase, |
| 104 | gNextAddress - gAddressBase, |
| 105 | MEM_DECOMMIT); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 106 | assert (rval); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 107 | } |
| 108 | while (head) |
| 109 | { |
| 110 | GmListElement* next = head->next; |
| 111 | rval = VirtualFree (head->base, 0, MEM_RELEASE); |
| 112 | assert (rval); |
| 113 | LocalFree (head); |
| 114 | head = next; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static |
| 119 | void* findRegion (void* start_address, unsigned long size) |
| 120 | { |
| 121 | MEMORY_BASIC_INFORMATION info; |
| 122 | if (size >= TOP_MEMORY) return NULL; |
| 123 | |
| 124 | while ((unsigned long)start_address + size < TOP_MEMORY) |
| 125 | { |
| 126 | VirtualQuery (start_address, &info, sizeof (info)); |
| 127 | if ((info.State == MEM_FREE) && (info.RegionSize >= size)) |
| 128 | return start_address; |
| 129 | else |
| 130 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 131 | /* Requested region is not available so see if the */ |
| 132 | /* next region is available. Set 'start_address' */ |
| 133 | /* to the next region and call 'VirtualQuery()' */ |
| 134 | /* again. */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 135 | |
| 136 | start_address = (char*)info.BaseAddress + info.RegionSize; |
| 137 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 138 | /* Make sure we start looking for the next region */ |
| 139 | /* on the *next* 64K boundary. Otherwise, even if */ |
| 140 | /* the new region is free according to */ |
| 141 | /* 'VirtualQuery()', the subsequent call to */ |
| 142 | /* 'VirtualAlloc()' (which follows the call to */ |
| 143 | /* this routine in 'wsbrk()') will round *down* */ |
| 144 | /* the requested address to a 64K boundary which */ |
| 145 | /* we already know is an address in the */ |
| 146 | /* unavailable region. Thus, the subsequent call */ |
| 147 | /* to 'VirtualAlloc()' will fail and bring us back */ |
| 148 | /* here, causing us to go into an infinite loop. */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 149 | |
| 150 | start_address = |
| 151 | (void *) AlignPage64K((unsigned long) start_address); |
| 152 | } |
| 153 | } |
| 154 | return NULL; |
| 155 | |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void* wsbrk (long size) |
| 160 | { |
| 161 | void* tmp; |
| 162 | if (size > 0) |
| 163 | { |
| 164 | if (gAddressBase == 0) |
| 165 | { |
| 166 | gAllocatedSize = max (RESERVED_SIZE, AlignPage (size)); |
| 167 | gNextAddress = gAddressBase = |
| 168 | (unsigned int)VirtualAlloc (NULL, gAllocatedSize, |
| 169 | MEM_RESERVE, PAGE_NOACCESS); |
| 170 | } else if (AlignPage (gNextAddress + size) > (gAddressBase + |
| 171 | gAllocatedSize)) |
| 172 | { |
| 173 | long new_size = max (NEXT_SIZE, AlignPage (size)); |
| 174 | void* new_address = (void*)(gAddressBase+gAllocatedSize); |
| 175 | do |
| 176 | { |
| 177 | new_address = findRegion (new_address, new_size); |
| 178 | |
Heinrich Schuchardt | b58b9ca | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 179 | if (!new_address) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 180 | return (void*)-1; |
| 181 | |
| 182 | gAddressBase = gNextAddress = |
| 183 | (unsigned int)VirtualAlloc (new_address, new_size, |
| 184 | MEM_RESERVE, PAGE_NOACCESS); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 185 | /* repeat in case of race condition */ |
| 186 | /* The region that we found has been snagged */ |
| 187 | /* by another thread */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 188 | } |
| 189 | while (gAddressBase == 0); |
| 190 | |
| 191 | assert (new_address == (void*)gAddressBase); |
| 192 | |
| 193 | gAllocatedSize = new_size; |
| 194 | |
| 195 | if (!makeGmListElement ((void*)gAddressBase)) |
| 196 | return (void*)-1; |
| 197 | } |
| 198 | if ((size + gNextAddress) > AlignPage (gNextAddress)) |
| 199 | { |
| 200 | void* res; |
| 201 | res = VirtualAlloc ((void*)AlignPage (gNextAddress), |
| 202 | (size + gNextAddress - |
| 203 | AlignPage (gNextAddress)), |
| 204 | MEM_COMMIT, PAGE_READWRITE); |
Heinrich Schuchardt | b58b9ca | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 205 | if (!res) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 206 | return (void*)-1; |
| 207 | } |
| 208 | tmp = (void*)gNextAddress; |
| 209 | gNextAddress = (unsigned int)tmp + size; |
| 210 | return tmp; |
| 211 | } |
| 212 | else if (size < 0) |
| 213 | { |
| 214 | unsigned int alignedGoal = AlignPage (gNextAddress + size); |
| 215 | /* Trim by releasing the virtual memory */ |
| 216 | if (alignedGoal >= gAddressBase) |
| 217 | { |
| 218 | VirtualFree ((void*)alignedGoal, gNextAddress - alignedGoal, |
| 219 | MEM_DECOMMIT); |
| 220 | gNextAddress = gNextAddress + size; |
| 221 | return (void*)gNextAddress; |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | VirtualFree ((void*)gAddressBase, gNextAddress - gAddressBase, |
| 226 | MEM_DECOMMIT); |
| 227 | gNextAddress = gAddressBase; |
| 228 | return (void*)-1; |
| 229 | } |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | return (void*)gNextAddress; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | #endif |
| 238 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 239 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | Type declarations |
| 243 | */ |
| 244 | |
| 245 | |
| 246 | struct malloc_chunk |
| 247 | { |
| 248 | INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */ |
| 249 | INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */ |
| 250 | struct malloc_chunk* fd; /* double links -- used only if free. */ |
| 251 | struct malloc_chunk* bk; |
Joakim Tjernlund | c183eea | 2010-10-14 08:51:34 +0200 | [diff] [blame] | 252 | } __attribute__((__may_alias__)) ; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 253 | |
| 254 | typedef struct malloc_chunk* mchunkptr; |
| 255 | |
| 256 | /* |
| 257 | |
| 258 | malloc_chunk details: |
| 259 | |
| 260 | (The following includes lightly edited explanations by Colin Plumb.) |
| 261 | |
| 262 | Chunks of memory are maintained using a `boundary tag' method as |
| 263 | described in e.g., Knuth or Standish. (See the paper by Paul |
| 264 | Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a |
| 265 | survey of such techniques.) Sizes of free chunks are stored both |
| 266 | in the front of each chunk and at the end. This makes |
| 267 | consolidating fragmented chunks into bigger chunks very fast. The |
| 268 | size fields also hold bits representing whether chunks are free or |
| 269 | in use. |
| 270 | |
| 271 | An allocated chunk looks like this: |
| 272 | |
| 273 | |
| 274 | chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 275 | | Size of previous chunk, if allocated | | |
| 276 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 277 | | Size of chunk, in bytes |P| |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 278 | mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 279 | | User data starts here... . |
| 280 | . . |
| 281 | . (malloc_usable_space() bytes) . |
| 282 | . | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 283 | nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 284 | | Size of chunk | |
| 285 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | Where "chunk" is the front of the chunk for the purpose of most of |
| 289 | the malloc code, but "mem" is the pointer that is returned to the |
| 290 | user. "Nextchunk" is the beginning of the next contiguous chunk. |
| 291 | |
| 292 | Chunks always begin on even word boundries, so the mem portion |
| 293 | (which is returned to the user) is also on an even word boundary, and |
| 294 | thus double-word aligned. |
| 295 | |
| 296 | Free chunks are stored in circular doubly-linked lists, and look like this: |
| 297 | |
| 298 | chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 299 | | Size of previous chunk | |
| 300 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 301 | `head:' | Size of chunk, in bytes |P| |
| 302 | mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 303 | | Forward pointer to next chunk in list | |
| 304 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 305 | | Back pointer to previous chunk in list | |
| 306 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 307 | | Unused space (may be 0 bytes long) . |
| 308 | . . |
| 309 | . | |
Marek Bykowski | b4032a7 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 310 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 311 | nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 312 | `foot:' | Size of chunk, in bytes | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 313 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 314 | |
| 315 | The P (PREV_INUSE) bit, stored in the unused low-order bit of the |
| 316 | chunk size (which is always a multiple of two words), is an in-use |
| 317 | bit for the *previous* chunk. If that bit is *clear*, then the |
| 318 | word before the current chunk size contains the previous chunk |
| 319 | size, and can be used to find the front of the previous chunk. |
| 320 | (The very first chunk allocated always has this bit set, |
| 321 | preventing access to non-existent (or non-owned) memory.) |
| 322 | |
| 323 | Note that the `foot' of the current chunk is actually represented |
| 324 | as the prev_size of the NEXT chunk. (This makes it easier to |
| 325 | deal with alignments etc). |
| 326 | |
| 327 | The two exceptions to all this are |
| 328 | |
| 329 | 1. The special chunk `top', which doesn't bother using the |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 330 | trailing size field since there is no |
| 331 | next contiguous chunk that would have to index off it. (After |
| 332 | initialization, `top' is forced to always exist. If it would |
| 333 | become less than MINSIZE bytes long, it is replenished via |
| 334 | malloc_extend_top.) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 335 | |
| 336 | 2. Chunks allocated via mmap, which have the second-lowest-order |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 337 | bit (IS_MMAPPED) set in their size fields. Because they are |
| 338 | never merged or traversed from any other chunk, they have no |
| 339 | foot size or inuse information. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 340 | |
| 341 | Available chunks are kept in any of several places (all declared below): |
| 342 | |
| 343 | * `av': An array of chunks serving as bin headers for consolidated |
| 344 | chunks. Each bin is doubly linked. The bins are approximately |
| 345 | proportionally (log) spaced. There are a lot of these bins |
| 346 | (128). This may look excessive, but works very well in |
| 347 | practice. All procedures maintain the invariant that no |
| 348 | consolidated chunk physically borders another one. Chunks in |
| 349 | bins are kept in size order, with ties going to the |
| 350 | approximately least recently used chunk. |
| 351 | |
| 352 | The chunks in each bin are maintained in decreasing sorted order by |
| 353 | size. This is irrelevant for the small bins, which all contain |
| 354 | the same-sized chunks, but facilitates best-fit allocation for |
| 355 | larger chunks. (These lists are just sequential. Keeping them in |
| 356 | order almost never requires enough traversal to warrant using |
| 357 | fancier ordered data structures.) Chunks of the same size are |
| 358 | linked with the most recently freed at the front, and allocations |
| 359 | are taken from the back. This results in LRU or FIFO allocation |
| 360 | order, which tends to give each chunk an equal opportunity to be |
| 361 | consolidated with adjacent freed chunks, resulting in larger free |
| 362 | chunks and less fragmentation. |
| 363 | |
| 364 | * `top': The top-most available chunk (i.e., the one bordering the |
| 365 | end of available memory) is treated specially. It is never |
| 366 | included in any bin, is used only if no other chunk is |
| 367 | available, and is released back to the system if it is very |
| 368 | large (see M_TRIM_THRESHOLD). |
| 369 | |
| 370 | * `last_remainder': A bin holding only the remainder of the |
| 371 | most recently split (non-top) chunk. This bin is checked |
| 372 | before other non-fitting chunks, so as to provide better |
| 373 | locality for runs of sequentially allocated chunks. |
| 374 | |
| 375 | * Implicitly, through the host system's memory mapping tables. |
| 376 | If supported, requests greater than a threshold are usually |
| 377 | serviced via calls to mmap, and then later released via munmap. |
| 378 | |
| 379 | */ |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 380 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 381 | /* sizes, alignments */ |
| 382 | |
| 383 | #define SIZE_SZ (sizeof(INTERNAL_SIZE_T)) |
| 384 | #define MALLOC_ALIGNMENT (SIZE_SZ + SIZE_SZ) |
| 385 | #define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1) |
| 386 | #define MINSIZE (sizeof(struct malloc_chunk)) |
| 387 | |
| 388 | /* conversion from malloc headers to user pointers, and back */ |
| 389 | |
| 390 | #define chunk2mem(p) ((Void_t*)((char*)(p) + 2*SIZE_SZ)) |
| 391 | #define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ)) |
| 392 | |
| 393 | /* pad request bytes into a usable size */ |
| 394 | |
| 395 | #define request2size(req) \ |
| 396 | (((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \ |
| 397 | (long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \ |
| 398 | (((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK))) |
| 399 | |
| 400 | /* Check if m has acceptable alignment */ |
| 401 | |
| 402 | #define aligned_OK(m) (((unsigned long)((m)) & (MALLOC_ALIGN_MASK)) == 0) |
| 403 | |
| 404 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 405 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | Physical chunk operations |
| 409 | */ |
| 410 | |
| 411 | |
| 412 | /* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */ |
| 413 | |
| 414 | #define PREV_INUSE 0x1 |
| 415 | |
| 416 | /* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */ |
| 417 | |
| 418 | #define IS_MMAPPED 0x2 |
| 419 | |
| 420 | /* Bits to mask off when extracting size */ |
| 421 | |
| 422 | #define SIZE_BITS (PREV_INUSE|IS_MMAPPED) |
| 423 | |
| 424 | |
| 425 | /* Ptr to next physical malloc_chunk. */ |
| 426 | |
| 427 | #define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) )) |
| 428 | |
| 429 | /* Ptr to previous physical malloc_chunk */ |
| 430 | |
| 431 | #define prev_chunk(p)\ |
| 432 | ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) )) |
| 433 | |
| 434 | |
| 435 | /* Treat space at ptr + offset as a chunk */ |
| 436 | |
| 437 | #define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s))) |
| 438 | |
| 439 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 440 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | Dealing with use bits |
| 444 | */ |
| 445 | |
| 446 | /* extract p's inuse bit */ |
| 447 | |
| 448 | #define inuse(p)\ |
| 449 | ((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE) |
| 450 | |
| 451 | /* extract inuse bit of previous chunk */ |
| 452 | |
| 453 | #define prev_inuse(p) ((p)->size & PREV_INUSE) |
| 454 | |
| 455 | /* check for mmap()'ed chunk */ |
| 456 | |
| 457 | #define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED) |
| 458 | |
| 459 | /* set/clear chunk as in use without otherwise disturbing */ |
| 460 | |
| 461 | #define set_inuse(p)\ |
| 462 | ((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE |
| 463 | |
| 464 | #define clear_inuse(p)\ |
| 465 | ((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE) |
| 466 | |
| 467 | /* check/set/clear inuse bits in known places */ |
| 468 | |
| 469 | #define inuse_bit_at_offset(p, s)\ |
| 470 | (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE) |
| 471 | |
| 472 | #define set_inuse_bit_at_offset(p, s)\ |
| 473 | (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE) |
| 474 | |
| 475 | #define clear_inuse_bit_at_offset(p, s)\ |
| 476 | (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE)) |
| 477 | |
| 478 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 479 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | Dealing with size fields |
| 483 | */ |
| 484 | |
| 485 | /* Get size, ignoring use bits */ |
| 486 | |
| 487 | #define chunksize(p) ((p)->size & ~(SIZE_BITS)) |
| 488 | |
| 489 | /* Set size at head, without disturbing its use bit */ |
| 490 | |
| 491 | #define set_head_size(p, s) ((p)->size = (((p)->size & PREV_INUSE) | (s))) |
| 492 | |
| 493 | /* Set size/use ignoring previous bits in header */ |
| 494 | |
| 495 | #define set_head(p, s) ((p)->size = (s)) |
| 496 | |
| 497 | /* Set size at footer (only when chunk is not in use) */ |
| 498 | |
| 499 | #define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s)) |
| 500 | |
| 501 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 502 | |
| 503 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 504 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 505 | /* |
| 506 | Bins |
| 507 | |
| 508 | The bins, `av_' are an array of pairs of pointers serving as the |
| 509 | heads of (initially empty) doubly-linked lists of chunks, laid out |
| 510 | in a way so that each pair can be treated as if it were in a |
| 511 | malloc_chunk. (This way, the fd/bk offsets for linking bin heads |
| 512 | and chunks are the same). |
| 513 | |
| 514 | Bins for sizes < 512 bytes contain chunks of all the same size, spaced |
| 515 | 8 bytes apart. Larger bins are approximately logarithmically |
| 516 | spaced. (See the table below.) The `av_' array is never mentioned |
| 517 | directly in the code, but instead via bin access macros. |
| 518 | |
| 519 | Bin layout: |
| 520 | |
| 521 | 64 bins of size 8 |
| 522 | 32 bins of size 64 |
| 523 | 16 bins of size 512 |
| 524 | 8 bins of size 4096 |
| 525 | 4 bins of size 32768 |
| 526 | 2 bins of size 262144 |
| 527 | 1 bin of size what's left |
| 528 | |
| 529 | There is actually a little bit of slop in the numbers in bin_index |
| 530 | for the sake of speed. This makes no difference elsewhere. |
| 531 | |
| 532 | The special chunks `top' and `last_remainder' get their own bins, |
| 533 | (this is implemented via yet more trickery with the av_ array), |
| 534 | although `top' is never properly linked to its bin since it is |
| 535 | always handled specially. |
| 536 | |
| 537 | */ |
| 538 | |
| 539 | #define NAV 128 /* number of bins */ |
| 540 | |
| 541 | typedef struct malloc_chunk* mbinptr; |
| 542 | |
| 543 | /* access macros */ |
| 544 | |
| 545 | #define bin_at(i) ((mbinptr)((char*)&(av_[2*(i) + 2]) - 2*SIZE_SZ)) |
| 546 | #define next_bin(b) ((mbinptr)((char*)(b) + 2 * sizeof(mbinptr))) |
| 547 | #define prev_bin(b) ((mbinptr)((char*)(b) - 2 * sizeof(mbinptr))) |
| 548 | |
| 549 | /* |
| 550 | The first 2 bins are never indexed. The corresponding av_ cells are instead |
| 551 | used for bookkeeping. This is not to save space, but to simplify |
| 552 | indexing, maintain locality, and avoid some initialization tests. |
| 553 | */ |
| 554 | |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 555 | #define top (av_[2]) /* The topmost chunk */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 556 | #define last_remainder (bin_at(1)) /* remainder from last split */ |
| 557 | |
| 558 | |
| 559 | /* |
| 560 | Because top initially points to its own bin with initial |
| 561 | zero size, thus forcing extension on the first malloc request, |
| 562 | we avoid having any special code in malloc to check whether |
| 563 | it even exists yet. But we still need to in malloc_extend_top. |
| 564 | */ |
| 565 | |
| 566 | #define initial_top ((mchunkptr)(bin_at(0))) |
| 567 | |
| 568 | /* Helper macro to initialize bins */ |
| 569 | |
| 570 | #define IAV(i) bin_at(i), bin_at(i) |
| 571 | |
| 572 | static mbinptr av_[NAV * 2 + 2] = { |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 573 | NULL, NULL, |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 574 | IAV(0), IAV(1), IAV(2), IAV(3), IAV(4), IAV(5), IAV(6), IAV(7), |
| 575 | IAV(8), IAV(9), IAV(10), IAV(11), IAV(12), IAV(13), IAV(14), IAV(15), |
| 576 | IAV(16), IAV(17), IAV(18), IAV(19), IAV(20), IAV(21), IAV(22), IAV(23), |
| 577 | IAV(24), IAV(25), IAV(26), IAV(27), IAV(28), IAV(29), IAV(30), IAV(31), |
| 578 | IAV(32), IAV(33), IAV(34), IAV(35), IAV(36), IAV(37), IAV(38), IAV(39), |
| 579 | IAV(40), IAV(41), IAV(42), IAV(43), IAV(44), IAV(45), IAV(46), IAV(47), |
| 580 | IAV(48), IAV(49), IAV(50), IAV(51), IAV(52), IAV(53), IAV(54), IAV(55), |
| 581 | IAV(56), IAV(57), IAV(58), IAV(59), IAV(60), IAV(61), IAV(62), IAV(63), |
| 582 | IAV(64), IAV(65), IAV(66), IAV(67), IAV(68), IAV(69), IAV(70), IAV(71), |
| 583 | IAV(72), IAV(73), IAV(74), IAV(75), IAV(76), IAV(77), IAV(78), IAV(79), |
| 584 | IAV(80), IAV(81), IAV(82), IAV(83), IAV(84), IAV(85), IAV(86), IAV(87), |
| 585 | IAV(88), IAV(89), IAV(90), IAV(91), IAV(92), IAV(93), IAV(94), IAV(95), |
| 586 | IAV(96), IAV(97), IAV(98), IAV(99), IAV(100), IAV(101), IAV(102), IAV(103), |
| 587 | IAV(104), IAV(105), IAV(106), IAV(107), IAV(108), IAV(109), IAV(110), IAV(111), |
| 588 | IAV(112), IAV(113), IAV(114), IAV(115), IAV(116), IAV(117), IAV(118), IAV(119), |
| 589 | IAV(120), IAV(121), IAV(122), IAV(123), IAV(124), IAV(125), IAV(126), IAV(127) |
| 590 | }; |
| 591 | |
Marek Bykowski | b4032a7 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 592 | #ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT |
| 593 | static void malloc_init(void); |
| 594 | #endif |
| 595 | |
Peter Tyser | a78ded6 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 596 | ulong mem_malloc_start = 0; |
| 597 | ulong mem_malloc_end = 0; |
| 598 | ulong mem_malloc_brk = 0; |
| 599 | |
Simon Glass | 1a3e39b | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 600 | static bool malloc_testing; /* enable test mode */ |
| 601 | static int malloc_max_allocs; /* return NULL after this many calls to malloc() */ |
| 602 | |
Peter Tyser | a78ded6 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 603 | void *sbrk(ptrdiff_t increment) |
| 604 | { |
| 605 | ulong old = mem_malloc_brk; |
| 606 | ulong new = old + increment; |
| 607 | |
Kumar Gala | 293d7ad | 2010-11-15 18:41:43 -0600 | [diff] [blame] | 608 | /* |
| 609 | * if we are giving memory back make sure we clear it out since |
| 610 | * we set MORECORE_CLEARS to 1 |
| 611 | */ |
| 612 | if (increment < 0) |
| 613 | memset((void *)new, 0, -increment); |
| 614 | |
Peter Tyser | a78ded6 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 615 | if ((new < mem_malloc_start) || (new > mem_malloc_end)) |
karl.beldan@gmail.com | 34e5088 | 2010-04-06 22:18:08 +0200 | [diff] [blame] | 616 | return (void *)MORECORE_FAILURE; |
Peter Tyser | a78ded6 | 2009-08-21 23:05:19 -0500 | [diff] [blame] | 617 | |
| 618 | mem_malloc_brk = new; |
| 619 | |
| 620 | return (void *)old; |
| 621 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 622 | |
Peter Tyser | 781c9b8 | 2009-08-21 23:05:21 -0500 | [diff] [blame] | 623 | void mem_malloc_init(ulong start, ulong size) |
| 624 | { |
| 625 | mem_malloc_start = start; |
| 626 | mem_malloc_end = start + size; |
| 627 | mem_malloc_brk = start; |
| 628 | |
Marek Bykowski | b4032a7 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 629 | #ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT |
| 630 | malloc_init(); |
| 631 | #endif |
| 632 | |
Thierry Reding | 8023ec2 | 2014-08-26 17:34:22 +0200 | [diff] [blame] | 633 | debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start, |
| 634 | mem_malloc_end); |
Shengyu Qu | 10a3e61 | 2023-08-25 00:25:19 +0800 | [diff] [blame] | 635 | #if CONFIG_IS_ENABLED(SYS_MALLOC_CLEAR_ON_INIT) |
Przemyslaw Marczak | 8843678 | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 636 | memset((void *)mem_malloc_start, 0x0, size); |
| 637 | #endif |
Peter Tyser | 781c9b8 | 2009-08-21 23:05:21 -0500 | [diff] [blame] | 638 | } |
Peter Tyser | 781c9b8 | 2009-08-21 23:05:21 -0500 | [diff] [blame] | 639 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 640 | /* field-extraction macros */ |
| 641 | |
| 642 | #define first(b) ((b)->fd) |
| 643 | #define last(b) ((b)->bk) |
| 644 | |
| 645 | /* |
| 646 | Indexing into bins |
| 647 | */ |
| 648 | |
| 649 | #define bin_index(sz) \ |
| 650 | (((((unsigned long)(sz)) >> 9) == 0) ? (((unsigned long)(sz)) >> 3): \ |
| 651 | ((((unsigned long)(sz)) >> 9) <= 4) ? 56 + (((unsigned long)(sz)) >> 6): \ |
| 652 | ((((unsigned long)(sz)) >> 9) <= 20) ? 91 + (((unsigned long)(sz)) >> 9): \ |
| 653 | ((((unsigned long)(sz)) >> 9) <= 84) ? 110 + (((unsigned long)(sz)) >> 12): \ |
| 654 | ((((unsigned long)(sz)) >> 9) <= 340) ? 119 + (((unsigned long)(sz)) >> 15): \ |
| 655 | ((((unsigned long)(sz)) >> 9) <= 1364) ? 124 + (((unsigned long)(sz)) >> 18): \ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 656 | 126) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 657 | /* |
| 658 | bins for chunks < 512 are all spaced 8 bytes apart, and hold |
| 659 | identically sized chunks. This is exploited in malloc. |
| 660 | */ |
| 661 | |
| 662 | #define MAX_SMALLBIN 63 |
| 663 | #define MAX_SMALLBIN_SIZE 512 |
| 664 | #define SMALLBIN_WIDTH 8 |
| 665 | |
| 666 | #define smallbin_index(sz) (((unsigned long)(sz)) >> 3) |
| 667 | |
| 668 | /* |
| 669 | Requests are `small' if both the corresponding and the next bin are small |
| 670 | */ |
| 671 | |
| 672 | #define is_small_request(nb) (nb < MAX_SMALLBIN_SIZE - SMALLBIN_WIDTH) |
| 673 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 674 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 675 | |
| 676 | /* |
| 677 | To help compensate for the large number of bins, a one-level index |
| 678 | structure is used for bin-by-bin searching. `binblocks' is a |
| 679 | one-word bitvector recording whether groups of BINBLOCKWIDTH bins |
| 680 | have any (possibly) non-empty bins, so they can be skipped over |
| 681 | all at once during during traversals. The bits are NOT always |
| 682 | cleared as soon as all bins in a block are empty, but instead only |
| 683 | when all are noticed to be empty during traversal in malloc. |
| 684 | */ |
| 685 | |
| 686 | #define BINBLOCKWIDTH 4 /* bins per block */ |
| 687 | |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 688 | #define binblocks_r ((INTERNAL_SIZE_T)av_[1]) /* bitvector of nonempty blocks */ |
| 689 | #define binblocks_w (av_[1]) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 690 | |
| 691 | /* bin<->block macros */ |
| 692 | |
| 693 | #define idx2binblock(ix) ((unsigned)1 << (ix / BINBLOCKWIDTH)) |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 694 | #define mark_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r | idx2binblock(ii))) |
| 695 | #define clear_binblock(ii) (binblocks_w = (mbinptr)(binblocks_r & ~(idx2binblock(ii)))) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 696 | |
| 697 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 698 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 699 | |
| 700 | |
| 701 | /* Other static bookkeeping data */ |
| 702 | |
| 703 | /* variables holding tunable values */ |
| 704 | |
| 705 | static unsigned long trim_threshold = DEFAULT_TRIM_THRESHOLD; |
| 706 | static unsigned long top_pad = DEFAULT_TOP_PAD; |
| 707 | static unsigned int n_mmaps_max = DEFAULT_MMAP_MAX; |
| 708 | static unsigned long mmap_threshold = DEFAULT_MMAP_THRESHOLD; |
| 709 | |
| 710 | /* The first value returned from sbrk */ |
| 711 | static char* sbrk_base = (char*)(-1); |
| 712 | |
| 713 | /* The maximum memory obtained from system via sbrk */ |
| 714 | static unsigned long max_sbrked_mem = 0; |
| 715 | |
| 716 | /* The maximum via either sbrk or mmap */ |
| 717 | static unsigned long max_total_mem = 0; |
| 718 | |
| 719 | /* internal working copy of mallinfo */ |
| 720 | static struct mallinfo current_mallinfo = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 721 | |
| 722 | /* The total memory obtained from system via sbrk */ |
| 723 | #define sbrked_mem (current_mallinfo.arena) |
| 724 | |
| 725 | /* Tracking mmaps */ |
| 726 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 727 | #ifdef DEBUG |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 728 | static unsigned int n_mmaps = 0; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 729 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 730 | static unsigned long mmapped_mem = 0; |
| 731 | #if HAVE_MMAP |
| 732 | static unsigned int max_n_mmaps = 0; |
| 733 | static unsigned long max_mmapped_mem = 0; |
| 734 | #endif |
| 735 | |
Marek Bykowski | b4032a7 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 736 | #ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT |
| 737 | static void malloc_init(void) |
| 738 | { |
| 739 | int i, j; |
| 740 | |
| 741 | debug("bins (av_ array) are at %p\n", (void *)av_); |
| 742 | |
| 743 | av_[0] = NULL; av_[1] = NULL; |
| 744 | for (i = 2, j = 2; i < NAV * 2 + 2; i += 2, j++) { |
| 745 | av_[i] = bin_at(j - 2); |
| 746 | av_[i + 1] = bin_at(j - 2); |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 747 | |
Marek Bykowski | b4032a7 | 2020-04-29 18:23:07 +0200 | [diff] [blame] | 748 | /* Just print the first few bins so that |
| 749 | * we can see there are alright. |
| 750 | */ |
| 751 | if (i < 10) |
| 752 | debug("av_[%d]=%lx av_[%d]=%lx\n", |
| 753 | i, (ulong)av_[i], |
| 754 | i + 1, (ulong)av_[i + 1]); |
| 755 | } |
| 756 | |
| 757 | /* Init the static bookkeeping as well */ |
| 758 | sbrk_base = (char *)(-1); |
| 759 | max_sbrked_mem = 0; |
| 760 | max_total_mem = 0; |
| 761 | #ifdef DEBUG |
| 762 | memset((void *)¤t_mallinfo, 0, sizeof(struct mallinfo)); |
| 763 | #endif |
| 764 | } |
| 765 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 766 | |
| 767 | /* |
| 768 | Debugging support |
| 769 | */ |
| 770 | |
| 771 | #ifdef DEBUG |
| 772 | |
| 773 | |
| 774 | /* |
| 775 | These routines make a number of assertions about the states |
| 776 | of data structures that should be true at all times. If any |
| 777 | are not true, it's very likely that a user program has somehow |
| 778 | trashed memory. (It's also possible that there is a coding error |
| 779 | in malloc. In which case, please report it!) |
| 780 | */ |
| 781 | |
| 782 | #if __STD_C |
| 783 | static void do_check_chunk(mchunkptr p) |
| 784 | #else |
| 785 | static void do_check_chunk(p) mchunkptr p; |
| 786 | #endif |
| 787 | { |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 788 | INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 789 | |
| 790 | /* No checkable chunk is mmapped */ |
| 791 | assert(!chunk_is_mmapped(p)); |
| 792 | |
| 793 | /* Check for legal address ... */ |
| 794 | assert((char*)p >= sbrk_base); |
| 795 | if (p != top) |
| 796 | assert((char*)p + sz <= (char*)top); |
| 797 | else |
| 798 | assert((char*)p + sz <= sbrk_base + sbrked_mem); |
| 799 | |
| 800 | } |
| 801 | |
| 802 | |
| 803 | #if __STD_C |
| 804 | static void do_check_free_chunk(mchunkptr p) |
| 805 | #else |
| 806 | static void do_check_free_chunk(p) mchunkptr p; |
| 807 | #endif |
| 808 | { |
| 809 | INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 810 | mchunkptr next = chunk_at_offset(p, sz); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 811 | |
| 812 | do_check_chunk(p); |
| 813 | |
| 814 | /* Check whether it claims to be free ... */ |
| 815 | assert(!inuse(p)); |
| 816 | |
| 817 | /* Unless a special marker, must have OK fields */ |
| 818 | if ((long)sz >= (long)MINSIZE) |
| 819 | { |
| 820 | assert((sz & MALLOC_ALIGN_MASK) == 0); |
| 821 | assert(aligned_OK(chunk2mem(p))); |
| 822 | /* ... matching footer field */ |
| 823 | assert(next->prev_size == sz); |
| 824 | /* ... and is fully consolidated */ |
| 825 | assert(prev_inuse(p)); |
| 826 | assert (next == top || inuse(next)); |
| 827 | |
| 828 | /* ... and has minimally sane links */ |
| 829 | assert(p->fd->bk == p); |
| 830 | assert(p->bk->fd == p); |
| 831 | } |
| 832 | else /* markers are always of size SIZE_SZ */ |
| 833 | assert(sz == SIZE_SZ); |
| 834 | } |
| 835 | |
| 836 | #if __STD_C |
| 837 | static void do_check_inuse_chunk(mchunkptr p) |
| 838 | #else |
| 839 | static void do_check_inuse_chunk(p) mchunkptr p; |
| 840 | #endif |
| 841 | { |
| 842 | mchunkptr next = next_chunk(p); |
| 843 | do_check_chunk(p); |
| 844 | |
| 845 | /* Check whether it claims to be in use ... */ |
| 846 | assert(inuse(p)); |
| 847 | |
| 848 | /* ... and is surrounded by OK chunks. |
| 849 | Since more things can be checked with free chunks than inuse ones, |
| 850 | if an inuse chunk borders them and debug is on, it's worth doing them. |
| 851 | */ |
| 852 | if (!prev_inuse(p)) |
| 853 | { |
| 854 | mchunkptr prv = prev_chunk(p); |
| 855 | assert(next_chunk(prv) == p); |
| 856 | do_check_free_chunk(prv); |
| 857 | } |
| 858 | if (next == top) |
| 859 | { |
| 860 | assert(prev_inuse(next)); |
| 861 | assert(chunksize(next) >= MINSIZE); |
| 862 | } |
| 863 | else if (!inuse(next)) |
| 864 | do_check_free_chunk(next); |
| 865 | |
| 866 | } |
| 867 | |
| 868 | #if __STD_C |
| 869 | static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s) |
| 870 | #else |
| 871 | static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s; |
| 872 | #endif |
| 873 | { |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 874 | INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; |
| 875 | long room = sz - s; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 876 | |
| 877 | do_check_inuse_chunk(p); |
| 878 | |
| 879 | /* Legal size ... */ |
| 880 | assert((long)sz >= (long)MINSIZE); |
| 881 | assert((sz & MALLOC_ALIGN_MASK) == 0); |
| 882 | assert(room >= 0); |
| 883 | assert(room < (long)MINSIZE); |
| 884 | |
| 885 | /* ... and alignment */ |
| 886 | assert(aligned_OK(chunk2mem(p))); |
| 887 | |
| 888 | |
| 889 | /* ... and was allocated at front of an available chunk */ |
| 890 | assert(prev_inuse(p)); |
| 891 | |
| 892 | } |
| 893 | |
| 894 | |
| 895 | #define check_free_chunk(P) do_check_free_chunk(P) |
| 896 | #define check_inuse_chunk(P) do_check_inuse_chunk(P) |
| 897 | #define check_chunk(P) do_check_chunk(P) |
| 898 | #define check_malloced_chunk(P,N) do_check_malloced_chunk(P,N) |
| 899 | #else |
| 900 | #define check_free_chunk(P) |
| 901 | #define check_inuse_chunk(P) |
| 902 | #define check_chunk(P) |
| 903 | #define check_malloced_chunk(P,N) |
| 904 | #endif |
| 905 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 906 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | Macro-based internal utilities |
| 910 | */ |
| 911 | |
| 912 | |
| 913 | /* |
| 914 | Linking chunks in bin lists. |
| 915 | Call these only with variables, not arbitrary expressions, as arguments. |
| 916 | */ |
| 917 | |
| 918 | /* |
| 919 | Place chunk p of size s in its bin, in size order, |
| 920 | putting it ahead of others of same size. |
| 921 | */ |
| 922 | |
| 923 | |
| 924 | #define frontlink(P, S, IDX, BK, FD) \ |
| 925 | { \ |
| 926 | if (S < MAX_SMALLBIN_SIZE) \ |
| 927 | { \ |
| 928 | IDX = smallbin_index(S); \ |
| 929 | mark_binblock(IDX); \ |
| 930 | BK = bin_at(IDX); \ |
| 931 | FD = BK->fd; \ |
| 932 | P->bk = BK; \ |
| 933 | P->fd = FD; \ |
| 934 | FD->bk = BK->fd = P; \ |
| 935 | } \ |
| 936 | else \ |
| 937 | { \ |
| 938 | IDX = bin_index(S); \ |
| 939 | BK = bin_at(IDX); \ |
| 940 | FD = BK->fd; \ |
| 941 | if (FD == BK) mark_binblock(IDX); \ |
| 942 | else \ |
| 943 | { \ |
| 944 | while (FD != BK && S < chunksize(FD)) FD = FD->fd; \ |
| 945 | BK = FD->bk; \ |
| 946 | } \ |
| 947 | P->bk = BK; \ |
| 948 | P->fd = FD; \ |
| 949 | FD->bk = BK->fd = P; \ |
| 950 | } \ |
| 951 | } |
| 952 | |
| 953 | |
| 954 | /* take a chunk off a list */ |
| 955 | |
| 956 | #define unlink(P, BK, FD) \ |
| 957 | { \ |
| 958 | BK = P->bk; \ |
| 959 | FD = P->fd; \ |
| 960 | FD->bk = BK; \ |
| 961 | BK->fd = FD; \ |
| 962 | } \ |
| 963 | |
| 964 | /* Place p as the last remainder */ |
| 965 | |
| 966 | #define link_last_remainder(P) \ |
| 967 | { \ |
| 968 | last_remainder->fd = last_remainder->bk = P; \ |
| 969 | P->fd = P->bk = last_remainder; \ |
| 970 | } |
| 971 | |
| 972 | /* Clear the last_remainder bin */ |
| 973 | |
| 974 | #define clear_last_remainder \ |
| 975 | (last_remainder->fd = last_remainder->bk = last_remainder) |
| 976 | |
| 977 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 978 | |
| 979 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 980 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 981 | /* Routines dealing with mmap(). */ |
| 982 | |
| 983 | #if HAVE_MMAP |
| 984 | |
| 985 | #if __STD_C |
| 986 | static mchunkptr mmap_chunk(size_t size) |
| 987 | #else |
| 988 | static mchunkptr mmap_chunk(size) size_t size; |
| 989 | #endif |
| 990 | { |
| 991 | size_t page_mask = malloc_getpagesize - 1; |
| 992 | mchunkptr p; |
| 993 | |
| 994 | #ifndef MAP_ANONYMOUS |
| 995 | static int fd = -1; |
| 996 | #endif |
| 997 | |
| 998 | if(n_mmaps >= n_mmaps_max) return 0; /* too many regions */ |
| 999 | |
| 1000 | /* For mmapped chunks, the overhead is one SIZE_SZ unit larger, because |
| 1001 | * there is no following chunk whose prev_size field could be used. |
| 1002 | */ |
| 1003 | size = (size + SIZE_SZ + page_mask) & ~page_mask; |
| 1004 | |
| 1005 | #ifdef MAP_ANONYMOUS |
| 1006 | p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE, |
| 1007 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 1008 | #else /* !MAP_ANONYMOUS */ |
| 1009 | if (fd < 0) |
| 1010 | { |
| 1011 | fd = open("/dev/zero", O_RDWR); |
| 1012 | if(fd < 0) return 0; |
| 1013 | } |
| 1014 | p = (mchunkptr)mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 1015 | #endif |
| 1016 | |
| 1017 | if(p == (mchunkptr)-1) return 0; |
| 1018 | |
| 1019 | n_mmaps++; |
| 1020 | if (n_mmaps > max_n_mmaps) max_n_mmaps = n_mmaps; |
| 1021 | |
| 1022 | /* We demand that eight bytes into a page must be 8-byte aligned. */ |
| 1023 | assert(aligned_OK(chunk2mem(p))); |
| 1024 | |
| 1025 | /* The offset to the start of the mmapped region is stored |
| 1026 | * in the prev_size field of the chunk; normally it is zero, |
| 1027 | * but that can be changed in memalign(). |
| 1028 | */ |
| 1029 | p->prev_size = 0; |
| 1030 | set_head(p, size|IS_MMAPPED); |
| 1031 | |
| 1032 | mmapped_mem += size; |
| 1033 | if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem) |
| 1034 | max_mmapped_mem = mmapped_mem; |
| 1035 | if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) |
| 1036 | max_total_mem = mmapped_mem + sbrked_mem; |
| 1037 | return p; |
| 1038 | } |
| 1039 | |
| 1040 | #if __STD_C |
| 1041 | static void munmap_chunk(mchunkptr p) |
| 1042 | #else |
| 1043 | static void munmap_chunk(p) mchunkptr p; |
| 1044 | #endif |
| 1045 | { |
| 1046 | INTERNAL_SIZE_T size = chunksize(p); |
| 1047 | int ret; |
| 1048 | |
| 1049 | assert (chunk_is_mmapped(p)); |
| 1050 | assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem)); |
| 1051 | assert((n_mmaps > 0)); |
| 1052 | assert(((p->prev_size + size) & (malloc_getpagesize-1)) == 0); |
| 1053 | |
| 1054 | n_mmaps--; |
| 1055 | mmapped_mem -= (size + p->prev_size); |
| 1056 | |
| 1057 | ret = munmap((char *)p - p->prev_size, size + p->prev_size); |
| 1058 | |
| 1059 | /* munmap returns non-zero on failure */ |
| 1060 | assert(ret == 0); |
| 1061 | } |
| 1062 | |
| 1063 | #if HAVE_MREMAP |
| 1064 | |
| 1065 | #if __STD_C |
| 1066 | static mchunkptr mremap_chunk(mchunkptr p, size_t new_size) |
| 1067 | #else |
| 1068 | static mchunkptr mremap_chunk(p, new_size) mchunkptr p; size_t new_size; |
| 1069 | #endif |
| 1070 | { |
| 1071 | size_t page_mask = malloc_getpagesize - 1; |
| 1072 | INTERNAL_SIZE_T offset = p->prev_size; |
| 1073 | INTERNAL_SIZE_T size = chunksize(p); |
| 1074 | char *cp; |
| 1075 | |
| 1076 | assert (chunk_is_mmapped(p)); |
| 1077 | assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem)); |
| 1078 | assert((n_mmaps > 0)); |
| 1079 | assert(((size + offset) & (malloc_getpagesize-1)) == 0); |
| 1080 | |
| 1081 | /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */ |
| 1082 | new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask; |
| 1083 | |
| 1084 | cp = (char *)mremap((char *)p - offset, size + offset, new_size, 1); |
| 1085 | |
| 1086 | if (cp == (char *)-1) return 0; |
| 1087 | |
| 1088 | p = (mchunkptr)(cp + offset); |
| 1089 | |
| 1090 | assert(aligned_OK(chunk2mem(p))); |
| 1091 | |
| 1092 | assert((p->prev_size == offset)); |
| 1093 | set_head(p, (new_size - offset)|IS_MMAPPED); |
| 1094 | |
| 1095 | mmapped_mem -= size + offset; |
| 1096 | mmapped_mem += new_size; |
| 1097 | if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem) |
| 1098 | max_mmapped_mem = mmapped_mem; |
| 1099 | if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) |
| 1100 | max_total_mem = mmapped_mem + sbrked_mem; |
| 1101 | return p; |
| 1102 | } |
| 1103 | |
| 1104 | #endif /* HAVE_MREMAP */ |
| 1105 | |
| 1106 | #endif /* HAVE_MMAP */ |
| 1107 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1108 | /* |
| 1109 | Extend the top-most chunk by obtaining memory from system. |
| 1110 | Main interface to sbrk (but see also malloc_trim). |
| 1111 | */ |
| 1112 | |
| 1113 | #if __STD_C |
| 1114 | static void malloc_extend_top(INTERNAL_SIZE_T nb) |
| 1115 | #else |
| 1116 | static void malloc_extend_top(nb) INTERNAL_SIZE_T nb; |
| 1117 | #endif |
| 1118 | { |
| 1119 | char* brk; /* return value from sbrk */ |
| 1120 | INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of sbrked space */ |
| 1121 | INTERNAL_SIZE_T correction; /* bytes for 2nd sbrk call */ |
| 1122 | char* new_brk; /* return of 2nd sbrk call */ |
| 1123 | INTERNAL_SIZE_T top_size; /* new size of top chunk */ |
| 1124 | |
| 1125 | mchunkptr old_top = top; /* Record state of old top */ |
| 1126 | INTERNAL_SIZE_T old_top_size = chunksize(old_top); |
| 1127 | char* old_end = (char*)(chunk_at_offset(old_top, old_top_size)); |
| 1128 | |
| 1129 | /* Pad request with top_pad plus minimal overhead */ |
| 1130 | |
| 1131 | INTERNAL_SIZE_T sbrk_size = nb + top_pad + MINSIZE; |
| 1132 | unsigned long pagesz = malloc_getpagesize; |
| 1133 | |
| 1134 | /* If not the first time through, round to preserve page boundary */ |
| 1135 | /* Otherwise, we need to correct to a page size below anyway. */ |
| 1136 | /* (We also correct below if an intervening foreign sbrk call.) */ |
| 1137 | |
| 1138 | if (sbrk_base != (char*)(-1)) |
| 1139 | sbrk_size = (sbrk_size + (pagesz - 1)) & ~(pagesz - 1); |
| 1140 | |
| 1141 | brk = (char*)(MORECORE (sbrk_size)); |
| 1142 | |
| 1143 | /* Fail if sbrk failed or if a foreign sbrk call killed our space */ |
| 1144 | if (brk == (char*)(MORECORE_FAILURE) || |
| 1145 | (brk < old_end && old_top != initial_top)) |
| 1146 | return; |
| 1147 | |
| 1148 | sbrked_mem += sbrk_size; |
| 1149 | |
| 1150 | if (brk == old_end) /* can just add bytes to current top */ |
| 1151 | { |
| 1152 | top_size = sbrk_size + old_top_size; |
| 1153 | set_head(top, top_size | PREV_INUSE); |
| 1154 | } |
| 1155 | else |
| 1156 | { |
| 1157 | if (sbrk_base == (char*)(-1)) /* First time through. Record base */ |
| 1158 | sbrk_base = brk; |
| 1159 | else /* Someone else called sbrk(). Count those bytes as sbrked_mem. */ |
| 1160 | sbrked_mem += brk - (char*)old_end; |
| 1161 | |
| 1162 | /* Guarantee alignment of first new chunk made from this space */ |
| 1163 | front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK; |
| 1164 | if (front_misalign > 0) |
| 1165 | { |
| 1166 | correction = (MALLOC_ALIGNMENT) - front_misalign; |
| 1167 | brk += correction; |
| 1168 | } |
| 1169 | else |
| 1170 | correction = 0; |
| 1171 | |
| 1172 | /* Guarantee the next brk will be at a page boundary */ |
| 1173 | |
| 1174 | correction += ((((unsigned long)(brk + sbrk_size))+(pagesz-1)) & |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1175 | ~(pagesz - 1)) - ((unsigned long)(brk + sbrk_size)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1176 | |
| 1177 | /* Allocate correction */ |
| 1178 | new_brk = (char*)(MORECORE (correction)); |
| 1179 | if (new_brk == (char*)(MORECORE_FAILURE)) return; |
| 1180 | |
| 1181 | sbrked_mem += correction; |
| 1182 | |
| 1183 | top = (mchunkptr)brk; |
| 1184 | top_size = new_brk - brk + correction; |
| 1185 | set_head(top, top_size | PREV_INUSE); |
| 1186 | |
| 1187 | if (old_top != initial_top) |
| 1188 | { |
| 1189 | |
| 1190 | /* There must have been an intervening foreign sbrk call. */ |
| 1191 | /* A double fencepost is necessary to prevent consolidation */ |
| 1192 | |
| 1193 | /* If not enough space to do this, then user did something very wrong */ |
| 1194 | if (old_top_size < MINSIZE) |
| 1195 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1196 | set_head(top, PREV_INUSE); /* will force null return from malloc */ |
| 1197 | return; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | /* Also keep size a multiple of MALLOC_ALIGNMENT */ |
| 1201 | old_top_size = (old_top_size - 3*SIZE_SZ) & ~MALLOC_ALIGN_MASK; |
| 1202 | set_head_size(old_top, old_top_size); |
| 1203 | chunk_at_offset(old_top, old_top_size )->size = |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1204 | SIZE_SZ|PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1205 | chunk_at_offset(old_top, old_top_size + SIZE_SZ)->size = |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1206 | SIZE_SZ|PREV_INUSE; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1207 | /* If possible, release the rest. */ |
| 1208 | if (old_top_size >= MINSIZE) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1209 | fREe(chunk2mem(old_top)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem) |
| 1214 | max_sbrked_mem = sbrked_mem; |
| 1215 | if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) |
| 1216 | max_total_mem = mmapped_mem + sbrked_mem; |
| 1217 | |
| 1218 | /* We always land on a page boundary */ |
| 1219 | assert(((unsigned long)((char*)top + top_size) & (pagesz - 1)) == 0); |
| 1220 | } |
| 1221 | |
| 1222 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 1223 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1224 | |
| 1225 | /* Main public routines */ |
| 1226 | |
| 1227 | |
| 1228 | /* |
| 1229 | Malloc Algorthim: |
| 1230 | |
| 1231 | The requested size is first converted into a usable form, `nb'. |
| 1232 | This currently means to add 4 bytes overhead plus possibly more to |
| 1233 | obtain 8-byte alignment and/or to obtain a size of at least |
| 1234 | MINSIZE (currently 16 bytes), the smallest allocatable size. |
| 1235 | (All fits are considered `exact' if they are within MINSIZE bytes.) |
| 1236 | |
| 1237 | From there, the first successful of the following steps is taken: |
| 1238 | |
| 1239 | 1. The bin corresponding to the request size is scanned, and if |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1240 | a chunk of exactly the right size is found, it is taken. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1241 | |
| 1242 | 2. The most recently remaindered chunk is used if it is big |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1243 | enough. This is a form of (roving) first fit, used only in |
| 1244 | the absence of exact fits. Runs of consecutive requests use |
| 1245 | the remainder of the chunk used for the previous such request |
| 1246 | whenever possible. This limited use of a first-fit style |
| 1247 | allocation strategy tends to give contiguous chunks |
| 1248 | coextensive lifetimes, which improves locality and can reduce |
| 1249 | fragmentation in the long run. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1250 | |
| 1251 | 3. Other bins are scanned in increasing size order, using a |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1252 | chunk big enough to fulfill the request, and splitting off |
| 1253 | any remainder. This search is strictly by best-fit; i.e., |
| 1254 | the smallest (with ties going to approximately the least |
| 1255 | recently used) chunk that fits is selected. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1256 | |
| 1257 | 4. If large enough, the chunk bordering the end of memory |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1258 | (`top') is split off. (This use of `top' is in accord with |
| 1259 | the best-fit search rule. In effect, `top' is treated as |
| 1260 | larger (and thus less well fitting) than any other available |
| 1261 | chunk since it can be extended to be as large as necessary |
| 1262 | (up to system limitations). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1263 | |
| 1264 | 5. If the request size meets the mmap threshold and the |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1265 | system supports mmap, and there are few enough currently |
| 1266 | allocated mmapped regions, and a call to mmap succeeds, |
| 1267 | the request is allocated via direct memory mapping. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1268 | |
| 1269 | 6. Otherwise, the top of memory is extended by |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1270 | obtaining more space from the system (normally using sbrk, |
| 1271 | but definable to anything else via the MORECORE macro). |
| 1272 | Memory is gathered from the system (in system page-sized |
| 1273 | units) in a way that allows chunks obtained across different |
| 1274 | sbrk calls to be consolidated, but does not require |
| 1275 | contiguous memory. Thus, it should be safe to intersperse |
| 1276 | mallocs with other sbrk calls. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1277 | |
| 1278 | |
| 1279 | All allocations are made from the the `lowest' part of any found |
| 1280 | chunk. (The implementation invariant is that prev_inuse is |
| 1281 | always true of any allocated chunk; i.e., that each allocated |
| 1282 | chunk borders either a previously allocated and still in-use chunk, |
| 1283 | or the base of its memory arena.) |
| 1284 | |
| 1285 | */ |
| 1286 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1287 | STATIC_IF_MCHECK |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1288 | #if __STD_C |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1289 | Void_t* mALLOc_impl(size_t bytes) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1290 | #else |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1291 | Void_t* mALLOc_impl(bytes) size_t bytes; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1292 | #endif |
| 1293 | { |
| 1294 | mchunkptr victim; /* inspected/selected chunk */ |
| 1295 | INTERNAL_SIZE_T victim_size; /* its size */ |
| 1296 | int idx; /* index for bin traversal */ |
| 1297 | mbinptr bin; /* associated bin */ |
| 1298 | mchunkptr remainder; /* remainder from a split */ |
| 1299 | long remainder_size; /* its size */ |
| 1300 | int remainder_index; /* its bin index */ |
| 1301 | unsigned long block; /* block traverser bit */ |
| 1302 | int startidx; /* first bin of a traversed block */ |
| 1303 | mchunkptr fwd; /* misc temp for linking */ |
| 1304 | mchunkptr bck; /* misc temp for linking */ |
| 1305 | mbinptr q; /* misc temp */ |
| 1306 | |
| 1307 | INTERNAL_SIZE_T nb; |
| 1308 | |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 1309 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Stephen Warren | 317581e | 2016-03-05 10:30:53 -0700 | [diff] [blame] | 1310 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 1311 | return malloc_simple(bytes); |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1312 | #endif |
| 1313 | |
Simon Glass | 1a3e39b | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 1314 | if (CONFIG_IS_ENABLED(UNIT_TEST) && malloc_testing) { |
| 1315 | if (--malloc_max_allocs < 0) |
| 1316 | return NULL; |
| 1317 | } |
| 1318 | |
Wolfgang Denk | b634942 | 2010-01-15 11:20:10 +0100 | [diff] [blame] | 1319 | /* check if mem_malloc_init() was run */ |
| 1320 | if ((mem_malloc_start == 0) && (mem_malloc_end == 0)) { |
| 1321 | /* not initialized yet */ |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1322 | return NULL; |
Wolfgang Denk | b634942 | 2010-01-15 11:20:10 +0100 | [diff] [blame] | 1323 | } |
| 1324 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1325 | if ((long)bytes < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1326 | |
| 1327 | nb = request2size(bytes); /* padded request size; */ |
| 1328 | |
| 1329 | /* Check for exact match in a bin */ |
| 1330 | |
| 1331 | if (is_small_request(nb)) /* Faster version for small requests */ |
| 1332 | { |
| 1333 | idx = smallbin_index(nb); |
| 1334 | |
| 1335 | /* No traversal or size check necessary for small bins. */ |
| 1336 | |
| 1337 | q = bin_at(idx); |
| 1338 | victim = last(q); |
| 1339 | |
| 1340 | /* Also scan the next one, since it would have a remainder < MINSIZE */ |
| 1341 | if (victim == q) |
| 1342 | { |
| 1343 | q = next_bin(q); |
| 1344 | victim = last(q); |
| 1345 | } |
| 1346 | if (victim != q) |
| 1347 | { |
| 1348 | victim_size = chunksize(victim); |
| 1349 | unlink(victim, bck, fwd); |
| 1350 | set_inuse_bit_at_offset(victim, victim_size); |
| 1351 | check_malloced_chunk(victim, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1352 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1353 | return chunk2mem(victim); |
| 1354 | } |
| 1355 | |
| 1356 | idx += 2; /* Set for bin scan below. We've already scanned 2 bins. */ |
| 1357 | |
| 1358 | } |
| 1359 | else |
| 1360 | { |
| 1361 | idx = bin_index(nb); |
| 1362 | bin = bin_at(idx); |
| 1363 | |
| 1364 | for (victim = last(bin); victim != bin; victim = victim->bk) |
| 1365 | { |
| 1366 | victim_size = chunksize(victim); |
| 1367 | remainder_size = victim_size - nb; |
| 1368 | |
| 1369 | if (remainder_size >= (long)MINSIZE) /* too big */ |
| 1370 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1371 | --idx; /* adjust to rescan below after checking last remainder */ |
| 1372 | break; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | else if (remainder_size >= 0) /* exact fit */ |
| 1376 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1377 | unlink(victim, bck, fwd); |
| 1378 | set_inuse_bit_at_offset(victim, victim_size); |
| 1379 | check_malloced_chunk(victim, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1380 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1381 | return chunk2mem(victim); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | ++idx; |
| 1386 | |
| 1387 | } |
| 1388 | |
| 1389 | /* Try to use the last split-off remainder */ |
| 1390 | |
| 1391 | if ( (victim = last_remainder->fd) != last_remainder) |
| 1392 | { |
| 1393 | victim_size = chunksize(victim); |
| 1394 | remainder_size = victim_size - nb; |
| 1395 | |
| 1396 | if (remainder_size >= (long)MINSIZE) /* re-split */ |
| 1397 | { |
| 1398 | remainder = chunk_at_offset(victim, nb); |
| 1399 | set_head(victim, nb | PREV_INUSE); |
| 1400 | link_last_remainder(remainder); |
| 1401 | set_head(remainder, remainder_size | PREV_INUSE); |
| 1402 | set_foot(remainder, remainder_size); |
| 1403 | check_malloced_chunk(victim, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1404 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1405 | return chunk2mem(victim); |
| 1406 | } |
| 1407 | |
| 1408 | clear_last_remainder; |
| 1409 | |
| 1410 | if (remainder_size >= 0) /* exhaust */ |
| 1411 | { |
| 1412 | set_inuse_bit_at_offset(victim, victim_size); |
| 1413 | check_malloced_chunk(victim, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1414 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1415 | return chunk2mem(victim); |
| 1416 | } |
| 1417 | |
| 1418 | /* Else place in bin */ |
| 1419 | |
| 1420 | frontlink(victim, victim_size, remainder_index, bck, fwd); |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | If there are any possibly nonempty big-enough blocks, |
| 1425 | search for best fitting chunk by scanning bins in blockwidth units. |
| 1426 | */ |
| 1427 | |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1428 | if ( (block = idx2binblock(idx)) <= binblocks_r) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1429 | { |
| 1430 | |
| 1431 | /* Get to the first marked block */ |
| 1432 | |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1433 | if ( (block & binblocks_r) == 0) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1434 | { |
| 1435 | /* force to an even block boundary */ |
| 1436 | idx = (idx & ~(BINBLOCKWIDTH - 1)) + BINBLOCKWIDTH; |
| 1437 | block <<= 1; |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1438 | while ((block & binblocks_r) == 0) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1439 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1440 | idx += BINBLOCKWIDTH; |
| 1441 | block <<= 1; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /* For each possibly nonempty block ... */ |
| 1446 | for (;;) |
| 1447 | { |
| 1448 | startidx = idx; /* (track incomplete blocks) */ |
| 1449 | q = bin = bin_at(idx); |
| 1450 | |
| 1451 | /* For each bin in this block ... */ |
| 1452 | do |
| 1453 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1454 | /* Find and use first big enough chunk ... */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1455 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1456 | for (victim = last(bin); victim != bin; victim = victim->bk) |
| 1457 | { |
| 1458 | victim_size = chunksize(victim); |
| 1459 | remainder_size = victim_size - nb; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1460 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1461 | if (remainder_size >= (long)MINSIZE) /* split */ |
| 1462 | { |
| 1463 | remainder = chunk_at_offset(victim, nb); |
| 1464 | set_head(victim, nb | PREV_INUSE); |
| 1465 | unlink(victim, bck, fwd); |
| 1466 | link_last_remainder(remainder); |
| 1467 | set_head(remainder, remainder_size | PREV_INUSE); |
| 1468 | set_foot(remainder, remainder_size); |
| 1469 | check_malloced_chunk(victim, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1470 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1471 | return chunk2mem(victim); |
| 1472 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1473 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1474 | else if (remainder_size >= 0) /* take */ |
| 1475 | { |
| 1476 | set_inuse_bit_at_offset(victim, victim_size); |
| 1477 | unlink(victim, bck, fwd); |
| 1478 | check_malloced_chunk(victim, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1479 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1480 | return chunk2mem(victim); |
| 1481 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1482 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1483 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1484 | |
| 1485 | bin = next_bin(bin); |
| 1486 | |
| 1487 | } while ((++idx & (BINBLOCKWIDTH - 1)) != 0); |
| 1488 | |
| 1489 | /* Clear out the block bit. */ |
| 1490 | |
| 1491 | do /* Possibly backtrack to try to clear a partial block */ |
| 1492 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1493 | if ((startidx & (BINBLOCKWIDTH - 1)) == 0) |
| 1494 | { |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1495 | av_[1] = (mbinptr)(binblocks_r & ~block); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | --startidx; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1499 | q = prev_bin(q); |
| 1500 | } while (first(q) == q); |
| 1501 | |
| 1502 | /* Get to the next possibly nonempty block */ |
| 1503 | |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1504 | if ( (block <<= 1) <= binblocks_r && (block != 0) ) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1505 | { |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 1506 | while ((block & binblocks_r) == 0) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1507 | { |
| 1508 | idx += BINBLOCKWIDTH; |
| 1509 | block <<= 1; |
| 1510 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1511 | } |
| 1512 | else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1513 | break; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | |
| 1518 | /* Try to use top chunk */ |
| 1519 | |
| 1520 | /* Require that there be a remainder, ensuring top always exists */ |
| 1521 | if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE) |
| 1522 | { |
| 1523 | |
| 1524 | #if HAVE_MMAP |
| 1525 | /* If big and would otherwise need to extend, try to use mmap instead */ |
| 1526 | if ((unsigned long)nb >= (unsigned long)mmap_threshold && |
Heinrich Schuchardt | b58b9ca | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 1527 | (victim = mmap_chunk(nb))) |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1528 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1529 | return chunk2mem(victim); |
| 1530 | #endif |
| 1531 | |
| 1532 | /* Try to extend */ |
| 1533 | malloc_extend_top(nb); |
| 1534 | if ( (remainder_size = chunksize(top) - nb) < (long)MINSIZE) |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1535 | return NULL; /* propagate failure */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | victim = top; |
| 1539 | set_head(victim, nb | PREV_INUSE); |
| 1540 | top = chunk_at_offset(victim, nb); |
| 1541 | set_head(top, remainder_size | PREV_INUSE); |
| 1542 | check_malloced_chunk(victim, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1543 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1544 | return chunk2mem(victim); |
| 1545 | |
| 1546 | } |
| 1547 | |
| 1548 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 1549 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1550 | |
| 1551 | /* |
| 1552 | |
| 1553 | free() algorithm : |
| 1554 | |
| 1555 | cases: |
| 1556 | |
| 1557 | 1. free(0) has no effect. |
| 1558 | |
| 1559 | 2. If the chunk was allocated via mmap, it is release via munmap(). |
| 1560 | |
| 1561 | 3. If a returned chunk borders the current high end of memory, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1562 | it is consolidated into the top, and if the total unused |
| 1563 | topmost memory exceeds the trim threshold, malloc_trim is |
| 1564 | called. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1565 | |
| 1566 | 4. Other chunks are consolidated as they arrive, and |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1567 | placed in corresponding bins. (This includes the case of |
| 1568 | consolidating with the current `last_remainder'). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1569 | |
| 1570 | */ |
| 1571 | |
| 1572 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1573 | STATIC_IF_MCHECK |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1574 | #if __STD_C |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1575 | void fREe_impl(Void_t* mem) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1576 | #else |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1577 | void fREe_impl(mem) Void_t* mem; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1578 | #endif |
| 1579 | { |
| 1580 | mchunkptr p; /* chunk corresponding to mem */ |
| 1581 | INTERNAL_SIZE_T hd; /* its head field */ |
| 1582 | INTERNAL_SIZE_T sz; /* its size */ |
| 1583 | int idx; /* its bin index */ |
| 1584 | mchunkptr next; /* next contiguous chunk */ |
| 1585 | INTERNAL_SIZE_T nextsz; /* its size */ |
| 1586 | INTERNAL_SIZE_T prevsz; /* size of previous contiguous chunk */ |
| 1587 | mchunkptr bck; /* misc temp for linking */ |
| 1588 | mchunkptr fwd; /* misc temp for linking */ |
| 1589 | int islr; /* track whether merging with last_remainder */ |
| 1590 | |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 1591 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1592 | /* free() is a no-op - all the memory will be freed on relocation */ |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1593 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
| 1594 | VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ); |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1595 | return; |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1596 | } |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1597 | #endif |
| 1598 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1599 | if (mem == NULL) /* free(0) has no effect */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1600 | return; |
| 1601 | |
| 1602 | p = mem2chunk(mem); |
| 1603 | hd = p->size; |
| 1604 | |
| 1605 | #if HAVE_MMAP |
| 1606 | if (hd & IS_MMAPPED) /* release mmapped memory. */ |
| 1607 | { |
| 1608 | munmap_chunk(p); |
| 1609 | return; |
| 1610 | } |
| 1611 | #endif |
| 1612 | |
| 1613 | check_inuse_chunk(p); |
| 1614 | |
| 1615 | sz = hd & ~PREV_INUSE; |
| 1616 | next = chunk_at_offset(p, sz); |
| 1617 | nextsz = chunksize(next); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1618 | VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1619 | |
| 1620 | if (next == top) /* merge with top */ |
| 1621 | { |
| 1622 | sz += nextsz; |
| 1623 | |
| 1624 | if (!(hd & PREV_INUSE)) /* consolidate backward */ |
| 1625 | { |
| 1626 | prevsz = p->prev_size; |
| 1627 | p = chunk_at_offset(p, -((long) prevsz)); |
| 1628 | sz += prevsz; |
| 1629 | unlink(p, bck, fwd); |
| 1630 | } |
| 1631 | |
| 1632 | set_head(p, sz | PREV_INUSE); |
| 1633 | top = p; |
| 1634 | if ((unsigned long)(sz) >= (unsigned long)trim_threshold) |
| 1635 | malloc_trim(top_pad); |
| 1636 | return; |
| 1637 | } |
| 1638 | |
| 1639 | set_head(next, nextsz); /* clear inuse bit */ |
| 1640 | |
| 1641 | islr = 0; |
| 1642 | |
| 1643 | if (!(hd & PREV_INUSE)) /* consolidate backward */ |
| 1644 | { |
| 1645 | prevsz = p->prev_size; |
| 1646 | p = chunk_at_offset(p, -((long) prevsz)); |
| 1647 | sz += prevsz; |
| 1648 | |
| 1649 | if (p->fd == last_remainder) /* keep as last_remainder */ |
| 1650 | islr = 1; |
| 1651 | else |
| 1652 | unlink(p, bck, fwd); |
| 1653 | } |
| 1654 | |
| 1655 | if (!(inuse_bit_at_offset(next, nextsz))) /* consolidate forward */ |
| 1656 | { |
| 1657 | sz += nextsz; |
| 1658 | |
| 1659 | if (!islr && next->fd == last_remainder) /* re-insert last_remainder */ |
| 1660 | { |
| 1661 | islr = 1; |
| 1662 | link_last_remainder(p); |
| 1663 | } |
| 1664 | else |
| 1665 | unlink(next, bck, fwd); |
| 1666 | } |
| 1667 | |
| 1668 | |
| 1669 | set_head(p, sz | PREV_INUSE); |
| 1670 | set_foot(p, sz); |
| 1671 | if (!islr) |
| 1672 | frontlink(p, sz, idx, bck, fwd); |
| 1673 | } |
| 1674 | |
| 1675 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1676 | |
| 1677 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 1678 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1679 | /* |
| 1680 | |
| 1681 | Realloc algorithm: |
| 1682 | |
| 1683 | Chunks that were obtained via mmap cannot be extended or shrunk |
| 1684 | unless HAVE_MREMAP is defined, in which case mremap is used. |
| 1685 | Otherwise, if their reallocation is for additional space, they are |
| 1686 | copied. If for less, they are just left alone. |
| 1687 | |
| 1688 | Otherwise, if the reallocation is for additional space, and the |
| 1689 | chunk can be extended, it is, else a malloc-copy-free sequence is |
| 1690 | taken. There are several different ways that a chunk could be |
| 1691 | extended. All are tried: |
| 1692 | |
| 1693 | * Extending forward into following adjacent free chunk. |
| 1694 | * Shifting backwards, joining preceding adjacent space |
| 1695 | * Both shifting backwards and extending forward. |
| 1696 | * Extending into newly sbrked space |
| 1697 | |
| 1698 | Unless the #define REALLOC_ZERO_BYTES_FREES is set, realloc with a |
| 1699 | size argument of zero (re)allocates a minimum-sized chunk. |
| 1700 | |
| 1701 | If the reallocation is for less space, and the new request is for |
| 1702 | a `small' (<512 bytes) size, then the newly unused space is lopped |
| 1703 | off and freed. |
| 1704 | |
| 1705 | The old unix realloc convention of allowing the last-free'd chunk |
| 1706 | to be used as an argument to realloc is no longer supported. |
| 1707 | I don't know of any programs still relying on this feature, |
| 1708 | and allowing it would also allow too many other incorrect |
| 1709 | usages of realloc to be sensible. |
| 1710 | |
| 1711 | |
| 1712 | */ |
| 1713 | |
| 1714 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1715 | STATIC_IF_MCHECK |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1716 | #if __STD_C |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1717 | Void_t* rEALLOc_impl(Void_t* oldmem, size_t bytes) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1718 | #else |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1719 | Void_t* rEALLOc_impl(oldmem, bytes) Void_t* oldmem; size_t bytes; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1720 | #endif |
| 1721 | { |
| 1722 | INTERNAL_SIZE_T nb; /* padded request size */ |
| 1723 | |
| 1724 | mchunkptr oldp; /* chunk corresponding to oldmem */ |
| 1725 | INTERNAL_SIZE_T oldsize; /* its size */ |
| 1726 | |
| 1727 | mchunkptr newp; /* chunk to return */ |
| 1728 | INTERNAL_SIZE_T newsize; /* its size */ |
| 1729 | Void_t* newmem; /* corresponding user mem */ |
| 1730 | |
| 1731 | mchunkptr next; /* next contiguous chunk after oldp */ |
| 1732 | INTERNAL_SIZE_T nextsize; /* its size */ |
| 1733 | |
| 1734 | mchunkptr prev; /* previous contiguous chunk before oldp */ |
| 1735 | INTERNAL_SIZE_T prevsize; /* its size */ |
| 1736 | |
| 1737 | mchunkptr remainder; /* holds split off extra space from newp */ |
| 1738 | INTERNAL_SIZE_T remainder_size; /* its size */ |
| 1739 | |
| 1740 | mchunkptr bck; /* misc temp for linking */ |
| 1741 | mchunkptr fwd; /* misc temp for linking */ |
| 1742 | |
| 1743 | #ifdef REALLOC_ZERO_BYTES_FREES |
Heinrich Schuchardt | b58b9ca | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 1744 | if (!bytes) { |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1745 | fREe_impl(oldmem); |
Heinrich Schuchardt | b58b9ca | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 1746 | return NULL; |
| 1747 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1748 | #endif |
| 1749 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1750 | if ((long)bytes < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1751 | |
| 1752 | /* realloc of null is supposed to be same as malloc */ |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1753 | if (oldmem == NULL) return mALLOc_impl(bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1754 | |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 1755 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 1756 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 1757 | /* This is harder to support and should not be needed */ |
| 1758 | panic("pre-reloc realloc() is not supported"); |
| 1759 | } |
| 1760 | #endif |
| 1761 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1762 | newp = oldp = mem2chunk(oldmem); |
| 1763 | newsize = oldsize = chunksize(oldp); |
| 1764 | |
| 1765 | |
| 1766 | nb = request2size(bytes); |
| 1767 | |
| 1768 | #if HAVE_MMAP |
| 1769 | if (chunk_is_mmapped(oldp)) |
| 1770 | { |
| 1771 | #if HAVE_MREMAP |
| 1772 | newp = mremap_chunk(oldp, nb); |
| 1773 | if(newp) return chunk2mem(newp); |
| 1774 | #endif |
| 1775 | /* Note the extra SIZE_SZ overhead. */ |
| 1776 | if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */ |
| 1777 | /* Must alloc, copy, free. */ |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1778 | newmem = mALLOc_impl(bytes); |
Heinrich Schuchardt | b58b9ca | 2017-11-10 21:46:34 +0100 | [diff] [blame] | 1779 | if (!newmem) |
| 1780 | return NULL; /* propagate failure */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1781 | MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ); |
| 1782 | munmap_chunk(oldp); |
| 1783 | return newmem; |
| 1784 | } |
| 1785 | #endif |
| 1786 | |
| 1787 | check_inuse_chunk(oldp); |
| 1788 | |
| 1789 | if ((long)(oldsize) < (long)(nb)) |
| 1790 | { |
| 1791 | |
| 1792 | /* Try expanding forward */ |
| 1793 | |
| 1794 | next = chunk_at_offset(oldp, oldsize); |
| 1795 | if (next == top || !inuse(next)) |
| 1796 | { |
| 1797 | nextsize = chunksize(next); |
| 1798 | |
| 1799 | /* Forward into top only if a remainder */ |
| 1800 | if (next == top) |
| 1801 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1802 | if ((long)(nextsize + newsize) >= (long)(nb + MINSIZE)) |
| 1803 | { |
| 1804 | newsize += nextsize; |
| 1805 | top = chunk_at_offset(oldp, nb); |
| 1806 | set_head(top, (newsize - nb) | PREV_INUSE); |
| 1807 | set_head_size(oldp, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1808 | VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ); |
| 1809 | VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1810 | return chunk2mem(oldp); |
| 1811 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | /* Forward into next chunk */ |
| 1815 | else if (((long)(nextsize + newsize) >= (long)(nb))) |
| 1816 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1817 | unlink(next, bck, fwd); |
| 1818 | newsize += nextsize; |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1819 | VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ); |
| 1820 | VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1821 | goto split; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | else |
| 1825 | { |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1826 | next = NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1827 | nextsize = 0; |
| 1828 | } |
| 1829 | |
| 1830 | /* Try shifting backwards. */ |
| 1831 | |
| 1832 | if (!prev_inuse(oldp)) |
| 1833 | { |
| 1834 | prev = prev_chunk(oldp); |
| 1835 | prevsize = chunksize(prev); |
| 1836 | |
| 1837 | /* try forward + backward first to save a later consolidation */ |
| 1838 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1839 | if (next != NULL) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1840 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1841 | /* into top */ |
| 1842 | if (next == top) |
| 1843 | { |
| 1844 | if ((long)(nextsize + prevsize + newsize) >= (long)(nb + MINSIZE)) |
| 1845 | { |
| 1846 | unlink(prev, bck, fwd); |
| 1847 | newp = prev; |
| 1848 | newsize += prevsize + nextsize; |
| 1849 | newmem = chunk2mem(newp); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1850 | VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1851 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
| 1852 | top = chunk_at_offset(newp, nb); |
| 1853 | set_head(top, (newsize - nb) | PREV_INUSE); |
| 1854 | set_head_size(newp, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1855 | VALGRIND_FREELIKE_BLOCK(oldmem, SIZE_SZ); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1856 | return newmem; |
| 1857 | } |
| 1858 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1859 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1860 | /* into next chunk */ |
| 1861 | else if (((long)(nextsize + prevsize + newsize) >= (long)(nb))) |
| 1862 | { |
| 1863 | unlink(next, bck, fwd); |
| 1864 | unlink(prev, bck, fwd); |
| 1865 | newp = prev; |
| 1866 | newsize += nextsize + prevsize; |
| 1867 | newmem = chunk2mem(newp); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1868 | VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1869 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
| 1870 | goto split; |
| 1871 | } |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /* backward only */ |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1875 | if (prev != NULL && (long)(prevsize + newsize) >= (long)nb) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1876 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1877 | unlink(prev, bck, fwd); |
| 1878 | newp = prev; |
| 1879 | newsize += prevsize; |
| 1880 | newmem = chunk2mem(newp); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1881 | VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1882 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
| 1883 | goto split; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | /* Must allocate */ |
| 1888 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1889 | newmem = mALLOc_impl (bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1890 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1891 | if (newmem == NULL) /* propagate failure */ |
| 1892 | return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1893 | |
| 1894 | /* Avoid copy if newp is next chunk after oldp. */ |
| 1895 | /* (This can only happen when new chunk is sbrk'ed.) */ |
| 1896 | |
| 1897 | if ( (newp = mem2chunk(newmem)) == next_chunk(oldp)) |
| 1898 | { |
| 1899 | newsize += chunksize(newp); |
| 1900 | newp = oldp; |
| 1901 | goto split; |
| 1902 | } |
| 1903 | |
| 1904 | /* Otherwise copy, free, and exit */ |
| 1905 | MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ); |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1906 | fREe_impl(oldmem); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1907 | return newmem; |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1908 | } else { |
| 1909 | VALGRIND_RESIZEINPLACE_BLOCK(oldmem, 0, bytes, SIZE_SZ); |
| 1910 | VALGRIND_MAKE_MEM_DEFINED(oldmem, bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | split: /* split off extra room in old or expanded chunk */ |
| 1915 | |
| 1916 | if (newsize - nb >= MINSIZE) /* split off remainder */ |
| 1917 | { |
| 1918 | remainder = chunk_at_offset(newp, nb); |
| 1919 | remainder_size = newsize - nb; |
| 1920 | set_head_size(newp, nb); |
| 1921 | set_head(remainder, remainder_size | PREV_INUSE); |
| 1922 | set_inuse_bit_at_offset(remainder, remainder_size); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 1923 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ, |
| 1924 | false); |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1925 | fREe_impl(chunk2mem(remainder)); /* let free() deal with it */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1926 | } |
| 1927 | else |
| 1928 | { |
| 1929 | set_head_size(newp, newsize); |
| 1930 | set_inuse_bit_at_offset(newp, newsize); |
| 1931 | } |
| 1932 | |
| 1933 | check_inuse_chunk(newp); |
| 1934 | return chunk2mem(newp); |
| 1935 | } |
| 1936 | |
| 1937 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 1938 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1939 | |
| 1940 | /* |
| 1941 | |
| 1942 | memalign algorithm: |
| 1943 | |
| 1944 | memalign requests more than enough space from malloc, finds a spot |
| 1945 | within that chunk that meets the alignment request, and then |
| 1946 | possibly frees the leading and trailing space. |
| 1947 | |
| 1948 | The alignment argument must be a power of two. This property is not |
| 1949 | checked by memalign, so misuse may result in random runtime errors. |
| 1950 | |
| 1951 | 8-byte alignment is guaranteed by normal malloc calls, so don't |
| 1952 | bother calling memalign with an argument of 8 or less. |
| 1953 | |
| 1954 | Overreliance on memalign is a sure way to fragment space. |
| 1955 | |
| 1956 | */ |
| 1957 | |
| 1958 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1959 | STATIC_IF_MCHECK |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1960 | #if __STD_C |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1961 | Void_t* mEMALIGn_impl(size_t alignment, size_t bytes) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1962 | #else |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1963 | Void_t* mEMALIGn_impl(alignment, bytes) size_t alignment; size_t bytes; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1964 | #endif |
| 1965 | { |
| 1966 | INTERNAL_SIZE_T nb; /* padded request size */ |
| 1967 | char* m; /* memory returned by malloc call */ |
| 1968 | mchunkptr p; /* corresponding chunk */ |
| 1969 | char* brk; /* alignment point within p */ |
| 1970 | mchunkptr newp; /* chunk to return */ |
| 1971 | INTERNAL_SIZE_T newsize; /* its size */ |
| 1972 | INTERNAL_SIZE_T leadsize; /* leading space befor alignment point */ |
| 1973 | mchunkptr remainder; /* spare room at end to split off */ |
| 1974 | long remainder_size; /* its size */ |
| 1975 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 1976 | if ((long)bytes < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1977 | |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 1978 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Ley Foon Tan | 2427ec6 | 2018-05-18 18:03:12 +0800 | [diff] [blame] | 1979 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
Andreas Dannenberg | ecc2740 | 2019-03-27 13:17:26 -0500 | [diff] [blame] | 1980 | return memalign_simple(alignment, bytes); |
Ley Foon Tan | 2427ec6 | 2018-05-18 18:03:12 +0800 | [diff] [blame] | 1981 | } |
| 1982 | #endif |
| 1983 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1984 | /* If need less alignment than we give anyway, just relay to malloc */ |
| 1985 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1986 | if (alignment <= MALLOC_ALIGNMENT) return mALLOc_impl(bytes); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1987 | |
| 1988 | /* Otherwise, ensure that it is at least a minimum chunk size */ |
| 1989 | |
| 1990 | if (alignment < MINSIZE) alignment = MINSIZE; |
| 1991 | |
| 1992 | /* Call malloc with worst case padding to hit alignment. */ |
| 1993 | |
| 1994 | nb = request2size(bytes); |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 1995 | m = (char*)(mALLOc_impl(nb + alignment + MINSIZE)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 1996 | |
Stephen Warren | 54ca0c7 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 1997 | /* |
| 1998 | * The attempt to over-allocate (with a size large enough to guarantee the |
| 1999 | * ability to find an aligned region within allocated memory) failed. |
| 2000 | * |
| 2001 | * Try again, this time only allocating exactly the size the user wants. If |
| 2002 | * the allocation now succeeds and just happens to be aligned, we can still |
| 2003 | * fulfill the user's request. |
| 2004 | */ |
| 2005 | if (m == NULL) { |
Stephen Warren | 44628b8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2006 | size_t extra, extra2; |
Stephen Warren | 54ca0c7 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 2007 | /* |
| 2008 | * Use bytes not nb, since mALLOc internally calls request2size too, and |
| 2009 | * each call increases the size to allocate, to account for the header. |
| 2010 | */ |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2011 | m = (char*)(mALLOc_impl(bytes)); |
Stephen Warren | 54ca0c7 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 2012 | /* Aligned -> return it */ |
| 2013 | if ((((unsigned long)(m)) % alignment) == 0) |
| 2014 | return m; |
Stephen Warren | 44628b8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2015 | /* |
| 2016 | * Otherwise, try again, requesting enough extra space to be able to |
| 2017 | * acquire alignment. |
| 2018 | */ |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2019 | fREe_impl(m); |
Stephen Warren | 44628b8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2020 | /* Add in extra bytes to match misalignment of unexpanded allocation */ |
| 2021 | extra = alignment - (((unsigned long)(m)) % alignment); |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2022 | m = (char*)(mALLOc_impl(bytes + extra)); |
Stephen Warren | 44628b8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2023 | /* |
| 2024 | * m might not be the same as before. Validate that the previous value of |
| 2025 | * extra still works for the current value of m. |
| 2026 | * If (!m), extra2=alignment so |
| 2027 | */ |
| 2028 | if (m) { |
| 2029 | extra2 = alignment - (((unsigned long)(m)) % alignment); |
| 2030 | if (extra2 > extra) { |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2031 | fREe_impl(m); |
Stephen Warren | 44628b8 | 2016-04-25 15:55:42 -0600 | [diff] [blame] | 2032 | m = NULL; |
| 2033 | } |
| 2034 | } |
| 2035 | /* Fall through to original NULL check and chunk splitting logic */ |
Stephen Warren | 54ca0c7 | 2016-01-25 14:03:42 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2038 | if (m == NULL) return NULL; /* propagate failure */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2039 | |
| 2040 | p = mem2chunk(m); |
| 2041 | |
| 2042 | if ((((unsigned long)(m)) % alignment) == 0) /* aligned */ |
| 2043 | { |
| 2044 | #if HAVE_MMAP |
| 2045 | if(chunk_is_mmapped(p)) |
| 2046 | return chunk2mem(p); /* nothing more to do */ |
| 2047 | #endif |
| 2048 | } |
| 2049 | else /* misaligned */ |
| 2050 | { |
| 2051 | /* |
| 2052 | Find an aligned spot inside chunk. |
| 2053 | Since we need to give back leading space in a chunk of at |
| 2054 | least MINSIZE, if the first calculation places us at |
| 2055 | a spot with less than MINSIZE leader, we can move to the |
| 2056 | next aligned spot -- we've allocated enough total room so that |
| 2057 | this is always possible. |
| 2058 | */ |
| 2059 | |
| 2060 | brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) & -((signed) alignment)); |
| 2061 | if ((long)(brk - (char*)(p)) < MINSIZE) brk = brk + alignment; |
| 2062 | |
| 2063 | newp = (mchunkptr)brk; |
| 2064 | leadsize = brk - (char*)(p); |
| 2065 | newsize = chunksize(p) - leadsize; |
| 2066 | |
| 2067 | #if HAVE_MMAP |
| 2068 | if(chunk_is_mmapped(p)) |
| 2069 | { |
| 2070 | newp->prev_size = p->prev_size + leadsize; |
| 2071 | set_head(newp, newsize|IS_MMAPPED); |
| 2072 | return chunk2mem(newp); |
| 2073 | } |
| 2074 | #endif |
| 2075 | |
| 2076 | /* give back leader, use the rest */ |
| 2077 | |
| 2078 | set_head(newp, newsize | PREV_INUSE); |
| 2079 | set_inuse_bit_at_offset(newp, newsize); |
| 2080 | set_head_size(p, leadsize); |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2081 | fREe_impl(chunk2mem(p)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2082 | p = newp; |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2083 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(p), bytes, SIZE_SZ, false); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2084 | |
| 2085 | assert (newsize >= nb && (((unsigned long)(chunk2mem(p))) % alignment) == 0); |
| 2086 | } |
| 2087 | |
| 2088 | /* Also give back spare room at the end */ |
| 2089 | |
| 2090 | remainder_size = chunksize(p) - nb; |
| 2091 | |
| 2092 | if (remainder_size >= (long)MINSIZE) |
| 2093 | { |
| 2094 | remainder = chunk_at_offset(p, nb); |
| 2095 | set_head(remainder, remainder_size | PREV_INUSE); |
| 2096 | set_head_size(p, nb); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2097 | VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ, |
| 2098 | false); |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2099 | fREe_impl(chunk2mem(remainder)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | check_inuse_chunk(p); |
| 2103 | return chunk2mem(p); |
| 2104 | |
| 2105 | } |
| 2106 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2107 | |
| 2108 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2109 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2110 | /* |
| 2111 | valloc just invokes memalign with alignment argument equal |
| 2112 | to the page size of the system (or as near to this as can |
| 2113 | be figured out from all the includes/defines above.) |
| 2114 | */ |
| 2115 | |
| 2116 | #if __STD_C |
| 2117 | Void_t* vALLOc(size_t bytes) |
| 2118 | #else |
| 2119 | Void_t* vALLOc(bytes) size_t bytes; |
| 2120 | #endif |
| 2121 | { |
| 2122 | return mEMALIGn (malloc_getpagesize, bytes); |
| 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | pvalloc just invokes valloc for the nearest pagesize |
| 2127 | that will accommodate request |
| 2128 | */ |
| 2129 | |
| 2130 | |
| 2131 | #if __STD_C |
| 2132 | Void_t* pvALLOc(size_t bytes) |
| 2133 | #else |
| 2134 | Void_t* pvALLOc(bytes) size_t bytes; |
| 2135 | #endif |
| 2136 | { |
| 2137 | size_t pagesize = malloc_getpagesize; |
| 2138 | return mEMALIGn (pagesize, (bytes + pagesize - 1) & ~(pagesize - 1)); |
| 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | |
| 2143 | calloc calls malloc, then zeroes out the allocated chunk. |
| 2144 | |
| 2145 | */ |
| 2146 | |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2147 | STATIC_IF_MCHECK |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2148 | #if __STD_C |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2149 | Void_t* cALLOc_impl(size_t n, size_t elem_size) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2150 | #else |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2151 | Void_t* cALLOc_impl(n, elem_size) size_t n; size_t elem_size; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2152 | #endif |
| 2153 | { |
| 2154 | mchunkptr p; |
| 2155 | INTERNAL_SIZE_T csz; |
| 2156 | |
| 2157 | INTERNAL_SIZE_T sz = n * elem_size; |
| 2158 | |
| 2159 | |
| 2160 | /* check if expand_top called, in which case don't need to clear */ |
Shengyu Qu | 10a3e61 | 2023-08-25 00:25:19 +0800 | [diff] [blame] | 2161 | #if CONFIG_IS_ENABLED(SYS_MALLOC_CLEAR_ON_INIT) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2162 | #if MORECORE_CLEARS |
| 2163 | mchunkptr oldtop = top; |
| 2164 | INTERNAL_SIZE_T oldtopsize = chunksize(top); |
| 2165 | #endif |
Przemyslaw Marczak | 8843678 | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 2166 | #endif |
Eugene Uriev | 33bd33d | 2024-03-31 23:03:19 +0300 | [diff] [blame] | 2167 | Void_t* mem = mALLOc_impl (sz); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2168 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2169 | if ((long)n < 0) return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2170 | |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2171 | if (mem == NULL) |
| 2172 | return NULL; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2173 | else |
| 2174 | { |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 2175 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Simon Glass | 9489046 | 2014-11-10 17:16:43 -0700 | [diff] [blame] | 2176 | if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) { |
Simon Goldschmidt | 6b89084 | 2019-10-25 21:23:35 +0200 | [diff] [blame] | 2177 | memset(mem, 0, sz); |
Simon Glass | 863e404 | 2014-07-10 22:23:28 -0600 | [diff] [blame] | 2178 | return mem; |
| 2179 | } |
| 2180 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2181 | p = mem2chunk(mem); |
| 2182 | |
| 2183 | /* Two optional cases in which clearing not necessary */ |
| 2184 | |
| 2185 | |
| 2186 | #if HAVE_MMAP |
| 2187 | if (chunk_is_mmapped(p)) return mem; |
| 2188 | #endif |
| 2189 | |
| 2190 | csz = chunksize(p); |
| 2191 | |
Shengyu Qu | 10a3e61 | 2023-08-25 00:25:19 +0800 | [diff] [blame] | 2192 | #if CONFIG_IS_ENABLED(SYS_MALLOC_CLEAR_ON_INIT) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2193 | #if MORECORE_CLEARS |
| 2194 | if (p == oldtop && csz > oldtopsize) |
| 2195 | { |
| 2196 | /* clear only the bytes from non-freshly-sbrked memory */ |
| 2197 | csz = oldtopsize; |
| 2198 | } |
| 2199 | #endif |
Przemyslaw Marczak | 8843678 | 2015-03-04 14:01:24 +0100 | [diff] [blame] | 2200 | #endif |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2201 | |
| 2202 | MALLOC_ZERO(mem, csz - SIZE_SZ); |
Sean Anderson | 98011e2 | 2022-03-23 14:04:49 -0400 | [diff] [blame] | 2203 | VALGRIND_MAKE_MEM_DEFINED(mem, sz); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2204 | return mem; |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | /* |
| 2209 | |
| 2210 | cfree just calls free. It is needed/defined on some systems |
| 2211 | that pair it with calloc, presumably for odd historical reasons. |
| 2212 | |
| 2213 | */ |
| 2214 | |
| 2215 | #if !defined(INTERNAL_LINUX_C_LIB) || !defined(__ELF__) |
| 2216 | #if __STD_C |
| 2217 | void cfree(Void_t *mem) |
| 2218 | #else |
| 2219 | void cfree(mem) Void_t *mem; |
| 2220 | #endif |
| 2221 | { |
| 2222 | fREe(mem); |
| 2223 | } |
| 2224 | #endif |
| 2225 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2226 | |
Eugene Uriev | 172be69 | 2024-03-31 23:03:22 +0300 | [diff] [blame] | 2227 | #ifdef MCHECK_HEAP_PROTECTION |
| 2228 | #include "mcheck_core.inc.h" |
| 2229 | #if !__STD_C |
| 2230 | #error "must have __STD_C" |
| 2231 | #endif |
| 2232 | |
| 2233 | Void_t *mALLOc(size_t bytes) |
| 2234 | { |
Eugene Uriev | 2601b61 | 2024-03-31 23:03:24 +0300 | [diff] [blame] | 2235 | mcheck_pedantic_prehook(); |
Eugene Uriev | 172be69 | 2024-03-31 23:03:22 +0300 | [diff] [blame] | 2236 | size_t fullsz = mcheck_alloc_prehook(bytes); |
| 2237 | void *p = mALLOc_impl(fullsz); |
| 2238 | |
| 2239 | if (!p) |
| 2240 | return p; |
| 2241 | return mcheck_alloc_posthook(p, bytes); |
| 2242 | } |
| 2243 | |
| 2244 | void fREe(Void_t *mem) { fREe_impl(mcheck_free_prehook(mem)); } |
| 2245 | |
| 2246 | Void_t *rEALLOc(Void_t *oldmem, size_t bytes) |
| 2247 | { |
Eugene Uriev | 2601b61 | 2024-03-31 23:03:24 +0300 | [diff] [blame] | 2248 | mcheck_pedantic_prehook(); |
Eugene Uriev | 172be69 | 2024-03-31 23:03:22 +0300 | [diff] [blame] | 2249 | if (bytes == 0) { |
| 2250 | if (oldmem) |
| 2251 | fREe(oldmem); |
| 2252 | return NULL; |
| 2253 | } |
| 2254 | |
| 2255 | if (oldmem == NULL) |
| 2256 | return mALLOc(bytes); |
| 2257 | |
| 2258 | void *p = mcheck_reallocfree_prehook(oldmem); |
| 2259 | size_t newsz = mcheck_alloc_prehook(bytes); |
| 2260 | |
| 2261 | p = rEALLOc_impl(p, newsz); |
| 2262 | if (!p) |
| 2263 | return p; |
| 2264 | return mcheck_alloc_noclean_posthook(p, bytes); |
| 2265 | } |
| 2266 | |
| 2267 | Void_t *mEMALIGn(size_t alignment, size_t bytes) |
| 2268 | { |
Eugene Uriev | 2601b61 | 2024-03-31 23:03:24 +0300 | [diff] [blame] | 2269 | mcheck_pedantic_prehook(); |
Eugene Uriev | c61e313 | 2024-03-31 23:03:23 +0300 | [diff] [blame] | 2270 | size_t fullsz = mcheck_memalign_prehook(alignment, bytes); |
| 2271 | void *p = mEMALIGn_impl(alignment, fullsz); |
| 2272 | |
| 2273 | if (!p) |
| 2274 | return p; |
| 2275 | return mcheck_memalign_posthook(alignment, p, bytes); |
Eugene Uriev | 172be69 | 2024-03-31 23:03:22 +0300 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | // pvALLOc, vALLOc - redirect to mEMALIGn, defined here, so they need no wrapping. |
| 2279 | |
| 2280 | Void_t *cALLOc(size_t n, size_t elem_size) |
| 2281 | { |
Eugene Uriev | 2601b61 | 2024-03-31 23:03:24 +0300 | [diff] [blame] | 2282 | mcheck_pedantic_prehook(); |
Eugene Uriev | 172be69 | 2024-03-31 23:03:22 +0300 | [diff] [blame] | 2283 | // NB: here is no overflow check. |
| 2284 | size_t fullsz = mcheck_alloc_prehook(n * elem_size); |
| 2285 | void *p = cALLOc_impl(1, fullsz); |
| 2286 | |
| 2287 | if (!p) |
| 2288 | return p; |
| 2289 | return mcheck_alloc_noclean_posthook(p, n * elem_size); |
| 2290 | } |
| 2291 | |
| 2292 | // mcheck API { |
Eugene Uriev | 2601b61 | 2024-03-31 23:03:24 +0300 | [diff] [blame] | 2293 | int mcheck_pedantic(mcheck_abortfunc_t f) |
| 2294 | { |
| 2295 | mcheck_initialize(f, 1); |
| 2296 | return 0; |
| 2297 | } |
| 2298 | |
Eugene Uriev | 172be69 | 2024-03-31 23:03:22 +0300 | [diff] [blame] | 2299 | int mcheck(mcheck_abortfunc_t f) |
| 2300 | { |
| 2301 | mcheck_initialize(f, 0); |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Eugene Uriev | 2601b61 | 2024-03-31 23:03:24 +0300 | [diff] [blame] | 2305 | void mcheck_check_all(void) { mcheck_pedantic_check(); } |
| 2306 | |
Eugene Uriev | 172be69 | 2024-03-31 23:03:22 +0300 | [diff] [blame] | 2307 | enum mcheck_status mprobe(void *__ptr) { return mcheck_mprobe(__ptr); } |
| 2308 | // mcheck API } |
| 2309 | #endif |
| 2310 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2311 | |
| 2312 | /* |
| 2313 | |
| 2314 | Malloc_trim gives memory back to the system (via negative |
| 2315 | arguments to sbrk) if there is unused memory at the `high' end of |
| 2316 | the malloc pool. You can call this after freeing large blocks of |
| 2317 | memory to potentially reduce the system-level memory requirements |
| 2318 | of a program. However, it cannot guarantee to reduce memory. Under |
| 2319 | some allocation patterns, some large free blocks of memory will be |
| 2320 | locked between two used chunks, so they cannot be given back to |
| 2321 | the system. |
| 2322 | |
| 2323 | The `pad' argument to malloc_trim represents the amount of free |
| 2324 | trailing space to leave untrimmed. If this argument is zero, |
| 2325 | only the minimum amount of memory to maintain internal data |
| 2326 | structures will be left (one page or less). Non-zero arguments |
| 2327 | can be supplied to maintain enough trailing space to service |
| 2328 | future expected allocations without having to re-obtain memory |
| 2329 | from the system. |
| 2330 | |
| 2331 | Malloc_trim returns 1 if it actually released any memory, else 0. |
| 2332 | |
| 2333 | */ |
| 2334 | |
| 2335 | #if __STD_C |
| 2336 | int malloc_trim(size_t pad) |
| 2337 | #else |
| 2338 | int malloc_trim(pad) size_t pad; |
| 2339 | #endif |
| 2340 | { |
| 2341 | long top_size; /* Amount of top-most memory */ |
| 2342 | long extra; /* Amount to release */ |
| 2343 | char* current_brk; /* address returned by pre-check sbrk call */ |
| 2344 | char* new_brk; /* address returned by negative sbrk call */ |
| 2345 | |
| 2346 | unsigned long pagesz = malloc_getpagesize; |
| 2347 | |
| 2348 | top_size = chunksize(top); |
| 2349 | extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz; |
| 2350 | |
| 2351 | if (extra < (long)pagesz) /* Not enough memory to release */ |
| 2352 | return 0; |
| 2353 | |
| 2354 | else |
| 2355 | { |
| 2356 | /* Test to make sure no one else called sbrk */ |
| 2357 | current_brk = (char*)(MORECORE (0)); |
| 2358 | if (current_brk != (char*)(top) + top_size) |
| 2359 | return 0; /* Apparently we don't own memory; must fail */ |
| 2360 | |
| 2361 | else |
| 2362 | { |
| 2363 | new_brk = (char*)(MORECORE (-extra)); |
| 2364 | |
| 2365 | if (new_brk == (char*)(MORECORE_FAILURE)) /* sbrk failed? */ |
| 2366 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2367 | /* Try to figure out what we have */ |
| 2368 | current_brk = (char*)(MORECORE (0)); |
| 2369 | top_size = current_brk - (char*)top; |
| 2370 | if (top_size >= (long)MINSIZE) /* if not, we are very very dead! */ |
| 2371 | { |
| 2372 | sbrked_mem = current_brk - sbrk_base; |
| 2373 | set_head(top, top_size | PREV_INUSE); |
| 2374 | } |
| 2375 | check_chunk(top); |
| 2376 | return 0; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | else |
| 2380 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2381 | /* Success. Adjust top accordingly. */ |
| 2382 | set_head(top, (top_size - extra) | PREV_INUSE); |
| 2383 | sbrked_mem -= extra; |
| 2384 | check_chunk(top); |
| 2385 | return 1; |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2391 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2392 | |
| 2393 | /* |
| 2394 | malloc_usable_size: |
| 2395 | |
| 2396 | This routine tells you how many bytes you can actually use in an |
| 2397 | allocated chunk, which may be more than you requested (although |
| 2398 | often not). You can use this many bytes without worrying about |
| 2399 | overwriting other allocated objects. Not a particularly great |
| 2400 | programming practice, but still sometimes useful. |
| 2401 | |
| 2402 | */ |
| 2403 | |
| 2404 | #if __STD_C |
| 2405 | size_t malloc_usable_size(Void_t* mem) |
| 2406 | #else |
| 2407 | size_t malloc_usable_size(mem) Void_t* mem; |
| 2408 | #endif |
| 2409 | { |
| 2410 | mchunkptr p; |
Kim Phillips | b052b60 | 2012-10-29 13:34:32 +0000 | [diff] [blame] | 2411 | if (mem == NULL) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2412 | return 0; |
| 2413 | else |
| 2414 | { |
| 2415 | p = mem2chunk(mem); |
| 2416 | if(!chunk_is_mmapped(p)) |
| 2417 | { |
| 2418 | if (!inuse(p)) return 0; |
| 2419 | check_inuse_chunk(p); |
| 2420 | return chunksize(p) - SIZE_SZ; |
| 2421 | } |
| 2422 | return chunksize(p) - 2*SIZE_SZ; |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2427 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2428 | |
| 2429 | /* Utility to update current_mallinfo for malloc_stats and mallinfo() */ |
| 2430 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2431 | #ifdef DEBUG |
Tom Rini | 03787a9 | 2023-02-27 17:08:34 -0500 | [diff] [blame] | 2432 | static void malloc_update_mallinfo(void) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2433 | { |
| 2434 | int i; |
| 2435 | mbinptr b; |
| 2436 | mchunkptr p; |
| 2437 | #ifdef DEBUG |
| 2438 | mchunkptr q; |
| 2439 | #endif |
| 2440 | |
| 2441 | INTERNAL_SIZE_T avail = chunksize(top); |
| 2442 | int navail = ((long)(avail) >= (long)MINSIZE)? 1 : 0; |
| 2443 | |
| 2444 | for (i = 1; i < NAV; ++i) |
| 2445 | { |
| 2446 | b = bin_at(i); |
| 2447 | for (p = last(b); p != b; p = p->bk) |
| 2448 | { |
| 2449 | #ifdef DEBUG |
| 2450 | check_free_chunk(p); |
| 2451 | for (q = next_chunk(p); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2452 | q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE; |
| 2453 | q = next_chunk(q)) |
| 2454 | check_inuse_chunk(q); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2455 | #endif |
| 2456 | avail += chunksize(p); |
| 2457 | navail++; |
| 2458 | } |
| 2459 | } |
| 2460 | |
| 2461 | current_mallinfo.ordblks = navail; |
| 2462 | current_mallinfo.uordblks = sbrked_mem - avail; |
| 2463 | current_mallinfo.fordblks = avail; |
| 2464 | current_mallinfo.hblks = n_mmaps; |
| 2465 | current_mallinfo.hblkhd = mmapped_mem; |
| 2466 | current_mallinfo.keepcost = chunksize(top); |
| 2467 | |
| 2468 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2469 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2470 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2471 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2472 | |
| 2473 | /* |
| 2474 | |
| 2475 | malloc_stats: |
| 2476 | |
| 2477 | Prints on the amount of space obtain from the system (both |
| 2478 | via sbrk and mmap), the maximum amount (which may be more than |
| 2479 | current if malloc_trim and/or munmap got called), the maximum |
| 2480 | number of simultaneous mmap regions used, and the current number |
| 2481 | of bytes allocated via malloc (or realloc, etc) but not yet |
| 2482 | freed. (Note that this is the number of bytes allocated, not the |
| 2483 | number requested. It will be larger than the number requested |
| 2484 | because of alignment and bookkeeping overhead.) |
| 2485 | |
| 2486 | */ |
| 2487 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2488 | #ifdef DEBUG |
Tom Rini | 03787a9 | 2023-02-27 17:08:34 -0500 | [diff] [blame] | 2489 | void malloc_stats(void) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2490 | { |
| 2491 | malloc_update_mallinfo(); |
| 2492 | printf("max system bytes = %10u\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2493 | (unsigned int)(max_total_mem)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2494 | printf("system bytes = %10u\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2495 | (unsigned int)(sbrked_mem + mmapped_mem)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2496 | printf("in use bytes = %10u\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2497 | (unsigned int)(current_mallinfo.uordblks + mmapped_mem)); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2498 | #if HAVE_MMAP |
| 2499 | printf("max mmap regions = %10u\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2500 | (unsigned int)max_n_mmaps); |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2501 | #endif |
| 2502 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2503 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2504 | |
| 2505 | /* |
| 2506 | mallinfo returns a copy of updated current mallinfo. |
| 2507 | */ |
| 2508 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2509 | #ifdef DEBUG |
Tom Rini | 03787a9 | 2023-02-27 17:08:34 -0500 | [diff] [blame] | 2510 | struct mallinfo mALLINFo(void) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2511 | { |
| 2512 | malloc_update_mallinfo(); |
| 2513 | return current_mallinfo; |
| 2514 | } |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2515 | #endif /* DEBUG */ |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2516 | |
| 2517 | |
Simon Glass | 7471cc7 | 2014-07-10 22:23:25 -0600 | [diff] [blame] | 2518 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2519 | |
| 2520 | /* |
| 2521 | mallopt: |
| 2522 | |
| 2523 | mallopt is the general SVID/XPG interface to tunable parameters. |
| 2524 | The format is to provide a (parameter-number, parameter-value) pair. |
| 2525 | mallopt then sets the corresponding parameter to the argument |
| 2526 | value if it can (i.e., so long as the value is meaningful), |
| 2527 | and returns 1 if successful else 0. |
| 2528 | |
| 2529 | See descriptions of tunable parameters above. |
| 2530 | |
| 2531 | */ |
| 2532 | |
| 2533 | #if __STD_C |
| 2534 | int mALLOPt(int param_number, int value) |
| 2535 | #else |
| 2536 | int mALLOPt(param_number, value) int param_number; int value; |
| 2537 | #endif |
| 2538 | { |
| 2539 | switch(param_number) |
| 2540 | { |
| 2541 | case M_TRIM_THRESHOLD: |
| 2542 | trim_threshold = value; return 1; |
| 2543 | case M_TOP_PAD: |
| 2544 | top_pad = value; return 1; |
| 2545 | case M_MMAP_THRESHOLD: |
| 2546 | mmap_threshold = value; return 1; |
| 2547 | case M_MMAP_MAX: |
| 2548 | #if HAVE_MMAP |
| 2549 | n_mmaps_max = value; return 1; |
| 2550 | #else |
| 2551 | if (value != 0) return 0; else n_mmaps_max = value; return 1; |
| 2552 | #endif |
| 2553 | |
| 2554 | default: |
| 2555 | return 0; |
| 2556 | } |
| 2557 | } |
| 2558 | |
Simon Glass | d1d087d | 2015-02-27 22:06:36 -0700 | [diff] [blame] | 2559 | int initf_malloc(void) |
| 2560 | { |
Simon Glass | adad2d0 | 2023-09-26 08:14:27 -0600 | [diff] [blame] | 2561 | #if CONFIG_IS_ENABLED(SYS_MALLOC_F) |
Simon Glass | d1d087d | 2015-02-27 22:06:36 -0700 | [diff] [blame] | 2562 | assert(gd->malloc_base); /* Set up by crt0.S */ |
Andy Yan | 1fa20e4d | 2017-07-24 17:43:34 +0800 | [diff] [blame] | 2563 | gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN); |
Simon Glass | d1d087d | 2015-02-27 22:06:36 -0700 | [diff] [blame] | 2564 | gd->malloc_ptr = 0; |
| 2565 | #endif |
| 2566 | |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
Simon Glass | 1a3e39b | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 2570 | void malloc_enable_testing(int max_allocs) |
| 2571 | { |
| 2572 | malloc_testing = true; |
| 2573 | malloc_max_allocs = max_allocs; |
| 2574 | } |
| 2575 | |
| 2576 | void malloc_disable_testing(void) |
| 2577 | { |
| 2578 | malloc_testing = false; |
| 2579 | } |
| 2580 | |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2581 | /* |
| 2582 | |
| 2583 | History: |
| 2584 | |
| 2585 | V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee) |
| 2586 | * return null for negative arguments |
| 2587 | * Added Several WIN32 cleanups from Martin C. Fong <mcfong@yahoo.com> |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2588 | * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h' |
| 2589 | (e.g. WIN32 platforms) |
| 2590 | * Cleanup up header file inclusion for WIN32 platforms |
| 2591 | * Cleanup code to avoid Microsoft Visual C++ compiler complaints |
| 2592 | * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing |
| 2593 | memory allocation routines |
| 2594 | * Set 'malloc_getpagesize' for WIN32 platforms (needs more work) |
| 2595 | * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2596 | usage of 'assert' in non-WIN32 code |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2597 | * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to |
| 2598 | avoid infinite loop |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2599 | * Always call 'fREe()' rather than 'free()' |
| 2600 | |
| 2601 | V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee) |
| 2602 | * Fixed ordering problem with boundary-stamping |
| 2603 | |
| 2604 | V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee) |
| 2605 | * Added pvalloc, as recommended by H.J. Liu |
| 2606 | * Added 64bit pointer support mainly from Wolfram Gloger |
| 2607 | * Added anonymously donated WIN32 sbrk emulation |
| 2608 | * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen |
| 2609 | * malloc_extend_top: fix mask error that caused wastage after |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2610 | foreign sbrks |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2611 | * Add linux mremap support code from HJ Liu |
| 2612 | |
| 2613 | V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) |
| 2614 | * Integrated most documentation with the code. |
| 2615 | * Add support for mmap, with help from |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2616 | Wolfram Gloger (Gloger@lrz.uni-muenchen.de). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2617 | * Use last_remainder in more cases. |
| 2618 | * Pack bins using idea from colin@nyx10.cs.du.edu |
| 2619 | * Use ordered bins instead of best-fit threshhold |
| 2620 | * Eliminate block-local decls to simplify tracing and debugging. |
| 2621 | * Support another case of realloc via move into top |
| 2622 | * Fix error occuring when initial sbrk_base not word-aligned. |
| 2623 | * Rely on page size for units instead of SBRK_UNIT to |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2624 | avoid surprises about sbrk alignment conventions. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2625 | * Add mallinfo, mallopt. Thanks to Raymond Nijssen |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2626 | (raymond@es.ele.tue.nl) for the suggestion. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2627 | * Add `pad' argument to malloc_trim and top_pad mallopt parameter. |
| 2628 | * More precautions for cases where other routines call sbrk, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2629 | courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2630 | * Added macros etc., allowing use in linux libc from |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2631 | H.J. Lu (hjl@gnu.ai.mit.edu) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2632 | * Inverted this history list |
| 2633 | |
| 2634 | V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee) |
| 2635 | * Re-tuned and fixed to behave more nicely with V2.6.0 changes. |
| 2636 | * Removed all preallocation code since under current scheme |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2637 | the work required to undo bad preallocations exceeds |
| 2638 | the work saved in good cases for most test programs. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2639 | * No longer use return list or unconsolidated bins since |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2640 | no scheme using them consistently outperforms those that don't |
| 2641 | given above changes. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2642 | * Use best fit for very large chunks to prevent some worst-cases. |
| 2643 | * Added some support for debugging |
| 2644 | |
| 2645 | V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee) |
| 2646 | * Removed footers when chunks are in use. Thanks to |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2647 | Paul Wilson (wilson@cs.texas.edu) for the suggestion. |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2648 | |
| 2649 | V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) |
| 2650 | * Added malloc_trim, with help from Wolfram Gloger |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2651 | (wmglo@Dent.MED.Uni-Muenchen.DE). |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2652 | |
| 2653 | V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g) |
| 2654 | |
| 2655 | V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g) |
| 2656 | * realloc: try to expand in both directions |
| 2657 | * malloc: swap order of clean-bin strategy; |
| 2658 | * realloc: only conditionally expand backwards |
| 2659 | * Try not to scavenge used bins |
| 2660 | * Use bin counts as a guide to preallocation |
| 2661 | * Occasionally bin return list chunks in first scan |
| 2662 | * Add a few optimizations from colin@nyx10.cs.du.edu |
| 2663 | |
| 2664 | V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g) |
| 2665 | * faster bin computation & slightly different binning |
| 2666 | * merged all consolidations to one part of malloc proper |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2667 | (eliminating old malloc_find_space & malloc_clean_bin) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2668 | * Scan 2 returns chunks (not just 1) |
| 2669 | * Propagate failure in realloc if malloc returns 0 |
| 2670 | * Add stuff to allow compilation on non-ANSI compilers |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2671 | from kpv@research.att.com |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2672 | |
| 2673 | V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) |
| 2674 | * removed potential for odd address access in prev_chunk |
| 2675 | * removed dependency on getpagesize.h |
| 2676 | * misc cosmetics and a bit more internal documentation |
| 2677 | * anticosmetics: mangled names in macros to evade debugger strangeness |
| 2678 | * tested on sparc, hp-700, dec-mips, rs6000 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2679 | with gcc & native cc (hp, dec only) allowing |
| 2680 | Detlefs & Zorn comparison study (in SIGPLAN Notices.) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2681 | |
| 2682 | Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) |
| 2683 | * Based loosely on libg++-1.2X malloc. (It retains some of the overall |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2684 | structure of old version, but most details differ.) |
wdenk | 217c9da | 2002-10-25 20:35:49 +0000 | [diff] [blame] | 2685 | |
| 2686 | */ |