kernel - close holes in autoconf's run_interrupt_driven_config_hooks()
[dragonfly.git] / libexec / rtld-elf / malloc.c
blobfaea9f0ea3f994455e3d9e8365ac8411c5743c52
1 /*-
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)malloc.c 5.11 (Berkeley) 2/23/91
34 * $FreeBSD: src/libexec/rtld-elf/malloc.c,v 1.3.2.3 2003/02/20 20:42:46 kan Exp $
35 * $DragonFly: src/libexec/rtld-elf/malloc.c,v 1.3 2008/06/05 18:01:49 swildner Exp $
39 * malloc.c (Caltech) 2/21/82
40 * Chris Kingsley, kingsley@cit-20.
42 * This is a very fast storage allocator. It allocates blocks of a small
43 * number of different sizes, and keeps free lists of each size. Blocks that
44 * don't exactly fit are passed up to the next larger size. In this
45 * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
46 * This is designed for use in a virtual memory environment.
49 #include <sys/types.h>
50 #include <err.h>
51 #include <paths.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <sys/param.h>
58 #include <sys/mman.h>
59 #ifndef BSD
60 #define MAP_COPY MAP_PRIVATE
61 #define MAP_FILE 0
62 #define MAP_ANON 0
63 #endif
65 #ifndef BSD /* Need do better than this */
66 #define NEED_DEV_ZERO 1
67 #endif
69 static void morecore();
70 static int findbucket();
73 * Pre-allocate mmap'ed pages
75 #define NPOOLPAGES (32*1024/pagesz)
76 static caddr_t pagepool_start, pagepool_end;
77 static int morepages();
80 * The overhead on a block is at least 4 bytes. When free, this space
81 * contains a pointer to the next free block, and the bottom two bits must
82 * be zero. When in use, the first byte is set to MAGIC, and the second
83 * byte is the size index. The remaining bytes are for alignment.
84 * If range checking is enabled then a second word holds the size of the
85 * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
86 * The order of elements is critical: ov_magic must overlay the low order
87 * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
89 union overhead {
90 union overhead *ov_next; /* when free */
91 struct {
92 u_char ovu_magic; /* magic number */
93 u_char ovu_index; /* bucket # */
94 #ifdef RCHECK
95 u_short ovu_rmagic; /* range magic number */
96 u_int ovu_size; /* actual block size */
97 #endif
98 } ovu;
99 #define ov_magic ovu.ovu_magic
100 #define ov_index ovu.ovu_index
101 #define ov_rmagic ovu.ovu_rmagic
102 #define ov_size ovu.ovu_size
105 #define MAGIC 0xef /* magic # on accounting info */
106 #define RMAGIC 0x5555 /* magic # on range info */
108 #ifdef RCHECK
109 #define RSLOP sizeof (u_short)
110 #else
111 #define RSLOP 0
112 #endif
115 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
116 * smallest allocatable block is 8 bytes. The overhead information
117 * precedes the data area returned to the user.
119 #define NBUCKETS 30
120 static union overhead *nextf[NBUCKETS];
122 static int pagesz; /* page size */
123 static int pagebucket; /* page size bucket */
125 #ifdef MSTATS
127 * nmalloc[i] is the difference between the number of mallocs and frees
128 * for a given block size.
130 static u_int nmalloc[NBUCKETS];
131 #include <stdio.h>
132 #endif
134 #if defined(MALLOC_DEBUG) || defined(RCHECK)
135 #define ASSERT(p) if (!(p)) botch("p")
136 #include <stdio.h>
137 static void
138 botch(s)
139 char *s;
141 fprintf(stderr, "\r\nassertion botched: %s\r\n", s);
142 (void) fflush(stderr); /* just in case user buffered it */
143 abort();
145 #else
146 #define ASSERT(p)
147 #endif
149 /* Debugging stuff */
150 static void xprintf(const char *, ...);
151 #define TRACE() xprintf("TRACE %s:%d\n", __FILE__, __LINE__)
153 void *
154 malloc(nbytes)
155 size_t nbytes;
157 register union overhead *op;
158 register int bucket;
159 register long n;
160 register unsigned amt;
163 * First time malloc is called, setup page size and
164 * align break pointer so all data will be page aligned.
166 if (pagesz == 0) {
167 pagesz = n = getpagesize();
168 if (morepages(NPOOLPAGES) == 0)
169 return NULL;
170 op = (union overhead *)(pagepool_start);
171 n = n - sizeof (*op) - ((long)op & (n - 1));
172 if (n < 0)
173 n += pagesz;
174 if (n) {
175 pagepool_start += n;
177 bucket = 0;
178 amt = 8;
179 while (pagesz > amt) {
180 amt <<= 1;
181 bucket++;
183 pagebucket = bucket;
186 * Convert amount of memory requested into closest block size
187 * stored in hash buckets which satisfies request.
188 * Account for space used per block for accounting.
190 if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
191 #ifndef RCHECK
192 amt = 8; /* size of first bucket */
193 bucket = 0;
194 #else
195 amt = 16; /* size of first bucket */
196 bucket = 1;
197 #endif
198 n = -(sizeof (*op) + RSLOP);
199 } else {
200 amt = pagesz;
201 bucket = pagebucket;
203 while (nbytes > amt + n) {
204 amt <<= 1;
205 if (amt == 0)
206 return (NULL);
207 bucket++;
210 * If nothing in hash bucket right now,
211 * request more memory from the system.
213 if ((op = nextf[bucket]) == NULL) {
214 morecore(bucket);
215 if ((op = nextf[bucket]) == NULL)
216 return (NULL);
218 /* remove from linked list */
219 nextf[bucket] = op->ov_next;
220 op->ov_magic = MAGIC;
221 op->ov_index = bucket;
222 #ifdef MSTATS
223 nmalloc[bucket]++;
224 #endif
225 #ifdef RCHECK
227 * Record allocated size of block and
228 * bound space with magic numbers.
230 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
231 op->ov_rmagic = RMAGIC;
232 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
233 #endif
234 return ((char *)(op + 1));
238 * Used by rtld.c, if we don't override it here the calloc from
239 * libc may try to pull in the malloc/realloc/free from libc too.
241 void *
242 calloc(size_t num, size_t size)
244 void *p;
246 size *= num;
247 if ((p = malloc(size)) != NULL)
248 bzero(p, size);
249 return(p);
253 * Allocate more memory to the indicated bucket.
255 static void
256 morecore(bucket)
257 int bucket;
259 register union overhead *op;
260 register int sz; /* size of desired block */
261 int amt; /* amount to allocate */
262 int nblks; /* how many blocks we get */
265 * sbrk_size <= 0 only for big, FLUFFY, requests (about
266 * 2^30 bytes on a VAX, I think) or for a negative arg.
268 sz = 1 << (bucket + 3);
269 #ifdef MALLOC_DEBUG
270 ASSERT(sz > 0);
271 #else
272 if (sz <= 0)
273 return;
274 #endif
275 if (sz < pagesz) {
276 amt = pagesz;
277 nblks = amt / sz;
278 } else {
279 amt = sz + pagesz;
280 nblks = 1;
282 if (amt > pagepool_end - pagepool_start)
283 if (morepages(amt/pagesz + NPOOLPAGES) == 0)
284 return;
285 op = (union overhead *)pagepool_start;
286 pagepool_start += amt;
289 * Add new memory allocated to that on
290 * free list for this hash bucket.
292 nextf[bucket] = op;
293 while (--nblks > 0) {
294 op->ov_next = (union overhead *)((caddr_t)op + sz);
295 op = (union overhead *)((caddr_t)op + sz);
299 void
300 free(cp)
301 void *cp;
303 register int size;
304 register union overhead *op;
306 if (cp == NULL)
307 return;
308 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
309 #ifdef MALLOC_DEBUG
310 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
311 #else
312 if (op->ov_magic != MAGIC)
313 return; /* sanity */
314 #endif
315 #ifdef RCHECK
316 ASSERT(op->ov_rmagic == RMAGIC);
317 ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
318 #endif
319 size = op->ov_index;
320 ASSERT(size < NBUCKETS);
321 op->ov_next = nextf[size]; /* also clobbers ov_magic */
322 nextf[size] = op;
323 #ifdef MSTATS
324 nmalloc[size]--;
325 #endif
329 * When a program attempts "storage compaction" as mentioned in the
330 * old malloc man page, it realloc's an already freed block. Usually
331 * this is the last block it freed; occasionally it might be farther
332 * back. We have to search all the free lists for the block in order
333 * to determine its bucket: 1st we make one pass thru the lists
334 * checking only the first block in each; if that fails we search
335 * ``realloc_srchlen'' blocks in each list for a match (the variable
336 * is extern so the caller can modify it). If that fails we just copy
337 * however many bytes was given to realloc() and hope it's not huge.
339 int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
341 void *
342 realloc(cp, nbytes)
343 void *cp;
344 size_t nbytes;
346 register u_int onb;
347 register int i;
348 union overhead *op;
349 char *res;
350 int was_alloced = 0;
352 if (cp == NULL)
353 return (malloc(nbytes));
354 op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
355 if (op->ov_magic == MAGIC) {
356 was_alloced++;
357 i = op->ov_index;
358 } else {
360 * Already free, doing "compaction".
362 * Search for the old block of memory on the
363 * free list. First, check the most common
364 * case (last element free'd), then (this failing)
365 * the last ``realloc_srchlen'' items free'd.
366 * If all lookups fail, then assume the size of
367 * the memory block being realloc'd is the
368 * largest possible (so that all "nbytes" of new
369 * memory are copied into). Note that this could cause
370 * a memory fault if the old area was tiny, and the moon
371 * is gibbous. However, that is very unlikely.
373 if ((i = findbucket(op, 1)) < 0 &&
374 (i = findbucket(op, realloc_srchlen)) < 0)
375 i = NBUCKETS;
377 onb = 1 << (i + 3);
378 if (onb < pagesz)
379 onb -= sizeof (*op) + RSLOP;
380 else
381 onb += pagesz - sizeof (*op) - RSLOP;
382 /* avoid the copy if same size block */
383 if (was_alloced) {
384 if (i) {
385 i = 1 << (i + 2);
386 if (i < pagesz)
387 i -= sizeof (*op) + RSLOP;
388 else
389 i += pagesz - sizeof (*op) - RSLOP;
391 if (nbytes <= onb && nbytes > i) {
392 #ifdef RCHECK
393 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
394 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
395 #endif
396 return(cp);
397 } else
398 free(cp);
400 if ((res = malloc(nbytes)) == NULL)
401 return (NULL);
402 if (cp != res) /* common optimization if "compacting" */
403 bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
404 return (res);
408 * Search ``srchlen'' elements of each free list for a block whose
409 * header starts at ``freep''. If srchlen is -1 search the whole list.
410 * Return bucket number, or -1 if not found.
412 static int
413 findbucket(freep, srchlen)
414 union overhead *freep;
415 int srchlen;
417 register union overhead *p;
418 register int i, j;
420 for (i = 0; i < NBUCKETS; i++) {
421 j = 0;
422 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
423 if (p == freep)
424 return (i);
425 j++;
428 return (-1);
431 #ifdef MSTATS
433 * mstats - print out statistics about malloc
435 * Prints two lines of numbers, one showing the length of the free list
436 * for each size category, the second showing the number of mallocs -
437 * frees for each size category.
439 mstats(s)
440 char *s;
442 register int i, j;
443 register union overhead *p;
444 int totfree = 0,
445 totused = 0;
447 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s);
448 for (i = 0; i < NBUCKETS; i++) {
449 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
451 fprintf(stderr, " %d", j);
452 totfree += j * (1 << (i + 3));
454 fprintf(stderr, "\nused:\t");
455 for (i = 0; i < NBUCKETS; i++) {
456 fprintf(stderr, " %d", nmalloc[i]);
457 totused += nmalloc[i] * (1 << (i + 3));
459 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n",
460 totused, totfree);
462 #endif
465 static int
466 morepages(n)
467 int n;
469 int fd = -1;
470 int offset;
472 #ifdef NEED_DEV_ZERO
473 fd = open(_PATH_DEVZERO, O_RDWR, 0);
474 if (fd == -1)
475 perror(_PATH_DEVZERO);
476 #endif
478 if (pagepool_end - pagepool_start > pagesz) {
479 caddr_t addr = (caddr_t)
480 (((long)pagepool_start + pagesz - 1) & ~(pagesz - 1));
481 if (munmap(addr, pagepool_end - addr) != 0)
482 warn("morepages: munmap %p", addr);
485 offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1));
487 if ((pagepool_start = mmap(0, n * pagesz,
488 PROT_READ|PROT_WRITE,
489 MAP_ANON|MAP_COPY, fd, 0)) == (caddr_t)-1) {
490 xprintf("Cannot map anonymous memory");
491 return 0;
493 pagepool_end = pagepool_start + n * pagesz;
494 pagepool_start += offset;
496 #ifdef NEED_DEV_ZERO
497 close(fd);
498 #endif
499 return n;
503 * Non-mallocing printf, for use by malloc itself.
505 static void
506 xprintf(const char *fmt, ...)
508 char buf[256];
509 va_list ap;
511 va_start(ap, fmt);
512 vsprintf(buf, fmt, ap);
513 (void)write(STDOUT_FILENO, buf, strlen(buf));
514 va_end(ap);