xref: /illumos-gate/usr/src/cmd/fm/modules/common/eversholt/alloc.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * alloc.c -- memory allocation wrapper functions, for eft.so FMD module
27*7c478bd9Sstevel@tonic-gate  *
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #include <strings.h>
35*7c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "alloc.h"
38*7c478bd9Sstevel@tonic-gate #include "out.h"
39*7c478bd9Sstevel@tonic-gate #include "stats.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate extern fmd_hdl_t *Hdl;		/* handle from eft.c */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /* room to store size */
44*7c478bd9Sstevel@tonic-gate #define	HDRSIZ	sizeof (size_t)
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static struct stats *Malloctotal;
47*7c478bd9Sstevel@tonic-gate static struct stats *Freetotal;
48*7c478bd9Sstevel@tonic-gate static struct stats *Malloccount;
49*7c478bd9Sstevel@tonic-gate static struct stats *Freecount;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate void
52*7c478bd9Sstevel@tonic-gate alloc_init(void)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
55*7c478bd9Sstevel@tonic-gate 	Freetotal = stats_new_counter("free.total", "bytes freed", 1);
56*7c478bd9Sstevel@tonic-gate 	Malloccount = stats_new_counter("alloc.calls", "alloc calls", 1);
57*7c478bd9Sstevel@tonic-gate 	Freecount = stats_new_counter("free.calls", "free calls", 1);
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate void
61*7c478bd9Sstevel@tonic-gate alloc_fini(void)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	struct stats *mt, *ft, *mc, *fc;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	mt = Malloctotal;
66*7c478bd9Sstevel@tonic-gate 	ft = Freetotal;
67*7c478bd9Sstevel@tonic-gate 	mc = Malloccount;
68*7c478bd9Sstevel@tonic-gate 	fc = Freecount;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	Malloctotal = NULL;
71*7c478bd9Sstevel@tonic-gate 	Freetotal = NULL;
72*7c478bd9Sstevel@tonic-gate 	Malloccount = NULL;
73*7c478bd9Sstevel@tonic-gate 	Freecount = NULL;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	stats_delete(mt);
76*7c478bd9Sstevel@tonic-gate 	stats_delete(ft);
77*7c478bd9Sstevel@tonic-gate 	stats_delete(mc);
78*7c478bd9Sstevel@tonic-gate 	stats_delete(fc);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate  * alloc_malloc -- a malloc() with checks
83*7c478bd9Sstevel@tonic-gate  *
84*7c478bd9Sstevel@tonic-gate  * this routine is typically called via the MALLOC() macro in alloc.h
85*7c478bd9Sstevel@tonic-gate  */
86*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
87*7c478bd9Sstevel@tonic-gate void *
88*7c478bd9Sstevel@tonic-gate alloc_malloc(size_t nbytes, const char *fname, int line)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	char *retval;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	ASSERT(nbytes > 0);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	retval = fmd_hdl_alloc(Hdl, nbytes + HDRSIZ, FMD_SLEEP);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	/* retval can't be NULL since fmd_hdl_alloc() sleeps for memory */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	bcopy((void *)&nbytes, (void *)retval, sizeof (nbytes));
99*7c478bd9Sstevel@tonic-gate 	retval += HDRSIZ;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if (Malloctotal)
102*7c478bd9Sstevel@tonic-gate 		stats_counter_add(Malloctotal, nbytes);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (Malloccount)
105*7c478bd9Sstevel@tonic-gate 		stats_counter_bump(Malloccount);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	return ((void *)retval);
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate /*
111*7c478bd9Sstevel@tonic-gate  * alloc_realloc -- a realloc() with checks
112*7c478bd9Sstevel@tonic-gate  *
113*7c478bd9Sstevel@tonic-gate  * this routine is typically called via the REALLOC() macro in alloc.h
114*7c478bd9Sstevel@tonic-gate  */
115*7c478bd9Sstevel@tonic-gate void *
116*7c478bd9Sstevel@tonic-gate alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	void *retval = alloc_malloc(nbytes, fname, line);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL) {
121*7c478bd9Sstevel@tonic-gate 		size_t osize;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize,
124*7c478bd9Sstevel@tonic-gate 		    sizeof (osize));
125*7c478bd9Sstevel@tonic-gate 		/* now we have the new memory, copy in the old contents */
126*7c478bd9Sstevel@tonic-gate 		bcopy(ptr, retval, (osize < nbytes) ? osize : nbytes);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 		/* don't need the old memory anymore */
129*7c478bd9Sstevel@tonic-gate 		alloc_free((char *)ptr, fname, line);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	return (retval);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * alloc_strdup -- a strdup() with checks
137*7c478bd9Sstevel@tonic-gate  *
138*7c478bd9Sstevel@tonic-gate  * this routine is typically called via the STRDUP() macro in alloc.h
139*7c478bd9Sstevel@tonic-gate  */
140*7c478bd9Sstevel@tonic-gate char *
141*7c478bd9Sstevel@tonic-gate alloc_strdup(const char *ptr, const char *fname, int line)
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	(void) strcpy(retval, ptr);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	return (retval);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate /*
151*7c478bd9Sstevel@tonic-gate  * alloc_free -- a free() with checks
152*7c478bd9Sstevel@tonic-gate  *
153*7c478bd9Sstevel@tonic-gate  * this routine is typically called via the FREE() macro in alloc.h
154*7c478bd9Sstevel@tonic-gate  */
155*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
156*7c478bd9Sstevel@tonic-gate void
157*7c478bd9Sstevel@tonic-gate alloc_free(void *ptr, const char *fname, int line)
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	size_t osize;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	ASSERT(ptr != NULL);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/* nothing to check in this version */
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if (Freetotal)
170*7c478bd9Sstevel@tonic-gate 		stats_counter_add(Freetotal, osize);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (Freecount)
173*7c478bd9Sstevel@tonic-gate 		stats_counter_bump(Freecount);
174*7c478bd9Sstevel@tonic-gate }
175