scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 1e3136430d2c8d2872b4d816b8d853a5ad3846f7
parent 86f5bf37e46015bab156c5e462f4a16cd502d552
Author: Quentin Rameau <quinq@fifth.space>
Date:   Sat, 18 Jun 2016 15:39:51 +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.

Diffstat:
Mcc1/main.c | 2+-
Mdriver/posix/scc.c | 6+++---
Minc/cc.h | 2+-
Mlib/newitem.c | 16++++++----------
4 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/cc1/main.c b/cc1/main.c @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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; }