[hackers] [scc] [lib] pass a struct items pointer in newitem || Quentin Rameau
commit 1e3136430d2c8d2872b4d816b8d853a5ad3846f7
Author: Quentin Rameau <quinq_AT_fifth.space>
AuthorDate: Sat Jun 18 15:39:51 2016 +0200
Commit: Quentin Rameau <quinq_AT_fifth.space>
CommitDate: Sat Jun 18 15:39:51 2016 +0200
[lib] pass a struct items pointer in newitem
Take advantage of the new struct items and get rid of multiple parameter
copies and incrementing.
diff --git a/cc1/main.c b/cc1/main.c
index 50b2651..8527747 100644
--- a/cc1/main.c
+++ b/cc1/main.c
_AT_@ -61,7 +61,7 @@ main(int argc, char *argv[])
incdir(EARGF(usage()));
break;
case 'U':
- uflags.s = newitem(uflags.s, uflags.n++, EARGF(usage()));
+ newitem(&uflags, EARGF(usage()));
break;
case 'd':
DBGON();
diff --git a/driver/posix/scc.c b/driver/posix/scc.c
index 7a056ea..4fbff5f 100644
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
_AT_@ -73,7 +73,7 @@ addarg(int tool, char *arg)
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
_AT_@ -84,7 +84,7 @@ setargv0(int tool, char *arg)
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
_AT_@ -347,7 +347,7 @@ build(char *file)
}
if (validatetools())
- objs->s = newitem(objs->s, objs->n++, outfilename(file, "o"));
+ newitem(objs, outfilename(file, "o"));
}
static void
diff --git a/inc/cc.h b/inc/cc.h
index 005bb46..3075282 100644
--- a/inc/cc.h
+++ b/inc/cc.h
_AT_@ -21,7 +21,7 @@ struct items {
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);
diff --git a/lib/newitem.c b/lib/newitem.c
index 50bb56b..1603602 100644
--- a/lib/newitem.c
+++ b/lib/newitem.c
_AT_@ -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;
}
Received on Tue Jun 21 2016 - 08:32:08 CEST
This archive was generated by hypermail 2.3.0
: Tue Jun 21 2016 - 08:36:26 CEST