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