home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / lib / gmalloc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  39KB  |  1,318 lines

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