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]
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"
35 #include <fm/fmd_api.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
;
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);
65 struct stats
*mt
, *ft
, *mc
, *fc
;
84 * alloc_malloc -- a malloc() with checks
86 * this routine is typically called via the MALLOC() macro in alloc.h
90 alloc_malloc(size_t nbytes
, const char *fname
, int line
)
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
));
104 stats_counter_add(Malloctotal
, nbytes
);
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
119 alloc_realloc(void *ptr
, size_t nbytes
, const char *fname
, int line
)
121 void *retval
= alloc_malloc(nbytes
, fname
, line
);
126 bcopy((void *)((char *)ptr
- HDRSIZ
), (void *)&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
);
139 * alloc_strdup -- a strdup() with checks
141 * this routine is typically called via the STRDUP() macro in alloc.h
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
);
154 * alloc_free -- a free() with checks
156 * this routine is typically called via the FREE() macro in alloc.h
160 alloc_free(void *ptr
, const char *fname
, int line
)
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
);
173 stats_counter_add(Freetotal
, osize
);
176 stats_counter_bump(Freecount
);
177 totalcount
-= osize
+ HDRSIZ
;
187 * variants that don't maintain size in header - saves space
190 alloc_xmalloc(size_t nbytes
)
195 retval
= fmd_hdl_alloc(Hdl
, nbytes
, FMD_SLEEP
);
197 stats_counter_add(Malloctotal
, nbytes
);
199 stats_counter_bump(Malloccount
);
200 totalcount
+= nbytes
;
201 return ((void *)retval
);
205 alloc_xfree(void *ptr
, size_t size
)
209 fmd_hdl_free(Hdl
, (char *)ptr
, size
);
211 stats_counter_add(Freetotal
, size
);
213 stats_counter_bump(Freecount
);