[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.5

1.1       espie       1: /* $OpenPackages$ */
1.5     ! espie       2: /* $OpenBSD$ */
1.1       espie       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.
1.2       millert    21:  * 3. Neither the name of the University nor the names of its contributors
1.1       espie      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>
1.4       espie      45: #include <stddef.h>
                     46: #include <stdint.h>
                     47: #include <ohash.h>
1.1       espie      48: #include "defines.h"
                     49: #include "memory.h"
                     50:
                     51: static void enomem(size_t);
1.5     ! espie      52: static void enocmem(size_t, size_t);
1.1       espie      53:
                     54: /*
                     55:  * emalloc --
                     56:  *     malloc, but die on error.
                     57:  */
                     58: void *
1.3       espie      59: emalloc(size_t size)
1.1       espie      60: {
                     61:        void *p;
                     62:
1.3       espie      63:        if ((p = malloc(size)) == NULL)
                     64:                enomem(size);
1.1       espie      65:        return p;
                     66: }
                     67:
                     68: /*
                     69:  * estrdup --
                     70:  *     strdup, but die on error.
                     71:  */
                     72: char *
1.3       espie      73: estrdup(const char *str)
1.1       espie      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 *
1.3       espie      90: erealloc(void *ptr, size_t size)
1.1       espie      91: {
                     92:        if ((ptr = realloc(ptr, size)) == NULL)
                     93:                enomem(size);
                     94:        return ptr;
                     95: }
                     96:
                     97: void *
1.3       espie      98: ecalloc(size_t s1, size_t s2)
1.1       espie      99: {
                    100:        void *p;
                    101:
                    102:        if ((p = calloc(s1, s2)) == NULL)
1.5     ! espie     103:                enocmem(s1, s2);
1.1       espie     104:        return p;
                    105: }
                    106:
1.5     ! espie     107: void *
        !           108: erecalloc(void *ptr, size_t s1, size_t s2)
        !           109: {
        !           110:        if ((ptr = recalloc(ptr, s1, s2)) == NULL)
        !           111:                enocmem(s1, s2);
        !           112:        return ptr;
        !           113: }
        !           114:
1.1       espie     115: /* Support routines for hash tables.  */
                    116: void *
1.3       espie     117: hash_alloc(size_t s, void *u UNUSED)
1.1       espie     118: {
                    119:        return ecalloc(s, 1);
                    120: }
                    121:
                    122: void
1.3       espie     123: hash_free(void *p, size_t s UNUSED, void *u UNUSED)
1.1       espie     124: {
                    125:        free(p);
                    126: }
                    127:
                    128: void *
1.3       espie     129: element_alloc(size_t s, void *u UNUSED)
1.1       espie     130: {
                    131:        return emalloc(s);
                    132: }
                    133:
                    134:
                    135:
                    136: /*
                    137:  * enomem --
                    138:  *     die when out of memory.
                    139:  */
                    140: void
1.3       espie     141: enomem(size_t size)
1.1       espie     142: {
1.5     ! espie     143:        fprintf(stderr, "make: %s (%zu)\n", strerror(errno), size);
1.1       espie     144:        exit(2);
                    145: }
                    146:
1.5     ! espie     147: void
        !           148: enocmem(size_t sz1, size_t sz2)
        !           149: {
        !           150:        fprintf(stderr, "make: %s (%zu * %zu)\n", strerror(errno), sz1, sz2);
        !           151:        exit(2);
        !           152: }
1.1       espie     153: /*
                    154:  * esetenv --
                    155:  *     change environment, die on error.
                    156:  */
                    157: void
1.3       espie     158: esetenv(const char *name, const char *value)
1.1       espie     159: {
                    160:        if (setenv(name, value, 1) == 0)
                    161:            return;
                    162:
                    163:        fprintf(stderr, "make: setenv failed (%s)\n", strerror(errno));
                    164:        exit(2);
                    165: }
                    166:
                    167:
                    168: /*
                    169:  * enunlink --
                    170:  *     Remove a file carefully, avoiding directories.
                    171:  */
                    172: int
1.3       espie     173: eunlink(const char *file)
1.1       espie     174: {
                    175:        struct stat st;
                    176:
                    177:        if (lstat(file, &st) == -1)
                    178:                return -1;
                    179:
                    180:        if (S_ISDIR(st.st_mode)) {
                    181:                errno = EISDIR;
                    182:                return -1;
                    183:        }
                    184:        return unlink(file);
1.4       espie     185: }
                    186:
                    187: void
                    188: free_hash(struct ohash *h)
                    189: {
                    190:        void *e;
                    191:        unsigned int i;
                    192:
                    193:        for (e = ohash_first(h, &i); e != NULL; e = ohash_next(h, &i))
                    194:                free(e);
                    195:        ohash_delete(h);
1.1       espie     196: }
                    197: