10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*4436Sstephh * Common Development and Distribution License (the "License").
6*4436Sstephh * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*4436Sstephh * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * alloc.c -- memory allocation wrapper functions, replacable in more
260Sstevel@tonic-gate * constrained environments, such as within a DE.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include "alloc.h"
350Sstevel@tonic-gate #include "out.h"
360Sstevel@tonic-gate #include "stats.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate static struct stats *Malloctotal;
390Sstevel@tonic-gate static struct stats *Malloccount;
400Sstevel@tonic-gate
410Sstevel@tonic-gate void
alloc_init(void)420Sstevel@tonic-gate alloc_init(void)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
450Sstevel@tonic-gate Malloccount = stats_new_counter("alloc.calls", "total calls", 1);
460Sstevel@tonic-gate }
470Sstevel@tonic-gate
480Sstevel@tonic-gate void
alloc_fini(void)490Sstevel@tonic-gate alloc_fini(void)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate struct stats *mt, *mc;
520Sstevel@tonic-gate
530Sstevel@tonic-gate mt = Malloctotal;
540Sstevel@tonic-gate mc = Malloccount;
550Sstevel@tonic-gate
560Sstevel@tonic-gate Malloctotal = NULL;
570Sstevel@tonic-gate Malloccount = NULL;
580Sstevel@tonic-gate
590Sstevel@tonic-gate stats_delete(mt);
600Sstevel@tonic-gate stats_delete(mc);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * alloc_malloc -- a malloc() with checks
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * this routine is typically called via the MALLOC() macro in alloc.h
670Sstevel@tonic-gate */
680Sstevel@tonic-gate
690Sstevel@tonic-gate void *
alloc_malloc(size_t nbytes,const char * fname,int line)700Sstevel@tonic-gate alloc_malloc(size_t nbytes, const char *fname, int line)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate void *retval = malloc(nbytes);
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (retval == NULL)
750Sstevel@tonic-gate outfl(O_DIE, fname, line, "malloc: out of memory");
760Sstevel@tonic-gate
770Sstevel@tonic-gate if (Malloctotal)
780Sstevel@tonic-gate stats_counter_add(Malloctotal, nbytes);
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (Malloccount)
810Sstevel@tonic-gate stats_counter_bump(Malloccount);
820Sstevel@tonic-gate
830Sstevel@tonic-gate return (retval);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * alloc_realloc -- a realloc() with checks
880Sstevel@tonic-gate *
890Sstevel@tonic-gate * this routine is typically called via the REALLOC() macro in alloc.h
900Sstevel@tonic-gate */
910Sstevel@tonic-gate void *
alloc_realloc(void * ptr,size_t nbytes,const char * fname,int line)920Sstevel@tonic-gate alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate void *retval = realloc(ptr, nbytes);
950Sstevel@tonic-gate
960Sstevel@tonic-gate if (retval == NULL)
970Sstevel@tonic-gate out(O_DIE, fname, line, "realloc: out of memory");
980Sstevel@tonic-gate
990Sstevel@tonic-gate return (retval);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * alloc_strdup -- a strdup() with checks
1040Sstevel@tonic-gate *
1050Sstevel@tonic-gate * this routine is typically called via the STRDUP() macro in alloc.h
1060Sstevel@tonic-gate */
1070Sstevel@tonic-gate char *
alloc_strdup(const char * ptr,const char * fname,int line)1080Sstevel@tonic-gate alloc_strdup(const char *ptr, const char *fname, int line)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate char *retval = strdup(ptr);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if (retval == NULL)
1130Sstevel@tonic-gate outfl(O_DIE, fname, line, "strdup: out of memory");
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate return (retval);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * alloc_free -- a free() with checks
1200Sstevel@tonic-gate *
1210Sstevel@tonic-gate * this routine is typically called via the FREE() macro in alloc.h
1220Sstevel@tonic-gate */
1230Sstevel@tonic-gate /*ARGSUSED1*/
1240Sstevel@tonic-gate void
alloc_free(void * ptr,const char * fname,int line)1250Sstevel@tonic-gate alloc_free(void *ptr, const char *fname, int line)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate /* nothing to check in this version */
1280Sstevel@tonic-gate free(ptr);
1290Sstevel@tonic-gate }
130*4436Sstephh
131*4436Sstephh /*
132*4436Sstephh * variants that don't maintain size in header - saves space
133*4436Sstephh */
134*4436Sstephh void *
alloc_xmalloc(size_t nbytes)135*4436Sstephh alloc_xmalloc(size_t nbytes)
136*4436Sstephh {
137*4436Sstephh void *retval;
138*4436Sstephh
139*4436Sstephh retval = malloc(nbytes);
140*4436Sstephh if (retval == NULL)
141*4436Sstephh out(O_DIE, "malloc: out of memory");
142*4436Sstephh if (Malloctotal)
143*4436Sstephh stats_counter_add(Malloctotal, nbytes);
144*4436Sstephh if (Malloccount)
145*4436Sstephh stats_counter_bump(Malloccount);
146*4436Sstephh return (retval);
147*4436Sstephh }
148*4436Sstephh
149*4436Sstephh /*ARGSUSED*/
150*4436Sstephh void
alloc_xfree(void * ptr,size_t size)151*4436Sstephh alloc_xfree(void *ptr, size_t size)
152*4436Sstephh {
153*4436Sstephh free(ptr);
154*4436Sstephh }
155