1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 23*7c478bd9Sstevel@tonic-gate 24*7c478bd9Sstevel@tonic-gate /* 25*7c478bd9Sstevel@tonic-gate * Copyright (c) 1986 by Sun Microsystems, Inc. 26*7c478bd9Sstevel@tonic-gate */ 27*7c478bd9Sstevel@tonic-gate 28*7c478bd9Sstevel@tonic-gate /* 29*7c478bd9Sstevel@tonic-gate * file: mallint.h 30*7c478bd9Sstevel@tonic-gate * description: 31*7c478bd9Sstevel@tonic-gate * 32*7c478bd9Sstevel@tonic-gate * Definitions for malloc.c and friends (realloc.c, memalign.c) 33*7c478bd9Sstevel@tonic-gate * 34*7c478bd9Sstevel@tonic-gate * The node header structure. Header info never overlaps with user 35*7c478bd9Sstevel@tonic-gate * data space, in order to accommodate the following atrocity: 36*7c478bd9Sstevel@tonic-gate * free(p); 37*7c478bd9Sstevel@tonic-gate * realloc(p, newsize); 38*7c478bd9Sstevel@tonic-gate * ... which was historically used to obtain storage compaction as 39*7c478bd9Sstevel@tonic-gate * a side effect of the realloc() call, when the block referenced 40*7c478bd9Sstevel@tonic-gate * by p was coalesced with another free block by the call to free(). 41*7c478bd9Sstevel@tonic-gate * 42*7c478bd9Sstevel@tonic-gate * To reduce storage consumption, a header block is associated with 43*7c478bd9Sstevel@tonic-gate * free blocks only, not allocated blocks. 44*7c478bd9Sstevel@tonic-gate * When a free block is allocated, its header block is put on 45*7c478bd9Sstevel@tonic-gate * a free header block list. 46*7c478bd9Sstevel@tonic-gate * 47*7c478bd9Sstevel@tonic-gate * This creates a header space and a free block space. 48*7c478bd9Sstevel@tonic-gate * The left pointer of a header blocks is used to chain free header 49*7c478bd9Sstevel@tonic-gate * blocks together. New header blocks are allocated in chunks of 50*7c478bd9Sstevel@tonic-gate * NFREE_HDRS. 51*7c478bd9Sstevel@tonic-gate */ 52*7c478bd9Sstevel@tonic-gate #include <malloc.h> 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate typedef enum {false,true} bool; 55*7c478bd9Sstevel@tonic-gate typedef struct freehdr *Freehdr; 56*7c478bd9Sstevel@tonic-gate typedef struct dblk *Dblk; 57*7c478bd9Sstevel@tonic-gate typedef unsigned int uint; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate /* 60*7c478bd9Sstevel@tonic-gate * Description of a header for a free block 61*7c478bd9Sstevel@tonic-gate * Only free blocks have such headers. 62*7c478bd9Sstevel@tonic-gate */ 63*7c478bd9Sstevel@tonic-gate struct freehdr { 64*7c478bd9Sstevel@tonic-gate Freehdr left; /* Left tree pointer */ 65*7c478bd9Sstevel@tonic-gate Freehdr right; /* Right tree pointer */ 66*7c478bd9Sstevel@tonic-gate Dblk block; /* Ptr to the data block */ 67*7c478bd9Sstevel@tonic-gate uint size; 68*7c478bd9Sstevel@tonic-gate }; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate #define NIL ((Freehdr) 0) 71*7c478bd9Sstevel@tonic-gate #define NFREE_HDRS 512 /* Get this many headers at a time */ 72*7c478bd9Sstevel@tonic-gate #define SMALLEST_BLK sizeof(struct dblk) /* Size of smallest block */ 73*7c478bd9Sstevel@tonic-gate #define NULL 0 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* 76*7c478bd9Sstevel@tonic-gate * Description of a data block. 77*7c478bd9Sstevel@tonic-gate * A data block consists of a length word, possibly followed by 78*7c478bd9Sstevel@tonic-gate * a filler word for alignment, followed by the user's data. 79*7c478bd9Sstevel@tonic-gate * To back up from the user's data to the length word, use 80*7c478bd9Sstevel@tonic-gate * (address of data) - ALIGNSIZ; 81*7c478bd9Sstevel@tonic-gate */ 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate #ifdef sparc 84*7c478bd9Sstevel@tonic-gate #define ALIGNSIZ sizeof(double) 85*7c478bd9Sstevel@tonic-gate struct dblk { 86*7c478bd9Sstevel@tonic-gate uint size; /* Size of the block */ 87*7c478bd9Sstevel@tonic-gate uint filler; /* filler, for double alignment */ 88*7c478bd9Sstevel@tonic-gate char data[ALIGNSIZ]; /* Addr returned to the caller */ 89*7c478bd9Sstevel@tonic-gate }; 90*7c478bd9Sstevel@tonic-gate #endif 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate #ifdef mc68000 93*7c478bd9Sstevel@tonic-gate #define ALIGNSIZ sizeof(uint) 94*7c478bd9Sstevel@tonic-gate struct dblk { 95*7c478bd9Sstevel@tonic-gate uint size; /* Size of the block */ 96*7c478bd9Sstevel@tonic-gate char data[ALIGNSIZ]; /* Addr returned to the caller */ 97*7c478bd9Sstevel@tonic-gate }; 98*7c478bd9Sstevel@tonic-gate #endif 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate /* 102*7c478bd9Sstevel@tonic-gate * weight(x) is the size of a block, in bytes; or 0 if and only if x 103*7c478bd9Sstevel@tonic-gate * is a null pointer. Note that malloc() and free() should be 104*7c478bd9Sstevel@tonic-gate * prepared to deal with things like zero-length blocks, which 105*7c478bd9Sstevel@tonic-gate * can be introduced by errant programs. 106*7c478bd9Sstevel@tonic-gate */ 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate #define weight(x) ((x) == NIL? 0: (x->size)) 109*7c478bd9Sstevel@tonic-gate #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 110*7c478bd9Sstevel@tonic-gate #define nextblk(p, size) ((Dblk) ((char *) (p) + (size))) 111*7c478bd9Sstevel@tonic-gate #define max(a, b) ((a) < (b)? (b): (a)) 112*7c478bd9Sstevel@tonic-gate #define min(a, b) ((a) < (b)? (a): (b)) 113*7c478bd9Sstevel@tonic-gate #define heapsize() (_ubound - _lbound) 114*7c478bd9Sstevel@tonic-gate #define misaligned(p) ((unsigned)(p)&3) 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate extern Freehdr _root; 117*7c478bd9Sstevel@tonic-gate extern char *_lbound, *_ubound; 118*7c478bd9Sstevel@tonic-gate extern int malloc_debug(); 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate extern struct mallinfo __mallinfo; 121