Revert "st.c: use ccan linked-list (try 2)"

This reverts commit r51044
Still getting failure notices from ko1's CI machine.
ref: g3qkqn

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51045 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
normal 2015-06-26 22:52:26 +00:00
Родитель d3725a86de
Коммит b8d544832b
3 изменённых файлов: 163 добавлений и 144 удалений

Просмотреть файл

@ -1,26 +1,3 @@
Sat Jun 27 06:58:01 2015 Eric Wong <e@80x24.org>
* include/ruby/st.h (struct st_table): hide struct list_head
* st.c (struct st_table_entry): adjust struct
(head, tail): remove shortcut macros
(st_head): new wrapper function
(st_init_table_with_size): adjust to new struct and API
(st_clear): ditto
(add_direct): ditto
(unpack_entries): ditto
(rehash): ditto
(st_copy): ditto
(remove_entry): ditto
(st_shift): ditto
(st_foreach_check): ditto
(st_foreach): ditto
(get_keys): ditto
(get_values): ditto
(st_values_check): ditto
(st_reverse_foreach_check): ditto (unused)
(st_reverse_foreach): ditto (unused)
[ruby-core:69726] [Misc #10278]
Fri Jun 26 12:48:37 2015 Nobuyoshi Nakada <nobu@ruby-lang.org> Fri Jun 26 12:48:37 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* transcode.c (load_transcoder_entry): fix transcoder loading race * transcode.c (load_transcoder_entry): fix transcoder loading race

Просмотреть файл

@ -86,7 +86,7 @@ struct st_table {
union { union {
struct { struct {
struct st_table_entry **bins; struct st_table_entry **bins;
void *private_list_head[2]; struct st_table_entry *head, *tail;
} big; } big;
struct { struct {
struct st_packed_entry *entries; struct st_packed_entry *entries;

202
st.c
Просмотреть файл

@ -23,7 +23,7 @@ struct st_table_entry {
st_data_t key; st_data_t key;
st_data_t record; st_data_t record;
st_table_entry *next; st_table_entry *next;
struct list_node olist; st_table_entry *fore, *back;
}; };
typedef struct st_packed_entry { typedef struct st_packed_entry {
@ -108,6 +108,8 @@ st_realloc_bins(st_table_entry **bins, st_index_t newsize, st_index_t oldsize)
/* Shortcut */ /* Shortcut */
#define bins as.big.bins #define bins as.big.bins
#define head as.big.head
#define tail as.big.tail
#define real_entries as.packed.real_entries #define real_entries as.packed.real_entries
/* preparation for possible packing improvements */ /* preparation for possible packing improvements */
@ -193,12 +195,6 @@ stat_col(void)
} }
#endif #endif
static struct list_head *
st_head(const st_table *tbl)
{
return (struct list_head *)&tbl->as.big.private_list_head;
}
st_table* st_table*
st_init_table_with_size(const struct st_hash_type *type, st_index_t size) st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
{ {
@ -224,14 +220,14 @@ st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
tbl->entries_packed = size <= MAX_PACKED_HASH; tbl->entries_packed = size <= MAX_PACKED_HASH;
if (tbl->entries_packed) { if (tbl->entries_packed) {
size = ST_DEFAULT_PACKED_TABLE_SIZE; size = ST_DEFAULT_PACKED_TABLE_SIZE;
tbl->real_entries = 0;
} }
else { else {
size = new_size(size); /* round up to power-of-two */ size = new_size(size); /* round up to power-of-two */
list_head_init(st_head(tbl));
} }
tbl->num_bins = size; tbl->num_bins = size;
tbl->bins = st_alloc_bins(size); tbl->bins = st_alloc_bins(size);
tbl->head = 0;
tbl->tail = 0;
return tbl; return tbl;
} }
@ -282,6 +278,7 @@ void
st_clear(st_table *table) st_clear(st_table *table)
{ {
register st_table_entry *ptr, *next; register st_table_entry *ptr, *next;
st_index_t i;
if (table->entries_packed) { if (table->entries_packed) {
table->num_entries = 0; table->num_entries = 0;
@ -289,13 +286,18 @@ st_clear(st_table *table)
return; return;
} }
list_for_each_safe(st_head(table), ptr, next, olist) { for (i = 0; i < table->num_bins; i++) {
/* list_del is not needed */ ptr = table->bins[i];
table->bins[i] = 0;
while (ptr != 0) {
next = ptr->next;
st_free_entry(ptr); st_free_entry(ptr);
ptr = next;
}
} }
table->num_entries = 0; table->num_entries = 0;
MEMZERO(table->bins, st_table_entry*, table->num_bins); table->head = 0;
list_head_init(st_head(table)); table->tail = 0;
} }
void void
@ -463,7 +465,16 @@ add_direct(st_table *table, st_data_t key, st_data_t value,
} }
entry = new_entry(table, key, value, hash_val, bin_pos); entry = new_entry(table, key, value, hash_val, bin_pos);
list_add_tail(st_head(table), &entry->olist);
if (table->head != 0) {
entry->fore = 0;
(entry->back = table->tail)->fore = entry;
table->tail = entry;
}
else {
table->head = table->tail = entry;
entry->fore = entry->back = 0;
}
table->num_entries++; table->num_entries++;
} }
@ -472,7 +483,7 @@ unpack_entries(register st_table *table)
{ {
st_index_t i; st_index_t i;
st_packed_entry packed_bins[MAX_PACKED_HASH]; st_packed_entry packed_bins[MAX_PACKED_HASH];
register st_table_entry *entry; register st_table_entry *entry, *preventry = 0, **chain;
st_table tmp_table = *table; st_table tmp_table = *table;
MEMCPY(packed_bins, PACKED_BINS(table), st_packed_entry, MAX_PACKED_HASH); MEMCPY(packed_bins, PACKED_BINS(table), st_packed_entry, MAX_PACKED_HASH);
@ -484,24 +495,22 @@ unpack_entries(register st_table *table)
tmp_table.bins = st_realloc_bins(tmp_table.bins, ST_DEFAULT_INIT_TABLE_SIZE, tmp_table.num_bins); tmp_table.bins = st_realloc_bins(tmp_table.bins, ST_DEFAULT_INIT_TABLE_SIZE, tmp_table.num_bins);
tmp_table.num_bins = ST_DEFAULT_INIT_TABLE_SIZE; tmp_table.num_bins = ST_DEFAULT_INIT_TABLE_SIZE;
#endif #endif
/*
* order is important here, we need to keep the original table
* walkable during GC (GC may be triggered by new_entry call)
*/
i = 0; i = 0;
list_head_init(st_head(&tmp_table)); chain = &tmp_table.head;
do { do {
st_data_t key = packed_bins[i].key; st_data_t key = packed_bins[i].key;
st_data_t val = packed_bins[i].val; st_data_t val = packed_bins[i].val;
st_index_t hash = packed_bins[i].hash; st_index_t hash = packed_bins[i].hash;
entry = new_entry(&tmp_table, key, val, hash, entry = new_entry(&tmp_table, key, val, hash,
hash_pos(hash, ST_DEFAULT_INIT_TABLE_SIZE)); hash_pos(hash, ST_DEFAULT_INIT_TABLE_SIZE));
list_add_tail(st_head(&tmp_table), &entry->olist); *chain = entry;
entry->back = preventry;
preventry = entry;
chain = &entry->fore;
} while (++i < MAX_PACKED_HASH); } while (++i < MAX_PACKED_HASH);
*chain = NULL;
tmp_table.tail = entry;
*table = tmp_table; *table = tmp_table;
list_head_init(st_head(table));
list_append_list(st_head(table), st_head(&tmp_table));
} }
static void static void
@ -611,10 +620,12 @@ rehash(register st_table *table)
table->num_bins = new_num_bins; table->num_bins = new_num_bins;
table->bins = new_bins; table->bins = new_bins;
list_for_each(st_head(table), ptr, olist) { if ((ptr = table->head) != 0) {
do {
hash_val = hash_pos(ptr->hash, new_num_bins); hash_val = hash_pos(ptr->hash, new_num_bins);
ptr->next = new_bins[hash_val]; ptr->next = new_bins[hash_val];
new_bins[hash_val] = ptr; new_bins[hash_val] = ptr;
} while ((ptr = ptr->fore) != 0);
} }
} }
@ -622,8 +633,9 @@ st_table*
st_copy(st_table *old_table) st_copy(st_table *old_table)
{ {
st_table *new_table; st_table *new_table;
st_table_entry *ptr, *entry; st_table_entry *ptr, *entry, *prev, **tailp;
st_index_t num_bins = old_table->num_bins; st_index_t num_bins = old_table->num_bins;
st_index_t hash_val;
new_table = st_alloc_table(); new_table = st_alloc_table();
if (new_table == 0) { if (new_table == 0) {
@ -643,12 +655,24 @@ st_copy(st_table *old_table)
return new_table; return new_table;
} }
list_head_init(st_head(new_table)); if ((ptr = old_table->head) != 0) {
prev = 0;
list_for_each(st_head(old_table), ptr, olist) { tailp = &new_table->head;
entry = new_entry(new_table, ptr->key, ptr->record, ptr->hash, do {
hash_pos(ptr->hash, num_bins)); entry = st_alloc_entry();
list_add_tail(st_head(new_table), &entry->olist); if (entry == 0) {
st_free_table(new_table);
return 0;
}
*entry = *ptr;
hash_val = hash_pos(entry->hash, num_bins);
entry->next = new_table->bins[hash_val];
new_table->bins[hash_val] = entry;
entry->back = prev;
*tailp = prev = entry;
tailp = &entry->fore;
} while ((ptr = ptr->fore) != 0);
new_table->tail = prev;
} }
return new_table; return new_table;
@ -657,7 +681,17 @@ st_copy(st_table *old_table)
static inline void static inline void
remove_entry(st_table *table, st_table_entry *ptr) remove_entry(st_table *table, st_table_entry *ptr)
{ {
list_del(&ptr->olist); if (ptr->fore == 0 && ptr->back == 0) {
table->head = 0;
table->tail = 0;
}
else {
st_table_entry *fore = ptr->fore, *back = ptr->back;
if (fore) fore->back = back;
if (back) back->fore = fore;
if (ptr == table->head) table->head = fore;
if (ptr == table->tail) table->tail = back;
}
table->num_entries--; table->num_entries--;
} }
@ -737,7 +771,6 @@ st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *val
int int
st_shift(register st_table *table, register st_data_t *key, st_data_t *value) st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
{ {
st_table_entry *old;
st_table_entry **prev; st_table_entry **prev;
register st_table_entry *ptr; register st_table_entry *ptr;
@ -753,13 +786,12 @@ st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
return 1; return 1;
} }
old = list_pop(st_head(table), st_table_entry, olist); prev = &table->bins[hash_pos(table->head->hash, table->num_bins)];
table->num_entries--; while ((ptr = *prev) != table->head) prev = &ptr->next;
prev = &table->bins[hash_pos(old->hash, table->num_bins)];
while ((ptr = *prev) != old) prev = &ptr->next;
*prev = ptr->next; *prev = ptr->next;
if (value != 0) *value = ptr->record; if (value != 0) *value = ptr->record;
*key = ptr->key; *key = ptr->key;
remove_entry(table, ptr);
st_free_entry(ptr); st_free_entry(ptr);
return 1; return 1;
} }
@ -886,8 +918,7 @@ st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data
int int
st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never) st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never)
{ {
st_table_entry *ptr, **last, *tmp, *next; st_table_entry *ptr, **last, *tmp;
struct list_head *head;
enum st_retval retval; enum st_retval retval;
st_index_t i; st_index_t i;
@ -904,10 +935,8 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
FIND_ENTRY(table, ptr, hash, i); FIND_ENTRY(table, ptr, hash, i);
if (retval == ST_CHECK) { if (retval == ST_CHECK) {
if (!ptr) goto deleted; if (!ptr) goto deleted;
goto unpacked_continue;
} }
if (table->num_entries == 0) return 0;
head = st_head(table);
next = list_entry(ptr->olist.next, st_table_entry, olist);
goto unpacked; goto unpacked;
} }
switch (retval) { switch (retval) {
@ -932,10 +961,14 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
} }
return 0; return 0;
} }
else {
ptr = table->head;
}
head = st_head(table); if (ptr != 0) {
list_for_each_safe(head, ptr, next, olist) { do {
if (ptr->key != never) { if (ptr->key == never)
goto unpacked_continue;
i = hash_pos(ptr->hash, table->num_bins); i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0); retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked: unpacked:
@ -951,6 +984,8 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
} }
/* fall through */ /* fall through */
case ST_CONTINUE: case ST_CONTINUE:
unpacked_continue:
ptr = ptr->fore;
break; break;
case ST_STOP: case ST_STOP:
return 0; return 0;
@ -958,15 +993,16 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
last = &table->bins[hash_pos(ptr->hash, table->num_bins)]; last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) { for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) { if (ptr == tmp) {
tmp = ptr->fore;
remove_entry(table, ptr); remove_entry(table, ptr);
ptr->key = ptr->record = never; ptr->key = ptr->record = never;
ptr->hash = 0; ptr->hash = 0;
ptr = tmp;
break; break;
} }
} }
if (table->num_entries == 0) return 0;
}
} }
} while (ptr && table->head);
} }
return 0; return 0;
} }
@ -974,9 +1010,8 @@ st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t
int int
st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
{ {
st_table_entry *ptr, **last, *tmp, *next; st_table_entry *ptr, **last, *tmp;
enum st_retval retval; enum st_retval retval;
struct list_head *head;
st_index_t i; st_index_t i;
if (table->entries_packed) { if (table->entries_packed) {
@ -990,8 +1025,6 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
if (!table->entries_packed) { if (!table->entries_packed) {
FIND_ENTRY(table, ptr, hash, i); FIND_ENTRY(table, ptr, hash, i);
if (!ptr) return 0; if (!ptr) return 0;
head = st_head(table);
next = list_entry(ptr->olist.next, st_table_entry, olist);
goto unpacked; goto unpacked;
} }
switch (retval) { switch (retval) {
@ -1008,14 +1041,18 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
} }
return 0; return 0;
} }
else {
ptr = table->head;
}
head = st_head(table); if (ptr != 0) {
list_for_each_safe(head, ptr, next, olist) { do {
i = hash_pos(ptr->hash, table->num_bins); i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0); retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked: unpacked:
switch (retval) { switch (retval) {
case ST_CONTINUE: case ST_CONTINUE:
ptr = ptr->fore;
break; break;
case ST_CHECK: case ST_CHECK:
case ST_STOP: case ST_STOP:
@ -1024,14 +1061,16 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
last = &table->bins[hash_pos(ptr->hash, table->num_bins)]; last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) { for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) { if (ptr == tmp) {
tmp = ptr->fore;
*last = ptr->next; *last = ptr->next;
remove_entry(table, ptr); remove_entry(table, ptr);
st_free_entry(ptr); st_free_entry(ptr);
ptr = tmp;
break; break;
} }
} }
if (table->num_entries == 0) return 0;
} }
} while (ptr && table->head);
} }
return 0; return 0;
} }
@ -1053,11 +1092,9 @@ get_keys(st_table *table, st_data_t *keys, st_index_t size, int check, st_data_t
} }
} }
else { else {
st_table_entry *ptr; st_table_entry *ptr = table->head;
st_data_t *keys_end = keys + size; st_data_t *keys_end = keys + size;
for (; ptr && keys < keys_end; ptr = ptr->fore) {
list_for_each(st_head(table), ptr, olist) {
if (keys >= keys_end) break;
key = ptr->key; key = ptr->key;
if (check && key == never) continue; if (check && key == never) continue;
*keys++ = key; *keys++ = key;
@ -1096,11 +1133,9 @@ get_values(st_table *table, st_data_t *values, st_index_t size, int check, st_da
} }
} }
else { else {
st_table_entry *ptr; st_table_entry *ptr = table->head;
st_data_t *values_end = values + size; st_data_t *values_end = values + size;
for (; ptr && values < values_end; ptr = ptr->fore) {
list_for_each(st_head(table), ptr, olist) {
if (values >= values_end) break;
key = ptr->key; key = ptr->key;
if (check && key == never) continue; if (check && key == never) continue;
*values++ = ptr->record; *values++ = ptr->record;
@ -1126,8 +1161,7 @@ st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t n
int int
st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never) st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never)
{ {
st_table_entry *ptr, **last, *tmp, *next; st_table_entry *ptr, **last, *tmp;
struct list_head *head;
enum st_retval retval; enum st_retval retval;
st_index_t i; st_index_t i;
@ -1145,10 +1179,8 @@ st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, s
FIND_ENTRY(table, ptr, hash, i); FIND_ENTRY(table, ptr, hash, i);
if (retval == ST_CHECK) { if (retval == ST_CHECK) {
if (!ptr) goto deleted; if (!ptr) goto deleted;
goto unpacked_continue;
} }
if (table->num_entries == 0) return 0;
head = st_head(table);
next = list_entry(ptr->olist.next, st_table_entry, olist);
goto unpacked; goto unpacked;
} }
switch (retval) { switch (retval) {
@ -1173,10 +1205,14 @@ st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, s
} }
return 0; return 0;
} }
else {
ptr = table->tail;
}
head = st_head(table); if (ptr != 0) {
list_for_each_rev_safe(head, ptr, next, olist) { do {
if (ptr->key != never) { if (ptr->key == never)
goto unpacked_continue;
i = hash_pos(ptr->hash, table->num_bins); i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0); retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked: unpacked:
@ -1192,6 +1228,8 @@ st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, s
} }
/* fall through */ /* fall through */
case ST_CONTINUE: case ST_CONTINUE:
unpacked_continue:
ptr = ptr->back;
break; break;
case ST_STOP: case ST_STOP:
return 0; return 0;
@ -1199,15 +1237,16 @@ st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, s
last = &table->bins[hash_pos(ptr->hash, table->num_bins)]; last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) { for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) { if (ptr == tmp) {
tmp = ptr->back;
remove_entry(table, ptr); remove_entry(table, ptr);
ptr->key = ptr->record = never; ptr->key = ptr->record = never;
ptr->hash = 0; ptr->hash = 0;
ptr = tmp;
break; break;
} }
} }
if (table->num_entries == 0) return 0;
}
} }
} while (ptr && table->head);
} }
return 0; return 0;
} }
@ -1215,9 +1254,8 @@ st_reverse_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, s
int int
st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
{ {
st_table_entry *ptr, **last, *tmp, *next; st_table_entry *ptr, **last, *tmp;
enum st_retval retval; enum st_retval retval;
struct list_head *head;
st_index_t i; st_index_t i;
if (table->entries_packed) { if (table->entries_packed) {
@ -1232,8 +1270,6 @@ st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
if (!table->entries_packed) { if (!table->entries_packed) {
FIND_ENTRY(table, ptr, hash, i); FIND_ENTRY(table, ptr, hash, i);
if (!ptr) return 0; if (!ptr) return 0;
head = st_head(table);
next = list_entry(ptr->olist.next, st_table_entry, olist);
goto unpacked; goto unpacked;
} }
switch (retval) { switch (retval) {
@ -1249,14 +1285,18 @@ st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
} }
return 0; return 0;
} }
else {
ptr = table->tail;
}
head = st_head(table); if (ptr != 0) {
list_for_each_rev_safe(head, ptr, next, olist) { do {
i = hash_pos(ptr->hash, table->num_bins); i = hash_pos(ptr->hash, table->num_bins);
retval = (*func)(ptr->key, ptr->record, arg, 0); retval = (*func)(ptr->key, ptr->record, arg, 0);
unpacked: unpacked:
switch (retval) { switch (retval) {
case ST_CONTINUE: case ST_CONTINUE:
ptr = ptr->back;
break; break;
case ST_CHECK: case ST_CHECK:
case ST_STOP: case ST_STOP:
@ -1265,14 +1305,16 @@ st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
last = &table->bins[hash_pos(ptr->hash, table->num_bins)]; last = &table->bins[hash_pos(ptr->hash, table->num_bins)];
for (; (tmp = *last) != 0; last = &tmp->next) { for (; (tmp = *last) != 0; last = &tmp->next) {
if (ptr == tmp) { if (ptr == tmp) {
tmp = ptr->back;
*last = ptr->next; *last = ptr->next;
remove_entry(table, ptr); remove_entry(table, ptr);
st_free_entry(ptr); st_free_entry(ptr);
ptr = tmp;
break; break;
} }
} }
if (table->num_entries == 0) return 0;
} }
} while (ptr && table->head);
} }
return 0; return 0;
} }