xref: /freebsd/contrib/tcsh/tc.alloc.c (revision 45e5710b)
145e5710bSMark Peek /*
2c80476e4SDavid E. O'Brien  * tc.alloc.c (Caltech) 2/21/82
3c80476e4SDavid E. O'Brien  * Chris Kingsley, kingsley@cit-20.
4c80476e4SDavid E. O'Brien  *
5c80476e4SDavid E. O'Brien  * This is a very fast storage allocator.  It allocates blocks of a small
6c80476e4SDavid E. O'Brien  * number of different sizes, and keeps free lists of each size.  Blocks that
7c80476e4SDavid E. O'Brien  * don't exactly fit are passed up to the next larger size.  In this
8c80476e4SDavid E. O'Brien  * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
9c80476e4SDavid E. O'Brien  * This is designed for use in a program that uses vast quantities of memory,
10c80476e4SDavid E. O'Brien  * but bombs when it runs out.
11c80476e4SDavid E. O'Brien  */
12c80476e4SDavid E. O'Brien /*-
13c80476e4SDavid E. O'Brien  * Copyright (c) 1980, 1991 The Regents of the University of California.
14c80476e4SDavid E. O'Brien  * All rights reserved.
15c80476e4SDavid E. O'Brien  *
16c80476e4SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
17c80476e4SDavid E. O'Brien  * modification, are permitted provided that the following conditions
18c80476e4SDavid E. O'Brien  * are met:
19c80476e4SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
20c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
21c80476e4SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
22c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
23c80476e4SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
24c80476e4SDavid E. O'Brien  * 3. Neither the name of the University nor the names of its contributors
2529301572SMark Peek  *    may be used to endorse or promote products derived from this software
26c80476e4SDavid E. O'Brien  *    without specific prior written permission.
27c80476e4SDavid E. O'Brien  *
28c80476e4SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29c80476e4SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30c80476e4SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31c80476e4SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32c80476e4SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33c80476e4SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34c80476e4SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35c80476e4SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36c80476e4SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37c80476e4SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38c80476e4SDavid E. O'Brien  * SUCH DAMAGE.
39c80476e4SDavid E. O'Brien  */
40c80476e4SDavid E. O'Brien #include "sh.h"
41c80476e4SDavid E. O'Brien #ifdef HAVE_MALLINFO
42c80476e4SDavid E. O'Brien #include <malloc.h>
4345e5710bSMark Peek #endif
4445e5710bSMark Peek #if defined(HAVE_SBRK) && !defined(__APPLE__)
4545e5710bSMark Peek #define USE_SBRK
4645e5710bSMark Peek #endif
47c80476e4SDavid E. O'Brien 
48c80476e4SDavid E. O'Brien #define RCHECK
49c80476e4SDavid E. O'Brien #define DEBUG
50c80476e4SDavid E. O'Brien 
51c80476e4SDavid E. O'Brien static char   *memtop = NULL;		/* PWP: top of current memory */
52c80476e4SDavid E. O'Brien static char   *membot = NULL;		/* PWP: bottom of allocatable memory */
533b6eaa7bSAndrey A. Chernov 
54c80476e4SDavid E. O'Brien int dont_free = 0;
55c80476e4SDavid E. O'Brien 
56c80476e4SDavid E. O'Brien #ifdef WINNT_NATIVE
57c80476e4SDavid E. O'Brien # define malloc		fmalloc
583b6eaa7bSAndrey A. Chernov # define free		ffree
59c80476e4SDavid E. O'Brien # define calloc		fcalloc
6045e5710bSMark Peek # define realloc	frealloc
6145e5710bSMark Peek #endif /* WINNT_NATIVE */
6245e5710bSMark Peek 
6345e5710bSMark Peek #if !defined(DEBUG) || defined(SYSMALLOC)
6445e5710bSMark Peek static void
out_of_memory(void)65c80476e4SDavid E. O'Brien out_of_memory (void)
6645e5710bSMark Peek {
6745e5710bSMark Peek     static const char msg[] = "Out of memory\n";
6845e5710bSMark Peek 
6945e5710bSMark Peek     TCSH_IGNORE(write(didfds ? 2 : SHDIAG, msg, strlen(msg)));
7045e5710bSMark Peek     _exit(1);
7145e5710bSMark Peek }
72c80476e4SDavid E. O'Brien #endif
73c80476e4SDavid E. O'Brien 
74c80476e4SDavid E. O'Brien #ifndef SYSMALLOC
75c80476e4SDavid E. O'Brien 
76c80476e4SDavid E. O'Brien #ifdef SX
77c80476e4SDavid E. O'Brien extern void* sbrk();
78c80476e4SDavid E. O'Brien #endif
79c80476e4SDavid E. O'Brien /*
80c80476e4SDavid E. O'Brien  * Lots of os routines are busted and try to free invalid pointers.
81c80476e4SDavid E. O'Brien  * Although our free routine is smart enough and it will pick bad
82c80476e4SDavid E. O'Brien  * pointers most of the time, in cases where we know we are going to get
83c80476e4SDavid E. O'Brien  * a bad pointer, we'd rather leak.
84c80476e4SDavid E. O'Brien  */
85c80476e4SDavid E. O'Brien 
86c80476e4SDavid E. O'Brien #ifndef NULL
87c80476e4SDavid E. O'Brien #define	NULL 0
88c80476e4SDavid E. O'Brien #endif
89c80476e4SDavid E. O'Brien 
90c80476e4SDavid E. O'Brien typedef unsigned char U_char;	/* we don't really have signed chars */
91c80476e4SDavid E. O'Brien typedef unsigned int U_int;
92c80476e4SDavid E. O'Brien typedef unsigned short U_short;
93c80476e4SDavid E. O'Brien typedef unsigned long U_long;
94c80476e4SDavid E. O'Brien 
95c80476e4SDavid E. O'Brien 
96c80476e4SDavid E. O'Brien /*
97c80476e4SDavid E. O'Brien  * The overhead on a block is at least 4 bytes.  When free, this space
98c80476e4SDavid E. O'Brien  * contains a pointer to the next free block, and the bottom two bits must
99c80476e4SDavid E. O'Brien  * be zero.  When in use, the first byte is set to MAGIC, and the second
100c80476e4SDavid E. O'Brien  * byte is the size index.  The remaining bytes are for alignment.
101c80476e4SDavid E. O'Brien  * If range checking is enabled and the size of the block fits
102c80476e4SDavid E. O'Brien  * in two bytes, then the top two bytes hold the size of the requested block
103c80476e4SDavid E. O'Brien  * plus the range checking words, and the header word MINUS ONE.
104c80476e4SDavid E. O'Brien  */
105c80476e4SDavid E. O'Brien 
106c80476e4SDavid E. O'Brien 
107c80476e4SDavid E. O'Brien #define MEMALIGN(a) (((a) + ROUNDUP) & ~ROUNDUP)
108c80476e4SDavid E. O'Brien 
109c80476e4SDavid E. O'Brien union overhead {
110c80476e4SDavid E. O'Brien     union overhead *ov_next;	/* when free */
111c80476e4SDavid E. O'Brien     struct {
112c80476e4SDavid E. O'Brien 	U_char  ovu_magic;	/* magic number */
113c80476e4SDavid E. O'Brien 	U_char  ovu_index;	/* bucket # */
114c80476e4SDavid E. O'Brien #ifdef RCHECK
115c80476e4SDavid E. O'Brien 	U_short ovu_size;	/* actual block size */
116c80476e4SDavid E. O'Brien 	U_int   ovu_rmagic;	/* range magic number */
117c80476e4SDavid E. O'Brien #endif
118c80476e4SDavid E. O'Brien     }       ovu;
119c80476e4SDavid E. O'Brien #define	ov_magic	ovu.ovu_magic
120c80476e4SDavid E. O'Brien #define	ov_index	ovu.ovu_index
121c80476e4SDavid E. O'Brien #define	ov_size		ovu.ovu_size
122c80476e4SDavid E. O'Brien #define	ov_rmagic	ovu.ovu_rmagic
123c80476e4SDavid E. O'Brien };
124c80476e4SDavid E. O'Brien 
125c80476e4SDavid E. O'Brien #define	MAGIC		0xfd	/* magic # on accounting info */
126c80476e4SDavid E. O'Brien #define RMAGIC		0x55555555	/* magic # on range info */
127c80476e4SDavid E. O'Brien #ifdef RCHECK
128c80476e4SDavid E. O'Brien #define	RSLOP		sizeof (U_int)
129c80476e4SDavid E. O'Brien #else
130c80476e4SDavid E. O'Brien #define	RSLOP		0
131c80476e4SDavid E. O'Brien #endif
132c80476e4SDavid E. O'Brien 
133c80476e4SDavid E. O'Brien 
134c80476e4SDavid E. O'Brien #ifdef _LP64
135c80476e4SDavid E. O'Brien #define ROUNDUP	15
136c80476e4SDavid E. O'Brien #else
137c80476e4SDavid E. O'Brien #define ROUNDUP	7
138c80476e4SDavid E. O'Brien #endif
139c80476e4SDavid E. O'Brien 
140c80476e4SDavid E. O'Brien /*
141c80476e4SDavid E. O'Brien  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
142c80476e4SDavid E. O'Brien  * smallest allocatable block is 8 bytes.  The overhead information
143c80476e4SDavid E. O'Brien  * precedes the data area returned to the user.
144c80476e4SDavid E. O'Brien  */
145c80476e4SDavid E. O'Brien #define	NBUCKETS ((sizeof(long) << 3) - 3)
146c80476e4SDavid E. O'Brien static union overhead *nextf[NBUCKETS] IZERO_STRUCT;
147c80476e4SDavid E. O'Brien 
14845e5710bSMark Peek /*
14945e5710bSMark Peek  * nmalloc[i] is the difference between the number of mallocs and frees
150c80476e4SDavid E. O'Brien  * for a given block size.
151c80476e4SDavid E. O'Brien  */
152c80476e4SDavid E. O'Brien static U_int nmalloc[NBUCKETS] IZERO_STRUCT;
153c80476e4SDavid E. O'Brien 
154c80476e4SDavid E. O'Brien #ifndef lint
155c80476e4SDavid E. O'Brien static	int	findbucket	(union overhead *, int);
156c80476e4SDavid E. O'Brien static	void	morecore	(int);
15745e5710bSMark Peek #endif
158c80476e4SDavid E. O'Brien 
159c80476e4SDavid E. O'Brien 
160c80476e4SDavid E. O'Brien #ifdef DEBUG
161c80476e4SDavid E. O'Brien # define CHECK(a, str, p) \
162c80476e4SDavid E. O'Brien     if (a) { \
163c80476e4SDavid E. O'Brien 	xprintf(str, p);	\
16445e5710bSMark Peek 	xprintf(" (memtop = %p membot = %p)\n", memtop, membot);	\
165c80476e4SDavid E. O'Brien 	abort(); \
166c80476e4SDavid E. O'Brien     }
167c80476e4SDavid E. O'Brien #else
168c80476e4SDavid E. O'Brien # define CHECK(a, str, p) \
169c80476e4SDavid E. O'Brien     if (a) { \
17045e5710bSMark Peek 	xprintf(str, p);	\
171c80476e4SDavid E. O'Brien 	xprintf(" (memtop = %p membot = %p)\n", memtop, membot);	\
172c80476e4SDavid E. O'Brien 	return; \
17323338178SMark Peek     }
17423338178SMark Peek #endif
17523338178SMark Peek 
176c80476e4SDavid E. O'Brien memalign_t
malloc(size_t nbytes)177c80476e4SDavid E. O'Brien malloc(size_t nbytes)
178c80476e4SDavid E. O'Brien {
179c80476e4SDavid E. O'Brien #ifndef lint
180c80476e4SDavid E. O'Brien     union overhead *p;
181c80476e4SDavid E. O'Brien     int bucket = 0;
182c80476e4SDavid E. O'Brien     unsigned shiftr;
183c80476e4SDavid E. O'Brien 
184c80476e4SDavid E. O'Brien     /*
185c80476e4SDavid E. O'Brien      * Convert amount of memory requested into closest block size stored in
186c80476e4SDavid E. O'Brien      * hash buckets which satisfies request.  Account for space used per block
187c80476e4SDavid E. O'Brien      * for accounting.
188c80476e4SDavid E. O'Brien      */
189c80476e4SDavid E. O'Brien #ifdef SUNOS4
190c80476e4SDavid E. O'Brien     /*
191c80476e4SDavid E. O'Brien      * SunOS localtime() overwrites the 9th byte on an 8 byte malloc()....
192c80476e4SDavid E. O'Brien      * so we get one more...
193c80476e4SDavid E. O'Brien      * From Michael Schroeder: This is not true. It depends on the
194c80476e4SDavid E. O'Brien      * timezone string. In Europe it can overwrite the 13th byte on a
195c80476e4SDavid E. O'Brien      * 12 byte malloc.
196c80476e4SDavid E. O'Brien      * So we punt and we always allocate an extra byte.
197c80476e4SDavid E. O'Brien      */
198c80476e4SDavid E. O'Brien     nbytes++;
199c80476e4SDavid E. O'Brien #endif
200c80476e4SDavid E. O'Brien 
201c80476e4SDavid E. O'Brien     nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead)) + nbytes + RSLOP);
202c80476e4SDavid E. O'Brien     shiftr = (nbytes - 1) >> 2;
203c80476e4SDavid E. O'Brien 
204c80476e4SDavid E. O'Brien     /* apart from this loop, this is O(1) */
205c80476e4SDavid E. O'Brien     while ((shiftr >>= 1) != 0)
20645e5710bSMark Peek 	bucket++;
207c80476e4SDavid E. O'Brien     /*
208c80476e4SDavid E. O'Brien      * If nothing in hash bucket right now, request more memory from the
20945e5710bSMark Peek      * system.
210c80476e4SDavid E. O'Brien      */
211c80476e4SDavid E. O'Brien     if (nextf[bucket] == NULL)
21245e5710bSMark Peek 	morecore(bucket);
213c80476e4SDavid E. O'Brien     if ((p = nextf[bucket]) == NULL) {
214c80476e4SDavid E. O'Brien 	child++;
215c80476e4SDavid E. O'Brien #ifndef DEBUG
216c80476e4SDavid E. O'Brien 	out_of_memory();
217c80476e4SDavid E. O'Brien #else
218c80476e4SDavid E. O'Brien 	showall(NULL, NULL);
219c80476e4SDavid E. O'Brien 	xprintf(CGETS(19, 1, "nbytes=%zu: Out of memory\n"), nbytes);
220c80476e4SDavid E. O'Brien 	abort();
221c80476e4SDavid E. O'Brien #endif
222c80476e4SDavid E. O'Brien 	/* fool lint */
223c80476e4SDavid E. O'Brien 	return ((memalign_t) 0);
224c80476e4SDavid E. O'Brien     }
225c80476e4SDavid E. O'Brien     /* remove from linked list */
226c80476e4SDavid E. O'Brien     nextf[bucket] = nextf[bucket]->ov_next;
227c80476e4SDavid E. O'Brien     p->ov_magic = MAGIC;
228c80476e4SDavid E. O'Brien     p->ov_index = bucket;
229c80476e4SDavid E. O'Brien     nmalloc[bucket]++;
230c80476e4SDavid E. O'Brien #ifdef RCHECK
231c80476e4SDavid E. O'Brien     /*
232c80476e4SDavid E. O'Brien      * Record allocated size of block and bound space with magic numbers.
233c80476e4SDavid E. O'Brien      */
234c80476e4SDavid E. O'Brien     p->ov_size = (p->ov_index <= 13) ? (U_short)nbytes - 1 : 0;
235c80476e4SDavid E. O'Brien     p->ov_rmagic = RMAGIC;
236c80476e4SDavid E. O'Brien     *((U_int *) (((caddr_t) p) + nbytes - RSLOP)) = RMAGIC;
237c80476e4SDavid E. O'Brien #endif
238c80476e4SDavid E. O'Brien     return ((memalign_t) (((caddr_t) p) + MEMALIGN(sizeof(union overhead))));
239c80476e4SDavid E. O'Brien #else
240c80476e4SDavid E. O'Brien     if (nbytes)
241c80476e4SDavid E. O'Brien 	return ((memalign_t) 0);
242c80476e4SDavid E. O'Brien     else
243c80476e4SDavid E. O'Brien 	return ((memalign_t) 0);
244c80476e4SDavid E. O'Brien #endif /* !lint */
24545e5710bSMark Peek }
246c80476e4SDavid E. O'Brien 
24723338178SMark Peek #ifndef lint
24823338178SMark Peek /*
24923338178SMark Peek  * Allocate more memory to the indicated bucket.
25023338178SMark Peek  */
251c80476e4SDavid E. O'Brien static void
morecore(int bucket)252c80476e4SDavid E. O'Brien morecore(int bucket)
253c80476e4SDavid E. O'Brien {
254c80476e4SDavid E. O'Brien     union overhead *op;
255c80476e4SDavid E. O'Brien     int rnu;		/* 2^rnu bytes will be requested */
256c80476e4SDavid E. O'Brien     int nblks;		/* become nblks blocks of the desired size */
257c80476e4SDavid E. O'Brien     int siz;
258c80476e4SDavid E. O'Brien 
259c80476e4SDavid E. O'Brien     if (nextf[bucket])
260c80476e4SDavid E. O'Brien 	return;
261c80476e4SDavid E. O'Brien     /*
262c80476e4SDavid E. O'Brien      * Insure memory is allocated on a page boundary.  Should make getpageize
26345e5710bSMark Peek      * call?
264c80476e4SDavid E. O'Brien      */
265c80476e4SDavid E. O'Brien     op = (union overhead *) sbrk(0);
266c80476e4SDavid E. O'Brien     memtop = (char *) op;
267c80476e4SDavid E. O'Brien     if (membot == NULL)
268c80476e4SDavid E. O'Brien 	membot = memtop;
269c80476e4SDavid E. O'Brien     if ((long) op & 0x3ff) {
27045e5710bSMark Peek 	memtop = sbrk((int) (1024 - ((long) op & 0x3ff)));
271c80476e4SDavid E. O'Brien 	memtop += (long) (1024 - ((long) op & 0x3ff));
272c80476e4SDavid E. O'Brien     }
273c80476e4SDavid E. O'Brien 
274c80476e4SDavid E. O'Brien     /* take 2k unless the block is bigger than that */
275c80476e4SDavid E. O'Brien     rnu = (bucket <= 8) ? 11 : bucket + 3;
276c80476e4SDavid E. O'Brien     nblks = 1 << (rnu - (bucket + 3));	/* how many blocks to get */
277c80476e4SDavid E. O'Brien     memtop = sbrk(1 << rnu);	/* PWP */
278c80476e4SDavid E. O'Brien     op = (union overhead *) memtop;
279c80476e4SDavid E. O'Brien     /* no more room! */
280c80476e4SDavid E. O'Brien     if ((long) op == -1)
281c80476e4SDavid E. O'Brien 	return;
282c80476e4SDavid E. O'Brien     memtop += (long) (1 << rnu);
283c80476e4SDavid E. O'Brien     /*
284c80476e4SDavid E. O'Brien      * Round up to minimum allocation size boundary and deduct from block count
285c80476e4SDavid E. O'Brien      * to reflect.
286c80476e4SDavid E. O'Brien      */
287c80476e4SDavid E. O'Brien     if (((U_long) op) & ROUNDUP) {
288c80476e4SDavid E. O'Brien 	op = (union overhead *) (((U_long) op + (ROUNDUP + 1)) & ~ROUNDUP);
289c80476e4SDavid E. O'Brien 	nblks--;
290c80476e4SDavid E. O'Brien     }
291c80476e4SDavid E. O'Brien     /*
292c80476e4SDavid E. O'Brien      * Add new memory allocated to that on free list for this hash bucket.
293c80476e4SDavid E. O'Brien      */
294c80476e4SDavid E. O'Brien     nextf[bucket] = op;
295c80476e4SDavid E. O'Brien     siz = 1 << (bucket + 3);
296c80476e4SDavid E. O'Brien     while (--nblks > 0) {
297c80476e4SDavid E. O'Brien 	op->ov_next = (union overhead *) (((caddr_t) op) + siz);
298c80476e4SDavid E. O'Brien 	op = (union overhead *) (((caddr_t) op) + siz);
29945e5710bSMark Peek     }
300c80476e4SDavid E. O'Brien     op->ov_next = NULL;
301c80476e4SDavid E. O'Brien }
30223338178SMark Peek 
30323338178SMark Peek #endif
304c80476e4SDavid E. O'Brien 
305c80476e4SDavid E. O'Brien void
free(ptr_t cp)306c80476e4SDavid E. O'Brien free(ptr_t cp)
307c80476e4SDavid E. O'Brien {
308c80476e4SDavid E. O'Brien #ifndef lint
309c80476e4SDavid E. O'Brien     int size;
310c80476e4SDavid E. O'Brien     union overhead *op;
311c80476e4SDavid E. O'Brien 
31245e5710bSMark Peek     /*
313c80476e4SDavid E. O'Brien      * the don't free flag is there so that we avoid os bugs in routines
31445e5710bSMark Peek      * that free invalid pointers!
315c80476e4SDavid E. O'Brien      */
31645e5710bSMark Peek     if (cp == NULL || dont_free)
317c80476e4SDavid E. O'Brien 	return;
318c80476e4SDavid E. O'Brien     CHECK(!memtop || !membot,
31945e5710bSMark Peek 	  CGETS(19, 2, "free(%p) called before any allocations."), cp);
320c80476e4SDavid E. O'Brien     CHECK(cp > (ptr_t) memtop,
321c80476e4SDavid E. O'Brien 	  CGETS(19, 3, "free(%p) above top of memory."), cp);
322c80476e4SDavid E. O'Brien     CHECK(cp < (ptr_t) membot,
323c80476e4SDavid E. O'Brien 	  CGETS(19, 4, "free(%p) below bottom of memory."), cp);
32445e5710bSMark Peek     op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
325c80476e4SDavid E. O'Brien     CHECK(op->ov_magic != MAGIC,
326c80476e4SDavid E. O'Brien 	  CGETS(19, 5, "free(%p) bad block."), cp);
32745e5710bSMark Peek 
328c80476e4SDavid E. O'Brien #ifdef RCHECK
329c80476e4SDavid E. O'Brien     if (op->ov_index <= 13)
330c80476e4SDavid E. O'Brien 	CHECK(*(U_int *) ((caddr_t) op + op->ov_size + 1 - RSLOP) != RMAGIC,
331c80476e4SDavid E. O'Brien 	      CGETS(19, 6, "free(%p) bad range check."), cp);
332c80476e4SDavid E. O'Brien #endif
333c80476e4SDavid E. O'Brien     CHECK(op->ov_index >= NBUCKETS,
334c80476e4SDavid E. O'Brien 	  CGETS(19, 7, "free(%p) bad block index."), cp);
335c80476e4SDavid E. O'Brien     size = op->ov_index;
336c80476e4SDavid E. O'Brien     op->ov_next = nextf[size];
337c80476e4SDavid E. O'Brien     nextf[size] = op;
338c80476e4SDavid E. O'Brien 
339c80476e4SDavid E. O'Brien     nmalloc[size]--;
340c80476e4SDavid E. O'Brien 
34145e5710bSMark Peek #else
342c80476e4SDavid E. O'Brien     if (cp == NULL)
343c80476e4SDavid E. O'Brien 	return;
34445e5710bSMark Peek #endif
345c80476e4SDavid E. O'Brien }
346c80476e4SDavid E. O'Brien 
34745e5710bSMark Peek memalign_t
calloc(size_t i,size_t j)34845e5710bSMark Peek calloc(size_t i, size_t j)
349c80476e4SDavid E. O'Brien {
35045e5710bSMark Peek #ifndef lint
351c80476e4SDavid E. O'Brien     char *cp;
352c80476e4SDavid E. O'Brien     volatile size_t k;
353c80476e4SDavid E. O'Brien 
354c80476e4SDavid E. O'Brien     i *= j;
355c80476e4SDavid E. O'Brien     cp = xmalloc(i);
356c80476e4SDavid E. O'Brien     /* Stop gcc 5.x from optimizing malloc+memset = calloc */
357c80476e4SDavid E. O'Brien     k = i;
358c80476e4SDavid E. O'Brien     memset(cp, 0, k);
359c80476e4SDavid E. O'Brien 
360c80476e4SDavid E. O'Brien     return ((memalign_t) cp);
361c80476e4SDavid E. O'Brien #else
362c80476e4SDavid E. O'Brien     if (i && j)
363c80476e4SDavid E. O'Brien 	return ((memalign_t) 0);
364c80476e4SDavid E. O'Brien     else
365c80476e4SDavid E. O'Brien 	return ((memalign_t) 0);
366c80476e4SDavid E. O'Brien #endif
367c80476e4SDavid E. O'Brien }
368c80476e4SDavid E. O'Brien 
369c80476e4SDavid E. O'Brien /*
370c80476e4SDavid E. O'Brien  * When a program attempts "storage compaction" as mentioned in the
371c80476e4SDavid E. O'Brien  * old malloc man page, it realloc's an already freed block.  Usually
372c80476e4SDavid E. O'Brien  * this is the last block it freed; occasionally it might be farther
373c80476e4SDavid E. O'Brien  * back.  We have to search all the free lists for the block in order
374c80476e4SDavid E. O'Brien  * to determine its bucket: 1st we make one pass thru the lists
375c80476e4SDavid E. O'Brien  * checking only the first block in each; if that fails we search
37645e5710bSMark Peek  * ``realloc_srchlen'' blocks in each list for a match (the variable
377c80476e4SDavid E. O'Brien  * is extern so the caller can modify it).  If that fails we just copy
378c80476e4SDavid E. O'Brien  * however many bytes was given to realloc() and hope it's not huge.
37923338178SMark Peek  */
380c80476e4SDavid E. O'Brien #ifndef lint
381c80476e4SDavid E. O'Brien /* 4 should be plenty, -1 =>'s whole list */
38223338178SMark Peek static int     realloc_srchlen = 4;
383c80476e4SDavid E. O'Brien #endif /* lint */
384c80476e4SDavid E. O'Brien 
385c80476e4SDavid E. O'Brien memalign_t
realloc(ptr_t cp,size_t nbytes)386c80476e4SDavid E. O'Brien realloc(ptr_t cp, size_t nbytes)
387c80476e4SDavid E. O'Brien {
388c80476e4SDavid E. O'Brien #ifndef lint
389c80476e4SDavid E. O'Brien     U_int onb;
390c80476e4SDavid E. O'Brien     union overhead *op;
391c80476e4SDavid E. O'Brien     ptr_t res;
392c80476e4SDavid E. O'Brien     int i;
393c80476e4SDavid E. O'Brien     int     was_alloced = 0;
394c80476e4SDavid E. O'Brien 
395c80476e4SDavid E. O'Brien     if (cp == NULL)
396c80476e4SDavid E. O'Brien 	return (malloc(nbytes));
397c80476e4SDavid E. O'Brien     op = (union overhead *) (((caddr_t) cp) - MEMALIGN(sizeof(union overhead)));
398c80476e4SDavid E. O'Brien     if (op->ov_magic == MAGIC) {
399c80476e4SDavid E. O'Brien 	was_alloced++;
400c80476e4SDavid E. O'Brien 	i = op->ov_index;
401c80476e4SDavid E. O'Brien     }
402c80476e4SDavid E. O'Brien     else
403c80476e4SDavid E. O'Brien 	/*
404c80476e4SDavid E. O'Brien 	 * Already free, doing "compaction".
405c80476e4SDavid E. O'Brien 	 *
406c80476e4SDavid E. O'Brien 	 * Search for the old block of memory on the free list.  First, check the
407c80476e4SDavid E. O'Brien 	 * most common case (last element free'd), then (this failing) the last
408c80476e4SDavid E. O'Brien 	 * ``realloc_srchlen'' items free'd. If all lookups fail, then assume
409c80476e4SDavid E. O'Brien 	 * the size of the memory block being realloc'd is the smallest
410c80476e4SDavid E. O'Brien 	 * possible.
411c80476e4SDavid E. O'Brien 	 */
412c80476e4SDavid E. O'Brien 	if ((i = findbucket(op, 1)) < 0 &&
413c80476e4SDavid E. O'Brien 	    (i = findbucket(op, realloc_srchlen)) < 0)
414c80476e4SDavid E. O'Brien 	    i = 0;
415c80476e4SDavid E. O'Brien 
416c80476e4SDavid E. O'Brien     onb = MEMALIGN(nbytes + MEMALIGN(sizeof(union overhead)) + RSLOP);
417c80476e4SDavid E. O'Brien 
418c80476e4SDavid E. O'Brien     /* avoid the copy if same size block */
419c80476e4SDavid E. O'Brien     if (was_alloced && (onb <= (U_int) (1 << (i + 3))) &&
420c80476e4SDavid E. O'Brien 	(onb > (U_int) (1 << (i + 2)))) {
421c80476e4SDavid E. O'Brien #ifdef RCHECK
422c80476e4SDavid E. O'Brien 	/* JMR: formerly this wasn't updated ! */
423c80476e4SDavid E. O'Brien 	nbytes = MEMALIGN(MEMALIGN(sizeof(union overhead))+nbytes+RSLOP);
424c80476e4SDavid E. O'Brien 	*((U_int *) (((caddr_t) op) + nbytes - RSLOP)) = RMAGIC;
425c80476e4SDavid E. O'Brien 	op->ov_rmagic = RMAGIC;
426c80476e4SDavid E. O'Brien 	op->ov_size = (op->ov_index <= 13) ? (U_short)nbytes - 1 : 0;
427c80476e4SDavid E. O'Brien #endif
42845e5710bSMark Peek 	return ((memalign_t) cp);
429c80476e4SDavid E. O'Brien     }
430c80476e4SDavid E. O'Brien     if ((res = malloc(nbytes)) == NULL)
431c80476e4SDavid E. O'Brien 	return ((memalign_t) NULL);
432c80476e4SDavid E. O'Brien     if (cp != res) {		/* common optimization */
433c80476e4SDavid E. O'Brien 	/*
434c80476e4SDavid E. O'Brien 	 * christos: this used to copy nbytes! It should copy the
435c80476e4SDavid E. O'Brien 	 * smaller of the old and new size
436c80476e4SDavid E. O'Brien 	 */
437c80476e4SDavid E. O'Brien 	onb = (1 << (i + 3)) - MEMALIGN(sizeof(union overhead)) - RSLOP;
438c80476e4SDavid E. O'Brien 	(void) memmove(res, cp, onb < nbytes ? onb : nbytes);
439c80476e4SDavid E. O'Brien     }
440c80476e4SDavid E. O'Brien     if (was_alloced)
441c80476e4SDavid E. O'Brien 	free(cp);
442c80476e4SDavid E. O'Brien     return ((memalign_t) res);
443c80476e4SDavid E. O'Brien #else
444c80476e4SDavid E. O'Brien     if (cp && nbytes)
445c80476e4SDavid E. O'Brien 	return ((memalign_t) 0);
446c80476e4SDavid E. O'Brien     else
447c80476e4SDavid E. O'Brien 	return ((memalign_t) 0);
448c80476e4SDavid E. O'Brien #endif /* !lint */
449c80476e4SDavid E. O'Brien }
45045e5710bSMark Peek 
451c80476e4SDavid E. O'Brien /*
45223338178SMark Peek  * On linux, _nss_nis_setnetgrent() calls this function to determine
45323338178SMark Peek  * the usable size of the pointer passed, but this is not a portable
45423338178SMark Peek  * API, so we cannot use our malloc replacement without providing one.
455c80476e4SDavid E. O'Brien  * Thanks a lot glibc!
456c80476e4SDavid E. O'Brien  */
457c80476e4SDavid E. O'Brien #ifdef __linux__
458c80476e4SDavid E. O'Brien #define M_U_S_CONST
459c80476e4SDavid E. O'Brien #else
460c80476e4SDavid E. O'Brien #define M_U_S_CONST
461c80476e4SDavid E. O'Brien #endif
462c80476e4SDavid E. O'Brien size_t malloc_usable_size(M_U_S_CONST void *);
463c80476e4SDavid E. O'Brien size_t
malloc_usable_size(M_U_S_CONST void * ptr)464c80476e4SDavid E. O'Brien malloc_usable_size(M_U_S_CONST void *ptr)
465c80476e4SDavid E. O'Brien {
466c80476e4SDavid E. O'Brien     const union overhead *op = (const union overhead *)
467c80476e4SDavid E. O'Brien 	(((const char *) ptr) - MEMALIGN(sizeof(*op)));
468c80476e4SDavid E. O'Brien     if (op->ov_magic == MAGIC)
469c80476e4SDavid E. O'Brien 	    return 1 << (op->ov_index + 3);
470c80476e4SDavid E. O'Brien     else
471c80476e4SDavid E. O'Brien 	    return 0;
472c80476e4SDavid E. O'Brien }
473c80476e4SDavid E. O'Brien 
474c80476e4SDavid E. O'Brien 
475c80476e4SDavid E. O'Brien #ifndef lint
476c80476e4SDavid E. O'Brien /*
477c80476e4SDavid E. O'Brien  * Search ``srchlen'' elements of each free list for a block whose
478c80476e4SDavid E. O'Brien  * header starts at ``freep''.  If srchlen is -1 search the whole list.
479c80476e4SDavid E. O'Brien  * Return bucket number, or -1 if not found.
480c80476e4SDavid E. O'Brien  */
481c80476e4SDavid E. O'Brien static int
findbucket(union overhead * freep,int srchlen)482c80476e4SDavid E. O'Brien findbucket(union overhead *freep, int srchlen)
483c80476e4SDavid E. O'Brien {
484c80476e4SDavid E. O'Brien     union overhead *p;
48545e5710bSMark Peek     size_t i;
486c80476e4SDavid E. O'Brien     int j;
487c80476e4SDavid E. O'Brien 
488c80476e4SDavid E. O'Brien     for (i = 0; i < NBUCKETS; i++) {
489c80476e4SDavid E. O'Brien 	j = 0;
490c80476e4SDavid E. O'Brien 	for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
49123338178SMark Peek 	    if (p == freep)
492c80476e4SDavid E. O'Brien 		return (i);
49345e5710bSMark Peek 	    j++;
49423338178SMark Peek 	}
495c80476e4SDavid E. O'Brien     }
49645e5710bSMark Peek     return (-1);
49745e5710bSMark Peek }
49823338178SMark Peek 
499c80476e4SDavid E. O'Brien #endif
500c80476e4SDavid E. O'Brien 
501c80476e4SDavid E. O'Brien 
50245e5710bSMark Peek #else				/* SYSMALLOC */
50323338178SMark Peek 
504c80476e4SDavid E. O'Brien /**
505c80476e4SDavid E. O'Brien  ** ``Protected versions'' of malloc, realloc, calloc, and free
506c80476e4SDavid E. O'Brien  **
507c80476e4SDavid E. O'Brien  ** On many systems:
50845e5710bSMark Peek  **
509c80476e4SDavid E. O'Brien  ** 1. malloc(0) is bad
510c80476e4SDavid E. O'Brien  ** 2. free(0) is bad
511c80476e4SDavid E. O'Brien  ** 3. realloc(0, n) is bad
512c80476e4SDavid E. O'Brien  ** 4. realloc(n, 0) is bad
513c80476e4SDavid E. O'Brien  **
51423338178SMark Peek  ** Also we call our error routine if we run out of memory.
515c80476e4SDavid E. O'Brien  **/
51645e5710bSMark Peek memalign_t
smalloc(size_t n)51723338178SMark Peek smalloc(size_t n)
518c80476e4SDavid E. O'Brien {
51945e5710bSMark Peek     ptr_t   ptr;
52045e5710bSMark Peek 
52123338178SMark Peek     n = n ? n : 1;
522c80476e4SDavid E. O'Brien 
523c80476e4SDavid E. O'Brien #ifdef USE_SBRK
524c80476e4SDavid E. O'Brien     if (membot == NULL)
52545e5710bSMark Peek 	membot = sbrk(0);
52623338178SMark Peek #endif /* USE_SBRK */
527c80476e4SDavid E. O'Brien 
528c80476e4SDavid E. O'Brien     if ((ptr = malloc(n)) == NULL)
529c80476e4SDavid E. O'Brien 	out_of_memory();
530c80476e4SDavid E. O'Brien #ifndef USE_SBRK
53145e5710bSMark Peek     if (memtop < ((char *) ptr) + n)
532c80476e4SDavid E. O'Brien 	memtop = ((char *) ptr) + n;
533c80476e4SDavid E. O'Brien     if (membot == NULL)
534c80476e4SDavid E. O'Brien 	membot = ptr;
535c80476e4SDavid E. O'Brien #endif /* !USE_SBRK */
536c80476e4SDavid E. O'Brien     return ((memalign_t) ptr);
537c80476e4SDavid E. O'Brien }
53823338178SMark Peek 
539c80476e4SDavid E. O'Brien memalign_t
srealloc(ptr_t p,size_t n)54045e5710bSMark Peek srealloc(ptr_t p, size_t n)
54123338178SMark Peek {
542c80476e4SDavid E. O'Brien     ptr_t   ptr;
54345e5710bSMark Peek 
54445e5710bSMark Peek     n = n ? n : 1;
545c80476e4SDavid E. O'Brien 
54645e5710bSMark Peek #ifdef USE_SBRK
547c80476e4SDavid E. O'Brien     if (membot == NULL)
54823338178SMark Peek 	membot = sbrk(0);
549c80476e4SDavid E. O'Brien #endif /* USE_SBRK */
550c80476e4SDavid E. O'Brien 
551c80476e4SDavid E. O'Brien     if ((ptr = (p ? realloc(p, n) : malloc(n))) == NULL)
55245e5710bSMark Peek 	out_of_memory();
55323338178SMark Peek #ifndef USE_SBRK
554c80476e4SDavid E. O'Brien     if (memtop < ((char *) ptr) + n)
555c80476e4SDavid E. O'Brien 	memtop = ((char *) ptr) + n;
556c80476e4SDavid E. O'Brien     if (membot == NULL)
557c80476e4SDavid E. O'Brien 	membot = ptr;
558c80476e4SDavid E. O'Brien #endif /* !USE_SBRK */
55945e5710bSMark Peek     return ((memalign_t) ptr);
560c80476e4SDavid E. O'Brien }
561c80476e4SDavid E. O'Brien 
562c80476e4SDavid E. O'Brien memalign_t
scalloc(size_t s,size_t n)563c80476e4SDavid E. O'Brien scalloc(size_t s, size_t n)
564c80476e4SDavid E. O'Brien {
565c80476e4SDavid E. O'Brien     ptr_t   ptr;
566c80476e4SDavid E. O'Brien 
567c80476e4SDavid E. O'Brien     n *= s;
568c80476e4SDavid E. O'Brien     n = n ? n : 1;
569c80476e4SDavid E. O'Brien 
570c80476e4SDavid E. O'Brien #ifdef USE_SBRK
571c80476e4SDavid E. O'Brien     if (membot == NULL)
572c80476e4SDavid E. O'Brien 	membot = sbrk(0);
573c80476e4SDavid E. O'Brien #endif /* USE_SBRK */
574c80476e4SDavid E. O'Brien 
575c80476e4SDavid E. O'Brien     if ((ptr = malloc(n)) == NULL)
57645e5710bSMark Peek 	out_of_memory();
577c80476e4SDavid E. O'Brien 
578c80476e4SDavid E. O'Brien     memset (ptr, 0, n);
57923338178SMark Peek 
58023338178SMark Peek #ifndef USE_SBRK
581c80476e4SDavid E. O'Brien     if (memtop < ((char *) ptr) + n)
582c80476e4SDavid E. O'Brien 	memtop = ((char *) ptr) + n;
583c80476e4SDavid E. O'Brien     if (membot == NULL)
584c80476e4SDavid E. O'Brien 	membot = ptr;
585c80476e4SDavid E. O'Brien #endif /* !USE_SBRK */
586c80476e4SDavid E. O'Brien 
58745e5710bSMark Peek     return ((memalign_t) ptr);
588c80476e4SDavid E. O'Brien }
589c80476e4SDavid E. O'Brien 
590c80476e4SDavid E. O'Brien void
sfree(ptr_t p)591c80476e4SDavid E. O'Brien sfree(ptr_t p)
59245e5710bSMark Peek {
593c80476e4SDavid E. O'Brien     if (p && !dont_free)
594c80476e4SDavid E. O'Brien 	free(p);
595c80476e4SDavid E. O'Brien }
596c80476e4SDavid E. O'Brien 
597c80476e4SDavid E. O'Brien #endif /* SYSMALLOC */
598c80476e4SDavid E. O'Brien 
599c80476e4SDavid E. O'Brien /*
600c80476e4SDavid E. O'Brien  * mstats - print out statistics about malloc
601c80476e4SDavid E. O'Brien  *
60223338178SMark Peek  * Prints two lines of numbers, one showing the length of the free list
60345e5710bSMark Peek  * for each size category, the second showing the number of mallocs -
60423338178SMark Peek  * frees for each size category.
605c80476e4SDavid E. O'Brien  */
606c80476e4SDavid E. O'Brien /*ARGSUSED*/
607c80476e4SDavid E. O'Brien void
showall(Char ** v,struct command * c)608c80476e4SDavid E. O'Brien showall(Char **v, struct command *c)
609c80476e4SDavid E. O'Brien {
610c80476e4SDavid E. O'Brien #ifndef SYSMALLOC
611c80476e4SDavid E. O'Brien     size_t i, j;
612     union overhead *p;
613     int     totfree = 0, totused = 0;
614 
615     xprintf(CGETS(19, 8, "%s current memory allocation:\nfree:\t"), progname);
616     for (i = 0; i < NBUCKETS; i++) {
617 	for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
618 	    continue;
619 	xprintf(" %4zd", j);
620 	totfree += j * (1 << (i + 3));
621     }
622     xprintf("\n%s:\t", CGETS(19, 9, "used"));
623     for (i = 0; i < NBUCKETS; i++) {
624 	xprintf(" %4d", nmalloc[i]);
625 	totused += nmalloc[i] * (1 << (i + 3));
626     }
627     xprintf(CGETS(19, 10, "\n\tTotal in use: %d, total free: %d\n"),
628 	    totused, totfree);
629     xprintf(CGETS(19, 11,
630 	    "\tAllocated memory from 0x%lx to 0x%lx.  Real top at 0x%lx\n"),
631 	    (unsigned long) membot, (unsigned long) memtop,
632 	    (unsigned long) sbrk(0));
633 #else /* SYSMALLOC */
634 #ifndef HAVE_MALLINFO
635 #ifdef USE_SBRK
636     memtop = sbrk(0);
637 #endif /* USE_SBRK */
638     xprintf(CGETS(19, 12, "Allocated memory from 0x%lx to 0x%lx (%ld).\n"),
639 	    (unsigned long) membot, (unsigned long) memtop,
640 	    (unsigned long) (memtop - membot));
641 #else /* HAVE_MALLINFO */
642     struct mallinfo mi;
643 
644     mi = mallinfo();
645     xprintf(CGETS(19, 13, "%s current memory allocation:\n"), progname);
646     xprintf(CGETS(19, 14, "Total space allocated from system: %d\n"), mi.arena);
647     xprintf(CGETS(19, 15, "Number of non-inuse chunks: %d\n"), mi.ordblks);
648     xprintf(CGETS(19, 16, "Number of mmapped regions: %d\n"), mi.hblks);
649     xprintf(CGETS(19, 17, "Total space in mmapped regions: %d\n"), mi.hblkhd);
650     xprintf(CGETS(19, 18, "Total allocated space: %d\n"), mi.uordblks);
651     xprintf(CGETS(19, 19, "Total non-inuse space: %d\n"), mi.fordblks);
652     xprintf(CGETS(19, 20, "Top-most, releasable space: %d\n"), mi.keepcost);
653 #endif /* HAVE_MALLINFO */
654 #endif /* SYSMALLOC */
655     USE(c);
656     USE(v);
657 }
658 
659 #ifndef SYSMALLOC
660 /* jemalloc defines these */
661 void _malloc_prefork(void);
662 void _malloc_postfork(void);
663 void _malloc_postfork_child(void);
_malloc_prefork(void)664 void _malloc_prefork(void) {}
_malloc_postfork(void)665 void _malloc_postfork(void) {}
_malloc_postfork_child(void)666 void _malloc_postfork_child(void) {}
667 #endif
668