Ptex: PtexReaderCache Class Reference

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:

## Classes
struct | Purger
struct | MruList

## 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.
virtual void | setSearchPath (const char *path)
| Set a search path for finding textures.
virtual const char * | getSearchPath ()
| Query the search path.
virtual PtexTexture * | get (const char *path, Ptex::String &error)
| Access a texture.
virtual void | purge (PtexTexture *)
| Remove a texture file from the cache.
virtual void | purge (const char *)
| Remove a texture file from the cache by pathname.
virtual void | purgeAll ()
| Remove all texture files from the cache.
virtual void | getStats (Stats &stats)
| Get stats.
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 | pruneFilesIfNeeded ()
void | pruneDataIfNeeded ()
| 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 | maxMruFiles = 50

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

## Detailed Description

Cache for reading Ptex texture files.

Definition at line 210 of file PtexCache.h.

## Member Typedef Documentation

## ◆ FileMap

typedef PtexHashMap<StringKey,PtexCachedReader*> PtexReaderCache::FileMap
| private

Definition at line 297 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 213 of file PtexCache.h.

References _blockReads, _err, _fileOpens, _filesOpen, _io, _maxFiles, _maxMem, _memUsed, _mruList, _mruLists, _mruLock, _peakFilesOpen, _peakMemUsed, _premultiply, _prevMruList, and CACHE_LINE_PAD_INIT.

## ◆ ~PtexReaderCache()

PtexReaderCache::~PtexReaderCache | ( |  | ) |
| inline

Definition at line 224 of file PtexCache.h.

## Member Function Documentation

## ◆ adjustFilesOpen()

void PtexReaderCache::adjustFilesOpen | ( | size_t | amount | ) |
| inline

Definition at line 272 of file PtexCache.h.

References _filesOpen, _peakFilesOpen, and AtomicAdd().

Referenced by processMru().

## ◆ adjustMemUsed()

void PtexReaderCache::adjustMemUsed | ( | size_t | amount | ) |
| inline

Definition at line 266 of file PtexCache.h.

References _memUsed, _peakMemUsed, and AtomicAdd().

Referenced by get(), processMru(), pruneDataIfNeeded(), 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 95 of file PtexCache.cpp.

References _searchdirs.

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

  path | File path. If path is relative, search path will be used to find the file.
  error | Error string set if texture could not be opened.

Implements PtexCache.

Definition at line 125 of file PtexCache.cpp.

References _err, _files, _io, _premultiply, adjustMemUsed(), findFile(), PtexReader::invalidate(), PtexReader::logOpen(), PtexReader::needToOpen(), PtexReader::ok(), PtexReader::open(), PtexReader::pendingPurge(), purge(), PtexCachedReader::ref(), and PtexCachedReader::unref().

## ◆ getSearchPath()

virtual const char * PtexReaderCache::getSearchPath | ( |  | ) |
| inlinevirtual

Query the search path.

Returns string set via setSearchPath.

Implements PtexCache.

Definition at line 252 of file PtexCache.h.

References _searchpath.

## ◆ getStats()

void PtexReaderCache::getStats | ( | Stats & | stats | ) |
| virtual

Get stats.

Implements PtexCache.

Definition at line 331 of file PtexCache.cpp.

References _blockReads, _fileOpens, _files, _filesOpen, _memUsed, _peakFilesOpen, _peakMemUsed, PtexCache::Stats::blockReads, PtexCache::Stats::fileReopens, PtexCache::Stats::filesAccessed, PtexCache::Stats::filesOpen, PtexCache::Stats::memUsed, PtexCache::Stats::peakFilesOpen, and PtexCache::Stats::peakMemUsed.

## ◆ logRecentlyUsed()

void PtexReaderCache::logRecentlyUsed | ( | PtexCachedReader * | reader | ) |

Definition at line 184 of file PtexCache.cpp.

References _mruList, AtomicIncrement(), PtexReaderCache::MruList::files, maxMruFiles, PtexReaderCache::MruList::next, and processMru().

## ◆ processMru()

void PtexReaderCache::processMru | ( |  | ) |
| private

Definition at line 200 of file PtexCache.cpp.

References _activeFiles, _blockReads, _fileOpens, _maxMem, _mruList, _mruLock, _openFiles, _prevMruList, adjustFilesOpen(), adjustMemUsed(), AtomicStore(), PtexReaderCache::MruList::files, PtexCachedReader::getBlockReadsChange(), PtexCachedReader::getMemUsedChange(), PtexCachedReader::getOpensChange(), maxMruFiles, PtexReaderCache::MruList::next, pruneDataIfNeeded(), and pruneFilesIfNeeded().

Referenced by logRecentlyUsed().

## ◆ pruneDataIfNeeded()

void PtexReaderCache::pruneDataIfNeeded | ( |  | ) |
| private

Definition at line 268 of file PtexCache.cpp.

