dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / eversholt / common / alloc.c
blobe1b051bc747b27f78e31b6fedc157627d01072c9
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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"
31 #include <stdlib.h>
32 #include <string.h>
34 #include "alloc.h"
35 #include "out.h"
36 #include "stats.h"
38 static struct stats *Malloctotal;
39 static struct stats *Malloccount;
41 void
42 alloc_init(void)
44 Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
45 Malloccount = stats_new_counter("alloc.calls", "total calls", 1);
48 void
49 alloc_fini(void)
51 struct stats *mt, *mc;
53 mt = Malloctotal;
54 mc = Malloccount;
56 Malloctotal = NULL;
57 Malloccount = NULL;
59 stats_delete(mt);
60 stats_delete(mc);
64 * alloc_malloc -- a malloc() with checks
66 * this routine is typically called via the MALLOC() macro in alloc.h
69 void *
70 alloc_malloc(size_t nbytes, const char *fname, int line)
72 void *retval = malloc(nbytes);
74 if (retval == NULL)
75 outfl(O_DIE, fname, line, "malloc: out of memory");
77 if (Malloctotal)
78 stats_counter_add(Malloctotal, nbytes);
80 if (Malloccount)
81 stats_counter_bump(Malloccount);
83 return (retval);
87 * alloc_realloc -- a realloc() with checks
89 * this routine is typically called via the REALLOC() macro in alloc.h
91 void *
92 alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
94 void *retval = realloc(ptr, nbytes);
96 if (retval == NULL)
97 out(O_DIE, fname, line, "realloc: out of memory");
99 return (retval);
103 * alloc_strdup -- a strdup() with checks
105 * this routine is typically called via the STRDUP() macro in alloc.h
107 char *
108 alloc_strdup(const char *ptr, const char *fname, int line)
110 char *retval = strdup(ptr);
112 if (retval == NULL)
113 outfl(O_DIE, fname, line, "strdup: out of memory");
115 return (retval);
119 * alloc_free -- a free() with checks
121 * this routine is typically called via the FREE() macro in alloc.h
123 /*ARGSUSED1*/
124 void
125 alloc_free(void *ptr, const char *fname, int line)
127 /* nothing to check in this version */
128 free(ptr);
132 * variants that don't maintain size in header - saves space
134 void *
135 alloc_xmalloc(size_t nbytes)
137 void *retval;
139 retval = malloc(nbytes);
140 if (retval == NULL)
141 out(O_DIE, "malloc: out of memory");
142 if (Malloctotal)
143 stats_counter_add(Malloctotal, nbytes);
144 if (Malloccount)
145 stats_counter_bump(Malloccount);
146 return (retval);
149 /*ARGSUSED*/
150 void
151 alloc_xfree(void *ptr, size_t size)
153 free(ptr);