scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 65952ed9fa3d04aa7750d1cef19324b25ae95c8a
parent 64e796d7f793a5f8dc05855cf1d534fa60935939
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 11 Feb 2019 16:56:45 +0000

[libmach] Remove objreset()

Diffstat:
Minclude/scc/scc/mach.h | 5++---
Msrc/libmach/Makefile | 3+--
Msrc/libmach/coff32/coff32read.c | 2+-
Msrc/libmach/deps.mk | 2--
Msrc/libmach/forsym.c | 2+-
Msrc/libmach/libmach.h | 5+++--
Msrc/libmach/objdel.c | 5++---
Msrc/libmach/objfree.c | 34+++++++++++++++++-----------------
Msrc/libmach/objlookup.c | 14++++----------
Msrc/libmach/objnew.c | 2+-
Dsrc/libmach/objreset.c | 22----------------------
Msrc/libmach/objstrip.c | 4+++-
12 files changed, 35 insertions(+), 65 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -42,7 +42,7 @@ struct objsymdef { struct object { int type; Objsym *htab[NR_SYMHASH]; - Objsym *head; + Objsym *symbols; fpos_t pos; int nsecs; void *data; @@ -61,8 +61,7 @@ extern int archive(FILE *fp); extern long armember(FILE *fp, char *member); extern int objtype(FILE *fp, char **name); extern Obj *objnew(int type); -extern int objdel(Obj *obj); -extern int objreset(Obj *obj); +extern void objdel(Obj *obj); extern int objread(Obj *obj, FILE *fp); extern Objsym *objlookup(Obj *obj, char *name, int install); extern int objstrip(Obj *obj); diff --git a/src/libmach/Makefile b/src/libmach/Makefile @@ -12,7 +12,7 @@ OBJS = addr2line.o \ objnew.o \ objpos.o \ objread.o \ - objreset.o \ + objfree.o \ objstrip.o \ objsect.o \ getindex.o \ @@ -22,7 +22,6 @@ OBJS = addr2line.o \ formember.o \ objtype.o \ objwrite.o \ - objfree.o \ pack.o \ unpack.o \ new.o \ diff --git a/src/libmach/coff32/coff32read.c b/src/libmach/coff32/coff32read.c @@ -445,6 +445,6 @@ coff32read(Obj *obj, FILE *fp) return 0; error: - objreset(obj); + objfree(obj, TARGETDEL); return -1; } diff --git a/src/libmach/deps.mk b/src/libmach/deps.mk @@ -69,8 +69,6 @@ ./objpos.o: ./libmach.h ./objread.o: $(INCDIR)/scc/scc/mach.h ./objread.o: ./libmach.h -./objreset.o: $(INCDIR)/scc/scc/mach.h -./objreset.o: ./libmach.h ./objsect.o: $(INCDIR)/scc/scc/mach.h ./objsect.o: ./libmach.h ./objstrip.o: $(INCDIR)/scc/scc/mach.h diff --git a/src/libmach/forsym.c b/src/libmach/forsym.c @@ -10,7 +10,7 @@ forsym(Obj *obj, int (*fn)(Objsym *, void *), void *data) int r; Objsym *sym; - for (sym = obj->head; sym; sym = sym->next) { + for (sym = obj->symbols; sym; sym = sym->next) { r = (*fn)(sym, data); if (r <= 0) return r; diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h @@ -23,8 +23,9 @@ enum order { BIG_ENDIAN, }; -enum freeflags { - FREESYM, +enum deltype { + GENERICDEL = 1 << 0, + TARGETDEL = 1 << 1, }; typedef int (*newfun_t)(Obj *obj); diff --git a/src/libmach/objdel.c b/src/libmach/objdel.c @@ -5,10 +5,9 @@ #include "libmach.h" -int +void objdel(Obj *obj) { - if (objreset(obj) < 0) - return -1; + objfree(obj, TARGETDEL | GENERICDEL); free(obj); } diff --git a/src/libmach/objfree.c b/src/libmach/objfree.c @@ -1,29 +1,29 @@ #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <scc/mach.h> #include "libmach.h" -static void -delsyms(Obj *obj) -{ - Objsym *sym, *next; - - for (sym = obj->head; sym; sym = next) { - next = sym->next; - free(sym->name); - free(sym); - } - - obj->head = NULL; - memset(obj->htab, 0, sizeof(obj->htab)); -} +extern delfun_t delv[]; void objfree(Obj *obj, int what) { - if (what & FREESYM) - delsyms(obj); + int fmt; + delfun_t fn; + + if (what & TARGETDEL) { + fmt = FORMAT(obj->type); + if (fmt < NFORMATS) + return -1; + fn = delv[fmt]; + (*fn)(obj); + } + + if (what & GENERICDEL) { + free(obj->symbols); + obj->symbols = NULL; + memset(obj->htab, 0, sizeof(obj->htab)); + } } diff --git a/src/libmach/objlookup.c b/src/libmach/objlookup.c @@ -9,8 +9,6 @@ Objsym * objlookup(Obj *obj, char *name, int install) { unsigned h; - size_t len; - char *s; Objsym *sym; h = genhash(name) % NR_SYMHASH; @@ -23,19 +21,15 @@ objlookup(Obj *obj, char *name, int install) if ((sym = malloc(sizeof(*sym))) == NULL) return NULL; - len = strlen(name) + 1; - if ((s = malloc(len)) == NULL) { - free(sym); - return NULL; - } - sym->name = memcpy(s, name, len); + + sym->name = name; sym->type = 'U'; sym->size = 0; sym->value = 0; sym->hash = obj->htab[h]; obj->htab[h] = sym; - sym->next = obj->head; - obj->head = sym; + sym->next = obj->symbols; + obj->symbols = sym; return sym; } diff --git a/src/libmach/objnew.c b/src/libmach/objnew.c @@ -23,7 +23,7 @@ objnew(int type) return NULL; obj->type = type; - obj->head = NULL; + obj->symbols = NULL; memset(obj->htab, 0, sizeof(obj->htab)); fn = newv[fmt]; diff --git a/src/libmach/objreset.c b/src/libmach/objreset.c @@ -1,22 +0,0 @@ -#include <stdio.h> - -#include <scc/mach.h> - -#include "libmach.h" - -extern delfun_t delv[]; - -int -objreset(Obj *obj) -{ - int fmt; - delfun_t fn; - - fmt = FORMAT(obj->type); - if (fmt < NFORMATS) - return -1; - fn = delv[fmt]; - (*fn)(obj); - objfree(obj, FREESYM); - return 0; -} diff --git a/src/libmach/objstrip.c b/src/libmach/objstrip.c @@ -17,6 +17,8 @@ objstrip(Obj *obj) return -1; fn = stripv[fmt]; (*fn)(obj); - objfree(obj, FREESYM); + + objfree(obj, GENERICDEL); + return 0; }