1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright (c) 1996-2001 by Sun Microsystems, Inc. 24*0Sstevel@tonic-gate * All rights reserved. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2 */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #ifndef _REENTRANT 34*0Sstevel@tonic-gate #define _REENTRANT 35*0Sstevel@tonic-gate #endif 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include <stdlib.h> 38*0Sstevel@tonic-gate #include <unistd.h> 39*0Sstevel@tonic-gate #include <fcntl.h> 40*0Sstevel@tonic-gate #include <string.h> 41*0Sstevel@tonic-gate #include <errno.h> 42*0Sstevel@tonic-gate #include <memory.h> 43*0Sstevel@tonic-gate #include <thread.h> 44*0Sstevel@tonic-gate #include <synch.h> 45*0Sstevel@tonic-gate #include <malloc.h> 46*0Sstevel@tonic-gate #include <procfs.h> 47*0Sstevel@tonic-gate #include <limits.h> 48*0Sstevel@tonic-gate #include <sys/types.h> 49*0Sstevel@tonic-gate #include <sys/stat.h> 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* debugging macros */ 52*0Sstevel@tonic-gate #ifdef DEBUG 53*0Sstevel@tonic-gate #define ASSERT(p) ((void) ((p) || (abort(), 0))) 54*0Sstevel@tonic-gate #define COUNT(n) ((void) n++) 55*0Sstevel@tonic-gate static int nmalloc, nrealloc, nfree; 56*0Sstevel@tonic-gate #else 57*0Sstevel@tonic-gate #define ASSERT(p) ((void)0) 58*0Sstevel@tonic-gate #define COUNT(n) ((void)0) 59*0Sstevel@tonic-gate #endif /* DEBUG */ 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate /* for conveniences */ 62*0Sstevel@tonic-gate #ifndef NULL 63*0Sstevel@tonic-gate #define NULL (0) 64*0Sstevel@tonic-gate #endif 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #define WORDSIZE (sizeof (WORD)) 67*0Sstevel@tonic-gate #define MINSIZE (sizeof (TREE) - sizeof (WORD)) 68*0Sstevel@tonic-gate #define ROUND(s) if ((s)%WORDSIZE) (s) += (WORDSIZE - ((s)%WORDSIZE)) 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate /* 71*0Sstevel@tonic-gate * All of our allocations will be aligned on the least multiple of 4, 72*0Sstevel@tonic-gate * at least, so the two low order bits are guaranteed to be available. 73*0Sstevel@tonic-gate */ 74*0Sstevel@tonic-gate #ifdef _LP64 75*0Sstevel@tonic-gate #define ALIGN 16 76*0Sstevel@tonic-gate #else 77*0Sstevel@tonic-gate #define ALIGN 8 78*0Sstevel@tonic-gate #endif 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate /* the proto-word; size must be ALIGN bytes */ 81*0Sstevel@tonic-gate typedef union _w_ { 82*0Sstevel@tonic-gate size_t w_i; /* an unsigned int */ 83*0Sstevel@tonic-gate struct _t_ *w_p[2]; /* two pointers */ 84*0Sstevel@tonic-gate } WORD; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate /* structure of a node in the free tree */ 87*0Sstevel@tonic-gate typedef struct _t_ { 88*0Sstevel@tonic-gate WORD t_s; /* size of this element */ 89*0Sstevel@tonic-gate WORD t_p; /* parent node */ 90*0Sstevel@tonic-gate WORD t_l; /* left child */ 91*0Sstevel@tonic-gate WORD t_r; /* right child */ 92*0Sstevel@tonic-gate WORD t_n; /* next in link list */ 93*0Sstevel@tonic-gate WORD t_d; /* dummy to reserve space for self-pointer */ 94*0Sstevel@tonic-gate } TREE; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate /* usable # of bytes in the block */ 97*0Sstevel@tonic-gate #define SIZE(b) (((b)->t_s).w_i) 98*0Sstevel@tonic-gate #define RSIZE(b) (((b)->t_s).w_i & ~BITS01) 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* free tree pointers */ 101*0Sstevel@tonic-gate #define PARENT(b) (((b)->t_p).w_p[0]) 102*0Sstevel@tonic-gate #define LEFT(b) (((b)->t_l).w_p[0]) 103*0Sstevel@tonic-gate #define RIGHT(b) (((b)->t_r).w_p[0]) 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate /* forward link in lists of small blocks */ 106*0Sstevel@tonic-gate #define AFTER(b) (((b)->t_p).w_p[0]) 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* forward and backward links for lists in the tree */ 109*0Sstevel@tonic-gate #define LINKFOR(b) (((b)->t_n).w_p[0]) 110*0Sstevel@tonic-gate #define LINKBAK(b) (((b)->t_p).w_p[0]) 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate /* set/test indicator if a block is in the tree or in a list */ 113*0Sstevel@tonic-gate #define SETNOTREE(b) (LEFT(b) = (TREE *)(-1)) 114*0Sstevel@tonic-gate #define ISNOTREE(b) (LEFT(b) == (TREE *)(-1)) 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate /* functions to get information on a block */ 117*0Sstevel@tonic-gate #define DATA(b) (((char *)(b)) + WORDSIZE) 118*0Sstevel@tonic-gate #define BLOCK(d) ((TREE *)(((char *)(d)) - WORDSIZE)) 119*0Sstevel@tonic-gate #define SELFP(b) (&(NEXT(b)->t_s.w_p[1])) 120*0Sstevel@tonic-gate #define LAST(b) ((b)->t_s.w_p[1]) 121*0Sstevel@tonic-gate #define NEXT(b) ((TREE *)(((char *)(b)) + RSIZE(b) + WORDSIZE)) 122*0Sstevel@tonic-gate #define BOTTOM(b) ((DATA(b) + RSIZE(b) + WORDSIZE) == Baddr) 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate /* functions to set and test the lowest two bits of a word */ 125*0Sstevel@tonic-gate #define BIT0 (01) /* ...001 */ 126*0Sstevel@tonic-gate #define BIT1 (02) /* ...010 */ 127*0Sstevel@tonic-gate #define BITS01 (03) /* ...011 */ 128*0Sstevel@tonic-gate #define ISBIT0(w) ((w) & BIT0) /* Is busy? */ 129*0Sstevel@tonic-gate #define ISBIT1(w) ((w) & BIT1) /* Is the preceding free? */ 130*0Sstevel@tonic-gate #define SETBIT0(w) ((w) |= BIT0) /* Block is busy */ 131*0Sstevel@tonic-gate #define SETBIT1(w) ((w) |= BIT1) /* The preceding is free */ 132*0Sstevel@tonic-gate #define CLRBIT0(w) ((w) &= ~BIT0) /* Clean bit0 */ 133*0Sstevel@tonic-gate #define CLRBIT1(w) ((w) &= ~BIT1) /* Clean bit1 */ 134*0Sstevel@tonic-gate #define SETBITS01(w) ((w) |= BITS01) /* Set bits 0 & 1 */ 135*0Sstevel@tonic-gate #define CLRBITS01(w) ((w) &= ~BITS01) /* Clean bits 0 & 1 */ 136*0Sstevel@tonic-gate #define SETOLD01(n, o) ((n) |= (BITS01 & (o))) 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate /* system call to get more memory */ 139*0Sstevel@tonic-gate #define GETCORE sbrk 140*0Sstevel@tonic-gate #define ERRCORE ((char *)(-1)) 141*0Sstevel@tonic-gate #define CORESIZE (1024*ALIGN) 142*0Sstevel@tonic-gate #define MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */ 143*0Sstevel@tonic-gate #define MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */ 144*0Sstevel@tonic-gate #define MAX_ALIGN (1 + (size_t)SSIZE_MAX) 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate /* where are these *really* declared? */ 147*0Sstevel@tonic-gate extern int _mutex_lock(mutex_t *mp); 148*0Sstevel@tonic-gate extern int _mutex_unlock(mutex_t *mp); 149