/* * section.c */ #include #include "emalloc.h" #include "htab.h" #include "section.h" static struct section *section_from_hnode(const struct hnode *hnode) { /* hnode is first in section, so no need to mess with offsetof() */ return (struct section*)hnode; } static struct htab section_htab; void section_init(void) { htab_init(§ion_htab, 8, NULL); } struct section *section_enter(const struct strnode *strnode) { struct section *section; section = section_from_hnode(htab_lookup(§ion_htab, (uintptr_t)strnode, NULL)); if (section == NULL) { section = emalloc(sizeof *section); memset(section, '\0', sizeof *section); section->hnode.hval = (uintptr_t)strnode; htab_init(§ion->subsects, 4, NULL); htab_insert(§ion_htab, §ion->hnode); } return section; } static struct subsection *subsection_from_hnode(const struct hnode *hnode) { /* hnode is first in subsection, so no need to mess with offsetof() */ return (struct subsection*)hnode; } struct subsection *subsection_enter(struct section *section, int subsectnr) { struct subsection *subsection; subsection = subsection_from_hnode(htab_lookup(§ion->subsects, (uintptr_t)subsectnr, NULL)); if (subsection == NULL) { subsection = emalloc(sizeof *subsection); subsection->hnode.hval = (uintptr_t)subsectnr; subsection->stmts = arrlst_alloc(sizeof(struct stmt)); htab_insert(§ion->subsects, &subsection->hnode); } return subsection; }