From 02f85d320ebaadf37f9475677f6bec773b9d4224 Mon Sep 17 00:00:00 2001
From: Luben Karavelov <karavelov@spnet.net>
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
|
b
|
|
782 | 782 | expand_hash(PARROT_INTERP, ARGMOD(Hash *hash)) |
783 | 783 | { |
784 | 784 | ASSERT_ARGS(expand_hash) |
785 | | HashBucket **old_bi, **new_bi; |
786 | | HashBucket *bs, *b, *new_mem; |
| 785 | HashBucket **old_index, **new_index; |
| 786 | HashBucket *old_buckets,*new_buckets, *bucket; |
787 | 787 | HashBucket * const old_offset = (HashBucket *)((char *)hash + sizeof (Hash)); |
788 | 788 | |
| 789 | void * new_mem; |
789 | 790 | void * const old_mem = hash->buckets; |
790 | 791 | const UINTVAL old_size = hash->mask + 1; |
791 | 792 | const UINTVAL new_size = old_size << 1; /* Double. Right-shift is 2x */ |
792 | | const UINTVAL old_nb = N_BUCKETS(old_size); |
| 793 | const UINTVAL new_mask = new_size - 1 ; |
793 | 794 | size_t offset, i; |
794 | 795 | |
795 | 796 | /* |
… |
… |
|
806 | 807 | /* resize mem */ |
807 | 808 | if (old_offset != old_mem) { |
808 | 809 | /* This buffer has been reallocated at least once before. */ |
809 | | new_mem = (HashBucket *)Parrot_gc_reallocate_memory_chunk_with_interior_pointers( |
| 810 | new_mem = Parrot_gc_reallocate_memory_chunk_with_interior_pointers( |
810 | 811 | interp, old_mem, HASH_ALLOC_SIZE(new_size), HASH_ALLOC_SIZE(old_size)); |
811 | 812 | } |
812 | 813 | else { |
813 | 814 | /* Allocate a new buffer. */ |
814 | | new_mem = (HashBucket *)Parrot_gc_allocate_memory_chunk_with_interior_pointers( |
| 815 | new_mem = Parrot_gc_allocate_memory_chunk_with_interior_pointers( |
815 | 816 | interp, HASH_ALLOC_SIZE(new_size)); |
816 | 817 | memcpy(new_mem, old_mem, HASH_ALLOC_SIZE(old_size)); |
817 | 818 | } |
818 | 819 | |
819 | 820 | /* |
820 | 821 | +---+---+---+---+---+---+-+-+-+-+-+-+-+-+ |
821 | | | buckets | old_bi | new_bi | |
| 822 | | buckets | old_index | new_index | |
822 | 823 | +---+---+---+---+---+---+-+-+-+-+-+-+-+-+ |
823 | 824 | ^ ^ |
824 | 825 | | new_mem | hash->index |
825 | 826 | */ |
826 | 827 | |
827 | | bs = new_mem; |
828 | | old_bi = (HashBucket **)(bs + old_nb); |
829 | | new_bi = (HashBucket **)(bs + N_BUCKETS(new_size)); |
| 828 | old_buckets = hash->buckets; |
| 829 | old_index = hash->index; |
| 830 | |
| 831 | new_buckets = (HashBucket *) new_mem; |
| 832 | new_index = (HashBucket **)(new_buckets + N_BUCKETS(new_size)); |
830 | 833 | |
831 | 834 | /* things can have moved by this offset */ |
832 | | offset = (char *)new_mem - (char *)old_mem; |
| 835 | offset = (char *)new_buckets - (char *)old_buckets; |
833 | 836 | |
834 | 837 | /* relocate the bucket index */ |
835 | | mem_sys_memmove(new_bi, old_bi, old_size * sizeof (HashBucket *)); |
| 838 | mem_sys_memmove(new_index, old_index, old_size * sizeof (HashBucket *)); |
836 | 839 | |
837 | 840 | /* update hash data */ |
838 | | hash->index = new_bi; |
839 | | hash->buckets = bs; |
840 | | hash->mask = new_size - 1; |
| 841 | hash->index = new_index; |
| 842 | hash->buckets = new_buckets; |
| 843 | hash->mask = new_mask; |
841 | 844 | |
842 | 845 | /* clear freshly allocated bucket index */ |
843 | | memset(new_bi + old_size, 0, sizeof (HashBucket *) * old_size); |
| 846 | memset(new_index + old_size, 0, sizeof (HashBucket *) * old_size); |
844 | 847 | |
845 | 848 | /* |
846 | 849 | * reloc pointers - this part would be also needed, if we |
… |
… |
|
851 | 854 | if (offset) { |
852 | 855 | size_t j; |
853 | 856 | for (j = 0; j < old_size; ++j) { |
854 | | HashBucket **next_p = new_bi + j; |
| 857 | HashBucket **next_p = new_index + j; |
855 | 858 | while (*next_p) { |
856 | 859 | *next_p = (HashBucket *)((char *)*next_p + offset); |
857 | | b = *next_p; |
858 | | next_p = &b->next; |
| 860 | bucket = *next_p; |
| 861 | next_p = &bucket->next; |
859 | 862 | } |
860 | 863 | } |
861 | 864 | } |
862 | 865 | |
863 | 866 | /* recalc bucket index */ |
864 | 867 | for (i = 0; i < old_size; ++i) { |
865 | | HashBucket **next_p = new_bi + i; |
| 868 | HashBucket **next_p = new_index + i; |
866 | 869 | |
867 | | while ((b = *next_p) != NULL) { |
| 870 | while ((bucket = *next_p) != NULL) { |
868 | 871 | /* rehash the bucket */ |
869 | 872 | const size_t new_loc = |
870 | | get_hash_val(interp, hash, b->key) & (new_size - 1); |
| 873 | get_hash_val(interp, hash, bucket->key) & new_mask; |
871 | 874 | |
872 | 875 | if (i != new_loc) { |
873 | | *next_p = b->next; |
874 | | b->next = new_bi[new_loc]; |
875 | | new_bi[new_loc] = b; |
| 876 | *next_p = bucket->next; |
| 877 | bucket->next = new_index[new_loc]; |
| 878 | new_index[new_loc] = bucket; |
876 | 879 | } |
877 | 880 | else |
878 | | next_p = &b->next; |
| 881 | next_p = &bucket->next; |
879 | 882 | } |
880 | 883 | } |
881 | 884 | |
882 | 885 | /* add new buckets to free_list in reverse order |
883 | 886 | * lowest bucket is top on free list and will be used first */ |
884 | | for (i = 0, b = (HashBucket *)new_bi - 1; i < old_nb; ++i, --b) { |
885 | | b->next = hash->free_list; |
886 | | b->key = b->value = NULL; |
887 | | hash->free_list = b; |
| 887 | |
| 888 | bucket = (HashBucket *)new_index - 1; |
| 889 | for (i = 0; i < N_BUCKETS(old_size); ++i, --bucket) { |
| 890 | bucket->next = hash->free_list; |
| 891 | bucket->key = bucket->value = NULL; |
| 892 | hash->free_list = bucket; |
888 | 893 | } |
889 | 894 | } |
890 | 895 | |
… |
… |
|
1023 | 1028 | hash->seed = interp->hash_seed; |
1024 | 1029 | hash->mask = INITIAL_BUCKETS - 1; |
1025 | 1030 | hash->entries = 0; |
| 1031 | hash->free_list = NULL; |
| 1032 | |
| 1033 | |
1026 | 1034 | |
1027 | | bp = (HashBucket *)((char *)alloc + sizeof (Hash)); |
1028 | | hash->free_list = NULL; |
| 1035 | hash->buckets = (HashBucket *)((char *)alloc + sizeof (Hash));; |
| 1036 | hash->index = (HashBucket **)(hash->buckets + N_BUCKETS(INITIAL_BUCKETS)); |
1029 | 1037 | |
1030 | 1038 | /* fill free_list from hi addresses so that we can use |
1031 | 1039 | * buckets[i] directly in an OrderedHash, *if* nothing |
1032 | 1040 | * was deleted */ |
1033 | 1041 | |
1034 | | hash->buckets = bp; |
1035 | | bp += N_BUCKETS(INITIAL_BUCKETS); |
1036 | | hash->index = (HashBucket **)bp; |
| 1042 | bp = (hash->buckets + N_BUCKETS(INITIAL_BUCKETS)); |
1037 | 1043 | |
1038 | 1044 | for (i = 0, --bp; i < N_BUCKETS(INITIAL_BUCKETS); ++i, --bp) { |
1039 | 1045 | bp->next = hash->free_list; |