Move the bmake_malloc() functions into their own .c and .h files.

Include instead of make.h in a few places.
This commit is contained in:
dsl 2009-01-24 11:59:39 +00:00
parent 5d04927c08
commit 4fb693c12c
9 changed files with 172 additions and 108 deletions

View File

@ -1,10 +1,11 @@
# $NetBSD: Makefile,v 1.45 2008/12/20 22:41:53 dsl Exp $
# $NetBSD: Makefile,v 1.46 2009/01/24 11:59:39 dsl Exp $
# @(#)Makefile 5.2 (Berkeley) 12/28/90
PROG= make
SRCS= arch.c buf.c compat.c cond.c dir.c for.c hash.c job.c main.c \
make.c parse.c str.c suff.c targ.c trace.c var.c util.c
SRCS+= strlist.c
SRCS+= make_malloc.c
SRCS+= lstAppend.c lstAtEnd.c lstAtFront.c lstClose.c lstConcat.c \
lstDatum.c lstDeQueue.c lstDestroy.c lstDupl.c lstEnQueue.c \
lstFind.c lstFindFrom.c lstFirst.c lstForEach.c lstForEachFrom.c \

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.boot,v 1.18 2008/12/20 22:41:53 dsl Exp $
# $NetBSD: Makefile.boot,v 1.19 2009/01/24 11:59:39 dsl Exp $
#
# a very simple makefile...
#
@ -22,7 +22,7 @@ CFLAGS= -DTARGET_MACHINE=\"${MACHINE}\" \
LIBS=
OBJ=arch.o buf.o compat.o cond.o dir.o for.o hash.o job.o main.o make.o \
parse.o str.o strlist.o suff.o targ.o trace.o var.o util.o
make_malloc.o parse.o str.o strlist.o suff.o targ.o trace.o var.o util.o
LIBOBJ= lst.lib/lstAppend.o lst.lib/lstAtEnd.o lst.lib/lstAtFront.o \
lst.lib/lstClose.o lst.lib/lstConcat.o lst.lib/lstDatum.o \

View File

