xref: /titanic_51/usr/src/cmd/fm/eversholt/common/alloc.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * alloc.c -- memory allocation wrapper functions, replacable in more
27  * constrained environments, such as within a DE.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "alloc.h"
36 #include "out.h"
37 #include "stats.h"
38 
39 static struct stats *Malloctotal;
40 static struct stats *Malloccount;
41 
42 void
43 alloc_init(void)
44 {
45 	Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
46 	Malloccount = stats_new_counter("alloc.calls", "total calls", 1);
47 }
48 
49 void
50 alloc_fini(void)
51 {
52 	struct stats *mt, *mc;
53 
54 	mt = Malloctotal;
55 	mc = Malloccount;
56 
57 	Malloctotal = NULL;
58 	Malloccount = NULL;
59 
60 	stats_delete(mt);
61 	stats_delete(mc);
62 }
63 
64 /*
65  * alloc_malloc -- a malloc() with checks
66  *
67  * this routine is typically called via the MALLOC() macro in alloc.h
68  */
69 
70 void *
71 alloc_malloc(size_t nbytes, const char *fname, int line)
72 {
73 	void *retval = malloc(nbytes);
74 
75 	if (retval == NULL)
76 		outfl(O_DIE, fname, line, "malloc: out of memory");
77 
78 	if (Malloctotal)
79 		stats_counter_add(Malloctotal, nbytes);
80 
81 	if (Malloccount)
82 		stats_counter_bump(Malloccount);
83 
84 	return (retval);
85 }
86 
87 /*
88  * alloc_realloc -- a realloc() with checks
89  *
90  * this routine is typically called via the REALLOC() macro in alloc.h
91  */
92 void *
93 alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
94 {
95 	void *retval = realloc(ptr, nbytes);
96 
97 	if (retval == NULL)
98 		out(O_DIE, fname, line, "realloc: out of memory");
99 
100 	return (retval);
101 }
102 
103 /*
104  * alloc_strdup -- a strdup() with checks
105  *
106  * this routine is typically called via the STRDUP() macro in alloc.h
107  */
108 char *
109 alloc_strdup(const char *ptr, const char *fname, int line)
110 {
111 	char *retval = strdup(ptr);
112 
113 	if (retval == NULL)
114 		outfl(O_DIE, fname, line, "strdup: out of memory");
115 
116 	return (retval);
117 }
118 
119 /*
120  * alloc_free -- a free() with checks
121  *
122  * this routine is typically called via the FREE() macro in alloc.h
123  */
124 /*ARGSUSED1*/
125 void
126 alloc_free(void *ptr, const char *fname, int line)
127 {
128 	/* nothing to check in this version */
129 	free(ptr);
130 }
131