Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUFreeList.h
1 //
2 // CUFreeList.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This header provides a template for a free list class. A free list allows you to
6 // "recycle" memory. As allocating and deleting memory is an expensive operation,
7 // this has significant performance problems with systems that create a lot of short
8 // live objects (e.g. particle systems). Free lists solve this problem by replacing
9 // the new operator with a method to manage your memory.
10 //
11 // A free list can also allocate a block of memory at creation. This allows you to
12 // group all your initial allocations ahead of time. See the class documentation
13 // for more information.
14 //
15 // This is not a class. It is a class template. Templates do not have cpp files.
16 // They only have a header file. When you include the header, it compiles the specific
17 // template used by your program. Hence all of the code for this templated class is
18 // in this header.
19 //
20 // Author: Walker White
21 // Version: 11/15/15
22 //
23 #ifndef __CU_FREE_LIST_H__
24 #define __CU_FREE_LIST_H__
25 
26 #include <queue>
27 #include <cocos2d.h>
28 
29 NS_CC_BEGIN
30 
31 #pragma mark -
32 #pragma mark FreeList Template
33 
68 template <class T>
69 class CC_DLL FreeList {
70 protected:
72  size_t _allocated;
74  size_t _released;
76  size_t _peaksize;
77 
81  size_t _capacity;
82 
84  queue<T*> _freeobjs;
85 
89  queue<T*> _expansion;
90 
91 public:
102  FreeList(size_t capacity=0, bool expand=false) {
103  _expandable = expand;
104  _capacity = capacity;
105  _released = 0;
106  _allocated = 0;
107  _prealloc = (_capacity > 0 ? new T[_capacity] : nullptr);
108  }
109 
117  clear();
118  if (_prealloc != nullptr) {
119  delete[] _prealloc;
120  }
121  }
122 
123 
124 #pragma mark Accessors
125 
133  size_t getAvailable() const { return (_capacity-(long)_allocated)+_freeobjs.size(); }
134 
143  size_t getCapacity() const { return _capacity; }
144 
153  size_t getUsage() const { return _allocated-_released; }
154 
163  size_t getPeakUsage() const { return _peaksize; }
164 
173  bool isExpandable() const { return _expandable; }
174 
175  // TODO: COMMENT ME
176  T* getPreallocated() { return _prealloc; }
177 
178 
179 #pragma mark Memory Managment
180 
190  virtual T* alloc();
191 
206  void free(T* obj);
207 
215  virtual void clear();
216 };
217 
218 
219 #pragma mark -
220 #pragma mark Method Implementations
221 
232 template <class T>
234  T* result = nullptr;
235  if (!_freeobjs.empty()) {
236  result = _freeobjs.front();
237  _freeobjs.pop();
238  _allocated++;
239  } else if (_allocated < _capacity) {
240  result = &_prealloc[_allocated];
241  _allocated++;
242  } else if (_expandable) {
243  result = new T();
244  _expansion.push(result);
245  _allocated++;
246  }
247  _peaksize = (_peaksize < getUsage() ? getUsage() : _peaksize);
248  return result;
249 }
250 
265 template <class T>
266 void FreeList<T>::free(T* obj) {
267  CCASSERT(obj != nullptr, "Attempt to free null pointer");
268  _freeobjs.push(obj);
269  obj->reset();
270  _released++;
271 }
272 
280 template <class T>
282  // We own anything in expansion. Clear it.
283  while (!_expansion.empty()) {
284  T* a = _expansion.front();
285  _expansion.pop();
286  delete a;
287  }
288 
289  // Clear the preallocated
290  for(int ii =0; ii < _capacity; ii++) {
291  _prealloc[ii].reset();
292  }
293 
294  // Clear everything else
295  while (!_expansion.empty()) {
296  _freeobjs.pop();
297  }
298 
299  _allocated = 0;
300  _released = 0;
301 }
302 
303 NS_CC_END
304 
305 #endif /* defined(__CU_FREE_LIST_H__) */
size_t getAvailable() const
Definition: CUFreeList.h:133
queue< T * > _expansion
Definition: CUFreeList.h:89
size_t _peaksize
Definition: CUFreeList.h:76
queue< T * > _freeobjs
Definition: CUFreeList.h:84
~FreeList()
Definition: CUFreeList.h:116
size_t _allocated
Definition: CUFreeList.h:72
bool isExpandable() const
Definition: CUFreeList.h:173
size_t getCapacity() const
Definition: CUFreeList.h:143
size_t _released
Definition: CUFreeList.h:74
FreeList(size_t capacity=0, bool expand=false)
Definition: CUFreeList.h:102
bool _expandable
Definition: CUFreeList.h:87
size_t _capacity
Definition: CUFreeList.h:81
size_t getUsage() const
Definition: CUFreeList.h:153
size_t getPeakUsage() const
Definition: CUFreeList.h:163
Definition: CUFreeList.h:69
T * _prealloc
Definition: CUFreeList.h:79
virtual T * alloc()
Definition: CUFreeList.h:233
void free(T *obj)
Definition: CUFreeList.h:266
virtual void clear()
Definition: CUFreeList.h:281