dlmalloc.c 72 KB

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