Hi list, Can someone explain the symhash voodoo in the dogensym function of m_class.c?
To me it looks like sym1 could be referring to a location in the symhash array which isn't yet initialized, so I can't wrap my head around why sym2->s_name doesn't just crash Pd.
Code below for easy reference:
/* ---------------- the symbol table ------------------------ */
#define HASHSIZE 1024
static t_symbol *symhash[HASHSIZE];
t_symbol *dogensym(const char *s, t_symbol *oldsym) { t_symbol **sym1, *sym2; unsigned int hash1 = 0, hash2 = 0; int length = 0; const char *s2 = s; while (*s2) { hash1 += *s2; hash2 += hash1; length++; s2++; } sym1 = symhash + (hash2 & (HASHSIZE-1)); while (sym2 = *sym1) { if (!strcmp(sym2->s_name, s)) return(sym2); sym1 = &sym2->s_next; } if (oldsym) sym2 = oldsym; else { sym2 = (t_symbol *)t_getbytes(sizeof(*sym2)); sym2->s_name = t_getbytes(length+1); sym2->s_next = 0; sym2->s_thing = 0; strcpy(sym2->s_name, s); } *sym1 = sym2; return (sym2); }
On 09/06/2013 04:29 PM, Jonathan Wilkes wrote:
Hi list, Can someone explain the symhash voodoo in the dogensym function of m_class.c?
To me it looks like sym1 could be referring to a location in the symhash array which isn't yet initialized, so I can't wrap my head around why sym2->s_name doesn't just crash Pd.
Ah I see-- global array gets initialized to zero. Then the while loop won't execute until something that is nonzero exists at that index.
Nevermind!
Best, Jonathan
Code below for easy reference:
/* ---------------- the symbol table ------------------------ */
#define HASHSIZE 1024
static t_symbol *symhash[HASHSIZE];
t_symbol *dogensym(const char *s, t_symbol *oldsym) { t_symbol **sym1, *sym2; unsigned int hash1 = 0, hash2 = 0; int length = 0; const char *s2 = s; while (*s2) { hash1 += *s2; hash2 += hash1; length++; s2++; } sym1 = symhash + (hash2 & (HASHSIZE-1)); while (sym2 = *sym1) { if (!strcmp(sym2->s_name, s)) return(sym2); sym1 = &sym2->s_next; } if (oldsym) sym2 = oldsym; else { sym2 = (t_symbol *)t_getbytes(sizeof(*sym2)); sym2->s_name = t_getbytes(length+1); sym2->s_next = 0; sym2->s_thing = 0; strcpy(sym2->s_name, s); } *sym1 = sym2; return (sym2); }
Pd-dev mailing list Pd-dev@iem.at http://lists.puredata.info/listinfo/pd-dev