0ad/source/lib/file/io/block_cache.cpp
janwas 9269be9ee3 remove mmgr and macros that redefine malloc/new/free
(see http://www.wildfiregames.com/forum/index.php?showtopic=11450&hl= )

clean up debug module
. no longer include platform-dependent header (-> less rebuilds)
. DISPLAY_ERROR -> DEBUG_DISPLAY_ERROR
. parts of config.h that don't affect all files moved to config.2 (->
fewer full rebuilds)
. remove creaky symbol cache (no longer needed for mmgr)
. remove TLS thread naming stuff (can use debugger's thread window
instead; no need for platform independence there)

wdbg: remove thread suspension and breakpoint APIs (not needed)

acpi: fix: u64 -> uintptr_t

wutil: fix WinScopedLock, use that instead of direct lock() functions

misc:
. get rid of SAFE_STRCPY, replace with strcpy_s
. remove _getcwd (shouldn't be used)

This was SVN commit r5563.
2008-01-19 11:33:11 +00:00

145 lines
3.1 KiB
C++

/**
* =========================================================================
* File : block_cache.cpp
* Project : 0 A.D.
* Description : cache for aligned I/O m_blocks.
* =========================================================================
*/
// license: GPL; see lib/license.txt
#include "precompiled.h"
#include "block_cache.h"
#include "lib/config2.h" // CONFIG2_CACHE_READ_ONLY
#include "lib/file/common/file_stats.h"
#include "lib/lockfree.h"
#include "lib/allocators/pool.h"
#include "lib/fnv_hash.h"
#include "io_internal.h"
//-----------------------------------------------------------------------------
BlockId::BlockId()
: m_id(0)
{
}
BlockId::BlockId(const Path& pathname, off_t ofs)
{
m_id = fnv_hash64(pathname.string().c_str(), pathname.string().length());
const size_t indexBits = 16;
m_id <<= indexBits;
const off_t blockIndex = ofs / BLOCK_SIZE;
debug_assert(blockIndex < (1ul << indexBits));
m_id |= blockIndex;
}
bool BlockId::operator==(const BlockId& rhs) const
{
return m_id == rhs.m_id;
}
bool BlockId::operator!=(const BlockId& rhs) const
{
return !operator==(rhs);
}
//-----------------------------------------------------------------------------
struct Block
{
Block(BlockId id, shared_ptr<u8> buf)
{
this->id = id;
this->buf = buf;
}
// block is "valid" and can satisfy Retrieve() requests if a
// (non-default-constructed) ID has been assigned.
BlockId id;
// this block is "in use" if use_count != 1.
shared_ptr<u8> buf;
};
//-----------------------------------------------------------------------------
class BlockCache::Impl
{
public:
Impl(size_t numBlocks)
: m_maxBlocks(numBlocks)
{
}
void Add(BlockId id, shared_ptr<u8> buf)
{
if(m_blocks.size() > m_maxBlocks)
{
#if CONFIG2_CACHE_READ_ONLY
mprotect((void*)m_blocks.front().buf.get(), BLOCK_SIZE, PROT_READ);
#endif
m_blocks.pop_front(); // evict oldest block
}
#if CONFIG2_CACHE_READ_ONLY
mprotect((void*)buf.get(), BLOCK_SIZE, PROT_WRITE|PROT_READ);
#endif
m_blocks.push_back(Block(id, buf));
}
bool Retrieve(BlockId id, shared_ptr<u8>& buf)
{
// (linear search is ok since we only expect to manage a few blocks)
for(size_t i = 0; i < m_blocks.size(); i++)
{
Block& block = m_blocks[i];
if(block.id == id)
{
buf = block.buf;
return true;
}
}
return false;
}
void InvalidateAll()
{
// note: don't check whether any references are held etc. because
// this should only be called at the end of the (test) program.
m_blocks.clear();
}
private:
size_t m_maxBlocks;
typedef std::deque<Block> Blocks;
Blocks m_blocks;
};
//-----------------------------------------------------------------------------
BlockCache::BlockCache(size_t numBlocks)
: impl(new Impl(numBlocks))
{
}
void BlockCache::Add(BlockId id, shared_ptr<u8> buf)
{
impl->Add(id, buf);
}
bool BlockCache::Retrieve(BlockId id, shared_ptr<u8>& buf)
{
return impl->Retrieve(id, buf);
}
void BlockCache::InvalidateAll()
{
return impl->InvalidateAll();
}