gfx/skia/trunk/src/core/SkTObjectPool.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /*
michael@0 2 * Copyright 2014 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 #ifndef SkFreeList_DEFINED
michael@0 9 #define SkFreeList_DEFINED
michael@0 10
michael@0 11 #include "SkTInternalSList.h"
michael@0 12
michael@0 13 /**
michael@0 14 * An implementation of a self growing pool of objects.
michael@0 15 * It maintains a pool of fully initialized objects. If an attempt is made to
michael@0 16 * acquire one, and there are none left, it makes some more.
michael@0 17 * It does not automatically reclaim them, they have to be given back to it.
michael@0 18 * Constructors will be called on objects allocated by the pool at allocation
michael@0 19 * time.
michael@0 20 * All allocated objects will be destroyed and memory will be reclaimed when
michael@0 21 * the pool is destroyed, so the pool must survive longer than you are using
michael@0 22 * any item taken from it.
michael@0 23 */
michael@0 24 template<typename T, int numItemsPerBlock = 4096/sizeof(T)> class SkTObjectPool {
michael@0 25 public:
michael@0 26 SkTObjectPool() {}
michael@0 27 ~SkTObjectPool() {
michael@0 28 while (!fBlocks.isEmpty()) {
michael@0 29 SkDELETE(fBlocks.pop());
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 /**
michael@0 34 * Get an item from the pool.
michael@0 35 * If the pool has no free items, it will allocate and construct some more.
michael@0 36 * The returned item is only valid as long as the pool has not been
michael@0 37 * destroyed, at that point all memory allocated by grow will have been
michael@0 38 * reclaimed.
michael@0 39 * This method is *not* thread safe.
michael@0 40 */
michael@0 41 T* acquire() {
michael@0 42 if (fAvailable.isEmpty()) {
michael@0 43 grow();
michael@0 44 }
michael@0 45 return fAvailable.pop();
michael@0 46 }
michael@0 47
michael@0 48 /**
michael@0 49 * Release an item into the pool.
michael@0 50 * The item does not have to have come from the pool, but if it did not
michael@0 51 * it must have a lifetime greater than the pool does.
michael@0 52 * This method is *not* thread safe.
michael@0 53 */
michael@0 54 void release(T* entry) {
michael@0 55 fAvailable.push(entry);
michael@0 56 }
michael@0 57
michael@0 58 /**
michael@0 59 * Takes all the items from an SkTInternalSList and adds them back to this
michael@0 60 * pool. The other list will be left empty.
michael@0 61 */
michael@0 62 void releaseAll(SkTInternalSList<T>* other) {
michael@0 63 fAvailable.pushAll(other);
michael@0 64 }
michael@0 65
michael@0 66 /**
michael@0 67 * Returns the number of items immediately available without having to
michael@0 68 * construct any new ones.
michael@0 69 */
michael@0 70 int available() const { return fAvailable.getCount(); }
michael@0 71
michael@0 72 /**
michael@0 73 * Returns the number of blocks of items the pool has allocated so far.
michael@0 74 */
michael@0 75 int blocks() const { return fBlocks.getCount(); }
michael@0 76
michael@0 77 private:
michael@0 78 /**
michael@0 79 * The type for a new block of entries for the list.
michael@0 80 */
michael@0 81 struct Block {
michael@0 82 T entries[numItemsPerBlock];
michael@0 83 SK_DECLARE_INTERNAL_SLIST_INTERFACE(Block);
michael@0 84 };
michael@0 85 SkTInternalSList<Block> fBlocks;
michael@0 86 SkTInternalSList<T> fAvailable;
michael@0 87
michael@0 88 /**
michael@0 89 * When the free list runs out of items, this method is called to allocate
michael@0 90 * a new block of them.
michael@0 91 * It calls the constructors and then pushes the nodes into the available
michael@0 92 * list.
michael@0 93 */
michael@0 94 void grow() {
michael@0 95 Block* block = SkNEW(Block);
michael@0 96 fBlocks.push(block);
michael@0 97 for(int index = 0; index < numItemsPerBlock; ++index) {
michael@0 98 fAvailable.push(&block->entries[index]);
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 };
michael@0 103
michael@0 104 #endif

mercurial