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