degate  0.1.2
Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | Friends
degate::TileCache< PixelPolicy > Class Template Reference

The TileCache class handles caching of image tiles. More...

#include <TileCache.h>

Inheritance diagram for degate::TileCache< PixelPolicy >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 TileCache (std::string const &_directory, unsigned int _tile_width_exp, bool _persistent, unsigned int _min_cache_tiles=4)
 Create a TileCache object.
 ~TileCache ()
 Destroy a TileCache object.
void print () const
std::shared_ptr< MemoryMap
< typename
PixelPolicy::pixel_type > > 
get_tile (unsigned int x, unsigned int y)
 Get a tile.

Protected Member Functions

void cleanup_cache ()
 Remove the oldest entry from the cache.

Private Types

typedef std::shared_ptr
< MemoryMap< typename
PixelPolicy::pixel_type > > 
MemoryMap_shptr
typedef std::map< std::string,
std::pair< MemoryMap_shptr,
struct timespec > > 
cache_type

Private Member Functions

size_t get_image_size () const
 Get image size in bytes.
std::shared_ptr< MemoryMap
< typename
PixelPolicy::pixel_type > > 
load (std::string const &filename) const
 Load a tile from an image file.

Private Attributes

const std::string directory
const unsigned int tile_width_exp
const bool persistent
cache_type cache
MemoryMap_shptr current_tile
unsigned curr_tile_num_x
unsigned curr_tile_num_y

Friends

class GlobalTileCache

Detailed Description

template<class PixelPolicy>
class degate::TileCache< PixelPolicy >

The TileCache class handles caching of image tiles.

The implementation keeps track how old the cached tile is. If new tiles become loaded, old tiles are removed from the cache. You can control the numer of cached tiles via the constructor parameter _min_cache_tiles. The memory requirement is around _min_cache_tiles*sizeof(PixelPolicy::pixel_type)*(2^_tile_width_exp)^2 , where sizeof(PixelPolicy::pixel_type) is the size of a pixel.

Definition at line 239 of file TileCache.h.


Member Typedef Documentation

template<class PixelPolicy >
typedef std::map< std::string, std::pair<MemoryMap_shptr, struct timespec> > degate::TileCache< PixelPolicy >::cache_type [private]

Definition at line 247 of file TileCache.h.

template<class PixelPolicy >
typedef std::shared_ptr<MemoryMap<typename PixelPolicy::pixel_type> > degate::TileCache< PixelPolicy >::MemoryMap_shptr [private]

Definition at line 245 of file TileCache.h.


Constructor & Destructor Documentation

template<class PixelPolicy >
degate::TileCache< PixelPolicy >::TileCache ( std::string const &  _directory,
unsigned int  _tile_width_exp,
bool  _persistent,
unsigned int  _min_cache_tiles = 4 
) [inline]

Create a TileCache object.

Parameters:
_directoryThe directory where all the tiles are for a TileImage.
_tile_width_exp
_persistent

Definition at line 270 of file TileCache.h.

                                                 :
      directory(_directory),
      tile_width_exp(_tile_width_exp),
      persistent(_persistent) {}
template<class PixelPolicy >
degate::TileCache< PixelPolicy >::~TileCache ( ) [inline]

Destroy a TileCache object.

Definition at line 282 of file TileCache.h.

References degate::TileCache< PixelPolicy >::cache, degate::TileCache< PixelPolicy >::get_image_size(), degate::SingletonBase< GlobalTileCache >::get_instance(), and degate::GlobalTileCache::release_cache_memory().

                 {
      if(cache.size() > 0) {
        GlobalTileCache & gtc = GlobalTileCache::get_instance();
        gtc.release_cache_memory(this, cache.size() * get_image_size());
      }
    }

Here is the call graph for this function:


Member Function Documentation

template<class PixelPolicy >
void degate::TileCache< PixelPolicy >::cleanup_cache ( ) [inline, protected, virtual]

Remove the oldest entry from the cache.

Implements degate::TileCacheBase.

Definition at line 356 of file TileCache.h.

References degate::TileCache< PixelPolicy >::cache, debug(), GET_CLOCK, degate::TileCache< PixelPolicy >::get_image_size(), degate::SingletonBase< GlobalTileCache >::get_instance(), and TM.

                         {

      if(cache.size() == 0) return;

      struct timespec oldest_clock_val;
      GET_CLOCK(oldest_clock_val);

      typename cache_type::iterator oldest = cache.begin();

      for(typename cache_type::iterator iter = cache.begin();
          iter != cache.end(); ++iter) {

        struct timespec clock_val = (*iter).second.second;
        if(clock_val < oldest_clock_val) {
          oldest_clock_val.tv_sec = clock_val.tv_sec;
          oldest_clock_val.tv_nsec = clock_val.tv_nsec;
          oldest = iter;
        }
      }

      assert(oldest != cache.end());
      (*oldest).second.first.reset(); // explicit reset of smart pointer
      cache.erase(oldest);
#ifdef TILECACHE_DEBUG
      debug(TM, "local cache: %d entries after remove\n", cache.size());
#endif
      GlobalTileCache & gtc = GlobalTileCache::get_instance();
      gtc.release_cache_memory(this, get_image_size());

    }

Here is the call graph for this function:

template<class PixelPolicy >
size_t degate::TileCache< PixelPolicy >::get_image_size ( ) const [inline, private]

Get image size in bytes.

Definition at line 393 of file TileCache.h.

References degate::TileCache< PixelPolicy >::tile_width_exp.

