Ptex
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
PtexReaderCache Class Reference

Cache for reading Ptex texture files. More...

#include <PtexCache.h>

Inheritance diagram for PtexReaderCache:
PtexCache

Classes

struct  MruList
 
struct  Purger
 

Public Member Functions

 PtexReaderCache (int maxFiles, size_t maxMem, bool premultiply, PtexInputHandler *inputHandler, PtexErrorHandler *errorHandler)
 
 ~PtexReaderCache ()
 
virtual void release ()
 Release PtexCache. Cache will be immediately destroyed and all resources will be released. More...
 
virtual void setSearchPath (const char *path)
 Set a search path for finding textures. More...
 
virtual const char * getSearchPath ()
 Query the search path. More...
 
virtual PtexTextureget (const char *path, Ptex::String &error)
 Access a texture. More...
 
virtual void purge (PtexTexture *)
 Remove a texture file from the cache. More...
 
virtual void purge (const char *)
 Remove a texture file from the cache by pathname. More...
 
virtual void purgeAll ()
 Remove all texture files from the cache. More...
 
virtual void getStats (Stats &stats)
 Get stats. More...
 
void purge (PtexCachedReader *reader)
 
void adjustMemUsed (size_t amount)
 
void adjustFilesOpen (size_t amount)
 
void logRecentlyUsed (PtexCachedReader *reader)
 

Private Types

typedef PtexHashMap< StringKey, PtexCachedReader * > FileMap
 

Private Member Functions

bool findFile (const char *&filename, std::string &buffer, Ptex::String &error)
 
void processMru ()
 
void pruneFiles ()
 
void pruneData ()
 
 CACHE_LINE_PAD (_memUsed, size_t)
 
 CACHE_LINE_PAD (_filesOpen, size_t)
 
 CACHE_LINE_PAD (_mruLock, Mutex)
 

Private Attributes

size_t _maxFiles
 
size_t _maxMem
 
PtexInputHandler_io
 
PtexErrorHandler_err
 
std::string _searchpath
 
std::vector< std::string > _searchdirs
 
FileMap _files
 
bool _premultiply
 
volatile size_t _memUsed
 
volatile size_t _filesOpen
 
Mutex _mruLock
 
MruList _mruLists [2]
 
MruList *volatile _mruList
 
MruList *volatile _prevMruList
 
PtexLruList< PtexCachedReader, &PtexCachedReader::_openFilesItem_openFiles
 
PtexLruList< PtexCachedReader, &PtexCachedReader::_activeFilesItem_activeFiles
 
size_t _peakMemUsed
 
size_t _peakFilesOpen
 
size_t _fileOpens
 
size_t _blockReads
 

Static Private Attributes

static const int numMruFiles = 50
 

Additional Inherited Members

- Static Public Member Functions inherited from PtexCache
static PtexCachecreate (int maxFiles, size_t maxMem, bool premultiply=false, PtexInputHandler *inputHandler=0, PtexErrorHandler *errorHandler=0)
 Create a cache with the specified limits. More...
 
- Protected Member Functions inherited from PtexCache
virtual ~PtexCache ()
 Destructor not for public use. Use release() instead. More...
 

Detailed Description

Cache for reading Ptex texture files.

Definition at line 197 of file PtexCache.h.

Member Typedef Documentation

◆ FileMap

Definition at line 284 of file PtexCache.h.

Constructor & Destructor Documentation

◆ PtexReaderCache()

PtexReaderCache::PtexReaderCache ( int  maxFiles,
size_t  maxMem,
bool  premultiply,
PtexInputHandler inputHandler,
PtexErrorHandler errorHandler 
)
inline

Definition at line 200 of file PtexCache.h.

References _filesOpen, _memUsed, _mruLists, _mruLock, and CACHE_LINE_PAD_INIT.

◆ ~PtexReaderCache()

PtexReaderCache::~PtexReaderCache ( )
inline

Definition at line 211 of file PtexCache.h.

Member Function Documentation

◆ adjustFilesOpen()

void PtexReaderCache::adjustFilesOpen ( size_t  amount)
inline

Definition at line 259 of file PtexCache.h.

References _filesOpen, _peakFilesOpen, AtomicAdd(), and PtexUtils::max().

Referenced by processMru().

◆ adjustMemUsed()

void PtexReaderCache::adjustMemUsed ( size_t  amount)
inline

Definition at line 253 of file PtexCache.h.

References _memUsed, _peakMemUsed, AtomicAdd(), and PtexUtils::max().

