1*6eef5f0cSAntonio Huete Jimenez /* $NetBSD: make_malloc.c,v 1.26 2022/01/07 08:30:04 rillig Exp $ */
201e196c8SJohn Marino
3a34d5fb1SAntonio Huete Jimenez /*
401e196c8SJohn Marino * Copyright (c) 2009 The NetBSD Foundation, Inc.
501e196c8SJohn Marino * All rights reserved.
601e196c8SJohn Marino *
701e196c8SJohn Marino * Redistribution and use in source and binary forms, with or without
801e196c8SJohn Marino * modification, are permitted provided that the following conditions
901e196c8SJohn Marino * are met:
1001e196c8SJohn Marino * 1. Redistributions of source code must retain the above copyright
1101e196c8SJohn Marino * notice, this list of conditions and the following disclaimer.
1201e196c8SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1301e196c8SJohn Marino * notice, this list of conditions and the following disclaimer in the
1401e196c8SJohn Marino * documentation and/or other materials provided with the distribution.
1501e196c8SJohn Marino *
1601e196c8SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1701e196c8SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1801e196c8SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1901e196c8SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2001e196c8SJohn Marino * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2101e196c8SJohn Marino * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2201e196c8SJohn Marino * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2301e196c8SJohn Marino * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2401e196c8SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2501e196c8SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2601e196c8SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
2701e196c8SJohn Marino */
2801e196c8SJohn Marino
2901e196c8SJohn Marino #include <errno.h>
3001e196c8SJohn Marino
3101e196c8SJohn Marino #include "make.h"
3201e196c8SJohn Marino
33*6eef5f0cSAntonio Huete Jimenez MAKE_RCSID("$NetBSD: make_malloc.c,v 1.26 2022/01/07 08:30:04 rillig Exp $");
3401e196c8SJohn Marino
35a34d5fb1SAntonio Huete Jimenez #ifndef USE_EMALLOC
36a34d5fb1SAntonio Huete Jimenez
37a34d5fb1SAntonio Huete Jimenez /* die when out of memory. */
38ca58f742SDaniel Fojt static MAKE_ATTR_DEAD void
enomem(void)3901e196c8SJohn Marino enomem(void)
4001e196c8SJohn Marino {
4101e196c8SJohn Marino (void)fprintf(stderr, "%s: %s.\n", progname, strerror(ENOMEM));
4201e196c8SJohn Marino exit(2);
4301e196c8SJohn Marino }
4401e196c8SJohn Marino
45a34d5fb1SAntonio Huete Jimenez /* malloc, but die on error. */
4601e196c8SJohn Marino void *
bmake_malloc(size_t len)4701e196c8SJohn Marino bmake_malloc(size_t len)
4801e196c8SJohn Marino {
4901e196c8SJohn Marino void *p;
5001e196c8SJohn Marino
5101e196c8SJohn Marino if ((p = malloc(len)) == NULL)
5201e196c8SJohn Marino enomem();
53ca58f742SDaniel Fojt return p;
5401e196c8SJohn Marino }
5501e196c8SJohn Marino
56a34d5fb1SAntonio Huete Jimenez /* strdup, but die on error. */
5701e196c8SJohn Marino char *
bmake_strdup(const char * str)5801e196c8SJohn Marino bmake_strdup(const char *str)
5901e196c8SJohn Marino {
60*6eef5f0cSAntonio Huete Jimenez size_t size;
6101e196c8SJohn Marino char *p;
6201e196c8SJohn Marino
63*6eef5f0cSAntonio Huete Jimenez size = strlen(str) + 1;
64*6eef5f0cSAntonio Huete Jimenez p = bmake_malloc(size);
65*6eef5f0cSAntonio Huete Jimenez return memcpy(p, str, size);
6601e196c8SJohn Marino }
6701e196c8SJohn Marino
68a34d5fb1SAntonio Huete Jimenez /* Allocate a string starting from str with exactly len characters. */
6901e196c8SJohn Marino char *
bmake_strldup(const char * str,size_t len)70a34d5fb1SAntonio Huete Jimenez bmake_strldup(const char *str, size_t len)
7101e196c8SJohn Marino {
72a34d5fb1SAntonio Huete Jimenez char *p = bmake_malloc(len + 1);
7301e196c8SJohn Marino memcpy(p, str, len);
7401e196c8SJohn Marino p[len] = '\0';
75ca58f742SDaniel Fojt return p;
7601e196c8SJohn Marino }
7701e196c8SJohn Marino
78a34d5fb1SAntonio Huete Jimenez /* realloc, but die on error. */
7901e196c8SJohn Marino void *
bmake_realloc(void * ptr,size_t size)8001e196c8SJohn Marino bmake_realloc(void *ptr, size_t size)
8101e196c8SJohn Marino {
8201e196c8SJohn Marino if ((ptr = realloc(ptr, size)) == NULL)
8301e196c8SJohn Marino enomem();
84ca58f742SDaniel Fojt return ptr;
8501e196c8SJohn Marino }
8601e196c8SJohn Marino #endif
87a34d5fb1SAntonio Huete Jimenez
88a34d5fb1SAntonio Huete Jimenez /* Allocate a string from start up to but excluding end. */
89a34d5fb1SAntonio Huete Jimenez char *
bmake_strsedup(const char * start,const char * end)90a34d5fb1SAntonio Huete Jimenez bmake_strsedup(const char *start, const char *end)
91a34d5fb1SAntonio Huete Jimenez {
92a34d5fb1SAntonio Huete Jimenez return bmake_strldup(start, (size_t)(end - start));
93a34d5fb1SAntonio Huete Jimenez }
94