gmalloc.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /* DO NOT EDIT THIS FILE -- it is automagically generated. -*- C -*- */
  2. #define _MALLOC_INTERNAL
  3. /* The malloc headers and source files from the C library follow here. */
  4. /* Declarations for `malloc' and friends.
  5. Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
  6. Written May 1989 by Mike Haertel.
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; see the file COPYING.LIB. If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.
  19. The author may be reached (Email) at the address mike@ai.mit.edu,
  20. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  21. #ifndef _MALLOC_H
  22. #define _MALLOC_H 1
  23. #ifdef _MALLOC_INTERNAL
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
  28. #include <string.h>
  29. #else
  30. #ifndef memset
  31. #define memset(s, zero, n) bzero ((s), (n))
  32. #endif
  33. #ifndef memcpy
  34. #define memcpy(d, s, n) bcopy ((s), (d), (n))
  35. #endif
  36. #endif
  37. #if defined (__GNU_LIBRARY__) || (defined (__STDC__) && __STDC__)
  38. #include <limits.h>
  39. #else
  40. #ifndef CHAR_BIT
  41. #define CHAR_BIT 8
  42. #endif
  43. #endif
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #endif /* _MALLOC_INTERNAL. */
  48. #ifdef __cplusplus
  49. extern "C"
  50. {
  51. #endif
  52. #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
  53. #undef __P
  54. #define __P(args) args
  55. #undef __ptr_t
  56. #define __ptr_t void *
  57. #else /* Not C++ or ANSI C. */
  58. #undef __P
  59. #define __P(args) ()
  60. #undef const
  61. #define const
  62. #undef __ptr_t
  63. #define __ptr_t char *
  64. #endif /* C++ or ANSI C. */
  65. #if defined (__STDC__) && __STDC__
  66. #include <stddef.h>
  67. #define __malloc_size_t size_t
  68. #define __malloc_ptrdiff_t ptrdiff_t
  69. #else
  70. #define __malloc_size_t unsigned int
  71. #define __malloc_ptrdiff_t int
  72. #endif
  73. #ifndef NULL
  74. #define NULL 0
  75. #endif
  76. /* Allocate SIZE bytes of memory. */
  77. extern __ptr_t malloc __P ((__malloc_size_t __size));
  78. /* Re-allocate the previously allocated block
  79. in __ptr_t, making the new block SIZE bytes long. */
  80. extern __ptr_t realloc __P ((__ptr_t __ptr, __malloc_size_t __size));
  81. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
  82. extern __ptr_t calloc __P ((__malloc_size_t __nmemb, __malloc_size_t __size));
  83. /* Free a block allocated by `malloc', `realloc' or `calloc'. */
  84. extern void free __P ((__ptr_t __ptr));
  85. /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
  86. extern __ptr_t memalign __P ((__malloc_size_t __alignment,
  87. __malloc_size_t __size));
  88. /* Allocate SIZE bytes on a page boundary. */
  89. extern __ptr_t valloc __P ((__malloc_size_t __size));
  90. #ifdef _MALLOC_INTERNAL
  91. /* The allocator divides the heap into blocks of fixed size; large
  92. requests receive one or more whole blocks, and small requests
  93. receive a fragment of a block. Fragment sizes are powers of two,
  94. and all fragments of a block are the same size. When all the
  95. fragments in a block have been freed, the block itself is freed. */
  96. #define INT_BIT (CHAR_BIT * sizeof(int))
  97. #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
  98. #define BLOCKSIZE (1 << BLOCKLOG)
  99. #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  100. /* Determine the amount of memory spanned by the initial heap table
  101. (not an absolute limit). */
  102. #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
  103. /* Number of contiguous free blocks allowed to build up at the end of
  104. memory before they will be returned to the system. */
  105. #define FINAL_FREE_BLOCKS 8
  106. /* Data structure giving per-block information. */
  107. typedef union
  108. {
  109. /* Heap information for a busy block. */
  110. struct
  111. {
  112. /* Zero for a large (multiblock) object, or positive giving the
  113. logarithm to the base two of the fragment size. */
  114. int type;
  115. union
  116. {
  117. struct
  118. {
  119. __malloc_size_t nfree; /* Free frags in a fragmented block. */
  120. __malloc_size_t first; /* First free fragment of the block. */
  121. } frag;
  122. /* For a large object, in its first block, this has the number
  123. of blocks in the object. In the other blocks, this has a
  124. negative number which says how far back the first block is. */
  125. __malloc_ptrdiff_t size;
  126. } info;
  127. } busy;
  128. /* Heap information for a free block
  129. (that may be the first of a free cluster). */
  130. struct
  131. {
  132. __malloc_size_t size; /* Size (in blocks) of a free cluster. */
  133. __malloc_size_t next; /* Index of next free cluster. */
  134. __malloc_size_t prev; /* Index of previous free cluster. */
  135. } free;
  136. } malloc_info;
  137. /* Pointer to first block of the heap. */
  138. extern char *_heapbase;
  139. /* Table indexed by block number giving per-block information. */
  140. extern malloc_info *_heapinfo;
  141. /* Address to block number and vice versa. */
  142. #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  143. #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
  144. /* Current search index for the heap table. */
  145. extern __malloc_size_t _heapindex;
  146. /* Limit of valid info table indices. */
  147. extern __malloc_size_t _heaplimit;
  148. /* Doubly linked lists of free fragments. */
  149. struct list
  150. {
  151. struct list *next;
  152. struct list *prev;
  153. };
  154. /* Free list headers for each fragment size. */
  155. extern struct list _fraghead[];
  156. /* List of blocks allocated with `memalign' (or `valloc'). */
  157. struct alignlist
  158. {
  159. struct alignlist *next;
  160. __ptr_t aligned; /* The address that memaligned returned. */
  161. __ptr_t exact; /* The address that malloc returned. */
  162. };
  163. extern struct alignlist *_aligned_blocks;
  164. /* Instrumentation. */
  165. extern __malloc_size_t _chunks_used;
  166. extern __malloc_size_t _bytes_used;
  167. extern __malloc_size_t _chunks_free;
  168. extern __malloc_size_t _bytes_free;
  169. /* Internal version of `free' used in `morecore' (malloc.c). */
  170. extern void _free_internal __P ((__ptr_t __ptr));
  171. #endif /* _MALLOC_INTERNAL. */
  172. /* Given an address in the middle of a malloc'd object,
  173. return the address of the beginning of the object. */
  174. extern __ptr_t malloc_find_object_address __P ((__ptr_t __ptr));
  175. /* Underlying allocation function; successive calls should
  176. return contiguous pieces of memory. */
  177. extern __ptr_t (*__morecore) __P ((__malloc_ptrdiff_t __size));
  178. /* Default value of `__morecore'. */
  179. extern __ptr_t __default_morecore __P ((__malloc_ptrdiff_t __size));
  180. /* If not NULL, this function is called after each time
  181. `__morecore' is called to increase the data size. */
  182. extern void (*__after_morecore_hook) __P ((void));
  183. /* Nonzero if `malloc' has been called and done its initialization. */
  184. extern int __malloc_initialized;
  185. /* Hooks for debugging versions. */
  186. extern void (*__malloc_initialize_hook) __P ((void));
  187. extern void (*__free_hook) __P ((__ptr_t __ptr));
  188. extern __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
  189. extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
  190. extern __ptr_t (*__memalign_hook) __P ((__malloc_size_t __size,
  191. __malloc_size_t __alignment));
  192. /* Return values for `mprobe': these are the kinds of inconsistencies that
  193. `mcheck' enables detection of. */
  194. enum mcheck_status
  195. {
  196. MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
  197. MCHECK_OK, /* Block is fine. */
  198. MCHECK_FREE, /* Block freed twice. */
  199. MCHECK_HEAD, /* Memory before the block was clobbered. */
  200. MCHECK_TAIL /* Memory after the block was clobbered. */
  201. };
  202. /* Activate a standard collection of debugging hooks. This must be called
  203. before `malloc' is ever called. ABORTFUNC is called with an error code
  204. (see enum above) when an inconsistency is detected. If ABORTFUNC is
  205. null, the standard function prints on stderr and then calls `abort'. */
  206. extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
  207. /* Check for aberrations in a particular malloc'd block. You must have
  208. called `mcheck' already. These are the same checks that `mcheck' does
  209. when you free or reallocate a block. */
  210. extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
  211. /* Activate a standard collection of tracing hooks. */
  212. extern void mtrace __P ((void));
  213. extern void muntrace __P ((void));
  214. /* Statistics available to the user. */
  215. struct mstats
  216. {
  217. __malloc_size_t bytes_total; /* Total size of the heap. */
  218. __malloc_size_t chunks_used; /* Chunks allocated by the user. */
  219. __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
  220. __malloc_size_t chunks_free; /* Chunks in the free list. */
  221. __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
  222. };
  223. /* Pick up the current statistics. */
  224. extern struct mstats mstats __P ((void));
  225. /* Call WARNFUN with a warning message when memory usage is high. */
  226. extern void memory_warnings __P ((__ptr_t __start,
  227. void (*__warnfun) __P ((const char *))));
  228. /* Relocating allocator. */
  229. /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
  230. extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
  231. /* Free the storage allocated in HANDLEPTR. */
  232. extern void r_alloc_free __P ((__ptr_t *__handleptr));
  233. /* Adjust the block at HANDLEPTR to be SIZE bytes long. */
  234. extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #endif /* malloc.h */
  239. /* Allocate memory on a page boundary.
  240. Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  241. This library is free software; you can redistribute it and/or
  242. modify it under the terms of the GNU Library General Public License as
  243. published by the Free Software Foundation; either version 2 of the
  244. License, or (at your option) any later version.
  245. This library is distributed in the hope that it will be useful,
  246. but WITHOUT ANY WARRANTY; without even the implied warranty of
  247. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  248. Library General Public License for more details.
  249. You should have received a copy of the GNU Library General Public
  250. License along with this library; see the file COPYING.LIB. If
  251. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  252. Cambridge, MA 02139, USA.
  253. The author may be reached (Email) at the address mike@ai.mit.edu,
  254. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  255. #if defined (__GNU_LIBRARY__) || defined (_LIBC)
  256. #include <stddef.h>
  257. #include <sys/cdefs.h>
  258. extern size_t __getpagesize __P ((void));
  259. #else
  260. #include "getpagesize.h"
  261. #define __getpagesize() getpagesize()
  262. #endif
  263. #ifndef _MALLOC_INTERNAL
  264. #define _MALLOC_INTERNAL
  265. #include <malloc.h>
  266. #endif
  267. static __malloc_size_t pagesize;
  268. __ptr_t
  269. valloc (size)
  270. __malloc_size_t size;
  271. {
  272. if (pagesize == 0)
  273. pagesize = __getpagesize ();
  274. return memalign (pagesize, size);
  275. }
  276. /* Memory allocator `malloc'.
  277. Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  278. Written May 1989 by Mike Haertel.
  279. This library is free software; you can redistribute it and/or
  280. modify it under the terms of the GNU Library General Public License as
  281. published by the Free Software Foundation; either version 2 of the
  282. License, or (at your option) any later version.
  283. This library is distributed in the hope that it will be useful,
  284. but WITHOUT ANY WARRANTY; without even the implied warranty of
  285. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  286. Library General Public License for more details.
  287. You should have received a copy of the GNU Library General Public
  288. License along with this library; see the file COPYING.LIB. If
  289. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  290. Cambridge, MA 02139, USA.
  291. The author may be reached (Email) at the address mike@ai.mit.edu,
  292. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  293. #ifndef _MALLOC_INTERNAL
  294. #define _MALLOC_INTERNAL
  295. #include <malloc.h>
  296. #endif
  297. /* How to really get more memory. */
  298. __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
  299. /* Debugging hook for `malloc'. */
  300. __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
  301. /* Pointer to the base of the first block. */
  302. char *_heapbase;
  303. /* Block information table. Allocated with align/__free (not malloc/free). */
  304. malloc_info *_heapinfo;
  305. /* Number of info entries. */
  306. static __malloc_size_t heapsize;
  307. /* Search index in the info table. */
  308. __malloc_size_t _heapindex;
  309. /* Limit of valid info table indices. */
  310. __malloc_size_t _heaplimit;
  311. /* Free lists for each fragment size. */
  312. struct list _fraghead[BLOCKLOG];
  313. /* Instrumentation. */
  314. __malloc_size_t _chunks_used;
  315. __malloc_size_t _bytes_used;
  316. __malloc_size_t _chunks_free;
  317. __malloc_size_t _bytes_free;
  318. /* Are you experienced? */
  319. int __malloc_initialized;
  320. void (*__malloc_initialize_hook) __P ((void));
  321. void (*__after_morecore_hook) __P ((void));
  322. /* Aligned allocation. */
  323. static __ptr_t align __P ((__malloc_size_t));
  324. static __ptr_t
  325. align (size)
  326. __malloc_size_t size;
  327. {
  328. __ptr_t result;
  329. unsigned long int adj;
  330. result = (*__morecore) (size);
  331. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  332. (char *) NULL)) % BLOCKSIZE;
  333. if (adj != 0)
  334. {
  335. adj = BLOCKSIZE - adj;
  336. (void) (*__morecore) (adj);
  337. result = (char *) result + adj;
  338. }
  339. if (__after_morecore_hook)
  340. (*__after_morecore_hook) ();
  341. return result;
  342. }
  343. /* Set everything up and remember that we have. */
  344. static int initialize __P ((void));
  345. static int
  346. initialize ()
  347. {
  348. if (__malloc_initialize_hook)
  349. (*__malloc_initialize_hook) ();
  350. heapsize = HEAP / BLOCKSIZE;
  351. _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
  352. if (_heapinfo == NULL)
  353. return 0;
  354. memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
  355. _heapinfo[0].free.size = 0;
  356. _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
  357. _heapindex = 0;
  358. _heapbase = (char *) _heapinfo;
  359. /* Account for the _heapinfo block itself in the statistics. */
  360. _bytes_used = heapsize * sizeof (malloc_info);
  361. _chunks_used = 1;
  362. __malloc_initialized = 1;
  363. return 1;
  364. }
  365. /* Get neatly aligned memory, initializing or
  366. growing the heap info table as necessary. */
  367. static __ptr_t morecore __P ((__malloc_size_t));
  368. static __ptr_t
  369. morecore (size)
  370. __malloc_size_t size;
  371. {
  372. __ptr_t result;
  373. malloc_info *newinfo, *oldinfo;
  374. __malloc_size_t newsize;
  375. result = align (size);
  376. if (result == NULL)
  377. return NULL;
  378. /* Check if we need to grow the info table. */
  379. if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
  380. {
  381. newsize = heapsize;
  382. while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize)
  383. newsize *= 2;
  384. newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
  385. if (newinfo == NULL)
  386. {
  387. (*__morecore) (-size);
  388. return NULL;
  389. }
  390. memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
  391. memset (&newinfo[heapsize], 0,
  392. (newsize - heapsize) * sizeof (malloc_info));
  393. oldinfo = _heapinfo;
  394. newinfo[BLOCK (oldinfo)].busy.type = 0;
  395. newinfo[BLOCK (oldinfo)].busy.info.size
  396. = BLOCKIFY (heapsize * sizeof (malloc_info));
  397. _heapinfo = newinfo;
  398. /* Account for the _heapinfo block itself in the statistics. */
  399. _bytes_used += newsize * sizeof (malloc_info);
  400. ++_chunks_used;
  401. _free_internal (oldinfo);
  402. heapsize = newsize;
  403. }
  404. _heaplimit = BLOCK ((char *) result + size);
  405. return result;
  406. }
  407. /* Allocate memory from the heap. */
  408. __ptr_t
  409. malloc (size)
  410. __malloc_size_t size;
  411. {
  412. __ptr_t result;
  413. __malloc_size_t block, blocks, lastblocks, start;
  414. register __malloc_size_t i;
  415. struct list *next;
  416. /* ANSI C allows `malloc (0)' to either return NULL, or to return a
  417. valid address you can realloc and free (though not dereference).
  418. It turns out that some extant code (sunrpc, at least Ultrix's version)
  419. expects `malloc (0)' to return non-NULL and breaks otherwise.
  420. Be compatible. */
  421. #if 0
  422. if (size == 0)
  423. return NULL;
  424. #endif
  425. if (__malloc_hook != NULL)
  426. return (*__malloc_hook) (size);
  427. if (!__malloc_initialized)
  428. if (!initialize ())
  429. return NULL;
  430. if (size < sizeof (struct list))
  431. size = sizeof (struct list);
  432. #ifdef SUNOS_LOCALTIME_BUG
  433. if (size < 16)
  434. size = 16;
  435. #endif
  436. /* Determine the allocation policy based on the request size. */
  437. if (size <= BLOCKSIZE / 2)
  438. {
  439. /* Small allocation to receive a fragment of a block.
  440. Determine the logarithm to base two of the fragment size. */
  441. register __malloc_size_t log = 1;
  442. --size;
  443. while ((size /= 2) != 0)
  444. ++log;
  445. /* Look in the fragment lists for a
  446. free fragment of the desired size. */
  447. next = _fraghead[log].next;
  448. if (next != NULL)
  449. {
  450. /* There are free fragments of this size.
  451. Pop a fragment out of the fragment list and return it.
  452. Update the block's nfree and first counters. */
  453. result = (__ptr_t) next;
  454. next->prev->next = next->next;
  455. if (next->next != NULL)
  456. next->next->prev = next->prev;
  457. block = BLOCK (result);
  458. if (--_heapinfo[block].busy.info.frag.nfree != 0)
  459. _heapinfo[block].busy.info.frag.first = (unsigned long int)
  460. ((unsigned long int) ((char *) next->next - (char *) NULL)
  461. % BLOCKSIZE) >> log;
  462. /* Update the statistics. */
  463. ++_chunks_used;
  464. _bytes_used += 1 << log;
  465. --_chunks_free;
  466. _bytes_free -= 1 << log;
  467. }
  468. else
  469. {
  470. /* No free fragments of the desired size, so get a new block
  471. and break it into fragments, returning the first. */
  472. result = malloc (BLOCKSIZE);
  473. if (result == NULL)
  474. return NULL;
  475. /* Link all fragments but the first into the free list. */
  476. for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
  477. {
  478. next = (struct list *) ((char *) result + (i << log));
  479. next->next = _fraghead[log].next;
  480. next->prev = &_fraghead[log];
  481. next->prev->next = next;
  482. if (next->next != NULL)
  483. next->next->prev = next;
  484. }
  485. /* Initialize the nfree and first counters for this block. */
  486. block = BLOCK (result);
  487. _heapinfo[block].busy.type = log;
  488. _heapinfo[block].busy.info.frag.nfree = i - 1;
  489. _heapinfo[block].busy.info.frag.first = i - 1;
  490. _chunks_free += (BLOCKSIZE >> log) - 1;
  491. _bytes_free += BLOCKSIZE - (1 << log);
  492. _bytes_used -= BLOCKSIZE - (1 << log);
  493. }
  494. }
  495. else
  496. {
  497. /* Large allocation to receive one or more blocks.
  498. Search the free list in a circle starting at the last place visited.
  499. If we loop completely around without finding a large enough
  500. space we will have to get more memory from the system. */
  501. blocks = BLOCKIFY (size);
  502. start = block = _heapindex;
  503. while (_heapinfo[block].free.size < blocks)
  504. {
  505. block = _heapinfo[block].free.next;
  506. if (block == start)
  507. {
  508. /* Need to get more from the system. Check to see if
  509. the new core will be contiguous with the final free
  510. block; if so we don't need to get as much. */
  511. block = _heapinfo[0].free.prev;
  512. lastblocks = _heapinfo[block].free.size;
  513. if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
  514. (*__morecore) (0) == ADDRESS (block + lastblocks) &&
  515. (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
  516. {
  517. /* Which block we are extending (the `final free
  518. block' referred to above) might have changed, if
  519. it got combined with a freed info table. */
  520. block = _heapinfo[0].free.prev;
  521. _heapinfo[block].free.size += (blocks - lastblocks);
  522. _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
  523. continue;
  524. }
  525. result = morecore (blocks * BLOCKSIZE);
  526. if (result == NULL)
  527. return NULL;
  528. block = BLOCK (result);
  529. _heapinfo[block].busy.type = 0;
  530. _heapinfo[block].busy.info.size = blocks;
  531. ++_chunks_used;
  532. _bytes_used += blocks * BLOCKSIZE;
  533. return result;
  534. }
  535. }
  536. /* At this point we have found a suitable free list entry.
  537. Figure out how to remove what we need from the list. */
  538. result = ADDRESS (block);
  539. if (_heapinfo[block].free.size > blocks)
  540. {
  541. /* The block we found has a bit left over,
  542. so relink the tail end back into the free list. */
  543. _heapinfo[block + blocks].free.size
  544. = _heapinfo[block].free.size - blocks;
  545. _heapinfo[block + blocks].free.next
  546. = _heapinfo[block].free.next;
  547. _heapinfo[block + blocks].free.prev
  548. = _heapinfo[block].free.prev;
  549. _heapinfo[_heapinfo[block].free.prev].free.next
  550. = _heapinfo[_heapinfo[block].free.next].free.prev
  551. = _heapindex = block + blocks;
  552. }
  553. else
  554. {
  555. /* The block exactly matches our requirements,
  556. so just remove it from the list. */
  557. _heapinfo[_heapinfo[block].free.next].free.prev
  558. = _heapinfo[block].free.prev;
  559. _heapinfo[_heapinfo[block].free.prev].free.next
  560. = _heapindex = _heapinfo[block].free.next;
  561. --_chunks_free;
  562. }
  563. _heapinfo[block].busy.type = 0;
  564. _heapinfo[block].busy.info.size = blocks;
  565. ++_chunks_used;
  566. _bytes_used += blocks * BLOCKSIZE;
  567. _bytes_free -= blocks * BLOCKSIZE;
  568. /* Mark all the blocks of the object just allocated except for the
  569. first with a negative number so you can find the first block by
  570. adding that adjustment. */
  571. while (--blocks > 0)
  572. _heapinfo[block + blocks].busy.info.size = -blocks;
  573. }
  574. return result;
  575. }
  576. #ifndef _LIBC
  577. /* On some ANSI C systems, some libc functions call _malloc, _free
  578. and _realloc. Make them use the GNU functions. */
  579. __ptr_t
  580. _malloc (size)
  581. __malloc_size_t size;
  582. {
  583. return malloc (size);
  584. }
  585. void
  586. _free (ptr)
  587. __ptr_t ptr;
  588. {
  589. free (ptr);
  590. }
  591. __ptr_t
  592. _realloc (ptr, size)
  593. __ptr_t ptr;
  594. __malloc_size_t size;
  595. {
  596. return realloc (ptr, size);
  597. }
  598. #endif
  599. /* Free a block of memory allocated by `malloc'.
  600. Copyright 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
  601. Written May 1989 by Mike Haertel.
  602. This library is free software; you can redistribute it and/or
  603. modify it under the terms of the GNU Library General Public License as
  604. published by the Free Software Foundation; either version 2 of the
  605. License, or (at your option) any later version.
  606. This library is distributed in the hope that it will be useful,
  607. but WITHOUT ANY WARRANTY; without even the implied warranty of
  608. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  609. Library General Public License for more details.
  610. You should have received a copy of the GNU Library General Public
  611. License along with this library; see the file COPYING.LIB. If
  612. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  613. Cambridge, MA 02139, USA.
  614. The author may be reached (Email) at the address mike@ai.mit.edu,
  615. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  616. #ifndef _MALLOC_INTERNAL
  617. #define _MALLOC_INTERNAL
  618. #include <malloc.h>
  619. #endif
  620. /* Debugging hook for free. */
  621. void (*__free_hook) __P ((__ptr_t __ptr));
  622. /* List of blocks allocated by memalign. */
  623. struct alignlist *_aligned_blocks = NULL;
  624. /* Return memory to the heap.
  625. Like `free' but don't call a __free_hook if there is one. */
  626. void
  627. _free_internal (ptr)
  628. __ptr_t ptr;
  629. {
  630. int type;
  631. __malloc_size_t block, blocks;
  632. register __malloc_size_t i;
  633. struct list *prev, *next;
  634. block = BLOCK (ptr);
  635. type = _heapinfo[block].busy.type;
  636. switch (type)
  637. {
  638. case 0:
  639. /* Get as many statistics as early as we can. */
  640. --_chunks_used;
  641. _bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
  642. _bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
  643. /* Find the free cluster previous to this one in the free list.
  644. Start searching at the last block referenced; this may benefit
  645. programs with locality of allocation. */
  646. i = _heapindex;
  647. if (i > block)
  648. while (i > block)
  649. i = _heapinfo[i].free.prev;
  650. else
  651. {
  652. do
  653. i = _heapinfo[i].free.next;
  654. while (i > 0 && i < block);
  655. i = _heapinfo[i].free.prev;
  656. }
  657. /* Determine how to link this block into the free list. */
  658. if (block == i + _heapinfo[i].free.size)
  659. {
  660. /* Coalesce this block with its predecessor. */
  661. _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
  662. block = i;
  663. }
  664. else
  665. {
  666. /* Really link this block back into the free list. */
  667. _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
  668. _heapinfo[block].free.next = _heapinfo[i].free.next;
  669. _heapinfo[block].free.prev = i;
  670. _heapinfo[i].free.next = block;
  671. _heapinfo[_heapinfo[block].free.next].free.prev = block;
  672. ++_chunks_free;
  673. }
  674. /* Now that the block is linked in, see if we can coalesce it
  675. with its successor (by deleting its successor from the list
  676. and adding in its size). */
  677. if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
  678. {
  679. _heapinfo[block].free.size
  680. += _heapinfo[_heapinfo[block].free.next].free.size;
  681. _heapinfo[block].free.next
  682. = _heapinfo[_heapinfo[block].free.next].free.next;
  683. _heapinfo[_heapinfo[block].free.next].free.prev = block;
  684. --_chunks_free;
  685. }
  686. /* Now see if we can return stuff to the system. */
  687. blocks = _heapinfo[block].free.size;
  688. if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
  689. && (*__morecore) (0) == ADDRESS (block + blocks))
  690. {
  691. register __malloc_size_t bytes = blocks * BLOCKSIZE;
  692. _heaplimit -= blocks;
  693. (*__morecore) (-bytes);
  694. _heapinfo[_heapinfo[block].free.prev].free.next
  695. = _heapinfo[block].free.next;
  696. _heapinfo[_heapinfo[block].free.next].free.prev
  697. = _heapinfo[block].free.prev;
  698. block = _heapinfo[block].free.prev;
  699. --_chunks_free;
  700. _bytes_free -= bytes;
  701. }
  702. /* Set the next search to begin at this block. */
  703. _heapindex = block;
  704. break;
  705. default:
  706. /* Do some of the statistics. */
  707. --_chunks_used;
  708. _bytes_used -= 1 << type;
  709. ++_chunks_free;
  710. _bytes_free += 1 << type;
  711. /* Get the address of the first free fragment in this block. */
  712. prev = (struct list *) ((char *) ADDRESS (block) +
  713. (_heapinfo[block].busy.info.frag.first << type));
  714. if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
  715. {
  716. /* If all fragments of this block are free, remove them
  717. from the fragment list and free the whole block. */
  718. next = prev;
  719. for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
  720. next = next->next;
  721. prev->prev->next = next;
  722. if (next != NULL)
  723. next->prev = prev->prev;
  724. _heapinfo[block].busy.type = 0;
  725. _heapinfo[block].busy.info.size = 1;
  726. /* Keep the statistics accurate. */
  727. ++_chunks_used;
  728. _bytes_used += BLOCKSIZE;
  729. _chunks_free -= BLOCKSIZE >> type;
  730. _bytes_free -= BLOCKSIZE;
  731. free (ADDRESS (block));
  732. }
  733. else if (_heapinfo[block].busy.info.frag.nfree != 0)
  734. {
  735. /* If some fragments of this block are free, link this
  736. fragment into the fragment list after the first free
  737. fragment of this block. */
  738. next = (struct list *) ptr;
  739. next->next = prev->next;
  740. next->prev = prev;
  741. prev->next = next;
  742. if (next->next != NULL)
  743. next->next->prev = next;
  744. ++_heapinfo[block].busy.info.frag.nfree;
  745. }
  746. else
  747. {
  748. /* No fragments of this block are free, so link this
  749. fragment into the fragment list and announce that
  750. it is the first free fragment of this block. */
  751. prev = (struct list *) ptr;
  752. _heapinfo[block].busy.info.frag.nfree = 1;
  753. _heapinfo[block].busy.info.frag.first = (unsigned long int)
  754. ((unsigned long int) ((char *) ptr - (char *) NULL)
  755. % BLOCKSIZE >> type);
  756. prev->next = _fraghead[type].next;
  757. prev->prev = &_fraghead[type];
  758. prev->prev->next = prev;
  759. if (prev->next != NULL)
  760. prev->next->prev = prev;
  761. }
  762. break;
  763. }
  764. }
  765. /* Return memory to the heap. */
  766. void
  767. free (ptr)
  768. __ptr_t ptr;
  769. {
  770. register struct alignlist *l;
  771. if (ptr == NULL)
  772. return;
  773. for (l = _aligned_blocks; l != NULL; l = l->next)
  774. if (l->aligned == ptr)
  775. {
  776. l->aligned = NULL; /* Mark the slot in the list as free. */
  777. ptr = l->exact;
  778. break;
  779. }
  780. if (__free_hook != NULL)
  781. (*__free_hook) (ptr);
  782. else
  783. _free_internal (ptr);
  784. }
  785. /* Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
  786. This file is part of the GNU C Library.
  787. The GNU C Library is free software; you can redistribute it and/or
  788. modify it under the terms of the GNU Library General Public License as
  789. published by the Free Software Foundation; either version 2 of the
  790. License, or (at your option) any later version.
  791. The GNU C Library is distributed in the hope that it will be useful,
  792. but WITHOUT ANY WARRANTY; without even the implied warranty of
  793. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  794. Library General Public License for more details.
  795. You should have received a copy of the GNU Library General Public
  796. License along with the GNU C Library; see the file COPYING.LIB. If
  797. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  798. Cambridge, MA 02139, USA. */
  799. #ifndef _MALLOC_INTERNAL
  800. #define _MALLOC_INTERNAL
  801. #include <malloc.h>
  802. #endif
  803. #ifdef _LIBC
  804. #include <ansidecl.h>
  805. #include <gnu-stabs.h>
  806. #undef cfree
  807. function_alias(cfree, free, void, (ptr),
  808. DEFUN(cfree, (ptr), PTR ptr))
  809. #else
  810. void
  811. cfree (ptr)
  812. __ptr_t ptr;
  813. {
  814. free (ptr);
  815. }
  816. #endif
  817. /* Change the size of a block allocated by `malloc'.
  818. Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  819. Written May 1989 by Mike Haertel.
  820. This library is free software; you can redistribute it and/or
  821. modify it under the terms of the GNU Library General Public License as
  822. published by the Free Software Foundation; either version 2 of the
  823. License, or (at your option) any later version.
  824. This library is distributed in the hope that it will be useful,
  825. but WITHOUT ANY WARRANTY; without even the implied warranty of
  826. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  827. Library General Public License for more details.
  828. You should have received a copy of the GNU Library General Public
  829. License along with this library; see the file COPYING.LIB. If
  830. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  831. Cambridge, MA 02139, USA.
  832. The author may be reached (Email) at the address mike@ai.mit.edu,
  833. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  834. #ifndef _MALLOC_INTERNAL
  835. #define _MALLOC_INTERNAL
  836. #include <malloc.h>
  837. #endif
  838. #if (defined (MEMMOVE_MISSING) || \
  839. !defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
  840. /* Snarfed directly from Emacs src/dispnew.c:
  841. XXX Should use system bcopy if it handles overlap. */
  842. #ifndef emacs
  843. /* Like bcopy except never gets confused by overlap. */
  844. static void
  845. safe_bcopy (from, to, size)
  846. char *from, *to;
  847. int size;
  848. {
  849. if (size <= 0 || from == to)
  850. return;
  851. /* If the source and destination don't overlap, then bcopy can
  852. handle it. If they do overlap, but the destination is lower in
  853. memory than the source, we'll assume bcopy can handle that. */
  854. if (to < from || from + size <= to)
  855. bcopy (from, to, size);
  856. /* Otherwise, we'll copy from the end. */
  857. else
  858. {
  859. register char *endf = from + size;
  860. register char *endt = to + size;
  861. /* If TO - FROM is large, then we should break the copy into
  862. nonoverlapping chunks of TO - FROM bytes each. However, if
  863. TO - FROM is small, then the bcopy function call overhead
  864. makes this not worth it. The crossover point could be about
  865. anywhere. Since I don't think the obvious copy loop is too
  866. bad, I'm trying to err in its favor. */
  867. if (to - from < 64)
  868. {
  869. do
  870. *--endt = *--endf;
  871. while (endf != from);
  872. }
  873. else
  874. {
  875. for (;;)
  876. {
  877. endt -= (to - from);
  878. endf -= (to - from);
  879. if (endt < to)
  880. break;
  881. bcopy (endf, endt, to - from);
  882. }
  883. /* If SIZE wasn't a multiple of TO - FROM, there will be a
  884. little left over. The amount left over is
  885. (endt + (to - from)) - to, which is endt - from. */
  886. bcopy (from, to, endt - from);
  887. }
  888. }
  889. }
  890. #endif /* Not emacs. */
  891. #define memmove(to, from, size) safe_bcopy ((from), (to), (size))
  892. #endif
  893. #define min(A, B) ((A) < (B) ? (A) : (B))
  894. /* Debugging hook for realloc. */
  895. __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
  896. /* Resize the given region to the new size, returning a pointer
  897. to the (possibly moved) region. This is optimized for speed;
  898. some benchmarks seem to indicate that greater compactness is
  899. achieved by unconditionally allocating and copying to a
  900. new region. This module has incestuous knowledge of the
  901. internals of both free and malloc. */
  902. __ptr_t
  903. realloc (ptr, size)
  904. __ptr_t ptr;
  905. __malloc_size_t size;
  906. {
  907. __ptr_t result;
  908. int type;
  909. __malloc_size_t block, blocks, oldlimit;
  910. if (size == 0)
  911. {
  912. free (ptr);
  913. return malloc (0);
  914. }
  915. else if (ptr == NULL)
  916. return malloc (size);
  917. if (__realloc_hook != NULL)
  918. return (*__realloc_hook) (ptr, size);
  919. block = BLOCK (ptr);
  920. type = _heapinfo[block].busy.type;
  921. switch (type)
  922. {
  923. case 0:
  924. /* Maybe reallocate a large block to a small fragment. */
  925. if (size <= BLOCKSIZE / 2)
  926. {
  927. result = malloc (size);
  928. if (result != NULL)
  929. {
  930. memcpy (result, ptr, size);
  931. _free_internal (ptr);
  932. return result;
  933. }
  934. }
  935. /* The new size is a large allocation as well;
  936. see if we can hold it in place. */
  937. blocks = BLOCKIFY (size);
  938. if (blocks < _heapinfo[block].busy.info.size)
  939. {
  940. /* The new size is smaller; return
  941. excess memory to the free list. */
  942. _heapinfo[block + blocks].busy.type = 0;
  943. _heapinfo[block + blocks].busy.info.size
  944. = _heapinfo[block].busy.info.size - blocks;
  945. _heapinfo[block].busy.info.size = blocks;
  946. /* We have just created a new chunk by splitting a chunk in two.
  947. Now we will free this chunk; increment the statistics counter
  948. so it doesn't become wrong when _free_internal decrements it. */
  949. ++_chunks_used;
  950. _free_internal (ADDRESS (block + blocks));
  951. result = ptr;
  952. }
  953. else if (blocks == _heapinfo[block].busy.info.size)
  954. /* No size change necessary. */
  955. result = ptr;
  956. else
  957. {
  958. /* Won't fit, so allocate a new region that will.
  959. Free the old region first in case there is sufficient
  960. adjacent free space to grow without moving. */
  961. blocks = _heapinfo[block].busy.info.size;
  962. /* Prevent free from actually returning memory to the system. */
  963. oldlimit = _heaplimit;
  964. _heaplimit = 0;
  965. _free_internal (ptr);
  966. _heaplimit = oldlimit;
  967. result = malloc (size);
  968. if (result == NULL)
  969. {
  970. /* Now we're really in trouble. We have to unfree
  971. the thing we just freed. Unfortunately it might
  972. have been coalesced with its neighbors. */
  973. if (_heapindex == block)
  974. (void) malloc (blocks * BLOCKSIZE);
  975. else
  976. {
  977. __ptr_t previous = malloc ((block - _heapindex) * BLOCKSIZE);
  978. (void) malloc (blocks * BLOCKSIZE);
  979. _free_internal (previous);
  980. }
  981. return NULL;
  982. }
  983. if (ptr != result)
  984. memmove (result, ptr, blocks * BLOCKSIZE);
  985. }
  986. break;
  987. default:
  988. /* Old size is a fragment; type is logarithm
  989. to base two of the fragment size. */
  990. if (size > (__malloc_size_t) (1 << (type - 1)) &&
  991. size <= (__malloc_size_t) (1 << type))
  992. /* The new size is the same kind of fragment. */
  993. result = ptr;
  994. else
  995. {
  996. /* The new size is different; allocate a new space,
  997. and copy the lesser of the new size and the old. */
  998. result = malloc (size);
  999. if (result == NULL)
  1000. return NULL;
  1001. memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
  1002. free (ptr);
  1003. }
  1004. break;
  1005. }
  1006. return result;
  1007. }
  1008. /* Copyright (C) 1991, 1992, 1994 Free Software Foundation, Inc.
  1009. This library is free software; you can redistribute it and/or
  1010. modify it under the terms of the GNU Library General Public License as
  1011. published by the Free Software Foundation; either version 2 of the
  1012. License, or (at your option) any later version.
  1013. This library is distributed in the hope that it will be useful,
  1014. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1015. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1016. Library General Public License for more details.
  1017. You should have received a copy of the GNU Library General Public
  1018. License along with this library; see the file COPYING.LIB. If
  1019. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1020. Cambridge, MA 02139, USA.
  1021. The author may be reached (Email) at the address mike@ai.mit.edu,
  1022. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  1023. #ifndef _MALLOC_INTERNAL
  1024. #define _MALLOC_INTERNAL
  1025. #include <malloc.h>
  1026. #endif
  1027. /* Allocate an array of NMEMB elements each SIZE bytes long.
  1028. The entire array is initialized to zeros. */
  1029. __ptr_t
  1030. calloc (nmemb, size)
  1031. register __malloc_size_t nmemb;
  1032. register __malloc_size_t size;
  1033. {
  1034. register __ptr_t result = malloc (nmemb * size);
  1035. if (result != NULL)
  1036. (void) memset (result, 0, nmemb * size);
  1037. return result;
  1038. }
  1039. /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  1040. This file is part of the GNU C Library.
  1041. The GNU C Library is free software; you can redistribute it and/or modify
  1042. it under the terms of the GNU General Public License as published by
  1043. the Free Software Foundation; either version 2, or (at your option)
  1044. any later version.
  1045. The GNU C Library is distributed in the hope that it will be useful,
  1046. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1047. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1048. GNU General Public License for more details.
  1049. You should have received a copy of the GNU General Public License
  1050. along with the GNU C Library; see the file COPYING. If not, write to
  1051. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  1052. #ifndef _MALLOC_INTERNAL
  1053. #define _MALLOC_INTERNAL
  1054. #include <malloc.h>
  1055. #endif
  1056. #ifndef __GNU_LIBRARY__
  1057. #define __sbrk sbrk
  1058. #endif
  1059. #ifdef __GNU_LIBRARY__
  1060. /* It is best not to declare this and cast its result on foreign operating
  1061. systems with potentially hostile include files. */
  1062. extern __ptr_t __sbrk __P ((int increment));
  1063. #endif
  1064. #ifndef NULL
  1065. #define NULL 0
  1066. #endif
  1067. /* Allocate INCREMENT more bytes of data space,
  1068. and return the start of data space, or NULL on errors.
  1069. If INCREMENT is negative, shrink data space. */
  1070. __ptr_t
  1071. __default_morecore (increment)
  1072. #ifdef __STDC__
  1073. ptrdiff_t increment;
  1074. #else
  1075. int increment;
  1076. #endif
  1077. {
  1078. __ptr_t result = (__ptr_t) __sbrk ((int) increment);
  1079. if (result == (__ptr_t) -1)
  1080. return NULL;
  1081. return result;
  1082. }
  1083. /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  1084. This library is free software; you can redistribute it and/or
  1085. modify it under the terms of the GNU Library General Public License as
  1086. published by the Free Software Foundation; either version 2 of the
  1087. License, or (at your option) any later version.
  1088. This library is distributed in the hope that it will be useful,
  1089. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1090. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1091. Library General Public License for more details.
  1092. You should have received a copy of the GNU Library General Public
  1093. License along with this library; see the file COPYING.LIB. If
  1094. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  1095. Cambridge, MA 02139, USA. */
  1096. #ifndef _MALLOC_INTERNAL
  1097. #define _MALLOC_INTERNAL
  1098. #include <malloc.h>
  1099. #endif
  1100. __ptr_t (*__memalign_hook) __P ((size_t __size, size_t __alignment));
  1101. __ptr_t
  1102. memalign (alignment, size)
  1103. __malloc_size_t alignment;
  1104. __malloc_size_t size;
  1105. {
  1106. __ptr_t result;
  1107. unsigned long int adj;
  1108. if (__memalign_hook)
  1109. return (*__memalign_hook) (alignment, size);
  1110. size = ((size + alignment - 1) / alignment) * alignment;
  1111. result = malloc (size);
  1112. if (result == NULL)
  1113. return NULL;
  1114. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  1115. (char *) NULL)) % alignment;
  1116. if (adj != 0)
  1117. {
  1118. struct alignlist *l;
  1119. for (l = _aligned_blocks; l != NULL; l = l->next)
  1120. if (l->aligned == NULL)
  1121. /* This slot is free. Use it. */
  1122. break;
  1123. if (l == NULL)
  1124. {
  1125. l = (struct alignlist *) malloc (sizeof (struct alignlist));
  1126. if (l == NULL)
  1127. {
  1128. free (result);
  1129. return NULL;
  1130. }
  1131. l->next = _aligned_blocks;
  1132. _aligned_blocks = l;
  1133. }
  1134. l->exact = result;
  1135. result = l->aligned = (char *) result + alignment - adj;
  1136. }
  1137. return result;
  1138. }