Ticket #1741: 0003-make-variable-names-in-expand_hash-more-meaningful.patch

File 0003-make-variable-names-in-expand_hash-more-meaningful.patch, 6.2 KB (added by luben, 4 years ago)
  • src/hash.c

    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  
    782782expand_hash(PARROT_INTERP, ARGMOD(Hash *hash)) 
    783783{ 
    784784    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; 
    787787    HashBucket * const old_offset = (HashBucket *)((char *)hash + sizeof (Hash)); 
    788788 
     789    void *        new_mem; 
    789790    void * const  old_mem    = hash->buckets; 
    790791    const UINTVAL old_size   = hash->mask + 1; 
    791792    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 ; 
    793794    size_t        offset, i; 
    794795 
    795796    /* 
     
    806807    /* resize mem */ 
    807808    if (old_offset != old_mem) { 
    808809        /* 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( 
    810811                interp, old_mem, HASH_ALLOC_SIZE(new_size), HASH_ALLOC_SIZE(old_size)); 
    811812    } 
    812813    else { 
    813814        /* 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( 
    815816                interp, HASH_ALLOC_SIZE(new_size)); 
    816817        memcpy(new_mem, old_mem, HASH_ALLOC_SIZE(old_size)); 
    817818    } 
    818819 
    819820    /* 
    820821         +---+---+---+---+---+---+-+-+-+-+-+-+-+-+ 
    821          |  buckets  | old_bi    |  new_bi       | 
     822         |  buckets  | old_index    |  new_index       | 
    822823         +---+---+---+---+---+---+-+-+-+-+-+-+-+-+ 
    823824         ^                       ^ 
    824825         | new_mem               | hash->index 
    825826    */ 
    826827 
    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)); 
    830833 
    831834    /* things can have moved by this offset */ 
    832     offset = (char *)new_mem - (char *)old_mem; 
     835    offset = (char *)new_buckets - (char *)old_buckets; 
    833836 
    834837    /* 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 *)); 
    836839 
    837840    /* 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; 
    841844 
    842845    /* 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); 
    844847 
    845848    /* 
    846849     * reloc pointers - this part would be also needed, if we 
     
    851854    if (offset) { 
    852855        size_t j; 
    853856        for (j = 0; j < old_size; ++j) { 
    854             HashBucket **next_p = new_bi + j; 
     857            HashBucket **next_p = new_index + j; 
    855858            while (*next_p) { 
    856859                *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; 
    859862            } 
    860863        } 
    861864    } 
    862865 
    863866    /* recalc bucket index */ 
    864867    for (i = 0; i < old_size; ++i) { 
    865         HashBucket **next_p = new_bi + i; 
     868        HashBucket **next_p = new_index + i; 
    866869 
    867         while ((b = *next_p) != NULL) { 
     870        while ((bucket = *next_p) != NULL) { 
    868871            /* rehash the bucket */ 
    869872            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; 
    871874 
    872875            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; 
    876879            } 
    877880            else 
    878                 next_p = &b->next; 
     881                next_p = &bucket->next; 
    879882        } 
    880883    } 
    881884 
    882885    /* add new buckets to free_list in reverse order 
    883886     * 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; 
    888893    } 
    889894} 
    890895 
     
    10231028    hash->seed       = interp->hash_seed; 
    10241029    hash->mask       = INITIAL_BUCKETS - 1; 
    10251030    hash->entries    = 0; 
     1031    hash->free_list  = NULL; 
     1032 
     1033 
    10261034 
    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)); 
    10291037 
    10301038    /* fill free_list from hi addresses so that we can use 
    10311039     * buckets[i] directly in an OrderedHash, *if* nothing 
    10321040     * was deleted */ 
    10331041 
    1034     hash->buckets = bp; 
    1035     bp += N_BUCKETS(INITIAL_BUCKETS); 
    1036     hash->index = (HashBucket **)bp; 
     1042    bp = (hash->buckets + N_BUCKETS(INITIAL_BUCKETS)); 
    10371043 
    10381044    for (i = 0, --bp; i < N_BUCKETS(INITIAL_BUCKETS); ++i, --bp) { 
    10391045        bp->next        = hash->free_list;