acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
allocator.h
1// Created by camilo on 2023-11-27 03:54 <3ThomasBorregaardSorensen!!
2#pragma once
3
4
5#include "acme/memory/allocator_base.h"
6#include "acme/platform/acme.h"
7#include "acme/prototype/string/c/strdup.h"
8
9
10namespace allocator
11{
12
13
14#if REFERENCING_DEBUGGING
15
16
17 CLASS_DECL_ACME void __on_start_construct(void * p, memsize s, bool bParticleAndHeapAllocation);
18
19 CLASS_DECL_ACME void __on_after_construct_subparticle(::subparticle* psubparticle);
20
21 CLASS_DECL_ACME void __on_after_construct_non_subparticle();
22
23 CLASS_DECL_ACME void start_referencing_debugging_suppression();
24
25 CLASS_DECL_ACME void stop_suppressing_referencing_debugging();
26
27 CLASS_DECL_ACME ::subparticle * task_get_top_track();
28
29 CLASS_DECL_ACME void on_construct_subparticle(::subparticle* psubparticle);
30
31 CLASS_DECL_ACME void on_after_construct_subparticle(::subparticle* psubparticle);
32
33 CLASS_DECL_ACME void __on_after_construct();
34
35 CLASS_DECL_ACME void on_destruct_subparticle(::subparticle* psubparticle);
36
37 CLASS_DECL_ACME void __on_after_construct_subparticle(::subparticle* psubparticle);
38
39 CLASS_DECL_ACME void __on_after_construct_non_subparticle();
40
41
42#endif
43
44
45 //template < typename ALLOCATOR_ACCESSOR, bool t_bDisableReferencingDebugging = false >
47 {
48 public:
49
50 //using ACCESSOR = ALLOCATOR_ACCESSOR;
51
52 template < typename T, typename ...Args >
53 static T * __accessor_on_construct(void * data, Args &&... args)
54 {
55
56 auto p = ::new(data) T(::std::forward<Args>(args)...);
57
58 return p;
59
60 }
61
62
63 template < typename T >
64 static void __accessor_on_destruct(T * p)
65 {
66
67 p->~T();
68
69 }
70
71
72 template < a_particle PARTICLE, typename ...Args >
73 static inline PARTICLE * __on_construct(void * data, memsize s, Args &&... args)
74 {
75
76 //__on_start_construct(data, s, t_bDisableReferencingDebugging, true);
77#if REFERENCING_DEBUGGING
78 __on_start_construct(data, s, true);
79#endif
80#if REFERENCING_DEBUGGING
81
82 auto p = __on_referencing_debugging_construct<PARTICLE>(data, s, ::std::forward<Args>(args)...);
83
84 __on_after_construct_subparticle(p);
85
86#else
87
88 auto p = __on_normal_construct<PARTICLE>(data, s, ::std::forward<Args>(args)...);
89
90#endif
91
92 //p->m_pAllocation = data;
93
94 return p;
95
96 }
97
98
99 template < non_particle NON_PARTICLE, typename ...Args >
100 inline static NON_PARTICLE * __on_construct(void * data, memsize s, Args &&... args)
101 {
102#if REFERENCING_DEBUGGING
103
104 __on_start_construct(data, s, false);
105#endif
106#if REFERENCING_DEBUGGING
107
108 auto p = __on_referencing_debugging_construct<NON_PARTICLE>(data, s, ::std::forward<Args>(args)...);
109
110 __on_after_construct();
111
112 return p;
113
114#else
115
116 return __on_normal_construct<NON_PARTICLE>(data, s, ::std::forward<Args>(args)...);
117
118#endif
119
120 }
121
122
123 template < a_particle PARTICLE, typename ...Args >
124 inline static PARTICLE * __on_referencing_debugging_construct(void * data, memsize s, Args &&... args)
125 {
126
127 //auto p = ::new(data) PARTICLE(::std::forward<Args>(args)...);
128
129 auto p = __accessor_on_construct<PARTICLE>(data, ::std::forward<Args>(args)...);
130
131 //on_after_construct_particle(p);
132
133 return p;
134
135 }
136
137
138 template < non_particle NON_PARTICLE, typename ...Args >
139 inline static NON_PARTICLE * __on_referencing_debugging_construct(void * data, memsize s, Args &&... args)
140 {
141
142 //auto p = ::new(pdata) NON_PARTICLE(::std::forward<Args>(args)...);
143#if REFERENCING_DEBUGGING
144
145 ::allocator::defer_erase_referer();
146#endif
147 auto p = __accessor_on_construct<NON_PARTICLE>(data, ::std::forward<Args>(args)...);
148
149 return p;
150
151 }
152
153
154 //template < non_particle NON_PARTICLE, typename ...Args >
155 //inline static NON_PARTICLE * __on_referencing_debugging_construct(void * data, memsize s, Args &&... args)
156 //{
157
158 // //auto p = ::new(pdata) NON_PARTICLE(::std::forward<Args>(args)...);
159
160 // auto p = __accessor_on_construct<NON_PARTICLE>(data, ::std::forward<Args>(args)...);
161
162 // return p;
163
164 //}
165
166
167 template < a_particle PARTICLE, typename ...Args >
168 inline static PARTICLE * __on_normal_construct(void * data, memsize s, Args &&... args)
169 {
170
171 //auto p = ::new(data) PARTICLE(::std::forward<Args>(args)...);
172
173 auto p = __accessor_on_construct<PARTICLE>(data, ::std::forward<Args>(args)...);
174
175#if REFERENCING_DEBUGGING
176 on_after_construct_particle(p);
177#endif
178
179 return p;
180
181 }
182
183
184 template < non_particle NON_PARTICLE, typename ...Args >
185 inline static NON_PARTICLE * __on_normal_construct(void * data, memsize s, Args &&... args)
186 {
187
188 //auto p = ::new(pdata) NON_PARTICLE(::std::forward<Args>(args)...);
189
190 auto p = __accessor_on_construct<NON_PARTICLE>(data, ::std::forward <Args>(args)...);
191
192 return p;
193
194 }
195
196
197 template < typename T >
198 inline static void __on_destruct(T * p)
199 {
200
201 //p->~T();
202
203 __accessor_on_destruct<T>(p);
204
205 }
206
207
208 template < typename T, typename ...Args >
209 static ::pointer < T > __allocator_base_allocate(::heap::allocator_base * pallocatorbase, Args &&... args)
210 {
211
212#if REFERENCING_DEBUGGING
213
214 auto preferer = ::allocator::get_top_referer();
215
216#endif
217
218 auto p = __allocator_base_new< T >(pallocatorbase, ::std::forward < Args >(args)...);
219
220 pointer < T > pointer{ transfer_t{}, p };
221
222#if REFERENCING_DEBUGGING
223
224 if (!p->is_referencing_debugging_enabled())
225 {
226
227 pointer.m_preferer = nullptr;
228
229 }
230 else
231 {
232
233 pointer.m_preferer = preferer;
234
235 }
236
237#endif
238
239 return ::transfer(pointer);
240
241 }
242
243
244// template < typename T, typename ...Args >
245// inline static T * __allocator_base_new(::heap::allocator_base * pallocatorbase, Args &&... args)
246// {
247//
248// auto s = sizeof(T);
249//
250// char_pointer pszAnnotation = nullptr;
251//
252//#if MEMORY_ANNOTATION
253//
254// pszAnnotation = ::c::strdup(::type<T>().name());
255//
256//#endif
257//
258// auto data = pallocatorbase->allocate(s, nullptr, pszAnnotation);
259//
260// auto p = __on_construct< T >(data, s, ::std::forward < Args >(args)...);
261//
262// return p;
263//
264// }
265//
266//
267// template < typename T >
268// inline static T * __allocator_base_new_array(::heap::allocator_base * pallocatorbase, ::collection::count c)
269// {
270//
271// constexpr auto sElement = sizeof(T);
272//
273// auto s = sElement * c;
274//
275// char_pointer pszAnnotation = nullptr;
276//
277//#if MEMORY_ANNOTATION
278//
279// pszAnnotation = ::c::strdup2("array of ", ::type<T>().name());
280//
281//#endif
282//
283// auto * data = pallocatorbase->allocate(s, pszAnnotation);
284//
285// auto p = (::u8*)data;
286//
287// for (::collection::index i = 0; i < c; i++)
288// {
289//
290// __on_construct < T >(data, sElement);
291//
292// p += sElement;
293//
294// }
295//
296// return (T *)data;
297//
298// }
299//
300//
301// template < typename T >
302// inline static void __allocator_base_delete(::heap::allocator_base * pallocatorbase, T * p)
303// {
304//
305// __on_destruct(p);
306//
307// pallocatorbase->free(p);
308//
309// }
310//
311//
312// template < typename T >
313// inline static void __allocator_base_delete(::heap::allocator_base * pallocatorbase, T* p, void * pAllocation)
314// {
315//
316// __on_destruct(p);
317//
318// pallocatorbase->free(pAllocation);
319//
320// }
321
322
323 template < typename T, typename ...Args >
324 static ::pointer < T > __memory_allocate(::heap::enum_memory ememory, Args &&... args)
325 {
326
327 auto pheapmanagement = ::acme::get()->m_pheapmanagement;
328
329 auto & pmemory = pheapmanagement->m_memorya[ememory];
330
331 auto p = ::transfer(__allocator_base_allocate< T >(
332 pmemory,
333 ::std::forward<Args>(args)...));
334
335 return ::transfer(p);
336
337 }
338
339
340 template < typename T, typename ...Args >
341 inline static T * __memory_new(::heap::enum_memory ememory, Args &&... args)
342 {
343
344 auto pheapmanagement = ::acme::get()->m_pheapmanagement;
345
346 auto & pmemory = pheapmanagement->m_memorya[ememory];
347
348 auto p = __allocator_base_new < T >(
349 pmemory,
350 ::std::forward < Args >(args)...);
351
352 return p;
353
354 }
355
356
357 template < typename T >
358 inline static T * __memory_new_array(::heap::enum_memory ememory, ::collection::count c)
359 {
360
361 auto pheapmanagement = ::acme::get()->m_pheapmanagement;
362
363 auto & pmemory = pheapmanagement->m_memorya[ememory];
364
365 auto p = __allocator_base_new_array < T >(
366 pmemory,
367 c);
368
369 return p;
370
371 }
372
373
374 template < typename T >
375 static void __memory_delete(::heap::enum_memory ememory, T * p)
376 {
377
378 auto pheapmanagement = ::acme::get()->m_pheapmanagement;
379
380 auto & pmemory = pheapmanagement->m_memorya[ememory];
381
382 __allocator_base_delete< T >(
383 pmemory,
384 p);
385
386 }
387
388
389 template < typename T >
390 static void __memory_delete(::heap::enum_memory ememory, T* p, void * pAllocation)
391 {
392
393 auto pheapmanagement = ::acme::get()->m_pheapmanagement;
394
395 auto & pmemory = pheapmanagement->m_memorya[ememory];
396
397 __allocator_base_delete< T >(
398 pmemory,
399 p,
400 pAllocation);
401
402 }
403
404
405 template < typename T, typename ...Args >
406 ::pointer < T > static __call_allocate(Args &&... args)
407 {
408
409 auto p = ::transfer(__memory_allocate< T >(
410 ::heap::e_memory_main,
411 ::std::forward<Args>(args)...));
412
413 return ::transfer(p);
414
415 }
416
417
418#if REFERENCING_DEBUGGING
419
420 //accessor < ALLOCATOR_ACCESSOR, t_bDisableReferencingDebugging > * __call__add_referer(const ::reference_referer & referer) const
421 accessor * __call__add_referer(const ::reference_referer & referer) const
422 {
423
424#if REFERENCING_DEBUGGING
425
426 ::allocator::push_referer(referer);
427
428#endif
429
430 return (accessor *) this;
431
432 }
433
434#endif
435
436
437 template < typename T, typename ...Args >
438 inline static T * __call_allocate(Args &&... args)
439 {
440
441 auto p = __memory_new < T >(
442 ::heap::e_memory_main,
443 ::std::forward < Args >(args)...);
444
445 return p;
446
447 }
448
449
450 template < typename T >
451 inline static T * øallocate_array(::collection::count c)
452 {
453
454 auto p = __memory_new_array < T >(
455 ::heap::e_memory_main,
456 c);
457
458 return p;
459
460 }
461
462
463 template < typename T >
464 static void __call__delete(T * p)
465 {
466
467 __memory_delete< T >(
468 ::heap::e_memory_main,
469 p);
470
471 }
472
473
474 template < typename T >
475 static void __call__delete(T* p, void * pAllocation)
476 {
477
478 __memory_delete< T >(
479 ::heap::e_memory_main,
480 p, pAllocation);
481
482 }
483
484 };
485
486
487 CLASS_DECL_ACME extern ::allocator::accessor * g_pacessorDefault;
488
489
490} // namespace allocator
491
492
493namespace platform
494{
495
496
497 using allocator = ::allocator::accessor;
498
499 //using raw_allocator = ::allocator::accessor < default_t, true >;
500
501
502} // namespace platform
503
504
505//template < typename T, typename ...Args >
506//::pointer < T > __call_allocate(Args &&... args)
507//{
508//
509// auto p = ::transfer(
510// ::platform::allocator::__call_allocate< T >(
511// ::std::forward<Args>(args)...));
512//
513// return ::transfer(p);
514//
515//}
516//
517
518
Definition allocator.h:47
Definition allocator_base.h:16
Definition _initial_concepts.h:860
Definition _initial_concepts.h:863
include "thread/AutoLock.h"
Definition _forward_declaration.h:877
Definition _structures.h:6