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