Referenced by degate::TileCache< PixelPolicy >::cleanup_cache(), degate::TileCache< PixelPolicy >::get_tile(), and degate::TileCache< PixelPolicy >::~TileCache().

                                  {
      return sizeof(typename PixelPolicy::pixel_type) * (1<< tile_width_exp) * (1<< tile_width_exp);
    }

Here is the caller graph for this function:

template<class PixelPolicy >
std::shared_ptr<MemoryMap<typename PixelPolicy::pixel_type> > degate::TileCache< PixelPolicy >::get_tile ( unsigned int  x,
unsigned int  y 
) [inline]

Get a tile.

If the tile is not in the cache, the tile is loaded.

Parameters:
xAbsolut pixel coordinate.
yAbsolut pixel coordinate.
Returns:
Returns a shared pointer to a MemoryMap object.

Definition at line 311 of file TileCache.h.

References degate::TileCache< PixelPolicy >::cache, degate::TileCache< PixelPolicy >::curr_tile_num_x, degate::TileCache< PixelPolicy >::curr_tile_num_y, degate::TileCache< PixelPolicy >::current_tile, GET_CLOCK, degate::TileCache< PixelPolicy >::get_image_size(), degate::SingletonBase< GlobalTileCache >::get_instance(), degate::TileCache< PixelPolicy >::load(), degate::GlobalTileCache::print_table(), degate::GlobalTileCache::request_cache_memory(), and degate::TileCache< PixelPolicy >::tile_width_exp.

                                                    {

      unsigned int tile_num_x = x >> tile_width_exp;
      unsigned int tile_num_y = y >> tile_width_exp;

      if(!(current_tile != NULL &&
           tile_num_x == curr_tile_num_x &&
           tile_num_y == curr_tile_num_y)) {

        // create a file name from tile number
        char filename[PATH_MAX];
        snprintf(filename, sizeof(filename), "%d_%d.dat", tile_num_x, tile_num_y);
        //debug(TM, "filename is: [%s]", filename);

        // if filename/ object is not in cache, load the tile
        typename cache_type::const_iterator iter = cache.find(filename);

        if(iter == cache.end()) {
          //cleanup_cache();
          GlobalTileCache & gtc = GlobalTileCache::get_instance();
          bool ok = gtc.request_cache_memory(this, get_image_size());
          assert(ok == true);
          struct timespec now;
          GET_CLOCK(now);

          cache[filename] = std::make_pair(load(filename), now);
#ifdef TILECACHE_DEBUG
          gtc.print_table();
#endif
        }

        current_tile = cache[filename].first;
        curr_tile_num_x = tile_num_x;
        curr_tile_num_y = tile_num_y;

      }

      return current_tile;
    }

Here is the call graph for this function:

template<class PixelPolicy >
std::shared_ptr<MemoryMap<typename PixelPolicy::pixel_type> > degate::TileCache< PixelPolicy >::load ( std::string const &  filename) const [inline, private]

Load a tile from an image file.

Parameters:
filenameJust the name of the file to load. The filename is relative to the directory.

Definition at line 403 of file TileCache.h.

References degate::TileCache< PixelPolicy >::directory, degate::join_pathes(), degate::MAP_STORAGE_TYPE_PERSISTENT_FILE, and degate::TileCache< PixelPolicy >::tile_width_exp.

Referenced by degate::TileCache< PixelPolicy >::get_tile().

                                          {

      //debug(TM, "directory: [%s] file: [%s]", directory.c_str(), filename.c_str());
      MemoryMap_shptr mem(new MemoryMap<typename PixelPolicy::pixel_type>
                          (1 << tile_width_exp,
                           1 << tile_width_exp,
                           MAP_STORAGE_TYPE_PERSISTENT_FILE,
                           join_pathes(directory, filename)));

      return mem;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class PixelPolicy >
void degate::TileCache< PixelPolicy >::print ( ) const [inline, virtual]

Implements degate::TileCacheBase.

Definition at line 289 of file TileCache.h.

References degate::TileCache< PixelPolicy >::cache, and degate::TileCache< PixelPolicy >::directory.

                       {
      for(typename cache_type::const_iterator iter = cache.begin();
          iter != cache.end(); ++iter) {
        std::cout << "\t+ "
                  << directory << "/"
                  << (*iter).first << " "
                  << (*iter).second.second.tv_sec
                  << "/"
                  << (*iter).second.second.tv_nsec
                  << std::endl;
      }
    }

Friends And Related Function Documentation

template<class PixelPolicy >
friend class GlobalTileCache [friend]

Definition at line 241 of file TileCache.h.


Member Data Documentation

template<class PixelPolicy >
cache_type degate::TileCache< PixelPolicy >::cache [private]
template<class PixelPolicy >
unsigned degate::TileCache< PixelPolicy >::curr_tile_num_x [mutable, private]

Definition at line 257 of file TileCache.h.

Referenced by degate::TileCache< PixelPolicy >::get_tile().

template<class PixelPolicy >
unsigned degate::TileCache< PixelPolicy >::curr_tile_num_y [mutable, private]

Definition at line 258 of file TileCache.h.

Referenced by degate::TileCache< PixelPolicy >::get_tile().

template<class PixelPolicy >
MemoryMap_shptr degate::TileCache< PixelPolicy >::current_tile [mutable, private]

Definition at line 256 of file TileCache.h.

Referenced by degate::TileCache< PixelPolicy >::get_tile().

template<class PixelPolicy >
const std::string degate::TileCache< PixelPolicy >::directory [private]
template<class PixelPolicy >
const bool degate::TileCache< PixelPolicy >::persistent [private]

Definition at line 251 of file TileCache.h.

template<class PixelPolicy >
const unsigned int degate::TileCache< PixelPolicy >::tile_width_exp [private]

The documentation for this class was generated from the following file: