home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / quartz / quartz10.lha / src / presto / shmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-02  |  22.5 KB  |  861 lines

  1. /*
  2.  * This memory allocation code is derived from the GNU memory allocator.
  3.  * Please read the FSF copyright notice at the end of this file.
  4.  */
  5.  
  6. /*
  7.  * @(#)nmalloc.c 1 (Caltech) 2/21/82
  8.  *
  9.  *    U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
  10.  *
  11.  *    Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
  12.  *
  13.  * This is a very fast storage allocator.  It allocates blocks of a small 
  14.  * number of different sizes, and keeps free lists of each size.  Blocks
  15.  * that don't exactly fit are passed up to the next larger size.  In this 
  16.  * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
  17.  * This is designed for use in a program that uses vast quantities of
  18.  * memory, but bombs when it runs out.  To make it a little better, it
  19.  * warns the user when he starts to get near the end.
  20.  *
  21.  * June 84, ACT: modified rcheck code to check the range given to malloc,
  22.  * rather than the range determined by the 2-power used.
  23.  *
  24.  * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
  25.  * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
  26.  * You should call malloc_init to reinitialize after loading dumped Emacs.
  27.  * Call malloc_stats to get info on memory stats if MSTATS turned on.
  28.  * realloc knows how to return same block given, just changing its size,
  29.  * if the power of 2 is correct.
  30.  *
  31.  * May 88, Univ. of Washington: Significant (but relatively simple)
  32.  * changes to make this code work in a Sequent shared-memory/PRESTO
  33.  * environment.
  34.  *
  35.  * Perhaps the biggest difference is that whenever we have to ask for
  36.  * more space, we get a BIG chunk rather than a relatively small one.
  37.  * Another change is that requests for memory >= the system page size
  38.  * will return memory that is aligned to a page boundary, due to the
  39.  * prevalent use of mmap(2).
  40.  *
  41.  * June 89, Univ. of Washington: Worked on memory alignment bug in malloc().
  42.  *
  43.  * Most of the changes are bracketed by the SHARED and PRESTO $ifdef's.
  44.  *
  45.  */
  46.  
  47.  
  48. #define SHARED
  49. #define PRESTO
  50.  
  51. #include <sys/types.h>
  52.  
  53. #ifdef SHARED
  54.  
  55. #ifdef sequent
  56. #include <parallel/parallel.h>
  57. #define sbrk shsbrk
  58. #endif sequent
  59.  
  60. #ifdef sun
  61. #  define shared /**/
  62. #  define private /**/
  63.    typedef    int slock_t;
  64. #  define L_UNLOCKED 0
  65. #  ifdef PRESTO
  66. #      define S_LOCK s_lock
  67. #      define S_UNLOCK s_unlock
  68. #  endif PRESTO
  69. #endif sun
  70.  
  71. #ifdef vax
  72. #define shared /**/
  73. #define private /**/
  74. typedef    int slock_t;
  75. #define L_UNLOCKED 0
  76.  
  77. #ifdef PRESTO
  78. /*
  79.  * Redefine S_LOCK and S_UNLOCK to be s_lock() and s_unlock(),
  80.  * included in vax_lock.s in Presto source.  Should probably be
  81.  * asm macros.  On the vax, the shared allocation code won't ld
  82.  * outside of a presto environment.
  83.  */
  84. #define S_LOCK s_lock
  85. #define S_UNLOCK s_unlock
  86. #else
  87. XXX
  88. #endif PRESTO
  89. #endif vax
  90.  
  91. #ifdef PRESTO
  92. /*
  93.  * See comment on naming of the memory allocator in misc.c.
  94.  *
  95.  * #define malloc shmalloc
  96.  * #define free shfree
  97.  * #define realloc shrealloc
  98.  */
  99. #endif PRESTO
  100.  
  101. #define mstats shmstats
  102.     
  103. #endif SHARED
  104.  
  105. #ifdef PRESTO
  106. /*
  107.  * These external routines are needed to avoid a deadlock situation where
  108.  * we preempt a thread controlling access to the memory management 
  109.  * stuff.  Preempting while holding onto this stuff could make things
  110.  * go very very slowly.
  111.  */
  112.  
  113. extern void enable_interrupts();
  114. extern int disable_interrupts();
  115. #endif PRESTO
  116.  
  117.  
  118. /*
  119.  * Determine which kind of system this is.
  120.  */
  121. #define BSD42
  122. #define BSD
  123. #include <sys/time.h>
  124. #include <sys/resource.h>
  125.  
  126. #define ISALLOC ((char) 0xf7)    /* magic byte that implies allocation */
  127. #define ISFREE ((char) 0x54)    /* magic byte that implies free block */
  128.                 /* this is for error checking only */
  129. #define ISMEMALIGN ((char) 0xd6)  /* Stored before the value returned by
  130.                      memalign, with the rest of the word
  131.                      being the distance to the true
  132.                      beginning of the block.  */
  133.  
  134. /*
  135.  * NBINS == number of "headers" pointing to blocks of size 2**(n+3).
  136.  */
  137. #define NBINS 30
  138.  
  139. /*
  140.  * If MSTATS is defined then
  141.  * nmalloc[i] is the difference between the number of mallocs and frees
  142.  * for a given block size.
  143.  */
  144.  
  145. #ifdef MSTATS
  146. #ifdef SHARED
  147. shared static int nmalloc[NBINS];
  148. shared static int nmal, nfre;
  149. #else
  150. static int nmalloc[NBINS];
  151. static int nmal, nfre;
  152. #endif
  153. #endif /* MSTATS */
  154.  
  155. /*
  156.  * If range checking is not turned on, all we have is a flag indicating
  157.  * whether memory is allocated, an index in nextf[], and a size field; to
  158.  * realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
  159.  * on whether the former can hold the exact size (given the value of
  160.  * 'index').  If range checking is on, we always need to know how much space
  161.  * is allocated, so the 'size' field is only used to remember memory alignment
  162.  */
  163.  
  164. struct mhead {
  165.     char     mh_alloc;    /* ISALLOC or ISFREE */
  166.     char     mh_index;    /* index in nextf[] */
  167. /* Remainder are valid only when block is allocated */
  168.     unsigned short mh_size;    /* size, if < 0x10000 */
  169. #ifdef rcheck
  170.     unsigned mh_nbytes;    /* number of bytes allocated */
  171.     int      mh_magic4;    /* should be == MAGIC4 */
  172. #endif /* rcheck */
  173. };
  174.  
  175. /*
  176.  * Access free-list pointer of a block.
  177.  * It is stored at block + 4.
  178.  * This is not a field in the mhead structure
  179.  * because we want sizeof (struct mhead)
  180.  * to describe the overhead for when the block is in use,
  181.  * and we do not want the free-list pointer to count in that.
  182.  */
  183.  
  184. #define CHAIN(a) \
  185.   (*(struct mhead **) (sizeof (char *) + (char *) (a)))
  186.  
  187. #ifdef rcheck
  188.  
  189. /*
  190.  * To implement range checking, we write magic values in at the beginning and
  191.  * end of each allocated block, and make sure they are undisturbed whenever a
  192.  * free or a realloc occurs.
  193.  */
  194.  
  195. /* Written in each of the 4 bytes following the block's real space */
  196. #define MAGIC1 0x55
  197.  
  198. /* Written in the 4 bytes before the block's real space */
  199. #define MAGIC4 0x55555555
  200.  
  201. #define ASSERT(p) if (!(p)) botch("p"); else
  202. #define EXTRA  4        /* 4 bytes extra for MAGIC1s */
  203.  
  204. #else
  205.  
  206. #define ASSERT(p)
  207. #define EXTRA  0
  208.  
  209. #endif /* rcheck */
  210.  
  211. /*
  212.  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
  213.  * smallest allocatable block is 8 bytes.  The overhead information will
  214.  * go in the first int of the block, and the returned pointer will point
  215.  * to the second.
  216.  *
  217.  * busy[i] is meant to indicate that a recursive call to malloc
  218.  * has been made (e.g., via a signal handler). Not used in this
  219.  * (May 88) version.
  220.  */
  221.  
  222. #ifdef SHARED
  223. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  224. shared static struct mhead *nextf[NBINS];
  225.  
  226. /* busy[i] is nonzero while allocation of block size i is in progress.  */
  227. shared static char busy[NBINS];
  228.  
  229. /* Number of bytes of writable memory we can expect to be able to get */
  230. shared static unsigned int lim_data;
  231.  
  232. /* nonzero once initial bunch of free blocks made */
  233. shared static int gotpool;
  234.  
  235. /* Mutex for structure access */
  236. shared static slock_t lock;
  237.  
  238. /* System page size in bytes */
  239. shared static int syspgsize;
  240.  
  241. #ifdef PRESTO
  242.  
  243. #define BEGIN_CRITICAL {        \
  244.     disable_interrupts();        \
  245.     S_LOCK(&lock);            \
  246. }
  247.  
  248. #define END_CRITICAL {            \
  249.     S_UNLOCK(&lock);        \
  250.     enable_interrupts();        \
  251. }
  252.  
  253. #else
  254.  
  255. #define BEGIN_CRITICAL {        \
  256.     S_LOCK(&lock);            \
  257. }
  258.  
  259. #define END_CRITICAL {            \
  260.     S_UNLOCK(&lock);        \
  261. }
  262.  
  263. #endif PRESTO
  264.  
  265. #else    /* not shared */
  266.  
  267. #ifdef PRESTO
  268.  
  269. #define BEGIN_CRITICAL disable_interrupts();
  270. #define END_CRITICAL enable_interrupts();
  271.  
  272. #else
  273.  
  274. #define BEGIN_CRITICAL ;
  275. #define END_CRITICAL ;
  276.  
  277. #endif PRESTO
  278.  
  279. /* nextf[i] is free list of blocks of size 2**(i + 3)  */
  280. static struct mhead *nextf[NBINS];
  281.  
  282. /* busy[i] is nonzero while allocation of block size i is in progress.  */
  283. static char busy[NBINS];
  284.  
  285. /* Number of bytes of writable memory we can expect to be able to get */
  286. static unsigned int lim_data;
  287.  
  288. /* nonzero once initial bunch of free blocks made */
  289. static int gotpool;
  290.  
  291. /* System page size in bytes */
  292. static int syspgsize;
  293.  
  294. #endif SHARED
  295.  
  296. char *grab_hunk();
  297.  
  298. /*
  299.  * BRKSIZELOG2 is log2-3 of the minimum chunk of memory we will ask for
  300.  * if we have to request more memory (ie, via sbrk).  In our case we ask for
  301.  * the maximum that will fit in the mh_size field, 65K.
  302.  */
  303. #define BRKSIZELOG2 13
  304.  
  305. static morecore (nu)            /* ask system for more memory */
  306.     register int nu;        /* size index to get more of  */
  307. {
  308.     register char *cp;
  309.     register int nblks;
  310.     register unsigned int siz;
  311.  
  312.  /*
  313.   * On initial startup, get one block of each size up to some reasonable
  314.   * size.
  315.   */
  316.   if (!gotpool) {
  317.     getpool (); 
  318.     gotpool = 1;
  319.     if ( nextf[nu] ) return; /* Getpool got it for us */
  320.    }
  321.  
  322.  /*
  323.   * Must be either a big block or we are really out of memory.
  324.   */
  325.   nblks = 1;
  326.   if ((siz = nu) < BRKSIZELOG2)
  327.     nblks = 1 << ((siz = BRKSIZELOG2) - nu);
  328.  
  329.   if ((cp = grab_hunk(1 << (siz + 3))) == (char *) -1)
  330.     return;            /* no more room! */
  331.  
  332.   if ((int) cp & 7) {
  333.     /*
  334.      * shouldn't happen, but just in case
  335.      */
  336.     cp = (char *) (((int) cp + 8) & ~7);
  337.     nblks--;
  338.   }
  339.  
  340.   /*
  341.    * save new header and link the nblks blocks together
  342.    */
  343.   nextf[nu] = (struct mhead  *) cp;
  344.   siz = 1 << (nu + 3);
  345.   while (1) {
  346.     ((struct mhead *) cp) -> mh_alloc = ISFREE;
  347.     ((struct mhead *) cp) -> mh_index = nu;
  348.     if (--nblks <= 0)
  349.     break;
  350.     CHAIN ((struct mhead   *) cp) = (struct mhead  *) (cp + siz);
  351.     cp += siz;
  352.   }
  353.   CHAIN ((struct mhead   *) cp) = 0;
  354. }
  355.  
  356. /*
  357.  * Get a chunk of memory from the system.  Make sure the pointer that
  358.  * we return is just sizeof(struct mhead) bytes shy of a page boundary.
  359.  * Calls to this function must be serialized.
  360.  */
  361.  
  362. char *
  363. grab_hunk(nbytes)
  364.   int nbytes;
  365. {
  366.   char *cp;
  367.   char *sbrk ();
  368.  
  369.   cp = sbrk(0);
  370.   /*
  371.    * land on pagesize boundaries 
  372.    */
  373.   if (((int) cp + sizeof(struct mhead)) & (syspgsize-1)) {
  374.     sbrk (syspgsize - (((int) cp + sizeof( struct mhead)) & (syspgsize-1)));
  375.   }
  376.  
  377.   return(sbrk(nbytes));
  378. }
  379.  
  380. /*
  381.  * Get a chunk of memory from the system.  Make sure the pointer that
  382.  * we return is just on a page boundary.  This memory should **not**
  383.  * be used by the memory allocator.
  384.  */
  385.  
  386. char *
  387. grab_nonmalloc_hunk(nbytes)
  388.   int nbytes;
  389. {
  390.   char *cp;
  391.   char *sbrk ();
  392.  
  393.   BEGIN_CRITICAL;
  394.   cp = sbrk(0);
  395.   /*
  396.    * land on pagesize boundaries 
  397.    */
  398.   if (((int) cp) & (syspgsize-1)) {
  399.     sbrk (syspgsize - (((int) cp) & (syspgsize-1)));
  400.   }
  401.  
  402.   cp = sbrk(nbytes);
  403.   END_CRITICAL;
  404.   return cp;
  405. }
  406.  
  407. static getpool ()
  408. {
  409.   register int nu;
  410.   register char *cp;
  411.   int logpgsz;
  412.   
  413.   /* Get 16*pagesize of storage */
  414.   cp = grab_hunk(16*syspgsize);
  415.   if (cp == (char *) -1)
  416.     return;
  417.  
  418.   /*
  419.   /* Divide it into an initial 8-word block
  420.    * plus one block of size 2**nu for nu = 3 ... log2(chunksize*pgsize)-1.
  421.    */
  422.   logpgsz = 8*syspgsize;
  423.   nu = 0;
  424.   while ( ! (logpgsz & 1) ) {
  425.       logpgsz >>= 1;
  426.       nu++;
  427.   }
  428.   logpgsz = nu - 3;
  429.   
  430.   CHAIN (cp) = nextf[0];
  431.   nextf[0] = (struct mhead *) cp;
  432.   ((struct mhead *) cp) -> mh_alloc = ISFREE;
  433.   ((struct mhead *) cp) -> mh_index = 0;
  434.   cp += 8;
  435.  
  436.   for (nu = 0; nu < logpgsz; nu++)
  437.     {
  438.       CHAIN (cp) = nextf[nu];
  439.       nextf[nu] = (struct mhead *) cp;
  440.       ((struct mhead *) cp) -> mh_alloc = ISFREE;
  441.       ((struct mhead *) cp) -> mh_index = nu;
  442.       cp += 8 << nu;
  443.     }
  444. }
  445.  
  446. char *malloc (n)        /* get a block */
  447.      unsigned n;
  448. {
  449.   register struct mhead *p;
  450.   register unsigned int nbytes;
  451.   register int nunits = 0;
  452.   int mustalign;
  453.   char *aligned;
  454.   struct mhead *p2;
  455.   
  456.   /*
  457.    * Figure out how many bytes are required, rounding up to the nearest
  458.    * multiple of 4, then figure out which nextf[] area to use.
  459.    */
  460.   if ( !syspgsize ) {
  461.       syspgsize = getpagesize();
  462.   }
  463.  
  464.   /*
  465.    * If asking for >= syspgsize bytes, make sure
  466.    * the return value is page aligned in case of mmap(2).
  467.    */
  468.   if ( n >= syspgsize ) {
  469.     mustalign = 1;
  470.       n += syspgsize;
  471.   }
  472.   else {
  473.     mustalign = 0;
  474.   }
  475.     
  476.   nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
  477.   {
  478.     register unsigned int   shiftr = (nbytes - 1) >> 2;
  479.  
  480.     while (shiftr >>= 1)
  481.       nunits++;
  482.   }
  483.  
  484.   BEGIN_CRITICAL;
  485.  
  486.   /* If there are no blocks of the appropriate size, go get some */
  487.   /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
  488.   if (nextf[nunits] == 0)
  489.     morecore (nunits);
  490.  
  491.   /* Get one block off the list, and set the new list head */
  492.   if ((p = nextf[nunits]) == 0) {
  493.       END_CRITICAL;
  494.       return 0;
  495.   }
  496.  
  497.   nextf[nunits] = CHAIN (p);
  498.  
  499.   END_CRITICAL;
  500.  
  501.   /*
  502.    * Check for free block clobbered
  503.    * If not for this check, we would gobble a clobbered free chain ptr
  504.    * and bomb out on the NEXT allocate of this size block
  505.    */
  506.   if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
  507. #ifdef rcheck
  508.     botch ("block on free list clobbered");
  509. #else /* not rcheck */
  510.     abort ();
  511. #endif /* not rcheck */
  512.  
  513.   /* Fill in the info, and if range checking, set up the magic numbers */
  514.   p -> mh_alloc = ISALLOC;
  515.  
  516. #ifdef rcheck
  517.   p -> mh_nbytes = n;
  518.   p -> mh_magic4 = MAGIC4;
  519.   {
  520.     register char  *m = (char *) (p + 1) + n;
  521.  
  522.     *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
  523.   }
  524. #else /* not rcheck */
  525.   p -> mh_size = n;
  526. #endif /* not rcheck */
  527.  
  528. #ifdef MSTATS
  529.   nmalloc[nunits]++;
  530.   nmal++;
  531. #endif /* MSTATS */
  532.  
  533.   if ( mustalign ) {
  534.       if ( ((int)(p+1) & (syspgsize-1)) != 0 ) {
  535.       aligned = (char *) (p+1);
  536.       aligned = (char *) (((int)aligned + syspgsize - 1) & -syspgsize);
  537.       p2 = (struct mhead *) aligned - 1;
  538.       p2->mh_size = aligned - (char *)( p + 1 );
  539.       p2->mh_alloc = ISMEMALIGN;
  540.       return aligned;
  541.       }
  542.   }
  543.   
  544.   return (char *) (p + 1);
  545. }
  546.  
  547. free (mem)
  548.      char *mem;
  549. {
  550.   register struct mhead *p;
  551.  
  552.   {
  553.     register char *ap = mem;
  554.  
  555.     if (ap == 0)
  556.       return;
  557.  
  558.     p = (struct mhead *) ap - 1;
  559.     if (p -> mh_alloc == ISMEMALIGN)
  560.       {
  561.     ap -= p->mh_size;
  562.     p = (struct mhead *) ap - 1;
  563.       }
  564.  
  565.     if (p -> mh_alloc != ISALLOC)
  566. #ifdef rcheck
  567.       abort ();
  568. #else
  569.       return;
  570. #endif
  571. #ifdef rcheck
  572.     ASSERT (p -> mh_magic4 == MAGIC4);
  573.     ap += p -> mh_nbytes;
  574.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
  575.     ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
  576. #endif /* rcheck */
  577.  
  578.   }
  579.  
  580.   {
  581.     register int nunits = p -> mh_index;
  582.  
  583.     ASSERT (nunits <= (NBINS-1));
  584.     p -> mh_alloc = ISFREE;
  585.  
  586.     BEGIN_CRITICAL;
  587.  
  588.     /* Put this block on the free list.  */
  589.     CHAIN (p) = nextf[nunits];
  590.     nextf[nunits] = p;
  591.  
  592. #ifdef MSTATS
  593.     nmalloc[nunits]--;
  594.     nfre++;
  595. #endif /* MSTATS */
  596.  
  597.     END_CRITICAL;
  598.   }
  599. }
  600.  
  601.  
  602. #ifdef MSTATS
  603. /*
  604.  * Return statistics describing allocation of blocks of size 2**n.
  605.  *
  606.  * This seems like a foolish way to do things, but who am I to argue
  607.  * with Stallman, et al.
  608.  */
  609.  
  610. struct mstats_value {
  611.     int blocksize;
  612.     int nfree;
  613.     int nused;
  614. };
  615.  
  616. struct mstats_value malloc_stats (size)
  617.      int size;
  618. {
  619.   struct mstats_value v;
  620.   register int i;
  621.   register struct mhead *p;
  622.  
  623.   v.nfree = 0;
  624.  
  625.   if (size < 0 || size >= NBINS)
  626.     {
  627.       v.blocksize = 0;
  628.       v.nused = 0;
  629.       return v;
  630.     }
  631.  
  632.   BEGIN_CRITICAL;
  633.   v.blocksize = 1 << (size + 3);
  634.   v.nused = nmalloc[size];
  635.   for (p = nextf[size]; p; p = CHAIN (p))
  636.     v.nfree++;
  637.   END_CRITICAL;
  638.  
  639.   return v;
  640. }
  641. #endif /* MSTATS */
  642.  
  643. char *realloc (mem, n)
  644.     char *mem;
  645.     register unsigned n;
  646. {
  647.   register struct mhead *p;
  648.   register unsigned int tocopy;
  649.   register unsigned int nbytes;
  650.   register int nunits;
  651.  
  652.   if ((p = (struct mhead *) mem) == 0)
  653.     return malloc (n);
  654.   p--;
  655.   nunits = p -> mh_index;
  656.   ASSERT (p -> mh_alloc == ISALLOC);
  657.  
  658. #ifdef rcheck
  659.   ASSERT (p -> mh_magic4 == MAGIC4);
  660.   {
  661.     register char *m = mem + (tocopy = p -> mh_nbytes);
  662.     ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
  663.     ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
  664.   }
  665. #else /* not rcheck */
  666.   if (p -> mh_index >= 13)
  667.     tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
  668.   else
  669.     tocopy = p -> mh_size;
  670. #endif /* not rcheck */
  671.  
  672.   /* See if desired size rounds to same power of 2 as actual size. */
  673.   nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
  674.  
  675.   /* If ok, use the same block, just marking its size as changed.  */
  676.   if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
  677.     {
  678. #ifdef rcheck
  679.       register char *m = mem + tocopy;
  680.       *m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
  681.       p-> mh_nbytes = n;
  682.       m = mem + n;
  683.       *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
  684. #else /* not rcheck */
  685.       p -> mh_size = n;
  686. #endif /* not rcheck */
  687.       return mem;
  688.     }
  689.  
  690.   if (n < tocopy)
  691.     tocopy = n;
  692.   {
  693.     register char *new;
  694.  
  695.     if ((new = malloc (n)) == 0)
  696.       return 0;
  697.     bcopy (mem, new, tocopy);
  698.     free (mem);
  699.     return new;
  700.   }
  701. }
  702.  
  703. /*
  704.  * The following routines are not used and are left for documentation
  705.  * purposes only.
  706.  */
  707.  
  708. #ifdef NOTDEF
  709. char *memalign ( alignment, size)
  710.      unsigned alignment, size;
  711. {
  712.   register char *ptr = malloc (size + alignment);
  713.   register char *aligned;
  714.   register struct mhead *p;
  715.  
  716.   if (ptr == 0)
  717.     return 0;
  718.   /*
  719.    * If entire block has the desired alignment, just accept it.
  720.    */
  721.   if (((int) ptr & (alignment - 1)) == 0)
  722.     return ptr;
  723.   /* Otherwise, get address of byte in the block that has that alignment.  */
  724.   aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
  725.   /* Store a suitable indication of how to free the block,
  726.      so that free can find the true beginning of it.  */
  727.   p = (struct mhead *) aligned - 1;
  728.   p -> mh_size = aligned - ptr;
  729.   p -> mh_alloc = ISMEMALIGN;
  730.   return aligned;
  731. }
  732.  
  733. /* This runs into trouble with getpagesize on HPUX.
  734.    Patching out seems cleaner than the ugly fix needed.
  735. char *
  736. valloc (size)
  737. {
  738.   return memalign (getpagesize (), size);
  739. }
  740. */
  741.  
  742. get_lim_data ()
  743. {
  744.   struct rlimit XXrlimit;
  745.  
  746.   getrlimit (RLIMIT_DATA, &XXrlimit);
  747. #ifdef RLIM_INFINITY
  748.   lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
  749. #else
  750.   lim_data = XXrlimit.rlim_cur;    /* soft limit */
  751. #endif
  752. }
  753.  
  754. #endif NOTDEF
  755.  
  756.  
  757.  
  758. /* dynamic memory allocation for GNU.
  759.    Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  760.  
  761.                NO WARRANTY
  762.  
  763.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  764. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  765. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  766. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  767. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  768. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  769. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  770. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  771. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  772. CORRECTION.
  773.  
  774.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  775. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  776. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  777. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  778. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  779. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  780. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  781. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  782. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  783. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  784.  
  785.         GENERAL PUBLIC LICENSE TO COPY
  786.  
  787.   1. You may copy and distribute verbatim copies of this source file
  788. as you receive it, in any medium, provided that you conspicuously and
  789. appropriately publish on each copy a valid copyright notice "Copyright
  790. (C) 1985 Free Software Foundation, Inc."; and include following the
  791. copyright notice a verbatim copy of the above disclaimer of warranty
  792. and of this License.  You may charge a distribution fee for the
  793. physical act of transferring a copy.
  794.  
  795.   2. You may modify your copy or copies of this source file or
  796. any portion of it, and copy and distribute such modifications under
  797. the terms of Paragraph 1 above, provided that you also do the following:
  798.  
  799.     a) cause the modified files to carry prominent notices stating
  800.     that you changed the files and the date of any change; and
  801.  
  802.     b) cause the whole of any work that you distribute or publish,
  803.     that in whole or in part contains or is a derivative of this
  804.     program or any part thereof, to be licensed at no charge to all
  805.     third parties on terms identical to those contained in this
  806.     License Agreement (except that you may choose to grant more extensive
  807.     warranty protection to some or all third parties, at your option).
  808.  
  809.     c) You may charge a distribution fee for the physical act of
  810.     transferring a copy, and you may at your option offer warranty
  811.     protection in exchange for a fee.
  812.  
  813. Mere aggregation of another unrelated program with this program (or its
  814. derivative) on a volume of a storage or distribution medium does not bring
  815. the other program under the scope of these terms.
  816.  
  817.   3. You may copy and distribute this program (or a portion or derivative
  818. of it, under Paragraph 2) in object code or executable form under the terms
  819. of Paragraphs 1 and 2 above provided that you also do one of the following:
  820.  
  821.     a) accompany it with the complete corresponding machine-readable
  822.     source code, which must be distributed under the terms of
  823.     Paragraphs 1 and 2 above; or,
  824.  
  825.     b) accompany it with a written offer, valid for at least three
  826.     years, to give any third party free (except for a nominal
  827.     shipping charge) a complete machine-readable copy of the
  828.     corresponding source code, to be distributed under the terms of
  829.     Paragraphs 1 and 2 above; or,
  830.  
  831.     c) accompany it with the information you received as to where the
  832.     corresponding source code may be obtained.  (This alternative is
  833.     allowed only for noncommercial distribution and only if you
  834.     received the program in object code or executable form alone.)
  835.  
  836. For an executable file, complete source code means all the source code for
  837. all modules it contains; but, as a special exception, it need not include
  838. source code for modules which are standard libraries that accompany the
  839. operating system on which the executable file runs.
  840.  
  841.   4. You may not copy, sublicense, distribute or transfer this program
  842. except as expressly provided under this License Agreement.  Any attempt
  843. otherwise to copy, sublicense, distribute or transfer this program is void and
  844. your rights to use the program under this License agreement shall be
  845. automatically terminated.  However, parties who have received computer
  846. software programs from you with this License Agreement will not have
  847. their licenses terminated so long as such parties remain in full compliance.
  848.  
  849.   5. If you wish to incorporate parts of this program into other free
  850. programs whose distribution conditions are different, write to the Free
  851. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  852. worked out a simple rule that can be stated here, but we will often permit
  853. this.  We will be guided by the two goals of preserving the free status of
  854. all derivatives of our free software and of promoting the sharing and reuse of
  855. software.
  856.  
  857.  
  858. In other words, you are welcome to use, share and improve this program.
  859. You are forbidden to forbid anyone else to use, share and improve
  860. what you give them.   Help stamp out software-hoarding!  */
  861.