xref: /netbsd-src/usr.bin/make/make_malloc.c (revision 399f66b1c1402d5bbc162d34b16002a9d605a165)
1*399f66b1Srillig /*	$NetBSD: make_malloc.c,v 1.26 2022/01/07 08:30:04 rillig Exp $	*/
24fb693c1Sdsl 
385aee7a6Srillig /*
44fb693c1Sdsl  * Copyright (c) 2009 The NetBSD Foundation, Inc.
54fb693c1Sdsl  * All rights reserved.
64fb693c1Sdsl  *
74fb693c1Sdsl  * Redistribution and use in source and binary forms, with or without
84fb693c1Sdsl  * modification, are permitted provided that the following conditions
94fb693c1Sdsl  * are met:
104fb693c1Sdsl  * 1. Redistributions of source code must retain the above copyright
114fb693c1Sdsl  *    notice, this list of conditions and the following disclaimer.
124fb693c1Sdsl  * 2. Redistributions in binary form must reproduce the above copyright
134fb693c1Sdsl  *    notice, this list of conditions and the following disclaimer in the
144fb693c1Sdsl  *    documentation and/or other materials provided with the distribution.
154fb693c1Sdsl  *
164fb693c1Sdsl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
174fb693c1Sdsl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
184fb693c1Sdsl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
194fb693c1Sdsl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
204fb693c1Sdsl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
214fb693c1Sdsl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
224fb693c1Sdsl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
234fb693c1Sdsl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
244fb693c1Sdsl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
254fb693c1Sdsl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
264fb693c1Sdsl  * POSSIBILITY OF SUCH DAMAGE.
274fb693c1Sdsl  */
284fb693c1Sdsl 
299fde1a67Scegger #include <errno.h>
304fb693c1Sdsl 
31b1e1b1d4Sjoerg #include "make.h"
3297347d2cSdsl 
33*399f66b1Srillig MAKE_RCSID("$NetBSD: make_malloc.c,v 1.26 2022/01/07 08:30:04 rillig Exp $");
346de75a95Srillig 
354fb693c1Sdsl #ifndef USE_EMALLOC
368e7413b1Swiz 
3709d92712Srillig /* die when out of memory. */
383e81c534Sdholland static MAKE_ATTR_DEAD void
enomem(void)394fb693c1Sdsl enomem(void)
404fb693c1Sdsl {
41f8d6aa2cSdholland 	(void)fprintf(stderr, "%s: %s.\n", progname, strerror(ENOMEM));
424fb693c1Sdsl 	exit(2);
434fb693c1Sdsl }
444fb693c1Sdsl 
4509d92712Srillig /* malloc, but die on error. */
464fb693c1Sdsl void *
bmake_malloc(size_t len)474fb693c1Sdsl bmake_malloc(size_t len)
484fb693c1Sdsl {
494fb693c1Sdsl 	void *p;
504fb693c1Sdsl 
514fb693c1Sdsl 	if ((p = malloc(len)) == NULL)
524fb693c1Sdsl 		enomem();
5344d841d2Srillig 	return p;
544fb693c1Sdsl }
554fb693c1Sdsl 
5609d92712Srillig /* strdup, but die on error. */
574fb693c1Sdsl char *
bmake_strdup(const char * str)584fb693c1Sdsl bmake_strdup(const char *str)
594fb693c1Sdsl {
60*399f66b1Srillig 	size_t size;
614fb693c1Sdsl 	char *p;
624fb693c1Sdsl 
63*399f66b1Srillig 	size = strlen(str) + 1;
64*399f66b1Srillig 	p = bmake_malloc(size);
65*399f66b1Srillig 	return memcpy(p, str, size);
664fb693c1Sdsl }
674fb693c1Sdsl 
680cbd5c0eSrillig /* Allocate a string starting from str with exactly len characters. */
694fb693c1Sdsl char *
bmake_strldup(const char * str,size_t len)700cbd5c0eSrillig bmake_strldup(const char *str, size_t len)
714fb693c1Sdsl {
720cbd5c0eSrillig 	char *p = bmake_malloc(len + 1);
734fb693c1Sdsl 	memcpy(p, str, len);
744fb693c1Sdsl 	p[len] = '\0';
7544d841d2Srillig 	return p;
764fb693c1Sdsl }
774fb693c1Sdsl 
7809d92712Srillig /* realloc, but die on error. */
794fb693c1Sdsl void *
bmake_realloc(void * ptr,size_t size)804fb693c1Sdsl bmake_realloc(void *ptr, size_t size)
814fb693c1Sdsl {
824fb693c1Sdsl 	if ((ptr = realloc(ptr, size)) == NULL)
834fb693c1Sdsl 		enomem();
8444d841d2Srillig 	return ptr;
854fb693c1Sdsl }
864fb693c1Sdsl #endif
8764e0a331Srillig 
8864e0a331Srillig /* Allocate a string from start up to but excluding end. */
8964e0a331Srillig char *
bmake_strsedup(const char * start,const char * end)9064e0a331Srillig bmake_strsedup(const char *start, const char *end)
9164e0a331Srillig {
9264e0a331Srillig 	return bmake_strldup(start, (size_t)(end - start));
9364e0a331Srillig }
94