1 /* $OpenBSD: memory.c,v 1.8 2010/07/19 19:46:44 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 <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <stddef.h> 45 #include <stdint.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 * 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 * 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 * 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 * 97 ecalloc(size_t s1, size_t s2) 98 { 99 void *p; 100 101 if ((p = calloc(s1, s2)) == NULL) 102 enocmem(s1, s2); 103 return p; 104 } 105 106 void * 107 emult_realloc(void *ptr, size_t s1, size_t s2) 108 { 109 size_t size; 110 111 if (s1 && SIZE_MAX / s1 < s2) { 112 errno = ENOMEM; 113 enocmem(s1, s2); 114 } 115 size = s1 * s2; 116 if ((ptr = realloc(ptr, size)) == NULL) 117 enocmem(s1, s2); 118 return ptr; 119 } 120 121 /* Support routines for hash tables. */ 122 void * 123 hash_alloc(size_t s, void *u UNUSED) 124 { 125 return ecalloc(s, 1); 126 } 127 128 void 129 hash_free(void *p, size_t s UNUSED, void *u UNUSED) 130 { 131 free(p); 132 } 133 134 void * 135 element_alloc(size_t s, void *u UNUSED) 136 { 137 return emalloc(s); 138 } 139 140 141 142 /* 143 * enomem -- 144 * die when out of memory. 145 */ 146 void 147 enomem(size_t size) 148 { 149 fprintf(stderr, "make: %s (%zu)\n", strerror(errno), size); 150 exit(2); 151 } 152 153 void 154 enocmem(size_t sz1, size_t sz2) 155 { 156 fprintf(stderr, "make: %s (%zu * %zu)\n", strerror(errno), sz1, sz2); 157 exit(2); 158 } 159 /* 160 * esetenv -- 161 * change environment, die on error. 162 */ 163 void 164 esetenv(const char *name, const char *value) 165 { 166 if (setenv(name, value, 1) == 0) 167 return; 168 169 fprintf(stderr, "make: setenv failed (%s)\n", strerror(errno)); 170 exit(2); 171 } 172 173 174 /* 175 * enunlink -- 176 * Remove a file carefully, avoiding directories. 177 */ 178 int 179 eunlink(const char *file) 180 { 181 struct stat st; 182 183 if (lstat(file, &st) == -1) 184 return -1; 185 186 if (S_ISDIR(st.st_mode)) { 187 errno = EISDIR; 188 return -1; 189 } 190 return unlink(file); 191 } 192 193 void 194 free_hash(struct ohash *h) 195 { 196 void *e; 197 unsigned int i; 198 199 for (e = ohash_first(h, &i); e != NULL; e = ohash_next(h, &i)) 200 free(e); 201 ohash_delete(h); 202 } 203 204