Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
BlockAllocated.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5namespace Stroika::Foundation::Memory {
6
7 /*
8 ********************************************************************************
9 ************************* BlockAllocationUseHelper<T> **************************
10 ********************************************************************************
11 */
12 template <typename T>
13 inline void* BlockAllocationUseHelper<T>::operator new ([[maybe_unused]] size_t n)
14 {
15 Require (n == sizeof (T));
16 return BlockAllocator<T>{}.allocate (1);
17 }
18 template <typename T>
19 inline void* BlockAllocationUseHelper<T>::operator new ([[maybe_unused]] size_t n, int, const char*, int)
20 {
21 Require (n == sizeof (T));
22 return BlockAllocator<T>{}.allocate (1);
23 }
24 template <typename T>
25 inline void BlockAllocationUseHelper<T>::operator delete (void* p)
26 {
27 BlockAllocator<T>{}.deallocate (reinterpret_cast<T*> (p), 1);
28 }
29 template <typename T>
30 inline void BlockAllocationUseHelper<T>::operator delete (void* p, int, const char*, int)
31 {
32 BlockAllocator<T>{}.deallocate (reinterpret_cast<T*> (p), 1);
33 }
34
35 /*
36 ********************************************************************************
37 *************************** UsesBlockAllocation<T> *****************************
38 ********************************************************************************
39 */
40 template <typename T>
41 constexpr bool UsesBlockAllocation ()
42 {
43 return derived_from<T, BlockAllocationUseHelper<T>>;
44 }
45
46 /*
47 ********************************************************************************
48 ********************************* MakeSharedPtr<T> *****************************
49 ********************************************************************************
50 */
51 template <typename T, typename... ARGS_TYPE>
52 inline auto MakeSharedPtr (ARGS_TYPE&&... args) -> shared_ptr<T>
53 {
54 if constexpr (UsesBlockAllocation<T> ()) {
55 return allocate_shared<T> (BlockAllocator<T>{}, forward<ARGS_TYPE> (args)...);
56 }
57 else {
58 return make_shared<T> (forward<ARGS_TYPE> (args)...);
59 }
60 }
61
62 /*
63 ********************************************************************************
64 ******************* BlockAllocationUseGlobalAllocatorHelper<T> *****************
65 ********************************************************************************
66 */
67 template <typename T>
68 inline void* BlockAllocationUseGlobalAllocatorHelper<T>::operator new (size_t n)
69 {
70 return ::operator new (n);
71 }
72 template <typename T>
73 inline void* BlockAllocationUseGlobalAllocatorHelper<T>::operator new (size_t n, int, const char*, int)
74 {
75 return ::operator new (n);
76 }
77 template <typename T>
78 inline void BlockAllocationUseGlobalAllocatorHelper<T>::operator delete (void* p)
79 {
80 ::operator delete (p);
81 }
82 template <typename T>
83 inline void BlockAllocationUseGlobalAllocatorHelper<T>::operator delete (void* p, int, const char*, int)
84 {
85 ::operator delete (p);
86 }
87
88 /*
89 ********************************************************************************
90 *************************** ManuallyBlockAllocated<T> **************************
91 ********************************************************************************
92 */
93 template <typename T>
94 template <typename... ARGS>
95 inline T* ManuallyBlockAllocated<T>::New (ARGS&&... args)
96 {
97#if qStroika_Foundation_Memory_PreferBlockAllocation
98 return new (BlockAllocator<T>{}.allocate (1)) T{forward<ARGS> (args)...};
99#else
100 return new T{forward<ARGS> (args)...};
101#endif
102 }
103 template <typename T>
104 inline void ManuallyBlockAllocated<T>::Delete (T* p) noexcept
105 {
106#if qStroika_Foundation_Memory_PreferBlockAllocation
107 if (p != nullptr) {
108 destroy_at (p);
109 BlockAllocator<T>{}.deallocate (p, 1);
110 }
111#else
112 delete p;
113#endif
114 }
115
116}
auto MakeSharedPtr(ARGS_TYPE &&... args) -> shared_ptr< T >
same as make_shared, but if type T has block allocation, then use block allocation for the 'shared pa...