From 02f85d320ebaadf37f9475677f6bec773b9d4224 Mon Sep 17 00:00:00 2001 From: Luben Karavelov Date: Sun, 15 Aug 2010 00:35:14 +0300 Subject: [PATCH 3/7] make variable names in expand_hash more meaningful --- src/hash.c | 74 ++++++++++++++++++++++++++++++++--------------------------- 1 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/hash.c b/src/hash.c index a59b739..f5b73f5 100644 --- a/src/hash.c +++ b/src/hash.c @@ -782,14 +782,15 @@ static void expand_hash(PARROT_INTERP, ARGMOD(Hash *hash)) { ASSERT_ARGS(expand_hash) - HashBucket **old_bi, **new_bi; - HashBucket *bs, *b, *new_mem; + HashBucket **old_index, **new_index; + HashBucket *old_buckets,*new_buckets, *bucket; HashBucket * const old_offset = (HashBucket *)((char *)hash + sizeof (Hash)); + void * new_mem; void * const old_mem = hash->buckets; const UINTVAL old_size = hash->mask + 1; const UINTVAL new_size = old_size << 1; /* Double. Right-shift is 2x */ - const UINTVAL old_nb = N_BUCKETS(old_size); + const UINTVAL new_mask = new_size - 1 ; size_t offset, i; /* @@ -806,41 +807,43 @@ expand_hash(PARROT_INTERP, ARGMOD(Hash *hash)) /* resize mem */ if (old_offset != old_mem) { /* This buffer has been reallocated at least once before. */ - new_mem = (HashBucket *)Parrot_gc_reallocate_memory_chunk_with_interior_pointers( + new_mem = Parrot_gc_reallocate_memory_chunk_with_interior_pointers( interp, old_mem, HASH_ALLOC_SIZE(new_size), HASH_ALLOC_SIZE(old_size)); } else { /* Allocate a new buffer. */ - new_mem = (HashBucket *)Parrot_gc_allocate_memory_chunk_with_interior_pointers( + new_mem = Parrot_gc_allocate_memory_chunk_with_interior_pointers( interp, HASH_ALLOC_SIZE(new_size)); memcpy(new_mem, old_mem, HASH_ALLOC_SIZE(old_size)); } /* +---+---+---+---+---+---+-+-+-+-+-+-+-+-+ - | buckets | old_bi | new_bi | + | buckets | old_index | new_index | +---+---+---+---+---+---+-+-+-+-+-+-+-+-+ ^ ^ | new_mem | hash->index */ - bs = new_mem; - old_bi = (HashBucket **)(bs + old_nb); - new_bi = (HashBucket **)(bs + N_BUCKETS(new_size)); + old_buckets = hash->buckets; + old_index = hash->index; + + new_buckets = (HashBucket *) new_mem; + new_index = (HashBucket **)(new_buckets + N_BUCKETS(new_size)); /* things can have moved by this offset */ - offset = (char *)new_mem - (char *)old_mem; + offset = (char *)new_buckets - (char *)old_buckets; /* relocate the bucket index */ - mem_sys_memmove(new_bi, old_bi, old_size * sizeof (HashBucket *)); + mem_sys_memmove(new_index, old_index, old_size * sizeof (HashBucket *)); /* update hash data */ - hash->index = new_bi; - hash->buckets = bs; - hash->mask = new_size - 1; + hash->index = new_index; + hash->buckets = new_buckets; + hash->mask = new_mask; /* clear freshly allocated bucket index */ - memset(new_bi + old_size, 0, sizeof (HashBucket *) * old_size); + memset(new_index + old_size, 0, sizeof (HashBucket *) * old_size); /* * reloc pointers - this part would be also needed, if we @@ -851,40 +854,42 @@ expand_hash(PARROT_INTERP, ARGMOD(Hash *hash)) if (offset) { size_t j; for (j = 0; j < old_size; ++j) { - HashBucket **next_p = new_bi + j; + HashBucket **next_p = new_index + j; while (*next_p) { *next_p = (HashBucket *)((char *)*next_p + offset); - b = *next_p; - next_p = &b->next; + bucket = *next_p; + next_p = &bucket->next; } } } /* recalc bucket index */ for (i = 0; i < old_size; ++i) { - HashBucket **next_p = new_bi + i; + HashBucket **next_p = new_index + i; - while ((b = *next_p) != NULL) { + while ((bucket = *next_p) != NULL) { /* rehash the bucket */ const size_t new_loc = - get_hash_val(interp, hash, b->key) & (new_size - 1); + get_hash_val(interp, hash, bucket->key) & new_mask; if (i != new_loc) { - *next_p = b->next; - b->next = new_bi[new_loc]; - new_bi[new_loc] = b; + *next_p = bucket->next; + bucket->next = new_index[new_loc]; + new_index[new_loc] = bucket; } else - next_p = &b->next; + next_p = &bucket->next; } } /* add new buckets to free_list in reverse order * lowest bucket is top on free list and will be used first */ - for (i = 0, b = (HashBucket *)new_bi - 1; i < old_nb; ++i, --b) { - b->next = hash->free_list; - b->key = b->value = NULL; - hash->free_list = b; + + bucket = (HashBucket *)new_index - 1; + for (i = 0; i < N_BUCKETS(old_size); ++i, --bucket) { + bucket->next = hash->free_list; + bucket->key = bucket->value = NULL; + hash->free_list = bucket; } } @@ -1023,17 +1028,18 @@ parrot_create_hash(PARROT_INTERP, PARROT_DATA_TYPE val_type, Hash_key_type hkey_ hash->seed = interp->hash_seed; hash->mask = INITIAL_BUCKETS - 1; hash->entries = 0; + hash->free_list = NULL; + + - bp = (HashBucket *)((char *)alloc + sizeof (Hash)); - hash->free_list = NULL; + hash->buckets = (HashBucket *)((char *)alloc + sizeof (Hash));; + hash->index = (HashBucket **)(hash->buckets + N_BUCKETS(INITIAL_BUCKETS)); /* fill free_list from hi addresses so that we can use * buckets[i] directly in an OrderedHash, *if* nothing * was deleted */ - hash->buckets = bp; - bp += N_BUCKETS(INITIAL_BUCKETS); - hash->index = (HashBucket **)bp; + bp = (hash->buckets + N_BUCKETS(INITIAL_BUCKETS)); for (i = 0, --bp; i < N_BUCKETS(INITIAL_BUCKETS); ++i, --bp) { bp->next = hash->free_list; -- 1.7.1