Commit 7a766395 authored by Pekka Pessi's avatar Pekka Pessi

htable.h, htable2.h: freeing old hash table after resize.

Ensuring that resized hash table has enough slots for existing entries.

darcs-hash:20071102142310-65a35-fde9929c9a7d65f23aaafa5e5311ab58f7d78bf2.gz
parent 9a2e20f3
......@@ -18,7 +18,7 @@ bin_PROGRAMS = addrinfo localinfo
check_PROGRAMS = torture_su torture_su_port \
torture_su_alloc torture_su_time torture_su_tag \
test_htable torture_rbtree torture_heap \
test_htable test_htable2 torture_rbtree torture_heap \
test_memmem torture_su_bm \
torture_su_root torture_su_timer \
test_su su_proxy test_poll $(OSXPROGS)
......@@ -28,7 +28,7 @@ check_PROGRAMS = torture_su torture_su_port \
TESTS = torture_su torture_su_port \
torture_su_alloc torture_su_time torture_su_tag \
test_htable torture_rbtree torture_heap \
test_htable test_htable2 torture_rbtree torture_heap \
test_memmem torture_su_bm \
torture_su_root torture_su_timer \
run_addrinfo run_localinfo run_test_su \
......
......@@ -141,6 +141,8 @@ int prefix##_resize(su_home_t *home, \
new_size = 2 * pr->pr##_size + 1; \
if (new_size < HTABLE_MIN_SIZE) \
new_size = HTABLE_MIN_SIZE; \
if (new_size < 5 * pr->pr##_used / 4) \
new_size = 5 * pr->pr##_used / 4; \
\
if (!(new_hash = su_zalloc(home, sizeof(*new_hash) * new_size))) \
return -1; \
......@@ -169,6 +171,8 @@ int prefix##_resize(su_home_t *home, \
pr->pr##_table = new_hash, pr->pr##_size = new_size; \
\
assert(pr->pr##_used == used); \
\
su_free(home, old_hash); \
\
return 0; \
} \
......
......@@ -30,8 +30,9 @@
*
* Hash tables templates, take 2.
*
* Note: this version can handle structures as entries, and it can be used
* without <sofia-sip/su_alloc.h>.
* Note: this version stores the given element types as entries (instead of
* always storing a pointer to element). It can be used without
* <sofia-sip/su_alloc.h>.
*
* This file contain a hash table template for C. The hash tables are
* resizeable, and they usually contain pointers to entries. The
......@@ -66,21 +67,24 @@ typedef unsigned long hash_value_t;
/** Declare hash table structure type.
*
* The macro HTABLE2_DECLARE() expands to a declaration for hash table
* structure. The its typedef will be <em>prefix</em><code>_t</code>, the
* field names start with @a pr. The entry type is @a entrytype.
* The macro HTABLE2_DECLARE2() expands to a declaration for hash table
* structure. The its typedef will be @a type, the field names start with @a
* pr. The entry type is @a entrytype.
*
* @param type hash table typedef
* @param sname name of struct
* @param prefix hash table type and function prefix
* @param pr hash table field prefix
* @param entrytype entry type
*/
#define HTABLE2_DECLARE2(type, sname, pr, entrytype, size_t) \
typedef struct sname { \
size_t pr##size; \
size_t pr##used; \
entrytype *pr##table; \
} type
#define HTABLE2_DECLARE(sname, prefix, pr, entrytype) \
struct sname { \
unsigned pr##size; \
unsigned pr##used; \
entrytype *pr##table; /**< Hash table itself */ \
}
HTABLE2_DECLARE2(prefix##t, sname, pr, entrytype, unsigned)
#ifndef HTABLE2_SCOPE
/** Default scope for hash table functions. */
......@@ -89,27 +93,30 @@ struct sname { \
/** Prototypes for hash table
*
* The macro HTABLE2_PROTOS() expands to the prototypes of hash table
* The macro HTABLE2_PROTOS2() expands to the prototypes of hash table
* functions. The function and type names start with @a prefix, the field
* names start with @a pr. The entry type is @a entrytype.
*
* @param type hash table typedef
* @param prefix function prefix
* @param pr hash table field prefix
* @param pr hash table field prefix (not used)
* @param entrytype entry type
*/
#define HTABLE2_PROTOS(type, prefix, pr, entrytype) \
HTABLE2_SCOPE int prefix##_resize(void *a, type pr[1], unsigned); \
#define HTABLE2_PROTOS2(type, prefix, pr, entrytype) \
HTABLE2_SCOPE int prefix##_resize(void *a, type *, usize_t); \
HTABLE2_SCOPE int prefix##_is_full(type const *); \
HTABLE2_SCOPE entrytype *prefix##_hash(type const *, hash_value_t hv); \
HTABLE2_SCOPE entrytype *prefix##_next(type const *, entrytype *ee); \
HTABLE2_SCOPE void prefix##_append(type *pr, entrytype e); \
HTABLE2_SCOPE void prefix##_insert(type *pr, entrytype e); \
HTABLE2_SCOPE int prefix##_remove(type *, entrytype const e)
HTABLE2_SCOPE entrytype *prefix##_hash(type const *, hash_value_t); \
HTABLE2_SCOPE entrytype *prefix##_next(type const *, entrytype *); \
HTABLE2_SCOPE entrytype *prefix##_append(type *, entrytype); \
HTABLE2_SCOPE entrytype *prefix##_insert(type *, entrytype); \
HTABLE2_SCOPE int prefix##_remove(type *, entrytype const)
#define HTABLE2_PROTOS(type, prefix, pr, entrytype) \
HTABLE2_PROTOS2(type, prefix, pr, entrytype)
/** Hash table implementation.
*
* The macro HTABLE2_BODIES() expands the hash table functions. The function
* The macro HTABLE2_BODIES2() expands the hash table functions. The function
* and type names start with @a prefix, the field names start with @a pr.
* The entry type is @a entrytype. The function (or macro) name returning
* hash value of each entry is given as @a hfun.
......@@ -118,25 +125,26 @@ HTABLE2_SCOPE int prefix##_remove(type *, entrytype const e)
* @param prefix function prefix for hash table
* @param pr field prefix for hash table
* @param entrytype type of entry element
* @param size_t size_t type
* @param hfun function or macro returning hash value of entry
* @param is_used function or macro returning true if entry is occupied
* @param reclaim function or macro zeroing entry
* @param is_equal equality test
* @param halloc function allocating or freeing memory
*/
#define HTABLE2_BODIES(type, prefix, pr, entrytype, \
hfun, is_used, reclaim, is_equal, halloc) \
#define HTABLE2_BODIES2(type, prefix, pr, entrytype, size_t, \
hfun, is_used, reclaim, is_equal, halloc) \
/** Reallocate new hash table */ \
HTABLE2_SCOPE \
int prefix##_resize(void *realloc_arg, \
type pr[1], \
unsigned new_size) \
usize_t new_size) \
{ \
entrytype *new_hash; \
entrytype *old_hash = pr->pr##table; \
unsigned old_size; \
unsigned i, j, i0; \
unsigned again = 0, used = 0, collisions = 0; \
usize_t old_size; \
usize_t i, j, i0; \
usize_t again = 0, used = 0, collisions = 0; \
\
(void)realloc_arg; \
\
......@@ -144,11 +152,15 @@ int prefix##_resize(void *realloc_arg, \
new_size = 2 * pr->pr##size + 1; \
if (new_size < HTABLE2_MIN_SIZE) \
new_size = HTABLE2_MIN_SIZE; \
if (new_size < 5 * pr->pr##used / 4) \
new_size = 5 * pr->pr##used / 4; \
\
if (!(new_hash = halloc(realloc_arg, NULL, sizeof(*new_hash) * new_size))) \
return -1; \
\
memset(new_hash, 0, sizeof(*new_hash) * new_size); \
for (i = 0; i < new_size; i++) { \
(reclaim(&new_hash[i])); \
} \
old_size = pr->pr##size; \
\
do for (j = 0; j < old_size; j++) { \
......@@ -202,9 +214,13 @@ entrytype *prefix##_next(type const *pr, entrytype *ee) \
} \
\
HTABLE2_SCOPE \
void prefix##_append(type *pr, entrytype e) \
entrytype *prefix##_append(type *pr, entrytype e) \
{ \
entrytype *ee; \
\
assert(pr->pr##used < pr->pr##size); \
if (pr->pr##used == pr->pr##size) \
return (entrytype *)0; \
\
pr->pr##used++; \
for (ee = prefix##_hash(pr, hfun(e)); \
......@@ -212,13 +228,19 @@ void prefix##_append(type *pr, entrytype e) \
ee = prefix##_next(pr, ee)) \
; \
*ee = e; \
\
return ee; \
} \
\
HTABLE2_SCOPE \
void prefix##_insert(type *pr, entrytype e) \
entrytype *prefix##_insert(type *pr, entrytype e) \
{ \
entrytype e0; \
entrytype *ee; \
\
assert(pr->pr##used < pr->pr##size); \
if (pr->pr##used == pr->pr##size) \
return (entrytype *)0; \
\
pr->pr##used++; \
/* Insert entry into hash table (before other entries with same hash) */ \
......@@ -227,12 +249,14 @@ void prefix##_insert(type *pr, entrytype e) \
ee = prefix##_next(pr, ee)) \
*ee = e, e = e0; \
*ee = e; \
\
return ee; \
} \
\
HTABLE2_SCOPE \
int prefix##_remove(type *pr, entrytype const e) \
{ \
unsigned i, j, k, size = pr->pr##size; \
usize_t i, j, k, size = pr->pr##size; \
entrytype *htable = pr->pr##table; \
\
/* Search for entry */ \
......@@ -263,4 +287,10 @@ int prefix##_remove(type *pr, entrytype const e) \
} \
extern int const prefix##_dummy
#define HTABLE2_BODIES(type, prefix, pr, entrytype, \
hfun, is_used, reclaim, is_equal, halloc) \
HTABLE2_BODIES2(type, prefix, pr, entrytype, unsigned, \
hfun, is_used, reclaim, is_equal, halloc)
#endif /** !defined(HTABLE2_H) */
......@@ -157,18 +157,23 @@ void zap(context_t *c, entry_t *e)
su_free(c->c_home, e);
}
/*
* Check that all n entries with hash h are in hash table
* and they are stored in same order
* in which they were added to the hash
*/
static unsigned long count(context_t *c, hash_value_t h)
{
entry_t *e, **ee;
unsigned long n;
unsigned long n, expect = 1;
for (ee = htable_hash(c->c_hash, h), n = 0;
(e = *ee);
ee = htable_next(c->c_hash, ee)) {
if (e->e_value != h)
continue;
if (e->e_n == n)
n++;
if (e->e_n == expect)
n++, expect++;
}
return n;
......@@ -218,9 +223,9 @@ int table_test(int flags)
for correct ordering */
hash_value_t size = c->c_hash->ht_size, h = size - 1;
TEST_1(add(c, h, 0)); TEST_1(add(c, h, 1)); TEST_1(add(c, h, 2));
TEST_1(add(c, h, 3)); TEST_1(add(c, h, 4)); TEST_1(add(c, h, 5));
TEST_1(add(c, h, 6)); TEST_1(add(c, h, 7)); TEST_1(add(c, h, 8));
TEST_1(add(c, h, 1)); TEST_1(add(c, h, 2)); TEST_1(add(c, h, 3));
TEST_1(add(c, h, 4)); TEST_1(add(c, h, 5)); TEST_1(add(c, h, 6));
TEST_1(add(c, h, 7)); TEST_1(add(c, h, 8)); TEST_1(add(c, h, 9));
TEST(count(c, h), 9);
......
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**@ingroup su_htable
*
* @CFILE test_htable.c
*
* Test functions for the @b su library hash table implementation.
*
* @internal
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
*
* @date Created: Tue Aug 21 15:18:26 2001 ppessi
*/
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <assert.h>
#include "sofia-sip/su_alloc.h"
#include "sofia-sip/htable2.h"
#define TSTFLAGS flags
#include <sofia-sip/tstdef.h>
char const name[] = "test_htable2";
void usage(int exitcode)
{
fprintf(stderr, "usage: %s [-v|--verbatim] [-a|--abort]\n", name);
exit(exitcode);
}
static int table2_test(int flags);
int main(int argc, char *argv[])
{
int flags = 0;
int retval = 0;
int i;
for (i = 1; argv[i]; i++) {
if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbatim") == 0)
flags |= tst_verbatim;
else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--abort") == 0)
flags |= tst_abort;
else
usage(1);
}
retval |= table2_test(flags); fflush(stdout);
return retval;
}
typedef struct hentry_s entry_t;
HTABLE2_DECLARE2(htable2_t, htable2_s, ht2_, entry_t, size_t);
HTABLE2_PROTOS2(htable2_t, htable2, ht2_, entry_t);
struct hentry_s
{
unsigned long e_hash;
unsigned long e_n;
};
#define HENTRY_HASH(e) ((e).e_hash)
#define HENTRY_IS_USED(e) ((e).e_n != 0)
#define HENTRY_REMOVE(e) ((e)->e_n = 0, (e)->e_hash = 0)
#define HENTRY_IS_EQUAL(a, b) ((a).e_n == (b).e_n)
#define HALLOC(home, old, newsize) (su_realloc(home, old, newsize))
HTABLE2_BODIES2(htable2_t, htable2, ht2_, entry_t, size_t,
HENTRY_HASH, HENTRY_IS_USED, HENTRY_REMOVE, HENTRY_IS_EQUAL,
HALLOC);
typedef struct context_s
{
su_home_t c_home[1];
htable2_t c_hash[1];
} context_t;
context_t *context_create(void)
{
context_t *c = su_home_clone(NULL, sizeof(*c));
if (c)
htable2_resize(c->c_home, c->c_hash, 0);
return c;
}
static
entry_t *search(context_t *c, unsigned long value, unsigned long n, int add)
{
htable2_t *ht = c->c_hash;
entry_t *e;
unsigned long hash = value;
/* Search for entry in hash table */
for (e = htable2_hash(ht, hash);
e->e_n != 0;
e = htable2_next(ht, e)) {
if (e->e_hash == value && e->e_n == n)
return e;
}
if (add) {
entry_t entry;
/* Resize hash table */
if (htable2_is_full(ht)) {
htable2_resize(c->c_home, ht, 0);
fprintf(stderr, "htable: resized to "MOD_ZU" with "MOD_ZU" entries\n",
ht->ht2_size, ht->ht2_used);
}
/* Add an entry */
e = &entry, e->e_hash = value, e->e_n = n;
return htable2_append(ht, *e);
}
return NULL;
}
static int add(context_t *c, unsigned long value, unsigned long n)
{
return search(c, value, n, 1) != NULL;
}
static
void zap(context_t *c, entry_t e)
{
htable2_remove(c->c_hash, e);
}
/*
* Check that all n entries with hash h are in hash table
* and they are stored in same order
* in which they were added to the hash
*/
static unsigned long count(context_t *c, hash_value_t h)
{
entry_t *e;
unsigned long n;
unsigned long expect = 1;
for (e = htable2_hash(c->c_hash, h), n = 0;
e->e_n != 0;
e = htable2_next(c->c_hash, e)) {
if (e->e_hash != h)
continue;
if (e->e_n == expect)
n++, expect++;
}
return n;
}
int table2_test(int flags)
{
context_t *c;
entry_t *e, e0;
BEGIN();
TEST_1(c = context_create());
TEST_1(add(c, 0, 1)); TEST_1(c->c_hash->ht2_table[0].e_n == 1);
TEST_1(add(c, 1, 2)); TEST_1(c->c_hash->ht2_table[1].e_n == 2);
TEST_1(add(c, 2, 3)); TEST_1(c->c_hash->ht2_table[2].e_n == 3);
TEST_1(add(c, 0, 4)); TEST_1(c->c_hash->ht2_table[3].e_n == 4);
TEST_1(add(c, 2, 5)); TEST_1(c->c_hash->ht2_table[4].e_n == 5);
TEST_1(e = search(c, 1, 2, 0));
e0 = *e;
TEST(htable2_remove(c->c_hash, e0), 0);
TEST(htable2_remove(c->c_hash, e0), -1);
/* after remove , 4 is mode to [1], 5 to [4] */
TEST(c->c_hash->ht2_table[0].e_n, 1);
TEST(c->c_hash->ht2_table[1].e_n, 4);
TEST(c->c_hash->ht2_table[2].e_n, 3);
TEST(c->c_hash->ht2_table[3].e_n, 5);
TEST(c->c_hash->ht2_table[4].e_n, 0);
zap(c, c->c_hash->ht2_table[0]);
/* after remove , 4 is mode to [1], 5 to [4] */
TEST(c->c_hash->ht2_table[0].e_n, 4);
TEST(c->c_hash->ht2_table[1].e_n, 0);
TEST(c->c_hash->ht2_table[2].e_n, 3);
TEST(c->c_hash->ht2_table[3].e_n, 5);
TEST(c->c_hash->ht2_table[4].e_n, 0);
TEST_1(add(c, 0, 6)); TEST(c->c_hash->ht2_table[1].e_n, 6);
TEST_1(add(c, 1, 7)); TEST(c->c_hash->ht2_table[4].e_n, 7);
/* Test that zapping entry 0 does not move 2 and 3 */
zap(c, c->c_hash->ht2_table[0]);
TEST(c->c_hash->ht2_table[4].e_n, 0);
{
/* Insert entries at the end of hash, then resize and check
for correct ordering */
size_t size = c->c_hash->ht2_size;
hash_value_t h = (hash_value_t)size - 1;
TEST_1(add(c, h, 1)); TEST_1(add(c, h, 2)); TEST_1(add(c, h, 3));
TEST_1(add(c, h, 4)); TEST_1(add(c, h, 5)); TEST_1(add(c, h, 6));
TEST_1(add(c, h, 7)); TEST_1(add(c, h, 8)); TEST_1(add(c, h, 9));
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
TEST(htable2_resize(c->c_home, c->c_hash, ++size), 0);
TEST(count(c, h), 9);
}
TEST_VOID(su_home_unref(c->c_home));
END();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment