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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "alloc.h"
35 #include "out.h"
36 #include "stats.h"
37 
38 static struct stats *Malloctotal;
39 static struct stats *Malloccount;
40 
41 void
alloc_init(void)42 alloc_init(void)
43 {
44 	Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
45 	Malloccount = stats_new_counter("alloc.calls", "total calls", 1);
46 }
47 
48 void
alloc_fini(void)49 alloc_fini(void)
50 {
51 	struct stats *mt, *mc;
52 
53 	mt = Malloctotal;
54 	mc = Malloccount;
55 
56 	Malloctotal = NULL;
57 	Malloccount = NULL;
58 
59 	stats_delete(mt);
60 	stats_delete(mc);
61 }
62 
63 /*
64  * alloc_malloc -- a malloc() with checks
65  *
66  * this routine is typically called via the MALLOC() macro in alloc.h
67  */
68 
69 void *
alloc_malloc(size_t nbytes,const char * fname,int line)70 alloc_malloc(size_t nbytes, const char *fname, int line)
71 {
72 	void *retval = malloc(nbytes);
73 
74 	if (retval == NULL)
75 		outfl(O_DIE, fname, line, "malloc: out of memory");
76 
77 	if (Malloctotal)
78 		stats_counter_add(Malloctotal, nbytes);
79 
80 	if (Malloccount)
81 		stats_counter_bump(Malloccount);
82 
83 	return (retval);
84 }
85 
86 /*
87  * alloc_realloc -- a realloc() with checks
88  *
89  * this routine is typically called via the REALLOC() macro in alloc.h
90  */
91 void *
alloc_realloc(void * ptr,size_t nbytes,const char * fname,int line)92 alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
93 {
94 	void *retval = realloc(ptr, nbytes);
95 
96 	if (retval == NULL)
97 		out(O_DIE, fname, line, "realloc: out of memory");
98 
99 	return (retval);
100 }
101 
102 /*
103  * alloc_strdup -- a strdup() with checks
104  *
105  * this routine is typically called via the STRDUP() macro in alloc.h
106  */
107 char *
alloc_strdup(const char * ptr,const char * fname,int line)108 alloc_strdup(const char *ptr, const char *fname, int line)
109 {
110 	char *retval = strdup(ptr);
111 
112 	if (retval == NULL)
113 		outfl(O_DIE, fname, line, "strdup: out of memory");
114 
115 	return (retval);
116 }
117 
118 /*
119  * alloc_free -- a free() with checks
120  *
121  * this routine is typically called via the FREE() macro in alloc.h
122  */
123 /*ARGSUSED1*/
124 void
alloc_free(void * ptr,const char * fname,int line)125 alloc_free(void *ptr, const char *fname, int line)
126 {
127 	/* nothing to check in this version */
128 	free(ptr);
129 }
130 
131 /*
132  * variants that don't maintain size in header - saves space
133  */
134 void *
alloc_xmalloc(size_t nbytes)135 alloc_xmalloc(size_t nbytes)
136 {
137 	void *retval;
138 
139 	retval = malloc(nbytes);
140 	if (retval == NULL)
141 		out(O_DIE, "malloc: out of memory");
142 	if (Malloctotal)
143 		stats_counter_add(Malloctotal, nbytes);
144 	if (Malloccount)
145 		stats_counter_bump(Malloccount);
146 	return (retval);
147 }
148 
149 /*ARGSUSED*/
150 void
alloc_xfree(void * ptr,size_t size)151 alloc_xfree(void *ptr, size_t size)
152 {
153 	free(ptr);
154 }
155