17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
580ab886dSwesolows * Common Development and Distribution License (the "License").
680ab886dSwesolows * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
2180ab886dSwesolows
227c478bd9Sstevel@tonic-gate /*
23*b5016cbbSstephh * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate *
267c478bd9Sstevel@tonic-gate * alloc.c -- memory allocation wrapper functions, for eft.so FMD module
277c478bd9Sstevel@tonic-gate *
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include "alloc.h"
387c478bd9Sstevel@tonic-gate #include "out.h"
397c478bd9Sstevel@tonic-gate #include "stats.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate extern fmd_hdl_t *Hdl; /* handle from eft.c */
427c478bd9Sstevel@tonic-gate
4380ab886dSwesolows /* room to store size, possibly more to maintain alignment for long longs */
4480ab886dSwesolows #define HDRSIZ sizeof (long long)
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate static struct stats *Malloctotal;
477c478bd9Sstevel@tonic-gate static struct stats *Freetotal;
487c478bd9Sstevel@tonic-gate static struct stats *Malloccount;
497c478bd9Sstevel@tonic-gate static struct stats *Freecount;
507c478bd9Sstevel@tonic-gate
51*b5016cbbSstephh static int totalcount;
52*b5016cbbSstephh
537c478bd9Sstevel@tonic-gate void
alloc_init(void)547c478bd9Sstevel@tonic-gate alloc_init(void)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
577c478bd9Sstevel@tonic-gate Freetotal = stats_new_counter("free.total", "bytes freed", 1);
587c478bd9Sstevel@tonic-gate Malloccount = stats_new_counter("alloc.calls", "alloc calls", 1);
597c478bd9Sstevel@tonic-gate Freecount = stats_new_counter("free.calls", "free calls", 1);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate void
alloc_fini(void)637c478bd9Sstevel@tonic-gate alloc_fini(void)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate struct stats *mt, *ft, *mc, *fc;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate mt = Malloctotal;
687c478bd9Sstevel@tonic-gate ft = Freetotal;
697c478bd9Sstevel@tonic-gate mc = Malloccount;
707c478bd9Sstevel@tonic-gate fc = Freecount;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate Malloctotal = NULL;
737c478bd9Sstevel@tonic-gate Freetotal = NULL;
747c478bd9Sstevel@tonic-gate Malloccount = NULL;
757c478bd9Sstevel@tonic-gate Freecount = NULL;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate stats_delete(mt);
787c478bd9Sstevel@tonic-gate stats_delete(ft);
797c478bd9Sstevel@tonic-gate stats_delete(mc);
807c478bd9Sstevel@tonic-gate stats_delete(fc);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate * alloc_malloc -- a malloc() with checks
857c478bd9Sstevel@tonic-gate *
867c478bd9Sstevel@tonic-gate * this routine is typically called via the MALLOC() macro in alloc.h
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
897c478bd9Sstevel@tonic-gate void *
alloc_malloc(size_t nbytes,const char * fname,int line)907c478bd9Sstevel@tonic-gate alloc_malloc(size_t nbytes, const char *fname, int line)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate char *retval;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate ASSERT(nbytes > 0);
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate retval = fmd_hdl_alloc(Hdl, nbytes + HDRSIZ, FMD_SLEEP);
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /* retval can't be NULL since fmd_hdl_alloc() sleeps for memory */
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate bcopy((void *)&nbytes, (void *)retval, sizeof (nbytes));
1017c478bd9Sstevel@tonic-gate retval += HDRSIZ;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate if (Malloctotal)
1047c478bd9Sstevel@tonic-gate stats_counter_add(Malloctotal, nbytes);
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate if (Malloccount)
1077c478bd9Sstevel@tonic-gate stats_counter_bump(Malloccount);
1087c478bd9Sstevel@tonic-gate
109*b5016cbbSstephh totalcount += nbytes + HDRSIZ;
1107c478bd9Sstevel@tonic-gate return ((void *)retval);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate * alloc_realloc -- a realloc() with checks
1157c478bd9Sstevel@tonic-gate *
1167c478bd9Sstevel@tonic-gate * this routine is typically called via the REALLOC() macro in alloc.h
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate void *
alloc_realloc(void * ptr,size_t nbytes,const char * fname,int line)1197c478bd9Sstevel@tonic-gate alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate void *retval = alloc_malloc(nbytes, fname, line);
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if (ptr != NULL) {
1247c478bd9Sstevel@tonic-gate size_t osize;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize,
1277c478bd9Sstevel@tonic-gate sizeof (osize));
1287c478bd9Sstevel@tonic-gate /* now we have the new memory, copy in the old contents */
1297c478bd9Sstevel@tonic-gate bcopy(ptr, retval, (osize < nbytes) ? osize : nbytes);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* don't need the old memory anymore */
1327c478bd9Sstevel@tonic-gate alloc_free((char *)ptr, fname, line);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate return (retval);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * alloc_strdup -- a strdup() with checks
1407c478bd9Sstevel@tonic-gate *
1417c478bd9Sstevel@tonic-gate * this routine is typically called via the STRDUP() macro in alloc.h
1427c478bd9Sstevel@tonic-gate */
1437c478bd9Sstevel@tonic-gate char *
alloc_strdup(const char * ptr,const char * fname,int line)1447c478bd9Sstevel@tonic-gate alloc_strdup(const char *ptr, const char *fname, int line)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate (void) strcpy(retval, ptr);
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate return (retval);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate * alloc_free -- a free() with checks
1557c478bd9Sstevel@tonic-gate *
1567c478bd9Sstevel@tonic-gate * this routine is typically called via the FREE() macro in alloc.h
1577c478bd9Sstevel@tonic-gate */
1587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1597c478bd9Sstevel@tonic-gate void
alloc_free(void * ptr,const char * fname,int line)1607c478bd9Sstevel@tonic-gate alloc_free(void *ptr, const char *fname, int line)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate size_t osize;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate ASSERT(ptr != NULL);
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /* nothing to check in this version */
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate if (Freetotal)
1737c478bd9Sstevel@tonic-gate stats_counter_add(Freetotal, osize);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate if (Freecount)
1767c478bd9Sstevel@tonic-gate stats_counter_bump(Freecount);
177*b5016cbbSstephh totalcount -= osize + HDRSIZ;
178*b5016cbbSstephh }
179*b5016cbbSstephh
180*b5016cbbSstephh int
alloc_total()181*b5016cbbSstephh alloc_total()
182*b5016cbbSstephh {
183*b5016cbbSstephh return (totalcount);
184*b5016cbbSstephh }
185*b5016cbbSstephh
186*b5016cbbSstephh /*
187*b5016cbbSstephh * variants that don't maintain size in header - saves space
188*b5016cbbSstephh */
189*b5016cbbSstephh void *
alloc_xmalloc(size_t nbytes)190*b5016cbbSstephh alloc_xmalloc(size_t nbytes)
191*b5016cbbSstephh {
192*b5016cbbSstephh char *retval;
193*b5016cbbSstephh
194*b5016cbbSstephh ASSERT(nbytes > 0);
195*b5016cbbSstephh retval = fmd_hdl_alloc(Hdl, nbytes, FMD_SLEEP);
196*b5016cbbSstephh if (Malloctotal)
197*b5016cbbSstephh stats_counter_add(Malloctotal, nbytes);
198*b5016cbbSstephh if (Malloccount)
199*b5016cbbSstephh stats_counter_bump(Malloccount);
200*b5016cbbSstephh totalcount += nbytes;
201*b5016cbbSstephh return ((void *)retval);
202*b5016cbbSstephh }
203*b5016cbbSstephh
204*b5016cbbSstephh void
alloc_xfree(void * ptr,size_t size)205*b5016cbbSstephh alloc_xfree(void *ptr, size_t size)
206*b5016cbbSstephh {
207*b5016cbbSstephh ASSERT(ptr != NULL);
208*b5016cbbSstephh
209*b5016cbbSstephh fmd_hdl_free(Hdl, (char *)ptr, size);
210*b5016cbbSstephh if (Freetotal)
211*b5016cbbSstephh stats_counter_add(Freetotal, size);
212*b5016cbbSstephh if (Freecount)
213*b5016cbbSstephh stats_counter_bump(Freecount);
214*b5016cbbSstephh totalcount -= size;
2157c478bd9Sstevel@tonic-gate }
216