Changeset f1ba5d6 in mainline


Ignore:
Timestamp:
2008-11-02T13:38:56Z (16 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e1c88d5
Parents:
0c243b4
Message:

Add block_cache_init().

Location:
uspace
Files:
3 edited

Legend:

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

    r0c243b4 rf1ba5d6  
    4949#include <futex.h>
    5050#include <libadt/list.h>
     51#include <libadt/hash_table.h>
    5152
    5253/** Lock protecting the device connection list */
     
    5455/** Device connection list head. */
    5556static LIST_INITIALIZE(dcl_head);
     57
     58#define CACHE_BUCKETS_LOG2              10
     59#define CACHE_BUCKETS                   (1 << CACHE_BUCKETS_LOG2)
     60
     61typedef struct {
     62        futex_t lock;
     63        size_t block_size;              /**< Block size. */
     64        unsigned block_count;           /**< Total number of blocks. */
     65        hash_table_t block_hash;
     66        link_t free_head;
     67} cache_t;
    5668
    5769typedef struct {
     
    6476        off_t bb_off;
    6577        size_t bb_size;
     78        cache_t *cache;
    6679} devcon_t;
    6780
     
    100113        devcon->bb_off = 0;
    101114        devcon->bb_size = 0;
     115        devcon->cache = NULL;
    102116
    103117        futex_down(&dcl_lock);
     
    168182        if (devcon->bb_buf)
    169183                free(devcon->bb_buf);
     184
     185        if (devcon->cache) {
     186                hash_table_destroy(&devcon->cache->block_hash);
     187                free(devcon->cache);
     188        }
     189
    170190        munmap(devcon->com_area, devcon->com_size);
    171191        ipc_hangup(devcon->dev_phone);
     
    208228        assert(devcon);
    209229        return devcon->bb_buf;
     230}
     231
     232static hash_index_t cache_hash(unsigned long *key)
     233{
     234        return *key & (CACHE_BUCKETS - 1);
     235}
     236
     237static int cache_compare(unsigned long *key, hash_count_t keys, link_t *item)
     238{
     239        block_t *b = hash_table_get_instance(item, block_t, hash_link);
     240        return b->boff == *key;
     241}
     242
     243static void cache_remove_callback(link_t *item)
     244{
     245}
     246
     247static hash_table_operations_t cache_ops = {
     248        .hash = cache_hash,
     249        .compare = cache_compare,
     250        .remove_callback = cache_remove_callback
     251};
     252
     253int block_cache_init(dev_handle_t dev_handle, size_t size, unsigned blocks)
     254{
     255        devcon_t *devcon = devcon_search(dev_handle);
     256        cache_t *cache;
     257        if (!devcon)
     258                return ENOENT;
     259        if (devcon->cache)
     260                return EEXIST;
     261        cache = malloc(sizeof(cache_t));
     262        if (!cache)
     263                return ENOMEM;
     264       
     265        futex_initialize(&cache->lock, 0);
     266        list_initialize(&cache->free_head);
     267        cache->block_size = size;
     268        cache->block_count = blocks;
     269
     270        if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
     271            &cache_ops)) {
     272                free(cache);
     273                return ENOMEM;
     274        }
     275
     276        devcon->cache = cache;
     277        return EOK;
    210278}
    211279
  • uspace/lib/libblock/libblock.h

    r0c243b4 rf1ba5d6  
    7474extern void *block_bb_get(dev_handle_t);
    7575
     76extern int block_cache_init(dev_handle_t, size_t, unsigned);
     77
    7678extern block_t *block_get(dev_handle_t, off_t, size_t);
    7779extern void block_put(block_t *);
  • uspace/srv/fs/fat/fat_ops.c

    r0c243b4 rf1ba5d6  
    462462        }
    463463
     464        /* Initialize the block cache */
     465        rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
     466        if (rc != EOK) {
     467                block_fini(dev_handle);
     468                ipc_answer_0(rid, rc);
     469                return;
     470        }
     471
    464472        rc = fat_idx_init_by_dev_handle(dev_handle);
    465473        if (rc != EOK) {
Note: See TracChangeset for help on using the changeset viewer.