2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkDiscardableMemoryPool_DEFINED
9 #define SkDiscardableMemoryPool_DEFINED
11 #include "SkDiscardableMemory.h"
13 #ifndef SK_LAZY_CACHE_STATS
15 #define SK_LAZY_CACHE_STATS 1
17 #define SK_LAZY_CACHE_STATS 0
22 * An implementation of Discardable Memory that manages a fixed-size
23 * budget of memory. When the allocated memory exceeds this size,
24 * unlocked blocks of memory are purged. If all memory is locked, it
25 * can exceed the memory-use budget.
27 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
29 virtual ~SkDiscardableMemoryPool() { }
31 virtual size_t getRAMUsed() = 0;
32 virtual void setRAMBudget(size_t budget) = 0;
33 virtual size_t getRAMBudget() = 0;
35 /** purges all unlocked DMs */
36 virtual void dumpPool() = 0;
38 #if SK_LAZY_CACHE_STATS
40 * These two values are a count of the number of successful and
41 * failed calls to SkDiscardableMemory::lock() for all DMs managed
44 virtual int getCacheHits() = 0;
45 virtual int getCacheMisses() = 0;
46 virtual void resetCacheHitsAndMisses() = 0;
50 * This non-global pool can be used for unit tests to verify that
52 * Without mutex, will be not be thread safe.
54 static SkDiscardableMemoryPool* Create(
55 size_t size, SkBaseMutex* mutex = NULL);
59 * Returns (and creates if needed) a threadsafe global
60 * SkDiscardableMemoryPool.
62 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool();
64 #if !defined(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE)
65 #define SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (128 * 1024 * 1024)
68 #endif // SkDiscardableMemoryPool_DEFINED