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): replace PtrVector with Vector, which c...



details:   https://anonhg.NetBSD.org/src/rev/45ece9b99300
branches:  trunk
changeset: 977492:45ece9b99300
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Oct 25 13:06:12 2020 +0000

description:
make(1): replace PtrVector with Vector, which can contain any type

diffstat:

 usr.bin/make/lst.c   |  49 +++++++++++++++++++++++++++----------------------
 usr.bin/make/lst.h   |  27 ++++++++++++++-------------
 usr.bin/make/parse.c |  33 +++++++++++++++++++++------------
 usr.bin/make/var.c   |  26 ++++++++++++++------------
 4 files changed, 76 insertions(+), 59 deletions(-)

diffs (280 lines):

diff -r fcda270d6628 -r 45ece9b99300 usr.bin/make/lst.c
--- a/usr.bin/make/lst.c        Sun Oct 25 12:08:53 2020 +0000
+++ b/usr.bin/make/lst.c        Sun Oct 25 13:06:12 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lst.c,v 1.89 2020/10/25 12:08:53 rillig Exp $ */
+/* $NetBSD: lst.c,v 1.90 2020/10/25 13:06:12 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -34,7 +34,7 @@
 
 #include "make.h"
 
-MAKE_RCSID("$NetBSD: lst.c,v 1.89 2020/10/25 12:08:53 rillig Exp $");
+MAKE_RCSID("$NetBSD: lst.c,v 1.90 2020/10/25 13:06:12 rillig Exp $");
 
 static ListNode *
 LstNodeNew(ListNode *prev, ListNode *next, void *datum)
@@ -275,43 +275,48 @@
 }
 
 void
-PtrVector_Init(PtrVector *v)
+Vector_Init(Vector *v, size_t itemSize)
 {
     v->len = 0;
-    v->cap = 10;
-    v->items = bmake_malloc(v->cap * sizeof v->items[0]);
+    v->priv_cap = 10;
+    v->itemSize = itemSize;
+    v->items = bmake_malloc(v->priv_cap * v->itemSize);
 }
 
-Boolean PtrVector_IsEmpty(PtrVector *v)
+/* Return the pointer to the given item in the vector.
+ * The returned data is valid until the next modifying operation. */
+void *
+Vector_Get(Vector *v, size_t i)
 {
-    return v->len == 0;
+    unsigned char *items = v->items;
+    return items + i * v->itemSize;
 }
 
