[hackers] [scc] Keep hash order in lookup() too || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 11 Aug 2015 17:57:55 +0200 (CEST)

commit d092be22d3220430733d7fb532d72d6a2348e9d1
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Aug 11 11:53:31 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Aug 11 12:38:13 2015 +0200

    Keep hash order in lookup() too
    
    The order was not preserved in lookup(), which was
    inserting always in the head.

diff --git a/cc1/symbol.c b/cc1/symbol.c
index f746901..6328897 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -53,6 +53,44 @@ hash(const char *s)
         return h & NR_SYM_HASH-1;
 }
 
+static Symbol *
+linkhash(Symbol *sym, char *name)
+{
+ Symbol **h, *p, *prev;
+
+ sym->name = xstrdup(name);
+ h = &htab[hash(name)];
+
+ for (prev = p = *h; p; prev = p, p = p->hash) {
+ if (p->ctx <= sym->ctx)
+ break;
+ }
+ if (p == prev) {
+ sym->hash = *h;
+ *h = sym;
+ } else {
+ p = prev->hash;
+ prev->hash = sym;
+ sym->hash = p;
+ }
+
+ return sym;
+}
+
+static void
+unlinkhash(Symbol *sym)
+{
+ Symbol **h, *p, *prev;
+
+ h = &htab[hash(sym->name)];
+ for (prev = p = *h; p != sym; prev = p, p = p->hash)
+ /* nothing */;
+ if (prev == p)
+ *h = sym->hash;
+ else
+ prev->hash = sym->hash;
+}
+
 void
 pushctx(void)
 {
_AT_@ -94,7 +132,7 @@ popctx(void)
                 }
                 if (sym->name) {
                         short f = sym->flags;
- htab[hash(sym->name)] = sym->hash;
+ unlinkhash(sym);
                         if ((f & (ISUSED|ISGLOBAL|ISDECLARED)) == ISDECLARED)
                                 warn("'%s' defined but not used", sym->name);
                 }
_AT_@ -119,7 +157,7 @@ duptype(Type *base)
 Symbol *
 newsym(unsigned ns)
 {
- Symbol *sym;
+ Symbol *sym, *p, *prev;
 
         sym = malloc(sizeof(*sym));
         sym->id = 0;
_AT_@ -129,22 +167,24 @@ newsym(unsigned ns)
         sym->flags = ISDECLARED;
         sym->name = NULL;
         sym->type = NULL;
- sym->hash = NULL;
+ sym->next = sym->hash = NULL;
+
+ if (ns == NS_CPP)
+ return sym;
 
- if (!head || head->ctx <= curctx) {
+ for (prev = p = head; p; prev = p, p = p->next) {
+ if (p->ctx <= sym->ctx)
+ break;
+ }
+ if (p == prev) {
                 sym->next = head;
                 head = sym;
         } else {
- Symbol *p, *prev;
-
- for (prev = p = head; p; prev = p, p = p->next) {
- if (p->ctx <= sym->ctx)
- break;
- }
                 p = prev->next;
                 prev->next = sym;
                 sym->next = p;
         }
+
         return sym;
 }
 
_AT_@ -168,12 +208,9 @@ lookup(unsigned ns)
                         continue;
                 return sym;
         }
-
- sym = newsym(ns);
- sym->name = xstrdup(yytext);
+ sym = linkhash(newsym(ns), yytext);
         sym->flags &= ~ISDECLARED;
- sym->hash = *h;
- *h = sym;
+
         return sym;
 }
 
_AT_@ -213,27 +250,7 @@ install(unsigned ns, Symbol *sym)
                         return NULL;
                 sym->flags |= ISDECLARED;
         } else {
- char *name = sym->name;
- Symbol **h;
-
- sym = newsym(ns);
- sym->name = xstrdup(name);
- h = &htab[hash(name)];
-
- if (!*h || (*h)->ctx <= curctx) {
- sym->hash = *h;
- *h = sym;
- } else {
- Symbol *p, *prev;
-
- for (prev = p = *h; p; prev = p, p = p->hash) {
- if (p->ctx <= sym->ctx)
- break;
- }
- p = prev->hash;
- prev->hash = sym;
- sym->hash = p;
- }
+ sym = linkhash(newsym(ns), sym->name);
         }
 
         if (sym->ns != NS_CPP)
Received on Tue Aug 11 2015 - 17:57:55 CEST

This archive was generated by hypermail 2.3.0 : Tue Aug 11 2015 - 18:00:13 CEST