shithub: scc

Download patch

ref: ef15a4a3654853bb188d80b91578fdbddf462e3b
parent: b2162f3f8c5612a6cbeddacf972cdecf36a3b32d
author: Quentin Rameau <quinq@fifth.space>
date: Sat Jun 18 11:39:51 EDT 2016

[lib] pass a struct items pointer in newitem

Take advantage of the new struct items and get rid of multiple parameter
copies and incrementing.

--- a/cc1/main.c
+++ b/cc1/main.c
@@ -61,7 +61,7 @@
 		incdir(EARGF(usage()));
 		break;
 	case 'U':
-		uflags.s = newitem(uflags.s, uflags.n++, EARGF(usage()));
+		newitem(&uflags, EARGF(usage()));
 		break;
 	case 'd':
 		DBGON();
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -73,7 +73,7 @@
 	if (t->args.n < 1)
 		t->args.n = 1;
 
-	t->args.s = newitem(t->args.s, t->args.n++, arg);
+	newitem(&t->args, arg);
 }
 
 static void
@@ -84,7 +84,7 @@
 	if (t->args.n > 0)
 		t->args.s[0] = arg;
 	else
-		t->args.s = newitem(t->args.s, t->args.n++, arg);
+		newitem(&t->args, arg);
 }
 
 static int
@@ -347,7 +347,7 @@
 	}
 
 	if (validatetools())
-		objs->s = newitem(objs->s, objs->n++, outfilename(file, "o"));
+		newitem(objs, outfilename(file, "o"));
 }
 
 static void
--- a/inc/cc.h
+++ b/inc/cc.h
@@ -21,7 +21,7 @@
 
 extern void die(const char *fmt, ...);
 extern void dbg(const char *fmt, ...);
-extern char **newitem(char **array, unsigned num, char *item);
+extern void newitem(struct items *items, char *item);
 extern void *xmalloc(size_t size);
 extern void *xcalloc(size_t nmemb, size_t size);
 extern char *xstrdup(const char *s);
--- a/lib/newitem.c
+++ b/lib/newitem.c
@@ -1,16 +1,12 @@
 #include "../inc/cc.h"
 
-char **
-newitem(char **array, unsigned num, char *item)
+void
+newitem(struct items *items, char *item)
 {
-	char **ar;
+	if ((items->n + 1) < items->n)
+		die("newitem: overflow (%u + 1)", items->n);
 
-	if ((num + 1) < num)
-		die("newitem: overflow (%u + 1)", num);
-
-	ar = xrealloc(array, (num + 1) * sizeof(char **));
-	ar[num] = item;
-
-	return ar;
+	items->s = xrealloc(items->s, (items->n + 1) * sizeof(char **));
+	items->s[items->n++] = item;
 }