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