1*2bc7c627SLionel Sambuc /* $NetBSD: make_malloc.c,v 1.10 2012/06/20 17:46:28 sjg Exp $ */
22e2caf59SThomas Veerman
32e2caf59SThomas Veerman /*-
42e2caf59SThomas Veerman * Copyright (c) 2009 The NetBSD Foundation, Inc.
52e2caf59SThomas Veerman * All rights reserved.
62e2caf59SThomas Veerman *
72e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without
82e2caf59SThomas Veerman * modification, are permitted provided that the following conditions
92e2caf59SThomas Veerman * are met:
102e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright
112e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer.
122e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
132e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the
142e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution.
152e2caf59SThomas Veerman *
162e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172e2caf59SThomas Veerman * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182e2caf59SThomas Veerman * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192e2caf59SThomas Veerman * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202e2caf59SThomas Veerman * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212e2caf59SThomas Veerman * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222e2caf59SThomas Veerman * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232e2caf59SThomas Veerman * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242e2caf59SThomas Veerman * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252e2caf59SThomas Veerman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262e2caf59SThomas Veerman * POSSIBILITY OF SUCH DAMAGE.
272e2caf59SThomas Veerman */
282e2caf59SThomas Veerman
292e2caf59SThomas Veerman #ifdef MAKE_NATIVE
302e2caf59SThomas Veerman #include <sys/cdefs.h>
31*2bc7c627SLionel Sambuc __RCSID("$NetBSD: make_malloc.c,v 1.10 2012/06/20 17:46:28 sjg Exp $");
322e2caf59SThomas Veerman #endif
332e2caf59SThomas Veerman
342e2caf59SThomas Veerman #include <stdio.h>
352e2caf59SThomas Veerman #include <stdlib.h>
362e2caf59SThomas Veerman #include <string.h>
372e2caf59SThomas Veerman #include <errno.h>
382e2caf59SThomas Veerman
39*2bc7c627SLionel Sambuc #include "make.h"
402e2caf59SThomas Veerman
412e2caf59SThomas Veerman #ifndef USE_EMALLOC
42*2bc7c627SLionel Sambuc static void enomem(void) MAKE_ATTR_DEAD;
43*2bc7c627SLionel Sambuc
442e2caf59SThomas Veerman /*
452e2caf59SThomas Veerman * enomem --
462e2caf59SThomas Veerman * die when out of memory.
472e2caf59SThomas Veerman */
482e2caf59SThomas Veerman static void
enomem(void)492e2caf59SThomas Veerman enomem(void)
502e2caf59SThomas Veerman {
512e2caf59SThomas Veerman (void)fprintf(stderr, "%s: %s.\n", progname, strerror(ENOMEM));
522e2caf59SThomas Veerman exit(2);
532e2caf59SThomas Veerman }
542e2caf59SThomas Veerman
552e2caf59SThomas Veerman /*
562e2caf59SThomas Veerman * bmake_malloc --
572e2caf59SThomas Veerman * malloc, but die on error.
582e2caf59SThomas Veerman */
592e2caf59SThomas Veerman void *
bmake_malloc(size_t len)602e2caf59SThomas Veerman bmake_malloc(size_t len)
612e2caf59SThomas Veerman {
622e2caf59SThomas Veerman void *p;
632e2caf59SThomas Veerman
642e2caf59SThomas Veerman if ((p = malloc(len)) == NULL)
652e2caf59SThomas Veerman enomem();
662e2caf59SThomas Veerman return(p);
672e2caf59SThomas Veerman }
682e2caf59SThomas Veerman
692e2caf59SThomas Veerman /*
702e2caf59SThomas Veerman * bmake_strdup --
712e2caf59SThomas Veerman * strdup, but die on error.
722e2caf59SThomas Veerman */
732e2caf59SThomas Veerman char *
bmake_strdup(const char * str)742e2caf59SThomas Veerman bmake_strdup(const char *str)
752e2caf59SThomas Veerman {
762e2caf59SThomas Veerman size_t len;
772e2caf59SThomas Veerman char *p;
782e2caf59SThomas Veerman
792e2caf59SThomas Veerman len = strlen(str) + 1;
802e2caf59SThomas Veerman if ((p = malloc(len)) == NULL)
812e2caf59SThomas Veerman enomem();
822e2caf59SThomas Veerman return memcpy(p, str, len);
832e2caf59SThomas Veerman }
842e2caf59SThomas Veerman
852e2caf59SThomas Veerman /*
862e2caf59SThomas Veerman * bmake_strndup --
872e2caf59SThomas Veerman * strndup, but die on error.
882e2caf59SThomas Veerman */
892e2caf59SThomas Veerman char *
bmake_strndup(const char * str,size_t max_len)902e2caf59SThomas Veerman bmake_strndup(const char *str, size_t max_len)
912e2caf59SThomas Veerman {
922e2caf59SThomas Veerman size_t len;
932e2caf59SThomas Veerman char *p;
942e2caf59SThomas Veerman
952e2caf59SThomas Veerman if (str == NULL)
962e2caf59SThomas Veerman return NULL;
972e2caf59SThomas Veerman
982e2caf59SThomas Veerman len = strlen(str);
992e2caf59SThomas Veerman if (len > max_len)
1002e2caf59SThomas Veerman len = max_len;
1012e2caf59SThomas Veerman p = bmake_malloc(len + 1);
1022e2caf59SThomas Veerman memcpy(p, str, len);
1032e2caf59SThomas Veerman p[len] = '\0';
1042e2caf59SThomas Veerman
1052e2caf59SThomas Veerman return(p);
1062e2caf59SThomas Veerman }
1072e2caf59SThomas Veerman
1082e2caf59SThomas Veerman /*
1092e2caf59SThomas Veerman * bmake_realloc --
1102e2caf59SThomas Veerman * realloc, but die on error.
1112e2caf59SThomas Veerman */
1122e2caf59SThomas Veerman void *
bmake_realloc(void * ptr,size_t size)1132e2caf59SThomas Veerman bmake_realloc(void *ptr, size_t size)
1142e2caf59SThomas Veerman {
1152e2caf59SThomas Veerman if ((ptr = realloc(ptr, size)) == NULL)
1162e2caf59SThomas Veerman enomem();
1172e2caf59SThomas Veerman return(ptr);
1182e2caf59SThomas Veerman }
1192e2caf59SThomas Veerman #endif
120