acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
fixed_alloc.h
1#pragma once
2
3
4#include "acme/prototype/collection/address_array.h"
5
6
7//namespace main_memory_allocate_heap
8//{
9//
10 struct plex;
11//
12//}
13
14
15class CLASS_DECL_ACME fixed_alloc_no_sync
16{
17public:
18
19
20 struct node
21 {
22 node* pNext; // only valid when in free list_base
23 };
24
25 ::heap::allocator * m_pallocator;
26 ::u32 m_nAllocSize; // i32_size of each block from Alloc
27 ::u32 m_nBlockSize; // number of blocks to get at a time
28 ::plex* m_pBlocks; // linked list_base of blocks (is nBlocks*nAllocSize)
29 node* m_pnodeFree; // first free node (nullptr if no free nodes)
30
31
32 fixed_alloc_no_sync(::heap::allocator * pallocator, ::u32 nAllocSize, ::u32 nBlockSize = 64);
33 ~fixed_alloc_no_sync();
34
35
36 ::u32 GetAllocSize() { return m_nAllocSize; }
37
38
39 inline void * Alloc(); // return a chunk of memory of nAllocSize
40 inline void Free(void * p); // free chunk of memory returned from Alloc
41 void FreeAll(); // free everything allocated from this allocator
42
43
44 void NewBlock();
45
46};
47
48
49inline void * fixed_alloc_no_sync::Alloc()
50{
51 if (m_pnodeFree == nullptr)
52 {
53 NewBlock();
54 }
55 // erase the first available node from the free list_base
56 void * pNode = m_pnodeFree;
57 m_pnodeFree = m_pnodeFree->pNext;
58 return (void *) (((::u8 *)pNode) + 16);
59}
60
61inline void fixed_alloc_no_sync::Free(void * p)
62{
63 p = (void *) (((::u8 *)p) - 16);
64 if (p != nullptr)
65 {
66 // simply return the node to the free list_base
67 node* pNode = (node*)p;
68 pNode->pNext = m_pnodeFree;
69 m_pnodeFree = pNode;
70 }
71}
72
73
74class CLASS_DECL_ACME fixed_alloc_sync
75{
76public:
77
78
79 ::heap::allocator * m_pallocator;
80 ::i32 m_i32;
81 ::i32 m_iShareCount;
82 ::array_base < ::critical_section > m_criticalsectiona;
83 address_array_base < fixed_alloc_no_sync * > m_allocptra;
84
85
86 fixed_alloc_sync(::heap::allocator * pallocator, ::u32 nAllocSize, ::u32 nBlockSize = 64, ::i32 iShareCount = 2);
87 ~fixed_alloc_sync();
88
89
90 inline void * Alloc(); // return a chunk of memory of nAllocSize
91 inline void Free(void * p); // free chunk of memory returned from Alloc
92 void FreeAll(); // free everything allocated from this allocator
93
94
95};
96
97
98
99
100class CLASS_DECL_ACME fixed_alloc
101{
102public:
103
104
105 ::heap::allocator * m_pallocator;
106 ::i32 m_i32;
107 ::i32 m_iShareCount;
108 address_array_base < fixed_alloc_sync * > m_allocptra;
109
110
111 fixed_alloc(::heap::allocator * pallocator, ::u32 nAllocSize, ::u32 nBlockSize = 64);
112 ~fixed_alloc();
113
114
115 inline void * Alloc(); // return a chunk of memory of nAllocSize
116 inline void Free(void * p); // free chunk of memory returned from Alloc
117 void FreeAll(); // free everything allocated from this allocator
118
119
120};
121
122
123class CLASS_DECL_ACME fixed_alloc_array :
124 public address_array_base < fixed_alloc * >
125{
126public:
127
128
129 ::heap::allocator * m_pallocator;
130
131 fixed_alloc_array(::heap::allocator * pallocator);
132 virtual ~fixed_alloc_array();
133
134 void * _alloc(size_t nAllocSize, memsize * psizeAllocated);
135 void _free(void * p, size_t nAllocSize);
136 void * _realloc(void * p, size_t nOldAllocSize, size_t nNewAllocSize);
137
138 fixed_alloc * find(size_t nAllocSize, memsize * psizeFound);
139
140
141};
142
143
144
145
146// DECLARE_FIXED_ALLOC -- used in class definition
147/*#define DECLARE_AXIS_FIXED_ALLOC(class_name) \
148public: \
149 void * operator new(size_t) { return s_palloc->Alloc(); } \
150 void * operator new(size_t, void * p) { return p; } \
151 void operator delete(void * p) { s_palloc->Free(p); } \
152 void * operator new(size_t, const_char_pointer ,::i32) { return s_palloc->Alloc(); } \
153 void operator delete(void * p, const_char_pointer ,::i32) { s_palloc->Free(p); } \
154 static fixed_alloc * s_palloc;
155
156
157// IMPLEMENT_FIXED_ALLOC -- used in class implementation file
158#define IMPLEMENT_AXIS_FIXED_ALLOC_STATIC(class_name) \
159fixed_alloc * class_name::s_palloc = nullptr;
160
161
162#define IMPLEMENT_AXIS_FIXED_ALLOC_CONSTRUCTOR(class_name, block_size) \
163class_name::s_palloc = allocateø< fixed_alloc(sizeof >(class_name), block_size);
164
165#define IMPLEMENT_AXIS_FIXED_ALLOC_DESTRUCTOR(class_name) \
166if(class_name::s_palloc != nullptr) \
167{ \
168 delete class_name::s_palloc; \
169}*/
170
171
172
173
174
175
Definition address_array.h:12
Definition fixed_alloc.h:101
Definition allocator.h:37
Definition fixed_alloc.h:21
Definition plex.h:6