make_malloc.c revision 1.3 1 /* $NetBSD: make_malloc.c,v 1.3 2009/01/24 14:43:28 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: make_malloc.c,v 1.3 2009/01/24 14:43:28 dsl Exp $");
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include "make_malloc.h"
38
39 #ifndef USE_EMALLOC
40 /*
41 * enomem --
42 * die when out of memory.
43 */
44 static void
45 enomem(void)
46 {
47 extern char *progname;
48
49 (void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
50 exit(2);
51 }
52
53 /*
54 * bmake_malloc --
55 * malloc, but die on error.
56 */
57 void *
58 bmake_malloc(size_t len)
59 {
60 void *p;
61
62 if ((p = malloc(len)) == NULL)
63 enomem();
64 return(p);
65 }
66
67 /*
68 * bmake_strdup --
69 * strdup, but die on error.
70 */
71 char *
72 bmake_strdup(const char *str)
73 {
74 size_t len;
75 char *p;
76
77 len = strlen(str) + 1;
78 if ((p = malloc(len)) == NULL)
79 enomem();
80 return memcpy(p, str, len);
81 }
82
83 /*
84 * bmake_strndup --
85 * strndup, but die on error.
86 */
87 char *
88 bmake_strndup(const char *str, size_t max_len)
89 {
90 size_t len;
91 char *p;
92
93 if (str == NULL)
94 return NULL;
95
96 len = strlen(str);
97 if (len > max_len)
98 len = max_len;
99 p = bmake_malloc(len + 1);
100 memcpy(p, str, len);
101 p[len] = '\0';
102
103 return(p);
104 }
105
106 /*
107 * bmake_realloc --
108 * realloc, but die on error.
109 */
110 void *
111 bmake_realloc(void *ptr, size_t size)
112 {
113 if ((ptr = realloc(ptr, size)) == NULL)
114 enomem();
115 return(ptr);
116 }
117 #endif
118