malloc.c revision 102235
1151497Sru/*- 2151497Sru * Copyright (c) 1983 Regents of the University of California. 3151497Sru * All rights reserved. 4151497Sru * 5151497Sru * Redistribution and use in source and binary forms, with or without 6151497Sru * modification, are permitted provided that the following conditions 7151497Sru * are met: 8151497Sru * 1. Redistributions of source code must retain the above copyright 9151497Sru * notice, this list of conditions and the following disclaimer. 10151497Sru * 2. Redistributions in binary form must reproduce the above copyright 11151497Sru * notice, this list of conditions and the following disclaimer in the 12151497Sru * documentation and/or other materials provided with the distribution. 13151497Sru * 3. All advertising materials mentioning features or use of this software 14151497Sru * must display the following acknowledgement: 15151497Sru * This product includes software developed by the University of 16151497Sru * California, Berkeley and its contributors. 17151497Sru * 4. Neither the name of the University nor the names of its contributors 18151497Sru * may be used to endorse or promote products derived from this software 19151497Sru * without specific prior written permission. 20151497Sru * 21151497Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22151497Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23151497Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24151497Sru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25151497Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26151497Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27151497Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28151497Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29151497Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30151497Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31151497Sru * SUCH DAMAGE. 32151497Sru */ 33151497Sru 34151497Sru#if defined(LIBC_SCCS) && !defined(lint) 35151497Sru/*static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/ 36151497Srustatic char *rcsid = "$FreeBSD: head/libexec/rtld-elf/malloc.c 102235 2002-08-21 19:03:26Z imp $"; 37151497Sru#endif /* LIBC_SCCS and not lint */ 38151497Sru 39151497Sru/* 40151497Sru * malloc.c (Caltech) 2/21/82 41151497Sru * Chris Kingsley, kingsley@cit-20. 42151497Sru * 43151497Sru * This is a very fast storage allocator. It allocates blocks of a small 44151497Sru * number of different sizes, and keeps free lists of each size. Blocks that 45151497Sru * don't exactly fit are passed up to the next larger size. In this 46151497Sru * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long. 47151497Sru * This is designed for use in a virtual memory environment. 48151497Sru */ 49151497Sru 50151497Sru#include <sys/types.h> 51151497Sru#include <err.h> 52151497Sru#include <paths.h> 53151497Sru#include <stdlib.h> 54151497Sru#include <string.h> 55151497Sru#include <stddef.h> 56151497Sru#include <unistd.h> 57151497Sru#include <sys/param.h> 58151497Sru#include <sys/mman.h> 59151497Sru#ifndef BSD 60151497Sru#define MAP_COPY MAP_PRIVATE 61151497Sru#define MAP_FILE 0 62151497Sru#define MAP_ANON 0 63151497Sru#endif 64151497Sru 65151497Sru#ifndef BSD /* Need do better than this */ 66151497Sru#define NEED_DEV_ZERO 1 67151497Sru#endif 68151497Sru 69151497Srustatic void morecore(); 70151497Srustatic int findbucket(); 71151497Sru 72151497Sru/* 73151497Sru * Pre-allocate mmap'ed pages 74151497Sru */ 75151497Sru#define NPOOLPAGES (32*1024/pagesz) 76151497Srustatic caddr_t pagepool_start, pagepool_end; 77151497Srustatic int morepages(); 78151497Sru 79151497Sru/* 80151497Sru * The overhead on a block is at least 4 bytes. When free, this space 81151497Sru * contains a pointer to the next free block, and the bottom two bits must 82151497Sru * be zero. When in use, the first byte is set to MAGIC, and the second 83151497Sru * byte is the size index. The remaining bytes are for alignment. 84151497Sru * If range checking is enabled then a second word holds the size of the 85151497Sru * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC). 86151497Sru * The order of elements is critical: ov_magic must overlay the low order 87151497Sru * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern. 88151497Sru */ 89151497Sruunion overhead { 90151497Sru union overhead *ov_next; /* when free */ 91151497Sru struct { 92151497Sru u_char ovu_magic; /* magic number */ 93151497Sru u_char ovu_index; /* bucket # */ 94151497Sru#ifdef RCHECK 95151497Sru u_short ovu_rmagic; /* range magic number */ 96151497Sru u_int ovu_size; /* actual block size */ 97151497Sru#endif 98151497Sru } ovu; 99151497Sru#define ov_magic ovu.ovu_magic 100151497Sru#define ov_index ovu.ovu_index 101151497Sru#define ov_rmagic ovu.ovu_rmagic 102151497Sru#define ov_size ovu.ovu_size 103151497Sru}; 104151497Sru 105151497Sru#define MAGIC 0xef /* magic # on accounting info */ 106151497Sru#define RMAGIC 0x5555 /* magic # on range info */ 107151497Sru 108151497Sru#ifdef RCHECK 109151497Sru#define RSLOP sizeof (u_short) 110151497Sru#else 111151497Sru#define RSLOP 0 112151497Sru#endif 113151497Sru 114151497Sru/* 115151497Sru * nextf[i] is the pointer to the next free block of size 2^(i+3). The 116151497Sru * smallest allocatable block is 8 bytes. The overhead information 117151497Sru * precedes the data area returned to the user. 118151497Sru */ 119151497Sru#define NBUCKETS 30 120151497Srustatic union overhead *nextf[NBUCKETS]; 121151497Sru 122151497Srustatic int pagesz; /* page size */ 123151497Srustatic int pagebucket; /* page size bucket */ 124151497Sru 125151497Sru#ifdef MSTATS 126151497Sru/* 127151497Sru * nmalloc[i] is the difference between the number of mallocs and frees 128151497Sru * for a given block size. 129151497Sru */ 130151497Srustatic u_int nmalloc[NBUCKETS]; 131151497Sru#include <stdio.h> 132151497Sru#endif 133151497Sru 134151497Sru#if defined(MALLOC_DEBUG) || defined(RCHECK) 135151497Sru#define ASSERT(p) if (!(p)) botch("p") 136151497Sru#include <stdio.h> 137151497Srustatic void 138151497Srubotch(s) 139151497Sru char *s; 140151497Sru{ 141151497Sru fprintf(stderr, "\r\nassertion botched: %s\r\n", s); 142151497Sru (void) fflush(stderr); /* just in case user buffered it */ 143151497Sru abort(); 144151497Sru} 145151497Sru#else 146151497Sru#define ASSERT(p) 147151497Sru#endif 148151497Sru 149151497Sru/* Debugging stuff */ 150151497Sruextern void xprintf(const char *, ...); 151151497Sru#define TRACE() xprintf("TRACE %s:%d\n", __FILE__, __LINE__) 152151497Sru 153151497Sruvoid * 154151497Srumalloc(nbytes) 155151497Sru size_t nbytes; 156151497Sru{ 157151497Sru register union overhead *op; 158151497Sru register int bucket; 159151497Sru register long n; 160151497Sru register unsigned amt; 161151497Sru 162151497Sru /* 163151497Sru * First time malloc is called, setup page size and 164151497Sru * align break pointer so all data will be page aligned. 165151497Sru */ 166151497Sru if (pagesz == 0) { 167151497Sru pagesz = n = getpagesize(); 168151497Sru if (morepages(NPOOLPAGES) == 0) 169151497Sru return NULL; 170151497Sru op = (union overhead *)(pagepool_start); 171151497Sru n = n - sizeof (*op) - ((long)op & (n - 1)); 172151497Sru if (n < 0) 173151497Sru n += pagesz; 174151497Sru if (n) { 175151497Sru pagepool_start += n; 176151497Sru } 177151497Sru bucket = 0; 178151497Sru amt = 8; 179151497Sru while (pagesz > amt) { 180151497Sru amt <<= 1; 181151497Sru bucket++; 182151497Sru } 183151497Sru pagebucket = bucket; 184151497Sru } 185151497Sru /* 186151497Sru * Convert amount of memory requested into closest block size 187151497Sru * stored in hash buckets which satisfies request. 188151497Sru * Account for space used per block for accounting. 189151497Sru */ 190151497Sru if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { 191151497Sru#ifndef RCHECK 192151497Sru amt = 8; /* size of first bucket */ 193151497Sru bucket = 0; 194151497Sru#else 195151497Sru amt = 16; /* size of first bucket */ 196151497Sru bucket = 1; 197151497Sru#endif 198151497Sru n = -(sizeof (*op) + RSLOP); 199151497Sru } else { 200151497Sru amt = pagesz; 201151497Sru bucket = pagebucket; 202151497Sru } 203151497Sru while (nbytes > amt + n) { 204151497Sru amt <<= 1; 205151497Sru if (amt == 0) 206151497Sru return (NULL); 207151497Sru bucket++; 208151497Sru } 209151497Sru /* 210151497Sru * If nothing in hash bucket right now, 211151497Sru * request more memory from the system. 212151497Sru */ 213151497Sru if ((op = nextf[bucket]) == NULL) { 214151497Sru morecore(bucket); 215151497Sru if ((op = nextf[bucket]) == NULL) 216151497Sru return (NULL); 217151497Sru } 218151497Sru /* remove from linked list */ 219151497Sru nextf[bucket] = op->ov_next; 220151497Sru op->ov_magic = MAGIC; 221151497Sru op->ov_index = bucket; 222151497Sru#ifdef MSTATS 223151497Sru nmalloc[bucket]++; 224151497Sru#endif 225151497Sru#ifdef RCHECK 226151497Sru /* 227151497Sru * Record allocated size of block and 228151497Sru * bound space with magic numbers. 229151497Sru */ 230151497Sru op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 231151497Sru op->ov_rmagic = RMAGIC; 232151497Sru *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 233151497Sru#endif 234151497Sru return ((char *)(op + 1)); 235151497Sru} 236151497Sru 237151497Sru/* 238151497Sru * Allocate more memory to the indicated bucket. 239151497Sru */ 240151497Srustatic void 241151497Srumorecore(bucket) 242151497Sru int bucket; 243151497Sru{ 244151497Sru register union overhead *op; 245151497Sru register int sz; /* size of desired block */ 246151497Sru int amt; /* amount to allocate */ 247151497Sru int nblks; /* how many blocks we get */ 248151497Sru 249151497Sru /* 250151497Sru * sbrk_size <= 0 only for big, FLUFFY, requests (about 251151497Sru * 2^30 bytes on a VAX, I think) or for a negative arg. 252151497Sru */ 253151497Sru sz = 1 << (bucket + 3); 254151497Sru#ifdef MALLOC_DEBUG 255151497Sru ASSERT(sz > 0); 256151497Sru#else 257151497Sru if (sz <= 0) 258151497Sru return; 259151497Sru#endif 260151497Sru if (sz < pagesz) { 261151497Sru amt = pagesz; 262151497Sru nblks = amt / sz; 263151497Sru } else { 264151497Sru amt = sz + pagesz; 265151497Sru nblks = 1; 266151497Sru } 267151497Sru if (amt > pagepool_end - pagepool_start) 268151497Sru if (morepages(amt/pagesz + NPOOLPAGES) == 0) 269151497Sru return; 270151497Sru op = (union overhead *)pagepool_start; 271151497Sru pagepool_start += amt; 272151497Sru 273151497Sru /* 274151497Sru * Add new memory allocated to that on 275151497Sru * free list for this hash bucket. 276151497Sru */ 277151497Sru nextf[bucket] = op; 278151497Sru while (--nblks > 0) { 279151497Sru op->ov_next = (union overhead *)((caddr_t)op + sz); 280151497Sru op = (union overhead *)((caddr_t)op + sz); 281151497Sru } 282151497Sru} 283151497Sru 284151497Sruvoid 285151497Srufree(cp) 286151497Sru void *cp; 287151497Sru{ 288151497Sru register int size; 289151497Sru register union overhead *op; 290151497Sru 291151497Sru if (cp == NULL) 292151497Sru return; 293151497Sru op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 294151497Sru#ifdef MALLOC_DEBUG 295151497Sru ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 296151497Sru#else 297151497Sru if (op->ov_magic != MAGIC) 298151497Sru return; /* sanity */ 299151497Sru#endif 300151497Sru#ifdef RCHECK 301151497Sru ASSERT(op->ov_rmagic == RMAGIC); 302151497Sru ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); 303151497Sru#endif 304151497Sru size = op->ov_index; 305151497Sru ASSERT(size < NBUCKETS); 306151497Sru op->ov_next = nextf[size]; /* also clobbers ov_magic */ 307151497Sru nextf[size] = op; 308151497Sru#ifdef MSTATS 309151497Sru nmalloc[size]--; 310151497Sru#endif 311151497Sru} 312151497Sru 313151497Sru/* 314151497Sru * When a program attempts "storage compaction" as mentioned in the 315151497Sru * old malloc man page, it realloc's an already freed block. Usually 316151497Sru * this is the last block it freed; occasionally it might be farther 317151497Sru * back. We have to search all the free lists for the block in order 318151497Sru * to determine its bucket: 1st we make one pass thru the lists 319151497Sru * checking only the first block in each; if that fails we search 320151497Sru * ``realloc_srchlen'' blocks in each list for a match (the variable 321151497Sru * is extern so the caller can modify it). If that fails we just copy 322151497Sru * however many bytes was given to realloc() and hope it's not huge. 323151497Sru */ 324151497Sruint realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 325151497Sru 326151497Sruvoid * 327151497Srurealloc(cp, nbytes) 328151497Sru void *cp; 329151497Sru size_t nbytes; 330151497Sru{ 331151497Sru register u_int onb; 332151497Sru register int i; 333151497Sru union overhead *op; 334151497Sru char *res; 335151497Sru int was_alloced = 0; 336151497Sru 337151497Sru if (cp == NULL) 338151497Sru return (malloc(nbytes)); 339151497Sru op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 340151497Sru if (op->ov_magic == MAGIC) { 341151497Sru was_alloced++; 342151497Sru i = op->ov_index; 343151497Sru } else { 344151497Sru /* 345151497Sru * Already free, doing "compaction". 346151497Sru * 347151497Sru * Search for the old block of memory on the 348151497Sru * free list. First, check the most common 349151497Sru * case (last element free'd), then (this failing) 350151497Sru * the last ``realloc_srchlen'' items free'd. 351151497Sru * If all lookups fail, then assume the size of 352151497Sru * the memory block being realloc'd is the 353151497Sru * largest possible (so that all "nbytes" of new 354151497Sru * memory are copied into). Note that this could cause 355151497Sru * a memory fault if the old area was tiny, and the moon 356151497Sru * is gibbous. However, that is very unlikely. 357151497Sru */ 358151497Sru if ((i = findbucket(op, 1)) < 0 && 359151497Sru (i = findbucket(op, realloc_srchlen)) < 0) 360151497Sru i = NBUCKETS; 361151497Sru } 362151497Sru onb = 1 << (i + 3); 363151497Sru if (onb < pagesz) 364151497Sru onb -= sizeof (*op) + RSLOP; 365151497Sru else 366151497Sru onb += pagesz - sizeof (*op) - RSLOP; 367151497Sru /* avoid the copy if same size block */ 368151497Sru if (was_alloced) { 369151497Sru if (i) { 370151497Sru i = 1 << (i + 2); 371151497Sru if (i < pagesz) 372151497Sru i -= sizeof (*op) + RSLOP; 373151497Sru else 374151497Sru i += pagesz - sizeof (*op) - RSLOP; 375151497Sru } 376151497Sru if (nbytes <= onb && nbytes > i) { 377151497Sru#ifdef RCHECK 378151497Sru op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 379151497Sru *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 380151497Sru#endif 381151497Sru return(cp); 382151497Sru } else 383151497Sru free(cp); 384151497Sru } 385151497Sru if ((res = malloc(nbytes)) == NULL) 386151497Sru return (NULL); 387151497Sru if (cp != res) /* common optimization if "compacting" */ 388151497Sru bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 389151497Sru return (res); 390151497Sru} 391151497Sru 392151497Sru/* 393151497Sru * Search ``srchlen'' elements of each free list for a block whose 394151497Sru * header starts at ``freep''. If srchlen is -1 search the whole list. 395151497Sru * Return bucket number, or -1 if not found. 396151497Sru */ 397151497Srustatic int 398151497Srufindbucket(freep, srchlen) 399151497Sru union overhead *freep; 400151497Sru int srchlen; 401151497Sru{ 402151497Sru register union overhead *p; 403151497Sru register int i, j; 404151497Sru 405151497Sru for (i = 0; i < NBUCKETS; i++) { 406151497Sru j = 0; 407151497Sru for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 408151497Sru if (p == freep) 409151497Sru return (i); 410151497Sru j++; 411151497Sru } 412151497Sru } 413151497Sru return (-1); 414151497Sru} 415151497Sru 416151497Sru#ifdef MSTATS 417151497Sru/* 418151497Sru * mstats - print out statistics about malloc 419151497Sru * 420151497Sru * Prints two lines of numbers, one showing the length of the free list 421151497Sru * for each size category, the second showing the number of mallocs - 422151497Sru * frees for each size category. 423151497Sru */ 424151497Srumstats(s) 425151497Sru char *s; 426151497Sru{ 427151497Sru register int i, j; 428151497Sru register union overhead *p; 429151497Sru int totfree = 0, 430151497Sru totused = 0; 431151497Sru 432151497Sru fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 433151497Sru for (i = 0; i < NBUCKETS; i++) { 434151497Sru for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 435151497Sru ; 436151497Sru fprintf(stderr, " %d", j); 437151497Sru totfree += j * (1 << (i + 3)); 438151497Sru } 439151497Sru fprintf(stderr, "\nused:\t"); 440151497Sru for (i = 0; i < NBUCKETS; i++) { 441151497Sru fprintf(stderr, " %d", nmalloc[i]); 442151497Sru totused += nmalloc[i] * (1 << (i + 3)); 443151497Sru } 444151497Sru fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 445151497Sru totused, totfree); 446151497Sru} 447151497Sru#endif 448151497Sru 449151497Sru 450151497Srustatic int 451151497Srumorepages(n) 452151497Sruint n; 453151497Sru{ 454151497Sru int fd = -1; 455151497Sru int offset; 456151497Sru 457151497Sru#ifdef NEED_DEV_ZERO 458151497Sru fd = open(_PATH_DEVZERO, O_RDWR, 0); 459151497Sru if (fd == -1) 460151497Sru perror(_PATH_DEVZERO); 461151497Sru#endif 462151497Sru 463151497Sru if (pagepool_end - pagepool_start > pagesz) { 464151497Sru caddr_t addr = (caddr_t) 465151497Sru (((long)pagepool_start + pagesz - 1) & ~(pagesz - 1)); 466151497Sru if (munmap(addr, pagepool_end - addr) != 0) 467151497Sru warn("morepages: munmap %p", addr); 468151497Sru } 469151497Sru 470151497Sru offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1)); 471151497Sru 472151497Sru if ((pagepool_start = mmap(0, n * pagesz, 473151497Sru PROT_READ|PROT_WRITE, 474151497Sru MAP_ANON|MAP_COPY, fd, 0)) == (caddr_t)-1) { 475151497Sru xprintf("Cannot map anonymous memory"); 476151497Sru return 0; 477151497Sru } 478151497Sru pagepool_end = pagepool_start + n * pagesz; 479151497Sru pagepool_start += offset; 480151497Sru 481151497Sru#ifdef NEED_DEV_ZERO 482151497Sru close(fd); 483151497Sru#endif 484151497Sru return n; 485151497Sru} 486151497Sru