Project

General

Profile

Feature #5789 » st_pool_alloc.patch

use pool allocation for parts of hash - funny_falcon (Yura Sokolov), 12/28/2011 06:52 PM

View differences:

common.mk
$(VM_CORE_H_INCLUDES) {$(VPATH)}debug.h
sprintf.$(OBJEXT): {$(VPATH)}sprintf.c $(RUBY_H_INCLUDES) {$(VPATH)}re.h \
{$(VPATH)}regex.h {$(VPATH)}vsnprintf.c $(ENCODING_H_INCLUDES)
st.$(OBJEXT): {$(VPATH)}st.c $(RUBY_H_INCLUDES)
st.$(OBJEXT): {$(VPATH)}st.c $(RUBY_H_INCLUDES) {$(VPATH)}pool_alloc.inc.h
strftime.$(OBJEXT): {$(VPATH)}strftime.c $(RUBY_H_INCLUDES) \
{$(VPATH)}timev.h
string.$(OBJEXT): {$(VPATH)}string.c $(RUBY_H_INCLUDES) {$(VPATH)}re.h \
pool_alloc.inc.h
/*
* this is generic pool allocator
* you should define following macroses:
* ITEM_NAME - unique identifier, which allows to hold functions in a namespace
* ITEM_TYPEDEF(name) - passed to typedef to localize item type
* free_entry - desired name of function for free entry
* alloc_entry - defired name of function for allocate entry
*/
#define NAME_(prefix, kind) sta_##prefix##_##kind
#define NAME(prefix, kind) NAME_(prefix, kind)
#define holder_typename NAME(holder, ITEM_NAME)
#define entry_typename NAME(entry, ITEM_NAME)
#define list_typename NAME(list, ITEM_NAME)
#define union_typename NAME(union, ITEM_NAME)
#define item_type NAME(item, ITEM_NAME)
typedef ITEM_TYPEDEF(item_type);
typedef struct holder_typename holder_typename;
typedef struct entry_typename entry_typename;
typedef struct list_typename {
entry_typename *fore, *back;
} list_typename;
typedef union union_typename {
list_typename l;
item_type item;
} union_typename;
struct entry_typename {
union_typename p;
holder_typename *holder;
};
#define HOLDER_SIZE (4096 / sizeof(entry_typename) - 1)
struct holder_typename {
unsigned int free;
entry_typename items[HOLDER_SIZE];
};
#define free_entry_p NAME(free_pointer, ITEM_NAME)
#define free_entry_count NAME(count, ITEM_NAME)
static entry_typename *free_entry_p = NULL;
static unsigned long free_entry_count = 0;
#define entry_chain NAME(chain, ITEM_NAME)
#define holder_alloc NAME(holder_alloc, ITEM_NAME)
#define holder_free NAME(holder_free, ITEM_NAME)
#define fore p.l.fore
#define back p.l.back
static inline void
entry_chain(entry_typename *entry)
{
entry->fore = free_entry_p;
entry->back = NULL;
if (free_entry_p) {
free_entry_p->back = entry;
}
free_entry_p = entry;
}
static void
holder_alloc()
{
holder_typename *holder = alloc(holder_typename);
unsigned int i = HOLDER_SIZE - 1;
register entry_typename *ptr = holder->items;
holder->free = HOLDER_SIZE;
for(; i; ptr++, i-- ) {
ptr->holder = holder;
ptr->fore = ptr + 1;
(ptr + 1)->back = ptr;
}
holder->items[0].back = NULL;
holder->items[HOLDER_SIZE - 1].holder = holder;
holder->items[HOLDER_SIZE - 1].fore = free_entry_p;
free_entry_p = &holder->items[0];
free_entry_count+= HOLDER_SIZE;
}
static void
holder_free(holder_typename *holder)
{
unsigned int i;
entry_typename *ptr;
for(i = 0; i < HOLDER_SIZE; i++) {
ptr = &holder->items[i];
if (ptr->fore) {
ptr->fore->back = ptr->back;
}
if (ptr->back) {
ptr->back->fore = ptr->fore;
} else {
free_entry_p = ptr->fore;
}
}
free_entry_count-= HOLDER_SIZE;
free(holder);
}
static void
free_entry(item_type *entry)
{
holder_typename *holder = ((entry_typename *)entry)->holder;
entry_chain((entry_typename *)entry);
holder->free++;
free_entry_count++;
if (holder->free == HOLDER_SIZE && free_entry_count > HOLDER_SIZE) {
holder_free(holder);
}
}
static item_type *
alloc_entry()
{
entry_typename *result;
if (!free_entry_p) {
holder_alloc();
}
result = free_entry_p;
free_entry_p = result->fore;
result->holder->free--;
free_entry_count--;
return (item_type *)result;
}
#undef NAME_
#undef NAME
#undef holder_typename
#undef entry_typename
#undef list_typename
#undef union_typename
#undef item_type
#undef free_entry_p
#undef free_entry_count
#undef HOLDER_SIZE
#undef entry_chain
#undef holder_alloc
#undef holdef_free
#undef fore
#undef back
st.c
st_table_entry *fore, *back;
};
#define ST_DEFAULT_MAX_DENSITY 5
#define ST_DEFAULT_MAX_DENSITY 3
#define ST_DEFAULT_INIT_TABLE_SIZE 11
/*
......
#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 ST_USE_POOLED_ALLOCATOR
#ifdef ST_USE_POOLED_ALLOCATOR
#define ITEM_NAME entry
#define ITEM_TYPEDEF(name) st_table_entry name
#define free_entry st_free_entry
#define alloc_entry st_alloc_entry
#include "pool_alloc.inc.h"
#undef ITEM_NAME
#undef ITEM_TYPEDEF
#undef free_entry
#undef alloc_entry
#define ITEM_NAME bins11
typedef st_table_entry *st_table_entry_p;
#define ITEM_TYPEDEF(name) st_table_entry_p name[ST_DEFAULT_INIT_TABLE_SIZE]
#define free_entry st_free_bins11
#define alloc_entry st_alloc_bins11
#include "pool_alloc.inc.h"
#undef ITEM_NAME
#undef ITEM_TYPEDEF
#undef free_entry
#undef alloc_entry
#define ITEM_NAME table
#define ITEM_TYPEDEF(name) st_table name
#define free_entry st_dealloc_table
#define alloc_entry st_alloc_table
#include "pool_alloc.inc.h"
#undef ITEM_NAME
#undef ITEM_TYPEDEF
#undef free_entry
#undef alloc_entry
static st_table_entry **
st_alloc_bins(st_index_t num_bins)
{
st_table_entry **result;
if (num_bins == ST_DEFAULT_INIT_TABLE_SIZE) {
result = (st_table_entry **) st_alloc_bins11();
}
else {
result = (st_table_entry **) malloc(num_bins * sizeof(st_table_entry *));
}
memset(result, 0, num_bins * sizeof(st_table_entry *));
return result;
}
static void
st_free_bins(st_table_entry **bins, st_index_t num_bins)
{
if (num_bins == ST_DEFAULT_INIT_TABLE_SIZE) {
st_free_bins11(
(st_table_entry_p (*)[ST_DEFAULT_INIT_TABLE_SIZE]) bins);
} else {
free(bins);
}
}
#else
#define st_alloc_entry() alloc(st_table_entry)
#define st_free_entry(entry) free(entry)
#define st_alloc_table() alloc(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)
#endif
/*
* MINSIZE is the minimum size of a dictionary.
*/
......
Table of prime numbers 2^n+a, 2<=n<=30.
*/
static const unsigned int primes[] = {
8 + 3,
ST_DEFAULT_INIT_TABLE_SIZE,
16 + 3,
32 + 5,
64 + 3,
......
size = new_size(size); /* round up to prime number */
tbl = alloc(st_table);
tbl = st_alloc_table();
tbl->type = type;
tbl->num_entries = 0;
tbl->entries_packed = type == &type_numhash && size/2 <= MAX_PACKED_NUMHASH;
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->tail = 0;
......
table->bins[i] = 0;
while (ptr != 0) {
next = ptr->next;
free(ptr);
st_free_entry(ptr);
ptr = next;
}
}
......
st_free_table(st_table *table)
{
st_clear(table);
free(table->bins);
free(table);
st_free_bins(table->bins, table->num_bins);
st_dealloc_table(table);
}
size_t
......
#define FOUND_ENTRY
#endif
#define FIND_ENTRY(table, ptr, hash_val, bin_pos) do {\
(bin_pos) = (hash_val)%(table)->num_bins;\
(ptr) = (table)->bins[(bin_pos)];\
FOUND_ENTRY;\
if (PTR_NOT_EQUAL((table), (ptr), (hash_val), key)) {\
COLLISION;\
while (PTR_NOT_EQUAL((table), (ptr)->next, (hash_val), key)) {\
(ptr) = (ptr)->next;\
}\
(ptr) = (ptr)->next;\
}\
} while (0)
static st_table_entry *
find_entry(st_table *table, st_data_t key, st_index_t hash_val, st_index_t *bin_posp)
{
st_index_t bin_pos = hash_val % table->num_bins;
register st_table_entry *ptr = table->bins[bin_pos];
FOUND_ENTRY;
if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {
COLLISION;
while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {
ptr = ptr->next;
}
ptr = ptr->next;
}
if (bin_posp != NULL)
*bin_posp = bin_pos;
return ptr;
}
static inline st_index_t
find_packed_index(st_table *table, st_data_t key)
{
st_index_t i = 0;
while (i < table->num_entries && (st_data_t)table->bins[i*2] != key) i++;
return i;
}
#define collision_check 0
int
st_lookup(st_table *table, register st_data_t key, st_data_t *value)
{
st_index_t hash_val, bin_pos;
register st_table_entry *ptr;
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
if (value !=0) *value = (st_data_t)table->bins[i*2+1];
return 1;
}
}
st_index_t i = find_packed_index(table, key);
if (i < table->num_entries) {
if (value != 0) *value = (st_data_t)table->bins[i*2+1];
return 1;
}
return 0;
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, do_hash(key, table), NULL);
if (ptr == 0) {
return 0;
......
int
st_get_key(st_table *table, register st_data_t key, st_data_t *result)
{
st_index_t hash_val, bin_pos;
register st_table_entry *ptr;
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
if (result !=0) *result = (st_data_t)table->bins[i*2];
return 1;
}
}
st_index_t i = find_packed_index(table, key);
if (i < table->num_entries) {
if (result != 0) *result = (st_data_t)table->bins[i*2];
return 1;
}
return 0;
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, do_hash(key, table), NULL);
if (ptr == 0) {
return 0;
......
((st_index_t)((table)->num_entries+1) * 2 <= (table)->num_bins && \
(table)->num_entries+1 <= MAX_PACKED_NUMHASH)
#define ADD_DIRECT(table, key, value, hash_val, bin_pos)\
do {\
st_table_entry *entry;\
if ((table)->num_entries > ST_DEFAULT_MAX_DENSITY * (table)->num_bins) {\
rehash(table);\
(bin_pos) = (hash_val) % (table)->num_bins;\
}\
\
entry = alloc(st_table_entry);\
\
entry->hash = (hash_val);\
entry->key = (key);\
entry->record = (value);\
entry->next = (table)->bins[(bin_pos)];\
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)->bins[(bin_pos)] = entry;\
(table)->num_entries++;\
} while (0)
static void
add_direct(st_table * table, st_data_t key, st_data_t value,
st_index_t hash_val, st_index_t bin_pos)
{
st_table_entry *entry;
if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) {
rehash(table);
bin_pos = hash_val % table->num_bins;
}
entry = st_alloc_entry();
entry->hash = hash_val;
entry->key = key;
entry->record = value;
entry->next = table->bins[bin_pos];
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->bins[bin_pos] = entry;
table->num_entries++;
}
static void
unpack_entries(register st_table *table)
......
*table = tmp_table;
}
static int
add_packed_direct(st_table *table, st_data_t key, st_data_t value)
{
int res = 1;
if (MORE_PACKABLE_P(table)) {
st_index_t i = table->num_entries++;
table->bins[i*2] = (struct st_table_entry*)key;
table->bins[i*2+1] = (struct st_table_entry*)value;
}
else {
unpack_entries(table);
res = 0;
}
return res;
}
int
st_insert(register st_table *table, register st_data_t key, st_data_t value)
{
......
register st_table_entry *ptr;
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
table->bins[i*2+1] = (struct st_table_entry*)value;
return 1;
}
}
if (MORE_PACKABLE_P(table)) {
i = table->num_entries++;
table->bins[i*2] = (struct st_table_entry*)key;
table->bins[i*2+1] = (struct st_table_entry*)value;
return 0;
}
else {
unpack_entries(table);
st_index_t i = find_packed_index(table, key);
if (i < table->num_entries) {
table->bins[i*2+1] = (struct st_table_entry*)value;
return 1;
}
if (add_packed_direct(table, key, value)) {
return 0;
}
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, hash_val, &bin_pos);
if (ptr == 0) {
ADD_DIRECT(table, key, value, hash_val, bin_pos);
add_direct(table, key, value, hash_val, bin_pos);
return 0;
}
else {
......
register st_table_entry *ptr;
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == key) {
table->bins[i*2+1] = (struct st_table_entry*)value;
return 1;
}
}
if (MORE_PACKABLE_P(table)) {
i = table->num_entries++;
table->bins[i*2] = (struct st_table_entry*)key;
table->bins[i*2+1] = (struct st_table_entry*)value;
return 0;
}
else {
unpack_entries(table);
st_index_t i = find_packed_index(table, key);
if (i < table->num_entries) {
table->bins[i*2+1] = (struct st_table_entry*)value;
return 1;
}
if (add_packed_direct(table, key, value)) {
return 0;
}
}
hash_val = do_hash(key, table);
FIND_ENTRY(table, ptr, hash_val, bin_pos);
ptr = find_entry(table, key, hash_val, &bin_pos);
if (ptr == 0) {
key = (*func)(key);
ADD_DIRECT(table, key, value, hash_val, bin_pos);
add_direct(table, key, value, hash_val, bin_pos);
return 0;
}
else {
......
st_index_t hash_val, bin_pos;
if (table->entries_packed) {
int i;
if (MORE_PACKABLE_P(table)) {
i = table->num_entries++;
table->bins[i*2] = (struct st_table_entry*)key;
table->bins[i*2+1] = (struct st_table_entry*)value;
return;
}
else {
unpack_entries(table);
}
if (add_packed_direct(table, key, value)) {
return;
}
}
hash_val = do_hash(key, table);
bin_pos = hash_val % table->num_bins;
ADD_DIRECT(table, key, value, hash_val, bin_pos);
add_direct(table, key, value, hash_val, bin_pos);
}
static void
......
st_index_t i, new_num_bins, hash_val;
new_num_bins = new_size(table->num_bins+1);
new_bins = (st_table_entry**)
xrealloc(table->bins, new_num_bins * sizeof(st_table_entry*));
for (i = 0; i < new_num_bins; ++i) new_bins[i] = 0;
st_free_bins(table->bins, table->num_bins);
new_bins = st_alloc_bins(new_num_bins);
table->num_bins = new_num_bins;
table->bins = new_bins;
......
st_index_t num_bins = old_table->num_bins;
st_index_t hash_val;
new_table = alloc(st_table);
new_table = st_alloc_table();
if (new_table == 0) {
return 0;
}
*new_table = *old_table;
new_table->bins = (st_table_entry**)
Calloc((unsigned)num_bins, sizeof(st_table_entry*));
new_table->bins = st_alloc_bins(num_bins);
if (new_table->bins == 0) {
free(new_table);
st_dealloc_table(new_table);
return 0;
}
......
prev = 0;
tail = &new_table->head;
do {
entry = alloc(st_table_entry);
entry = st_alloc_entry();
if (entry == 0) {
st_free_table(new_table);
st_dealloc_table(new_table);
return 0;
}
*entry = *ptr;
......
return new_table;
}
#define REMOVE_ENTRY(table, ptr) do \
{ \
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--; \
} while (0)
static inline void
remove_entry(st_table *table, st_table_entry *ptr)
{
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--;
}
int
st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
......
register st_table_entry *ptr;
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == *key) {
if (value != 0) *value = (st_data_t)table->bins[i*2+1];
table->num_entries--;
memmove(&table->bins[i*2], &table->bins[(i+1)*2],
sizeof(struct st_table_entry*) * 2*(table->num_entries-i));
return 1;
}
st_index_t i = find_packed_index(table, *key);
if (i < table->num_entries) {
if (value != 0) *value = (st_data_t)table->bins[i*2+1];
table->num_entries--;
memmove(&table->bins[i*2], &table->bins[(i+1)*2],
sizeof(struct st_table_entry*) * 2*(table->num_entries-i));
return 1;
}
if (value != 0) *value = 0;
return 0;
......
for (prev = &table->bins[hash_val]; (ptr = *prev) != 0; prev = &ptr->next) {
if (EQUAL(table, *key, ptr->key)) {
*prev = ptr->next;
REMOVE_ENTRY(table, ptr);
remove_entry(table, ptr);
if (value != 0) *value = ptr->record;
*key = ptr->key;
free(ptr);
st_free_entry(ptr);
return 1;
}
}
......
register st_table_entry *ptr;
if (table->entries_packed) {
st_index_t i;
for (i = 0; i < table->num_entries; i++) {
if ((st_data_t)table->bins[i*2] == *key) {
if (value != 0) *value = (st_data_t)table->bins[i*2+1];
table->bins[i*2] = (void *)never;
return 1;
}
st_index_t i = find_packed_index(table, *key);
if (i < table->num_entries) {
if (value != 0) *value = (st_data_t)table->bins[i*2+1];
table->bins[i*2] = (void *)never;
return 1;
}
if (value != 0) *value = 0;
return 0;
......
for (; ptr != 0; ptr = ptr->next) {
if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
REMOVE_ENTRY(table, ptr);
remove_entry(table, ptr);
*key = ptr->key;
if (value != 0) *value = ptr->record;
ptr->key = ptr->record = never;
......
if (ptr->key == never) {
tmp = ptr;
*last = ptr = ptr->next;
free(tmp);
st_free_entry(tmp);
}
else {
ptr = *(last = &ptr->next);
......
if (ptr == tmp) {
tmp = ptr->fore;
*last = ptr->next;
REMOVE_ENTRY(table, ptr);
free(ptr);
remove_entry(table, ptr);
st_free_entry(ptr);
if (ptr == tmp) return 0;
ptr = tmp;
break;
......
if (ptr == tmp) {
tmp = ptr->back;
*last = ptr->next;
REMOVE_ENTRY(table, ptr);
remove_entry(table, ptr);
free(ptr);
ptr = tmp;
break;
(4-4/6)