dlmalloc.c 72 KB

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