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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /* Copyright (c) 1988 AT&T */
22 /* All Rights Reserved */
23 /*
24  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
25  * Use is subject to license terms.
26  */
27 /*
28  * Copyright 2006-2020 J. Schilling
29  *
30  * @(#)fmalloc.c	1.8 20/09/06 J. Schilling
31  */
32 #if defined(sun)
33 #pragma ident "@(#)fmalloc.c 1.8 20/09/06 J. Schilling"
34 #endif
35 /*
36  * @(#)fmalloc.c 1.5 06/12/12
37  */
38 
39 #if defined(sun)
40 #pragma ident	"@(#)fmalloc.c"
41 #pragma ident	"@(#)sccs:lib/mpwlib/fmalloc.c"
42 #endif
43 
44 /*
45 	The functions is this file replace xalloc-xfree-xfreeall from
46 	the PW library.
47 
48 	Xalloc allocated words, not bytes, so this adjustment is made
49 	here.  This inconsistency should eventually be cleaned-up in the
50 	other source, i.e. each request for memory should be in bytes.
51 
52 	These functions are complicated by the fact that libc has no
53 	equivalent to ffreeall.  This requires that pointers to allocated
54 	arrays be stored here.  If malloc ever has a freeall associated with
55 	it, most of this code can be discarded.
56 */
57 
58 #include <defines.h>
59 
60 #define LCHUNK	100
61 
62 static unsigned	ptrcnt = 0;
63 static unsigned	listsize = 0;
64 static void	**ptrlist = NULL;
65 
66 #ifdef	DBG_MALLOC
67 void *
dbg_fmalloc(asize,file,line)68 dbg_fmalloc(asize, file, line)
69 unsigned asize;
70 char	*file;
71 int	line;
72 {
73 	void *ptr;
74 	char dfile[100];
75 
76 	if (listsize == 0) {
77 		listsize = LCHUNK;
78 		snprintf(dfile, sizeof (dfile), "%s/%s", __FILE__, file);
79 		if ((ptrlist = (void **)dbg_malloc(sizeof(void *)*listsize, dfile, line)) == NULL)
80 			fatal(gettext("OUT OF SPACE (ut9)"));
81 	}
82 	if (ptrcnt >= listsize) {
83 		listsize += LCHUNK;
84 		snprintf(dfile, sizeof (dfile), "%s/%s", __FILE__, file);
85 		if ((ptrlist = (void **)dbg_realloc((void *) ptrlist,
86 					sizeof(void *)*listsize, dfile, line)) == NULL)
87 			fatal(gettext("OUT OF SPACE (ut9)"));
88 	}
89 
90 	if ((ptr = dbg_malloc(asize, file, line)) == NULL)
91 		fatal(gettext("OUT OF SPACE (ut9)"));
92 	else
93 		ptrlist[ptrcnt++] = ptr;
94 	return(ptr);
95 }
96 #undef	fmalloc
97 #endif
98 
99 void *
fmalloc(asize)100 fmalloc(asize)
101 unsigned asize;
102 {
103 	void *ptr;
104 
105 	if (listsize == 0) {
106 		listsize = LCHUNK;
107 		if ((ptrlist = (void **)malloc(sizeof(void *)*listsize)) == NULL)
108 			fatal(gettext("OUT OF SPACE (ut9)"));
109 	}
110 	if (ptrcnt >= listsize) {
111 		listsize += LCHUNK;
112 		if ((ptrlist = (void **)realloc((void *) ptrlist,
113 					sizeof(void *)*listsize)) == NULL)
114 			fatal(gettext("OUT OF SPACE (ut9)"));
115 	}
116 
117 	if ((ptr = malloc(asize)) == NULL)
118 		fatal(gettext("OUT OF SPACE (ut9)"));
119 	else
120 		ptrlist[ptrcnt++] = ptr;
121 	return(ptr);
122 }
123 
124 void
ffree(aptr)125 ffree(aptr)
126 void *aptr;
127 {
128 	register unsigned cnt;
129 
130 	cnt = ptrcnt;
131 	while (cnt)
132 		if (aptr == ptrlist[--cnt]) {
133 			free(aptr);
134 			ptrlist[cnt] = NULL;
135 			if (cnt == ptrcnt - 1)
136 				--ptrcnt;
137 			return;
138 		}
139 	fatal(gettext("ffree: Pointer not pointing to allocated area"));
140 	/*NOTREACHED*/
141 }
142 
143 void
ffreeall()144 ffreeall()
145 {
146 	while(ptrcnt)
147 		if (ptrlist[--ptrcnt] != NULL)
148 			free(ptrlist[ptrcnt]);
149 	if (ptrlist != NULL)
150 		free((void *)ptrlist);
151 	ptrlist = NULL;
152 	listsize = 0;
153 }
154