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 SkPurgeableMemoryBlock_DEFINED michael@0: #define SkPurgeableMemoryBlock_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: class SkPurgeableMemoryBlock : public SkNoncopyable { michael@0: michael@0: public: michael@0: /** michael@0: * Whether or not this platform has an implementation for purgeable memory. michael@0: */ michael@0: static bool IsSupported(); michael@0: michael@0: /** michael@0: * Create a new purgeable memory block of 'size' bytes. Returns NULL if not supported on this michael@0: * platform or on failure. michael@0: * @param size Number of bytes requested. michael@0: * @return A new block, or NULL on failure. michael@0: */ michael@0: static SkPurgeableMemoryBlock* Create(size_t size); michael@0: michael@0: #ifdef SK_DEBUG michael@0: /** michael@0: * Whether the platform supports one shot purge of all unpinned blocks. If so, michael@0: * PurgeAllUnpinnedBlocks will be used to test a purge. Otherwise, purge will be called on michael@0: * individual blocks. michael@0: */ michael@0: static bool PlatformSupportsPurgingAllUnpinnedBlocks(); michael@0: michael@0: /** michael@0: * Purge all unpinned blocks at once, if the platform supports it. michael@0: */ michael@0: static bool PurgeAllUnpinnedBlocks(); michael@0: michael@0: // If PlatformSupportsPurgingAllUnpinnedBlocks returns true, this will not be called, so it can michael@0: // simply return false. michael@0: bool purge(); michael@0: michael@0: bool isPinned() const { return fPinned; } michael@0: #endif michael@0: michael@0: ~SkPurgeableMemoryBlock(); michael@0: michael@0: /** michael@0: * Output parameter for pin(), stating whether the data has been retained. michael@0: */ michael@0: enum PinResult { michael@0: /** michael@0: * The data has been purged, or this is the first call to pin. michael@0: */ michael@0: kUninitialized_PinResult, michael@0: michael@0: /** michael@0: * The data has been retained. The memory contains the same data it held when unpin() was michael@0: * called. michael@0: */ michael@0: kRetained_PinResult, michael@0: }; michael@0: michael@0: /** michael@0: * Pin the memory for use. Must not be called while already pinned. michael@0: * @param PinResult Whether the data was retained. Ignored on failure. michael@0: * @return Pointer to the pinned data on success. NULL on failure. michael@0: */ michael@0: void* pin(PinResult*); michael@0: michael@0: /** michael@0: * Unpin the data so it can be purged if necessary. michael@0: */ michael@0: void unpin(); michael@0: michael@0: private: michael@0: void* fAddr; michael@0: size_t fSize; michael@0: bool fPinned; michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: int fFD; michael@0: #endif michael@0: michael@0: // Unimplemented default constructor is private, to prevent manual creation. michael@0: SkPurgeableMemoryBlock(); michael@0: michael@0: // The correct way to create a new one is from the static Create. michael@0: SkPurgeableMemoryBlock(size_t); michael@0: }; michael@0: michael@0: #endif // SkPurgeableMemoryBlock_DEFINED