xref: /openbsd-src/usr.bin/make/memory.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /* $OpenPackages$ */
2 /* $OpenBSD: memory.c,v 1.6 2008/03/24 18:03:53 espie Exp $ */
3 
4 /*
5  * Copyright (c) 1988, 1989, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * Copyright (c) 1989 by Berkeley Softworks
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Adam de Boor.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <stddef.h>
46 #include <stdint.h>
47 #include <ohash.h>
48 #include "defines.h"
49 #include "memory.h"
50 
51 static void enomem(size_t);
52 static void enocmem(size_t, size_t);
53 
54 /*
55  * emalloc --
56  *	malloc, but die on error.
57  */
58 void *
59 emalloc(size_t size)
60 {
61 	void *p;
62 
63 	if ((p = malloc(size)) == NULL)
64 		enomem(size);
65 	return p;
66 }
67 
68 /*
69  * estrdup --
70  *	strdup, but die on error.
71  */
72 char *
73 estrdup(const char *str)
74 {
75 	char *p;
76 	size_t size;
77 
78 	size = strlen(str) + 1;
79 
80 	p = emalloc(size);
81 	memcpy(p, str, size);
82 	return p;
83 }
84 
85 /*
86  * erealloc --
87  *	realloc, but die on error.
88  */
89 void *
90 erealloc(void *ptr, size_t size)
91 {
92 	if ((ptr = realloc(ptr, size)) == NULL)
93 		enomem(size);
94 	return ptr;
95 }
96 
97 void *
98 ecalloc(size_t s1, size_t s2)
99 {
100 	void *p;
101 
102 	if ((p = calloc(s1, s2)) == NULL)
103 		enocmem(s1, s2);
104 	return p;
105 }
106 
107 void *
108 emult_realloc(void *ptr, size_t s1, size_t s2)
109 {
110 	size_t size;
111 
112 	if (s1 && SIZE_MAX / s1 < s2) {
113 		errno = ENOMEM;
114 		enocmem(s1, s2);
115 	}
116 	size = s1 * s2;
117 	if ((ptr = realloc(ptr, size)) == NULL)
118 		enocmem(s1, s2);
119 	return ptr;
120 }
121 
122 /* Support routines for hash tables.  */
123 void *
124 hash_alloc(size_t s, void *u UNUSED)
125 {
126 	return ecalloc(s, 1);
127 }
128 
129 void
130 hash_free(void *p, size_t s UNUSED, void *u UNUSED)
131 {
132 	free(p);
133 }
134 
135 void *
136 element_alloc(size_t s, void *u UNUSED)
137 {
138 	return emalloc(s);
139 }
140 
141 
142 
143 /*
144  * enomem --
145  *	die when out of memory.
146  */
147 void
148 enomem(size_t size)
149 {
150 	fprintf(stderr, "make: %s (%zu)\n", strerror(errno), size);
151 	exit(2);
152 }
153 
154 void
155 enocmem(size_t sz1, size_t sz2)
156 {
157 	fprintf(stderr, "make: %s (%zu * %zu)\n", strerror(errno), sz1, sz2);
158 	exit(2);
159 }
160 /*
161  * esetenv --
162  *	change environment, die on error.
163  */
164 void
165 esetenv(const char *name, const char *value)
166 {
167 	if (setenv(name, value, 1) == 0)
168 	    return;
169 
170 	fprintf(stderr, "make: setenv failed (%s)\n", strerror(errno));
171 	exit(2);
172 }
173 
174 
175 /*
176  * enunlink --
177  *	Remove a file carefully, avoiding directories.
178  */
179 int
180 eunlink(const char *file)
181 {
182 	struct stat st;
183 
184 	if (lstat(file, &st) == -1)
185 		return -1;
186 
187 	if (S_ISDIR(st.st_mode)) {
188 		errno = EISDIR;
189 		return -1;
190 	}
191 	return unlink(file);
192 }
193 
194 void
195 free_hash(struct ohash *h)
196 {
197 	void *e;
198 	unsigned int i;
199 
200 	for (e = ohash_first(h, &i); e != NULL; e = ohash_next(h, &i))
201 		free(e);
202 	ohash_delete(h);
203 }
204 
205