ManaPlus
Typedefs | Functions | Variables
ResourceManager Namespace Reference

Typedefs

typedef Resource *(* loader) (SDL_RWops *rw, const std::string &name)
 
typedef Resource *(&) generator(const void *const data)
 
typedef std::map< std::string, Resource * > Resources
 
typedef Resources::iterator ResourceIterator
 
typedef Resources::const_iterator ResourceCIterator
 

Functions

void deleteResourceManager ()
 
void cleanUp (Resource *const res)
 
void cleanProtected ()
 
bool cleanOrphans (const bool always)
 
void logResource (const Resource *const res)
 
void logResources (const std::string &msg)
 
void clearDeleted (const bool full)
 
bool addResource (const std::string &idPath, Resource *const resource)
 
ResourcegetFromCache (const std::string &filename, const int variant)
 
bool isInCache (const std::string &idPath)
 
ResourcegetTempResource (const std::string &idPath)
 
ResourcegetFromCache (const std::string &idPath)
 
Resourceget (const std::string &idPath, generator fun, const void *const data)
 
void release (Resource *const res)
 
void moveToDeleted (Resource *const res)
 
void decRefDelete (Resource *const res)
 
void deleteInstance ()
 
void scheduleDelete (SDL_Surface *const surface)
 
void clearScheduled ()
 
void clearCache ()
 
int calcMemoryLocal ()
 
int calcMemoryChilds (const int level)
 
int calcMemory (const int level)
 
int size ()
 

Variables

std::set< SDL_Surface * > deletedSurfaces
 
Resources mResources
 
Resources mOrphanedResources
 
std::set< Resource * > mDeletedResources
 
time_t mOldestOrphan = 0
 
bool mDestruction = false
 

Typedef Documentation

◆ generator

typedef Resource*(&) ResourceManager::generator(const void *const data)

Definition at line 38 of file resourcefunctiontypes.h.

◆ loader

typedef Resource*(* ResourceManager::loader) (SDL_RWops *rw, const std::string &name)

Definition at line 36 of file resourcefunctiontypes.h.

◆ ResourceCIterator

typedef Resources::const_iterator ResourceManager::ResourceCIterator

Definition at line 37 of file resourcetypes.h.

◆ ResourceIterator

typedef Resources::iterator ResourceManager::ResourceIterator

Definition at line 36 of file resourcetypes.h.

◆ Resources

typedef std::map<std::string, Resource*> ResourceManager::Resources

Definition at line 35 of file resourcetypes.h.

Function Documentation

◆ addResource()

bool ResourceManager::addResource ( const std::string &  idPath,
Resource *const  resource 
)

Definition at line 359 of file resourcemanager.cpp.

References Resource::incRef(), Logger::log(), logger, and Resource::mIdPath.

Referenced by AtlasManager::injectToResources().

361 {
362  if (resource != nullptr)
363  {
364  resource->incRef();
365  resource->mIdPath = idPath;
366 #ifdef DEBUG_IMAGES
367  logger->log("set name %p, %s", static_cast<void*>(resource),
368  resource->mIdPath.c_str());
369 #endif // DEBUG_IMAGES
370 
371  mResources[idPath] = resource;
372  return true;
373  }
374  return false;
375 }
Logger * logger
Definition: logger.cpp:95
virtual void incRef()
Definition: resource.cpp:37
void log(const char *const log_text,...)
Definition: logger.cpp:243
std::string mIdPath
Definition: resource.h:83

◆ calcMemory()

int ResourceManager::calcMemory ( const int  level)

Definition at line 678 of file resourcemanager.cpp.

References calcMemoryChilds(), calcMemoryLocal(), memoryManager, and MemoryManager::printMemory().

Referenced by MemoryManager::printAllMemory().

679 {
680  const int sumLocal = calcMemoryLocal();
681  const int sumChilds = calcMemoryChilds(0);
682  memoryManager.printMemory("resource manager",
683  level,
684  sumLocal,
685  sumChilds);
686  return sumLocal + sumChilds;
687 }
void printMemory(const std::string &name, const int level, const int localSum, const int childsSum)
MemoryManager memoryManager
int calcMemoryChilds(const int level)

◆ calcMemoryChilds()

int ResourceManager::calcMemoryChilds ( const int  level)

Definition at line 658 of file resourcemanager.cpp.

References FOR_EACH.

Referenced by calcMemory().

