Replace htable_t with struct

This commit is contained in:
Lassi Kortela 2019-08-09 19:26:09 +03:00
parent 79d44c0780
commit 41cf0e7b2c
10 changed files with 52 additions and 51 deletions

View File

@ -16,8 +16,8 @@ value_t emptystringsym;
value_t structsym, arraysym, enumsym, cfunctionsym, voidsym, pointersym; value_t structsym, arraysym, enumsym, cfunctionsym, voidsym, pointersym;
value_t unionsym; value_t unionsym;
static htable_t TypeTable; static struct htable TypeTable;
static htable_t reverse_dlsym_lookup_table; static struct htable reverse_dlsym_lookup_table;
static fltype_t *int8type, *uint8type; static fltype_t *int8type, *uint8type;
static fltype_t *int16type, *uint16type; static fltype_t *int16type, *uint16type;
static fltype_t *int32type, *uint32type; static fltype_t *int32type, *uint32type;

View File

@ -4,7 +4,7 @@
// comparable tag // comparable tag
#define cmptag(v) (isfixnum(v) ? TAG_NUM : tag(v)) #define cmptag(v) (isfixnum(v) ? TAG_NUM : tag(v))
static value_t eq_class(htable_t *table, value_t key) static value_t eq_class(struct htable *table, value_t key)
{ {
value_t c = (value_t)ptrhash_get(table, (void *)key); value_t c = (value_t)ptrhash_get(table, (void *)key);
if (c == (value_t)HT_NOTFOUND) if (c == (value_t)HT_NOTFOUND)
@ -14,7 +14,7 @@ static value_t eq_class(htable_t *table, value_t key)
return eq_class(table, c); return eq_class(table, c);
} }
static void eq_union(htable_t *table, value_t a, value_t b, value_t c, static void eq_union(struct htable *table, value_t a, value_t b, value_t c,
value_t cb) value_t cb)
{ {
value_t ca = (c == NIL ? a : c); value_t ca = (c == NIL ? a : c);
@ -25,7 +25,8 @@ static void eq_union(htable_t *table, value_t a, value_t b, value_t c,
} }
static value_t bounded_compare(value_t a, value_t b, int bound, int eq); static value_t bounded_compare(value_t a, value_t b, int bound, int eq);
static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq); static value_t cyc_compare(value_t a, value_t b, struct htable *table,
int eq);
static value_t bounded_vector_compare(value_t a, value_t b, int bound, int eq) static value_t bounded_vector_compare(value_t a, value_t b, int bound, int eq)
{ {
@ -137,7 +138,7 @@ compare_top:
return (taga < tagb) ? fixnum(-1) : fixnum(1); return (taga < tagb) ? fixnum(-1) : fixnum(1);
} }
static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table, static value_t cyc_vector_compare(value_t a, value_t b, struct htable *table,
int eq) int eq)
{ {
size_t la = vector_size(a); size_t la = vector_size(a);
@ -187,7 +188,7 @@ static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
return fixnum(0); return fixnum(0);
} }
static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq) static value_t cyc_compare(value_t a, value_t b, struct htable *table, int eq)
{ {
value_t d, ca, cb; value_t d, ca, cb;
cyc_compare_top: cyc_compare_top:
@ -260,7 +261,7 @@ cyc_compare_top:
return bounded_compare(a, b, 1, eq); return bounded_compare(a, b, 1, eq);
} }
static htable_t equal_eq_hashtable; static struct htable equal_eq_hashtable;
void comparehash_init(void) { htable_new(&equal_eq_hashtable, 512); } void comparehash_init(void) { htable_new(&equal_eq_hashtable, 512); }
// 'eq' means unordered comparison is sufficient // 'eq' means unordered comparison is sufficient

View File

@ -163,8 +163,8 @@ char *tostring(value_t v, char *fname);
/* error handling */ /* error handling */
typedef struct _fl_readstate_t { typedef struct _fl_readstate_t {
htable_t backrefs; struct htable backrefs;
htable_t gensyms; struct htable gensyms;
value_t source; value_t source;
struct _fl_readstate_t *prev; struct _fl_readstate_t *prev;
} fl_readstate_t; } fl_readstate_t;