@ -1,4 +1,4 @@
/* $NetBSD: lstInt.h,v 1.18 2009/01/23 21:26:30 dsl Exp $ */
/* $NetBSD: lstInt.h,v 1.19 2009/01/24 11:59:39 dsl Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -41,7 +41,7 @@
#ifndef _LSTINT_H_
#define _LSTINT_H_
#include "../make.h"
#include "../make_malloc.h"
#include "../lst.h"
typedef struct ListNode {

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.165 2009/01/23 21:58:27 dsl Exp $ */
/* $NetBSD: main.c,v 1.166 2009/01/24 11:59:39 dsl Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,7 +69,7 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: main.c,v 1.165 2009/01/23 21:58:27 dsl Exp $";
static char rcsid[] = "$NetBSD: main.c,v 1.166 2009/01/24 11:59:39 dsl Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\
#if 0
static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: main.c,v 1.165 2009/01/23 21:58:27 dsl Exp $");
__RCSID("$NetBSD: main.c,v 1.166 2009/01/24 11:59:39 dsl Exp $");
#endif
#endif /* not lint */
#endif
@ -1695,84 +1695,6 @@ Finish(int errors)
Fatal("%d error%s", errors, errors == 1 ? "" : "s");
}
#ifndef USE_EMALLOC
/*
* enomem --
* die when out of memory.
*/
static void
enomem(void)
{
(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
exit(2);
}
/*
* bmake_malloc --
* malloc, but die on error.
*/
void *
bmake_malloc(size_t len)
{
void *p;
if ((p = malloc(len)) == NULL)
enomem();
return(p);
}
/*
* bmake_strdup --
* strdup, but die on error.
*/
char *
bmake_strdup(const char *str)
{
size_t len;
char *p;
len = strlen(str) + 1;
if ((p = malloc(len)) == NULL)
enomem();
return memcpy(p, str, len);
}
/*
* bmake_strndup --
* strndup, but die on error.
*/
char *
bmake_strndup(const char *str, size_t max_len)
{
size_t len;
char *p;
if (str == NULL)
return NULL;
len = strlen(str);
if (len > max_len)
len = max_len;
p = bmake_malloc(len + 1);
memcpy(p, str, len);
p[len] = '\0';
return(p);
}
/*
* bmake_realloc --
* realloc, but die on error.
*/
void *
bmake_realloc(void *ptr, size_t size)
{
if ((ptr = realloc(ptr, size)) == NULL)
enomem();
return(ptr);
}
#endif
/*
* enunlink --
* Remove a file carefully, avoiding directories.

View File

@ -1,4 +1,4 @@
/* $NetBSD: make.h,v 1.76 2008/12/13 15:19:29 dsl Exp $ */
/* $NetBSD: make.h,v 1.77 2009/01/24 11:59:39 dsl Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -428,11 +428,8 @@ extern int debug;
#define DEBUG(module) (debug & CONCAT(DEBUG_,module))
/*
* Since there are so many, all functions that return non-integer values are
* extracted by means of a sed script or two and stuck in the file "nonints.h"
*/
#include "nonints.h"
#include "make_malloc.h"
int Make_TimeStamp(GNode *, GNode *);
Boolean Make_OODate(GNode *);

112
usr.bin/make/make_malloc.c Normal file
View File

@ -0,0 +1,112 @@
/* $NetBSD: make_malloc.c,v 1.1 2009/01/24 11:59:39 dsl Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: make_malloc.c,v 1.1 2009/01/24 11:59:39 dsl Exp $");
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef USE_EMALLOC
/*
* enomem --
* die when out of memory.
*/
static void
enomem(void)
{
(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
exit(2);
}
/*
* bmake_malloc --
* malloc, but die on error.
*/
void *
bmake_malloc(size_t len)
{
void *p;
if ((p = malloc(len)) == NULL)
enomem();
return(p);
}
/*
* bmake_strdup --
* strdup, but die on error.
*/
char *
bmake_strdup(const char *str)
{
size_t len;
char *p;
len = strlen(str) + 1;
if ((p = malloc(len)) == NULL)
enomem();
return memcpy(p, str, len);
}
/*
* bmake_strndup --
* strndup, but die on error.
*/
char *
bmake_strndup(const char *str, size_t max_len)
{
size_t len;
char *p;
if (str == NULL)
return NULL;
len = strlen(str);
if (len > max_len)
len = max_len;
p = bmake_malloc(len + 1);
memcpy(p, str, len);
p[len] = '\0';
return(p);
}
/*
* bmake_realloc --
* realloc, but die on error.
*/
void *
bmake_realloc(void *ptr, size_t size)
{
if ((ptr = realloc(ptr, size)) == NULL)
enomem();
return(ptr);
}
#endif

View File

@ -0,0 +1,44 @@
/* $NetBSD: make_malloc.h,v 1.1 2009/01/24 11:59:39 dsl Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: make_malloc.h,v 1.1 2009/01/24 11:59:39 dsl Exp $");
#ifndef USE_EMALLOC
void *bmake_malloc(size_t);
void *bmake_realloc(void *, size_t);
char *bmake_strdup(const char *);
char *bmake_strndup(const char *, size_t);
#else
#include <util.h>
#define bmake_malloc(x) emalloc(x)
#define bmake_realloc(x,y) erealloc(x,y)
#define bmake_strdup(x) estrdup(x)
#define bmake_strndup(x,y) estrndup(x,y)
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: nonints.h,v 1.54 2009/01/23 21:26:30 dsl Exp $ */
/* $NetBSD: nonints.h,v 1.55 2009/01/24 11:59:39 dsl Exp $ */
/*-
* Copyright (c) 1988, 1989, 1990, 1993
@ -117,18 +117,6 @@ void Punt(const char *, ...)
void DieHorribly(void) __attribute__((__noreturn__));
int PrintAddr(void *, void *);
void Finish(int);
#ifndef USE_EMALLOC
void *bmake_malloc(size_t);
void *bmake_realloc(void *, size_t);
char *bmake_strdup(const char *);
char *bmake_strndup(const char *, size_t);
#else
#include <util.h>
#define bmake_malloc(x) emalloc(x)
#define bmake_realloc(x,y) erealloc(x,y)
#define bmake_strdup(x) estrdup(x)
#define bmake_strndup(x,y) estrndup(x,y)
#endif
int eunlink(const char *);
void execError(const char *, const char *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: strlist.c,v 1.3 2009/01/16 21:15:34 dsl Exp $ */
/* $NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $ */
/*-
* Copyright (c) 2008 - 2009 The NetBSD Foundation, Inc.
@ -33,18 +33,18 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: strlist.c,v 1.3 2009/01/16 21:15:34 dsl Exp $";
static char rcsid[] = "$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: strlist.c,v 1.3 2009/01/16 21:15:34 dsl Exp $");
__RCSID("$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $");
#endif /* not lint */
#endif
#include <stddef.h>
#include <stdlib.h>
#include "strlist.h"
#include "make.h"
#include "make_malloc.h"
void
strlist_init(strlist_t *sl)