Referenced by get(), processMru(), pruneData(), purge(), and purgeAll().

◆ CACHE_LINE_PAD() [1/3]

PtexReaderCache::CACHE_LINE_PAD ( _filesOpen  ,
size_t   
)
private

◆ CACHE_LINE_PAD() [2/3]

PtexReaderCache::CACHE_LINE_PAD ( _memUsed  ,
size_t   
)
private

◆ CACHE_LINE_PAD() [3/3]

PtexReaderCache::CACHE_LINE_PAD ( _mruLock  ,
Mutex   
)
private

◆ findFile()

bool PtexReaderCache::findFile ( const char *&  filename,
std::string &  buffer,
Ptex::String error 
)
private

Definition at line 91 of file PtexCache.cpp.

References _searchdirs, and Ptex::String::c_str().

Referenced by get().

◆ get()

PtexTexture * PtexReaderCache::get ( const char *  path,
Ptex::String error 
)
virtual

Access a texture.

If the specified path was previously accessed from the cache, then a pointer to the cached texture will be returned.

If the specified path hasn't been opened yet or was purged from the cache (via the purge or purgeAll methods) then the file will be opened. If the path is relative (i.e. doesn't begin with a '/') then the search path will be used to locate the file.

The texture will be accessible until the PtexTexture::release method is called, at which point the texture will be returned to the cache. Once released, the texture may have it's data pruned (immediately or some time later) to stay within the maximum cache size.

If the texture could not be opened, null will be returned and an error string will be set. If an error were previously encountered with the file (include the file not being found), null will be returned and no error string will be set.

Parameters
pathFile path. If path is relative, search path will be used to find the file.
errorError string set if texture could not be opened.

Implements PtexCache.

Definition at line 121 of file PtexCache.cpp.

References _err, _files, _io, _premultiply, adjustMemUsed(), findFile(), PtexHashMap< Key, Value >::get(), PtexReader::invalidate(), PtexReader::logOpen(), PtexReader::needToOpen(), PtexReader::ok(), PtexReader::open(), PtexReader::pendingPurge(), purge(), PtexCachedReader::ref(), PtexHashMap< Key, Value >::tryInsert(), and PtexCachedReader::unref().

◆ getSearchPath()

virtual const char* PtexReaderCache::getSearchPath ( )
inlinevirtual

Query the search path.

Returns string set via setSearchPath.

Implements PtexCache.

Definition at line 239 of file PtexCache.h.

References _searchpath.

◆ getStats()

void PtexReaderCache::getStats ( Stats stats)
virtual

◆ logRecentlyUsed()

void PtexReaderCache::logRecentlyUsed ( PtexCachedReader reader)

◆ processMru()

void PtexReaderCache::processMru ( )
private

◆ pruneData()

void PtexReaderCache::pruneData ( )
private

◆ pruneFiles()

void PtexReaderCache::pruneFiles ( )
private

Definition at line 254 of file PtexCache.cpp.

References _filesOpen, _maxFiles, _openFiles, PtexLruList< T, item >::pop(), and PtexReader::tryClose().

Referenced by processMru().

◆ purge() [1/3]

void PtexReaderCache::purge ( const char *  path)
virtual

Remove a texture file from the cache by pathname.

The path must match the full path as opened. This function will not search for the file, but if a search path was used, the path must match the path as found by the search path.

Implements PtexCache.

Definition at line 296 of file PtexCache.cpp.

References _files, PtexHashMap< Key, Value >::get(), and purge().

◆ purge() [2/3]

void PtexReaderCache::purge ( PtexCachedReader reader)

Definition at line 303 of file PtexCache.cpp.

References adjustMemUsed(), and PtexCachedReader::tryPurge().

◆ purge() [3/3]

void PtexReaderCache::purge ( PtexTexture texture)
virtual

Remove a texture file from the cache.

If the texture is in use by another thread, that reference will remain valid and the file will be purged once it is no longer in use. This texture should be released immediately after purging.

Implements PtexCache.

Definition at line 287 of file PtexCache.cpp.

References PtexCachedReader::ref(), and PtexCachedReader::unref().

Referenced by get(), and purge().

◆ purgeAll()

void PtexReaderCache::purgeAll ( )
virtual

Remove all texture files from the cache.

Textures with active PtexTexture* handles will remain valid and will be purged upon release.

Implements PtexCache.

Definition at line 319 of file PtexCache.cpp.

References _files, adjustMemUsed(), PtexHashMap< Key, Value >::foreach(), and PtexReaderCache::Purger::memUsedChangeTotal.

