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