xref: /titanic_50/usr/src/cmd/fm/modules/common/eversholt/alloc.c (revision b3700b074e637f8c6991b70754c88a2cfffb246b)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * alloc.c -- memory allocation wrapper functions, for eft.so FMD module
27  *
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <fm/fmd_api.h>
36 
37 #include "alloc.h"
38 #include "out.h"
39 #include "stats.h"
40 
41 extern fmd_hdl_t *Hdl;		/* handle from eft.c */
42 
43 /* room to store size, possibly more to maintain alignment for long longs */
44 #define	HDRSIZ	sizeof (long long)
45 
46 static struct stats *Malloctotal;
47 static struct stats *Freetotal;
48 static struct stats *Malloccount;
49 static struct stats *Freecount;
50 
51 static int totalcount;
52 
53 void
54 alloc_init(void)
55 {
56 	Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
57 	Freetotal = stats_new_counter("free.total", "bytes freed", 1);
58 	Malloccount = stats_new_counter("alloc.calls", "alloc calls", 1);
59 	Freecount = stats_new_counter("free.calls", "free calls", 1);
60 }
61 
62 void
63 alloc_fini(void)
64 {
65 	struct stats *mt, *ft, *mc, *fc;
66 
67 	mt = Malloctotal;
68 	ft = Freetotal;
69 	mc = Malloccount;
70 	fc = Freecount;
71 
72 	Malloctotal = NULL;
73 	Freetotal = NULL;
74 	Malloccount = NULL;
75 	Freecount = NULL;
76 
77 	stats_delete(mt);
78 	stats_delete(ft);
79 	stats_delete(mc);
80 	stats_delete(fc);
81 }
82 
83 /*
84  * alloc_malloc -- a malloc() with checks
85  *
86  * this routine is typically called via the MALLOC() macro in alloc.h
87  */
88 /*ARGSUSED*/
89 void *
90 alloc_malloc(size_t nbytes, const char *fname, int line)
91 {
92 	char *retval;
93 
94 	ASSERT(nbytes > 0);
95 
96 	retval = fmd_hdl_alloc(Hdl, nbytes + HDRSIZ, FMD_SLEEP);
97 
98 	/* retval can't be NULL since fmd_hdl_alloc() sleeps for memory */
99 
100 	bcopy((void *)&nbytes, (void *)retval, sizeof (nbytes));
101 	retval += HDRSIZ;
102 
103 	if (Malloctotal)
104 		stats_counter_add(Malloctotal, nbytes);
105 
106 	if (Malloccount)
107 		stats_counter_bump(Malloccount);
108 
109 	totalcount += nbytes + HDRSIZ;
110 	return ((void *)retval);
111 }
112 
113 /*
114  * alloc_realloc -- a realloc() with checks
115  *
116  * this routine is typically called via the REALLOC() macro in alloc.h
117  */
118 void *
119 alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
120 {
121 	void *retval = alloc_malloc(nbytes, fname, line);
122 
123 	if (ptr != NULL) {
124 		size_t osize;
125 
126 		bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize,
127 		    sizeof (osize));
128 		/* now we have the new memory, copy in the old contents */
129 		bcopy(ptr, retval, (osize < nbytes) ? osize : nbytes);
130 
131 		/* don't need the old memory anymore */
132 		alloc_free((char *)ptr, fname, line);
133 	}
134 
135 	return (retval);
136 }
137 
138 /*
139  * alloc_strdup -- a strdup() with checks
140  *
141  * this routine is typically called via the STRDUP() macro in alloc.h
142  */
143 char *
144 alloc_strdup(const char *ptr, const char *fname, int line)
145 {
146 	char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
147 
148 	(void) strcpy(retval, ptr);
149 
150 	return (retval);
151 }
152 
153 /*
154  * alloc_free -- a free() with checks
155  *
156  * this routine is typically called via the FREE() macro in alloc.h
157  */
158 /*ARGSUSED*/
159 void
160 alloc_free(void *ptr, const char *fname, int line)
161 {
162 	size_t osize;
163 
164 	ASSERT(ptr != NULL);
165 
166 	bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
167 
168 	/* nothing to check in this version */
169 
170 	fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
171 
172 	if (Freetotal)
173 		stats_counter_add(Freetotal, osize);
174 
175 	if (Freecount)
176 		stats_counter_bump(Freecount);
177 	totalcount -= osize + HDRSIZ;
178 }
179 
180 int
181 alloc_total()
182 {
183 	return (totalcount);
184 }
185 
186 /*
187  * variants that don't maintain size in header - saves space
188  */
189 void *
190 alloc_xmalloc(size_t nbytes)
191 {
192 	char *retval;
193 
194 	ASSERT(nbytes > 0);
195 	retval = fmd_hdl_alloc(Hdl, nbytes, FMD_SLEEP);
196 	if (Malloctotal)
197 		stats_counter_add(Malloctotal, nbytes);
198 	if (Malloccount)
199 		stats_counter_bump(Malloccount);
200 	totalcount += nbytes;
201 	return ((void *)retval);
202 }
203 
204 void
205 alloc_xfree(void *ptr, size_t size)
206 {
207 	ASSERT(ptr != NULL);
208 
209 	fmd_hdl_free(Hdl, (char *)ptr, size);
210 	if (Freetotal)
211 		stats_counter_add(Freetotal, size);
212 	if (Freecount)
213 		stats_counter_bump(Freecount);
214 	totalcount -= size;
215 }
216