15 #if !defined(KRATOS_CHUNK_H_INCLUDED )
16 #define KRATOS_CHUNK_H_INCLUDED
75 , mpUninitializedMemory(
nullptr)
77 , mBlockSizeInBytes(BlockSizeInBytes)
78 , mNumberOfAvailableBlocks(0)
79 , mFirstAvailableBlock(mpData)
80 , mBlockSizeAfterAlignment((BlockSizeInBytes +
sizeof(
BlockType) - 1) /
sizeof(
BlockType)){}
98 const std::size_t data_size = DataSize();
100 mpEnd = mpData + data_size;
101 mFirstAvailableBlock = mpData;
102 mNumberOfAvailableBlocks = AllocatableDataSize() / mBlockSizeAfterAlignment;
103 mpUninitializedMemory = mpData;
117 BlockType * p_result = mFirstAvailableBlock;
118 mFirstAvailableBlock = (p_result >= mpUninitializedMemory) ? mFirstAvailableBlock + mBlockSizeAfterAlignment : (
BlockType*)*p_result;
131 mNumberOfAvailableBlocks--;
134 mpUninitializedMemory += mBlockSizeAfterAlignment * ((p_result >= mpUninitializedMemory) && (mpUninitializedMemory < mpEnd));
140 if (pPointrerToRelease ==
nullptr)
152 *p_to_release = (
BlockType)mFirstAvailableBlock;
153 mFirstAvailableBlock = p_to_release;
155 mNumberOfAvailableBlocks++;
157 pPointrerToRelease =
nullptr;
162 if (mpData ==
nullptr)
167 mpUninitializedMemory =
nullptr;
176 if (mpData ==
nullptr)
177 return sizeof(
Chunk);
182 if (mpData ==
nullptr)
183 return sizeof(
Chunk);
192 return AllocatableDataSize() / mBlockSizeAfterAlignment;
196 return mNumberOfAvailableBlocks;
208 return mpData + DataSize();
222 bool Has(
const void* pThePointer)
const {
223 return ((pThePointer >= mpData) && (pThePointer < mpEnd));
231 return (mNumberOfAvailableBlocks == 0);
235 return mpData !=
nullptr;
239 return mpData ==
nullptr;
243 while(std::atomic_flag_test_and_set_explicit(&mLocked, std::memory_order_acquire))
248 std::atomic_flag_clear_explicit(&mLocked, std::memory_order_release);
284 std::atomic_flag mLocked = ATOMIC_FLAG_INIT;
285 const SizeType mBlockSizeAfterAlignment;
299 SizeType AllocatableDataSize()
const {
300 std::size_t header_size = GetHeaderSize();
301 std::size_t data_size = DataSize();
302 if(data_size > header_size)
303 return data_size - header_size;
307 static std::size_t GetBlockSize(std::size_t BlockSizeInBytes) {
Chunk is the smallest building block of Kratos memory management.
Definition: chunk.h:45
bool IsFull()
Definition: chunk.h:230
bool IsInitialized() const
Definition: chunk.h:234
void * Allocate()
This function does not throw and returns zero if cannot allocate.
Definition: chunk.h:110
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: chunk.h:261
Chunk(Chunk &&rOther)=delete
void lock()
Definition: chunk.h:242
~Chunk() noexcept
Destructor is not virtual. This class can not be drived.
Definition: chunk.h:83
std::size_t MemoryOverhead() const
Definition: chunk.h:181
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: chunk.h:266
SizeType GetNumberOfBlocks() const
Definition: chunk.h:191
bool IsReleased() const
Definition: chunk.h:238
Chunk(std::size_t BlockSizeInBytes, SizeType SizeInBytes) noexcept
The constructor to be called.
Definition: chunk.h:72
std::size_t SizeType
Definition: chunk.h:56
std::size_t MemoryUsed() const
Definition: chunk.h:175
std::string Info() const
Turn back information as a string.
Definition: chunk.h:256
Chunk()=delete
Default constructor is deleted.
bool HasAvailableBlock() const
Definition: chunk.h:215
const BlockType * pGetData() const
Definition: chunk.h:199
Chunk & operator=(Chunk const &rOther)=delete
Assignment operator is deleted.
bool Has(const void *pThePointer) const
Definition: chunk.h:222
void Initialize()
Definition: chunk.h:97
void Release()
Definition: chunk.h:161
const BlockType * pDataBegin() const
Definition: chunk.h:203
Chunk(Chunk const &rOther)=delete
Copy constructor is deleted.
std::size_t Size() const
Definition: chunk.h:171
void Deallocate(void *pPointrerToRelease)
Definition: chunk.h:139
SizeType GetNumberOfAvailableBlocks() const
Definition: chunk.h:195
std::int64_t BlockType
Definition: chunk.h:54
void unlock()
Definition: chunk.h:247
const BlockType * pDataEnd() const
Definition: chunk.h:207
bool IsEmpty() const
Definition: chunk.h:226
#define KRATOS_DEBUG_CHECK_EQUAL(a, b)
Definition: checks.h:217
#define KRATOS_DEBUG_CHECK_NOT_EQUAL(a, b)
Definition: checks.h:218
#define KRATOS_DEBUG_CHECK(IsTrue)
Definition: checks.h:214
#define KRATOS_DEBUG_CHECK_GREATER_EQUAL(a, b)
Definition: checks.h:227
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432