Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
MemoryAllocator.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5namespace Stroika::Foundation::Memory {
6
7 /*
8 ********************************************************************************
9 ****************** Memory::STLAllocator<T,BASE_ALLOCATOR> **********************
10 ********************************************************************************
11 */
12 template <typename T, typename BASE_ALLOCATOR>
13 inline typename STLAllocator<T, BASE_ALLOCATOR>::pointer
14 STLAllocator<T, BASE_ALLOCATOR>::address (typename STLAllocator<T, BASE_ALLOCATOR>::reference _Val) const noexcept
15 {
16 return &_Val;
17 }
18 template <typename T, typename BASE_ALLOCATOR>
19 inline typename STLAllocator<T, BASE_ALLOCATOR>::const_pointer
20 STLAllocator<T, BASE_ALLOCATOR>::address (typename STLAllocator<T, BASE_ALLOCATOR>::const_reference _Val) const noexcept
21 {
22 return (&_Val);
23 }
24 template <typename T, typename BASE_ALLOCATOR>
25 inline STLAllocator<T, BASE_ALLOCATOR>::STLAllocator ()
26 : fBaseAllocator ()
27 {
28 }
29 template <typename T, typename BASE_ALLOCATOR>
30 inline STLAllocator<T, BASE_ALLOCATOR>::STLAllocator (const STLAllocator<T, BASE_ALLOCATOR>& from)
31 : fBaseAllocator (from.fBaseAllocator)
32 {
33 }
34 template <typename T, typename BASE_ALLOCATOR>
35 template <typename OTHER>
36 inline STLAllocator<T, BASE_ALLOCATOR>::STLAllocator (const STLAllocator<OTHER, BASE_ALLOCATOR>& from)
37 : fBaseAllocator (from.fBaseAllocator)
38 {
39 }
40 template <typename T, typename BASE_ALLOCATOR>
41 template <typename OTHER>
42 inline STLAllocator<T, BASE_ALLOCATOR>& STLAllocator<T, BASE_ALLOCATOR>::operator= (const STLAllocator<OTHER, BASE_ALLOCATOR>& rhs)
43 {
44 fBaseAllocator = rhs.from.fBaseAllocator;
45 return (*this);
46 }
47 template <typename T, typename BASE_ALLOCATOR>
48 inline STLAllocator<T, BASE_ALLOCATOR> STLAllocator<T, BASE_ALLOCATOR>::select_on_container_copy_construction () const
49 {
50 return (*this);
51 }
52 template <typename T, typename BASE_ALLOCATOR>
53 inline typename STLAllocator<T, BASE_ALLOCATOR>::pointer STLAllocator<T, BASE_ALLOCATOR>::allocate (size_type nElements)
54 {
55 // allocate storage for _Count elements of type T
56 return ((T*)fBaseAllocator.Allocate (nElements * sizeof (T)));
57 }
58 template <typename T, typename BASE_ALLOCATOR>
59 inline typename STLAllocator<T, BASE_ALLOCATOR>::pointer STLAllocator<T, BASE_ALLOCATOR>::allocate (size_type nElements, const void*)
60 {
61 return (allocate (nElements));
62 }
63 template <typename T, typename BASE_ALLOCATOR>
64 inline void STLAllocator<T, BASE_ALLOCATOR>::deallocate (pointer ptr, size_type)
65 {
66 if (ptr != nullptr) {
67 fBaseAllocator.Deallocate (ptr);
68 }
69 }
70 template <typename T, typename BASE_ALLOCATOR>
71 inline void STLAllocator<T, BASE_ALLOCATOR>::construct (pointer ptr)
72 {
73 new (ptr) T{};
74 }
75 template <typename T, typename BASE_ALLOCATOR>
76 inline void STLAllocator<T, BASE_ALLOCATOR>::construct (pointer ptr, const T& v)
77 {
78 new (ptr) T{v};
79 }
80 template <typename T, typename BASE_ALLOCATOR>
81 template <typename OTHERT>
82 inline void STLAllocator<T, BASE_ALLOCATOR>::destroy (OTHERT* p)
83 {
84 destroy_at (p);
85 }
86 template <typename T, typename BASE_ALLOCATOR>
87 template <typename... ARGS>
88 inline void STLAllocator<T, BASE_ALLOCATOR>::construct (pointer p, ARGS&&... args)
89 {
90 ::new ((void*)p) T{forward<ARGS> (args)...};
91 }
92 template <typename T, typename BASE_ALLOCATOR>
93 inline size_t STLAllocator<T, BASE_ALLOCATOR>::max_size () const noexcept
94 {
95 return numeric_limits<size_type>::max () / sizeof (T);
96 }
97 template <typename T, typename BASE_ALLOCATOR>
98 inline bool STLAllocator<T, BASE_ALLOCATOR>::operator== (const STLAllocator& /*rhs*/) const
99 {
100 // @see https://en.cppreference.com/w/cpp/memory/allocator/operator_cmp - stateless allocators are always equal
101 return true;
102 }
103
104}