1/*-
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31/*static char *sccsid = "from: @(#)malloc.c	5.11 (Berkeley) 2/23/91";*/
32static char *rcsid = "$FreeBSD$";
33#endif /* LIBC_SCCS and not lint */
34
35/*
36 * malloc.c (Caltech) 2/21/82
37 * Chris Kingsley, kingsley@cit-20.
38 *
39 * This is a very fast storage allocator.  It allocates blocks of a small
40 * number of different sizes, and keeps free lists of each size.  Blocks that
41 * don't exactly fit are passed up to the next larger size.  In this
42 * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
43 * This is designed for use in a virtual memory environment.
44 */
45
46#include <sys/types.h>
47#include <sys/sysctl.h>
48#include <stdarg.h>
49#include <stddef.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54#include <sys/param.h>
55#include <sys/mman.h>
56#include "rtld_printf.h"
57
58static void morecore();
59static int findbucket();
60
61/*
62 * Pre-allocate mmap'ed pages
63 */
64#define	NPOOLPAGES	(32*1024/pagesz)
65static caddr_t		pagepool_start, pagepool_end;
66static int		morepages();
67
68/*
69 * The overhead on a block is at least 4 bytes.  When free, this space
70 * contains a pointer to the next free block, and the bottom two bits must
71 * be zero.  When in use, the first byte is set to MAGIC, and the second
72 * byte is the size index.  The remaining bytes are for alignment.
73 * If range checking is enabled then a second word holds the size of the
74 * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
75 * The order of elements is critical: ov_magic must overlay the low order
76 * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
77 */
78union	overhead {
79	union	overhead *ov_next;	/* when free */
80	struct {
81		u_char	ovu_magic;	/* magic number */
82		u_char	ovu_index;	/* bucket # */
83#ifdef RCHECK
84		u_short	ovu_rmagic;	/* range magic number */
85		u_int	ovu_size;	/* actual block size */
86#endif
87	} ovu;
88#define	ov_magic	ovu.ovu_magic
89#define	ov_index	ovu.ovu_index
90#define	ov_rmagic	ovu.ovu_rmagic
91#define	ov_size		ovu.ovu_size
92};
93
94#define	MAGIC		0xef		/* magic # on accounting info */
95#define RMAGIC		0x5555		/* magic # on range info */
96
97#ifdef RCHECK
98#define	RSLOP		sizeof (u_short)
99#else
100#define	RSLOP		0
101#endif
102
103/*
104 * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
105 * smallest allocatable block is 8 bytes.  The overhead information
106 * precedes the data area returned to the user.
107 */
108#define	NBUCKETS 30
109static	union overhead *nextf[NBUCKETS];
110
111static	int pagesz;			/* page size */
112static	int pagebucket;			/* page size bucket */
113
114#ifdef MSTATS
115/*
116 * nmalloc[i] is the difference between the number of mallocs and frees
117 * for a given block size.
118 */
119static	u_int nmalloc[NBUCKETS];
120#include <stdio.h>
121#endif
122
123#if defined(MALLOC_DEBUG) || defined(RCHECK)
124#define	ASSERT(p)   if (!(p)) botch("p")
125#include <stdio.h>
126static void
127botch(s)
128	char *s;
129{
130	fprintf(stderr, "\r\nassertion botched: %s\r\n", s);
131 	(void) fflush(stderr);		/* just in case user buffered it */
132	abort();
133}
134#else
135#define	ASSERT(p)
136#endif
137
138/* Debugging stuff */
139#define TRACE()	rtld_printf("TRACE %s:%d\n", __FILE__, __LINE__)
140
141/*
142 * The array of supported page sizes is provided by the user, i.e., the
143 * program that calls this storage allocator.  That program must initialize
144 * the array before making its first call to allocate storage.  The array
145 * must contain at least one page size.  The page sizes must be stored in
146 * increasing order.
147 */
148extern size_t *pagesizes;
149
150void *
151malloc(nbytes)
152	size_t nbytes;
153{
154  	register union overhead *op;
155  	register int bucket;
156	register long n;
157	register unsigned amt;
158
159	/*
160	 * First time malloc is called, setup page size and
161	 * align break pointer so all data will be page aligned.
162	 */
163	if (pagesz == 0) {
164		pagesz = n = pagesizes[0];
165		if (morepages(NPOOLPAGES) == 0)
166			return NULL;
167		op = (union overhead *)(pagepool_start);
168  		n = n - sizeof (*op) - ((long)op & (n - 1));
169		if (n < 0)
170			n += pagesz;
171  		if (n) {
172			pagepool_start += n;
173		}
174		bucket = 0;
175		amt = 8;
176		while ((unsigned)pagesz > amt) {
177			amt <<= 1;
178			bucket++;
179		}
180		pagebucket = bucket;
181	}
182	/*
183	 * Convert amount of memory requested into closest block size
184	 * stored in hash buckets which satisfies request.
185	 * Account for space used per block for accounting.
186	 */
187	if (nbytes <= (unsigned long)(n = pagesz - sizeof (*op) - RSLOP)) {
188#ifndef RCHECK
189		amt = 8;	/* size of first bucket */
190		bucket = 0;
191#else
192		amt = 16;	/* size of first bucket */
193		bucket = 1;
194#endif
195		n = -(sizeof (*op) + RSLOP);
196	} else {
197		amt = pagesz;
198		bucket = pagebucket;
199	}
200	while (nbytes > amt + n) {
201		amt <<= 1;
202		if (amt == 0)
203			return (NULL);
204		bucket++;
205	}
206	/*
207	 * If nothing in hash bucket right now,
208	 * request more memory from the system.
209	 */
210  	if ((op = nextf[bucket]) == NULL) {
211  		morecore(bucket);
212  		if ((op = nextf[bucket]) == NULL)
213  			return (NULL);
214	}
215	/* remove from linked list */
216  	nextf[bucket] = op->ov_next;
217	op->ov_magic = MAGIC;
218	op->ov_index = bucket;
219#ifdef MSTATS
220  	nmalloc[bucket]++;
221#endif
222#ifdef RCHECK
223	/*
224	 * Record allocated size of block and
225	 * bound space with magic numbers.
226	 */
227	op->ov_size = roundup2(nbytes, RSLOP);
228	op->ov_rmagic = RMAGIC;
229  	*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
230#endif
231  	return ((char *)(op + 1));
232}
233
234void *
235calloc(size_t num, size_t size)
236{
237	void *ret;
238
239	if (size != 0 && (num * size) / size != num) {
240		/* size_t overflow. */
241		return (NULL);
242	}
243
244	if ((ret = malloc(num * size)) != NULL)
245		memset(ret, 0, num * size);
246
247	return (ret);
248}
249
250/*
251 * Allocate more memory to the indicated bucket.
252 */
253static void
254morecore(bucket)
255	int bucket;
256{
257  	register union overhead *op;
258	register int sz;		/* size of desired block */
259  	int amt;			/* amount to allocate */
260  	int nblks;			/* how many blocks we get */
261
262	/*
263	 * sbrk_size <= 0 only for big, FLUFFY, requests (about
264	 * 2^30 bytes on a VAX, I think) or for a negative arg.
265	 */
266	sz = 1 << (bucket + 3);
267#ifdef MALLOC_DEBUG
268	ASSERT(sz > 0);
269#else
270	if (sz <= 0)
271		return;
272#endif
273	if (sz < pagesz) {
274		amt = pagesz;
275  		nblks = amt / sz;
276	} else {
277		amt = sz + pagesz;
278		nblks = 1;
279	}
280	if (amt > pagepool_end - pagepool_start)
281		if (morepages(amt/pagesz + NPOOLPAGES) == 0)
282			return;
283	op = (union overhead *)pagepool_start;
284	pagepool_start += amt;
285
286	/*
287	 * Add new memory allocated to that on
288	 * free list for this hash bucket.
289	 */
290  	nextf[bucket] = op;
291  	while (--nblks > 0) {
292		op->ov_next = (union overhead *)((caddr_t)op + sz);
293		op = (union overhead *)((caddr_t)op + sz);
294  	}
295}
296
297void
298free(cp)
299	void *cp;
300{
301  	register int size;
302	register union overhead *op;
303
304  	if (cp == NULL)
305  		return;
306	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
307#ifdef MALLOC_DEBUG
308  	ASSERT(op->ov_magic == MAGIC);		/* make sure it was in use */
309#else
310	if (op->ov_magic != MAGIC)
311		return;				/* sanity */
312#endif
313#ifdef RCHECK
314  	ASSERT(op->ov_rmagic == RMAGIC);
315	ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
316#endif
317  	size = op->ov_index;
318  	ASSERT(size < NBUCKETS);
319	op->ov_next = nextf[size];	/* also clobbers ov_magic */
320  	nextf[size] = op;
321#ifdef MSTATS
322  	nmalloc[size]--;
323#endif
324}
325
326/*
327 * When a program attempts "storage compaction" as mentioned in the
328 * old malloc man page, it realloc's an already freed block.  Usually
329 * this is the last block it freed; occasionally it might be farther
330 * back.  We have to search all the free lists for the block in order
331 * to determine its bucket: 1st we make one pass through the lists
332 * checking only the first block in each; if that fails we search
333 * ``realloc_srchlen'' blocks in each list for a match (the variable
334 * is extern so the caller can modify it).  If that fails we just copy
335 * however many bytes was given to realloc() and hope it's not huge.
336 */
337int realloc_srchlen = 4;	/* 4 should be plenty, -1 =>'s whole list */
338
339void *
340realloc(cp, nbytes)
341	void *cp;
342	size_t nbytes;
343{
344  	register u_int onb;
345	register int i;
346	union overhead *op;
347  	char *res;
348	int was_alloced = 0;
349
350  	if (cp == NULL)
351  		return (malloc(nbytes));
352	op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
353	if (op->ov_magic == MAGIC) {
354		was_alloced++;
355		i = op->ov_index;
356	} else {
357		/*
358		 * Already free, doing "compaction".
359		 *
360		 * Search for the old block of memory on the
361		 * free list.  First, check the most common
362		 * case (last element free'd), then (this failing)
363		 * the last ``realloc_srchlen'' items free'd.
364		 * If all lookups fail, then assume the size of
365		 * the memory block being realloc'd is the
366		 * largest possible (so that all "nbytes" of new
367		 * memory are copied into).  Note that this could cause
368		 * a memory fault if the old area was tiny, and the moon
369		 * is gibbous.  However, that is very unlikely.
370		 */
371		if ((i = findbucket(op, 1)) < 0 &&
372		    (i = findbucket(op, realloc_srchlen)) < 0)
373			i = NBUCKETS;
374	}
375	onb = 1 << (i + 3);
376	if (onb < (u_int)pagesz)
377		onb -= sizeof (*op) + RSLOP;
378	else
379		onb += pagesz - sizeof (*op) - RSLOP;
380	/* avoid the copy if same size block */
381	if (was_alloced) {
382		if (i) {
383			i = 1 << (i + 2);
384			if (i < pagesz)
385				i -= sizeof (*op) + RSLOP;
386			else
387				i += pagesz - sizeof (*op) - RSLOP;
388		}
389		if (nbytes <= onb && nbytes > (size_t)i) {
390#ifdef RCHECK
391			op->ov_size = roundup2(nbytes, RSLOP);
392			*(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
393#endif
394			return(cp);
395		} else
396			free(cp);
397	}
398  	if ((res = malloc(nbytes)) == NULL)
399  		return (NULL);
400  	if (cp != res)		/* common optimization if "compacting" */
401		bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
402  	return (res);
403}
404
405/*
406 * Search ``srchlen'' elements of each free list for a block whose
407 * header starts at ``freep''.  If srchlen is -1 search the whole list.
408 * Return bucket number, or -1 if not found.
409 */
410static int
411findbucket(freep, srchlen)
412	union overhead *freep;
413	int srchlen;
414{
415	register union overhead *p;
416	register int i, j;
417
418	for (i = 0; i < NBUCKETS; i++) {
419		j = 0;
420		for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
421			if (p == freep)
422				return (i);
423			j++;
424		}
425	}
426	return (-1);
427}
428
429#ifdef MSTATS
430/*
431 * mstats - print out statistics about malloc
432 *
433 * Prints two lines of numbers, one showing the length of the free list
434 * for each size category, the second showing the number of mallocs -
435 * frees for each size category.
436 */
437mstats(s)
438	char *s;
439{
440  	register int i, j;
441  	register union overhead *p;
442  	int totfree = 0,
443  	totused = 0;
444
445  	fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
446  	for (i = 0; i < NBUCKETS; i++) {
447  		for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
448  			;
449  		fprintf(stderr, " %d", j);
450  		totfree += j * (1 << (i + 3));
451  	}
452  	fprintf(stderr, "\nused:\t");
453  	for (i = 0; i < NBUCKETS; i++) {
454  		fprintf(stderr, " %d", nmalloc[i]);
455  		totused += nmalloc[i] * (1 << (i + 3));
456  	}
457  	fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
458	    totused, totfree);
459}
460#endif
461
462
463static int
464morepages(n)
465int	n;
466{
467	int	fd = -1;
468	int	offset;
469
470	if (pagepool_end - pagepool_start > pagesz) {
471		caddr_t	addr = (caddr_t)
472			(((long)pagepool_start + pagesz - 1) & ~(pagesz - 1));
473		if (munmap(addr, pagepool_end - addr) != 0)
474			rtld_fdprintf(STDERR_FILENO, "morepages: munmap %p",
475			    addr);
476	}
477
478	offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1));
479
480	if ((pagepool_start = mmap(0, n * pagesz,
481			PROT_READ|PROT_WRITE,
482			MAP_ANON|MAP_COPY, fd, 0)) == (caddr_t)-1) {
483		rtld_printf("Cannot map anonymous memory\n");
484		return 0;
485	}
486	pagepool_end = pagepool_start + n * pagesz;
487	pagepool_start += offset;
488
489	return n;
490}
491