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 #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "alloc.h"
367c478bd9Sstevel@tonic-gate #include "out.h"
377c478bd9Sstevel@tonic-gate #include "stats.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern fmd_hdl_t *Hdl;		/* handle from eft.c */
407c478bd9Sstevel@tonic-gate 
4180ab886dSwesolows /* room to store size, possibly more to maintain alignment for long longs */
4280ab886dSwesolows #define	HDRSIZ	sizeof (long long)
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static struct stats *Malloctotal;
457c478bd9Sstevel@tonic-gate static struct stats *Freetotal;
467c478bd9Sstevel@tonic-gate static struct stats *Malloccount;
477c478bd9Sstevel@tonic-gate static struct stats *Freecount;
487c478bd9Sstevel@tonic-gate 
49*b5016cbbSstephh static int totalcount;
50*b5016cbbSstephh 
517c478bd9Sstevel@tonic-gate void
alloc_init(void)527c478bd9Sstevel@tonic-gate alloc_init(void)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
557c478bd9Sstevel@tonic-gate 	Freetotal = stats_new_counter("free.total", "bytes freed", 1);
567c478bd9Sstevel@tonic-gate 	Malloccount = stats_new_counter("alloc.calls", "alloc calls", 1);
577c478bd9Sstevel@tonic-gate 	Freecount = stats_new_counter("free.calls", "free calls", 1);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
alloc_fini(void)617c478bd9Sstevel@tonic-gate alloc_fini(void)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	struct stats *mt, *ft, *mc, *fc;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	mt = Malloctotal;
667c478bd9Sstevel@tonic-gate 	ft = Freetotal;
677c478bd9Sstevel@tonic-gate 	mc = Malloccount;
687c478bd9Sstevel@tonic-gate 	fc = Freecount;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	Malloctotal = NULL;
717c478bd9Sstevel@tonic-gate 	Freetotal = NULL;
727c478bd9Sstevel@tonic-gate 	Malloccount = NULL;
737c478bd9Sstevel@tonic-gate 	Freecount = NULL;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	stats_delete(mt);
767c478bd9Sstevel@tonic-gate 	stats_delete(ft);
777c478bd9Sstevel@tonic-gate 	stats_delete(mc);
787c478bd9Sstevel@tonic-gate 	stats_delete(fc);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * alloc_malloc -- a malloc() with checks
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * this routine is typically called via the MALLOC() macro in alloc.h
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
877c478bd9Sstevel@tonic-gate void *
alloc_malloc(size_t nbytes,const char * fname,int line)887c478bd9Sstevel@tonic-gate alloc_malloc(size_t nbytes, const char *fname, int line)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	char *retval;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	ASSERT(nbytes > 0);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	retval = fmd_hdl_alloc(Hdl, nbytes + HDRSIZ, FMD_SLEEP);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/* retval can't be NULL since fmd_hdl_alloc() sleeps for memory */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	bcopy((void *)&nbytes, (void *)retval, sizeof (nbytes));
997c478bd9Sstevel@tonic-gate 	retval += HDRSIZ;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (Malloctotal)
1027c478bd9Sstevel@tonic-gate 		stats_counter_add(Malloctotal, nbytes);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (Malloccount)
1057c478bd9Sstevel@tonic-gate 		stats_counter_bump(Malloccount);
1067c478bd9Sstevel@tonic-gate 
107*b5016cbbSstephh 	totalcount += nbytes + HDRSIZ;
1087c478bd9Sstevel@tonic-gate 	return ((void *)retval);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * alloc_realloc -- a realloc() with checks
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * this routine is typically called via the REALLOC() macro in alloc.h
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate void *
alloc_realloc(void * ptr,size_t nbytes,const char * fname,int line)1177c478bd9Sstevel@tonic-gate alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	void *retval = alloc_malloc(nbytes, fname, line);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (ptr != NULL) {
1227c478bd9Sstevel@tonic-gate 		size_t osize;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize,
1257c478bd9Sstevel@tonic-gate 		    sizeof (osize));
1267c478bd9Sstevel@tonic-gate 		/* now we have the new memory, copy in the old contents */
1277c478bd9Sstevel@tonic-gate 		bcopy(ptr, retval, (osize < nbytes) ? osize : nbytes);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		/* don't need the old memory anymore */
1307c478bd9Sstevel@tonic-gate 		alloc_free((char *)ptr, fname, line);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (retval);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * alloc_strdup -- a strdup() with checks
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * this routine is typically called via the STRDUP() macro in alloc.h
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate char *
alloc_strdup(const char * ptr,const char * fname,int line)1427c478bd9Sstevel@tonic-gate alloc_strdup(const char *ptr, const char *fname, int line)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	(void) strcpy(retval, ptr);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	return (retval);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * alloc_free -- a free() with checks
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  * this routine is typically called via the FREE() macro in alloc.h
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1577c478bd9Sstevel@tonic-gate void
alloc_free(void * ptr,const char * fname,int line)1587c478bd9Sstevel@tonic-gate alloc_free(void *ptr, const char *fname, int line)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	size_t osize;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	ASSERT(ptr != NULL);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* nothing to check in this version */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (Freetotal)
1717c478bd9Sstevel@tonic-gate 		stats_counter_add(Freetotal, osize);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (Freecount)
1747c478bd9Sstevel@tonic-gate 		stats_counter_bump(Freecount);
175*b5016cbbSstephh 	totalcount -= osize + HDRSIZ;
176*b5016cbbSstephh }
177*b5016cbbSstephh 
178*b5016cbbSstephh int
alloc_total()179*b5016cbbSstephh alloc_total()
180*b5016cbbSstephh {
181*b5016cbbSstephh 	return (totalcount);
182*b5016cbbSstephh }
183*b5016cbbSstephh 
184*b5016cbbSstephh /*
185*b5016cbbSstephh  * variants that don't maintain size in header - saves space
186*b5016cbbSstephh  */
187*b5016cbbSstephh void *
alloc_xmalloc(size_t nbytes)188*b5016cbbSstephh alloc_xmalloc(size_t nbytes)
189*b5016cbbSstephh {
190*b5016cbbSstephh 	char *retval;
191*b5016cbbSstephh 
192*b5016cbbSstephh 	ASSERT(nbytes > 0);
193*b5016cbbSstephh 	retval = fmd_hdl_alloc(Hdl, nbytes, FMD_SLEEP);
194*b5016cbbSstephh 	if (Malloctotal)
195*b5016cbbSstephh 		stats_counter_add(Malloctotal, nbytes);
196*b5016cbbSstephh 	if (Malloccount)
197*b5016cbbSstephh 		stats_counter_bump(Malloccount);
198*b5016cbbSstephh 	totalcount += nbytes;
199*b5016cbbSstephh 	return ((void *)retval);
200*b5016cbbSstephh }
201*b5016cbbSstephh 
202*b5016cbbSstephh void
alloc_xfree(void * ptr,size_t size)203*b5016cbbSstephh alloc_xfree(void *ptr, size_t size)
204*b5016cbbSstephh {
205*b5016cbbSstephh 	ASSERT(ptr != NULL);
206*b5016cbbSstephh 
207*b5016cbbSstephh 	fmd_hdl_free(Hdl, (char *)ptr, size);
208*b5016cbbSstephh 	if (Freetotal)
209*b5016cbbSstephh 		stats_counter_add(Freetotal, size);
210*b5016cbbSstephh 	if (Freecount)
211*b5016cbbSstephh 		stats_counter_bump(Freecount);
212*b5016cbbSstephh 	totalcount -= size;
2137c478bd9Sstevel@tonic-gate }
214