xref: /titanic_52/usr/src/cmd/fm/modules/common/eversholt/alloc.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 2006 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 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
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 *
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 	return ((void *)retval);
108 }
109 
110 /*
111  * alloc_realloc -- a realloc() with checks
112  *
113  * this routine is typically called via the REALLOC() macro in alloc.h
114  */
115 void *
116 alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
117 {
118 	void *retval = alloc_malloc(nbytes, fname, line);
119 
120 	if (ptr != NULL) {
121 		size_t osize;
122 
123 		bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize,
124 		    sizeof (osize));
125 		/* now we have the new memory, copy in the old contents */
126 		bcopy(ptr, retval, (osize < nbytes) ? osize : nbytes);
127 
128 		/* don't need the old memory anymore */
129 		alloc_free((char *)ptr, fname, line);
130 	}
131 
132 	return (retval);
133 }
134 
135 /*
136  * alloc_strdup -- a strdup() with checks
137  *
138  * this routine is typically called via the STRDUP() macro in alloc.h
139  */
140 char *
141 alloc_strdup(const char *ptr, const char *fname, int line)
142 {
143 	char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
144 
145 	(void) strcpy(retval, ptr);
146 
147 	return (retval);
148 }
149 
150 /*
151  * alloc_free -- a free() with checks
152  *
153  * this routine is typically called via the FREE() macro in alloc.h
154  */
155 /*ARGSUSED*/
156 void
157 alloc_free(void *ptr, const char *fname, int line)
158 {
159 	size_t osize;
160 
161 	ASSERT(ptr != NULL);
162 
163 	bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
164 
165 	/* nothing to check in this version */
166 
167 	fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
168 
169 	if (Freetotal)
170 		stats_counter_add(Freetotal, osize);
171 
172 	if (Freecount)
173 		stats_counter_bump(Freecount);
174 }
175