Changeset 062d900 in mainline for uspace/lib/block/block.c


Ignore:
Timestamp:
2012-10-09T11:49:43Z (12 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4e00f87
Parents:
87e9392
git-author:
Adam Hraska <adam.hraska+hos@…> (2012-10-09 11:49:43)
git-committer:
Jakub Jermar <jakub@…> (2012-10-09 11:49:43)
Message:

Cherrypick userspace hash table changes from lp:~adam-hraska+lp/helenos/rcu/.

File:
1 edited

Legend:

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

    r87e9392 r062d900  
    6262static LIST_INITIALIZE(dcl);
    6363
    64 #define CACHE_BUCKETS_LOG2  10
    65 #define CACHE_BUCKETS       (1 << CACHE_BUCKETS_LOG2)
    6664
    6765typedef struct {
     
    233231}
    234232
    235 static hash_index_t cache_hash(unsigned long *key)
    236 {
    237         return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1);
    238 }
    239 
    240 static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
    241 {
    242         block_t *b = hash_table_get_instance(item, block_t, hash_link);
    243         return b->lba == MERGE_LOUP32(key[0], key[1]);
    244 }
    245 
    246 static void cache_remove_callback(link_t *item)
    247 {
    248 }
    249 
    250 static hash_table_operations_t cache_ops = {
     233static size_t cache_key_hash(void *key)
     234{
     235        aoff64_t *lba = (aoff64_t*)key;
     236        return *lba;
     237}
     238
     239static size_t cache_hash(const ht_link_t *item)
     240{
     241        block_t *b = hash_table_get_inst(item, block_t, hash_link);
     242        return b->lba;
     243}
     244
     245static bool cache_key_equal(void *key, const ht_link_t *item)
     246{
     247        aoff64_t *lba = (aoff64_t*)key;
     248        block_t *b = hash_table_get_inst(item, block_t, hash_link);
     249        return b->lba == *lba;
     250}
     251
     252
     253static hash_table_ops_t cache_ops = {
    251254        .hash = cache_hash,
    252         .compare = cache_compare,
    253         .remove_callback = cache_remove_callback
     255        .key_hash = cache_key_hash,
     256        .key_equal = cache_key_equal,
     257        .equal = 0,
     258        .remove_callback = 0
    254259};
    255260
     
    282287        cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
    283288
    284         if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2,
    285             &cache_ops)) {
     289        if (!hash_table_create(&cache->block_hash, 0, 0, &cache_ops)) {
    286290                free(cache);
    287291                return ENOMEM;
     
    321325                }
    322326
    323                 unsigned long key[2] = {
    324                         LOWER32(b->lba),
    325                         UPPER32(b->lba)
    326                 };
    327                 hash_table_remove(&cache->block_hash, key, 2);
     327                hash_table_remove_item(&cache->block_hash, &b->hash_link);
    328328               
    329329                free(b->data);
     
    357357        fibril_rwlock_initialize(&b->contents_lock);
    358358        link_initialize(&b->free_link);
    359         link_initialize(&b->hash_link);
    360359}
    361360
     
    377376        cache_t *cache;
    378377        block_t *b;
    379         link_t *l;
    380         unsigned long key[2] = {
    381                 LOWER32(ba),
    382                 UPPER32(ba)
    383         };
     378        link_t *link;
    384379
    385380        int rc;
     
    397392
    398393        fibril_mutex_lock(&cache->lock);
    399         l = hash_table_find(&cache->block_hash, key);
    400         if (l) {
     394        ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba);
     395        if (hlink) {
    401396found:
    402397                /*
    403398                 * We found the block in the cache.
    404399                 */
    405                 b = hash_table_get_instance(l, block_t, hash_link);
     400                b = hash_table_get_inst(hlink, block_t, hash_link);
    406401                fibril_mutex_lock(&b->lock);
    407402                if (b->refcnt++ == 0)
     
    441436                                goto out;
    442437                        }
    443                         l = list_first(&cache->free_list);
    444                         b = list_get_instance(l, block_t, free_link);
     438                        link = list_first(&cache->free_list);
     439                        b = list_get_instance(link, block_t, free_link);
    445440
    446441                        fibril_mutex_lock(&b->lock);
     
    479474                                        goto retry;
    480475                                }
    481                                 l = hash_table_find(&cache->block_hash, key);
    482                                 if (l) {
     476                                hlink = hash_table_find(&cache->block_hash, &ba);
     477                                if (hlink) {
    483478                                        /*
    484479                                         * Someone else must have already
     
    502497                         */
    503498                        list_remove(&b->free_link);
    504                         unsigned long temp_key[2] = {
    505                                 LOWER32(b->lba),
    506                                 UPPER32(b->lba)
    507                         };
    508                         hash_table_remove(&cache->block_hash, temp_key, 2);
     499                        hash_table_remove_item(&cache->block_hash, &b->hash_link);
    509500                }
    510501
     
    514505                b->lba = ba;
    515506                b->pba = ba_ltop(devcon, b->lba);
    516                 hash_table_insert(&cache->block_hash, key, &b->hash_link);
     507                hash_table_insert(&cache->block_hash, &b->hash_link);
    517508
    518509                /*
     
    622613                         * Take the block out of the cache and free it.
    623614                         */
    624                         unsigned long key[2] = {
    625                                 LOWER32(block->lba),
    626                                 UPPER32(block->lba)
    627                         };
    628                         hash_table_remove(&cache->block_hash, key, 2);
     615                        hash_table_remove_item(&cache->block_hash, &block->hash_link);
    629616                        fibril_mutex_unlock(&block->lock);
    630617                        free(block->data);
Note: See TracChangeset for help on using the changeset viewer.