Newton Dynamics  4.00
dContainersAlloc.h
1 /* Copyright (c) <2003-2019> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef _D_CONTAINERS_ALLOC_H_
23 #define _D_CONTAINERS_ALLOC_H_
24 
25 #include "dCoreStdafx.h"
26 #include "dClassAlloc.h"
27 
28 template<class T>
30 {
31  public:
33  {
34  }
35 
37  {
38  }
39 
40  static void FlushFreeList()
41  {
42  }
43 };
44 
45 template<class T>
47 {
48  class FreeList
49  {
50  public:
51  FreeList* m_next;
52  };
53 
54  public:
56  {
57  }
58 
60  {
61  }
62 
63  void *operator new (size_t size)
64  {
65  FreeList** const freeList = GetFreeList();
66  if (*freeList)
67  {
68  m_size--;
69  FreeList* const self = *freeList;
70  *freeList = self->m_next;
71  return self;
72  }
73  else
74  {
75  return dMemory::Malloc(size);
76  }
77  }
78 
79  void operator delete (void* ptr)
80  {
81  FreeList** const freeList = GetFreeList();
82  FreeList* const self = (FreeList*)ptr;
83  self->m_next = *freeList;
84  *freeList = self;
85  m_size++;
86  }
87 
88  static void FlushFreeList()
89  {
90  FreeList** const freeList = GetFreeList();
91  FreeList* first = *freeList;
92  while (first)
93  {
94  FreeList* const self = first;
95  first = first->m_next;
96  dMemory::Free(self);
97  }
98  m_size = 0;
99  *freeList = nullptr;
100  }
101 
102  private:
103  static FreeList** GetFreeList()
104  {
105  static FreeList* freeList = nullptr;
106  return &freeList;
107  }
108  static dUnsigned32 m_size;
109 };
110 
111 template<class T>
113 
114 
115 #endif
dContainersFreeListAlloc
Definition: dContainersAlloc.h:47
dClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: dClassAlloc.h:29
dMemory::Free
static D_CORE_API void Free(void *const ptr)
Destroy a memory buffer previously allocated by Malloc.
Definition: dMemory.cpp:58
dContainersAlloc
Definition: dContainersAlloc.h:30
dMemory::Malloc
static D_CORE_API void * Malloc(size_t size)
General Memory allocation function.
Definition: dMemory.cpp:44