1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkDiscardableMemoryPool_DEFINED 9 #define SkDiscardableMemoryPool_DEFINED 10 11 #include "SkDiscardableMemory.h" 12 #include "SkTInternalLList.h" 13 #include "SkThread.h" 14 15 class SkPoolDiscardableMemory; 16 17 #ifdef SK_DEBUG 18 #define LAZY_CACHE_STATS 1 19 #elif !defined(LAZY_CACHE_STATS) 20 #define LAZY_CACHE_STATS 0 21 #endif 22 23 /** 24 * This non-global pool can be used for unit tests to verify that the 25 * pool works. 26 */ 27 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory { 28 public: 29 /** 30 * Without mutex, will be not be thread safe. 31 */ 32 SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL); 33 virtual ~SkDiscardableMemoryPool(); 34 35 virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; 36 37 size_t getRAMUsed(); 38 void setRAMBudget(size_t budget); 39 40 /** purges all unlocked DMs */ 41 void dumpPool(); 42 43 #if LAZY_CACHE_STATS 44 int fCacheHits; 45 int fCacheMisses; 46 #endif // LAZY_CACHE_STATS 47 48 private: 49 SkBaseMutex* fMutex; 50 size_t fBudget; 51 size_t fUsed; 52 SkTInternalLList<SkPoolDiscardableMemory> fList; 53 54 /** Function called to free memory if needed */ 55 void dumpDownTo(size_t budget); 56 /** called by SkDiscardableMemoryPool upon destruction */ 57 void free(SkPoolDiscardableMemory* dm); 58 /** called by SkDiscardableMemoryPool::lock() */ 59 bool lock(SkPoolDiscardableMemory* dm); 60 /** called by SkDiscardableMemoryPool::unlock() */ 61 void unlock(SkPoolDiscardableMemory* dm); 62 63 friend class SkPoolDiscardableMemory; 64 65 typedef SkDiscardableMemory::Factory INHERITED; 66 }; 67 68 /** 69 * Returns (and creates if needed) a threadsafe global 70 * SkDiscardableMemoryPool. 71 */ 72 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool(); 73 74 #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE) 75 #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024) 76 #endif 77 78 #endif // SkDiscardableMemoryPool_DEFINED 79