alloc.c revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881
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/*
2380ab886wesolows * Copyright 2006 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
517c478bdstevel@tonic-gatevoid
527c478bdstevel@tonic-gatealloc_init(void)
537c478bdstevel@tonic-gate{
547c478bdstevel@tonic-gate	Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
557c478bdstevel@tonic-gate	Freetotal = stats_new_counter("free.total", "bytes freed", 1);
567c478bdstevel@tonic-gate	Malloccount = stats_new_counter("alloc.calls", "alloc calls", 1);
577c478bdstevel@tonic-gate	Freecount = stats_new_counter("free.calls", "free calls", 1);
587c478bdstevel@tonic-gate}
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gatevoid
617c478bdstevel@tonic-gatealloc_fini(void)
627c478bdstevel@tonic-gate{
637c478bdstevel@tonic-gate	struct stats *mt, *ft, *mc, *fc;
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate	mt = Malloctotal;
667c478bdstevel@tonic-gate	ft = Freetotal;
677c478bdstevel@tonic-gate	mc = Malloccount;
687c478bdstevel@tonic-gate	fc = Freecount;
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate	Malloctotal = NULL;
717c478bdstevel@tonic-gate	Freetotal = NULL;
727c478bdstevel@tonic-gate	Malloccount = NULL;
737c478bdstevel@tonic-gate	Freecount = NULL;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	stats_delete(mt);
767c478bdstevel@tonic-gate	stats_delete(ft);
777c478bdstevel@tonic-gate	stats_delete(mc);
787c478bdstevel@tonic-gate	stats_delete(fc);
797c478bdstevel@tonic-gate}
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate/*
827c478bdstevel@tonic-gate * alloc_malloc -- a malloc() with checks
837c478bdstevel@tonic-gate *
847c478bdstevel@tonic-gate * this routine is typically called via the MALLOC() macro in alloc.h
857c478bdstevel@tonic-gate */
867c478bdstevel@tonic-gate/*ARGSUSED*/
877c478bdstevel@tonic-gatevoid *
887c478bdstevel@tonic-gatealloc_malloc(size_t nbytes, const char *fname, int line)
897c478bdstevel@tonic-gate{
907c478bdstevel@tonic-gate	char *retval;
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate	ASSERT(nbytes > 0);
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate	retval = fmd_hdl_alloc(Hdl, nbytes + HDRSIZ, FMD_SLEEP);
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gate	/* retval can't be NULL since fmd_hdl_alloc() sleeps for memory */
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate	bcopy((void *)&nbytes, (void *)retval, sizeof (nbytes));
997c478bdstevel@tonic-gate	retval += HDRSIZ;
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate	if (Malloctotal)
1027c478bdstevel@tonic-gate		stats_counter_add(Malloctotal, nbytes);
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate	if (Malloccount)
1057c478bdstevel@tonic-gate		stats_counter_bump(Malloccount);
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	return ((void *)retval);
1087c478bdstevel@tonic-gate}
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate/*
1117c478bdstevel@tonic-gate * alloc_realloc -- a realloc() with checks
1127c478bdstevel@tonic-gate *
1137c478bdstevel@tonic-gate * this routine is typically called via the REALLOC() macro in alloc.h
1147c478bdstevel@tonic-gate */
1157c478bdstevel@tonic-gatevoid *
1167c478bdstevel@tonic-gatealloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
1177c478bdstevel@tonic-gate{
1187c478bdstevel@tonic-gate	void *retval = alloc_malloc(nbytes, fname, line);
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate	if (ptr != NULL) {
1217c478bdstevel@tonic-gate		size_t osize;
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate		bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize,
1247c478bdstevel@tonic-gate		    sizeof (osize));
1257c478bdstevel@tonic-gate		/* now we have the new memory, copy in the old contents */
1267c478bdstevel@tonic-gate		bcopy(ptr, retval, (osize < nbytes) ? osize : nbytes);
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gate		/* don't need the old memory anymore */
1297c478bdstevel@tonic-gate		alloc_free((char *)ptr, fname, line);
1307c478bdstevel@tonic-gate	}
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate	return (retval);
1337c478bdstevel@tonic-gate}
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate/*
1367c478bdstevel@tonic-gate * alloc_strdup -- a strdup() with checks
1377c478bdstevel@tonic-gate *
1387c478bdstevel@tonic-gate * this routine is typically called via the STRDUP() macro in alloc.h
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gatechar *
1417c478bdstevel@tonic-gatealloc_strdup(const char *ptr, const char *fname, int line)
1427c478bdstevel@tonic-gate{
1437c478bdstevel@tonic-gate	char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate	(void) strcpy(retval, ptr);
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate	return (retval);
1487c478bdstevel@tonic-gate}
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate/*
1517c478bdstevel@tonic-gate * alloc_free -- a free() with checks
1527c478bdstevel@tonic-gate *
1537c478bdstevel@tonic-gate * this routine is typically called via the FREE() macro in alloc.h
1547c478bdstevel@tonic-gate */
1557c478bdstevel@tonic-gate/*ARGSUSED*/
1567c478bdstevel@tonic-gatevoid
1577c478bdstevel@tonic-gatealloc_free(void *ptr, const char *fname, int line)
1587c478bdstevel@tonic-gate{
1597c478bdstevel@tonic-gate	size_t osize;
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gate	ASSERT(ptr != NULL);
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate	bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate	/* nothing to check in this version */
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate	fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate	if (Freetotal)
1707c478bdstevel@tonic-gate		stats_counter_add(Freetotal, osize);
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate	if (Freecount)
1737c478bdstevel@tonic-gate		stats_counter_bump(Freecount);
1747c478bdstevel@tonic-gate}
175