659 {
660  int sz = 0;
662  {
663  sz += static_cast<int>((*it).first.capacity());
664  sz += (*it).second->calcMemory(level + 1);
665  }
667  {
668  sz += static_cast<int>((*it).first.capacity());
669  sz += (*it).second->calcMemory(level + 1);
670  }
671  FOR_EACH (std::set<Resource*>::const_iterator, it, mDeletedResources)
672  {
673  sz += (*it)->calcMemory(level + 1);
674  }
675  return sz;
676 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::set< Resource * > mDeletedResources
Resources::const_iterator ResourceCIterator
Definition: resourcetypes.h:37
Resources mOrphanedResources

◆ calcMemoryLocal()

int ResourceManager::calcMemoryLocal ( )

Definition at line 648 of file resourcemanager.cpp.

References FOR_EACH, MemoryManager::getSurfaceSize(), and memoryManager.

Referenced by calcMemory().

649 {
650  int sz = 24;
651  FOR_EACH (std::set<SDL_Surface*>::iterator, it, deletedSurfaces)
652  {
653  sz += memoryManager.getSurfaceSize(*it);
654  }
655  return sz;
656 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::set< SDL_Surface * > deletedSurfaces
MemoryManager memoryManager
int getSurfaceSize(const SDL_Surface *const surface)

◆ cleanOrphans()

bool ResourceManager::cleanOrphans ( const bool  always)

Definition at line 226 of file resourcemanager.cpp.

References logResource(), and Resource::mTimeStamp.

Referenced by cleanUp(), clearCache(), deleteInstance(), Gui::slowLogic(), and Client::stateSwitchLogin1().

227 {
228  timeval tv;
229  gettimeofday(&tv, nullptr);
230  // Delete orphaned resources after 30 seconds.
231  time_t oldest = static_cast<time_t>(tv.tv_sec);
232  const time_t threshold = oldest - 30;
233 
234  if (mOrphanedResources.empty() || (!always && mOldestOrphan >= threshold))
235  return false;
236 
237  bool status(false);
238  ResourceIterator iter = mOrphanedResources.begin();
239  while (iter != mOrphanedResources.end())
240  {
241  Resource *const res = iter->second;
242  if (res == nullptr)
243  {
244  ++iter;
245  continue;
246  }
247  const time_t t = res->mTimeStamp;
248  if (!always && t >= threshold)
249  {
250  if (t < oldest)
251  oldest = t;
252  ++ iter;
253  }
254  else
255  {
256  logResource(res);
257  const ResourceIterator toErase = iter;
258  ++iter;
259  mOrphanedResources.erase(toErase);
260  delete res; // delete only after removal from list,
261  // to avoid issues in recursion
262  status = true;
263  }
264  }
265 
266  mOldestOrphan = oldest;
267  return status;
268 }
time_t mTimeStamp
Definition: resource.h:81
void logResource(const Resource *const res)
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
Resources mOrphanedResources

◆ cleanProtected()

void ResourceManager::cleanProtected ( )

Definition at line 203 of file resourcemanager.cpp.

References Resource::decRef(), and Resource::mProtected.

Referenced by Game::changeMap(), clearCache(), and deleteInstance().

204 {
205  ResourceIterator iter = mResources.begin();
206  while (iter != mResources.end())
207  {
208  Resource *const res = iter->second;
209  if (res == nullptr)
210  {
211  ++ iter;
212  continue;
213  }
214  if (res->mProtected)
215  {
216  res->mProtected = false;
217  res->decRef();
218  iter = mResources.begin();
219  continue;
220  }
221 
222  ++ iter;
223  }
224 }
virtual void decRef()
Definition: resource.cpp:49
bool mProtected
Definition: resource.h:87
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36

◆ cleanUp()

void ResourceManager::cleanUp ( Resource *const  res)

Definition at line 182 of file resourcemanager.cpp.

References cleanOrphans(), Logger::log(), logger, Resource::mIdPath, and Resource::mRefCount.

Referenced by deleteResourceManager().

183 {
184  if (res == nullptr)
185  return;
186 
187  const unsigned refCount = res->mRefCount;
188  if (refCount > 0)
189  {
190  logger->log("ResourceManager::~ResourceManager() cleaning up %u "
191  "reference%s to %s",
192  refCount,
193  (refCount == 1) ? "" : "s",
194  res->mIdPath.c_str());
195  }
196 
197  delete res;
198 #ifdef DEBUG_LEAKS
199  cleanOrphans(true);
200 #endif // DEBUG_LEAKS
201 }
unsigned int mRefCount
Definition: resource.h:86
Logger * logger
Definition: logger.cpp:95
bool cleanOrphans(const bool always)
void log(const char *const log_text,...)
Definition: logger.cpp:243
std::string mIdPath
Definition: resource.h:83

◆ clearCache()

void ResourceManager::clearCache ( )

Definition at line 641 of file resourcemanager.cpp.

References cleanOrphans(), and cleanProtected().

Referenced by Actions::doSit(), Being::setComment(), and Client::unloadData().

642 {
643  cleanProtected();
644  while (cleanOrphans(true))
645  continue;
646 }
bool cleanOrphans(const bool always)

◆ clearDeleted()

void ResourceManager::clearDeleted ( const bool  full)

Definition at line 320 of file resourcemanager.cpp.

References Logger::log1(), logger, and logResource().

Referenced by deleteResourceManager(), and AtlasResource::~AtlasResource().

321 {
322  bool status(true);
323  logger->log1("clear deleted");
324  while (status)
325  {
326  status = false;
327  std::set<Resource*>::iterator resDelIter = mDeletedResources.begin();
328  while (resDelIter != mDeletedResources.end())
329  {
330  if ((*resDelIter)->mRefCount == 0u)
331  {
332  status = true;
333  Resource *res = *resDelIter;
334  logResource(res);
335  mDeletedResources.erase(resDelIter);
336  delete res;
337  break;
338  }
339  ++ resDelIter;
340  }
341  }
342  if (full && !mDeletedResources.empty())
343  {
344  logger->log1("leaks in deleted");
345  std::set<Resource*>::iterator resDelIter = mDeletedResources.begin();
346  while (resDelIter != mDeletedResources.end())
347  {
348  logResource(*resDelIter);
349 
350  // for debug only
351 // delete *resDelIter;
352  // for debug only
353 
354  ++ resDelIter;
355  }
356  }
357 }
std::set< Resource * > mDeletedResources
void log1(const char *const log_text)
Definition: logger.cpp:222
Logger * logger
Definition: logger.cpp:95
void logResource(const Resource *const res)

◆ clearScheduled()

void ResourceManager::clearScheduled ( )

Definition at line 632 of file resourcemanager.cpp.

References BLOCK_END, BLOCK_START, FOR_EACH, and MSDL_FreeSurface.

Referenced by deleteResourceManager(), and Gui::logic().

633 {
634  BLOCK_START("ResourceManager::clearScheduled")
635  FOR_EACH (std::set<SDL_Surface*>::iterator, i, deletedSurfaces)
636  MSDL_FreeSurface(*i);
639 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::set< SDL_Surface * > deletedSurfaces
#define MSDL_FreeSurface(surface)
Definition: debug.h:53
#define BLOCK_START(name)
Definition: perfomance.h:78
void clear()
Definition: playerinfo.cpp:464
#define BLOCK_END(name)
Definition: perfomance.h:79

◆ decRefDelete()

void ResourceManager::decRefDelete ( Resource *const  res)

Definition at line 557 of file resourcemanager.cpp.

References Resource::decRef(), logResource(), Resource::mIdPath, and Resource::mRefCount.

Referenced by SoundManager::close(), and Desktop::setBestFittingWallpaper().

558 {
559  if (res == nullptr)
560  return;
561 
562  const int count = res->mRefCount;
563  if (count == 1)
564  {
565  logResource(res);
566 
567  ResourceIterator resIter = mResources.find(res->mIdPath);
568  if (resIter != mResources.end() && resIter->second == res)
569  {
570  mResources.erase(resIter);
571  }
572  else
573  {
574  resIter = mOrphanedResources.find(res->mIdPath);
575  if (resIter != mOrphanedResources.end() && resIter->second == res)
576  mOrphanedResources.erase(resIter);
577  }
578 
579  delete res;
580  }
581  else
582  {
583  res->decRef();
584  }
585 }
virtual void decRef()
Definition: resource.cpp:49
unsigned int mRefCount
Definition: resource.h:86
void logResource(const Resource *const res)
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
std::string mIdPath
Definition: resource.h:83
Resources mOrphanedResources

◆ deleteInstance()

void ResourceManager::deleteInstance ( )

Deletes the class instance if it exists.

Definition at line 587 of file resourcemanager.cpp.

References cleanOrphans(), cleanProtected(), deleteResourceManager(), Logger::log(), Logger::log1(), logger, Resource::mIdPath, Resource::mRefCount, mResources, reportAlways, and Catch::toString().

Referenced by Client::gameClear(), and Client::initConfigListeners().

588 {
589 #ifdef DUMP_LEAKED_RESOURCES
590  logger->log1("clean orphans start");
592  while (ResourceManager::cleanOrphans(true))
593  continue;
594  logger->log1("clean orphans end");
596 
597 #ifdef UNITTESTS
598  bool status(false);
599 #endif // UNITTESTS
600 
601  while (iter != ResourceManager::mResources.end())
602  {
603  const Resource *const res = iter->second;
604  if (res != nullptr)
605  {
606  if (res->mRefCount != 0u)
607  {
608  logger->log(std::string("ResourceLeak: ").append(
609  res->mIdPath).append(" (").append(toString(
610  res->mRefCount)).append(")"));
611 #ifdef UNITTESTS
612  status = true;
613 #endif // UNITTESTS
614  }
615  }
616  ++iter;
617  }
618 #ifdef UNITTESTS
619  if (status)
620  reportAlways("Found leaked resources.");
621 #endif // UNITTESTS
622 #endif // DUMP_LEAKED_RESOURCES
623 
625 }
void log1(const char *const log_text)
Definition: logger.cpp:222
unsigned int mRefCount
Definition: resource.h:86
void deleteResourceManager()
Logger * logger
Definition: logger.cpp:95
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
bool cleanOrphans(const bool always)
void log(const char *const log_text,...)
Definition: logger.cpp:243
std::string mIdPath
Definition: resource.h:83
#define reportAlways(...)
Definition: checkutils.h:252

◆ deleteResourceManager()

void ResourceManager::deleteResourceManager ( )

Cleans up remaining resources, warning about resources that were still referenced.

Definition at line 67 of file resourcemanager.cpp.

References cleanUp(), clearDeleted(), clearScheduled(), Logger::log(), logger, reportAlways, and Catch::toString().

Referenced by deleteInstance().

68 {
69  mDestruction = true;
70  mResources.insert(mOrphanedResources.begin(), mOrphanedResources.end());
71 
72  // Release any remaining spritedefs first because they depend on image sets
73  ResourceIterator iter = mResources.begin();
74 
75 #ifdef DEBUG_LEAKS
76 #ifdef UNITTESTS
77  bool status(false);
78 #endif // UNITTESTS
79 
80  while (iter != mResources.end())
81  {
82  if (iter->second)
83  {
84  if (iter->second->mRefCount)
85  {
86  logger->log(std::string("ResourceLeak: ").append(
87  iter->second->mIdPath).append(" (").append(
88  toString(iter->second->mRefCount)).append(")"));
89 #ifdef UNITTESTS
90  status = true;
91 #endif // UNITTESTS
92  }
93  }
94  ++iter;
95  }
96 
97 #ifdef UNITTESTS
98  if (status)
99  reportAlways("Found leaked resources.");
100 #endif // UNITTESTS
101 
102  iter = mResources.begin();
103 #endif // DEBUG_LEAKS
104 
105  while (iter != mResources.end())
106  {
107 #ifdef DEBUG_LEAKS
108  if (iter->second && iter->second->mRefCount)
109  {
110  ++iter;
111  continue;
112  }
113 #endif // DEBUG_LEAKS
114 
115  if (dynamic_cast<SpriteDef*>(iter->second) != nullptr)
116  {
117  cleanUp(iter->second);
118  const ResourceIterator toErase = iter;
119  ++iter;
120  mResources.erase(toErase);
121  }
122  else
123  {
124  ++iter;
125  }
126  }
127 
128  // Release any remaining image sets first because they depend on images
129  iter = mResources.begin();
130  while (iter != mResources.end())
131  {
132 #ifdef DEBUG_LEAKS
133  if (iter->second && iter->second->mRefCount)
134  {
135  ++iter;
136  continue;
137  }
138 #endif // DEBUG_LEAKS
139 
140  if (dynamic_cast<ImageSet*>(iter->second) != nullptr)
141  {
142  cleanUp(iter->second);
143  const ResourceIterator toErase = iter;
144  ++iter;
145  mResources.erase(toErase);
146  }
147  else
148  {
149  ++iter;
150  }
151  }
152 
153  // Release remaining resources, logging the number of dangling references.
154  iter = mResources.begin();
155  while (iter != mResources.end())
156  {
157 #ifdef DEBUG_LEAKS
158  if (iter->second && iter->second->mRefCount)
159  {
160  ++iter;
161  continue;
162  }
163 #endif // DEBUG_LEAKS
164 
165  if (iter->second != nullptr)
166  {
167  cleanUp(iter->second);
168  const ResourceIterator toErase = iter;
169  ++iter;
170  mResources.erase(toErase);
171  }
172  else
173  {
174  ++iter;
175  }
176  }
177  clearDeleted();
178  clearScheduled();
179  mDestruction = false;
180 }
void clearDeleted(const bool full)
Logger * logger
Definition: logger.cpp:95
void cleanUp(Resource *const res)
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
void log(const char *const log_text,...)
Definition: logger.cpp:243
#define reportAlways(...)
Definition: checkutils.h:252
Resources mOrphanedResources

◆ get()

Resource * ResourceManager::get ( const std::string &  idPath,
generator  fun,
const void *const  data 
)

Creates a resource and adds it to the resource map.

Parameters
idPathThe resource identifier path.
funA function for generating the resource.
dataExtra parameters for the generator.
Returns
A valid resource or NULL if the resource could not be generated.

Definition at line 427 of file resourcemanager.cpp.

References data, getFromCache(), Resource::incRef(), Logger::log(), logger, Resource::mIdPath, and reportAlways.

Referenced by Loader::getAtlas(), Loader::getEmptyAtlas(), Loader::getImage(), Loader::getImageSet(), Loader::getMusic(), Loader::getRescaled(), Loader::getShader(), Loader::getShaderProgram(), Loader::getSoundEffect(), Loader::getSprite(), Loader::getSubImage(), Loader::getSubImageSet(), Loader::getWalkLayer(), and Loader::getXml().

430 {
431 #ifndef DISABLE_RESOURCE_CACHING
432  Resource *resource = getFromCache(idPath);
433  if (resource != nullptr)
434  return resource;
435  resource = fun(data);
436 
437  if (resource != nullptr)
438  {
439  resource->incRef();
440  resource->mIdPath = idPath;
441 #ifdef DEBUG_IMAGES
442  logger->log("set name %p, %s", static_cast<void*>(resource),
443  resource->mIdPath.c_str());
444 #endif // DEBUG_IMAGES
445 
446  mResources[idPath] = resource;
447  }
448  else
449  {
450  reportAlways("Error loading image: %s", idPath.c_str());
451  }
452 #else // DISABLE_RESOURCE_CACHING
453 
454  Resource *resource = fun(data, idPath);
455 
456  if (resource)
457  {
458  resource->incRef();
459  resource->mIdPath = idPath;
460 #ifdef DEBUG_IMAGES
461  logger->log("set name %p, %s", static_cast<void*>(resource),
462  resource->mIdPath.c_str());
463 #endif // DEBUG_IMAGES
464  }
465  else
466  {
467  reportAlways("Error loading image: " + idPath);
468  }
469 #endif // DISABLE_RESOURCE_CACHING
470 
471  // Returns nullptr if the object could not be created.
472  return resource;
473 }
Logger * logger
Definition: logger.cpp:95
uint32_t data
Resource * getFromCache(const std::string &idPath)
virtual void incRef()
Definition: resource.cpp:37
void log(const char *const log_text,...)
Definition: logger.cpp:243
std::string mIdPath
Definition: resource.h:83
#define reportAlways(...)
Definition: checkutils.h:252

◆ getFromCache() [1/2]

Resource * ResourceManager::getFromCache ( const std::string &  filename,
const int  variant 
)

Definition at line 377 of file resourcemanager.cpp.

Referenced by AnimatedSprite::delayedLoad(), and get().

379 {
380  std::stringstream ss;
381  ss << filename << "[" << variant << "]";
382  return getFromCache(ss.str());
383 }
Resource * getFromCache(const std::string &idPath)

◆ getFromCache() [2/2]

Resource * ResourceManager::getFromCache ( const std::string &  idPath)

Definition at line 403 of file resourcemanager.cpp.

References Resource::incRef().

404 {
405  // Check if the id exists, and return the value if it does.
406  ResourceIterator resIter = mResources.find(idPath);
407  if (resIter != mResources.end())
408  {
409  if (resIter->second != nullptr)
410  resIter->second->incRef();
411  return resIter->second;
412  }
413 
414  resIter = mOrphanedResources.find(idPath);
415  if (resIter != mOrphanedResources.end())
416  {
417  Resource *const res = resIter->second;
418  mResources.insert(*resIter);
419  mOrphanedResources.erase(resIter);
420  if (res != nullptr)
421  res->incRef();
422  return res;
423  }
424  return nullptr;
425 }
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
virtual void incRef()
Definition: resource.cpp:37
Resources mOrphanedResources

◆ getTempResource()

Resource * ResourceManager::getTempResource ( const std::string &  idPath)

Definition at line 391 of file resourcemanager.cpp.

Referenced by AtlasManager::loadEmptyImages(), and AtlasManager::loadImages().

392 {
393  const ResourceCIterator &resIter = mResources.find(idPath);
394  if (resIter != mResources.end())
395  {
396  Resource *const res = resIter->second;
397  if (resIter->second != nullptr)
398  return res;
399  }
400  return nullptr;
401 }
Resources::const_iterator ResourceCIterator
Definition: resourcetypes.h:37

◆ isInCache()

bool ResourceManager::isInCache ( const std::string &  idPath)

Definition at line 385 of file resourcemanager.cpp.

386 {
387  const ResourceCIterator &resIter = mResources.find(idPath);
388  return (resIter != mResources.end() && (resIter->second != nullptr));
389 }
Resources::const_iterator ResourceCIterator
Definition: resourcetypes.h:37

◆ logResource()

void ResourceManager::logResource ( const Resource *const  res)

Definition at line 270 of file resourcemanager.cpp.

References Image::getGLImage(), Logger::log(), logger, Resource::mIdPath, Resource::mRefCount, Resource::mSource, and Catch::toString().

Referenced by cleanOrphans(), clearDeleted(), decRefDelete(), logResources(), and moveToDeleted().

271 {
272  if (res == nullptr)
273  return;
274 #ifdef USE_OPENGL
275  const Image *const image = dynamic_cast<const Image *>(res);
276  if (image != nullptr)
277  {
278  std::string src = image->mSource;
279  const int count = image->mRefCount;
280  if (count != 0)
281  src.append(" ").append(toString(count));
282  logger->log("resource(%s, %u) %s", res->mIdPath.c_str(),
283  image->getGLImage(), src.c_str());
284  }
285  else
286  {
287  std::string src = res->mSource;
288  const int count = res->mRefCount;
289  if (count > 0)
290  src.append(" ").append(toString(count));
291  logger->log("resource(%s) %s", res->mIdPath.c_str(), src.c_str());
292  }
293 #else // USE_OPENGL
294 
295  logger->log("resource(%s)", res->mIdPath.c_str());
296 #endif // USE_OPENGL
297 }
std::string mSource
Definition: resource.h:84
unsigned int mRefCount
Definition: resource.h:86
Logger * logger
Definition: logger.cpp:95
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
GLuint getGLImage() const
Definition: image.h:177
Definition: image.h:61
void log(const char *const log_text,...)
Definition: logger.cpp:243
std::string mIdPath
Definition: resource.h:83

◆ logResources()

void ResourceManager::logResources ( const std::string &  msg)

Definition at line 299 of file resourcemanager.cpp.

References FOR_EACH, Logger::log(), logger, and logResource().

300 {
301  logger->log("start of resources: " + msg);
302  logger->log("resources");
304  {
305  logResource((*it).second);
306  }
307  logger->log("orphaned resources");
309  {
310  logResource((*it).second);
311  }
312  logger->log("deleted resources");
313  FOR_EACH(std::set<Resource*>::iterator, it, mDeletedResources)
314  {
315  logResource(*it);
316  }
317  logger->log("end of resources");
318 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
std::set< Resource * > mDeletedResources
bool msg(InputEvent &event)
Definition: chat.cpp:38
Logger * logger
Definition: logger.cpp:95
void logResource(const Resource *const res)
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
void log(const char *const log_text,...)
Definition: logger.cpp:243
Resources mOrphanedResources

◆ moveToDeleted()

void ResourceManager::moveToDeleted ( Resource *const  res)

Move resource to deleted resources list.

Definition at line 523 of file resourcemanager.cpp.

References Resource::decRef(), logResource(), Resource::mIdPath, and Resource::mRefCount.

Referenced by AtlasManager::loadEmptyImages(), AtlasManager::loadImages(), and AtlasManager::moveToDeleted().

524 {
525  if (res == nullptr)
526  return;
527 
528  bool found(false);
529  const int count = res->mRefCount;
530  if (count == 1)
531  logResource(res);
532  res->decRef();
533  ResourceIterator resIter = mResources.find(res->mIdPath);
534  if (resIter != mResources.end() && resIter->second == res)
535  {
536  mResources.erase(resIter);
537  found = true;
538  }
539  else
540  {
541  resIter = mOrphanedResources.find(res->mIdPath);
542  if (resIter != mOrphanedResources.end() && resIter->second == res)
543  {
544  mOrphanedResources.erase(resIter);
545  found = true;
546  }
547  }
548  if (found)
549  {
550  if (count > 1)
551  mDeletedResources.insert(res);
552  else
553  delete res;
554  }
555 }
std::set< Resource * > mDeletedResources
virtual void decRef()
Definition: resource.cpp:49
unsigned int mRefCount
Definition: resource.h:86
void logResource(const Resource *const res)
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
std::string mIdPath
Definition: resource.h:83
Resources mOrphanedResources

◆ release()

void ResourceManager::release ( Resource *const  res)

Releases a resource, placing it in the set of orphaned resources.

Definition at line 475 of file resourcemanager.cpp.

References Resource::mIdPath, Resource::mTimeStamp, and reportAlways.

Referenced by Resource::decRef().

476 {
477  if ((res == nullptr) || mDestruction)
478  return;
479 
480 #ifndef DISABLE_RESOURCE_CACHING
481  std::set<Resource*>::iterator resDelIter = mDeletedResources.find(res);
482  if (resDelIter != mDeletedResources.end())
483  {
484  // we found zero counted image in deleted list. deleting it and exit.
485  mDeletedResources.erase(resDelIter);
486  delete res;
487  return;
488  }
489 
490  ResourceIterator resIter = mResources.find(res->mIdPath);
491 
492  if (resIter == mResources.end())
493  {
494  reportAlways("no resource in cache: %s",
495  res->mIdPath.c_str());
496  delete res;
497  return;
498  }
499  if (resIter->second != res)
500  {
501  reportAlways("in cache other image: %s",
502  res->mIdPath.c_str());
503  delete res;
504  return;
505  }
506 
507  timeval tv;
508  gettimeofday(&tv, nullptr);
509  const time_t timestamp = static_cast<time_t>(tv.tv_sec);
510 
511  res->mTimeStamp = timestamp;
512  if (mOrphanedResources.empty())
513  mOldestOrphan = timestamp;
514 
515  mOrphanedResources.insert(*resIter);
516  mResources.erase(resIter);
517 #else // DISABLE_RESOURCE_CACHING
518 
519  delete res;
520 #endif // DISABLE_RESOURCE_CACHING
521 }
std::set< Resource * > mDeletedResources
time_t mTimeStamp
Definition: resource.h:81
Resources::iterator ResourceIterator
Definition: resourcetypes.h:36
std::string mIdPath
Definition: resource.h:83
#define reportAlways(...)
Definition: checkutils.h:252
Resources mOrphanedResources

◆ scheduleDelete()

void ResourceManager::scheduleDelete ( SDL_Surface *const  surface)

Definition at line 627 of file resourcemanager.cpp.

Referenced by Image::SDLCleanCache().

628 {
629  deletedSurfaces.insert(surface);
630 }
std::set< SDL_Surface * > deletedSurfaces

◆ size()

int ResourceManager::size ( )

Variable Documentation

◆ deletedSurfaces

std::set<SDL_Surface*> ResourceManager::deletedSurfaces

Definition at line 60 of file resourcemanager.cpp.

◆ mDeletedResources

std::set<Resource*> ResourceManager::mDeletedResources

Definition at line 63 of file resourcemanager.cpp.

Referenced by size().

◆ mDestruction

bool ResourceManager::mDestruction = false

Definition at line 65 of file resourcemanager.cpp.

◆ mOldestOrphan

time_t ResourceManager::mOldestOrphan = 0

Definition at line 64 of file resourcemanager.cpp.

◆ mOrphanedResources

Resources ResourceManager::mOrphanedResources

Definition at line 62 of file resourcemanager.cpp.

Referenced by size().

◆ mResources

Resources ResourceManager::mResources

Definition at line 61 of file resourcemanager.cpp.

Referenced by deleteInstance(), and size().