From cb64e374dd6767732de6530904ad8452f447ac6f Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Mon, 8 Aug 2016 21:01:40 +0200 Subject: [PATCH] New features of the block cache --- kernel/include/block_cache.hpp | 3 +++ kernel/src/block_cache.cpp | 45 ++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/kernel/include/block_cache.hpp b/kernel/include/block_cache.hpp index cc4aaedb..2f19ba72 100644 --- a/kernel/include/block_cache.hpp +++ b/kernel/include/block_cache.hpp @@ -22,6 +22,9 @@ struct block_cache { void init(uint64_t payload_size, uint64_t blocks); + char* block_if_present(uint16_t device, uint64_t sector); + char* block_if_present(uint64_t key); + char* block(uint16_t device, uint64_t sector, bool& valid); char* block(uint64_t key, bool& valid); }; diff --git a/kernel/src/block_cache.cpp b/kernel/src/block_cache.cpp index b5cbe376..c6951c79 100644 --- a/kernel/src/block_cache.cpp +++ b/kernel/src/block_cache.cpp @@ -48,6 +48,32 @@ void block_cache::init(uint64_t payload_size, uint64_t blocks){ rear = previous; } +char* block_cache::block_if_present(uint16_t device, uint64_t sector){ + return block_if_present((uint64_t(device) << 16) + sector); +} + +char* block_cache::block_if_present(uint64_t orig_key){ + auto key = orig_key % (blocks * 2); + + if(hash_table[key]){ + auto* entry = static_cast(hash_table[key]); + + if(entry[0] == orig_key){ + return reinterpret_cast(reinterpret_cast(hash_table[key]) + 4 * sizeof(uint64_t)); + } else { + while(entry[1]){ + entry = reinterpret_cast(entry[1]); + + if(entry[0] == orig_key){ + return reinterpret_cast(reinterpret_cast(entry) + 4 * sizeof(uint64_t)); + } + } + } + } + + return nullptr; +} + char* block_cache::block(uint16_t device, uint64_t sector, bool& valid){ return block((uint64_t(device) << 16) + sector, valid); } @@ -57,22 +83,11 @@ char* block_cache::block(uint64_t orig_key, bool& valid){ // First, try to get it directly from the hash table - if(hash_table[key]){ - auto* entry = static_cast(hash_table[key]); + auto direct = block_if_present(orig_key); - if(entry[0] == orig_key){ - valid = true; - return reinterpret_cast(reinterpret_cast(hash_table[key]) + 4 * sizeof(uint64_t)); - } else { - while(entry[1]){ - entry = reinterpret_cast(entry[1]); - - if(entry[0] == orig_key){ - valid = true; - return reinterpret_cast(reinterpret_cast(entry) + 4 * sizeof(uint64_t)); - } - } - } + if(direct){ + valid = true; + return direct; } // At this point, we will allocate a new block