Ignore:
File:
1 edited

Legend:

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

    r9d58539 rbc216a0  
    6262static LIST_INITIALIZE(dcl);
    6363
    64 #define CACHE_BUCKETS_LOG2  10
    65 #define CACHE_BUCKETS       (1 << CACHE_BUCKETS_LOG2)
    6664
    6765typedef struct {
     
    256254}
    257255
    258 static hash_index_t cache_hash(unsigned long *key)
    259 {
    260         return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1);
    261 }
    262 
    263 static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
    264 {
    265         block_t *b = hash_table_get_instance(item, block_t, hash_link);
    266         return b->lba == MERGE_LOUP32(key[0], key[1]);
    267 }
    268 
    269 static void cache_remove_callback(link_t *item)
    270 {
    271 }
    272 
    273 static hash_table_operations_t cache_ops = {
     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;
     273}
     274
     275
     276static hash_table_ops_t cache_ops = {
    274277        .hash = cache_hash,
    275         .compare = cache_compare,
    276         .remove_callback = cache_remove_callback
     278        .key_hash = cache_key_hash,
     279        .key_equal = cache_key_equal,
     280        .equal = 0,
     281        .remove_callback = 0
    277282};
    278283
     
    305310        cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
    306311
    307         if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2,
    308             &cache_ops)) {
     312        if (!hash_table_create(&cache->block_hash, 0, 0, &cache_ops)) {
    309313                free(cache);
    310314                return ENOMEM;
     
    344348                }
    345349
    346                 unsigned long key[2] = {
    347                         LOWER32(b->lba),
    348                         UPPER32(b->lba)
    349                 };
    350                 hash_table_remove(&cache->block_hash, key, 2);
     350                hash_table_remove_item(&cache->block_hash, &b->hash_link);
    351351               
    352352                free(b->data);
     
    380380        fibril_rwlock_initialize(&b->contents_lock);
    381381        link_initialize(&b->free_link);
    382         link_initialize(&b->hash_link);
    383382}
    384383
     
    400399        cache_t *cache;
    401400        block_t *b;
    402         link_t *l;
    403         unsigned long key[2] = {
    404                 LOWER32(ba),
    405                 UPPER32(ba)
    406         };
     401        link_t *link;
    407402
    408403        int rc;
     
    420415
    421416        fibril_mutex_lock(&cache->lock);
    422         l = hash_table_find(&cache->block_hash, key);
    423         if (l) {
     417        ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba);
     418        if (hlink) {
    424419found:
    425420                /*
    426421                 * We found the block in the cache.
    427422                 */
    428                 b = hash_table_get_instance(l, block_t, hash_link);
     423                b = hash_table_get_inst(hlink, block_t, hash_link);
    429424                fibril_mutex_lock(&b->lock);
    430425                if (b->refcnt++ == 0)
     
    464459                                goto out;
    465460                        }
    466                         l = list_first(&cache->free_list);
    467                         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);
    468463
    469464                        fibril_mutex_lock(&b->lock);
     
    505500                                        goto retry;
    506501                                }
    507                                 l = hash_table_find(&cache->block_hash, key);
    508                                 if (l) {
     502                                hlink = hash_table_find(&cache->block_hash, &ba);
     503                                if (hlink) {
    509504                                        /*
    510505                                         * Someone else must have already
     
    528523                         */
    529524                        list_remove(&b->free_link);
    530                         unsigned long temp_key[2] = {
    531                                 LOWER32(b->lba),
    532                                 UPPER32(b->lba)
    533                         };
    534                         hash_table_remove(&cache->block_hash, temp_key, 2);
     525                        hash_table_remove_item(&cache->block_hash, &b->hash_link);
    535526                }
    536527
     
    540531                b->lba = ba;
    541532                b->pba = ba_ltop(devcon, b->lba);
    542                 hash_table_insert(&cache->block_hash, key, &b->hash_link);
     533                hash_table_insert(&cache->block_hash, &b->hash_link);
    543534
    544535                /*
     
    652643                         * Take the block out of the cache and free it.
    653644                         */
    654                         unsigned long key[2] = {
    655                                 LOWER32(block->lba),
    656                                 UPPER32(block->lba)
    657                         };
    658                         hash_table_remove(&cache->block_hash, key, 2);
     645                        hash_table_remove_item(&cache->block_hash, &block->hash_link);
    659646                        fibril_mutex_unlock(&block->lock);
    660647                        free(block->data);
Note: See TracChangeset for help on using the changeset viewer.