-void PtrVector_Push(PtrVector *v, void *datum)
+/* Add space for a new item to the vector and return a pointer to that space.
+ * The returned data is valid until the next modifying operation. */
+void *
+Vector_Push(Vector *v)
 {
-    if (v->len >= v->cap) {
-       v->cap *= 2;
-       v->items = bmake_realloc(v->items,
-                                v->cap * sizeof v->items[0]);
+    if (v->len >= v->priv_cap) {
+       v->priv_cap *= 2;
+       v->items = bmake_realloc(v->items, v->priv_cap * v->itemSize);
     }
-    v->items[v->len] = datum;
     v->len++;
+    return Vector_Get(v, v->len - 1);
 }
 
-void *PtrVector_Pop(PtrVector *v)
+/* Return the pointer to the last item in the vector.
+ * The returned data is valid until the next modifying operation. */
+void *
+Vector_Pop(Vector *v)
 {
-    void *datum;
-
     assert(v->len > 0);
     v->len--;
-    datum = v->items[v->len];
-#ifdef CLEANUP
-    v->items[v->len] = NULL;
-#endif
-    return datum;
+    return Vector_Get(v, v->len);
 }
 
-void PtrVector_Done(PtrVector *v)
+void
+Vector_Done(Vector *v)
 {
     free(v->items);
 }
diff -r fcda270d6628 -r 45ece9b99300 usr.bin/make/lst.h
--- a/usr.bin/make/lst.h        Sun Oct 25 12:08:53 2020 +0000
+++ b/usr.bin/make/lst.h        Sun Oct 25 13:06:12 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lst.h,v 1.81 2020/10/25 12:08:53 rillig Exp $  */
+/*     $NetBSD: lst.h,v 1.82 2020/10/25 13:06:12 rillig Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -163,18 +163,19 @@
 /* Remove the head node of the queue and return its datum. */
 void *Lst_Dequeue(List *);
 
-/* A pointer vector is an ordered collection of pointers, allowing for fast
- * indexed access. */
-typedef struct PtrVector {
-    void **items;
-    size_t len;
-    size_t cap;
-} PtrVector;
+/* A vector is an ordered collection of items, allowing for fast indexed
+ * access. */
+typedef struct Vector {
+    void *items;               /* memory holding the items */
+    size_t itemSize;           /* size of a single item in bytes */
+    size_t len;                        /* number of actually usable elements */
+    size_t priv_cap;           /* capacity */
+} Vector;
 
-void PtrVector_Init(PtrVector *);
-Boolean PtrVector_IsEmpty(PtrVector *);
-void PtrVector_Push(PtrVector *, void *);
-void *PtrVector_Pop(PtrVector *);
-void PtrVector_Done(PtrVector *);
+void Vector_Init(Vector *, size_t);
+void *Vector_Get(Vector *, size_t);
+void *Vector_Push(Vector *);
+void *Vector_Pop(Vector *);
+void Vector_Done(Vector *);
 
 #endif /* MAKE_LST_H */
diff -r fcda270d6628 -r 45ece9b99300 usr.bin/make/parse.c
--- a/usr.bin/make/parse.c      Sun Oct 25 12:08:53 2020 +0000
+++ b/usr.bin/make/parse.c      Sun Oct 25 13:06:12 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.399 2020/10/25 12:08:53 rillig Exp $       */
+/*     $NetBSD: parse.c,v 1.400 2020/10/25 13:06:12 rillig Exp $       */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "pathnames.h"
 
 /*     "@(#)parse.c    8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: parse.c,v 1.399 2020/10/25 12:08:53 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.400 2020/10/25 13:06:12 rillig Exp $");
 
 /* types and constants */
 
@@ -281,7 +281,13 @@
  *                     (not printed since it is below a .for loop)
  *     includes[0]:    include-main.mk:27
  */
-static PtrVector /* of IFile pointer */ includes;
+static Vector /* of IFile pointer */ includes;
+
+static IFile *
+GetInclude(size_t i)
+{
+    return *((IFile **)Vector_Get(&includes, i));
+}
 
 /* include paths (lists of directories) */
 SearchPath *parseIncPath;      /* dirs for "..." includes */
@@ -2322,8 +2328,8 @@
     size_t i;
 
     for (i = includes.len; i > 0; i--) {
-       IFile *parent = includes.items[i - 1];
-       IFile *child = i < includes.len ? includes.items[i] : curFile;
+       IFile *parent = GetInclude(i - 1);
+       IFile *child = i < includes.len ? GetInclude(i) : curFile;
        if (!child->fromForLoop)
            return parent->fname;
     }
@@ -2402,9 +2408,11 @@
        /* sanity */
        return;
 
-    if (curFile != NULL)
+    if (curFile != NULL) {
        /* Save existing file info */
-       PtrVector_Push(&includes, curFile);
+       IFile **next = Vector_Push(&includes);
+       *next = curFile;
+    }
 
     /* Allocate and fill in new structure */
     curFile = bmake_malloc(sizeof *curFile);
@@ -2599,7 +2607,7 @@
     free(curFile->P_str);
     free(curFile);
 
-    if (PtrVector_IsEmpty(&includes)) {
+    if (includes.len == 0) {
        curFile = NULL;
        /* We've run out of input */
        Var_Delete(".PARSEDIR", VAR_GLOBAL);
@@ -2609,7 +2617,8 @@
        return FALSE;
     }
 
-    curFile = PtrVector_Pop(&includes);
+    curFile = GetInclude(includes.len - 1);
+    Vector_Pop(&includes);
     DEBUG2(PARSE, "ParseEOF: returning to file %s, line %d\n",
           curFile->fname, curFile->lineno);
 
@@ -3147,7 +3156,7 @@
     parseIncPath = Lst_New();
     sysIncPath = Lst_New();
     defIncPath = Lst_New();
-    PtrVector_Init(&includes);
+    Vector_Init(&includes, sizeof(IFile *));
 #ifdef CLEANUP
     targCmds = Lst_New();
 #endif
@@ -3163,8 +3172,8 @@
     Lst_Destroy(defIncPath, Dir_Destroy);
     Lst_Destroy(sysIncPath, Dir_Destroy);
     Lst_Destroy(parseIncPath, Dir_Destroy);
-    assert(PtrVector_IsEmpty(&includes));
-    PtrVector_Done(&includes);
+    assert(includes.len == 0);
+    Vector_Done(&includes);
 #endif
 }
 
diff -r fcda270d6628 -r 45ece9b99300 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Oct 25 12:08:53 2020 +0000
+++ b/usr.bin/make/var.c        Sun Oct 25 13:06:12 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.584 2020/10/25 12:08:53 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.585 2020/10/25 13:06:12 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -121,7 +121,7 @@
 #include    "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.584 2020/10/25 12:08:53 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.585 2020/10/25 13:06:12 rillig Exp $");
 
 #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
 #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -3865,24 +3865,26 @@
 void
 Var_Dump(GNode *ctxt)
 {
-    PtrVector varnames;
+    Vector /* of const char * */ vec;
     HashIter hi;
     HashEntry *he;
     size_t i;
-
-    PtrVector_Init(&varnames);
+    const char **varnames;
+
+    Vector_Init(&vec, sizeof(const char *));
 
     HashIter_Init(&hi, &ctxt->context);
     while ((he = HashIter_Next(&hi)) != NULL)
-       PtrVector_Push(&varnames, he->key);
-
-    qsort(varnames.items, varnames.len, sizeof varnames.items[0], str_cmp_asc);
-
-    for (i = 0; i < varnames.len; i++) {
-        const char *varname = varnames.items[i];
+       *(const char **)Vector_Push(&vec) = he->key;
+    varnames = vec.items;
+
+    qsort(varnames, vec.len, sizeof varnames[0], str_cmp_asc);
+
+    for (i = 0; i < vec.len; i++) {
+        const char *varname = varnames[i];
         Var *var = Hash_FindValue(&ctxt->context, varname);
        debug_printf("%-16s = %s\n", varname, Buf_GetAll(&var->val, NULL));
     }
 
-    PtrVector_Done(&varnames);
+    Vector_Done(&vec);
 }



Home | Main Index | Thread Index | Old Index