View File

@ -12,7 +12,7 @@
#include "htable.h" #include "htable.h"
#include "hashing.h" #include "hashing.h"
htable_t *htable_new(htable_t *h, size_t size) struct htable *htable_new(struct htable *h, size_t size)
{ {
if (size <= HT_N_INLINE / 2) { if (size <= HT_N_INLINE / 2) {
h->size = size = HT_N_INLINE; h->size = size = HT_N_INLINE;
@ -32,14 +32,14 @@ htable_t *htable_new(htable_t *h, size_t size)
return h; return h;
} }
void htable_free(htable_t *h) void htable_free(struct htable *h)
{ {
if (h->table != &h->_space[0]) if (h->table != &h->_space[0])
LLT_FREE(h->table); LLT_FREE(h->table);
} }
// empty and reduce size // empty and reduce size
void htable_reset(htable_t *h, size_t sz) void htable_reset(struct htable *h, size_t sz)
{ {
sz = nextipow2(sz); sz = nextipow2(sz);
if (h->size > sz * 4 && h->size > HT_N_INLINE) { if (h->size > sz * 4 && h->size > HT_N_INLINE) {

View File

@ -3,20 +3,20 @@
#define HT_N_INLINE 32 #define HT_N_INLINE 32
typedef struct { struct htable {
size_t size; size_t size;
void **table; void **table;
void *_space[HT_N_INLINE]; void *_space[HT_N_INLINE];
} htable_t; };
// define this to be an invalid key/value // define this to be an invalid key/value
#define HT_NOTFOUND ((void *)1) #define HT_NOTFOUND ((void *)1)
// initialize and free // initialize and free
htable_t *htable_new(htable_t *h, size_t size); struct htable *htable_new(struct htable *h, size_t size);
void htable_free(htable_t *h); void htable_free(struct htable *h);
// clear and (possibly) change size // clear and (possibly) change size
void htable_reset(htable_t *h, size_t sz); void htable_reset(struct htable *h, size_t sz);
#endif #endif

View File

@ -11,7 +11,7 @@
((size) <= (HT_N_INLINE * 2) ? (HT_N_INLINE / 2) : (size) >> 3) ((size) <= (HT_N_INLINE * 2) ? (HT_N_INLINE / 2) : (size) >> 3)
#define HTIMPL(HTNAME, HFUNC, EQFUNC) \ #define HTIMPL(HTNAME, HFUNC, EQFUNC) \
static void **HTNAME##_lookup_bp(htable_t *h, void *key) \ static void **HTNAME##_lookup_bp(struct htable *h, void *key) \
{ \ { \
uint_t hv; \ uint_t hv; \
size_t i, orig, index, iter; \ size_t i, orig, index, iter; \
@ -80,21 +80,21 @@
return NULL; \ return NULL; \
} \ } \
\ \
void HTNAME##_put(htable_t *h, void *key, void *val) \ void HTNAME##_put(struct htable *h, void *key, void *val) \
{ \ { \
void **bp = HTNAME##_lookup_bp(h, key); \ void **bp = HTNAME##_lookup_bp(h, key); \
\ \
*bp = val; \ *bp = val; \
} \ } \
\ \
void **HTNAME##_bp(htable_t *h, void *key) \ void **HTNAME##_bp(struct htable *h, void *key) \
{ \ { \
return HTNAME##_lookup_bp(h, key); \ return HTNAME##_lookup_bp(h, key); \
} \ } \
\ \
/* returns bp if key is in hash, otherwise NULL */ \ /* returns bp if key is in hash, otherwise NULL */ \
/* if return is non-NULL and *bp == HT_NOTFOUND then key was deleted */ \ /* if return is non-NULL and *bp == HT_NOTFOUND then key was deleted */ \
static void **HTNAME##_peek_bp(htable_t *h, void *key) \ static void **HTNAME##_peek_bp(struct htable *h, void *key) \
{ \ { \
size_t sz = hash_size(h); \ size_t sz = hash_size(h); \
size_t maxprobe = max_probe(sz); \ size_t maxprobe = max_probe(sz); \
@ -119,7 +119,7 @@
return NULL; \ return NULL; \
} \ } \
\ \
void *HTNAME##_get(htable_t *h, void *key) \ void *HTNAME##_get(struct htable *h, void *key) \
{ \ { \
void **bp = HTNAME##_peek_bp(h, key); \ void **bp = HTNAME##_peek_bp(h, key); \
if (bp == NULL) \ if (bp == NULL) \
@ -127,12 +127,12 @@
return *bp; \ return *bp; \
} \ } \
\ \
int HTNAME##_has(htable_t *h, void *key) \ int HTNAME##_has(struct htable *h, void *key) \
{ \ { \
return (HTNAME##_get(h, key) != HT_NOTFOUND); \ return (HTNAME##_get(h, key) != HT_NOTFOUND); \
} \ } \
\ \
int HTNAME##_remove(htable_t *h, void *key) \ int HTNAME##_remove(struct htable *h, void *key) \
{ \ { \
void **bp = HTNAME##_peek_bp(h, key); \ void **bp = HTNAME##_peek_bp(h, key); \
if (bp != NULL) { \ if (bp != NULL) { \
@ -142,7 +142,7 @@
return 0; \ return 0; \
} \ } \
\ \
void HTNAME##_adjoin(htable_t *h, void *key, void *val) \ void HTNAME##_adjoin(struct htable *h, void *key, void *val) \
{ \ { \
void **bp = HTNAME##_lookup_bp(h, key); \ void **bp = HTNAME##_lookup_bp(h, key); \
if (*bp == HT_NOTFOUND) \ if (*bp == HT_NOTFOUND) \

View File

@ -1,12 +1,12 @@
//-*- mode:c -*- //-*- mode:c -*-
#define HTPROT(HTNAME) \ #define HTPROT(HTNAME) \
void *HTNAME##_get(htable_t *h, void *key); \ void *HTNAME##_get(struct htable *h, void *key); \
void HTNAME##_put(htable_t *h, void *key, void *val); \ void HTNAME##_put(struct htable *h, void *key, void *val); \
void HTNAME##_adjoin(htable_t *h, void *key, void *val); \ void HTNAME##_adjoin(struct htable *h, void *key, void *val); \
int HTNAME##_has(htable_t *h, void *key); \ int HTNAME##_has(struct htable *h, void *key); \
int HTNAME##_remove(htable_t *h, void *key); \ int HTNAME##_remove(struct htable *h, void *key); \
void **HTNAME##_bp(htable_t *h, void *key); void **HTNAME##_bp(struct htable *h, void *key);
// return value, or HT_NOTFOUND if key not found // return value, or HT_NOTFOUND if key not found

View File

@ -1,6 +1,6 @@
extern void *memrchr(const void *s, int c, size_t n); extern void *memrchr(const void *s, int c, size_t n);
static htable_t printconses; static struct htable printconses;
static u_int32_t printlabel; static u_int32_t printlabel;
static int print_pretty; static int print_pretty;
static int print_princ; static int print_princ;

View File

@ -29,7 +29,7 @@ static fltype_t *tabletype;
void print_htable(value_t v, ios_t *f) void print_htable(value_t v, ios_t *f)
{ {
htable_t *h = (htable_t *)cv_data((cvalue_t *)ptr(v)); struct htable *h = (struct htable *)cv_data((cvalue_t *)ptr(v));
size_t i; size_t i;
int first = 1; int first = 1;
fl_print_str("#table(", f); fl_print_str("#table(", f);
@ -48,7 +48,7 @@ void print_htable(value_t v, ios_t *f)
void print_traverse_htable(value_t self) void print_traverse_htable(value_t self)
{ {
htable_t *h = (htable_t *)cv_data((cvalue_t *)ptr(self)); struct htable *h = (struct htable *)cv_data((cvalue_t *)ptr(self));
size_t i; size_t i;
for (i = 0; i < h->size; i += 2) { for (i = 0; i < h->size; i += 2) {
if (h->table[i + 1] != HT_NOTFOUND) { if (h->table[i + 1] != HT_NOTFOUND) {
@ -60,14 +60,14 @@ void print_traverse_htable(value_t self)
void free_htable(value_t self) void free_htable(value_t self)
{ {
htable_t *h = (htable_t *)cv_data((cvalue_t *)ptr(self)); struct htable *h = (struct htable *)cv_data((cvalue_t *)ptr(self));
htable_free(h); htable_free(h);
} }
void relocate_htable(value_t oldv, value_t newv) void relocate_htable(value_t oldv, value_t newv)
{ {
htable_t *oldh = (htable_t *)cv_data((cvalue_t *)ptr(oldv)); struct htable *oldh = (struct htable *)cv_data((cvalue_t *)ptr(oldv));
htable_t *h = (htable_t *)cv_data((cvalue_t *)ptr(newv)); struct htable *h = (struct htable *)cv_data((cvalue_t *)ptr(newv));
if (oldh->table == &oldh->_space[0]) if (oldh->table == &oldh->_space[0])
h->table = &h->_space[0]; h->table = &h->_space[0];
size_t i; size_t i;
@ -91,11 +91,11 @@ value_t fl_tablep(value_t *args, uint32_t nargs)
return ishashtable(args[0]) ? FL_T : FL_F; return ishashtable(args[0]) ? FL_T : FL_F;
} }
static htable_t *totable(value_t v, char *fname) static struct htable *totable(value_t v, char *fname)
{ {
if (!ishashtable(v)) if (!ishashtable(v))
type_error(fname, "table", v); type_error(fname, "table", v);
return (htable_t *)cv_data((cvalue_t *)ptr(v)); return (struct htable *)cv_data((cvalue_t *)ptr(v));
} }
value_t fl_table(value_t *args, uint32_t nargs) value_t fl_table(value_t *args, uint32_t nargs)
@ -107,12 +107,12 @@ value_t fl_table(value_t *args, uint32_t nargs)
// prevent small tables from being added to finalizer list // prevent small tables from being added to finalizer list
if (cnt <= HT_N_INLINE) { if (cnt <= HT_N_INLINE) {
tabletype->vtable->finalize = NULL; tabletype->vtable->finalize = NULL;
nt = cvalue(tabletype, sizeof(htable_t)); nt = cvalue(tabletype, sizeof(struct htable));
tabletype->vtable->finalize = free_htable; tabletype->vtable->finalize = free_htable;
} else { } else {
nt = cvalue(tabletype, 2 * sizeof(void *)); nt = cvalue(tabletype, 2 * sizeof(void *));
} }
htable_t *h = (htable_t *)cv_data((cvalue_t *)ptr(nt)); struct htable *h = (struct htable *)cv_data((cvalue_t *)ptr(nt));
htable_new(h, cnt / 2); htable_new(h, cnt / 2);
uint32_t i; uint32_t i;
value_t k = FL_NIL, arg = FL_NIL; value_t k = FL_NIL, arg = FL_NIL;
@ -130,7 +130,7 @@ value_t fl_table(value_t *args, uint32_t nargs)
value_t fl_table_put(value_t *args, uint32_t nargs) value_t fl_table_put(value_t *args, uint32_t nargs)
{ {
argcount("put!", nargs, 3); argcount("put!", nargs, 3);
htable_t *h = totable(args[0], "put!"); struct htable *h = totable(args[0], "put!");
void **table0 = h->table; void **table0 = h->table;
equalhash_put(h, (void *)args[1], (void *)args[2]); equalhash_put(h, (void *)args[1], (void *)args[2]);
// register finalizer if we outgrew inline space // register finalizer if we outgrew inline space
@ -152,7 +152,7 @@ value_t fl_table_get(value_t *args, uint32_t nargs)
{ {
if (nargs != 3) if (nargs != 3)
argcount("get", nargs, 2); argcount("get", nargs, 2);
htable_t *h = totable(args[0], "get"); struct htable *h = totable(args[0], "get");
value_t v = (value_t)equalhash_get(h, (void *)args[1]); value_t v = (value_t)equalhash_get(h, (void *)args[1]);
if (v == (value_t)HT_NOTFOUND) { if (v == (value_t)HT_NOTFOUND) {
if (nargs == 3) if (nargs == 3)
@ -166,7 +166,7 @@ value_t fl_table_get(value_t *args, uint32_t nargs)
value_t fl_table_has(value_t *args, uint32_t nargs) value_t fl_table_has(value_t *args, uint32_t nargs)
{ {
argcount("has", nargs, 2); argcount("has", nargs, 2);
htable_t *h = totable(args[0], "has"); struct htable *h = totable(args[0], "has");
return equalhash_has(h, (void *)args[1]) ? FL_T : FL_F; return equalhash_has(h, (void *)args[1]) ? FL_T : FL_F;
} }
@ -174,7 +174,7 @@ value_t fl_table_has(value_t *args, uint32_t nargs)
value_t fl_table_del(value_t *args, uint32_t nargs) value_t fl_table_del(value_t *args, uint32_t nargs)
{ {
argcount("del!", nargs, 2); argcount("del!", nargs, 2);
htable_t *h = totable(args[0], "del!"); struct htable *h = totable(args[0], "del!");
if (!equalhash_remove(h, (void *)args[1])) if (!equalhash_remove(h, (void *)args[1]))
key_error("del!", args[1]); key_error("del!", args[1]);
return args[0]; return args[0];
@ -184,7 +184,7 @@ value_t fl_table_foldl(value_t *args, uint32_t nargs)
{ {
argcount("table.foldl", nargs, 3); argcount("table.foldl", nargs, 3);
value_t f = args[0], zero = args[1], t = args[2]; value_t f = args[0], zero = args[1], t = args[2];
htable_t *h = totable(t, "table.foldl"); struct htable *h = totable(t, "table.foldl");
size_t i, n = h->size; size_t i, n = h->size;
void **table = h->table; void **table = h->table;
fl_gc_handle(&f); fl_gc_handle(&f);
@ -195,7 +195,7 @@ value_t fl_table_foldl(value_t *args, uint32_t nargs)
zero = zero =
fl_applyn(3, f, (value_t)table[i], (value_t)table[i + 1], zero); fl_applyn(3, f, (value_t)table[i], (value_t)table[i + 1], zero);
// reload pointer // reload pointer
h = (htable_t *)cv_data((cvalue_t *)ptr(t)); h = (struct htable *)cv_data((cvalue_t *)ptr(t));
if (h->size != n) if (h->size != n)
lerror(EnumerationError, "table.foldl: table modified"); lerror(EnumerationError, "table.foldl: table modified");
table = h->table; table = h->table;
@ -218,6 +218,6 @@ void table_init(void)
{ {
tablesym = symbol("table"); tablesym = symbol("table");
tabletype = tabletype =
define_opaque_type(tablesym, sizeof(htable_t), &table_vtable, NULL); define_opaque_type(tablesym, sizeof(struct htable), &table_vtable, NULL);
assign_global_builtins(tablefunc_info); assign_global_builtins(tablefunc_info);
} }

View File

@ -82,7 +82,7 @@ fltype_t *define_opaque_type(value_t sym, size_t sz, cvtable_t *vtab,
void relocate_typetable(void) void relocate_typetable(void)
{ {
htable_t *h = &TypeTable; struct htable *h = &TypeTable;
size_t i; size_t i;
void *nv; void *nv;
for (i = 0; i < h->size; i += 2) { for (i = 0; i < h->size; i += 2) {