[BACK]Return to memory.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/memory.c, Revision 1.11

1.11    ! espie       1: /* $OpenBSD: memory.c,v 1.10 2014/05/12 19:11:19 espie Exp $ */
1.1       espie       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.
1.2       millert    20:  * 3. Neither the name of the University nor the names of its contributors
1.1       espie      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>
1.9       espie      40: #include <stddef.h>
                     41: #include <stdint.h>
1.1       espie      42: #include <stdio.h>
                     43: #include <stdlib.h>
                     44: #include <string.h>
                     45: #include <unistd.h>
1.4       espie      46: #include <ohash.h>
1.1       espie      47: #include "defines.h"
                     48: #include "memory.h"
                     49:
                     50: static void enomem(size_t);
1.5       espie      51: static void enocmem(size_t, size_t);
1.1       espie      52:
                     53: /*
                     54:  * emalloc --
                     55:  *     malloc, but die on error.
                     56:  */
                     57: void *
1.3       espie      58: emalloc(size_t size)
1.1       espie      59: {
                     60:        void *p;
                     61:
1.3       espie      62:        if ((p = malloc(size)) == NULL)
                     63:                enomem(size);
1.1       espie      64:        return p;
                     65: }
                     66:
                     67: /*
                     68:  * estrdup --
                     69:  *     strdup, but die on error.
                     70:  */
                     71: char *
1.3       espie      72: estrdup(const char *str)
1.1       espie      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 *
1.3       espie      89: erealloc(void *ptr, size_t size)
1.1       espie      90: {
                     91:        if ((ptr = realloc(ptr, size)) == NULL)
                     92:                enomem(size);
                     93:        return ptr;
                     94: }
                     95:
                     96: void *
1.10      espie      97: ereallocarray(void *ptr, size_t s1, size_t s2)
1.5       espie      98: {
1.10      espie      99:        if ((ptr = reallocarray(ptr, s1, s2)) == NULL)
1.5       espie     100:                enocmem(s1, s2);
                    101:        return ptr;
                    102: }
                    103:
1.1       espie     104: /* Support routines for hash tables.  */
                    105: void *
1.10      espie     106: hash_calloc(size_t n, size_t s, void *u UNUSED)
1.1       espie     107: {
1.11    ! espie     108:        void *p;
        !           109:
        !           110:        if ((p = calloc(n, s)) == NULL)
        !           111:                enocmem(n, s);
        !           112:        return p;
1.1       espie     113: }
                    114:
                    115: void
1.10      espie     116: hash_free(void *p, void *u UNUSED)
1.1       espie     117: {
                    118:        free(p);
                    119: }
                    120:
                    121: void *
1.3       espie     122: element_alloc(size_t s, void *u UNUSED)
1.1       espie     123: {
                    124:        return emalloc(s);
                    125: }
                    126:
                    127:
                    128:
                    129: /*
                    130:  * enomem --
                    131:  *     die when out of memory.
                    132:  */
                    133: void
1.3       espie     134: enomem(size_t size)
1.1       espie     135: {
1.5       espie     136:        fprintf(stderr, "make: %s (%zu)\n", strerror(errno), size);
1.1       espie     137:        exit(2);
                    138: }
                    139:
1.5       espie     140: void
                    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: }
1.1       espie     146: /*
                    147:  * esetenv --
                    148:  *     change environment, die on error.
                    149:  */
                    150: void
1.3       espie     151: esetenv(const char *name, const char *value)
1.1       espie     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
1.3       espie     166: eunlink(const char *file)
1.1       espie     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);
1.4       espie     178: }
                    179:
                    180: void
                    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);
1.1       espie     189: }
                    190: