OS  v1.8.0
Documentation
Loading...
Searching...
No Matches
memory.hpp
1#ifndef QOS_CPP_MEMORY
2#define QOS_CPP_MEMORY
3
4#include "include/types.hpp"
5
6#ifndef Q_BYTE_ALIGNMENT
7 #define Q_BYTE_ALIGNMENT ( 8 )
8#endif
9
10#if ( ( Q_BYTE_ALIGNMENT != 1 ) && ( Q_BYTE_ALIGNMENT != 2 ) && ( Q_BYTE_ALIGNMENT != 4 ) && ( Q_BYTE_ALIGNMENT != 8 ) )
11 #error Q_BYTE_ALIGNMENT value not allowed, use only 1,2,4 or 8(default).
12#endif
13
14namespace qOS {
15
19 namespace mem {
20
25
27 using address_t = size_t;
28 struct blockConnect_t {
29 blockConnect_t *next{ nullptr };
30 size_t blockSize{ 0U };
31 };
33
42 class pool {
43 private:
44 blockConnect_t *end{ nullptr };
45 blockConnect_t start;
46 uint8_t *poolMemory{ nullptr };
47 size_t poolMemSize{ 0U };
48 size_t freeBytesRemaining{ 0U };
49 void insertBlockIntoFreeList( blockConnect_t *xBlock ) noexcept;
50 void init( void ) noexcept;
51 pool( pool const& ) = delete;
52 void operator=( pool const& ) = delete;
53 public:
55 virtual ~pool() {}
64 inline pool( void *pArea, const size_t pSize ) noexcept {
65 (void)setup( pArea, pSize );
66 }
67
76 bool setup( void *pArea, const size_t pSize ) noexcept;
92 void free( void *ptr ) noexcept;
103 void* alloc( size_t pSize ) noexcept;
109 size_t getFreeSize( void ) const noexcept;
115 size_t getTotalSize( void ) const noexcept;
121 void* getPoolArea( void ) const noexcept;
126 bool isInitialized( void ) const {
127 return ( nullptr != poolMemory ) && ( poolMemSize > 0U );
128 }
129
133 explicit operator bool() const noexcept {
134 return isInitialized();
135 }
136 };
137
139 }
140}
141
142 #if ( Q_DEFAULT_HEAP_SIZE >= 64 )
143 void * operator new( size_t size);
144 void * operator new[]( size_t size);
145
146 void operator delete(void * ptr) noexcept;
147 void operator delete[](void * ptr) noexcept;
148
149 void operator delete(void* ptr, void* place) noexcept;
150 void operator delete[](void* ptr, void* place) noexcept;
151 #endif /*Q_USE_MEM_ALLOCATION_SCHEME*/
152
153#endif /*QOS_CPP_MEMORY*/
bool isInitialized(void) const
Check if the memory pool instance has been initialized.
Definition memory.hpp:126
bool setup(void *pArea, const size_t pSize) noexcept
Initializes a memory pool instance. This function should be called once before any heap memory reques...
size_t getTotalSize(void) const noexcept
Returns the total amount of heap space requested for the the memory pool.
void * getPoolArea(void) const noexcept
Returns a pointer to a memory block statically allocated to act as Heap of the memory pool.
pool(void *pArea, const size_t pSize) noexcept
Initializes a memory pool instance.
Definition memory.hpp:64
void * alloc(size_t pSize) noexcept
Allocate a block of memory that is pSize bytes large. If the requested memory can be allocated,...
void free(void *ptr) noexcept
Deallocates the space previously allocated by mem::pool::alloc(). Deallocation will be performed in t...
size_t getFreeSize(void) const noexcept
Returns the total amount of heap space that remains unallocated for the memory pool.
Memory management interfaces.
Definition memory.hpp:19
OS/Kernel interfaces.
Definition bytebuffer.hpp:7