|
degate 0.1.1
|
#include <TileCache.h>

Public Member Functions | |
| void | print_table () const |
| bool | request_cache_memory (TileCacheBase *requestor, size_t amount) |
| void | release_cache_memory (TileCacheBase *requestor, size_t amount) |
Private Types | |
| typedef std::pair< struct timespec, size_t > | cache_entry_t |
| typedef std::map < TileCacheBase *, cache_entry_t > | cache_t |
Private Member Functions | |
| GlobalTileCache () | |
| void | remove_oldest () |
Private Attributes | |
| size_t | max_cache_memory |
| size_t | allocated_memory |
| cache_t | cache |
Friends | |
| class | SingletonBase< GlobalTileCache > |
Definition at line 72 of file TileCache.h.
typedef std::pair<struct timespec, size_t> degate::GlobalTileCache::cache_entry_t [private] |
Definition at line 81 of file TileCache.h.
typedef std::map<TileCacheBase *, cache_entry_t> degate::GlobalTileCache::cache_t [private] |
Definition at line 82 of file TileCache.h.
| degate::GlobalTileCache::GlobalTileCache | ( | ) | [inline, private] |
Definition at line 88 of file TileCache.h.
References degate::SingletonBase< GlobalTileCache >::get_instance(), degate::Configuration::get_max_tile_cache_size(), and max_cache_memory.
: allocated_memory(0) { Configuration & conf = Configuration::get_instance(); max_cache_memory = conf.get_max_tile_cache_size() * 1024 *1024; }

| void degate::GlobalTileCache::print_table | ( | ) | const [inline] |
Definition at line 124 of file TileCache.h.
References allocated_memory, cache, and max_cache_memory.
Referenced by degate::TileCache< PixelPolicy >::get_tile(), release_cache_memory(), remove_oldest(), and request_cache_memory().
{
printf("Global Image Tile Cache:\n"
"Used memory : %llu bytes\n"
"Max memory : %llu bytes\n\n"
"Holder | Last access (sec,nsec) | Amount of memory\n"
"-----------------+---------------------------+------------------------------------\n",
(long long unsigned)allocated_memory, (long long unsigned)max_cache_memory);
for(cache_t::const_iterator iter = cache.begin(); iter != cache.end(); ++iter) {
cache_entry_t const& entry = iter->second;
printf("%16p | %12ld.%12ld | %lu M (%lu bytes)\n",
iter->first, entry.first.tv_sec, entry.first.tv_nsec, entry.second/(1024*1024), entry.second);
iter->first->print();
}
printf("\n");
}

| void degate::GlobalTileCache::release_cache_memory | ( | TileCacheBase * | requestor, |
| size_t | amount | ||
| ) | [inline] |
Definition at line 182 of file TileCache.h.
References allocated_memory, cache, debug(), print_table(), and TM.
Referenced by degate::TileCache< PixelPolicy >::~TileCache().
{
#ifdef TILECACHE_DEBUG
debug(TM, "Local cache %p releases %d bytes.", requestor, amount);
#endif
cache_t::iterator found = cache.find(requestor);
if(found == cache.end()) {
debug(TM, "Unknown memory should be released.");
print_table();
assert(1==0);
}
else {
cache_entry_t & entry = found->second;
if(entry.second >= amount) {
entry.second -= amount;
assert(allocated_memory >= amount);
if(allocated_memory >= amount) allocated_memory -= amount;
else {
debug(TM, "More mem to release than available.");
print_table();
assert(1==0);
}
}
else {
print_table();
assert(entry.second >= amount); // will break
}
if(entry.second == 0) {
#ifdef TILECACHE_DEBUG
debug(TM, "Memory completely released. Remove entry from global cache.");
#endif
cache.erase(found);
}
}
}


| void degate::GlobalTileCache::remove_oldest | ( | ) | [inline, private] |
Definition at line 93 of file TileCache.h.
References cache, degate::TileCacheBase::cleanup_cache(), debug(), GET_CLOCK, print_table(), and TM.
Referenced by request_cache_memory().
{
struct timespec now;
GET_CLOCK(now);
TileCacheBase * oldest = NULL;
for(cache_t::iterator iter = cache.begin(); iter != cache.end(); ++iter) {
cache_entry_t & entry = iter->second;
if(entry.first < now) {
now.tv_sec = entry.first.tv_sec;
now.tv_nsec = entry.first.tv_nsec;
oldest = iter->first;
}
}
if(oldest) {
#ifdef TILECACHE_DEBUG
debug(TM, "Will call cleanup on %p", oldest);
#endif
oldest->cleanup_cache();
}
else {
#ifdef TILECACHE_DEBUG
debug(TM, "there is nothing to free.");
print_table();
#endif
}
}


| bool degate::GlobalTileCache::request_cache_memory | ( | TileCacheBase * | requestor, |
| size_t | amount | ||
| ) | [inline] |
Definition at line 142 of file TileCache.h.
References allocated_memory, cache, debug(), GET_CLOCK, max_cache_memory, print_table(), remove_oldest(), and TM.
Referenced by degate::TileCache< PixelPolicy >::get_tile().
{
#ifdef TILECACHE_DEBUG
debug(TM, "Local cache %p requests %d bytes.", requestor, amount);
#endif
while(allocated_memory + amount > max_cache_memory) {
#ifdef TILECACHE_DEBUG
debug(TM, "Try to free memory");
#endif
remove_oldest();
}
if(allocated_memory + amount <= max_cache_memory) {
struct timespec now;
GET_CLOCK(now);
cache_t::iterator found = cache.find(requestor);
if(found == cache.end()) {
cache[requestor] = std::make_pair(now, amount);
}
else {
cache_entry_t & entry = found->second;
entry.first.tv_sec = now.tv_sec;
entry.first.tv_nsec = now.tv_nsec;
entry.second += amount;
}
allocated_memory += amount;
#ifdef TILECACHE_DEBUG
print_table();
#endif
return true;
}
debug(TM, "Can't free memory.");
print_table();
return false;
}


friend class SingletonBase< GlobalTileCache > [friend] |
Definition at line 74 of file TileCache.h.
size_t degate::GlobalTileCache::allocated_memory [private] |
Definition at line 79 of file TileCache.h.
Referenced by print_table(), release_cache_memory(), and request_cache_memory().
cache_t degate::GlobalTileCache::cache [private] |
Definition at line 84 of file TileCache.h.
Referenced by print_table(), release_cache_memory(), remove_oldest(), and request_cache_memory().
size_t degate::GlobalTileCache::max_cache_memory [private] |
Definition at line 78 of file TileCache.h.
Referenced by GlobalTileCache(), print_table(), and request_cache_memory().
1.7.4