Module Name:    src
Committed By:   rillig
Date:           Sun Oct 25 13:06:12 UTC 2020

Modified Files:
        src/usr.bin/make: lst.c lst.h parse.c var.c

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


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/usr.bin/make/lst.c
cvs rdiff -u -r1.81 -r1.82 src/usr.bin/make/lst.h
cvs rdiff -u -r1.399 -r1.400 src/usr.bin/make/parse.c
cvs rdiff -u -r1.584 -r1.585 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/lst.c
diff -u src/usr.bin/make/lst.c:1.89 src/usr.bin/make/lst.c:1.90
--- src/usr.bin/make/lst.c:1.89	Sun Oct 25 12:08:53 2020
+++ src/usr.bin/make/lst.c	Sun Oct 25 13:06:12 2020
@@ -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 @@ Lst_Dequeue(List *list)
 }
 
 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);
 }

Index: src/usr.bin/make/lst.h
diff -u src/usr.bin/make/lst.h:1.81 src/usr.bin/make/lst.h:1.82
--- src/usr.bin/make/lst.h:1.81	Sun Oct 25 12:08:53 2020
+++ src/usr.bin/make/lst.h	Sun Oct 25 13:06:12 2020
@@ -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 @@ void Lst_Enqueue(List *, void *);
 /* 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;
-
-void PtrVector_Init(PtrVector *);
-Boolean PtrVector_IsEmpty(PtrVector *);
-void PtrVector_Push(PtrVector *, void *);
-void *PtrVector_Pop(PtrVector *);
-void PtrVector_Done(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 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 */

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.399 src/usr.bin/make/parse.c:1.400
--- src/usr.bin/make/parse.c:1.399	Sun Oct 25 12:08:53 2020
+++ src/usr.bin/make/parse.c	Sun Oct 25 13:06:12 2020
@@ -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 @@ static IFile *curFile;
  *			(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 @@ GetActuallyIncludingFile(void)
     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 @@ Parse_SetInput(const char *name, int lin
 	/* 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 @@ ParseEOF(void)
     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 @@ ParseEOF(void)
 	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 @@ Parse_Init(void)
     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 @@ Parse_End(void)
     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
 }
 

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.584 src/usr.bin/make/var.c:1.585
--- src/usr.bin/make/var.c:1.584	Sun Oct 25 12:08:53 2020
+++ src/usr.bin/make/var.c	Sun Oct 25 13:06:12 2020
@@ -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 @@ Var_Stats(void)
 void
 Var_Dump(GNode *ctxt)
 {
-    PtrVector varnames;
+    Vector /* of const char * */ vec;
     HashIter hi;
     HashEntry *he;
     size_t i;
+    const char **varnames;
 
-    PtrVector_Init(&varnames);
+    Vector_Init(&vec, sizeof(const char *));
 
     HashIter_Init(&hi, &ctxt->context);
     while ((he = HashIter_Next(&hi)) != NULL)
-	PtrVector_Push(&varnames, he->key);
+	*(const char **)Vector_Push(&vec) = he->key;
+    varnames = vec.items;
 
-    qsort(varnames.items, varnames.len, sizeof varnames.items[0], str_cmp_asc);
+    qsort(varnames, vec.len, sizeof varnames[0], str_cmp_asc);
 
-    for (i = 0; i < varnames.len; i++) {
-        const char *varname = varnames.items[i];
+    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);
 }

Reply via email to