11 #include <debug/Crash.hpp> 12 #include <ios/Logger.hpp> 13 #include <system/SIMD.hpp> 14 #include <utility/ValWithExtreme.hpp> 19 #define DEFAULT_ALIGNMENT 53 const size_t alignment)
68 CRASH<<
"Failed to allocate "<<size<<
" with alignement "<<ALIGNMENT;
101 CRASH<<
"Unable to find dinamically allocated memory "<<ptr;
120 cached[size].push_back(ptr);
130 const size_t alignment)
133 reinterpret_cast<uintptr_t>(ptr)%alignment==0;
138 const size_t& alignment)
146 if(cachedIt==cached.end())
159 while(it!=list.begin()-1
and not isAligned(*it,alignment))
162 if(it==list.begin()-1)
177 cached.erase(cachedIt);
194 pushToCache(ptr,size);
216 template <
class T=
char>
218 const size_t alignment)
229 popFromCache(size,alignment);
234 allocateRawAligned(size,alignment);
238 pushToUsed(ptr,size);
241 static_cast<T*>(ptr);
264 while(el!=used.end())
286 while(el!=cached.end())
299 for(size_t i=0;i<n;i++)
313 template <
typename T>
317 stream<<
"Maximal memory used: "<<usedSize.extreme()<<
" bytes, currently used: "<<usedSize
318 <<
" bytes, number of allocation: "<<nUnalignedAlloc<<
" unaligned, "<<nAlignedAlloc<<
" aligned\n" 319 <<
"Maximal memory cached: "<<cachedSize.extreme()<<
" bytes, currently used: "<<cachedSize
320 <<
" bytes, number of reused: "<<nCachedReused;
326 runLog()<<
"Starting the memory manager";
332 runLog()<<
"Stopping the memory manager";
336 printStatistics(runLog());
T * provideAligned(const size_t nel, const size_t alignment)
Allocate or get from cache after computing the proper size.
void releaseAllUsedMemory()
Release all used memory.
ValWithMax< size_t > usedSize
Size of used memory.
void pushToCache(void *ptr, const size_t size)
Adds a memory to cache.
auto & printStatistics(T &&stream)
Print to a stream.
#define CRASH
Initialize the crasher.
#define DEFAULT_ALIGNMENT
Minimal alignment.
size_t popFromUsed(void *ptr)
ScopeIndenter(Logger &logger)
Create and increase indent level.
~Memory()
Destruct the memory manager.
void enableCache()
Enable cache usage.
size_t nCachedReused
Number of cached memory reused.
void release(T *ptr)
Decleare unused the memory.
void * allocateRawAligned(const size_t size, const size_t alignment)
void moveToCache(void *ptr)
Move the allocated memory to cache.
static bool isAligned(const void *ptr, const size_t alignment)
Check if a pointer is suitably aligned.
Logger runLog("/dev/stdout")
Global logger.
void pushToUsed(void *ptr, const size_t size)
Add to the list of used memory.
ValWithMax< size_t > cachedSize
Size of cached memory.
Memory memory
Memory manager.
decltype(auto) operator+(T1 &&smet1, T2 &&smet2)
Implement smet1+smet2.
void disableCache()
Disable cache usage.
#define SCOPE_INDENT(VAR)
Mark the stream to be more indented.
Memory()
Create the memory manager.
void * popFromCache(const size_t &size, const size_t &alignment)
Pop from the cache, returning to use.
void clearCache()
Release all memory from cache.