17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 54088bb40Sraf * Common Development and Distribution License (the "License"). 64088bb40Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 214088bb40Sraf 221d530678Sraf /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 241d530678Sraf * Use is subject to license terms. 251d530678Sraf */ 261d530678Sraf 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #ifndef debug 357c478bd9Sstevel@tonic-gate #define NDEBUG 367c478bd9Sstevel@tonic-gate #endif 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <stdlib.h> 397c478bd9Sstevel@tonic-gate #include <string.h> 407c478bd9Sstevel@tonic-gate #include "assert.h" 417c478bd9Sstevel@tonic-gate #include "malloc.h" 427c478bd9Sstevel@tonic-gate #include "mallint.h" 437c478bd9Sstevel@tonic-gate #include <thread.h> 441d530678Sraf #include <pthread.h> 457c478bd9Sstevel@tonic-gate #include <synch.h> 467c478bd9Sstevel@tonic-gate #include <unistd.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate static mutex_t mlock = DEFAULTMUTEX; 507c478bd9Sstevel@tonic-gate static ssize_t freespace(struct holdblk *); 517c478bd9Sstevel@tonic-gate static void *malloc_unlocked(size_t, int); 527c478bd9Sstevel@tonic-gate static void *realloc_unlocked(void *, size_t); 537c478bd9Sstevel@tonic-gate static void free_unlocked(void *); 547c478bd9Sstevel@tonic-gate static void *morecore(size_t); 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * use level memory allocater (malloc, free, realloc) 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * -malloc, free, realloc and mallopt form a memory allocator 607c478bd9Sstevel@tonic-gate * similar to malloc, free, and realloc. The routines 617c478bd9Sstevel@tonic-gate * here are much faster than the original, with slightly worse 627c478bd9Sstevel@tonic-gate * space usage (a few percent difference on most input). They 637c478bd9Sstevel@tonic-gate * do not have the property that data in freed blocks is left 647c478bd9Sstevel@tonic-gate * untouched until the space is reallocated. 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * -Memory is kept in the "arena", a singly linked list of blocks. 677c478bd9Sstevel@tonic-gate * These blocks are of 3 types. 687c478bd9Sstevel@tonic-gate * 1. A free block. This is a block not in use by the 697c478bd9Sstevel@tonic-gate * user. It has a 3 word header. (See description 707c478bd9Sstevel@tonic-gate * of the free queue.) 717c478bd9Sstevel@tonic-gate * 2. An allocated block. This is a block the user has 727c478bd9Sstevel@tonic-gate * requested. It has only a 1 word header, pointing 737c478bd9Sstevel@tonic-gate * to the next block of any sort. 747c478bd9Sstevel@tonic-gate * 3. A permanently allocated block. This covers space 757c478bd9Sstevel@tonic-gate * aquired by the user directly through sbrk(). It 767c478bd9Sstevel@tonic-gate * has a 1 word header, as does 2. 777c478bd9Sstevel@tonic-gate * Blocks of type 1 have the lower bit of the pointer to the 787c478bd9Sstevel@tonic-gate * nextblock = 0. Blocks of type 2 and 3 have that bit set, 797c478bd9Sstevel@tonic-gate * to mark them busy. 807c478bd9Sstevel@tonic-gate * 817c478bd9Sstevel@tonic-gate * -Unallocated blocks are kept on an unsorted doubly linked 827c478bd9Sstevel@tonic-gate * free list. 837c478bd9Sstevel@tonic-gate * 847c478bd9Sstevel@tonic-gate * -Memory is allocated in blocks, with sizes specified by the 857c478bd9Sstevel@tonic-gate * user. A circular first-fit startegy is used, with a roving 867c478bd9Sstevel@tonic-gate * head of the free queue, which prevents bunching of small 877c478bd9Sstevel@tonic-gate * blocks at the head of the queue. 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * -Compaction is performed at free time of any blocks immediately 907c478bd9Sstevel@tonic-gate * following the freed block. The freed block will be combined 917c478bd9Sstevel@tonic-gate * with a preceding block during the search phase of malloc. 927c478bd9Sstevel@tonic-gate * Since a freed block is added at the front of the free queue, 937c478bd9Sstevel@tonic-gate * which is moved to the end of the queue if considered and 947c478bd9Sstevel@tonic-gate * rejected during the search, fragmentation only occurs if 957c478bd9Sstevel@tonic-gate * a block with a contiguious preceding block that is free is 967c478bd9Sstevel@tonic-gate * freed and reallocated on the next call to malloc. The 977c478bd9Sstevel@tonic-gate * time savings of this strategy is judged to be worth the 987c478bd9Sstevel@tonic-gate * occasional waste of memory. 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * -Small blocks (of size < MAXSIZE) are not allocated directly. 1017c478bd9Sstevel@tonic-gate * A large "holding" block is allocated via a recursive call to 1027c478bd9Sstevel@tonic-gate * malloc. This block contains a header and ?????? small blocks. 1037c478bd9Sstevel@tonic-gate * Holding blocks for a given size of small block (rounded to the 1047c478bd9Sstevel@tonic-gate * nearest ALIGNSZ bytes) are kept on a queue with the property that any 1057c478bd9Sstevel@tonic-gate * holding block with an unused small block is in front of any without. 1067c478bd9Sstevel@tonic-gate * A list of free blocks is kept within the holding block. 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * description of arena, free queue, holding blocks etc. 1117c478bd9Sstevel@tonic-gate * 1127c478bd9Sstevel@tonic-gate * New compiler and linker does not guarentee order of initialized data. 1137c478bd9Sstevel@tonic-gate * Define freeptr as arena[2-3] to guarentee it follows arena in memory. 1147c478bd9Sstevel@tonic-gate * Later code depends on this order. 1157c478bd9Sstevel@tonic-gate */ 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static struct header arena[4] = { 1187c478bd9Sstevel@tonic-gate {0, 0, 0}, 1197c478bd9Sstevel@tonic-gate {0, 0, 0}, 1207c478bd9Sstevel@tonic-gate {0, 0, 0}, 1217c478bd9Sstevel@tonic-gate {0, 0, 0} 1227c478bd9Sstevel@tonic-gate }; 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * the second word is a minimal block to 1257c478bd9Sstevel@tonic-gate * start the arena. The first is a busy 1267c478bd9Sstevel@tonic-gate * block to be pointed to by the last block. 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate #define freeptr (arena + 2) 1307c478bd9Sstevel@tonic-gate /* first and last entry in free list */ 1317c478bd9Sstevel@tonic-gate static struct header *arenaend; /* ptr to block marking high end of arena */ 1327c478bd9Sstevel@tonic-gate static struct header *lastblk; /* the highest block in the arena */ 1337c478bd9Sstevel@tonic-gate static struct holdblk **holdhead; /* pointer to array of head pointers */ 1347c478bd9Sstevel@tonic-gate /* to holding block chains */ 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * In order to save time calculating indices, the array is 1 too 1377c478bd9Sstevel@tonic-gate * large, and the first element is unused 1387c478bd9Sstevel@tonic-gate * 1397c478bd9Sstevel@tonic-gate * Variables controlling algorithm, esp. how holding blocs are used 1407c478bd9Sstevel@tonic-gate */ 1417c478bd9Sstevel@tonic-gate static int numlblks = NUMLBLKS; 1427c478bd9Sstevel@tonic-gate static int minhead = MINHEAD; 1437c478bd9Sstevel@tonic-gate static int change = 0; /* != 0, once param changes are no longer allowed */ 1447c478bd9Sstevel@tonic-gate static int fastct = FASTCT; 1457c478bd9Sstevel@tonic-gate static unsigned int maxfast = MAXFAST; 1467c478bd9Sstevel@tonic-gate /* number of small block sizes to map to one size */ 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate static int grain = ALIGNSZ; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate #ifdef debug 1517c478bd9Sstevel@tonic-gate static int case1count = 0; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate static void 1547c478bd9Sstevel@tonic-gate checkq(void) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate register struct header *p; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate p = &freeptr[0]; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* check forward */ 1617c478bd9Sstevel@tonic-gate /*CSTYLED*/ 1627c478bd9Sstevel@tonic-gate while (p != &freeptr[1]) { 1637c478bd9Sstevel@tonic-gate p = p->nextfree; 1647c478bd9Sstevel@tonic-gate assert(p->prevfree->nextfree == p); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* check backward */ 1687c478bd9Sstevel@tonic-gate /*CSTYLED*/ 1697c478bd9Sstevel@tonic-gate while (p != &freeptr[0]) { 1707c478bd9Sstevel@tonic-gate p = p->prevfree; 1717c478bd9Sstevel@tonic-gate assert(p->nextfree->prevfree == p); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate #endif 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * malloc(nbytes) - give a user nbytes to use 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate void * 1827c478bd9Sstevel@tonic-gate malloc(size_t nbytes) 1837c478bd9Sstevel@tonic-gate { 1847c478bd9Sstevel@tonic-gate void *ret; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 1877c478bd9Sstevel@tonic-gate ret = malloc_unlocked(nbytes, 0); 1887c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 1897c478bd9Sstevel@tonic-gate return (ret); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * Use malloc_unlocked() to get the address to start with; Given this 1947c478bd9Sstevel@tonic-gate * address, find out the closest address that aligns with the request 1957c478bd9Sstevel@tonic-gate * and return that address after doing some house keeping (refer to the 1967c478bd9Sstevel@tonic-gate * ascii art below). 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate void * 1994088bb40Sraf memalign(size_t alignment, size_t size) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate void *alloc_buf; 2027c478bd9Sstevel@tonic-gate struct header *hd; 2037c478bd9Sstevel@tonic-gate size_t alloc_size; 2047c478bd9Sstevel@tonic-gate uintptr_t fr; 2057c478bd9Sstevel@tonic-gate static int realloc; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if (size == 0 || alignment == 0 || 208*7257d1b4Sraf (alignment & (alignment - 1)) != 0) { 2097c478bd9Sstevel@tonic-gate return (NULL); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate if (alignment <= ALIGNSZ) 2127c478bd9Sstevel@tonic-gate return (malloc(size)); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate alloc_size = size + alignment; 2157c478bd9Sstevel@tonic-gate if (alloc_size < size) { /* overflow */ 2167c478bd9Sstevel@tonic-gate return (NULL); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 2207c478bd9Sstevel@tonic-gate alloc_buf = malloc_unlocked(alloc_size, 1); 2217c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (alloc_buf == NULL) 2247c478bd9Sstevel@tonic-gate return (NULL); 2257c478bd9Sstevel@tonic-gate fr = (uintptr_t)alloc_buf; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate fr = (fr + alignment - 1) / alignment * alignment; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate if (fr == (uintptr_t)alloc_buf) 2307c478bd9Sstevel@tonic-gate return (alloc_buf); 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate if ((fr - (uintptr_t)alloc_buf) <= HEADSZ) { 2337c478bd9Sstevel@tonic-gate /* 2347c478bd9Sstevel@tonic-gate * we hit an edge case, where the space ahead of aligned 2357c478bd9Sstevel@tonic-gate * address is not sufficient to hold 'header' and hence we 2367c478bd9Sstevel@tonic-gate * can't free it. So double the allocation request. 2377c478bd9Sstevel@tonic-gate */ 2387c478bd9Sstevel@tonic-gate realloc++; 2397c478bd9Sstevel@tonic-gate free(alloc_buf); 2407c478bd9Sstevel@tonic-gate alloc_size = size + alignment*2; 2417c478bd9Sstevel@tonic-gate if (alloc_size < size) { 2427c478bd9Sstevel@tonic-gate return (NULL); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 2467c478bd9Sstevel@tonic-gate alloc_buf = malloc_unlocked(alloc_size, 1); 2477c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate if (alloc_buf == NULL) 2507c478bd9Sstevel@tonic-gate return (NULL); 2517c478bd9Sstevel@tonic-gate fr = (uintptr_t)alloc_buf; 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate fr = (fr + alignment - 1) / alignment * alignment; 2547c478bd9Sstevel@tonic-gate if (fr == (uintptr_t)alloc_buf) 2557c478bd9Sstevel@tonic-gate return (alloc_buf); 2567c478bd9Sstevel@tonic-gate if ((fr - (uintptr_t)alloc_buf) <= HEADSZ) { 2577c478bd9Sstevel@tonic-gate fr = fr + alignment; 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate /* 2627c478bd9Sstevel@tonic-gate * +-------+ +-------+ 2637c478bd9Sstevel@tonic-gate * +---| <a> | | <a> |--+ 2647c478bd9Sstevel@tonic-gate * | +-------+<--alloc_buf-->+-------+ | 2657c478bd9Sstevel@tonic-gate * | | | | | | 2667c478bd9Sstevel@tonic-gate * | | | | | | 2677c478bd9Sstevel@tonic-gate * | | | | | | 2687c478bd9Sstevel@tonic-gate * | | | hd--> +-------+ | 2697c478bd9Sstevel@tonic-gate * | | | +---| <b> |<-+ 2707c478bd9Sstevel@tonic-gate * | | | | +-------+<--- fr 2717c478bd9Sstevel@tonic-gate * | | | | | | 2727c478bd9Sstevel@tonic-gate * | | | | | | 2737c478bd9Sstevel@tonic-gate * | | | | | | 2747c478bd9Sstevel@tonic-gate * | | | | | | 2757c478bd9Sstevel@tonic-gate * | | | | | | 2767c478bd9Sstevel@tonic-gate * | | | | | | 2777c478bd9Sstevel@tonic-gate * | +-------+ | +-------+ 2787c478bd9Sstevel@tonic-gate * +-->| next | +-->| next | 2797c478bd9Sstevel@tonic-gate * +-------+ +-------+ 2807c478bd9Sstevel@tonic-gate * 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate hd = (struct header *)((char *)fr - minhead); 2837c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 2847c478bd9Sstevel@tonic-gate hd->nextblk = ((struct header *)((char *)alloc_buf - minhead))->nextblk; 2857c478bd9Sstevel@tonic-gate ((struct header *)((char *)alloc_buf - minhead))->nextblk = SETBUSY(hd); 2867c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 2877c478bd9Sstevel@tonic-gate free(alloc_buf); 2887c478bd9Sstevel@tonic-gate CHECKQ 2897c478bd9Sstevel@tonic-gate return ((void *)fr); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate void * 2934088bb40Sraf valloc(size_t size) 2947c478bd9Sstevel@tonic-gate { 2957c478bd9Sstevel@tonic-gate static unsigned pagesize; 2967c478bd9Sstevel@tonic-gate if (size == 0) 2977c478bd9Sstevel@tonic-gate return (NULL); 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if (!pagesize) 3007c478bd9Sstevel@tonic-gate pagesize = sysconf(_SC_PAGESIZE); 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate return (memalign(pagesize, size)); 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /* 3067c478bd9Sstevel@tonic-gate * malloc_unlocked(nbytes, nosmall) - Do the real work for malloc 3077c478bd9Sstevel@tonic-gate */ 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate static void * 3107c478bd9Sstevel@tonic-gate malloc_unlocked(size_t nbytes, int nosmall) 3117c478bd9Sstevel@tonic-gate { 3127c478bd9Sstevel@tonic-gate struct header *blk; 3137c478bd9Sstevel@tonic-gate size_t nb; /* size of entire block we need */ 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate /* on first call, initialize */ 3167c478bd9Sstevel@tonic-gate if (freeptr[0].nextfree == GROUND) { 3177c478bd9Sstevel@tonic-gate /* initialize arena */ 3187c478bd9Sstevel@tonic-gate arena[1].nextblk = (struct header *)BUSY; 3197c478bd9Sstevel@tonic-gate arena[0].nextblk = (struct header *)BUSY; 3207c478bd9Sstevel@tonic-gate lastblk = arenaend = &(arena[1]); 3217c478bd9Sstevel@tonic-gate /* initialize free queue */ 3227c478bd9Sstevel@tonic-gate freeptr[0].nextfree = &(freeptr[1]); 3237c478bd9Sstevel@tonic-gate freeptr[1].nextblk = &(arena[0]); 3247c478bd9Sstevel@tonic-gate freeptr[1].prevfree = &(freeptr[0]); 3257c478bd9Sstevel@tonic-gate /* mark that small blocks not init yet */ 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate if (nbytes == 0) 3287c478bd9Sstevel@tonic-gate return (NULL); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate if (nbytes <= maxfast && !nosmall) { 3317c478bd9Sstevel@tonic-gate /* 3327c478bd9Sstevel@tonic-gate * We can allocate out of a holding block 3337c478bd9Sstevel@tonic-gate */ 3347c478bd9Sstevel@tonic-gate struct holdblk *holdblk; /* head of right sized queue */ 3357c478bd9Sstevel@tonic-gate struct lblk *lblk; /* pointer to a little block */ 3367c478bd9Sstevel@tonic-gate struct holdblk *newhold; 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate if (!change) { 3397c478bd9Sstevel@tonic-gate int i; 3407c478bd9Sstevel@tonic-gate /* 3417c478bd9Sstevel@tonic-gate * This allocates space for hold block 3427c478bd9Sstevel@tonic-gate * pointers by calling malloc recursively. 3437c478bd9Sstevel@tonic-gate * Maxfast is temporarily set to 0, to 3447c478bd9Sstevel@tonic-gate * avoid infinite recursion. allocate 3457c478bd9Sstevel@tonic-gate * space for an extra ptr so that an index 3467c478bd9Sstevel@tonic-gate * is just ->blksz/grain, with the first 3477c478bd9Sstevel@tonic-gate * ptr unused. 3487c478bd9Sstevel@tonic-gate */ 3497c478bd9Sstevel@tonic-gate change = 1; /* change to algorithm params */ 3507c478bd9Sstevel@tonic-gate /* no longer allowed */ 3517c478bd9Sstevel@tonic-gate /* 3527c478bd9Sstevel@tonic-gate * temporarily alter maxfast, to avoid 3537c478bd9Sstevel@tonic-gate * infinite recursion 3547c478bd9Sstevel@tonic-gate */ 3557c478bd9Sstevel@tonic-gate maxfast = 0; 3567c478bd9Sstevel@tonic-gate holdhead = (struct holdblk **) 3577c478bd9Sstevel@tonic-gate malloc_unlocked(sizeof (struct holdblk *) * 3587c478bd9Sstevel@tonic-gate (fastct + 1), 0); 3597c478bd9Sstevel@tonic-gate if (holdhead == NULL) 3607c478bd9Sstevel@tonic-gate return (malloc_unlocked(nbytes, 0)); 3617c478bd9Sstevel@tonic-gate for (i = 1; i <= fastct; i++) { 3627c478bd9Sstevel@tonic-gate holdhead[i] = HGROUND; 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate maxfast = fastct * grain; 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * Note that this uses the absolute min header size (MINHEAD) 3687c478bd9Sstevel@tonic-gate * unlike the large block case which uses minhead 3697c478bd9Sstevel@tonic-gate * 3707c478bd9Sstevel@tonic-gate * round up to nearest multiple of grain 3717c478bd9Sstevel@tonic-gate * code assumes grain is a multiple of MINHEAD 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate /* round up to grain */ 3747c478bd9Sstevel@tonic-gate nb = (nbytes + grain - 1) / grain * grain; 3757c478bd9Sstevel@tonic-gate holdblk = holdhead[nb / grain]; 3767c478bd9Sstevel@tonic-gate nb = nb + MINHEAD; 3777c478bd9Sstevel@tonic-gate /* 3787c478bd9Sstevel@tonic-gate * look for space in the holding block. Blocks with 3797c478bd9Sstevel@tonic-gate * space will be in front of those without 3807c478bd9Sstevel@tonic-gate */ 3817c478bd9Sstevel@tonic-gate if ((holdblk != HGROUND) && (holdblk->lfreeq != LGROUND)) { 3827c478bd9Sstevel@tonic-gate /* there is space */ 3837c478bd9Sstevel@tonic-gate lblk = holdblk->lfreeq; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Now make lfreeq point to a free block. 3877c478bd9Sstevel@tonic-gate * If lblk has been previously allocated and 3887c478bd9Sstevel@tonic-gate * freed, it has a valid pointer to use. 3897c478bd9Sstevel@tonic-gate * Otherwise, lblk is at the beginning of 3907c478bd9Sstevel@tonic-gate * the unallocated blocks at the end of 3917c478bd9Sstevel@tonic-gate * the holding block, so, if there is room, take 3927c478bd9Sstevel@tonic-gate * the next space. If not, mark holdblk full, 3937c478bd9Sstevel@tonic-gate * and move holdblk to the end of the queue 3947c478bd9Sstevel@tonic-gate */ 3957c478bd9Sstevel@tonic-gate if (lblk < holdblk->unused) { 3967c478bd9Sstevel@tonic-gate /* move to next holdblk, if this one full */ 3977c478bd9Sstevel@tonic-gate if ((holdblk->lfreeq = 3987c478bd9Sstevel@tonic-gate CLRSMAL(lblk->header.nextfree)) == 3997c478bd9Sstevel@tonic-gate LGROUND) { 4007c478bd9Sstevel@tonic-gate holdhead[(nb-MINHEAD) / grain] = 4017c478bd9Sstevel@tonic-gate holdblk->nexthblk; 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate } else if (((char *)holdblk->unused + nb) < 4047c478bd9Sstevel@tonic-gate ((char *)holdblk + HOLDSZ(nb))) { 4057c478bd9Sstevel@tonic-gate holdblk->unused = (struct lblk *) 4067c478bd9Sstevel@tonic-gate ((char *)holdblk->unused+nb); 4077c478bd9Sstevel@tonic-gate holdblk->lfreeq = holdblk->unused; 4087c478bd9Sstevel@tonic-gate } else { 4097c478bd9Sstevel@tonic-gate holdblk->unused = (struct lblk *) 4107c478bd9Sstevel@tonic-gate ((char *)holdblk->unused+nb); 4117c478bd9Sstevel@tonic-gate holdblk->lfreeq = LGROUND; 4127c478bd9Sstevel@tonic-gate holdhead[(nb-MINHEAD)/grain] = 4137c478bd9Sstevel@tonic-gate holdblk->nexthblk; 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate /* mark as busy and small */ 4167c478bd9Sstevel@tonic-gate lblk->header.holder = (struct holdblk *)SETALL(holdblk); 4177c478bd9Sstevel@tonic-gate } else { 4187c478bd9Sstevel@tonic-gate /* we need a new holding block */ 4197c478bd9Sstevel@tonic-gate newhold = (struct holdblk *) 4207c478bd9Sstevel@tonic-gate malloc_unlocked(HOLDSZ(nb), 0); 4217c478bd9Sstevel@tonic-gate if ((char *)newhold == NULL) { 4227c478bd9Sstevel@tonic-gate return (NULL); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate /* add to head of free queue */ 4257c478bd9Sstevel@tonic-gate if (holdblk != HGROUND) { 4267c478bd9Sstevel@tonic-gate newhold->nexthblk = holdblk; 4277c478bd9Sstevel@tonic-gate newhold->prevhblk = holdblk->prevhblk; 4287c478bd9Sstevel@tonic-gate holdblk->prevhblk = newhold; 4297c478bd9Sstevel@tonic-gate newhold->prevhblk->nexthblk = newhold; 4307c478bd9Sstevel@tonic-gate } else { 4317c478bd9Sstevel@tonic-gate newhold->nexthblk = newhold->prevhblk = newhold; 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate holdhead[(nb-MINHEAD)/grain] = newhold; 4347c478bd9Sstevel@tonic-gate /* set up newhold */ 4357c478bd9Sstevel@tonic-gate lblk = (struct lblk *)(newhold->space); 4367c478bd9Sstevel@tonic-gate newhold->lfreeq = newhold->unused = 4377c478bd9Sstevel@tonic-gate (struct lblk *)((char *)newhold->space+nb); 4387c478bd9Sstevel@tonic-gate lblk->header.holder = (struct holdblk *)SETALL(newhold); 4397c478bd9Sstevel@tonic-gate newhold->blksz = nb-MINHEAD; 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate #ifdef debug 4427c478bd9Sstevel@tonic-gate assert(((struct holdblk *)CLRALL(lblk->header.holder))->blksz >= 4437c478bd9Sstevel@tonic-gate nbytes); 4447c478bd9Sstevel@tonic-gate #endif /* debug */ 4457c478bd9Sstevel@tonic-gate return ((char *)lblk + MINHEAD); 4467c478bd9Sstevel@tonic-gate } else { 4477c478bd9Sstevel@tonic-gate /* 4487c478bd9Sstevel@tonic-gate * We need an ordinary block 4497c478bd9Sstevel@tonic-gate */ 4507c478bd9Sstevel@tonic-gate struct header *newblk; /* used for creating a block */ 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate /* get number of bytes we need */ 4537c478bd9Sstevel@tonic-gate nb = nbytes + minhead; 4547c478bd9Sstevel@tonic-gate nb = (nb + ALIGNSZ - 1) / ALIGNSZ * ALIGNSZ; /* align */ 4557c478bd9Sstevel@tonic-gate nb = (nb > MINBLKSZ) ? nb : MINBLKSZ; 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * see if there is a big enough block 4587c478bd9Sstevel@tonic-gate * If none exists, you will get to freeptr[1]. 4597c478bd9Sstevel@tonic-gate * freeptr[1].next = &arena[0], so when you do the test, 4607c478bd9Sstevel@tonic-gate * the result is a large positive number, since arena[0] 4617c478bd9Sstevel@tonic-gate * comes before all blocks. Arena[0] is marked busy so 4627c478bd9Sstevel@tonic-gate * that it will not be compacted. This kludge is for the 4637c478bd9Sstevel@tonic-gate * sake of the almighty efficiency. 4647c478bd9Sstevel@tonic-gate */ 4657c478bd9Sstevel@tonic-gate /* check that a very large request won't cause an inf. loop */ 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate if ((freeptr[1].nextblk-&(freeptr[1])) < nb) { 4687c478bd9Sstevel@tonic-gate return (NULL); 4697c478bd9Sstevel@tonic-gate } else { 4707c478bd9Sstevel@tonic-gate struct header *next; /* following block */ 4717c478bd9Sstevel@tonic-gate struct header *nextnext; /* block after next */ 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate blk = freeptr; 4747c478bd9Sstevel@tonic-gate do { 4757c478bd9Sstevel@tonic-gate blk = blk->nextfree; 4767c478bd9Sstevel@tonic-gate /* see if we can compact */ 4777c478bd9Sstevel@tonic-gate next = blk->nextblk; 4787c478bd9Sstevel@tonic-gate if (!TESTBUSY(nextnext = next->nextblk)) { 4797c478bd9Sstevel@tonic-gate do { 4807c478bd9Sstevel@tonic-gate DELFREEQ(next); 4817c478bd9Sstevel@tonic-gate next = nextnext; 4827c478bd9Sstevel@tonic-gate nextnext = next->nextblk; 4837c478bd9Sstevel@tonic-gate } while (!TESTBUSY(nextnext)); 4847c478bd9Sstevel@tonic-gate /* 4857c478bd9Sstevel@tonic-gate * next will be at most == to lastblk, 4867c478bd9Sstevel@tonic-gate * but I think the >= test is faster 4877c478bd9Sstevel@tonic-gate */ 4887c478bd9Sstevel@tonic-gate if (next >= arenaend) 4897c478bd9Sstevel@tonic-gate lastblk = blk; 4907c478bd9Sstevel@tonic-gate blk->nextblk = next; 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate } while (((char *)(next) - (char *)blk) < nb); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate /* 4957c478bd9Sstevel@tonic-gate * if we didn't find a block, get more memory 4967c478bd9Sstevel@tonic-gate */ 4977c478bd9Sstevel@tonic-gate if (blk == &(freeptr[1])) { 4987c478bd9Sstevel@tonic-gate /* 4997c478bd9Sstevel@tonic-gate * careful coding could likely replace 5007c478bd9Sstevel@tonic-gate * newend with arenaend 5017c478bd9Sstevel@tonic-gate */ 5027c478bd9Sstevel@tonic-gate struct header *newend; /* new end of arena */ 5037c478bd9Sstevel@tonic-gate ssize_t nget; /* number of words to get */ 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate /* 5067c478bd9Sstevel@tonic-gate * Three cases - 1. There is space between arenaend 5077c478bd9Sstevel@tonic-gate * and the break value that will become 5087c478bd9Sstevel@tonic-gate * a permanently allocated block. 5097c478bd9Sstevel@tonic-gate * 2. Case 1 is not true, and the last 5107c478bd9Sstevel@tonic-gate * block is allocated. 5117c478bd9Sstevel@tonic-gate * 3. Case 1 is not true, and the last 5127c478bd9Sstevel@tonic-gate * block is free 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate if ((newblk = (struct header *)sbrk(0)) != 5157c478bd9Sstevel@tonic-gate (struct header *)((char *)arenaend + HEADSZ)) { 5167c478bd9Sstevel@tonic-gate /* case 1 */ 5177c478bd9Sstevel@tonic-gate #ifdef debug 5187c478bd9Sstevel@tonic-gate if (case1count++ > 0) 5197c478bd9Sstevel@tonic-gate (void) write(2, "Case 1 hit more that once." 5207c478bd9Sstevel@tonic-gate " brk or sbrk?\n", 41); 5217c478bd9Sstevel@tonic-gate #endif 5227c478bd9Sstevel@tonic-gate /* get size to fetch */ 5237c478bd9Sstevel@tonic-gate nget = nb + HEADSZ; 5247c478bd9Sstevel@tonic-gate /* round up to a block */ 5257c478bd9Sstevel@tonic-gate nget = (nget + BLOCKSZ - 1)/BLOCKSZ * BLOCKSZ; 5267c478bd9Sstevel@tonic-gate assert((uintptr_t)newblk % ALIGNSZ == 0); 5277c478bd9Sstevel@tonic-gate /* get memory */ 5287c478bd9Sstevel@tonic-gate if (morecore(nget) == (void *)-1) 5297c478bd9Sstevel@tonic-gate return (NULL); 5307c478bd9Sstevel@tonic-gate /* add to arena */ 5317c478bd9Sstevel@tonic-gate newend = (struct header *)((char *)newblk + nget 5327c478bd9Sstevel@tonic-gate - HEADSZ); 5337c478bd9Sstevel@tonic-gate assert((uintptr_t)newblk % ALIGNSZ == 0); 5347c478bd9Sstevel@tonic-gate newend->nextblk = SETBUSY(&(arena[1])); 5357c478bd9Sstevel@tonic-gate /* ??? newblk ?? */ 5367c478bd9Sstevel@tonic-gate newblk->nextblk = newend; 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate /* 5397c478bd9Sstevel@tonic-gate * space becomes a permanently allocated block. 5407c478bd9Sstevel@tonic-gate * This is likely not mt-safe as lock is not 5417c478bd9Sstevel@tonic-gate * shared with brk or sbrk 5427c478bd9Sstevel@tonic-gate */ 5437c478bd9Sstevel@tonic-gate arenaend->nextblk = SETBUSY(newblk); 5447c478bd9Sstevel@tonic-gate /* adjust other pointers */ 5457c478bd9Sstevel@tonic-gate arenaend = newend; 5467c478bd9Sstevel@tonic-gate lastblk = newblk; 5477c478bd9Sstevel@tonic-gate blk = newblk; 5487c478bd9Sstevel@tonic-gate } else if (TESTBUSY(lastblk->nextblk)) { 5497c478bd9Sstevel@tonic-gate /* case 2 */ 5507c478bd9Sstevel@tonic-gate nget = (nb + BLOCKSZ - 1) / BLOCKSZ * BLOCKSZ; 5517c478bd9Sstevel@tonic-gate if (morecore(nget) == (void *)-1) 5527c478bd9Sstevel@tonic-gate return (NULL); 5537c478bd9Sstevel@tonic-gate /* block must be word aligned */ 5547c478bd9Sstevel@tonic-gate assert(((uintptr_t)newblk%ALIGNSZ) == 0); 5557c478bd9Sstevel@tonic-gate /* 5567c478bd9Sstevel@tonic-gate * stub at old arenaend becomes first word 5577c478bd9Sstevel@tonic-gate * in blk 5587c478bd9Sstevel@tonic-gate */ 5597c478bd9Sstevel@tonic-gate /* ??? newblk = arenaend; */ 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate newend = 5627c478bd9Sstevel@tonic-gate (struct header *)((char *)arenaend+nget); 5637c478bd9Sstevel@tonic-gate newend->nextblk = SETBUSY(&(arena[1])); 5647c478bd9Sstevel@tonic-gate arenaend->nextblk = newend; 5657c478bd9Sstevel@tonic-gate lastblk = blk = arenaend; 5667c478bd9Sstevel@tonic-gate arenaend = newend; 5677c478bd9Sstevel@tonic-gate } else { 5687c478bd9Sstevel@tonic-gate /* case 3 */ 5697c478bd9Sstevel@tonic-gate /* 5707c478bd9Sstevel@tonic-gate * last block in arena is at end of memory and 5717c478bd9Sstevel@tonic-gate * is free 5727c478bd9Sstevel@tonic-gate */ 5737c478bd9Sstevel@tonic-gate /* 1.7 had this backward without cast */ 5747c478bd9Sstevel@tonic-gate nget = nb - 5757c478bd9Sstevel@tonic-gate ((char *)arenaend - (char *)lastblk); 5767c478bd9Sstevel@tonic-gate nget = (nget + (BLOCKSZ - 1)) / 5777c478bd9Sstevel@tonic-gate BLOCKSZ * BLOCKSZ; 5787c478bd9Sstevel@tonic-gate assert(((uintptr_t)newblk % ALIGNSZ) == 0); 5797c478bd9Sstevel@tonic-gate if (morecore(nget) == (void *)-1) 5807c478bd9Sstevel@tonic-gate return (NULL); 5817c478bd9Sstevel@tonic-gate /* combine with last block, put in arena */ 5827c478bd9Sstevel@tonic-gate newend = (struct header *) 5837c478bd9Sstevel@tonic-gate ((char *)arenaend + nget); 5847c478bd9Sstevel@tonic-gate arenaend = lastblk->nextblk = newend; 5857c478bd9Sstevel@tonic-gate newend->nextblk = SETBUSY(&(arena[1])); 5867c478bd9Sstevel@tonic-gate /* set which block to use */ 5877c478bd9Sstevel@tonic-gate blk = lastblk; 5887c478bd9Sstevel@tonic-gate DELFREEQ(blk); 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate } else { 5917c478bd9Sstevel@tonic-gate struct header *nblk; /* next block */ 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate /* take block found of free queue */ 5947c478bd9Sstevel@tonic-gate DELFREEQ(blk); 5957c478bd9Sstevel@tonic-gate /* 5967c478bd9Sstevel@tonic-gate * make head of free queue immediately follow blk, 5977c478bd9Sstevel@tonic-gate * unless blk was at the end of the queue 5987c478bd9Sstevel@tonic-gate */ 5997c478bd9Sstevel@tonic-gate nblk = blk->nextfree; 6007c478bd9Sstevel@tonic-gate if (nblk != &(freeptr[1])) { 6017c478bd9Sstevel@tonic-gate MOVEHEAD(nblk); 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate /* blk now points to an adequate block */ 6057c478bd9Sstevel@tonic-gate if (((char *)blk->nextblk - (char *)blk) - nb >= MINBLKSZ) { 6067c478bd9Sstevel@tonic-gate /* carve out the right size block */ 6077c478bd9Sstevel@tonic-gate /* newblk will be the remainder */ 6087c478bd9Sstevel@tonic-gate newblk = (struct header *)((char *)blk + nb); 6097c478bd9Sstevel@tonic-gate newblk->nextblk = blk->nextblk; 6107c478bd9Sstevel@tonic-gate /* mark the block busy */ 6117c478bd9Sstevel@tonic-gate blk->nextblk = SETBUSY(newblk); 6127c478bd9Sstevel@tonic-gate ADDFREEQ(newblk); 6137c478bd9Sstevel@tonic-gate /* if blk was lastblk, make newblk lastblk */ 6147c478bd9Sstevel@tonic-gate if (blk == lastblk) 6157c478bd9Sstevel@tonic-gate lastblk = newblk; 6167c478bd9Sstevel@tonic-gate } else { 6177c478bd9Sstevel@tonic-gate /* just mark the block busy */ 6187c478bd9Sstevel@tonic-gate blk->nextblk = SETBUSY(blk->nextblk); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate CHECKQ 6227c478bd9Sstevel@tonic-gate assert((char *)CLRALL(blk->nextblk) - 6237c478bd9Sstevel@tonic-gate ((char *)blk + minhead) >= nbytes); 6247c478bd9Sstevel@tonic-gate assert((char *)CLRALL(blk->nextblk) - 6257c478bd9Sstevel@tonic-gate ((char *)blk + minhead) < nbytes + MINBLKSZ); 6267c478bd9Sstevel@tonic-gate return ((char *)blk + minhead); 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * free(ptr) - free block that user thinks starts at ptr 6317c478bd9Sstevel@tonic-gate * 6327c478bd9Sstevel@tonic-gate * input - ptr-1 contains the block header. 6337c478bd9Sstevel@tonic-gate * If the header points forward, we have a normal 6347c478bd9Sstevel@tonic-gate * block pointing to the next block 6357c478bd9Sstevel@tonic-gate * if the header points backward, we have a small 6367c478bd9Sstevel@tonic-gate * block from a holding block. 6377c478bd9Sstevel@tonic-gate * In both cases, the busy bit must be set 6387c478bd9Sstevel@tonic-gate */ 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate void 6417c478bd9Sstevel@tonic-gate free(void *ptr) 6427c478bd9Sstevel@tonic-gate { 6437c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 6447c478bd9Sstevel@tonic-gate free_unlocked(ptr); 6457c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 6467c478bd9Sstevel@tonic-gate } 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate /* 6497c478bd9Sstevel@tonic-gate * free_unlocked(ptr) - Do the real work for free() 6507c478bd9Sstevel@tonic-gate */ 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate void 6537c478bd9Sstevel@tonic-gate free_unlocked(void *ptr) 6547c478bd9Sstevel@tonic-gate { 6557c478bd9Sstevel@tonic-gate struct holdblk *holdblk; /* block holding blk */ 6567c478bd9Sstevel@tonic-gate struct holdblk *oldhead; /* former head of the hold block */ 6577c478bd9Sstevel@tonic-gate /* queue containing blk's holder */ 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate if (ptr == NULL) 6607c478bd9Sstevel@tonic-gate return; 6617c478bd9Sstevel@tonic-gate if (TESTSMAL(((struct header *)((char *)ptr - MINHEAD))->nextblk)) { 6627c478bd9Sstevel@tonic-gate struct lblk *lblk; /* pointer to freed block */ 6637c478bd9Sstevel@tonic-gate ssize_t offset; /* choice of header lists */ 6647c478bd9Sstevel@tonic-gate 6657c478bd9Sstevel@tonic-gate lblk = (struct lblk *)CLRBUSY((char *)ptr - MINHEAD); 6667c478bd9Sstevel@tonic-gate assert((struct header *)lblk < arenaend); 6677c478bd9Sstevel@tonic-gate assert((struct header *)lblk > arena); 6687c478bd9Sstevel@tonic-gate /* allow twits (e.g. awk) to free a block twice */ 6697c478bd9Sstevel@tonic-gate holdblk = lblk->header.holder; 6707c478bd9Sstevel@tonic-gate if (!TESTBUSY(holdblk)) 6717c478bd9Sstevel@tonic-gate return; 6727c478bd9Sstevel@tonic-gate holdblk = (struct holdblk *)CLRALL(holdblk); 6737c478bd9Sstevel@tonic-gate /* put lblk on its hold block's free list */ 6747c478bd9Sstevel@tonic-gate lblk->header.nextfree = SETSMAL(holdblk->lfreeq); 6757c478bd9Sstevel@tonic-gate holdblk->lfreeq = lblk; 6767c478bd9Sstevel@tonic-gate /* move holdblk to head of queue, if its not already there */ 6777c478bd9Sstevel@tonic-gate offset = holdblk->blksz / grain; 6787c478bd9Sstevel@tonic-gate oldhead = holdhead[offset]; 6797c478bd9Sstevel@tonic-gate if (oldhead != holdblk) { 6807c478bd9Sstevel@tonic-gate /* first take out of current spot */ 6817c478bd9Sstevel@tonic-gate holdhead[offset] = holdblk; 6827c478bd9Sstevel@tonic-gate holdblk->nexthblk->prevhblk = holdblk->prevhblk; 6837c478bd9Sstevel@tonic-gate holdblk->prevhblk->nexthblk = holdblk->nexthblk; 6847c478bd9Sstevel@tonic-gate /* now add at front */ 6857c478bd9Sstevel@tonic-gate holdblk->nexthblk = oldhead; 6867c478bd9Sstevel@tonic-gate holdblk->prevhblk = oldhead->prevhblk; 6877c478bd9Sstevel@tonic-gate oldhead->prevhblk = holdblk; 6887c478bd9Sstevel@tonic-gate holdblk->prevhblk->nexthblk = holdblk; 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate } else { 6917c478bd9Sstevel@tonic-gate struct header *blk; /* real start of block */ 6927c478bd9Sstevel@tonic-gate struct header *next; /* next = blk->nextblk */ 6937c478bd9Sstevel@tonic-gate struct header *nextnext; /* block after next */ 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate blk = (struct header *)((char *)ptr - minhead); 6967c478bd9Sstevel@tonic-gate next = blk->nextblk; 6977c478bd9Sstevel@tonic-gate /* take care of twits (e.g. awk) who return blocks twice */ 6987c478bd9Sstevel@tonic-gate if (!TESTBUSY(next)) 6997c478bd9Sstevel@tonic-gate return; 7007c478bd9Sstevel@tonic-gate blk->nextblk = next = CLRBUSY(next); 7017c478bd9Sstevel@tonic-gate ADDFREEQ(blk); 7027c478bd9Sstevel@tonic-gate /* see if we can compact */ 7037c478bd9Sstevel@tonic-gate if (!TESTBUSY(nextnext = next->nextblk)) { 7047c478bd9Sstevel@tonic-gate do { 7057c478bd9Sstevel@tonic-gate DELFREEQ(next); 7067c478bd9Sstevel@tonic-gate next = nextnext; 7077c478bd9Sstevel@tonic-gate } while (!TESTBUSY(nextnext = next->nextblk)); 7087c478bd9Sstevel@tonic-gate if (next == arenaend) lastblk = blk; 7097c478bd9Sstevel@tonic-gate blk->nextblk = next; 7107c478bd9Sstevel@tonic-gate } 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate CHECKQ 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate /* 7177c478bd9Sstevel@tonic-gate * realloc(ptr, size) - give the user a block of size "size", with 7187c478bd9Sstevel@tonic-gate * the contents pointed to by ptr. Free ptr. 7197c478bd9Sstevel@tonic-gate */ 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate void * 7227c478bd9Sstevel@tonic-gate realloc(void *ptr, size_t size) 7237c478bd9Sstevel@tonic-gate { 7247c478bd9Sstevel@tonic-gate void *retval; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 7277c478bd9Sstevel@tonic-gate retval = realloc_unlocked(ptr, size); 7287c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 7297c478bd9Sstevel@tonic-gate return (retval); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate /* 7347c478bd9Sstevel@tonic-gate * realloc_unlocked(ptr) - Do the real work for realloc() 7357c478bd9Sstevel@tonic-gate */ 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate static void * 7387c478bd9Sstevel@tonic-gate realloc_unlocked(void *ptr, size_t size) 7397c478bd9Sstevel@tonic-gate { 7407c478bd9Sstevel@tonic-gate struct header *blk; /* block ptr is contained in */ 7417c478bd9Sstevel@tonic-gate size_t trusize; /* block size as allocater sees it */ 7427c478bd9Sstevel@tonic-gate char *newptr; /* pointer to user's new block */ 7437c478bd9Sstevel@tonic-gate size_t cpysize; /* amount to copy */ 7447c478bd9Sstevel@tonic-gate struct header *next; /* block after blk */ 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate if (ptr == NULL) 7477c478bd9Sstevel@tonic-gate return (malloc_unlocked(size, 0)); 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate if (size == 0) { 7507c478bd9Sstevel@tonic-gate free_unlocked(ptr); 7517c478bd9Sstevel@tonic-gate return (NULL); 7527c478bd9Sstevel@tonic-gate } 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate if (TESTSMAL(((struct lblk *)((char *)ptr - MINHEAD))-> 7557c478bd9Sstevel@tonic-gate header.holder)) { 7567c478bd9Sstevel@tonic-gate /* 7577c478bd9Sstevel@tonic-gate * we have a special small block which can't be expanded 7587c478bd9Sstevel@tonic-gate * 7597c478bd9Sstevel@tonic-gate * This makes the assumption that even if the user is 7607c478bd9Sstevel@tonic-gate * reallocating a free block, malloc doesn't alter the contents 7617c478bd9Sstevel@tonic-gate * of small blocks 7627c478bd9Sstevel@tonic-gate */ 7637c478bd9Sstevel@tonic-gate newptr = malloc_unlocked(size, 0); 7647c478bd9Sstevel@tonic-gate if (newptr == NULL) 7657c478bd9Sstevel@tonic-gate return (NULL); 7667c478bd9Sstevel@tonic-gate /* this isn't to save time--its to protect the twits */ 7677c478bd9Sstevel@tonic-gate if ((char *)ptr != newptr) { 7687c478bd9Sstevel@tonic-gate struct lblk *lblk; 7697c478bd9Sstevel@tonic-gate lblk = (struct lblk *)((char *)ptr - MINHEAD); 7707c478bd9Sstevel@tonic-gate cpysize = ((struct holdblk *) 7717c478bd9Sstevel@tonic-gate CLRALL(lblk->header.holder))->blksz; 7727c478bd9Sstevel@tonic-gate cpysize = (size > cpysize) ? cpysize : size; 7737c478bd9Sstevel@tonic-gate (void) memcpy(newptr, ptr, cpysize); 7747c478bd9Sstevel@tonic-gate free_unlocked(ptr); 7757c478bd9Sstevel@tonic-gate } 7767c478bd9Sstevel@tonic-gate } else { 7777c478bd9Sstevel@tonic-gate blk = (struct header *)((char *)ptr - minhead); 7787c478bd9Sstevel@tonic-gate next = blk->nextblk; 7797c478bd9Sstevel@tonic-gate /* 7807c478bd9Sstevel@tonic-gate * deal with twits who reallocate free blocks 7817c478bd9Sstevel@tonic-gate * 7827c478bd9Sstevel@tonic-gate * if they haven't reset minblk via getopt, that's 7837c478bd9Sstevel@tonic-gate * their problem 7847c478bd9Sstevel@tonic-gate */ 7857c478bd9Sstevel@tonic-gate if (!TESTBUSY(next)) { 7867c478bd9Sstevel@tonic-gate DELFREEQ(blk); 7877c478bd9Sstevel@tonic-gate blk->nextblk = SETBUSY(next); 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate next = CLRBUSY(next); 7907c478bd9Sstevel@tonic-gate /* make blk as big as possible */ 7917c478bd9Sstevel@tonic-gate if (!TESTBUSY(next->nextblk)) { 7927c478bd9Sstevel@tonic-gate do { 7937c478bd9Sstevel@tonic-gate DELFREEQ(next); 7947c478bd9Sstevel@tonic-gate next = next->nextblk; 7957c478bd9Sstevel@tonic-gate } while (!TESTBUSY(next->nextblk)); 7967c478bd9Sstevel@tonic-gate blk->nextblk = SETBUSY(next); 7977c478bd9Sstevel@tonic-gate if (next >= arenaend) lastblk = blk; 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate /* get size we really need */ 8007c478bd9Sstevel@tonic-gate trusize = size+minhead; 8017c478bd9Sstevel@tonic-gate trusize = (trusize + ALIGNSZ - 1)/ALIGNSZ*ALIGNSZ; 8027c478bd9Sstevel@tonic-gate trusize = (trusize >= MINBLKSZ) ? trusize : MINBLKSZ; 8037c478bd9Sstevel@tonic-gate /* see if we have enough */ 8047c478bd9Sstevel@tonic-gate /* this isn't really the copy size, but I need a register */ 8057c478bd9Sstevel@tonic-gate cpysize = (char *)next - (char *)blk; 8067c478bd9Sstevel@tonic-gate if (cpysize >= trusize) { 8077c478bd9Sstevel@tonic-gate /* carve out the size we need */ 8087c478bd9Sstevel@tonic-gate struct header *newblk; /* remainder */ 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate if (cpysize - trusize >= MINBLKSZ) { 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * carve out the right size block 8137c478bd9Sstevel@tonic-gate * newblk will be the remainder 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate newblk = (struct header *)((char *)blk + 8167c478bd9Sstevel@tonic-gate trusize); 8177c478bd9Sstevel@tonic-gate newblk->nextblk = next; 8187c478bd9Sstevel@tonic-gate blk->nextblk = SETBUSY(newblk); 8197c478bd9Sstevel@tonic-gate /* at this point, next is invalid */ 8207c478bd9Sstevel@tonic-gate ADDFREEQ(newblk); 8217c478bd9Sstevel@tonic-gate /* if blk was lastblk, make newblk lastblk */ 8227c478bd9Sstevel@tonic-gate if (blk == lastblk) 8237c478bd9Sstevel@tonic-gate lastblk = newblk; 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate newptr = ptr; 8267c478bd9Sstevel@tonic-gate } else { 8277c478bd9Sstevel@tonic-gate /* bite the bullet, and call malloc */ 8287c478bd9Sstevel@tonic-gate cpysize = (size > cpysize) ? cpysize : size; 8297c478bd9Sstevel@tonic-gate newptr = malloc_unlocked(size, 0); 8307c478bd9Sstevel@tonic-gate if (newptr == NULL) 8317c478bd9Sstevel@tonic-gate return (NULL); 8327c478bd9Sstevel@tonic-gate (void) memcpy(newptr, ptr, cpysize); 8337c478bd9Sstevel@tonic-gate free_unlocked(ptr); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate return (newptr); 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate /* 8417c478bd9Sstevel@tonic-gate * calloc - allocate and clear memory block 8427c478bd9Sstevel@tonic-gate */ 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate void * 8457c478bd9Sstevel@tonic-gate calloc(size_t num, size_t size) 8467c478bd9Sstevel@tonic-gate { 8477c478bd9Sstevel@tonic-gate char *mp; 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate num *= size; 8507c478bd9Sstevel@tonic-gate mp = malloc(num); 8517c478bd9Sstevel@tonic-gate if (mp == NULL) 8527c478bd9Sstevel@tonic-gate return (NULL); 8537c478bd9Sstevel@tonic-gate (void) memset(mp, 0, num); 8547c478bd9Sstevel@tonic-gate return (mp); 8557c478bd9Sstevel@tonic-gate } 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate 8587c478bd9Sstevel@tonic-gate /* 8597c478bd9Sstevel@tonic-gate * Mallopt - set options for allocation 8607c478bd9Sstevel@tonic-gate * 8617c478bd9Sstevel@tonic-gate * Mallopt provides for control over the allocation algorithm. 8627c478bd9Sstevel@tonic-gate * The cmds available are: 8637c478bd9Sstevel@tonic-gate * 8647c478bd9Sstevel@tonic-gate * M_MXFAST Set maxfast to value. Maxfast is the size of the 8657c478bd9Sstevel@tonic-gate * largest small, quickly allocated block. Maxfast 8667c478bd9Sstevel@tonic-gate * may be set to 0 to disable fast allocation entirely. 8677c478bd9Sstevel@tonic-gate * 8687c478bd9Sstevel@tonic-gate * M_NLBLKS Set numlblks to value. Numlblks is the number of 8697c478bd9Sstevel@tonic-gate * small blocks per holding block. Value must be 8707c478bd9Sstevel@tonic-gate * greater than 0. 8717c478bd9Sstevel@tonic-gate * 8727c478bd9Sstevel@tonic-gate * M_GRAIN Set grain to value. The sizes of all blocks 8737c478bd9Sstevel@tonic-gate * smaller than maxfast are considered to be rounded 8747c478bd9Sstevel@tonic-gate * up to the nearest multiple of grain. The default 8757c478bd9Sstevel@tonic-gate * value of grain is the smallest number of bytes 8767c478bd9Sstevel@tonic-gate * which will allow alignment of any data type. Grain 8777c478bd9Sstevel@tonic-gate * will be rounded up to a multiple of its default, 8787c478bd9Sstevel@tonic-gate * and maxsize will be rounded up to a multiple of 8797c478bd9Sstevel@tonic-gate * grain. Value must be greater than 0. 8807c478bd9Sstevel@tonic-gate * 8817c478bd9Sstevel@tonic-gate * M_KEEP Retain data in freed block until the next malloc, 8827c478bd9Sstevel@tonic-gate * realloc, or calloc. Value is ignored. 8837c478bd9Sstevel@tonic-gate * This option is provided only for compatibility with 8847c478bd9Sstevel@tonic-gate * the old version of malloc, and is not recommended. 8857c478bd9Sstevel@tonic-gate * 8867c478bd9Sstevel@tonic-gate * returns - 0, upon successful completion 8877c478bd9Sstevel@tonic-gate * 1, if malloc has previously been called or 8887c478bd9Sstevel@tonic-gate * if value or cmd have illegal values 8897c478bd9Sstevel@tonic-gate */ 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate int 8924088bb40Sraf mallopt(int cmd, int value) 8937c478bd9Sstevel@tonic-gate { 8947c478bd9Sstevel@tonic-gate /* disallow changes once a small block is allocated */ 8957c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 8967c478bd9Sstevel@tonic-gate if (change) { 8977c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 8987c478bd9Sstevel@tonic-gate return (1); 8997c478bd9Sstevel@tonic-gate } 9007c478bd9Sstevel@tonic-gate switch (cmd) { 9017c478bd9Sstevel@tonic-gate case M_MXFAST: 9027c478bd9Sstevel@tonic-gate if (value < 0) { 9037c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 9047c478bd9Sstevel@tonic-gate return (1); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate fastct = (value + grain - 1) / grain; 9077c478bd9Sstevel@tonic-gate maxfast = grain*fastct; 9087c478bd9Sstevel@tonic-gate break; 9097c478bd9Sstevel@tonic-gate case M_NLBLKS: 9107c478bd9Sstevel@tonic-gate if (value <= 1) { 9117c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 9127c478bd9Sstevel@tonic-gate return (1); 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate numlblks = value; 9157c478bd9Sstevel@tonic-gate break; 9167c478bd9Sstevel@tonic-gate case M_GRAIN: 9177c478bd9Sstevel@tonic-gate if (value <= 0) { 9187c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 9197c478bd9Sstevel@tonic-gate return (1); 9207c478bd9Sstevel@tonic-gate } 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate /* round grain up to a multiple of ALIGNSZ */ 9237c478bd9Sstevel@tonic-gate grain = (value + ALIGNSZ - 1)/ALIGNSZ*ALIGNSZ; 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* reduce fastct appropriately */ 9267c478bd9Sstevel@tonic-gate fastct = (maxfast + grain - 1) / grain; 9277c478bd9Sstevel@tonic-gate maxfast = grain * fastct; 9287c478bd9Sstevel@tonic-gate break; 9297c478bd9Sstevel@tonic-gate case M_KEEP: 9307c478bd9Sstevel@tonic-gate if (change && holdhead != NULL) { 931*7257d1b4Sraf (void) mutex_unlock(&mlock); 9327c478bd9Sstevel@tonic-gate return (1); 9337c478bd9Sstevel@tonic-gate } 9347c478bd9Sstevel@tonic-gate minhead = HEADSZ; 9357c478bd9Sstevel@tonic-gate break; 9367c478bd9Sstevel@tonic-gate default: 9377c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 9387c478bd9Sstevel@tonic-gate return (1); 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 9417c478bd9Sstevel@tonic-gate return (0); 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate 9447c478bd9Sstevel@tonic-gate /* 9457c478bd9Sstevel@tonic-gate * mallinfo-provide information about space usage 9467c478bd9Sstevel@tonic-gate * 9477c478bd9Sstevel@tonic-gate * input - max; mallinfo will return the size of the 9487c478bd9Sstevel@tonic-gate * largest block < max. 9497c478bd9Sstevel@tonic-gate * 9507c478bd9Sstevel@tonic-gate * output - a structure containing a description of 9517c478bd9Sstevel@tonic-gate * of space usage, defined in malloc.h 9527c478bd9Sstevel@tonic-gate */ 9537c478bd9Sstevel@tonic-gate 9547c478bd9Sstevel@tonic-gate struct mallinfo 9554088bb40Sraf mallinfo(void) 9567c478bd9Sstevel@tonic-gate { 9577c478bd9Sstevel@tonic-gate struct header *blk, *next; /* ptr to ordinary blocks */ 9587c478bd9Sstevel@tonic-gate struct holdblk *hblk; /* ptr to holding blocks */ 9597c478bd9Sstevel@tonic-gate struct mallinfo inf; /* return value */ 9607c478bd9Sstevel@tonic-gate int i; /* the ubiquitous counter */ 9617c478bd9Sstevel@tonic-gate ssize_t size; /* size of a block */ 9627c478bd9Sstevel@tonic-gate ssize_t fsp; /* free space in 1 hold block */ 9637c478bd9Sstevel@tonic-gate 9647c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 9657c478bd9Sstevel@tonic-gate (void) memset(&inf, 0, sizeof (struct mallinfo)); 9667c478bd9Sstevel@tonic-gate if (freeptr[0].nextfree == GROUND) { 9677c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 9687c478bd9Sstevel@tonic-gate return (inf); 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate blk = CLRBUSY(arena[1].nextblk); 9717c478bd9Sstevel@tonic-gate /* return total space used */ 9727c478bd9Sstevel@tonic-gate inf.arena = (char *)arenaend - (char *)blk; 9737c478bd9Sstevel@tonic-gate 9747c478bd9Sstevel@tonic-gate /* 9757c478bd9Sstevel@tonic-gate * loop through arena, counting # of blocks, and 9767c478bd9Sstevel@tonic-gate * and space used by blocks 9777c478bd9Sstevel@tonic-gate */ 9787c478bd9Sstevel@tonic-gate next = CLRBUSY(blk->nextblk); 9797c478bd9Sstevel@tonic-gate while (next != &(arena[1])) { 9807c478bd9Sstevel@tonic-gate inf.ordblks++; 9817c478bd9Sstevel@tonic-gate size = (char *)next - (char *)blk; 9827c478bd9Sstevel@tonic-gate if (TESTBUSY(blk->nextblk)) { 9837c478bd9Sstevel@tonic-gate inf.uordblks += size; 9847c478bd9Sstevel@tonic-gate inf.keepcost += HEADSZ-MINHEAD; 9857c478bd9Sstevel@tonic-gate } else { 9867c478bd9Sstevel@tonic-gate inf.fordblks += size; 9877c478bd9Sstevel@tonic-gate } 9887c478bd9Sstevel@tonic-gate blk = next; 9897c478bd9Sstevel@tonic-gate next = CLRBUSY(blk->nextblk); 9907c478bd9Sstevel@tonic-gate } 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate /* 9937c478bd9Sstevel@tonic-gate * if any holding block have been allocated 9947c478bd9Sstevel@tonic-gate * then examine space in holding blks 9957c478bd9Sstevel@tonic-gate */ 9967c478bd9Sstevel@tonic-gate if (change && holdhead != NULL) { 9977c478bd9Sstevel@tonic-gate for (i = fastct; i > 0; i--) { /* loop thru ea. chain */ 9987c478bd9Sstevel@tonic-gate hblk = holdhead[i]; 9997c478bd9Sstevel@tonic-gate /* do only if chain not empty */ 10007c478bd9Sstevel@tonic-gate if (hblk != HGROUND) { 10017c478bd9Sstevel@tonic-gate size = hblk->blksz + 10027c478bd9Sstevel@tonic-gate sizeof (struct lblk) - sizeof (int); 10037c478bd9Sstevel@tonic-gate do { /* loop thru 1 hold blk chain */ 10047c478bd9Sstevel@tonic-gate inf.hblks++; 10057c478bd9Sstevel@tonic-gate fsp = freespace(hblk); 10067c478bd9Sstevel@tonic-gate inf.fsmblks += fsp; 10077c478bd9Sstevel@tonic-gate inf.usmblks += numlblks*size - fsp; 10087c478bd9Sstevel@tonic-gate inf.smblks += numlblks; 10097c478bd9Sstevel@tonic-gate hblk = hblk->nexthblk; 10107c478bd9Sstevel@tonic-gate } while (hblk != holdhead[i]); 10117c478bd9Sstevel@tonic-gate } 10127c478bd9Sstevel@tonic-gate } 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate inf.hblkhd = (inf.smblks / numlblks) * sizeof (struct holdblk); 10157c478bd9Sstevel@tonic-gate /* holding block were counted in ordblks, so subtract off */ 10167c478bd9Sstevel@tonic-gate inf.ordblks -= inf.hblks; 10177c478bd9Sstevel@tonic-gate inf.uordblks -= inf.hblkhd + inf.usmblks + inf.fsmblks; 10187c478bd9Sstevel@tonic-gate inf.keepcost -= inf.hblks*(HEADSZ - MINHEAD); 10197c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 10207c478bd9Sstevel@tonic-gate return (inf); 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate 10247c478bd9Sstevel@tonic-gate /* 10257c478bd9Sstevel@tonic-gate * freespace - calc. how much space is used in the free 10267c478bd9Sstevel@tonic-gate * small blocks in a given holding block 10277c478bd9Sstevel@tonic-gate * 10287c478bd9Sstevel@tonic-gate * input - hblk = given holding block 10297c478bd9Sstevel@tonic-gate * 10307c478bd9Sstevel@tonic-gate * returns space used in free small blocks of hblk 10317c478bd9Sstevel@tonic-gate */ 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate static ssize_t 10347c478bd9Sstevel@tonic-gate freespace(struct holdblk *holdblk) 10357c478bd9Sstevel@tonic-gate { 10367c478bd9Sstevel@tonic-gate struct lblk *lblk; 10377c478bd9Sstevel@tonic-gate ssize_t space = 0; 10387c478bd9Sstevel@tonic-gate ssize_t size; 10397c478bd9Sstevel@tonic-gate struct lblk *unused; 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate lblk = CLRSMAL(holdblk->lfreeq); 10427c478bd9Sstevel@tonic-gate size = holdblk->blksz + sizeof (struct lblk) - sizeof (int); 10437c478bd9Sstevel@tonic-gate unused = CLRSMAL(holdblk->unused); 10447c478bd9Sstevel@tonic-gate /* follow free chain */ 10457c478bd9Sstevel@tonic-gate while ((lblk != LGROUND) && (lblk != unused)) { 10467c478bd9Sstevel@tonic-gate space += size; 10477c478bd9Sstevel@tonic-gate lblk = CLRSMAL(lblk->header.nextfree); 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate space += ((char *)holdblk + HOLDSZ(size)) - (char *)unused; 10507c478bd9Sstevel@tonic-gate return (space); 10517c478bd9Sstevel@tonic-gate } 10527c478bd9Sstevel@tonic-gate 10537c478bd9Sstevel@tonic-gate static void * 10547c478bd9Sstevel@tonic-gate morecore(size_t bytes) 10557c478bd9Sstevel@tonic-gate { 10567c478bd9Sstevel@tonic-gate void * ret; 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate if (bytes > LONG_MAX) { 10597c478bd9Sstevel@tonic-gate intptr_t wad; 10607c478bd9Sstevel@tonic-gate /* 10617c478bd9Sstevel@tonic-gate * The request size is too big. We need to do this in 10627c478bd9Sstevel@tonic-gate * chunks. Sbrk only takes an int for an arg. 10637c478bd9Sstevel@tonic-gate */ 10647c478bd9Sstevel@tonic-gate if (bytes == ULONG_MAX) 10657c478bd9Sstevel@tonic-gate return ((void *)-1); 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate ret = sbrk(0); 10687c478bd9Sstevel@tonic-gate wad = LONG_MAX; 10697c478bd9Sstevel@tonic-gate while (wad > 0) { 10707c478bd9Sstevel@tonic-gate if (sbrk(wad) == (void *)-1) { 10717c478bd9Sstevel@tonic-gate if (ret != sbrk(0)) 10727c478bd9Sstevel@tonic-gate (void) sbrk(-LONG_MAX); 10737c478bd9Sstevel@tonic-gate return ((void *)-1); 10747c478bd9Sstevel@tonic-gate } 10757c478bd9Sstevel@tonic-gate bytes -= LONG_MAX; 10767c478bd9Sstevel@tonic-gate wad = bytes; 10777c478bd9Sstevel@tonic-gate } 10787c478bd9Sstevel@tonic-gate } else 10797c478bd9Sstevel@tonic-gate ret = sbrk(bytes); 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate return (ret); 10827c478bd9Sstevel@tonic-gate } 10837c478bd9Sstevel@tonic-gate 10847c478bd9Sstevel@tonic-gate #ifdef debug 10857c478bd9Sstevel@tonic-gate int 10867c478bd9Sstevel@tonic-gate check_arena(void) 10877c478bd9Sstevel@tonic-gate { 10887c478bd9Sstevel@tonic-gate struct header *blk, *prev, *next; /* ptr to ordinary blocks */ 10897c478bd9Sstevel@tonic-gate 10907c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 10917c478bd9Sstevel@tonic-gate if (freeptr[0].nextfree == GROUND) { 10927c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 10937c478bd9Sstevel@tonic-gate return (-1); 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate blk = arena + 1; 10967c478bd9Sstevel@tonic-gate 10977c478bd9Sstevel@tonic-gate /* loop through arena, checking */ 10987c478bd9Sstevel@tonic-gate blk = (struct header *)CLRALL(blk->nextblk); 10997c478bd9Sstevel@tonic-gate next = (struct header *)CLRALL(blk->nextblk); 11007c478bd9Sstevel@tonic-gate while (next != arena + 1) { 11017c478bd9Sstevel@tonic-gate assert(blk >= arena + 1); 11027c478bd9Sstevel@tonic-gate assert(blk <= lastblk); 11037c478bd9Sstevel@tonic-gate assert(next >= blk + 1); 11047c478bd9Sstevel@tonic-gate assert(((uintptr_t)((struct header *)blk->nextblk) & 11057c478bd9Sstevel@tonic-gate (4 | SMAL)) == 0); 11067c478bd9Sstevel@tonic-gate 11077c478bd9Sstevel@tonic-gate if (TESTBUSY(blk->nextblk) == 0) { 11087c478bd9Sstevel@tonic-gate assert(blk->nextfree >= freeptr); 11097c478bd9Sstevel@tonic-gate assert(blk->prevfree >= freeptr); 11107c478bd9Sstevel@tonic-gate assert(blk->nextfree <= lastblk); 11117c478bd9Sstevel@tonic-gate assert(blk->prevfree <= lastblk); 11127c478bd9Sstevel@tonic-gate assert(((uintptr_t)((struct header *)blk->nextfree) & 11137c478bd9Sstevel@tonic-gate 7) == 0); 11147c478bd9Sstevel@tonic-gate assert(((uintptr_t)((struct header *)blk->prevfree) & 11157c478bd9Sstevel@tonic-gate 7) == 0 || blk->prevfree == freeptr); 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate blk = next; 11187c478bd9Sstevel@tonic-gate next = CLRBUSY(blk->nextblk); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 11217c478bd9Sstevel@tonic-gate return (0); 11227c478bd9Sstevel@tonic-gate } 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate #define RSTALLOC 1 11257c478bd9Sstevel@tonic-gate #endif 11267c478bd9Sstevel@tonic-gate 11277c478bd9Sstevel@tonic-gate #ifdef RSTALLOC 11287c478bd9Sstevel@tonic-gate /* 11297c478bd9Sstevel@tonic-gate * rstalloc - reset alloc routines 11307c478bd9Sstevel@tonic-gate * 11317c478bd9Sstevel@tonic-gate * description - return allocated memory and reset 11327c478bd9Sstevel@tonic-gate * allocation pointers. 11337c478bd9Sstevel@tonic-gate * 11347c478bd9Sstevel@tonic-gate * Warning - This is for debugging purposes only. 11357c478bd9Sstevel@tonic-gate * It will return all memory allocated after 11367c478bd9Sstevel@tonic-gate * the first call to malloc, even if some 11377c478bd9Sstevel@tonic-gate * of it was fetched by a user's sbrk(). 11387c478bd9Sstevel@tonic-gate */ 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate void 11417c478bd9Sstevel@tonic-gate rstalloc(void) 11427c478bd9Sstevel@tonic-gate { 11437c478bd9Sstevel@tonic-gate (void) mutex_lock(&mlock); 11447c478bd9Sstevel@tonic-gate minhead = MINHEAD; 11457c478bd9Sstevel@tonic-gate grain = ALIGNSZ; 11467c478bd9Sstevel@tonic-gate numlblks = NUMLBLKS; 11477c478bd9Sstevel@tonic-gate fastct = FASTCT; 11487c478bd9Sstevel@tonic-gate maxfast = MAXFAST; 11497c478bd9Sstevel@tonic-gate change = 0; 11507c478bd9Sstevel@tonic-gate if (freeptr[0].nextfree == GROUND) { 11517c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 11527c478bd9Sstevel@tonic-gate return; 11537c478bd9Sstevel@tonic-gate } 11547c478bd9Sstevel@tonic-gate brk(CLRBUSY(arena[1].nextblk)); 11557c478bd9Sstevel@tonic-gate freeptr[0].nextfree = GROUND; 11567c478bd9Sstevel@tonic-gate #ifdef debug 11577c478bd9Sstevel@tonic-gate case1count = 0; 11587c478bd9Sstevel@tonic-gate #endif 11597c478bd9Sstevel@tonic-gate (void) mutex_unlock(&mlock); 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate #endif /* RSTALLOC */ 11627c478bd9Sstevel@tonic-gate 11637c478bd9Sstevel@tonic-gate /* 11647c478bd9Sstevel@tonic-gate * cfree is an undocumented, obsolete function 11657c478bd9Sstevel@tonic-gate */ 11667c478bd9Sstevel@tonic-gate 11674088bb40Sraf /* ARGSUSED1 */ 11687c478bd9Sstevel@tonic-gate void 11694088bb40Sraf cfree(void *p, size_t num, size_t size) 11707c478bd9Sstevel@tonic-gate { 11717c478bd9Sstevel@tonic-gate free(p); 11727c478bd9Sstevel@tonic-gate } 11731d530678Sraf 11741d530678Sraf static void 11751d530678Sraf malloc_prepare() 11761d530678Sraf { 11771d530678Sraf (void) mutex_lock(&mlock); 11781d530678Sraf } 11791d530678Sraf 11801d530678Sraf static void 11811d530678Sraf malloc_release() 11821d530678Sraf { 11831d530678Sraf (void) mutex_unlock(&mlock); 11841d530678Sraf } 11851d530678Sraf 11861d530678Sraf #pragma init(malloc_init) 11871d530678Sraf static void 11881d530678Sraf malloc_init(void) 11891d530678Sraf { 11901d530678Sraf (void) pthread_atfork(malloc_prepare, malloc_release, malloc_release); 11911d530678Sraf } 1192