References _activeFiles, _maxMem, _memUsed, _mruLock, adjustMemUsed(), and PtexCachedReader::tryPrune().

Referenced by processMru().

## ◆ pruneFilesIfNeeded()

void PtexReaderCache::pruneFilesIfNeeded | ( |  | ) |
| private

Definition at line 249 of file PtexCache.cpp.

References _filesOpen, _maxFiles, _mruLock, _openFiles, 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 301 of file PtexCache.cpp.

References _files, and purge().

## ◆ purge() [2/3]

void PtexReaderCache::purge | ( | PtexCachedReader * | reader | ) |

Definition at line 308 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 292 of file PtexCache.cpp.

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

Referenced by get(), purge(), 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 324 of file PtexCache.cpp.

References _files, adjustMemUsed(), 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 227 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

  path | colon-delimited search path.

Implements PtexCache.

Definition at line 229 of file PtexCache.h.

References _searchdirs, and _searchpath.

## Member Data Documentation

## ◆ _activeFiles

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

Definition at line 314 of file PtexCache.h.

Referenced by processMru(), and pruneDataIfNeeded().

## ◆ _blockReads

size_t PtexReaderCache::_blockReads
| private

Definition at line 319 of file PtexCache.h.

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

## ◆ _err

PtexErrorHandler* PtexReaderCache::_err
| private

Definition at line 294 of file PtexCache.h.

Referenced by get(), and PtexReaderCache().

## ◆ _fileOpens

size_t PtexReaderCache::_fileOpens
| private

Definition at line 318 of file PtexCache.h.

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

## ◆ _files

FileMap PtexReaderCache::_files
| private

Definition at line 298 of file PtexCache.h.

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

## ◆ _filesOpen

volatile size_t PtexReaderCache::_filesOpen
| private

Definition at line 301 of file PtexCache.h.

Referenced by adjustFilesOpen(), getStats(), pruneFilesIfNeeded(), and PtexReaderCache().

## ◆ _io

PtexInputHandler* PtexReaderCache::_io
| private

Definition at line 293 of file PtexCache.h.

Referenced by get(), and PtexReaderCache().

## ◆ _maxFiles

size_t PtexReaderCache::_maxFiles
| private

Definition at line 291 of file PtexCache.h.

Referenced by pruneFilesIfNeeded(), and PtexReaderCache().

## ◆ _maxMem

size_t PtexReaderCache::_maxMem
| private

Definition at line 292 of file PtexCache.h.

Referenced by processMru(), pruneDataIfNeeded(), and PtexReaderCache().

## ◆ _memUsed

volatile size_t PtexReaderCache::_memUsed
| private

Definition at line 300 of file PtexCache.h.

Referenced by adjustMemUsed(), getStats(), pruneDataIfNeeded(), and PtexReaderCache().

## ◆ _mruList

MruList* volatile PtexReaderCache::_mruList
| private

Definition at line 310 of file PtexCache.h.

Referenced by logRecentlyUsed(), processMru(), and PtexReaderCache().

## ◆ _mruLists

MruList PtexReaderCache::_mruLists[2]
| private

Definition at line 309 of file PtexCache.h.

Referenced by PtexReaderCache().

## ◆ _mruLock

Mutex PtexReaderCache::_mruLock
| private

Definition at line 302 of file PtexCache.h.

Referenced by processMru(), pruneDataIfNeeded(), pruneFilesIfNeeded(), and PtexReaderCache().

## ◆ _openFiles

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

Definition at line 313 of file PtexCache.h.

Referenced by processMru(), and pruneFilesIfNeeded().

## ◆ _peakFilesOpen

size_t PtexReaderCache::_peakFilesOpen
| private

Definition at line 317 of file PtexCache.h.

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

## ◆ _peakMemUsed

size_t PtexReaderCache::_peakMemUsed
| private

Definition at line 316 of file PtexCache.h.

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

## ◆ _premultiply

bool PtexReaderCache::_premultiply
| private

Definition at line 299 of file PtexCache.h.

Referenced by get(), and PtexReaderCache().

## ◆ _prevMruList

MruList* volatile PtexReaderCache::_prevMruList
| private

Definition at line 311 of file PtexCache.h.

Referenced by processMru(), and PtexReaderCache().

## ◆ _searchdirs

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

Definition at line 296 of file PtexCache.h.

Referenced by findFile(), and setSearchPath().

## ◆ _searchpath

std::string PtexReaderCache::_searchpath
| private

Definition at line 295 of file PtexCache.h.

Referenced by getSearchPath(), and setSearchPath().

## ◆ maxMruFiles

const int PtexReaderCache::maxMruFiles = 50
| staticprivate

Definition at line 304 of file PtexCache.h.

Referenced by logRecentlyUsed(), and processMru().

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

- PtexCache.h
- PtexCache.cpp