Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): make API of Buf_Init simpler



details:   https://anonhg.NetBSD.org/src/rev/840b10874be1
branches:  trunk
changeset: 1016027:840b10874be1
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Nov 07 14:11:58 2020 +0000

description:
make(1): make API of Buf_Init simpler

In most cases, the caller doesn't want to specify the exact number of
preallocated bytes.

diffstat:

 usr.bin/make/buf.c  |  17 ++++++++++-------
 usr.bin/make/buf.h  |   5 +++--
 usr.bin/make/cond.c |   8 ++++----
 usr.bin/make/dir.c  |   6 +++---
 usr.bin/make/for.c  |   8 ++++----
 usr.bin/make/main.c |   8 ++++----
 usr.bin/make/var.c  |  24 ++++++++++++------------
 7 files changed, 40 insertions(+), 36 deletions(-)

diffs (truncated from 304 to 300 lines):

diff -r e263ce6542dc -r 840b10874be1 usr.bin/make/buf.c
--- a/usr.bin/make/buf.c        Sat Nov 07 14:04:49 2020 +0000
+++ b/usr.bin/make/buf.c        Sat Nov 07 14:11:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: buf.c,v 1.43 2020/11/07 10:16:18 rillig Exp $  */
+/*     $NetBSD: buf.c,v 1.44 2020/11/07 14:11:58 rillig Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -75,7 +75,7 @@
 #include "make.h"
 
 /*     "@(#)buf.c      8.1 (Berkeley) 6/6/93"  */
-MAKE_RCSID("$NetBSD: buf.c,v 1.43 2020/11/07 10:16:18 rillig Exp $");
+MAKE_RCSID("$NetBSD: buf.c,v 1.44 2020/11/07 14:11:58 rillig Exp $");
 
 /* Make space in the buffer for adding a single byte. */
 void
@@ -155,19 +155,22 @@
     buf->data[0] = '\0';
 }
 
-/* Initialize a buffer.
- * If the given initial capacity is 0, a reasonable default is used. */
+/* Initialize a buffer. */
 void
-Buf_Init(Buffer *buf, size_t cap)
+Buf_InitSize(Buffer *buf, size_t cap)
 {
-    if (cap == 0)
-       cap = 256;
     buf->cap = cap;
     buf->len = 0;
     buf->data = bmake_malloc(cap);
     buf->data[0] = '\0';
 }
 
+void
+Buf_Init(Buffer *buf)
+{
+    Buf_InitSize(buf, 256);
+}
+
 /* Reset the buffer.
  * If freeData is TRUE, the data from the buffer is freed as well.
  * Otherwise it is kept and returned. */
diff -r e263ce6542dc -r 840b10874be1 usr.bin/make/buf.h
--- a/usr.bin/make/buf.h        Sat Nov 07 14:04:49 2020 +0000
+++ b/usr.bin/make/buf.h        Sat Nov 07 14:11:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: buf.h,v 1.34 2020/09/27 16:59:02 rillig Exp $  */
+/*     $NetBSD: buf.h,v 1.35 2020/11/07 14:11:58 rillig Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -124,7 +124,8 @@
 void Buf_AddInt(Buffer *, int);
 char *Buf_GetAll(Buffer *, size_t *);
 void Buf_Empty(Buffer *);
-void Buf_Init(Buffer *, size_t);
+void Buf_Init(Buffer *);
+void Buf_InitSize(Buffer *, size_t);
 char *Buf_Destroy(Buffer *, Boolean);
 char *Buf_DestroyCompact(Buffer *);
 
diff -r e263ce6542dc -r 840b10874be1 usr.bin/make/cond.c
--- a/usr.bin/make/cond.c       Sat Nov 07 14:04:49 2020 +0000
+++ b/usr.bin/make/cond.c       Sat Nov 07 14:11:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cond.c,v 1.178 2020/11/07 10:16:18 rillig Exp $        */
+/*     $NetBSD: cond.c,v 1.179 2020/11/07 14:11:58 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -93,7 +93,7 @@
 #include "dir.h"
 
 /*     "@(#)cond.c     8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: cond.c,v 1.178 2020/11/07 10:16:18 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.179 2020/11/07 14:11:58 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
@@ -225,7 +225,7 @@
 
     cpp_skip_hspace(&p);
 
-    Buf_Init(&argBuf, 16);
+    Buf_InitSize(&argBuf, 16);
 
     paren_depth = 0;
     for (;;) {
@@ -398,7 +398,7 @@
     VarEvalFlags eflags;
     VarParseResult parseResult;
 
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
     str = NULL;
     *out_freeIt = NULL;
     *out_quoted = qt = par->p[0] == '"';
diff -r e263ce6542dc -r 840b10874be1 usr.bin/make/dir.c
--- a/usr.bin/make/dir.c        Sat Nov 07 14:04:49 2020 +0000
+++ b/usr.bin/make/dir.c        Sat Nov 07 14:11:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dir.c,v 1.196 2020/11/07 10:16:18 rillig Exp $ */
+/*     $NetBSD: dir.c,v 1.197 2020/11/07 14:11:58 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -134,7 +134,7 @@
 #include "job.h"
 
 /*     "@(#)dir.c      8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: dir.c,v 1.196 2020/11/07 10:16:18 rillig Exp $");
+MAKE_RCSID("$NetBSD: dir.c,v 1.197 2020/11/07 14:11:58 rillig Exp $");
 
 #define DIR_DEBUG0(text) DEBUG0(DIR, text)
 #define DIR_DEBUG1(fmt, arg1) DEBUG1(DIR, fmt, arg1)
@@ -1480,7 +1480,7 @@
     Buffer buf;
     SearchPathNode *ln;
 
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
 
     if (path != NULL) {
        for (ln = path->first; ln != NULL; ln = ln->next) {
diff -r e263ce6542dc -r 840b10874be1 usr.bin/make/for.c
--- a/usr.bin/make/for.c        Sat Nov 07 14:04:49 2020 +0000
+++ b/usr.bin/make/for.c        Sat Nov 07 14:11:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: for.c,v 1.113 2020/11/07 10:16:18 rillig Exp $ */
+/*     $NetBSD: for.c,v 1.114 2020/11/07 14:11:58 rillig Exp $ */
 
 /*
  * Copyright (c) 1992, The Regents of the University of California.
@@ -60,7 +60,7 @@
 #include "make.h"
 
 /*     "@(#)for.c      8.1 (Berkeley) 6/6/93"  */
