4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * alloc.c -- memory allocation wrapper functions, replacable in more
26 * constrained environments, such as within a DE.
29 #pragma ident "%Z%%M% %I% %E% SMI"
38 static struct stats
*Malloctotal
;
39 static struct stats
*Malloccount
;
44 Malloctotal
= stats_new_counter("alloc.total", "bytes allocated", 1);
45 Malloccount
= stats_new_counter("alloc.calls", "total calls", 1);
51 struct stats
*mt
, *mc
;
64 * alloc_malloc -- a malloc() with checks
66 * this routine is typically called via the MALLOC() macro in alloc.h
70 alloc_malloc(size_t nbytes
, const char *fname
, int line
)
72 void *retval
= malloc(nbytes
);
75 outfl(O_DIE
, fname
, line
, "malloc: out of memory");
78 stats_counter_add(Malloctotal
, nbytes
);
81 stats_counter_bump(Malloccount
);
87 * alloc_realloc -- a realloc() with checks
89 * this routine is typically called via the REALLOC() macro in alloc.h
92 alloc_realloc(void *ptr
, size_t nbytes
, const char *fname
, int line
)
94 void *retval
= realloc(ptr
, nbytes
);
97 out(O_DIE
, fname
, line
, "realloc: out of memory");
103 * alloc_strdup -- a strdup() with checks
105 * this routine is typically called via the STRDUP() macro in alloc.h
108 alloc_strdup(const char *ptr
, const char *fname
, int line
)
110 char *retval
= strdup(ptr
);
113 outfl(O_DIE
, fname
, line
, "strdup: out of memory");
119 * alloc_free -- a free() with checks
121 * this routine is typically called via the FREE() macro in alloc.h
125 alloc_free(void *ptr
, const char *fname
, int line
)
127 /* nothing to check in this version */
132 * variants that don't maintain size in header - saves space
135 alloc_xmalloc(size_t nbytes
)
139 retval
= malloc(nbytes
);
141 out(O_DIE
, "malloc: out of memory");
143 stats_counter_add(Malloctotal
, nbytes
);
145 stats_counter_bump(Malloccount
);
151 alloc_xfree(void *ptr
, size_t size
)