aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrMemoryPool.h
diff options
context:
space:
mode:
authorGravatar dskiba <dskiba@chromium.org>2016-11-29 06:50:35 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2016-11-29 06:50:35 -0800
commite4cd00699167cefde9abedbd49ede64f82d552c7 (patch)
tree4921bc19c367263ce66532675cb846ee3bc383ee /src/gpu/GrMemoryPool.h
parentc51c18fd783737391e26541cbb5a72af2396ef7b (diff)
Make GrMemoryPool play nice with bucketing allocators.
Some memory allocators have very coarse size buckets, so for example on Android (jemalloc) an attempt to allocate 32 KiB + 1 byte will end up allocating 40 KiB, wasting 8 KiB. GrMemoryPool ctor takes two arguments that specify prealloc / block sizes, and then inflates them to accommodate some bookkeeping structures. Since most places create GrMemoryPools with pow2 numbers (which have buckets in most allocators) the inflation causes allocator to select next size bucket, wasting memory. This CL makes GrMemoryPool to stop inflating sizes it was created with, and allocate specified amounts exactly. Part of allocated memory is then used for bookkeeping structures. Additionally, GrObjectMemoryPool template is provided, which takes prealloc / block object counts (instead of sizes) and guarantees that specified number of objects will fit in prealloc / block spaces. BUG=651872 GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2525773002 Review-Url: https://codereview.chromium.org/2525773002
Diffstat (limited to 'src/gpu/GrMemoryPool.h')
-rw-r--r--src/gpu/GrMemoryPool.h105
1 files changed, 94 insertions, 11 deletions
diff --git a/src/gpu/GrMemoryPool.h b/src/gpu/GrMemoryPool.h
index 43826d354a..e483aab6f2 100644
--- a/src/gpu/GrMemoryPool.h
+++ b/src/gpu/GrMemoryPool.h
@@ -13,16 +13,23 @@
/**
* Allocates memory in blocks and parcels out space in the blocks for allocation
* requests. It is optimized for allocate / release speed over memory
- * effeciency. The interface is designed to be used to implement operator new
+ * efficiency. The interface is designed to be used to implement operator new
* and delete overrides. All allocations are expected to be released before the
* pool's destructor is called. Allocations will be 8-byte aligned.
*/
class GrMemoryPool {
public:
/**
- * Prealloc size is the amount of space to make available at pool creation
- * time and keep around until pool destruction. The min alloc size is the
- * smallest allowed size of additional allocations.
+ * Prealloc size is the amount of space to allocate at pool creation
+ * time and keep around until pool destruction. The min alloc size is
+ * the smallest allowed size of additional allocations. Both sizes are
+ * adjusted to ensure that:
+ * 1. they are are 8-byte aligned
+ * 2. minAllocSize >= kSmallestMinAllocSize
+ * 3. preallocSize >= minAllocSize
+ *
+ * Both sizes is what the pool will end up allocating from the system, and
+ * portions of the allocated memory is used for internal bookkeeping.
*/
GrMemoryPool(size_t preallocSize, size_t minAllocSize);
@@ -48,6 +55,16 @@ public:
*/
size_t size() const { return fSize; }
+ /**
+ * Returns the preallocated size of the GrMemoryPool
+ */
+ size_t preallocSize() const { return fHead->fSize; }
+
+ /**
+ * Minimum value of minAllocSize constructor argument.
+ */
+ constexpr static size_t kSmallestMinAllocSize = 1 << 10;
+
private:
struct BlockHeader;
@@ -81,14 +98,7 @@ private:
BlockHeader* fHeader; ///< pointer back to the block header in which an alloc resides
};
- enum {
- // We assume this alignment is good enough for everybody.
- kAlignment = 8,
- kHeaderSize = GR_CT_ALIGN_UP(sizeof(BlockHeader), kAlignment),
- kPerAllocPad = GR_CT_ALIGN_UP(sizeof(AllocHeader), kAlignment),
- };
size_t fSize;
- size_t fPreallocSize;
size_t fMinAllocSize;
BlockHeader* fHead;
BlockHeader* fTail;
@@ -96,6 +106,79 @@ private:
int fAllocationCnt;
int fAllocBlockCnt;
#endif
+
+protected:
+ enum {
+ // We assume this alignment is good enough for everybody.
+ kAlignment = 8,
+ kHeaderSize = GR_CT_ALIGN_UP(sizeof(BlockHeader), kAlignment),
+ kPerAllocPad = GR_CT_ALIGN_UP(sizeof(AllocHeader), kAlignment),
+ };
+};
+
+/**
+ * Variant of GrMemoryPool that can only allocate objects of a single type. It is
+ * not as flexible as GrMemoryPool, but it has more convenient allocate() method,
+ * and more importantly, it guarantees number of objects that are preallocated at
+ * construction or when adding a new memory block. I.e.
+ *
+ * GrMemoryPool pool(3 * sizeof(T), 1000 * sizeof(T));
+ * pool.allocate(sizeof(T));
+ * pool.allocate(sizeof(T));
+ * pool.allocate(sizeof(T));
+ *
+ * will preallocate 3 * sizeof(T) bytes and use some of those bytes for internal
+ * structures. Because of that, last allocate() call will end up allocating a new
+ * block of 1000 * sizeof(T) bytes. In contrast,
+ *
+ * GrObjectMemoryPool<T> pool(3, 1000);
+ * pool.allocate();
+ * pool.allocate();
+ * pool.allocate();
+ *
+ * guarantees to preallocate enough memory for 3 objects of sizeof(T), so last
+ * allocate() will use preallocated memory and won't cause allocation of a new block.
+ *
+ * Same thing is true for the second (minAlloc) ctor argument: this class guarantees
+ * that a newly added block will have enough space for 1000 objects of sizeof(T), while
+ * GrMemoryPool does not.
+ */
+template <class T>
+class GrObjectMemoryPool: public GrMemoryPool {
+public:
+ /**
+ * Preallocates memory for preallocCount objects, and sets new block size to be
+ * enough to hold minAllocCount objects.
+ */
+ GrObjectMemoryPool(size_t preallocCount, size_t minAllocCount)
+ : GrMemoryPool(CountToSize(preallocCount),
+ CountToSize(SkTMax(minAllocCount, kSmallestMinAllocCount))) {
+ }
+
+ /**
+ * Allocates memory for an object, but doesn't construct or otherwise initialize it.
+ * The memory must be freed with release().
+ */
+ T* allocate() { return static_cast<T*>(GrMemoryPool::allocate(sizeof(T))); }
+
+private:
+ constexpr static size_t kTotalObjectSize =
+ kPerAllocPad + GR_CT_ALIGN_UP(sizeof(T), kAlignment);
+
+ constexpr static size_t CountToSize(size_t count) {
+ return kHeaderSize + count * kTotalObjectSize;
+ }
+
+public:
+ /**
+ * Minimum value of minAllocCount constructor argument.
+ */
+ constexpr static size_t kSmallestMinAllocCount =
+ (GrMemoryPool::kSmallestMinAllocSize - kHeaderSize + kTotalObjectSize - 1) /
+ kTotalObjectSize;
};
+template <class T>
+constexpr size_t GrObjectMemoryPool<T>::kSmallestMinAllocCount;
+
#endif