Ignore:
File:
1 edited

Legend:

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

    rbc216a0 r9d58539  
    6262static LIST_INITIALIZE(dcl);
    6363
     64#define CACHE_BUCKETS_LOG2  10
     65#define CACHE_BUCKETS       (1 << CACHE_BUCKETS_LOG2)
    6466
    6567typedef struct {
     
    254256}
    255257
    256 static size_t cache_key_hash(void *key)
    257 {
    258         aoff64_t *lba = (aoff64_t*)key;
    259         return *lba;
    260 }
    261 
    262 static 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 
    268 static 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 
    276 static hash_table_ops_t cache_ops = {
     258static hash_index_t cache_hash(unsigned long *key)
     259{
     260        return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1);
     261}
     262
     263static 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
     269static void cache_remove_callback(link_t *item)
     270{
     271}
     272
     273static hash_table_operations_t cache_ops = {
    277274        .hash = cache_hash,
    278         .key_hash = cache_key_hash,
    279         .key_equal = cache_key_equal,
    280         .equal = 0,
    281         .remove_callback = 0
     275        .compare = cache_compare,
     276        .remove_callback = cache_remove_callback
    282277};
    283278
     
    310305        cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
    311306
    312         if (!hash_table_create(&cache->block_hash, 0, 0, &cache_ops)) {
     307        if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2,
     308            &cache_ops)) {
    313309                free(cache);
    314310                return ENOMEM;
     
    348344                }
    349345
    350                 hash_table_remove_item(&cache->block_hash, &b->hash_link);
     346                unsigned long key[2] = {
     347                        LOWER32(b->lba),
     348                        UPPER32(b->lba)
     349                };
     350                hash_table_remove(&cache->block_hash, key, 2);
    351351               
    352352                free(b->data);
     
    380380        fibril_rwlock_initialize(&b->contents_lock);
    381381        link_initialize(&b->free_link);
     382        link_initialize(&b->hash_link);
    382383}
    383384
     
    399400        cache_t *cache;
    400401        block_t *b;
    401         link_t *link;
     402        link_t *l;
     403        unsigned long key[2] = {
     404                LOWER32(ba),
     405                UPPER32(ba)
     406        };
    402407
    403408        int rc;
     
    415420
    416421        fibril_mutex_lock(&cache->lock);
    417         ht_link_t *hlink = hash_table_find(&cache->block_hash, &ba);
    418         if (hlink) {
     422        l = hash_table_find(&cache->block_hash, key);
     423        if (l) {
    419424found:
    420425                /*
    421426                 * We found the block in the cache.
    422427                 */
    423                 b = hash_table_get_inst(hlink, block_t, hash_link);
     428                b = hash_table_get_instance(l, block_t, hash_link);
    424429                fibril_mutex_lock(&b->lock);
    425430                if (b->refcnt++ == 0)
     
    459464                                goto out;
    460465                        }
    461                         link = list_first(&cache->free_list);
    462                         b = list_get_instance(link, block_t, free_link);
     466                        l = list_first(&cache->free_list);
     467                        b = list_get_instance(l, block_t, free_link);
    463468
    464469                        fibril_mutex_lock(&b->lock);
     
    500505                                        goto retry;
    501506                                }
    502                                 hlink = hash_table_find(&cache->block_hash, &ba);
    503                                 if (hlink) {
     507                                l = hash_table_find(&cache->block_hash, key);
     508                                if (l) {
    504509                                        /*
    505510                                         * Someone else must have already
     
    523528                         */
    524529                        list_remove(&b->free_link);
    525                         hash_table_remove_item(&cache->block_hash, &b->hash_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);
    526535                }
    527536
     
    531540                b->lba = ba;
    532541                b->pba = ba_ltop(devcon, b->lba);
    533                 hash_table_insert(&cache->block_hash, &b->hash_link);
     542                hash_table_insert(&cache->block_hash, key, &b->hash_link);
    534543
    535544                /*
     
    643652                         * Take the block out of the cache and free it.
    644653                         */
    645                         hash_table_remove_item(&cache->block_hash, &block->hash_link);
     654                        unsigned long key[2] = {
     655                                LOWER32(block->lba),
     656                                UPPER32(block->lba)
     657                        };
     658                        hash_table_remove(&cache->block_hash, key, 2);
    646659                        fibril_mutex_unlock(&block->lock);
    647660                        free(block->data);
Note: See TracChangeset for help on using the changeset viewer.