home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / gmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-05  |  41.0 KB  |  1,391 lines

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