Changeset bc216a0 in mainline for uspace/lib/block/libblock.c


Ignore:
Timestamp:
2012-08-07T22:13:44Z (12 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
da68871a
Parents:
b17518e
Message:

Refactored any users of hash_table to use opaque void* keys instead of the cumbersome unsigned long[] keys. Switched from the ad hoc computations of hashes of multiple values to hash_combine().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/block/libblock.c

    rb17518e rbc216a0  
    254254}
    255255
    256 static size_t cache_key_hash(unsigned long *key)
    257 {
    258         /* As recommended by Effective Java, 2nd Edition. */
    259         size_t hash = 17;
    260         hash = 31 * hash + key[1];
    261         hash = 31 * hash + key[0];
    262         return hash;
    263 }
    264 
    265 static size_t cache_hash(const link_t *item)
    266 {
    267         block_t *b = hash_table_get_instance(item, block_t, hash_link);
    268         unsigned long key[] = {
    269                 LOWER32(b->lba),
    270                 UPPER32(b->lba)
    271         };
    272        
    273         return cache_key_hash(key);
    274 }
    275 
    276 static bool cache_match(unsigned long *key, size_t keys, const link_t *item)
    277 {
    278         block_t *b = hash_table_get_instance(item, block_t, hash_link);
    279         return b->lba == MERGE_LOUP32(key[0], key[1]);
     256static size_t cache_key_hash(void *key)
     257{
     258        aoff64_t *lba = (aoff64_t*)key;
     259        return *lba;
     260}
     261
     262static size_t cache_hash(const ht_link_t *item)
     263{
     264        block_t *b = hash_table_get_inst(item, block_t, hash_link);
     265        return b->lba;
     266}
     267
     268static bool cache_key_equal(void *key, const ht_link_t *item)
     269{
     270        aoff64_t *lba = (aoff64_t*)key;
     271        block_t *b = hash_table_get_inst(item, block_t, hash_link);
     272        return b->lba == *lba;
    280273}
    281274
     
    284277        .hash = cache_hash,
    285278        .key_hash = cache_key_hash,
    286         .match = cache_match,
     279        .key_equal = cache_key_equal,
    287280        .equal = 0,
    288281        .remove_callback = 0
     
    317310        cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
    318311
    319         if (!hash_table_create(&cache->block_hash, 0, 2, &cache_ops)) {
     312        if (!hash_table_create(&cache->block_hash, 0, 0, &cache_ops)) {
    320313                free(cache);
    321314                return ENOMEM;
     
    355348                }
    356349
    357                 unsigned long key[2] = {
    358                         LOWER32(b->lba),
    359                         UPPER32(b->lba)
    360                 };
    361                 hash_table_remove(&cache->block_hash, key, 2);
     350                hash_table_remove_item(&cache->block_hash, &b->hash_link);
    362351               
    363352                free(b->data);
     
    391380        fibril_rwlock_initialize(&b->contents_lock);
    392381        link_initialize(&b->free_link);
    393         link_initialize(&b->hash_link);
    394382}
    395383
     
    411399        cache_t *cache;
    412400        block_t *b;
    413         link_t *l;
    414         unsigned long key[2] = {
    415                 LOWER32(ba),
    416                 UPPER32(ba)
    417         };
     401        link_t *link;
    418402
    419403        int rc;
     
    431415
    432416        fibril_mutex_lock(&cache->lock);
    433         l = hash_table_find(&cache->block_hash, key);
    434         if (l) {
     417        ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba);
     418        if (hlink) {
    435419found:
    436420                /*
    437421                 * We found the block in the cache.
    438422                 */
    439                 b = hash_table_get_instance(l, block_t, hash_link);
     423                b = hash_table_get_inst(hlink, block_t, hash_link);
    440424                fibril_mutex_lock(&b->lock);
    441425                if (b->refcnt++ == 0)
     
    475459                                goto out;
    476460                        }
    477                         l = list_first(&cache->free_list);
    478                         b = list_get_instance(l, block_t, free_link);
     461                        link = list_first(&cache->free_list);
     462                        b = list_get_instance(link, block_t, free_link);
    479463
    480464                        fibril_mutex_lock(&b->lock);
     
    516500                                        goto retry;
    517501                                }
    518                                 l = hash_table_find(&cache->block_hash, key);
    519                                 if (l) {
     502                                hlink = hash_table_find(&cache->block_hash, &ba);
     503                                if (hlink) {
    520504                                        /*
    521505                                         * Someone else must have already
     
    539523                         */
    540524                        list_remove(&b->free_link);
    541                         unsigned long temp_key[2] = {
    542                                 LOWER32(b->lba),
    543                                 UPPER32(b->lba)
    544                         };
    545                         hash_table_remove(&cache->block_hash, temp_key, 2);
     525                        hash_table_remove_item(&cache->block_hash, &b->hash_link);
    546526                }
    547527
     
    663643                         * Take the block out of the cache and free it.
    664644                         */
    665                         unsigned long key[2] = {
    666                                 LOWER32(block->lba),
    667                                 UPPER32(block->lba)
    668                         };
    669                         hash_table_remove(&cache->block_hash, key, 2);
     645                        hash_table_remove_item(&cache->block_hash, &block->hash_link);
    670646                        fibril_mutex_unlock(&block->lock);
    671647                        free(block->data);
Note: See TracChangeset for help on using the changeset viewer.