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