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 227c478bd9Sstevel@tonic-gate /* 23*1d530678Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 244088bb40Sraf * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 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" /* SVr4.0 1.2 */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <fcntl.h> 357c478bd9Sstevel@tonic-gate #include <string.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <memory.h> 387c478bd9Sstevel@tonic-gate #include <thread.h> 39*1d530678Sraf #include <pthread.h> 407c478bd9Sstevel@tonic-gate #include <synch.h> 417c478bd9Sstevel@tonic-gate #include <procfs.h> 427c478bd9Sstevel@tonic-gate #include <limits.h> 437c478bd9Sstevel@tonic-gate #include <sys/types.h> 447c478bd9Sstevel@tonic-gate #include <sys/stat.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* debugging macros */ 477c478bd9Sstevel@tonic-gate #ifdef DEBUG 487c478bd9Sstevel@tonic-gate #define ASSERT(p) ((void) ((p) || (abort(), 0))) 497c478bd9Sstevel@tonic-gate #define COUNT(n) ((void) n++) 507c478bd9Sstevel@tonic-gate static int nmalloc, nrealloc, nfree; 517c478bd9Sstevel@tonic-gate #else 527c478bd9Sstevel@tonic-gate #define ASSERT(p) ((void)0) 537c478bd9Sstevel@tonic-gate #define COUNT(n) ((void)0) 547c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* for conveniences */ 577c478bd9Sstevel@tonic-gate #ifndef NULL 587c478bd9Sstevel@tonic-gate #define NULL (0) 597c478bd9Sstevel@tonic-gate #endif 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #define WORDSIZE (sizeof (WORD)) 627c478bd9Sstevel@tonic-gate #define MINSIZE (sizeof (TREE) - sizeof (WORD)) 637c478bd9Sstevel@tonic-gate #define ROUND(s) if ((s)%WORDSIZE) (s) += (WORDSIZE - ((s)%WORDSIZE)) 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * All of our allocations will be aligned on the least multiple of 4, 677c478bd9Sstevel@tonic-gate * at least, so the two low order bits are guaranteed to be available. 687c478bd9Sstevel@tonic-gate */ 697c478bd9Sstevel@tonic-gate #ifdef _LP64 707c478bd9Sstevel@tonic-gate #define ALIGN 16 717c478bd9Sstevel@tonic-gate #else 727c478bd9Sstevel@tonic-gate #define ALIGN 8 737c478bd9Sstevel@tonic-gate #endif 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* the proto-word; size must be ALIGN bytes */ 767c478bd9Sstevel@tonic-gate typedef union _w_ { 777c478bd9Sstevel@tonic-gate size_t w_i; /* an unsigned int */ 787c478bd9Sstevel@tonic-gate struct _t_ *w_p[2]; /* two pointers */ 797c478bd9Sstevel@tonic-gate } WORD; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* structure of a node in the free tree */ 827c478bd9Sstevel@tonic-gate typedef struct _t_ { 837c478bd9Sstevel@tonic-gate WORD t_s; /* size of this element */ 847c478bd9Sstevel@tonic-gate WORD t_p; /* parent node */ 857c478bd9Sstevel@tonic-gate WORD t_l; /* left child */ 867c478bd9Sstevel@tonic-gate WORD t_r; /* right child */ 877c478bd9Sstevel@tonic-gate WORD t_n; /* next in link list */ 887c478bd9Sstevel@tonic-gate WORD t_d; /* dummy to reserve space for self-pointer */ 897c478bd9Sstevel@tonic-gate } TREE; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* usable # of bytes in the block */ 927c478bd9Sstevel@tonic-gate #define SIZE(b) (((b)->t_s).w_i) 937c478bd9Sstevel@tonic-gate #define RSIZE(b) (((b)->t_s).w_i & ~BITS01) 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* free tree pointers */ 967c478bd9Sstevel@tonic-gate #define PARENT(b) (((b)->t_p).w_p[0]) 977c478bd9Sstevel@tonic-gate #define LEFT(b) (((b)->t_l).w_p[0]) 987c478bd9Sstevel@tonic-gate #define RIGHT(b) (((b)->t_r).w_p[0]) 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* forward link in lists of small blocks */ 1017c478bd9Sstevel@tonic-gate #define AFTER(b) (((b)->t_p).w_p[0]) 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* forward and backward links for lists in the tree */ 1047c478bd9Sstevel@tonic-gate #define LINKFOR(b) (((b)->t_n).w_p[0]) 1057c478bd9Sstevel@tonic-gate #define LINKBAK(b) (((b)->t_p).w_p[0]) 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* set/test indicator if a block is in the tree or in a list */ 1087c478bd9Sstevel@tonic-gate #define SETNOTREE(b) (LEFT(b) = (TREE *)(-1)) 1097c478bd9Sstevel@tonic-gate #define ISNOTREE(b) (LEFT(b) == (TREE *)(-1)) 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* functions to get information on a block */ 1127c478bd9Sstevel@tonic-gate #define DATA(b) (((char *)(b)) + WORDSIZE) 1137c478bd9Sstevel@tonic-gate #define BLOCK(d) ((TREE *)(((char *)(d)) - WORDSIZE)) 1147c478bd9Sstevel@tonic-gate #define SELFP(b) (&(NEXT(b)->t_s.w_p[1])) 1157c478bd9Sstevel@tonic-gate #define LAST(b) ((b)->t_s.w_p[1]) 1167c478bd9Sstevel@tonic-gate #define NEXT(b) ((TREE *)(((char *)(b)) + RSIZE(b) + WORDSIZE)) 1177c478bd9Sstevel@tonic-gate #define BOTTOM(b) ((DATA(b) + RSIZE(b) + WORDSIZE) == Baddr) 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* functions to set and test the lowest two bits of a word */ 1207c478bd9Sstevel@tonic-gate #define BIT0 (01) /* ...001 */ 1217c478bd9Sstevel@tonic-gate #define BIT1 (02) /* ...010 */ 1227c478bd9Sstevel@tonic-gate #define BITS01 (03) /* ...011 */ 1237c478bd9Sstevel@tonic-gate #define ISBIT0(w) ((w) & BIT0) /* Is busy? */ 1247c478bd9Sstevel@tonic-gate #define ISBIT1(w) ((w) & BIT1) /* Is the preceding free? */ 1257c478bd9Sstevel@tonic-gate #define SETBIT0(w) ((w) |= BIT0) /* Block is busy */ 1267c478bd9Sstevel@tonic-gate #define SETBIT1(w) ((w) |= BIT1) /* The preceding is free */ 1277c478bd9Sstevel@tonic-gate #define CLRBIT0(w) ((w) &= ~BIT0) /* Clean bit0 */ 1287c478bd9Sstevel@tonic-gate #define CLRBIT1(w) ((w) &= ~BIT1) /* Clean bit1 */ 1297c478bd9Sstevel@tonic-gate #define SETBITS01(w) ((w) |= BITS01) /* Set bits 0 & 1 */ 1307c478bd9Sstevel@tonic-gate #define CLRBITS01(w) ((w) &= ~BITS01) /* Clean bits 0 & 1 */ 1317c478bd9Sstevel@tonic-gate #define SETOLD01(n, o) ((n) |= (BITS01 & (o))) 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /* system call to get more memory */ 1347c478bd9Sstevel@tonic-gate #define GETCORE sbrk 1357c478bd9Sstevel@tonic-gate #define ERRCORE ((char *)(-1)) 1367c478bd9Sstevel@tonic-gate #define CORESIZE (1024*ALIGN) 1377c478bd9Sstevel@tonic-gate #define MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1)) /* round down ALIGN */ 1387c478bd9Sstevel@tonic-gate #define MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN) /* overflow chk */ 1397c478bd9Sstevel@tonic-gate #define MAX_ALIGN (1 + (size_t)SSIZE_MAX) 140