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
5*b5016cbbSstephh  * Common Development and Distribution License (the "License").
6*b5016cbbSstephh  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*b5016cbbSstephh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * alloc.c -- memory allocation wrapper functions, replacable in more
267c478bd9Sstevel@tonic-gate  * constrained environments, such as within a DE.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "alloc.h"
337c478bd9Sstevel@tonic-gate #include "out.h"
347c478bd9Sstevel@tonic-gate #include "stats.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static struct stats *Malloctotal;
377c478bd9Sstevel@tonic-gate static struct stats *Malloccount;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate void
alloc_init(void)407c478bd9Sstevel@tonic-gate alloc_init(void)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
437c478bd9Sstevel@tonic-gate 	Malloccount = stats_new_counter("alloc.calls", "total calls", 1);
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate void
alloc_fini(void)477c478bd9Sstevel@tonic-gate alloc_fini(void)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	struct stats *mt, *mc;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	mt = Malloctotal;
527c478bd9Sstevel@tonic-gate 	mc = Malloccount;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	Malloctotal = NULL;
557c478bd9Sstevel@tonic-gate 	Malloccount = NULL;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	stats_delete(mt);
587c478bd9Sstevel@tonic-gate 	stats_delete(mc);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * alloc_malloc -- a malloc() with checks
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * this routine is typically called via the MALLOC() macro in alloc.h
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate void *
alloc_malloc(size_t nbytes,const char * fname,int line)687c478bd9Sstevel@tonic-gate alloc_malloc(size_t nbytes, const char *fname, int line)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	void *retval = malloc(nbytes);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (retval == NULL)
737c478bd9Sstevel@tonic-gate 		outfl(O_DIE, fname, line, "malloc: out of memory");
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (Malloctotal)
767c478bd9Sstevel@tonic-gate 		stats_counter_add(Malloctotal, nbytes);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (Malloccount)
797c478bd9Sstevel@tonic-gate 		stats_counter_bump(Malloccount);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	return (retval);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * alloc_realloc -- a realloc() with checks
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * this routine is typically called via the REALLOC() macro in alloc.h
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate void *
alloc_realloc(void * ptr,size_t nbytes,const char * fname,int line)907c478bd9Sstevel@tonic-gate alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	void *retval = realloc(ptr, nbytes);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (retval == NULL)
957c478bd9Sstevel@tonic-gate 		out(O_DIE, fname, line, "realloc: out of memory");
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	return (retval);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * alloc_strdup -- a strdup() with checks
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * this routine is typically called via the STRDUP() macro in alloc.h
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate char *
alloc_strdup(const char * ptr,const char * fname,int line)1067c478bd9Sstevel@tonic-gate alloc_strdup(const char *ptr, const char *fname, int line)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	char *retval = strdup(ptr);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (retval == NULL)
1117c478bd9Sstevel@tonic-gate 		outfl(O_DIE, fname, line, "strdup: out of memory");
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return (retval);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * alloc_free -- a free() with checks
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * this routine is typically called via the FREE() macro in alloc.h
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1227c478bd9Sstevel@tonic-gate void
alloc_free(void * ptr,const char * fname,int line)1237c478bd9Sstevel@tonic-gate alloc_free(void *ptr, const char *fname, int line)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	/* nothing to check in this version */
1267c478bd9Sstevel@tonic-gate 	free(ptr);
1277c478bd9Sstevel@tonic-gate }
128*b5016cbbSstephh 
129*b5016cbbSstephh /*
130*b5016cbbSstephh  * variants that don't maintain size in header - saves space
131*b5016cbbSstephh  */
132*b5016cbbSstephh void *
alloc_xmalloc(size_t nbytes)133*b5016cbbSstephh alloc_xmalloc(size_t nbytes)
134*b5016cbbSstephh {
135*b5016cbbSstephh 	void *retval;
136*b5016cbbSstephh 
137*b5016cbbSstephh 	retval = malloc(nbytes);
138*b5016cbbSstephh 	if (retval == NULL)
139*b5016cbbSstephh 		out(O_DIE, "malloc: out of memory");
140*b5016cbbSstephh 	if (Malloctotal)
141*b5016cbbSstephh 		stats_counter_add(Malloctotal, nbytes);
142*b5016cbbSstephh 	if (Malloccount)
143*b5016cbbSstephh 		stats_counter_bump(Malloccount);
144*b5016cbbSstephh 	return (retval);
145*b5016cbbSstephh }
146*b5016cbbSstephh 
147*b5016cbbSstephh /*ARGSUSED*/
148*b5016cbbSstephh void
alloc_xfree(void * ptr,size_t size)149*b5016cbbSstephh alloc_xfree(void *ptr, size_t size)
150*b5016cbbSstephh {
151*b5016cbbSstephh 	free(ptr);
152*b5016cbbSstephh }
153