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

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