dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / modules / common / eversholt / alloc.c
blob50b0286a4b1816fb0b9c174a4968e413b1b695af
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * alloc.c -- memory allocation wrapper functions, for eft.so FMD module
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <fm/fmd_api.h>
37 #include "alloc.h"
38 #include "out.h"
39 #include "stats.h"
41 extern fmd_hdl_t *Hdl; /* handle from eft.c */
43 /* room to store size, possibly more to maintain alignment for long longs */
44 #define HDRSIZ sizeof (long long)
46 static struct stats *Malloctotal;
47 static struct stats *Freetotal;
48 static struct stats *Malloccount;
49 static struct stats *Freecount;
51 static int totalcount;
53 void
54 alloc_init(void)
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);
62 void
63 alloc_fini(void)
65 struct stats *mt, *ft, *mc, *fc;
67 mt = Malloctotal;
68 ft = Freetotal;
69 mc = Malloccount;
70 fc = Freecount;
72 Malloctotal = NULL;
73 Freetotal = NULL;
74 Malloccount = NULL;
75 Freecount = NULL;
77 stats_delete(mt);
78 stats_delete(ft);
79 stats_delete(mc);
80 stats_delete(fc);
84 * alloc_malloc -- a malloc() with checks
86 * this routine is typically called via the MALLOC() macro in alloc.h
88 /*ARGSUSED*/
89 void *
90 alloc_malloc(size_t nbytes, const char *fname, int line)
92 char *retval;
94 ASSERT(nbytes > 0);
96 retval = fmd_hdl_alloc(Hdl, nbytes + HDRSIZ, FMD_SLEEP);
98 /* retval can't be NULL since fmd_hdl_alloc() sleeps for memory */
100 bcopy((void *)&nbytes, (void *)retval, sizeof (nbytes));
101 retval += HDRSIZ;
103 if (Malloctotal)
104 stats_counter_add(Malloctotal, nbytes);
106 if (Malloccount)
107 stats_counter_bump(Malloccount);
109 totalcount += nbytes + HDRSIZ;
110 return ((void *)retval);
114 * alloc_realloc -- a realloc() with checks
116 * this routine is typically called via the REALLOC() macro in alloc.h
118 void *
119 alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
121 void *retval = alloc_malloc(nbytes, fname, line);
123 if (ptr != NULL) {
124 size_t osize;
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);
131 /* don't need the old memory anymore */
132 alloc_free((char *)ptr, fname, line);
135 return (retval);
139 * alloc_strdup -- a strdup() with checks
141 * this routine is typically called via the STRDUP() macro in alloc.h
143 char *
144 alloc_strdup(const char *ptr, const char *fname, int line)
146 char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
148 (void) strcpy(retval, ptr);
150 return (retval);
154 * alloc_free -- a free() with checks
156 * this routine is typically called via the FREE() macro in alloc.h
158 /*ARGSUSED*/
159 void
160 alloc_free(void *ptr, const char *fname, int line)
162 size_t osize;
164 ASSERT(ptr != NULL);
166 bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
168 /* nothing to check in this version */
170 fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
172 if (Freetotal)
173 stats_counter_add(Freetotal, osize);
175 if (Freecount)
176 stats_counter_bump(Freecount);
177 totalcount -= osize + HDRSIZ;
181 alloc_total()
183 return (totalcount);
187 * variants that don't maintain size in header - saves space
189 void *
190 alloc_xmalloc(size_t nbytes)
192 char *retval;
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);
204 void
205 alloc_xfree(void *ptr, size_t size)
207 ASSERT(ptr != NULL);
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;