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