Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkDiscardableMemoryPool.h" |
michael@0 | 9 | #include "SkOnce.h" |
michael@0 | 10 | |
michael@0 | 11 | // Note: |
michael@0 | 12 | // A PoolDiscardableMemory is memory that is counted in a pool. |
michael@0 | 13 | // A DiscardableMemoryPool is a pool of PoolDiscardableMemorys. |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * A SkPoolDiscardableMemory is a SkDiscardableMemory that relies on |
michael@0 | 17 | * a SkDiscardableMemoryPool object to manage the memory. |
michael@0 | 18 | */ |
michael@0 | 19 | class SkPoolDiscardableMemory : public SkDiscardableMemory { |
michael@0 | 20 | public: |
michael@0 | 21 | SkPoolDiscardableMemory(SkDiscardableMemoryPool* pool, |
michael@0 | 22 | void* pointer, size_t bytes); |
michael@0 | 23 | virtual ~SkPoolDiscardableMemory(); |
michael@0 | 24 | virtual bool lock() SK_OVERRIDE; |
michael@0 | 25 | virtual void* data() SK_OVERRIDE; |
michael@0 | 26 | virtual void unlock() SK_OVERRIDE; |
michael@0 | 27 | friend class SkDiscardableMemoryPool; |
michael@0 | 28 | private: |
michael@0 | 29 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(SkPoolDiscardableMemory); |
michael@0 | 30 | SkDiscardableMemoryPool* const fPool; |
michael@0 | 31 | bool fLocked; |
michael@0 | 32 | void* fPointer; |
michael@0 | 33 | const size_t fBytes; |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | SkPoolDiscardableMemory::SkPoolDiscardableMemory(SkDiscardableMemoryPool* pool, |
michael@0 | 37 | void* pointer, |
michael@0 | 38 | size_t bytes) |
michael@0 | 39 | : fPool(pool) |
michael@0 | 40 | , fLocked(true) |
michael@0 | 41 | , fPointer(pointer) |
michael@0 | 42 | , fBytes(bytes) { |
michael@0 | 43 | SkASSERT(fPool != NULL); |
michael@0 | 44 | SkASSERT(fPointer != NULL); |
michael@0 | 45 | SkASSERT(fBytes > 0); |
michael@0 | 46 | fPool->ref(); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | SkPoolDiscardableMemory::~SkPoolDiscardableMemory() { |
michael@0 | 50 | SkASSERT(!fLocked); // contract for SkDiscardableMemory |
michael@0 | 51 | fPool->free(this); |
michael@0 | 52 | fPool->unref(); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | bool SkPoolDiscardableMemory::lock() { |
michael@0 | 56 | SkASSERT(!fLocked); // contract for SkDiscardableMemory |
michael@0 | 57 | return fPool->lock(this); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | void* SkPoolDiscardableMemory::data() { |
michael@0 | 61 | SkASSERT(fLocked); // contract for SkDiscardableMemory |
michael@0 | 62 | return fPointer; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | void SkPoolDiscardableMemory::unlock() { |
michael@0 | 66 | SkASSERT(fLocked); // contract for SkDiscardableMemory |
michael@0 | 67 | fPool->unlock(this); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 71 | |
michael@0 | 72 | SkDiscardableMemoryPool::SkDiscardableMemoryPool(size_t budget, |
michael@0 | 73 | SkBaseMutex* mutex) |
michael@0 | 74 | : fMutex(mutex) |
michael@0 | 75 | , fBudget(budget) |
michael@0 | 76 | , fUsed(0) { |
michael@0 | 77 | #if LAZY_CACHE_STATS |
michael@0 | 78 | fCacheHits = 0; |
michael@0 | 79 | fCacheMisses = 0; |
michael@0 | 80 | #endif // LAZY_CACHE_STATS |
michael@0 | 81 | } |
michael@0 | 82 | SkDiscardableMemoryPool::~SkDiscardableMemoryPool() { |
michael@0 | 83 | // SkPoolDiscardableMemory objects that belong to this pool are |
michael@0 | 84 | // always deleted before deleting this pool since each one has a |
michael@0 | 85 | // ref to the pool. |
michael@0 | 86 | SkASSERT(fList.isEmpty()); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | void SkDiscardableMemoryPool::dumpDownTo(size_t budget) { |
michael@0 | 90 | // assert((NULL = fMutex) || fMutex->isLocked()); |
michael@0 | 91 | // TODO(halcanary) implement bool fMutex::isLocked(). |
michael@0 | 92 | // WARNING: only call this function after aquiring lock. |
michael@0 | 93 | if (fUsed <= budget) { |
michael@0 | 94 | return; |
michael@0 | 95 | } |
michael@0 | 96 | typedef SkTInternalLList<SkPoolDiscardableMemory>::Iter Iter; |
michael@0 | 97 | Iter iter; |
michael@0 | 98 | SkPoolDiscardableMemory* cur = iter.init(fList, Iter::kTail_IterStart); |
michael@0 | 99 | while ((fUsed > budget) && (NULL != cur)) { |
michael@0 | 100 | if (!cur->fLocked) { |
michael@0 | 101 | SkPoolDiscardableMemory* dm = cur; |
michael@0 | 102 | SkASSERT(dm->fPointer != NULL); |
michael@0 | 103 | sk_free(dm->fPointer); |
michael@0 | 104 | dm->fPointer = NULL; |
michael@0 | 105 | SkASSERT(fUsed >= dm->fBytes); |
michael@0 | 106 | fUsed -= dm->fBytes; |
michael@0 | 107 | cur = iter.prev(); |
michael@0 | 108 | // Purged DMs are taken out of the list. This saves times |
michael@0 | 109 | // looking them up. Purged DMs are NOT deleted. |
michael@0 | 110 | fList.remove(dm); |
michael@0 | 111 | } else { |
michael@0 | 112 | cur = iter.prev(); |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | SkDiscardableMemory* SkDiscardableMemoryPool::create(size_t bytes) { |
michael@0 | 118 | void* addr = sk_malloc_flags(bytes, 0); |
michael@0 | 119 | if (NULL == addr) { |
michael@0 | 120 | return NULL; |
michael@0 | 121 | } |
michael@0 | 122 | SkPoolDiscardableMemory* dm = SkNEW_ARGS(SkPoolDiscardableMemory, |
michael@0 | 123 | (this, addr, bytes)); |
michael@0 | 124 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
michael@0 | 125 | fList.addToHead(dm); |
michael@0 | 126 | fUsed += bytes; |
michael@0 | 127 | this->dumpDownTo(fBudget); |
michael@0 | 128 | return dm; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | void SkDiscardableMemoryPool::free(SkPoolDiscardableMemory* dm) { |
michael@0 | 132 | // This is called by dm's destructor. |
michael@0 | 133 | if (dm->fPointer != NULL) { |
michael@0 | 134 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
michael@0 | 135 | sk_free(dm->fPointer); |
michael@0 | 136 | dm->fPointer = NULL; |
michael@0 | 137 | SkASSERT(fUsed >= dm->fBytes); |
michael@0 | 138 | fUsed -= dm->fBytes; |
michael@0 | 139 | fList.remove(dm); |
michael@0 | 140 | } else { |
michael@0 | 141 | SkASSERT(!fList.isInList(dm)); |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | bool SkDiscardableMemoryPool::lock(SkPoolDiscardableMemory* dm) { |
michael@0 | 146 | SkASSERT(dm != NULL); |
michael@0 | 147 | if (NULL == dm->fPointer) { |
michael@0 | 148 | #if LAZY_CACHE_STATS |
michael@0 | 149 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
michael@0 | 150 | ++fCacheMisses; |
michael@0 | 151 | #endif // LAZY_CACHE_STATS |
michael@0 | 152 | return false; |
michael@0 | 153 | } |
michael@0 | 154 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
michael@0 | 155 | if (NULL == dm->fPointer) { |
michael@0 | 156 | // May have been purged while waiting for lock. |
michael@0 | 157 | #if LAZY_CACHE_STATS |
michael@0 | 158 | ++fCacheMisses; |
michael@0 | 159 | #endif // LAZY_CACHE_STATS |
michael@0 | 160 | return false; |
michael@0 | 161 | } |
michael@0 | 162 | dm->fLocked = true; |
michael@0 | 163 | fList.remove(dm); |
michael@0 | 164 | fList.addToHead(dm); |
michael@0 | 165 | #if LAZY_CACHE_STATS |
michael@0 | 166 | ++fCacheHits; |
michael@0 | 167 | #endif // LAZY_CACHE_STATS |
michael@0 | 168 | return true; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | void SkDiscardableMemoryPool::unlock(SkPoolDiscardableMemory* dm) { |
michael@0 | 172 | SkASSERT(dm != NULL); |
michael@0 | 173 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
michael@0 | 174 | dm->fLocked = false; |
michael@0 | 175 | this->dumpDownTo(fBudget); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | size_t SkDiscardableMemoryPool::getRAMUsed() { |
michael@0 | 179 | return fUsed; |
michael@0 | 180 | } |
michael@0 | 181 | void SkDiscardableMemoryPool::setRAMBudget(size_t budget) { |
michael@0 | 182 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
michael@0 | 183 | fBudget = budget; |
michael@0 | 184 | this->dumpDownTo(fBudget); |
michael@0 | 185 | } |
michael@0 | 186 | void SkDiscardableMemoryPool::dumpPool() { |
michael@0 | 187 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
michael@0 | 188 | this->dumpDownTo(0); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 192 | SK_DECLARE_STATIC_MUTEX(gMutex); |
michael@0 | 193 | static void create_pool(SkDiscardableMemoryPool** pool) { |
michael@0 | 194 | SkASSERT(NULL == *pool); |
michael@0 | 195 | *pool = SkNEW_ARGS(SkDiscardableMemoryPool, |
michael@0 | 196 | (SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE, |
michael@0 | 197 | &gMutex)); |
michael@0 | 198 | } |
michael@0 | 199 | SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() { |
michael@0 | 200 | static SkDiscardableMemoryPool* gPool(NULL); |
michael@0 | 201 | SK_DECLARE_STATIC_ONCE(create_pool_once); |
michael@0 | 202 | SkOnce(&create_pool_once, create_pool, &gPool); |
michael@0 | 203 | SkASSERT(NULL != gPool); |
michael@0 | 204 | return gPool; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | //////////////////////////////////////////////////////////////////////////////// |