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