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 /* 24*0Sstevel@tonic-gate * Copyright (c) 1992, 1993, 2000 by Sun Microsystems, Inc. 25*0Sstevel@tonic-gate * All rights reserved. 26*0Sstevel@tonic-gate */ 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #include <meta.h> 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate /* 33*0Sstevel@tonic-gate * free 34*0Sstevel@tonic-gate */ 35*0Sstevel@tonic-gate #ifdef _DEBUG_MALLOC_INC 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate void 38*0Sstevel@tonic-gate _Free( 39*0Sstevel@tonic-gate char *file, 40*0Sstevel@tonic-gate int line, 41*0Sstevel@tonic-gate void *p 42*0Sstevel@tonic-gate ) 43*0Sstevel@tonic-gate { 44*0Sstevel@tonic-gate debug_free(file, line, p); 45*0Sstevel@tonic-gate } 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate #else /* ! _DEBUG_MALLOC_INC */ 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate void 50*0Sstevel@tonic-gate Free( 51*0Sstevel@tonic-gate void *p 52*0Sstevel@tonic-gate ) 53*0Sstevel@tonic-gate { 54*0Sstevel@tonic-gate free(p); 55*0Sstevel@tonic-gate } 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate #endif /* ! _DEBUG_MALLOC_INC */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate /* 60*0Sstevel@tonic-gate * malloc 61*0Sstevel@tonic-gate */ 62*0Sstevel@tonic-gate #ifdef _DEBUG_MALLOC_INC 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate void * 65*0Sstevel@tonic-gate _Malloc( 66*0Sstevel@tonic-gate char *file, 67*0Sstevel@tonic-gate int line, 68*0Sstevel@tonic-gate size_t s 69*0Sstevel@tonic-gate ) 70*0Sstevel@tonic-gate { 71*0Sstevel@tonic-gate void *mem; 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate mem = debug_malloc(file, line, s); 74*0Sstevel@tonic-gate if (mem == NULL) { 75*0Sstevel@tonic-gate md_perror(""); 76*0Sstevel@tonic-gate md_exit(NULL, 1); 77*0Sstevel@tonic-gate } 78*0Sstevel@tonic-gate return (mem); 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate #else /* ! _DEBUG_MALLOC_INC */ 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate void * 84*0Sstevel@tonic-gate Malloc( 85*0Sstevel@tonic-gate size_t s 86*0Sstevel@tonic-gate ) 87*0Sstevel@tonic-gate { 88*0Sstevel@tonic-gate void *mem; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate if ((mem = malloc(s)) == NULL) { 91*0Sstevel@tonic-gate md_perror(""); 92*0Sstevel@tonic-gate md_exit(NULL, 1); 93*0Sstevel@tonic-gate } 94*0Sstevel@tonic-gate return (mem); 95*0Sstevel@tonic-gate } 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate #endif /* ! _DEBUG_MALLOC_INC */ 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate /* 100*0Sstevel@tonic-gate * zalloc 101*0Sstevel@tonic-gate */ 102*0Sstevel@tonic-gate #ifdef _DEBUG_MALLOC_INC 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate void * 105*0Sstevel@tonic-gate _Zalloc( 106*0Sstevel@tonic-gate char *file, 107*0Sstevel@tonic-gate int line, 108*0Sstevel@tonic-gate size_t s 109*0Sstevel@tonic-gate ) 110*0Sstevel@tonic-gate { 111*0Sstevel@tonic-gate return (memset(_Malloc(file, line, s), 0, s)); 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate #else /* ! _DEBUG_MALLOC_INC */ 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate void * 117*0Sstevel@tonic-gate Zalloc( 118*0Sstevel@tonic-gate size_t s 119*0Sstevel@tonic-gate ) 120*0Sstevel@tonic-gate { 121*0Sstevel@tonic-gate return (memset(Malloc(s), 0, s)); 122*0Sstevel@tonic-gate } 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate #endif /* ! _DEBUG_MALLOC_INC */ 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate /* 127*0Sstevel@tonic-gate * realloc 128*0Sstevel@tonic-gate */ 129*0Sstevel@tonic-gate #ifdef _DEBUG_MALLOC_INC 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate void * 132*0Sstevel@tonic-gate _Realloc( 133*0Sstevel@tonic-gate char *file, 134*0Sstevel@tonic-gate int line, 135*0Sstevel@tonic-gate void *p, 136*0Sstevel@tonic-gate size_t s 137*0Sstevel@tonic-gate ) 138*0Sstevel@tonic-gate { 139*0Sstevel@tonic-gate if (p == NULL) 140*0Sstevel@tonic-gate p = debug_malloc(file, line, s); 141*0Sstevel@tonic-gate else 142*0Sstevel@tonic-gate p = debug_realloc(file, line, p, s); 143*0Sstevel@tonic-gate if (p == NULL) { 144*0Sstevel@tonic-gate md_perror(""); 145*0Sstevel@tonic-gate md_exit(NULL, 1); 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate return (p); 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate #else /* ! _DEBUG_MALLOC_INC */ 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate void * 153*0Sstevel@tonic-gate Realloc( 154*0Sstevel@tonic-gate void *p, 155*0Sstevel@tonic-gate size_t s 156*0Sstevel@tonic-gate ) 157*0Sstevel@tonic-gate { 158*0Sstevel@tonic-gate if ((p = realloc(p, s)) == NULL) { 159*0Sstevel@tonic-gate md_perror(""); 160*0Sstevel@tonic-gate md_exit(NULL, 1); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate return (p); 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate #endif /* ! _DEBUG_MALLOC_INC */ 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate /* 168*0Sstevel@tonic-gate * calloc 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate #ifdef _DEBUG_MALLOC_INC 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate void * 173*0Sstevel@tonic-gate _Calloc( 174*0Sstevel@tonic-gate char *file, 175*0Sstevel@tonic-gate int line, 176*0Sstevel@tonic-gate size_t n, 177*0Sstevel@tonic-gate size_t s 178*0Sstevel@tonic-gate ) 179*0Sstevel@tonic-gate { 180*0Sstevel@tonic-gate unsigned long total; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate if (n == 0 || s == 0) { 183*0Sstevel@tonic-gate total = 0; 184*0Sstevel@tonic-gate } else { 185*0Sstevel@tonic-gate total = (unsigned long)n * s; 186*0Sstevel@tonic-gate /* check for overflow */ 187*0Sstevel@tonic-gate if (total / n != s) 188*0Sstevel@tonic-gate return (NULL); 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate return (_Zalloc(file, line, total)); 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate #else /* ! _DEBUG_MALLOC_INC */ 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate void * 196*0Sstevel@tonic-gate Calloc( 197*0Sstevel@tonic-gate size_t n, 198*0Sstevel@tonic-gate size_t s 199*0Sstevel@tonic-gate ) 200*0Sstevel@tonic-gate { 201*0Sstevel@tonic-gate unsigned long total; 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate if (n == 0 || s == 0) { 204*0Sstevel@tonic-gate total = 0; 205*0Sstevel@tonic-gate } else { 206*0Sstevel@tonic-gate total = (unsigned long)n * s; 207*0Sstevel@tonic-gate /* check for overflow */ 208*0Sstevel@tonic-gate if (total / n != s) 209*0Sstevel@tonic-gate return (NULL); 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate return (Zalloc(total)); 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate #endif /* ! _DEBUG_MALLOC_INC */ 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate /* 217*0Sstevel@tonic-gate * strdup 218*0Sstevel@tonic-gate */ 219*0Sstevel@tonic-gate #ifdef _DEBUG_MALLOC_INC 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate char * 222*0Sstevel@tonic-gate _Strdup( 223*0Sstevel@tonic-gate char *file, 224*0Sstevel@tonic-gate int line, 225*0Sstevel@tonic-gate char *p 226*0Sstevel@tonic-gate ) 227*0Sstevel@tonic-gate { 228*0Sstevel@tonic-gate p = DBstrdup(file, line, p); 229*0Sstevel@tonic-gate if (p == NULL) { 230*0Sstevel@tonic-gate md_perror(""); 231*0Sstevel@tonic-gate md_exit(NULL, 1); 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate return (p); 234*0Sstevel@tonic-gate } 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate #else /* ! _DEBUG_MALLOC_INC */ 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate char * 239*0Sstevel@tonic-gate Strdup( 240*0Sstevel@tonic-gate char *p 241*0Sstevel@tonic-gate ) 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate if ((p = strdup(p)) == NULL) { 244*0Sstevel@tonic-gate md_perror(""); 245*0Sstevel@tonic-gate md_exit(NULL, 1); 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate return (p); 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate #endif /* ! _DEBUG_MALLOC_INC */ 251