Wed Oct 28 02:43:16 2020 UTC ()
make(1): inline Vector_Get

It is simple enough that it neither bloats the code nor warrants the
extra function call.


(rillig)
diff -r1.90 -r1.91 src/usr.bin/make/lst.c
diff -r1.83 -r1.84 src/usr.bin/make/lst.h

cvs diff -r1.90 -r1.91 src/usr.bin/make/lst.c (expand / switch to unified diff)

--- src/usr.bin/make/lst.c 2020/10/25 13:06:12 1.90
+++ src/usr.bin/make/lst.c 2020/10/28 02:43:16 1.91
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lst.c,v 1.90 2020/10/25 13:06:12 rillig Exp $ */ 1/* $NetBSD: lst.c,v 1.91 2020/10/28 02:43:16 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -24,27 +24,27 @@ @@ -24,27 +24,27 @@
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 */ 33 */
34 34
35#include "make.h" 35#include "make.h"
36 36
37MAKE_RCSID("$NetBSD: lst.c,v 1.90 2020/10/25 13:06:12 rillig Exp $"); 37MAKE_RCSID("$NetBSD: lst.c,v 1.91 2020/10/28 02:43:16 rillig Exp $");
38 38
39static ListNode * 39static ListNode *
40LstNodeNew(ListNode *prev, ListNode *next, void *datum) 40LstNodeNew(ListNode *prev, ListNode *next, void *datum)
41{ 41{
42 ListNode *node = bmake_malloc(sizeof *node); 42 ListNode *node = bmake_malloc(sizeof *node);
43 node->prev = prev; 43 node->prev = prev;
44 node->next = next; 44 node->next = next;
45 node->datum = datum; 45 node->datum = datum;
46 return node; 46 return node;
47} 47}
48 48
49/* Create and initialize a new, empty list. */ 49/* Create and initialize a new, empty list. */
50List * 50List *
@@ -273,35 +273,26 @@ Lst_Dequeue(List *list) @@ -273,35 +273,26 @@ Lst_Dequeue(List *list)
273 assert(datum != NULL); /* since NULL would mean end of the list */ 273 assert(datum != NULL); /* since NULL would mean end of the list */
274 return datum; 274 return datum;
275} 275}
276 276
277void 277void
278Vector_Init(Vector *v, size_t itemSize) 278Vector_Init(Vector *v, size_t itemSize)
279{ 279{
280 v->len = 0; 280 v->len = 0;
281 v->priv_cap = 10; 281 v->priv_cap = 10;
282 v->itemSize = itemSize; 282 v->itemSize = itemSize;
283 v->items = bmake_malloc(v->priv_cap * v->itemSize); 283 v->items = bmake_malloc(v->priv_cap * v->itemSize);
284} 284}
285 285
286/* Return the pointer to the given item in the vector. 
287 * The returned data is valid until the next modifying operation. */ 
288void * 
289Vector_Get(Vector *v, size_t i) 
290{ 
291 unsigned char *items = v->items; 
292 return items + i * v->itemSize; 
293} 
294 
295/* Add space for a new item to the vector and return a pointer to that space. 286/* Add space for a new item to the vector and return a pointer to that space.
296 * The returned data is valid until the next modifying operation. */ 287 * The returned data is valid until the next modifying operation. */
297void * 288void *
298Vector_Push(Vector *v) 289Vector_Push(Vector *v)
299{ 290{
300 if (v->len >= v->priv_cap) { 291 if (v->len >= v->priv_cap) {
301 v->priv_cap *= 2; 292 v->priv_cap *= 2;
302 v->items = bmake_realloc(v->items, v->priv_cap * v->itemSize); 293 v->items = bmake_realloc(v->items, v->priv_cap * v->itemSize);
303 } 294 }
304 v->len++; 295 v->len++;
305 return Vector_Get(v, v->len - 1); 296 return Vector_Get(v, v->len - 1);
306} 297}
307 298

cvs diff -r1.83 -r1.84 src/usr.bin/make/lst.h (expand / switch to unified diff)

--- src/usr.bin/make/lst.h 2020/10/25 13:31:16 1.83
+++ src/usr.bin/make/lst.h 2020/10/28 02:43:16 1.84
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lst.h,v 1.83 2020/10/25 13:31:16 rillig Exp $ */ 1/* $NetBSD: lst.h,v 1.84 2020/10/28 02:43:16 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -160,19 +160,28 @@ void Lst_Enqueue(List *, void *); @@ -160,19 +160,28 @@ void Lst_Enqueue(List *, void *);
160/* Remove the head node of the queue and return its datum. */ 160/* Remove the head node of the queue and return its datum. */
161void *Lst_Dequeue(List *); 161void *Lst_Dequeue(List *);
162 162
163/* A vector is an ordered collection of items, allowing for fast indexed 163/* A vector is an ordered collection of items, allowing for fast indexed
164 * access. */ 164 * access. */
165typedef struct Vector { 165typedef struct Vector {
166 void *items; /* memory holding the items */ 166 void *items; /* memory holding the items */
167 size_t itemSize; /* size of a single item in bytes */ 167 size_t itemSize; /* size of a single item in bytes */
168 size_t len; /* number of actually usable elements */ 168 size_t len; /* number of actually usable elements */
169 size_t priv_cap; /* capacity */ 169 size_t priv_cap; /* capacity */
170} Vector; 170} Vector;
171 171
172void Vector_Init(Vector *, size_t); 172void Vector_Init(Vector *, size_t);
173void *Vector_Get(Vector *, size_t); 173
 174/* Return the pointer to the given item in the vector.
 175 * The returned data is valid until the next modifying operation. */
 176static inline MAKE_ATTR_UNUSED void *
 177Vector_Get(Vector *v, size_t i)
 178{
 179 unsigned char *items = v->items;
 180 return items + i * v->itemSize;
 181}
 182
174void *Vector_Push(Vector *); 183void *Vector_Push(Vector *);
175void *Vector_Pop(Vector *); 184void *Vector_Pop(Vector *);
176void Vector_Done(Vector *); 185void Vector_Done(Vector *);
177 186
178#endif /* MAKE_LST_H */ 187#endif /* MAKE_LST_H */