Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* @file ObjectAllocator.hpp |
3 |
|
|
* @author Sergey Baigudin, sergey@baigudin.software |
4 |
|
|
* @copyright 2021-2022, Sergey Baigudin, Baigudin Software |
5 |
|
|
*/ |
6 |
|
|
#ifndef LIB_OBJECTALLOCATOR_HPP_ |
7 |
|
|
#define LIB_OBJECTALLOCATOR_HPP_ |
8 |
|
|
|
9 |
|
|
#include "lib.Allocator.hpp" |
10 |
|
|
|
11 |
|
|
namespace eoos |
12 |
|
|
{ |
13 |
|
|
namespace lib |
14 |
|
|
{ |
15 |
|
|
|
16 |
|
|
/** |
17 |
|
|
* @class ObjectAllocator<A> |
18 |
|
|
* @brief Object memory allocator. |
19 |
|
|
* |
20 |
|
|
* @tparam A Heap memory allocator class. |
21 |
|
|
*/ |
22 |
|
|
template <class A = Allocator> |
23 |
|
|
class ObjectAllocator |
24 |
|
|
{ |
25 |
|
|
|
26 |
|
|
public: |
27 |
|
|
|
28 |
|
|
#ifdef EOOS_ENABLE_DYNAMIC_HEAP_MEMORY |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* @brief Operator new. |
32 |
|
|
* |
33 |
|
|
* @param size A number of bytes to allocate. |
34 |
|
|
* @return Allocated memory address or a null pointer. |
35 |
|
|
*/ |
36 |
|
388 |
static void* operator new(size_t const size) EOOS_KEYWORD_NOEXCEPT |
37 |
|
|
{ |
38 |
|
388 |
return A::allocate(size); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* @brief Operator delete. |
43 |
|
|
* |
44 |
|
|
* @param ptr An address of allocated memory block or a null pointer. |
45 |
|
|
*/ |
46 |
|
366 |
static void operator delete(void* const ptr) |
47 |
|
|
{ |
48 |
|
366 |
A::free(ptr); |
49 |
|
366 |
} |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* @brief Operator new. |
53 |
|
|
* |
54 |
|
|
* @param ptr A pointer to reserved memory area. |
55 |
|
|
* @return The given pointer. |
56 |
|
|
*/ |
57 |
|
2 |
static void* operator new(size_t, void* const ptr) EOOS_KEYWORD_NOEXCEPT |
58 |
|
|
{ |
59 |
|
2 |
return ptr; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* @brief Operator delete. |
64 |
|
|
*/ |
65 |
|
|
static void operator delete(void*, void*) |
66 |
|
|
{ ///< UT Justified Branch: Language dependency |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
#endif // EOOS_ENABLE_DYNAMIC_HEAP_MEMORY |
70 |
|
|
|
71 |
|
|
protected: |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* @brief Destructor. |
75 |
|
|
* |
76 |
|
|
* @note It's prohibited to cast to this class to delete any child classes. |
77 |
|
|
*/ |
78 |
|
2416 |
~ObjectAllocator() |
79 |
|
|
{ |
80 |
|
2416 |
} |
81 |
|
|
|
82 |
|
|
}; |
83 |
|
|
|
84 |
|
|
} // namespace lib |
85 |
|
|
} // namespace eoos |
86 |
|
|
|
87 |
|
|
#endif // OBJECTALLOCATOR_HPP_ |
88 |
|
|
|