◆ release()

virtual void PtexReaderCache::release ( )
inlinevirtual

Release PtexCache. Cache will be immediately destroyed and all resources will be released.

Implements PtexCache.

Definition at line 214 of file PtexCache.h.

◆ setSearchPath()

virtual void PtexReaderCache::setSearchPath ( const char *  path)
inlinevirtual

Set a search path for finding textures.

Note: if an input handler is installed the search path will be ignored.

Parameters
pathcolon-delimited search path.

Implements PtexCache.

Definition at line 216 of file PtexCache.h.

References _searchdirs, and _searchpath.

Member Data Documentation

◆ _activeFiles

PtexLruList<PtexCachedReader, &PtexCachedReader::_activeFilesItem> PtexReaderCache::_activeFiles
private

Definition at line 301 of file PtexCache.h.

Referenced by processMru(), and pruneData().

◆ _blockReads

size_t PtexReaderCache::_blockReads
private

Definition at line 306 of file PtexCache.h.

Referenced by getStats(), and processMru().

◆ _err

PtexErrorHandler* PtexReaderCache::_err
private

Definition at line 281 of file PtexCache.h.

Referenced by get().

◆ _fileOpens

size_t PtexReaderCache::_fileOpens
private

Definition at line 305 of file PtexCache.h.

Referenced by getStats(), and processMru().

◆ _files

FileMap PtexReaderCache::_files
private

Definition at line 285 of file PtexCache.h.

Referenced by get(), getStats(), purge(), and purgeAll().

◆ _filesOpen

volatile size_t PtexReaderCache::_filesOpen
private

Definition at line 288 of file PtexCache.h.

Referenced by adjustFilesOpen(), getStats(), processMru(), pruneFiles(), and PtexReaderCache().

◆ _io

PtexInputHandler* PtexReaderCache::_io
private

Definition at line 280 of file PtexCache.h.

Referenced by get().

◆ _maxFiles

size_t PtexReaderCache::_maxFiles
private

Definition at line 278 of file PtexCache.h.

Referenced by processMru(), and pruneFiles().

◆ _maxMem

size_t PtexReaderCache::_maxMem
private

Definition at line 279 of file PtexCache.h.

Referenced by processMru(), and pruneData().

◆ _memUsed

volatile size_t PtexReaderCache::_memUsed
private

Definition at line 287 of file PtexCache.h.

Referenced by adjustMemUsed(), getStats(), processMru(), pruneData(), and PtexReaderCache().

◆ _mruList

MruList* volatile PtexReaderCache::_mruList
private

Definition at line 297 of file PtexCache.h.

Referenced by logRecentlyUsed(), and processMru().

◆ _mruLists

MruList PtexReaderCache::_mruLists[2]
private

Definition at line 296 of file PtexCache.h.

Referenced by PtexReaderCache().

◆ _mruLock

Mutex PtexReaderCache::_mruLock
private

Definition at line 289 of file PtexCache.h.

Referenced by processMru(), and PtexReaderCache().

◆ _openFiles

PtexLruList<PtexCachedReader, &PtexCachedReader::_openFilesItem> PtexReaderCache::_openFiles
private

Definition at line 300 of file PtexCache.h.

Referenced by processMru(), and pruneFiles().

◆ _peakFilesOpen

size_t PtexReaderCache::_peakFilesOpen
private

Definition at line 304 of file PtexCache.h.

Referenced by adjustFilesOpen(), and getStats().

◆ _peakMemUsed

size_t PtexReaderCache::_peakMemUsed
private

Definition at line 303 of file PtexCache.h.

Referenced by adjustMemUsed(), and getStats().

◆ _premultiply

bool PtexReaderCache::_premultiply
private

Definition at line 286 of file PtexCache.h.

Referenced by get().

◆ _prevMruList

MruList* volatile PtexReaderCache::_prevMruList
private

Definition at line 298 of file PtexCache.h.

Referenced by processMru().

◆ _searchdirs

std::vector<std::string> PtexReaderCache::_searchdirs
private

Definition at line 283 of file PtexCache.h.

Referenced by findFile(), and setSearchPath().

◆ _searchpath

std::string PtexReaderCache::_searchpath
private

Definition at line 282 of file PtexCache.h.

Referenced by getSearchPath(), and setSearchPath().

◆ numMruFiles

const int PtexReaderCache::numMruFiles = 50
staticprivate

Definition at line 291 of file PtexCache.h.

Referenced by logRecentlyUsed(), and processMru().


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