Skip to content

st use function instead of macro #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use pool allocators for all hash parts
  • Loading branch information
funny-falcon committed Dec 31, 2011
commit 101821016d5be63b9a24f38f39717b4f1cb27a40
140 changes: 140 additions & 0 deletions pool_alloc.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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->free = HOLDER_SIZE;
for(i = 0; i < HOLDER_SIZE; i++) {
holder->items[i].holder = holder;
entry_chain(&holder->items[i]);
}
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
143 changes: 57 additions & 86 deletions st.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,12 @@

typedef struct st_table_entry st_table_entry;

typedef struct st_entry_holder st_entry_holder;

struct st_table_entry {
st_index_t hash;
st_data_t key;
st_data_t record;
st_table_entry *next;
st_table_entry *fore, *back;
st_entry_holder *holder;
};

#define ST_HOLDER_SIZE (4096 / sizeof(st_table_entry) - 1)
typedef unsigned short st_entry_count;
struct st_entry_holder {
st_entry_count total, free;
st_table_entry entries[ST_HOLDER_SIZE];
};

#define ST_DEFAULT_MAX_DENSITY 5
Expand Down Expand Up @@ -86,80 +76,63 @@ static void rehash(st_table *);
#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)

static st_table_entry *free_entry = NULL;
static st_index_t free_entry_count = 0;

static inline void
st_chain_entry(st_table_entry *entry)
{
entry->fore = free_entry;
entry->back = NULL;
if (free_entry) {
free_entry->back = entry;
}
free_entry = entry;
}

static void
st_entry_holder_alloc()
#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"
#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"
#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"
#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_entry_holder *holder = alloc(st_entry_holder);
unsigned int i;
holder->total = ST_HOLDER_SIZE;
holder->free = holder->total;
for(i = 0; i < holder->total; i++) {
holder->entries[i].holder = holder;
st_chain_entry(&holder->entries[i]);
st_table_entry **result;
if (num_bins == ST_DEFAULT_INIT_TABLE_SIZE) {
result = (st_table_entry **) st_alloc_bins11();
}
free_entry_count+= holder->total;
}

static void
st_entry_holder_free(st_entry_holder *holder)
{
unsigned int i;
st_table_entry *ptr;
for(i = 0; i < holder->total; i++) {
ptr = &holder->entries[i];
if (ptr->fore) {
ptr->fore->back = ptr->back;
}
if (ptr->back) {
ptr->back->fore = ptr->fore;
} else {
free_entry = ptr->fore;
}
else {
result = (st_table_entry **) malloc(num_bins * sizeof(st_table_entry *));
}
free_entry_count-= holder->total;
free(holder);
memset(result, 0, num_bins * sizeof(st_table_entry *));
return result;
}

static void
st_free_entry(st_table_entry *entry)
st_free_bins(st_table_entry **bins, st_index_t num_bins)
{
st_entry_holder *holder = entry->holder;
st_chain_entry(entry);
holder->free++;
free_entry_count++;
if (holder->free == holder->total && free_entry_count > holder->total) {
st_entry_holder_free(holder);
if (num_bins == ST_DEFAULT_INIT_TABLE_SIZE) {
st_free_bins11(
(st_table_entry_p (*)[ST_DEFAULT_INIT_TABLE_SIZE]) bins);
} else {
free(bins);
}
}

static st_table_entry *
st_alloc_entry()
{
st_table_entry *result;
if (!free_entry) {
st_entry_holder_alloc();
}
result = free_entry;
free_entry = result->fore;
result->holder->free--;
free_entry_count--;
return result;
}

/*
* MINSIZE is the minimum size of a dictionary.
*/
Expand Down Expand Up @@ -268,12 +241,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 */

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;

Expand Down Expand Up @@ -351,8 +324,8 @@ void
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
Expand Down Expand Up @@ -625,9 +598,8 @@ rehash(register st_table *table)
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;

Expand All @@ -648,17 +620,16 @@ st_copy(st_table *old_table)
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;
}

Expand All @@ -673,7 +644,7 @@ st_copy(st_table *old_table)
do {
entry = st_alloc_entry();
if (entry == 0) {
st_free_table(new_table);
st_dealloc_table(new_table);
return 0;
}
*entry = *ptr;
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy