1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

st macroses for allocation

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34312 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shyouhei 2012-01-15 15:46:37 +00:00
parent 93829abc0f
commit 638f696b6e

56
st.c
View file

@ -64,20 +64,33 @@ static void rehash(st_table *);
#ifdef RUBY #ifdef RUBY
#define malloc xmalloc #define malloc xmalloc
#define calloc xcalloc #define calloc xcalloc
#define realloc xrealloc
#define free(x) xfree(x) #define free(x) xfree(x)
#endif #endif
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
#define alloc(type) (type*)malloc((size_t)sizeof(type))
#define Calloc(n,s) (char*)calloc((n),(s))
#define EQUAL(table,x,y) ((x)==(y) || (*(table)->type->compare)((x),(y)) == 0) #define EQUAL(table,x,y) ((x)==(y) || (*(table)->type->compare)((x),(y)) == 0)
/* remove cast to unsigned int in the future */ /* remove cast to unsigned int in the future */
#define do_hash(key,table) (unsigned int)(st_index_t)(*(table)->type->hash)((key)) #define do_hash(key,table) (unsigned int)(st_index_t)(*(table)->type->hash)((key))
#define do_hash_bin(key,table) (do_hash((key), (table))%(table)->num_bins) #define do_hash_bin(key,table) (do_hash((key), (table))%(table)->num_bins)
/* preparation for possible allocation improvements */
#define st_alloc_entry() (st_table_entry *)malloc(sizeof(st_table_entry))
#define st_free_entry(entry) free(entry)
#define st_alloc_table() (st_table *)malloc(sizeof(st_table))
#define st_dealloc_table(table) free(table)
#define st_alloc_bins(size) (st_table_entry **)calloc(size, sizeof(st_table_entry *))
#define st_free_bins(bins, size) free(bins)
static inline st_table_entry**
st_realloc_bins(st_table_entry **bins, st_index_t newsize, st_index_t oldsize)
{
bins = (st_table_entry **) realloc(bins, newsize * sizeof(st_table_entry *));
memset(bins, 0, newsize * sizeof(st_table_entry *));
return bins;
}
/* preparation for possible packing improvements */ /* preparation for possible packing improvements */
#define PKEY_POS(i, num_bins) ((i)*2) #define PKEY_POS(i, num_bins) ((i)*2)
#define PVAL_POS(i, num_bins) ((i)*2+1) #define PVAL_POS(i, num_bins) ((i)*2+1)
@ -202,12 +215,12 @@ st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
size = new_size(size); /* round up to prime number */ size = new_size(size); /* round up to prime number */
tbl = alloc(st_table); tbl = st_alloc_table();
tbl->type = type; tbl->type = type;
tbl->num_entries = 0; tbl->num_entries = 0;
tbl->entries_packed = type == &type_numhash && size/2 <= MAX_PACKED_NUMHASH; tbl->entries_packed = type == &type_numhash && size/2 <= MAX_PACKED_NUMHASH;
tbl->num_bins = size; tbl->num_bins = size;
tbl->bins = (st_table_entry **)Calloc(size, sizeof(st_table_entry*)); tbl->bins = st_alloc_bins(size);
tbl->head = 0; tbl->head = 0;
tbl->tail = 0; tbl->tail = 0;
@ -272,7 +285,7 @@ st_clear(st_table *table)
table->bins[i] = 0; table->bins[i] = 0;
while (ptr != 0) { while (ptr != 0) {
next = ptr->next; next = ptr->next;
free(ptr); st_free_entry(ptr);
ptr = next; ptr = next;
} }
} }
@ -285,8 +298,8 @@ void
st_free_table(st_table *table) st_free_table(st_table *table)
{ {
st_clear(table); st_clear(table);
free(table->bins); st_free_bins(table->bins, table->num_bins);
free(table); st_dealloc_table(table);
} }
size_t size_t
@ -417,7 +430,7 @@ add_direct(st_table * table, st_data_t key, st_data_t value,
bin_pos = hash_val % table->num_bins; bin_pos = hash_val % table->num_bins;
} }
entry = alloc(st_table_entry); entry = st_alloc_entry();
entry->hash = hash_val; entry->hash = hash_val;
entry->key = key; entry->key = key;
@ -557,12 +570,10 @@ static void
rehash(register st_table *table) rehash(register st_table *table)
{ {
register st_table_entry *ptr, **new_bins; register st_table_entry *ptr, **new_bins;
st_index_t i, new_num_bins, hash_val; st_index_t new_num_bins, hash_val;
new_num_bins = new_size(table->num_bins+1); new_num_bins = new_size(table->num_bins+1);
new_bins = (st_table_entry**) new_bins = st_realloc_bins(table->bins, new_num_bins, table->num_bins);
xrealloc(table->bins, new_num_bins * sizeof(st_table_entry*));
for (i = 0; i < new_num_bins; ++i) new_bins[i] = 0;
table->num_bins = new_num_bins; table->num_bins = new_num_bins;
table->bins = new_bins; table->bins = new_bins;
@ -583,17 +594,16 @@ st_copy(st_table *old_table)
st_index_t num_bins = old_table->num_bins; st_index_t num_bins = old_table->num_bins;
st_index_t hash_val; st_index_t hash_val;
new_table = alloc(st_table); new_table = st_alloc_table();
if (new_table == 0) { if (new_table == 0) {
return 0; return 0;
} }
*new_table = *old_table; *new_table = *old_table;
new_table->bins = (st_table_entry**) new_table->bins = st_alloc_bins(num_bins);
Calloc((unsigned)num_bins, sizeof(st_table_entry*));
if (new_table->bins == 0) { if (new_table->bins == 0) {
free(new_table); st_dealloc_table(new_table);
return 0; return 0;
} }
@ -606,7 +616,7 @@ st_copy(st_table *old_table)
prev = 0; prev = 0;
tail = &new_table->head; tail = &new_table->head;
do { do {
entry = alloc(st_table_entry); entry = st_alloc_entry();
if (entry == 0) { if (entry == 0) {
st_free_table(new_table); st_free_table(new_table);
return 0; return 0;
@ -668,7 +678,7 @@ st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
remove_entry(table, ptr); remove_entry(table, ptr);
if (value != 0) *value = ptr->record; if (value != 0) *value = ptr->record;
*key = ptr->key; *key = ptr->key;
free(ptr); st_free_entry(ptr);
return 1; return 1;
} }
} }
@ -738,7 +748,7 @@ st_cleanup_safe(st_table *table, st_data_t never)
if (ptr->key == never) { if (ptr->key == never) {
tmp = ptr; tmp = ptr;
*last = ptr = ptr->next; *last = ptr = ptr->next;
free(tmp); st_free_entry(tmp);
} }
else { else {
ptr = *(last = &ptr->next); ptr = *(last = &ptr->next);
@ -790,7 +800,7 @@ st_update(st_table *table, st_data_t key, int (*func)(st_data_t key, st_data_t *
tmp = ptr->fore; tmp = ptr->fore;
*last = ptr->next; *last = ptr->next;
remove_entry(table, ptr); remove_entry(table, ptr);
free(ptr); st_free_entry(ptr);
break; break;
} }
} }
@ -874,7 +884,7 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
tmp = ptr->fore; tmp = ptr->fore;
*last = ptr->next; *last = ptr->next;
remove_entry(table, ptr); remove_entry(table, ptr);
free(ptr); st_free_entry(ptr);
if (ptr == tmp) return 0; if (ptr == tmp) return 0;
ptr = tmp; ptr = tmp;
break; break;
@ -952,7 +962,7 @@ st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
tmp = ptr->back; tmp = ptr->back;
*last = ptr->next; *last = ptr->next;
remove_entry(table, ptr); remove_entry(table, ptr);
free(ptr); st_free_entry(ptr);
ptr = tmp; ptr = tmp;
break; break;
} }