dlmalloc.c 72 KB

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