gwnavruntime/kernel/SF_StackMemPool.h Source File

SF_StackMemPool.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : KY_StackMemPool.h
11 Content : Policy-based memory pool, which tries to allocate memory
12  on stack first.
13 Created : January 22, 2009
14 Authors : Sergey Sikorskiy
15 
16 **************************************************************************/
17 
18 #ifndef INC_KY_Kernel_StackMemPool_H
19 #define INC_KY_Kernel_StackMemPool_H
20 
23 
24 namespace Kaim {
25 
27 // StackMemPool Alloc/Free policies.
28 
29 // Regular Alloc/Free logic.
30 struct MemPoolImmediateFree
31 {
32  MemPoolImmediateFree(MemoryHeap* pheap) : pHeap(pheap) {}
33 
34  void* Alloc(UPInt nbytes, UPInt align)
35  {
36  return pHeap ? Memory::AllocInHeap(pHeap, nbytes, align) : Memory::Alloc(nbytes, align);
37  }
38  void Free(void* p)
39  {
40  Memory::Free(p);
41  }
42 
43 private:
44  MemoryHeap* pHeap;
45 };
46 
47 // Postpone freeing of memory till the very end.
48 struct MemPoolPostponeFree
49 {
50  MemPoolPostponeFree(MemoryHeap* pheap) : pHeap(pheap) {}
51  ~MemPoolPostponeFree()
52  {
53  for (UPInt i = 0; i < AllocatedMem.GetSize(); ++i)
54  {
55  Memory::Free(AllocatedMem[i]);
56  }
57  }
58 
59  void* Alloc(UPInt nbytes, UPInt align)
60  {
61  void* mem = pHeap ? Memory::AllocInHeap(pHeap, nbytes, align) : Memory::Alloc(nbytes, align);
62 
63  if (mem)
64  {
65  AllocatedMem.PushBack(mem);
66  }
67 
68  return mem;
69  }
70  static void Free(void* /*p*/)
71  {
72  ; // Do nothing.
73  }
74 
75 private:
76  MemoryHeap* pHeap;
77  Array<void*> AllocatedMem;
78 };
79 
81 // Memory pool, which tries to allocate memory on stack.
82 // It can be used as a more flexible version of a stack memory buffer, or as
83 // a faster memory allocator for in-place constructors.
84 template <UPInt N = 512, UPInt A = sizeof(void*), class AFP = MemPoolImmediateFree>
85 class StackMemPool : AFP
86 {
87 public:
88  StackMemPool(MemoryHeap* pheap = NULL)
89  : AFP(pheap), BuffPtr(AlignMem(Buff)), BuffSize(buff_max_size - (BuffPtr - Buff))
90  {
91  }
92 
93 public:
94  void* Alloc(UPInt nbytes)
95  {
96  void* tmpBuffPtr = NULL;
97 
98  if (nbytes <= BuffSize)
99  {
100  // Allocate in Buffer.
101  tmpBuffPtr = BuffPtr;
102 
103  BuffPtr = AlignMem(BuffPtr + nbytes);
104  const UPInt curSize = BuffPtr - Buff;
105  BuffSize = buff_max_size > curSize ? buff_max_size - curSize : 0;
106  } else
107  {
108  // Allocate in heap.
109  tmpBuffPtr = AFP::Alloc(nbytes, align);
110  }
111 
112  return tmpBuffPtr;
113  }
114  void Free(void* p)
115  {
116  if (!(p >= Buff && p < Buff + buff_max_size))
117  {
118  // Pointer is not inside of the Buffer.
119  AFP::Free(p);
120  }
121  }
122 
123 private:
124  // Return new aligned offset. Align must be power of two.
125  static
126  char* AlignMem(char* offset)
127  {
128  return reinterpret_cast<char*>(A + ((reinterpret_cast<UPInt>(offset) - 1) & ~(A - 1)));
129  }
130 
131 private:
132  // align must be power of two.
133  enum { buff_max_size = N, align = A };
134 
135  char Buff[buff_max_size];
136  char* BuffPtr;
137  UPInt BuffSize;
138 };
139 
140 } // Scaleform
141 
142 #undef new
143 
144 template <Kaim::UPInt N, Kaim::UPInt A>
145 inline
146 void* operator new (Kaim::UPInt nbytes, Kaim::StackMemPool<N, A>& pool)
147 {
148  return pool.Alloc(nbytes);
149 }
150 
151 template <Kaim::UPInt N, Kaim::UPInt A>
152 inline
153 void operator delete (void* p, Kaim::StackMemPool<N, A>& pool)
154 {
155  pool.Free(p);
156 }
157 
158 // Redefine operator 'new' if necessary.
159 #if defined(KY_DEFINE_NEW)
160 #define new KY_DEFINE_NEW
161 #endif
162 
163 // This header has to follow "#define new".
165 
166 #endif // INC_GMEMSTACKPOOL_H
Definition: gamekitcrowddispersion.h:20