michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkDiscardableMemoryPool_DEFINED michael@0: #define SkDiscardableMemoryPool_DEFINED michael@0: michael@0: #include "SkDiscardableMemory.h" michael@0: #include "SkTInternalLList.h" michael@0: #include "SkThread.h" michael@0: michael@0: class SkPoolDiscardableMemory; michael@0: michael@0: #ifdef SK_DEBUG michael@0: #define LAZY_CACHE_STATS 1 michael@0: #elif !defined(LAZY_CACHE_STATS) michael@0: #define LAZY_CACHE_STATS 0 michael@0: #endif michael@0: michael@0: /** michael@0: * This non-global pool can be used for unit tests to verify that the michael@0: * pool works. michael@0: */ michael@0: class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { michael@0: public: michael@0: /** michael@0: * Without mutex, will be not be thread safe. michael@0: */ michael@0: SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL); michael@0: virtual ~SkDiscardableMemoryPool(); michael@0: michael@0: virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; michael@0: michael@0: size_t getRAMUsed(); michael@0: void setRAMBudget(size_t budget); michael@0: michael@0: /** purges all unlocked DMs */ michael@0: void dumpPool(); michael@0: michael@0: #if LAZY_CACHE_STATS michael@0: int fCacheHits; michael@0: int fCacheMisses; michael@0: #endif // LAZY_CACHE_STATS michael@0: michael@0: private: michael@0: SkBaseMutex* fMutex; michael@0: size_t fBudget; michael@0: size_t fUsed; michael@0: SkTInternalLList fList; michael@0: michael@0: /** Function called to free memory if needed */ michael@0: void dumpDownTo(size_t budget); michael@0: /** called by SkDiscardableMemoryPool upon destruction */ michael@0: void free(SkPoolDiscardableMemory* dm); michael@0: /** called by SkDiscardableMemoryPool::lock() */ michael@0: bool lock(SkPoolDiscardableMemory* dm); michael@0: /** called by SkDiscardableMemoryPool::unlock() */ michael@0: void unlock(SkPoolDiscardableMemory* dm); michael@0: michael@0: friend class SkPoolDiscardableMemory; michael@0: michael@0: typedef SkDiscardableMemory::Factory INHERITED; michael@0: }; michael@0: michael@0: /** michael@0: * Returns (and creates if needed) a threadsafe global michael@0: * SkDiscardableMemoryPool. michael@0: */ michael@0: SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); michael@0: michael@0: #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) michael@0: #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) michael@0: #endif michael@0: michael@0: #endif // SkDiscardableMemoryPool_DEFINED