1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * 25 * alloc.c -- memory allocation wrapper functions, replacable in more 26 * constrained environments, such as within a DE. 27 */ 28 29 #include <stdlib.h> 30 #include <string.h> 31 32 #include "alloc.h" 33 #include "out.h" 34 #include "stats.h" 35 36 static struct stats *Malloctotal; 37 static struct stats *Malloccount; 38 39 void 40 alloc_init(void) 41 { 42 Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1); 43 Malloccount = stats_new_counter("alloc.calls", "total calls", 1); 44 } 45 46 void 47 alloc_fini(void) 48 { 49 struct stats *mt, *mc; 50 51 mt = Malloctotal; 52 mc = Malloccount; 53 54 Malloctotal = NULL; 55 Malloccount = NULL; 56 57 stats_delete(mt); 58 stats_delete(mc); 59 } 60 61 /* 62 * alloc_malloc -- a malloc() with checks 63 * 64 * this routine is typically called via the MALLOC() macro in alloc.h 65 */ 66 67 void * 68 alloc_malloc(size_t nbytes, const char *fname, int line) 69 { 70 void *retval = malloc(nbytes); 71 72 if (retval == NULL) 73 outfl(O_DIE, fname, line, "malloc: out of memory"); 74 75 if (Malloctotal) 76 stats_counter_add(Malloctotal, nbytes); 77 78 if (Malloccount) 79 stats_counter_bump(Malloccount); 80 81 return (retval); 82 } 83 84 /* 85 * alloc_realloc -- a realloc() with checks 86 * 87 * this routine is typically called via the REALLOC() macro in alloc.h 88 */ 89 void * 90 alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line) 91 { 92 void *retval = realloc(ptr, nbytes); 93 94 if (retval == NULL) 95 out(O_DIE, fname, line, "realloc: out of memory"); 96 97 return (retval); 98 } 99 100 /* 101 * alloc_strdup -- a strdup() with checks 102 * 103 * this routine is typically called via the STRDUP() macro in alloc.h 104 */ 105 char * 106 alloc_strdup(const char *ptr, const char *fname, int line) 107 { 108 char *retval = strdup(ptr); 109 110 if (retval == NULL) 111 outfl(O_DIE, fname, line, "strdup: out of memory"); 112 113 return (retval); 114 } 115 116 /* 117 * alloc_free -- a free() with checks 118 * 119 * this routine is typically called via the FREE() macro in alloc.h 120 */ 121 /*ARGSUSED1*/ 122 void 123 alloc_free(void *ptr, const char *fname, int line) 124 { 125 /* nothing to check in this version */ 126 free(ptr); 127 } 128 129 /* 130 * variants that don't maintain size in header - saves space 131 */ 132 void * 133 alloc_xmalloc(size_t nbytes) 134 { 135 void *retval; 136 137 retval = malloc(nbytes); 138 if (retval == NULL) 139 out(O_DIE, "malloc: out of memory"); 140 if (Malloctotal) 141 stats_counter_add(Malloctotal, nbytes); 142 if (Malloccount) 143 stats_counter_bump(Malloccount); 144 return (retval); 145 } 146 147 /*ARGSUSED*/ 148 void 149 alloc_xfree(void *ptr, size_t size) 150 { 151 free(ptr); 152 } 153