-MAKE_RCSID("$NetBSD: for.c,v 1.113 2020/11/07 10:16:18 rillig Exp $");
+MAKE_RCSID("$NetBSD: for.c,v 1.114 2020/11/07 14:11:58 rillig Exp $");
 
 /* The .for loop substitutes the items as ${:U<value>...}, which means
  * that characters that break this syntax must be backslash-escaped. */
@@ -191,11 +191,11 @@
      */
 
     f = bmake_malloc(sizeof *f);
-    Buf_Init(&f->body, 0);
+    Buf_Init(&f->body);
     Vector_Init(&f->vars, sizeof(ForVar));
     f->items.words = NULL;
     f->items.freeIt = NULL;
-    Buf_Init(&f->curBody, 0);
+    Buf_Init(&f->curBody);
     f->short_var = FALSE;
     f->sub_next = 0;
 
diff -r e263ce6542dc -r 840b10874be1 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Sat Nov 07 14:04:49 2020 +0000
+++ b/usr.bin/make/main.c       Sat Nov 07 14:11:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.434 2020/11/07 10:25:28 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.435 2020/11/07 14:11:58 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -118,7 +118,7 @@
 #include "trace.h"
 
 /*     "@(#)main.c     8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: main.c,v 1.434 2020/11/07 10:25:28 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.435 2020/11/07 14:11:58 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
            "The Regents of the University of California.  "
@@ -1768,7 +1768,7 @@
        (void)close(fds[1]);    /* No need for the writing half */
 
        savederr = 0;
-       Buf_Init(&buf, 0);
+       Buf_Init(&buf);
 
        do {
            char result[BUFSIZ];
@@ -1950,7 +1950,7 @@
 {
        Buffer buf;
 
-       Buf_Init(&buf, 0);
+       Buf_Init(&buf);
        Buf_AddStr(&buf, progname);
        Buf_AddStr(&buf, ": ");
        Buf_AddStr(&buf, af);
diff -r e263ce6542dc -r 840b10874be1 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sat Nov 07 14:04:49 2020 +0000
+++ b/usr.bin/make/var.c        Sat Nov 07 14:11:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.672 2020/11/07 10:16:19 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.673 2020/11/07 14:11:58 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -130,7 +130,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.672 2020/11/07 10:16:19 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.673 2020/11/07 14:11:58 rillig Exp $");
 
 #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
 #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -287,7 +287,7 @@
     Var *var = bmake_malloc(sizeof *var);
     var->name = name;
     var->name_freeIt = name_freeIt;
-    Buf_Init(&var->val, value_len + 1);
+    Buf_InitSize(&var->val, value_len + 1);
     Buf_AddBytes(&var->val, value, value_len);
     var->flags = flags;
     return var;
@@ -1048,7 +1048,7 @@
 static void
 SepBuf_Init(SepBuf *buf, char sep)
 {
-    Buf_Init(&buf->buf, 32 /* bytes */);
+    Buf_InitSize(&buf->buf, 32);
     buf->needSep = FALSE;
     buf->sep = sep;
 }
@@ -1596,7 +1596,7 @@
     Buffer buf;
     size_t i;
 
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
 
     for (i = 0; i < words.len; i++) {
        if (i != 0)
@@ -1633,7 +1633,7 @@
 VarQuote(const char *str, Boolean quoteDollar)
 {
     Buffer buf;
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
 
     for (; *str != '\0'; str++) {
        if (*str == '\n') {
@@ -1883,7 +1883,7 @@
     Buffer buf;
     const char *p;
 
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
 
     /*
      * Skim through until the matching delimiter is found; pick up variable
@@ -2115,7 +2115,7 @@
            eflags |= VARE_WANTRES;
     }
 
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
     p = *pp + 1;
     while (*p != st->endc && *p != ':' && *p != '\0') {
 
@@ -2335,7 +2335,7 @@
        Words_Free(words);
     }
 
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
 
     for (i = 0; i < n; i++) {
        if (i != 0)
@@ -2741,7 +2741,7 @@
            size_t ac = words.len;
            Words_Free(words);
 
-           Buf_Init(&buf, 4);  /* 3 digits + '\0' is usually enough */
+           Buf_InitSize(&buf, 4);      /* 3 digits + '\0' is usually enough */
            Buf_AddInt(&buf, (int)ac);
            st->newVal = Buf_Destroy(&buf, FALSE);
        }
@@ -3507,7 +3507,7 @@
     const char *p = *pp;
     int depth = 1;
 
-    Buf_Init(&buf, 0);
+    Buf_Init(&buf);
 
     while (*p != '\0') {
        /* Track depth so we can spot parse errors. */
@@ -3959,7 +3959,7 @@
      * to prevent a plethora of messages when recursing */
     static Boolean errorReported;
 
-    Buf_Init(&buf, 0);



Home | Main Index | Thread Index | Old Index