dlmalloc.c 74 KB

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