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 SkPurgeableMemoryBlock_DEFINED 9 #define SkPurgeableMemoryBlock_DEFINED 10 11 #include "SkTypes.h" 12 13 class SkPurgeableMemoryBlock : public SkNoncopyable { 14 15 public: 16 /** 17 * Whether or not this platform has an implementation for purgeable memory. 18 */ 19 static bool IsSupported(); 20 21 /** 22 * Create a new purgeable memory block of 'size' bytes. Returns NULL if not supported on this 23 * platform or on failure. 24 * @param size Number of bytes requested. 25 * @return A new block, or NULL on failure. 26 */ 27 static SkPurgeableMemoryBlock* Create(size_t size); 28 29 #ifdef SK_DEBUG 30 /** 31 * Whether the platform supports one shot purge of all unpinned blocks. If so, 32 * PurgeAllUnpinnedBlocks will be used to test a purge. Otherwise, purge will be called on 33 * individual blocks. 34 */ 35 static bool PlatformSupportsPurgingAllUnpinnedBlocks(); 36 37 /** 38 * Purge all unpinned blocks at once, if the platform supports it. 39 */ 40 static bool PurgeAllUnpinnedBlocks(); 41 42 // If PlatformSupportsPurgingAllUnpinnedBlocks returns true, this will not be called, so it can 43 // simply return false. 44 bool purge(); 45 46 bool isPinned() const { return fPinned; } 47 #endif 48 49 ~SkPurgeableMemoryBlock(); 50 51 /** 52 * Output parameter for pin(), stating whether the data has been retained. 53 */ 54 enum PinResult { 55 /** 56 * The data has been purged, or this is the first call to pin. 57 */ 58 kUninitialized_PinResult, 59 60 /** 61 * The data has been retained. The memory contains the same data it held when unpin() was 62 * called. 63 */ 64 kRetained_PinResult, 65 }; 66 67 /** 68 * Pin the memory for use. Must not be called while already pinned. 69 * @param PinResult Whether the data was retained. Ignored on failure. 70 * @return Pointer to the pinned data on success. NULL on failure. 71 */ 72 void* pin(PinResult*); 73 74 /** 75 * Unpin the data so it can be purged if necessary. 76 */ 77 void unpin(); 78 79 private: 80 void* fAddr; 81 size_t fSize; 82 bool fPinned; 83 #ifdef SK_BUILD_FOR_ANDROID 84 int fFD; 85 #endif 86 87 // Unimplemented default constructor is private, to prevent manual creation. 88 SkPurgeableMemoryBlock(); 89 90 // The correct way to create a new one is from the static Create. 91 SkPurgeableMemoryBlock(size_t); 92 }; 93 94 #endif // SkPurgeableMemoryBlock_DEFINED 95