xref: /dragonfly/contrib/tcsh-6/tc.alloc.c (revision d6ab524c)
17d8fb588SMatthias Schmidt /*
27d8fb588SMatthias Schmidt  * tc.alloc.c (Caltech) 2/21/82
37d8fb588SMatthias Schmidt  * Chris Kingsley, kingsley@cit-20.
47d8fb588SMatthias Schmidt  *
57d8fb588SMatthias Schmidt  * This is a very fast storage allocator.  It allocates blocks of a small
67d8fb588SMatthias Schmidt  * number of different sizes, and keeps free lists of each size.  Blocks that
77d8fb588SMatthias Schmidt  * don't exactly fit are passed up to the next larger size.  In this
87d8fb588SMatthias Schmidt  * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
97d8fb588SMatthias Schmidt  * This is designed for use in a program that uses vast quantities of memory,
107d8fb588SMatthias Schmidt  * but bombs when it runs out.
117d8fb588SMatthias Schmidt  */
127d8fb588SMatthias Schmidt /*-
137d8fb588SMatthias Schmidt  * Copyright (c) 1980, 1991 The Regents of the University of California.
147d8fb588SMatthias Schmidt  * All rights reserved.
157d8fb588SMatthias Schmidt  *
167d8fb588SMatthias Schmidt  * Redistribution and use in source and binary forms, with or without
177d8fb588SMatthias Schmidt  * modification, are permitted provided that the following conditions
187d8fb588SMatthias Schmidt  * are met:
197d8fb588SMatthias Schmidt  * 1. Redistributions of source code must retain the above copyright
207d8fb588SMatthias Schmidt  *    notice, this list of conditions and the following disclaimer.
217d8fb588SMatthias Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
227d8fb588SMatthias Schmidt  *    notice, this list of conditions and the following disclaimer in the
237d8fb588SMatthias Schmidt  *    documentation and/or other materials provided with the distribution.
247d8fb588SMatthias Schmidt  * 3. Neither the name of the University nor the names of its contributors
257d8fb588SMatthias Schmidt  *    may be used to endorse or promote products derived from this software
267d8fb588SMatthias Schmidt  *    without specific prior written permission.
277d8fb588SMatthias Schmidt  *
287d8fb588SMatthias Schmidt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
297d8fb588SMatthias Schmidt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
307d8fb588SMatthias Schmidt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
317d8fb588SMatthias Schmidt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
327d8fb588SMatthias Schmidt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
337d8fb588SMatthias Schmidt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
347d8fb588SMatthias Schmidt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
357d8fb588SMatthias Schmidt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
367d8fb588SMatthias Schmidt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
377d8fb588SMatthias Schmidt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
387d8fb588SMatthias Schmidt  * SUCH DAMAGE.
397d8fb588SMatthias Schmidt  */
407d8fb588SMatthias Schmidt #include "sh.h"
4194afa86dSJohn Marino #ifdef HAVE_MALLINFO
4294afa86dSJohn Marino #include <malloc.h>
4394afa86dSJohn Marino #endif
4460962bbcSJohn Marino #if defined(HAVE_SBRK) && !defined(__APPLE__)
4560962bbcSJohn Marino #define USE_SBRK
4660962bbcSJohn Marino #endif
477d8fb588SMatthias Schmidt 
487d8fb588SMatthias Schmidt #define RCHECK
497d8fb588SMatthias Schmidt #define DEBUG
507d8fb588SMatthias Schmidt 
517d8fb588SMatthias Schmidt static char   *memtop = NULL;		/* PWP: top of current memory */
527d8fb588SMatthias Schmidt static char   *membot = NULL;		/* PWP: bottom of allocatable memory */
537d8fb588SMatthias Schmidt 
547d8fb588SMatthias Schmidt int dont_free = 0;
557d8fb588SMatthias Schmidt 
567d8fb588SMatthias Schmidt #ifdef WINNT_NATIVE
577d8fb588SMatthias Schmidt # define malloc		fmalloc
587d8fb588SMatthias Schmidt # define free		ffree
597d8fb588SMatthias Schmidt # define calloc		fcalloc
607d8fb588SMatthias Schmidt # define realloc	frealloc
617d8fb588SMatthias Schmidt #endif /* WINNT_NATIVE */
627d8fb588SMatthias Schmidt 
637d8fb588SMatthias Schmidt #if !defined(DEBUG) || defined(SYSMALLOC)
647d8fb588SMatthias Schmidt static void
out_of_memory(void)657d8fb588SMatthias Schmidt out_of_memory (void)
667d8fb588SMatthias Schmidt {
677d8fb588SMatthias Schmidt     static const char msg[] = "Out of memory\n";
687d8fb588SMatthias Schmidt 
6960962bbcSJohn Marino     TCSH_IGNORE(write(didfds ? 2 : SHDIAG, msg, strlen(msg)));
707d8fb588SMatthias Schmidt     _exit(1);
717d8fb588SMatthias Schmidt }
727d8fb588SMatthias Schmidt #endif
737d8fb588SMatthias Schmidt 
747d8fb588SMatthias Schmidt #ifndef SYSMALLOC
757d8fb588SMatthias Schmidt 
767d8fb588SMatthias Schmidt #ifdef SX
777d8fb588SMatthias Schmidt extern void* sbrk();
787d8fb588SMatthias Schmidt #endif
797d8fb588SMatthias Schmidt /*
807d8fb588SMatthias Schmidt  * Lots of os routines are busted and try to free invalid pointers.
817d8fb588SMatthias Schmidt  * Although our free routine is smart enough and it will pick bad
827d8fb588SMatthias Schmidt  * pointers most of the time, in cases where we know we are going to get
837d8fb588SMatthias Schmidt  * a bad pointer, we'd rather leak.
847d8fb588SMatthias Schmidt  */
857d8fb588SMatthias Schmidt 
867d8fb588SMatthias Schmidt #ifndef NULL
877d8fb588SMatthias Schmidt #define	NULL 0
887d8fb588SMatthias Schmidt #endif
897d8fb588SMatthias Schmidt 
907d8fb588SMatthias Schmidt typedef unsigned char U_char;	/* we don't really have signed chars */
917d8fb588SMatthias Schmidt typedef unsigned int U_int;
927d8fb588SMatthias Schmidt typedef unsigned short U_short;
937d8fb588SMatthias Schmidt typedef unsigned long U_long;
947d8fb588SMatthias Schmidt 
957d8fb588SMatthias Schmidt 
967d8fb588SMatthias Schmidt /*
977d8fb588SMatthias Schmidt  * The overhead on a block is at least 4 bytes.  When free, this space
987d8fb588SMatthias Schmidt  * contains a pointer to the next free block, and the bottom two bits must
997d8fb588SMatthias Schmidt  * be zero.  When in use, the first byte is set to MAGIC, and the second
1007d8fb588SMatthias Schmidt  * byte is the size index.  The remaining bytes are for alignment.
1017d8fb588SMatthias Schmidt  * If range checking is enabled and the size of the block fits
1027d8fb588SMatthias Schmidt  * in two bytes, then the top two bytes hold the size of the requested block
1037d8fb588SMatthias Schmidt  * plus the range checking words, and the header word MINUS ONE.
1047d8fb588SMatthias Schmidt  */
1057d8fb588SMatthias Schmidt 
1067d8fb588SMatthias Schmidt 
1077d8fb588SMatthias Schmidt #define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
1087d8fb588SMatthias Schmidt 
1097d8fb588SMatthias Schmidt union overhead {
1107d8fb588SMatthias Schmidt     union overhead *ov_next;	/* when free */
1117d8fb588SMatthias Schmidt     struct {
1127d8fb588SMatthias Schmidt 	U_char  ovu_magic;	/* magic number */
1137d8fb588SMatthias Schmidt 	U_char  ovu_index;	/* bucket # */
1147d8fb588SMatthias Schmidt #ifdef RCHECK
1157d8fb588SMatthias Schmidt 	U_short ovu_size;	/* actual block size */
1167d8fb588SMatthias Schmidt 	U_int   ovu_rmagic;	/* range magic number */
1177d8fb588SMatthias Schmidt #endif
1187d8fb588SMatthias Schmidt     }       ovu;
1197d8fb588SMatthias Schmidt #define	ov_magic	ovu.ovu_magic
1207d8fb588SMatthias Schmidt #define	ov_index	ovu.ovu_index
1217d8fb588SMatthias Schmidt #define	ov_size		ovu.ovu_size
1227d8fb588SMatthias Schmidt #define	ov_rmagic	ovu.ovu_rmagic
1237d8fb588SMatthias Schmidt };
1247d8fb588SMatthias Schmidt 
1257d8fb588SMatthias Schmidt #define	MAGIC		0xfd	/* magic # on accounting info */
1267d8fb588SMatthias Schmidt #define RMAGIC		0x55555555	/* magic # on range info */
1277d8fb588SMatthias Schmidt #ifdef RCHECK
1287d8fb588SMatthias Schmidt #define	RSLOP		sizeof (U_int)
1297d8fb588SMatthias Schmidt #else
1307d8fb588SMatthias Schmidt #define	RSLOP		0
1317d8fb588SMatthias Schmidt #endif
1327d8fb588SMatthias Schmidt 
1337d8fb588SMatthias Schmidt 
134653fab9eSSascha Wildner #ifdef _LP64
135653fab9eSSascha Wildner #define ROUNDUP	15
136653fab9eSSascha Wildner #else
1377d8fb588SMatthias Schmidt #define ROUNDUP	7
138653fab9eSSascha Wildner #endif
1397d8fb588SMatthias Schmidt 
1407d8fb588SMatthias Schmidt /*
1417d8fb588SMatthias Schmidt  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
1427d8fb588SMatthias Schmidt  * smallest allocatable block is 8 bytes.  The overhead information
1437d8fb588SMatthias Schmidt  * precedes the data area returned to the user.
1447d8fb588SMatthias Schmidt  */
1457d8fb588SMatthias Schmidt #define	NBUCKETS ((sizeof(long) << 3) - 3)
1467d8fb588SMatthias Schmidt static union overhead *nextf[NBUCKETS] IZERO_STRUCT;
1477d8fb588SMatthias Schmidt 
1487d8fb588SMatthias Schmidt /*
1497d8fb588SMatthias Schmidt  * nmalloc[i] is the difference between the number of mallocs and frees
1507d8fb588SMatthias Schmidt  * for a given block size.
1517d8fb588SMatthias Schmidt  */
1527d8fb588SMatthias Schmidt static U_int nmalloc[NBUCKETS] IZERO_STRUCT;
1537d8fb588SMatthias Schmidt 
1547d8fb588SMatthias Schmidt #ifndef lint
1557d8fb588SMatthias Schmidt static	int	findbucket	(union overhead *, int);
1567d8fb588SMatthias Schmidt static	void	morecore	(int);
1577d8fb588SMatthias Schmidt #endif
1587d8fb588SMatthias Schmidt 
1597d8fb588SMatthias Schmidt 
1607d8fb588SMatthias Schmidt #ifdef DEBUG
1617d8fb588SMatthias Schmidt # define CHECK(a, str, p) \
1627d8fb588SMatthias Schmidt     if (a) { \
1637d8fb588SMatthias Schmidt 	xprintf(str, p);	\
1647d8fb588SMatthias Schmidt 	xprintf(" (memtop = %p membot = %p)\n", memtop, membot);	\
1657d8fb588SMatthias Schmidt 	abort(); \
1667d8fb588SMatthias Schmidt     }
1677d8fb588SMatthias Schmidt #else
1687d8fb588SMatthias Schmidt # define CHECK(a, str, p) \
1697d8fb588SMatthias Schmidt     if (a) { \
1707d8fb588SMatthias Schmidt 	xprintf(str, p);	\
1717d8fb588SMatthias Schmidt 	xprintf(" (memtop = %p membot = %p)\n", memtop, membot);	\
1727d8fb588SMatthias Schmidt 	return; \
1737d8fb588SMatthias Schmidt     }
1747d8fb588SMatthias Schmidt #endif
1757d8fb588SMatthias Schmidt 
1767d8fb588SMatthias Schmidt memalign_t
malloc(size_t nbytes)1777d8fb588SMatthias Schmidt malloc(size_t nbytes)
1787d8fb588SMatthias Schmidt {
1797d8fb588SMatthias Schmidt #ifndef lint
1807d8fb588SMatthias Schmidt     union overhead *p;
1817d8fb588SMatthias Schmidt     int bucket = 0;
1827d8fb588SMatthias Schmidt     unsigned shiftr;
1837d8fb588SMatthias Schmidt 
1847d8fb588SMatthias Schmidt     /*
1857d8fb588SMatthias Schmidt      * Convert amount of memory requested into closest block size stored in
1867d8fb588SMatthias Schmidt      * hash buckets which satisfies request.  Account for space used per block
1877d8fb588SMatthias Schmidt      * for accounting.
1887d8fb588SMatthias Schmidt      */
1897d8fb588SMatthias Schmidt #ifdef SUNOS4
1907d8fb588SMatthias Schmidt     /*
1917d8fb588SMatthias Schmidt      * SunOS localtime() overwrites the 9th byte on an 8 byte malloc()....
1927d8fb588SMatthias Schmidt      * so we get one more...
1937d8fb588SMatthias Schmidt      * From Michael Schroeder: This is not true. It depends on the
1947d8fb588SMatthias Schmidt      * timezone string. In Europe it can overwrite the 13th byte on a
1957d8fb588SMatthias Schmidt      * 12 byte malloc.
1967d8fb588SMatthias Schmidt      * So we punt and we always allocate an extra byte.
1977d8fb588SMatthias Schmidt      */
1987d8fb588SMatthias Schmidt     nbytes++;
1997d8fb588SMatthias Schmidt #endif
2007d8fb588SMatthias Schmidt 
2017d8fb588SMatthias Schmidt     nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP);
2027d8fb588SMatthias Schmidt     shiftr = (nbytes - 1) >> 2;
2037d8fb588SMatthias Schmidt 
2047d8fb588SMatthias Schmidt     /* apart from this loop, this is O(1) */
2057d8fb588SMatthias Schmidt     while ((shiftr >>= 1) != 0)
2067d8fb588SMatthias Schmidt 	bucket++;
2077d8fb588SMatthias Schmidt     /*
2087d8fb588SMatthias Schmidt      * If nothing in hash bucket right now, request more memory from the
2097d8fb588SMatthias Schmidt      * system.
2107d8fb588SMatthias Schmidt      */
2117d8fb588SMatthias Schmidt     if (nextf[bucket] == NULL)
2127d8fb588SMatthias Schmidt 	morecore(bucket);
2137d8fb588SMatthias Schmidt     if ((p = nextf[bucket]) == NULL) {
2147d8fb588SMatthias Schmidt 	child++;
2157d8fb588SMatthias Schmidt #ifndef DEBUG
2167d8fb588SMatthias Schmidt 	out_of_memory();
2177d8fb588SMatthias Schmidt #else
2187d8fb588SMatthias Schmidt 	showall(NULL, NULL);
2197d8fb588SMatthias Schmidt 	xprintf(CGETS(19, 1, "nbytes=%zu: Out of memory\n"), nbytes);
2207d8fb588SMatthias Schmidt 	abort();
2217d8fb588SMatthias Schmidt #endif
2227d8fb588SMatthias Schmidt 	/* fool lint */
2237d8fb588SMatthias Schmidt 	return ((memalign_t) 0);
2247d8fb588SMatthias Schmidt     }
2257d8fb588SMatthias Schmidt     /* remove from linked list */
2267d8fb588SMatthias Schmidt     nextf[bucket] = nextf[bucket]->ov_next;
2277d8fb588SMatthias Schmidt     p->ov_magic = MAGIC;
2287d8fb588SMatthias Schmidt     p->ov_index = bucket;
2297d8fb588SMatthias Schmidt     nmalloc[bucket]++;
2307d8fb588SMatthias Schmidt #ifdef RCHECK
2317d8fb588SMatthias Schmidt     /*
2327d8fb588SMatthias Schmidt      * Record allocated size of block and bound space with magic numbers.
2337d8fb588SMatthias Schmidt      */
234653fab9eSSascha Wildner     p->ov_size = (p->ov_index <= 13) ? (U_short)nbytes - 1 : 0;
2357d8fb588SMatthias Schmidt     p->ov_rmagic = RMAGIC;
2367d8fb588SMatthias Schmidt     *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
2377d8fb588SMatthias Schmidt #endif
2387d8fb588SMatthias Schmidt     return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead))));
2397d8fb588SMatthias Schmidt #else
2407d8fb588SMatthias Schmidt     if (nbytes)
2417d8fb588SMatthias Schmidt 	return ((memalign_t) 0);
2427d8fb588SMatthias Schmidt     else
2437d8fb588SMatthias Schmidt 	return ((memalign_t) 0);
2447d8fb588SMatthias Schmidt #endif /* !lint */
2457d8fb588SMatthias Schmidt }
2467d8fb588SMatthias Schmidt 
2477d8fb588SMatthias Schmidt #ifndef lint
2487d8fb588SMatthias Schmidt /*
2497d8fb588SMatthias Schmidt  * Allocate more memory to the indicated bucket.
2507d8fb588SMatthias Schmidt  */
2517d8fb588SMatthias Schmidt static void
morecore(int bucket)2527d8fb588SMatthias Schmidt morecore(int bucket)
2537d8fb588SMatthias Schmidt {
2547d8fb588SMatthias Schmidt     union overhead *op;
2557d8fb588SMatthias Schmidt     int rnu;		/* 2^rnu bytes will be requested */
2567d8fb588SMatthias Schmidt     int nblks;		/* become nblks blocks of the desired size */
2577d8fb588SMatthias Schmidt     int siz;
2587d8fb588SMatthias Schmidt 
2597d8fb588SMatthias Schmidt     if (nextf[bucket])
2607d8fb588SMatthias Schmidt 	return;
2617d8fb588SMatthias Schmidt     /*
2627d8fb588SMatthias Schmidt      * Insure memory is allocated on a page boundary.  Should make getpageize
2637d8fb588SMatthias Schmidt      * call?
2647d8fb588SMatthias Schmidt      */
2657d8fb588SMatthias Schmidt     op = (union overhead *) sbrk(0);
2667d8fb588SMatthias Schmidt     memtop = (char *) op;
2677d8fb588SMatthias Schmidt     if (membot == NULL)
2687d8fb588SMatthias Schmidt 	membot = memtop;
2697d8fb588SMatthias Schmidt     if ((long) op & 0x3ff) {
2707d8fb588SMatthias Schmidt 	memtop = sbrk((int) (1024 - ((long) op & 0x3ff)));
2717d8fb588SMatthias Schmidt 	memtop += (long) (1024 - ((long) op & 0x3ff));
2727d8fb588SMatthias Schmidt     }
2737d8fb588SMatthias Schmidt 
2747d8fb588SMatthias Schmidt     /* take 2k unless the block is bigger than that */
2757d8fb588SMatthias Schmidt     rnu = (bucket <= 8) ? 11 : bucket + 3;
2767d8fb588SMatthias Schmidt     nblks = 1 << (rnu - (bucket + 3));	/* how many blocks to get */
2777d8fb588SMatthias Schmidt     memtop = sbrk(1 << rnu);	/* PWP */
2787d8fb588SMatthias Schmidt     op = (union overhead *) memtop;
2797d8fb588SMatthias Schmidt     /* no more room! */
2807d8fb588SMatthias Schmidt     if ((long) op == -1)
2817d8fb588SMatthias Schmidt 	return;
2827d8fb588SMatthias Schmidt     memtop += (long) (1 << rnu);
2837d8fb588SMatthias Schmidt     /*
2847d8fb588SMatthias Schmidt      * Round up to minimum allocation size boundary and deduct from block count
2857d8fb588SMatthias Schmidt      * to reflect.
2867d8fb588SMatthias Schmidt      */
2877d8fb588SMatthias Schmidt     if (((U_long) op) & ROUNDUP) {
2887d8fb588SMatthias Schmidt 	op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP);
2897d8fb588SMatthias Schmidt 	nblks--;
2907d8fb588SMatthias Schmidt     }
2917d8fb588SMatthias Schmidt     /*
2927d8fb588SMatthias Schmidt      * Add new memory allocated to that on free list for this hash bucket.
2937d8fb588SMatthias Schmidt      */
2947d8fb588SMatthias Schmidt     nextf[bucket] = op;
2957d8fb588SMatthias Schmidt     siz = 1 << (bucket + 3);
2967d8fb588SMatthias Schmidt     while (--nblks > 0) {
2977d8fb588SMatthias Schmidt 	op->ov_next = (union overhead *) (((caddr_t) op) + siz);
2987d8fb588SMatthias Schmidt 	op = (union overhead *) (((caddr_t) op) + siz);
2997d8fb588SMatthias Schmidt     }
3007d8fb588SMatthias Schmidt     op->ov_next = NULL;
3017d8fb588SMatthias Schmidt }
3027d8fb588SMatthias Schmidt 
3037d8fb588SMatthias Schmidt #endif
3047d8fb588SMatthias Schmidt 
3057d8fb588SMatthias Schmidt void
free(ptr_t cp)3067d8fb588SMatthias Schmidt free(ptr_t cp)
3077d8fb588SMatthias Schmidt {
3087d8fb588SMatthias Schmidt #ifndef lint
3097d8fb588SMatthias Schmidt     int size;
3107d8fb588SMatthias Schmidt     union overhead *op;
3117d8fb588SMatthias Schmidt 
3127d8fb588SMatthias Schmidt     /*
3137d8fb588SMatthias Schmidt      * the don't free flag is there so that we avoid os bugs in routines
3147d8fb588SMatthias Schmidt      * that free invalid pointers!
3157d8fb588SMatthias Schmidt      */
3167d8fb588SMatthias Schmidt     if (cp == NULL || dont_free)
3177d8fb588SMatthias Schmidt 	return;
3187d8fb588SMatthias Schmidt     CHECK(!memtop || !membot,
3197d8fb588SMatthias Schmidt 	  CGETS(19, 2, "free(%p) called before any allocations."), cp);
3207d8fb588SMatthias Schmidt     CHECK(cp > (ptr_t) memtop,
3217d8fb588SMatthias Schmidt 	  CGETS(19, 3, "free(%p) above top of memory."), cp);
3227d8fb588SMatthias Schmidt     CHECK(cp < (ptr_t) membot,
3237d8fb588SMatthias Schmidt 	  CGETS(19, 4, "free(%p) below bottom of memory."), cp);
3247d8fb588SMatthias Schmidt     op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
3257d8fb588SMatthias Schmidt     CHECK(op->ov_magic != MAGIC,
3267d8fb588SMatthias Schmidt 	  CGETS(19, 5, "free(%p) bad block."), cp);
3277d8fb588SMatthias Schmidt 
3287d8fb588SMatthias Schmidt #ifdef RCHECK
3297d8fb588SMatthias Schmidt     if (op->ov_index <= 13)
3307d8fb588SMatthias Schmidt 	CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
3317d8fb588SMatthias Schmidt 	      CGETS(19, 6, "free(%p) bad range check."), cp);
3327d8fb588SMatthias Schmidt #endif
3337d8fb588SMatthias Schmidt     CHECK(op->ov_index >= NBUCKETS,
3347d8fb588SMatthias Schmidt 	  CGETS(19, 7, "free(%p) bad block index."), cp);
3357d8fb588SMatthias Schmidt     size = op->ov_index;
3367d8fb588SMatthias Schmidt     op->ov_next = nextf[size];
3377d8fb588SMatthias Schmidt     nextf[size] = op;
3387d8fb588SMatthias Schmidt 
3397d8fb588SMatthias Schmidt     nmalloc[size]--;
3407d8fb588SMatthias Schmidt 
3417d8fb588SMatthias Schmidt #else
3427d8fb588SMatthias Schmidt     if (cp == NULL)
3437d8fb588SMatthias Schmidt 	return;
3447d8fb588SMatthias Schmidt #endif
3457d8fb588SMatthias Schmidt }
3467d8fb588SMatthias Schmidt 
3477d8fb588SMatthias Schmidt memalign_t
calloc(size_t i,size_t j)3487d8fb588SMatthias Schmidt calloc(size_t i, size_t j)
3497d8fb588SMatthias Schmidt {
3507d8fb588SMatthias Schmidt #ifndef lint
3517d8fb588SMatthias Schmidt     char *cp;
352653fab9eSSascha Wildner     volatile size_t k;
3537d8fb588SMatthias Schmidt 
3547d8fb588SMatthias Schmidt     i *= j;
3557d8fb588SMatthias Schmidt     cp = xmalloc(i);
356653fab9eSSascha Wildner     /* Stop gcc 5.x from optimizing malloc+memset = calloc */
357653fab9eSSascha Wildner     k = i;
358653fab9eSSascha Wildner     memset(cp, 0, k);
3597d8fb588SMatthias Schmidt 
3607d8fb588SMatthias Schmidt     return ((memalign_t) cp);
3617d8fb588SMatthias Schmidt #else
3627d8fb588SMatthias Schmidt     if (i && j)
3637d8fb588SMatthias Schmidt 	return ((memalign_t) 0);
3647d8fb588SMatthias Schmidt     else
3657d8fb588SMatthias Schmidt 	return ((memalign_t) 0);
3667d8fb588SMatthias Schmidt #endif
3677d8fb588SMatthias Schmidt }
3687d8fb588SMatthias Schmidt 
3697d8fb588SMatthias Schmidt /*
3707d8fb588SMatthias Schmidt  * When a program attempts "storage compaction" as mentioned in the
3717d8fb588SMatthias Schmidt  * old malloc man page, it realloc's an already freed block.  Usually
3727d8fb588SMatthias Schmidt  * this is the last block it freed; occasionally it might be farther
3737d8fb588SMatthias Schmidt  * back.  We have to search all the free lists for the block in order
3747d8fb588SMatthias Schmidt  * to determine its bucket: 1st we make one pass thru the lists
3757d8fb588SMatthias Schmidt  * checking only the first block in each; if that fails we search
3767d8fb588SMatthias Schmidt  * ``realloc_srchlen'' blocks in each list for a match (the variable
3777d8fb588SMatthias Schmidt  * is extern so the caller can modify it).  If that fails we just copy
3787d8fb588SMatthias Schmidt  * however many bytes was given to realloc() and hope it's not huge.
3797d8fb588SMatthias Schmidt  */
3807d8fb588SMatthias Schmidt #ifndef lint
3817d8fb588SMatthias Schmidt /* 4 should be plenty, -1 =>'s whole list */
3827d8fb588SMatthias Schmidt static int     realloc_srchlen = 4;
3837d8fb588SMatthias Schmidt #endif /* lint */
3847d8fb588SMatthias Schmidt 
3857d8fb588SMatthias Schmidt memalign_t
realloc(ptr_t cp,size_t nbytes)3867d8fb588SMatthias Schmidt realloc(ptr_t cp, size_t nbytes)
3877d8fb588SMatthias Schmidt {
3887d8fb588SMatthias Schmidt #ifndef lint
3897d8fb588SMatthias Schmidt     U_int onb;
3907d8fb588SMatthias Schmidt     union overhead *op;
3917d8fb588SMatthias Schmidt     ptr_t res;
3927d8fb588SMatthias Schmidt     int i;
3937d8fb588SMatthias Schmidt     int     was_alloced = 0;
3947d8fb588SMatthias Schmidt 
3957d8fb588SMatthias Schmidt     if (cp == NULL)
3967d8fb588SMatthias Schmidt 	return (malloc(nbytes));
3977d8fb588SMatthias Schmidt     op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
3987d8fb588SMatthias Schmidt     if (op->ov_magic == MAGIC) {
3997d8fb588SMatthias Schmidt 	was_alloced++;
4007d8fb588SMatthias Schmidt 	i = op->ov_index;
4017d8fb588SMatthias Schmidt     }
4027d8fb588SMatthias Schmidt     else
4037d8fb588SMatthias Schmidt 	/*
4047d8fb588SMatthias Schmidt 	 * Already free, doing "compaction".
4057d8fb588SMatthias Schmidt 	 *
4067d8fb588SMatthias Schmidt 	 * Search for the old block of memory on the free list.  First, check the
4077d8fb588SMatthias Schmidt 	 * most common case (last element free'd), then (this failing) the last
4087d8fb588SMatthias Schmidt 	 * ``realloc_srchlen'' items free'd. If all lookups fail, then assume
4097d8fb588SMatthias Schmidt 	 * the size of the memory block being realloc'd is the smallest
4107d8fb588SMatthias Schmidt 	 * possible.
4117d8fb588SMatthias Schmidt 	 */
4127d8fb588SMatthias Schmidt 	if ((i = findbucket(op, 1)) < 0 &&
4137d8fb588SMatthias Schmidt 	    (i = findbucket(op, realloc_srchlen)) < 0)
4147d8fb588SMatthias Schmidt 	    i = 0;
4157d8fb588SMatthias Schmidt 
4167d8fb588SMatthias Schmidt     onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP);
4177d8fb588SMatthias Schmidt 
4187d8fb588SMatthias Schmidt     /* avoid the copy if same size block */
4197d8fb588SMatthias Schmidt     if (was_alloced && (onb <= (U_int) (1 << (i + 3))) &&
4207d8fb588SMatthias Schmidt 	(onb > (U_int) (1 << (i + 2)))) {
4217d8fb588SMatthias Schmidt #ifdef RCHECK
4227d8fb588SMatthias Schmidt 	/* JMR: formerly this wasn't updated ! */
4237d8fb588SMatthias Schmidt 	nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP);
4247d8fb588SMatthias Schmidt 	*((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC;
4257d8fb588SMatthias Schmidt 	op->ov_rmagic = RMAGIC;
426653fab9eSSascha Wildner 	op->ov_size = (op->ov_index <= 13) ? (U_short)nbytes - 1 : 0;
4277d8fb588SMatthias Schmidt #endif
4287d8fb588SMatthias Schmidt 	return ((memalign_t) cp);
4297d8fb588SMatthias Schmidt     }
4307d8fb588SMatthias Schmidt     if ((res = malloc(nbytes)) == NULL)
4317d8fb588SMatthias Schmidt 	return ((memalign_t) NULL);
4327d8fb588SMatthias Schmidt     if (cp != res) {		/* common optimization */
4337d8fb588SMatthias Schmidt 	/*
4347d8fb588SMatthias Schmidt 	 * christos: this used to copy nbytes! It should copy the
4357d8fb588SMatthias Schmidt 	 * smaller of the old and new size
4367d8fb588SMatthias Schmidt 	 */
4377d8fb588SMatthias Schmidt 	onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP;
4387d8fb588SMatthias Schmidt 	(void) memmove(res, cp, onb < nbytes ? onb : nbytes);
4397d8fb588SMatthias Schmidt     }
4407d8fb588SMatthias Schmidt     if (was_alloced)
4417d8fb588SMatthias Schmidt 	free(cp);
4427d8fb588SMatthias Schmidt     return ((memalign_t) res);
4437d8fb588SMatthias Schmidt #else
4447d8fb588SMatthias Schmidt     if (cp && nbytes)
4457d8fb588SMatthias Schmidt 	return ((memalign_t) 0);
4467d8fb588SMatthias Schmidt     else
4477d8fb588SMatthias Schmidt 	return ((memalign_t) 0);
4487d8fb588SMatthias Schmidt #endif /* !lint */
4497d8fb588SMatthias Schmidt }
4507d8fb588SMatthias Schmidt 
45194afa86dSJohn Marino /*
45294afa86dSJohn Marino  * On linux, _nss_nis_setnetgrent() calls this function to determine
45394afa86dSJohn Marino  * the usable size of the pointer passed, but this is not a portable
45494afa86dSJohn Marino  * API, so we cannot use our malloc replacement without providing one.
45594afa86dSJohn Marino  * Thanks a lot glibc!
45694afa86dSJohn Marino  */
45794afa86dSJohn Marino #ifdef __linux__
45894afa86dSJohn Marino #define M_U_S_CONST
45994afa86dSJohn Marino #elif defined(__DragonFly__)
46094afa86dSJohn Marino #define M_U_S_CONST const
46194afa86dSJohn Marino #else
46294afa86dSJohn Marino #define M_U_S_CONST
46394afa86dSJohn Marino #endif
46494afa86dSJohn Marino size_t malloc_usable_size(M_U_S_CONST void *);
46594afa86dSJohn Marino size_t
malloc_usable_size(M_U_S_CONST void * ptr)46694afa86dSJohn Marino malloc_usable_size(M_U_S_CONST void *ptr)
46794afa86dSJohn Marino {
46894afa86dSJohn Marino     const union overhead *op = (const union overhead *)
46960962bbcSJohn Marino 	(((const char *) ptr) - MEMALIGN(sizeof(*op)));
47094afa86dSJohn Marino     if (op->ov_magic == MAGIC)
47194afa86dSJohn Marino 	    return 1 << (op->ov_index + 3);
47294afa86dSJohn Marino     else
4737d8fb588SMatthias Schmidt 	    return 0;
4747d8fb588SMatthias Schmidt }
4757d8fb588SMatthias Schmidt 
4767d8fb588SMatthias Schmidt 
4777d8fb588SMatthias Schmidt #ifndef lint
4787d8fb588SMatthias Schmidt /*
4797d8fb588SMatthias Schmidt  * Search ``srchlen'' elements of each free list for a block whose
4807d8fb588SMatthias Schmidt  * header starts at ``freep''.  If srchlen is -1 search the whole list.
4817d8fb588SMatthias Schmidt  * Return bucket number, or -1 if not found.
4827d8fb588SMatthias Schmidt  */
4837d8fb588SMatthias Schmidt static int
findbucket(union overhead * freep,int srchlen)4847d8fb588SMatthias Schmidt findbucket(union overhead *freep, int srchlen)
4857d8fb588SMatthias Schmidt {
4867d8fb588SMatthias Schmidt     union overhead *p;
4877d8fb588SMatthias Schmidt     size_t i;
4887d8fb588SMatthias Schmidt     int j;
4897d8fb588SMatthias Schmidt 
4907d8fb588SMatthias Schmidt     for (i = 0; i < NBUCKETS; i++) {
4917d8fb588SMatthias Schmidt 	j = 0;
4927d8fb588SMatthias Schmidt 	for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
4937d8fb588SMatthias Schmidt 	    if (p == freep)
4947d8fb588SMatthias Schmidt 		return (i);
4957d8fb588SMatthias Schmidt 	    j++;
4967d8fb588SMatthias Schmidt 	}
4977d8fb588SMatthias Schmidt     }
4987d8fb588SMatthias Schmidt     return (-1);
4997d8fb588SMatthias Schmidt }
5007d8fb588SMatthias Schmidt 
5017d8fb588SMatthias Schmidt #endif
5027d8fb588SMatthias Schmidt 
5037d8fb588SMatthias Schmidt 
5047d8fb588SMatthias Schmidt #else				/* SYSMALLOC */
5057d8fb588SMatthias Schmidt 
5067d8fb588SMatthias Schmidt /**
5077d8fb588SMatthias Schmidt  ** ``Protected versions'' of malloc, realloc, calloc, and free
5087d8fb588SMatthias Schmidt  **
5097d8fb588SMatthias Schmidt  ** On many systems:
5107d8fb588SMatthias Schmidt  **
5117d8fb588SMatthias Schmidt  ** 1. malloc(0) is bad
5127d8fb588SMatthias Schmidt  ** 2. free(0) is bad
5137d8fb588SMatthias Schmidt  ** 3. realloc(0, n) is bad
5147d8fb588SMatthias Schmidt  ** 4. realloc(n, 0) is bad
5157d8fb588SMatthias Schmidt  **
5167d8fb588SMatthias Schmidt  ** Also we call our error routine if we run out of memory.
5177d8fb588SMatthias Schmidt  **/
5187d8fb588SMatthias Schmidt memalign_t
smalloc(size_t n)5197d8fb588SMatthias Schmidt smalloc(size_t n)
5207d8fb588SMatthias Schmidt {
5217d8fb588SMatthias Schmidt     ptr_t   ptr;
5227d8fb588SMatthias Schmidt 
52360962bbcSJohn Marino     n = n ? n : 1;
5247d8fb588SMatthias Schmidt 
5257d8fb588SMatthias Schmidt #ifdef USE_SBRK
52660962bbcSJohn Marino     if (membot == NULL)
5277d8fb588SMatthias Schmidt 	membot = sbrk(0);
5287d8fb588SMatthias Schmidt #endif /* USE_SBRK */
5297d8fb588SMatthias Schmidt 
53060962bbcSJohn Marino     if ((ptr = malloc(n)) == NULL)
5317d8fb588SMatthias Schmidt 	out_of_memory();
5327d8fb588SMatthias Schmidt #ifndef USE_SBRK
5337d8fb588SMatthias Schmidt     if (memtop < ((char *) ptr) + n)
5347d8fb588SMatthias Schmidt 	memtop = ((char *) ptr) + n;
53560962bbcSJohn Marino     if (membot == NULL)
5367d8fb588SMatthias Schmidt 	membot = ptr;
5377d8fb588SMatthias Schmidt #endif /* !USE_SBRK */
5387d8fb588SMatthias Schmidt     return ((memalign_t) ptr);
5397d8fb588SMatthias Schmidt }
5407d8fb588SMatthias Schmidt 
5417d8fb588SMatthias Schmidt memalign_t
srealloc(ptr_t p,size_t n)5427d8fb588SMatthias Schmidt srealloc(ptr_t p, size_t n)
5437d8fb588SMatthias Schmidt {
5447d8fb588SMatthias Schmidt     ptr_t   ptr;
5457d8fb588SMatthias Schmidt 
54660962bbcSJohn Marino     n = n ? n : 1;
5477d8fb588SMatthias Schmidt 
5487d8fb588SMatthias Schmidt #ifdef USE_SBRK
54960962bbcSJohn Marino     if (membot == NULL)
5507d8fb588SMatthias Schmidt 	membot = sbrk(0);
5517d8fb588SMatthias Schmidt #endif /* USE_SBRK */
5527d8fb588SMatthias Schmidt 
55360962bbcSJohn Marino     if ((ptr = (p ? realloc(p, n) : malloc(n))) == NULL)
5547d8fb588SMatthias Schmidt 	out_of_memory();
5557d8fb588SMatthias Schmidt #ifndef USE_SBRK
5567d8fb588SMatthias Schmidt     if (memtop < ((char *) ptr) + n)
5577d8fb588SMatthias Schmidt 	memtop = ((char *) ptr) + n;
55860962bbcSJohn Marino     if (membot == NULL)
5597d8fb588SMatthias Schmidt 	membot = ptr;
5607d8fb588SMatthias Schmidt #endif /* !USE_SBRK */
5617d8fb588SMatthias Schmidt     return ((memalign_t) ptr);
5627d8fb588SMatthias Schmidt }
5637d8fb588SMatthias Schmidt 
5647d8fb588SMatthias Schmidt memalign_t
scalloc(size_t s,size_t n)5657d8fb588SMatthias Schmidt scalloc(size_t s, size_t n)
5667d8fb588SMatthias Schmidt {
5677d8fb588SMatthias Schmidt     ptr_t   ptr;
5687d8fb588SMatthias Schmidt 
5697d8fb588SMatthias Schmidt     n *= s;
57060962bbcSJohn Marino     n = n ? n : 1;
5717d8fb588SMatthias Schmidt 
5727d8fb588SMatthias Schmidt #ifdef USE_SBRK
57360962bbcSJohn Marino     if (membot == NULL)
5747d8fb588SMatthias Schmidt 	membot = sbrk(0);
5757d8fb588SMatthias Schmidt #endif /* USE_SBRK */
5767d8fb588SMatthias Schmidt 
5777d8fb588SMatthias Schmidt     if ((ptr = malloc(n)) == NULL)
5787d8fb588SMatthias Schmidt 	out_of_memory();
5797d8fb588SMatthias Schmidt 
58060962bbcSJohn Marino     memset (ptr, 0, n);
5817d8fb588SMatthias Schmidt 
5827d8fb588SMatthias Schmidt #ifndef USE_SBRK
5837d8fb588SMatthias Schmidt     if (memtop < ((char *) ptr) + n)
5847d8fb588SMatthias Schmidt 	memtop = ((char *) ptr) + n;
58560962bbcSJohn Marino     if (membot == NULL)
5867d8fb588SMatthias Schmidt 	membot = ptr;
5877d8fb588SMatthias Schmidt #endif /* !USE_SBRK */
5887d8fb588SMatthias Schmidt 
5897d8fb588SMatthias Schmidt     return ((memalign_t) ptr);
5907d8fb588SMatthias Schmidt }
5917d8fb588SMatthias Schmidt 
5927d8fb588SMatthias Schmidt void
sfree(ptr_t p)5937d8fb588SMatthias Schmidt sfree(ptr_t p)
5947d8fb588SMatthias Schmidt {
5957d8fb588SMatthias Schmidt     if (p && !dont_free)
5967d8fb588SMatthias Schmidt 	free(p);
5977d8fb588SMatthias Schmidt }
5987d8fb588SMatthias Schmidt 
5997d8fb588SMatthias Schmidt #endif /* SYSMALLOC */
6007d8fb588SMatthias Schmidt 
6017d8fb588SMatthias Schmidt /*
6027d8fb588SMatthias Schmidt  * mstats - print out statistics about malloc
6037d8fb588SMatthias Schmidt  *
6047d8fb588SMatthias Schmidt  * Prints two lines of numbers, one showing the length of the free list
6057d8fb588SMatthias Schmidt  * for each size category, the second showing the number of mallocs -
6067d8fb588SMatthias Schmidt  * frees for each size category.
6077d8fb588SMatthias Schmidt  */
6087d8fb588SMatthias Schmidt /*ARGSUSED*/
6097d8fb588SMatthias Schmidt void
showall(Char ** v,struct command * c)6107d8fb588SMatthias Schmidt showall(Char **v, struct command *c)
6117d8fb588SMatthias Schmidt {
6127d8fb588SMatthias Schmidt #ifndef SYSMALLOC
6137d8fb588SMatthias Schmidt     size_t i, j;
6147d8fb588SMatthias Schmidt     union overhead *p;
6157d8fb588SMatthias Schmidt     int     totfree = 0, totused = 0;
6167d8fb588SMatthias Schmidt 
6177d8fb588SMatthias Schmidt     xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname);
6187d8fb588SMatthias Schmidt     for (i = 0; i < NBUCKETS; i++) {
6197d8fb588SMatthias Schmidt 	for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
6207d8fb588SMatthias Schmidt 	    continue;
6217d8fb588SMatthias Schmidt 	xprintf(" %4zd", j);
62294afa86dSJohn Marino 	totfree += j * (1 << (i + 3));
6237d8fb588SMatthias Schmidt     }
6247d8fb588SMatthias Schmidt     xprintf("\n%s:\t", CGETS(19, 9, "used"));
6257d8fb588SMatthias Schmidt     for (i = 0; i < NBUCKETS; i++) {
6267d8fb588SMatthias Schmidt 	xprintf(" %4d", nmalloc[i]);
6277d8fb588SMatthias Schmidt 	totused += nmalloc[i] * (1 << (i + 3));
6287d8fb588SMatthias Schmidt     }
6297d8fb588SMatthias Schmidt     xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"),
6307d8fb588SMatthias Schmidt 	    totused, totfree);
6317d8fb588SMatthias Schmidt     xprintf(CGETS(19, 11,
6327d8fb588SMatthias Schmidt 	    "\tAllocated memory from 0x%lx to 0x%lx.  Real top at 0x%lx\n"),
63394afa86dSJohn Marino 	    (unsigned long) membot, (unsigned long) memtop,
634*d6ab524cSAntonio Huete Jimenez 	    (unsigned long) sbrk(0));
63560962bbcSJohn Marino #else /* SYSMALLOC */
6367d8fb588SMatthias Schmidt #if !defined(HAVE_MALLINFO) && !defined(HAVE_MALLINFO2)
63760962bbcSJohn Marino #ifdef USE_SBRK
6387d8fb588SMatthias Schmidt     memtop = sbrk(0);
6397d8fb588SMatthias Schmidt #endif /* USE_SBRK */
6407d8fb588SMatthias Schmidt     xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"),
641*d6ab524cSAntonio Huete Jimenez 	    (unsigned long) membot, (unsigned long) memtop,
642*d6ab524cSAntonio Huete Jimenez 	    (unsigned long) (memtop - membot));
643*d6ab524cSAntonio Huete Jimenez #else
644*d6ab524cSAntonio Huete Jimenez # if defined(HAVE_MALLINFO2)
645*d6ab524cSAntonio Huete Jimenez     struct mallinfo2 mi;
646*d6ab524cSAntonio Huete Jimenez 
64794afa86dSJohn Marino     mi = mallinfo2();
64894afa86dSJohn Marino # else
64994afa86dSJohn Marino     struct mallinfo mi;
650*d6ab524cSAntonio Huete Jimenez 
65194afa86dSJohn Marino     mi = mallinfo();
652*d6ab524cSAntonio Huete Jimenez # endif
653*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 13, "%s current memory allocation:\n"), progname);
654*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 14, "Total space allocated from system: %zu\n"),
655*d6ab524cSAntonio Huete Jimenez 	(size_t)mi.arena);
656*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 15, "Number of non-inuse chunks: %zu\n"),
657*d6ab524cSAntonio Huete Jimenez 	(size_t)mi.ordblks);
658*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 16, "Number of mmapped regions: %zu\n"),
659*d6ab524cSAntonio Huete Jimenez 	(size_t)mi.hblks);
660*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 17, "Total space in mmapped regions: %zu\n"),
661*d6ab524cSAntonio Huete Jimenez 	(size_t)mi.hblkhd);
662*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 18, "Total allocated space: %zu\n"),
663*d6ab524cSAntonio Huete Jimenez 	(size_t)mi.uordblks);
664*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 19, "Total non-inuse space: %zu\n"),
665*d6ab524cSAntonio Huete Jimenez 	(size_t)mi.fordblks);
666*d6ab524cSAntonio Huete Jimenez     xprintf(CGETS(19, 20, "Top-most, releasable space: %zu\n"),
6677d8fb588SMatthias Schmidt 	(size_t)mi.keepcost);
6687d8fb588SMatthias Schmidt #endif /* HAVE_MALLINFO || HAVE_MALLINFO2 */
6697d8fb588SMatthias Schmidt #endif /* SYSMALLOC */
6707d8fb588SMatthias Schmidt     USE(c);
671*d6ab524cSAntonio Huete Jimenez     USE(v);
672*d6ab524cSAntonio Huete Jimenez }
673*d6ab524cSAntonio Huete Jimenez 
674*d6ab524cSAntonio Huete Jimenez #ifndef SYSMALLOC
675*d6ab524cSAntonio Huete Jimenez /* jemalloc defines these */
676*d6ab524cSAntonio Huete Jimenez void _malloc_prefork(void);
677*d6ab524cSAntonio Huete Jimenez void _malloc_postfork(void);
678*d6ab524cSAntonio Huete Jimenez void _malloc_postfork_child(void);
_malloc_prefork(void)679*d6ab524cSAntonio Huete Jimenez void _malloc_prefork(void) {}
_malloc_postfork(void)680*d6ab524cSAntonio Huete Jimenez void _malloc_postfork(void) {}
_malloc_postfork_child(void)681 void _malloc_postfork_child(void) {}
682 #endif
683