Skip to content

Commit

Permalink
Changes for Size no longer being alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Epixu committed Jan 22, 2024
1 parent 716478d commit 82a6d6e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
9 changes: 5 additions & 4 deletions source/Allocation.inl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Allocation.hpp"
#include <Core/Utilities.hpp>


namespace Langulus::Fractalloc
{
namespace Inner
Expand All @@ -20,17 +21,17 @@ namespace Langulus::Fractalloc
/// @param u - number
/// @return the log2
LANGULUS(INLINED)
constexpr Size FastLog2(Size x) noexcept {
constexpr Size FastLog2(const Size x) noexcept {
return x < 2
? 0 : Size{8 * sizeof(Size)} - ::std::countl_zero(x) - Size{1};
? 0 : Size{8 * sizeof(Size)} - ::std::countl_zero(x.mSize) - Size{1};
}

/// Get least significant bit
/// https://stackoverflow.com/questions/757059
/// @param n - number
/// @return the least significant bit
LANGULUS(INLINED)
constexpr Size LSB(const Size& n) noexcept {
constexpr Size LSB(const Size n) noexcept {
#if LANGULUS(BITNESS) == 32
constexpr Size DeBruijnBitPosition[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
Expand Down Expand Up @@ -70,7 +71,7 @@ namespace Langulus::Fractalloc
/// @return the byte size of the entry, including alignment
LANGULUS(INLINED)
constexpr Size Allocation::GetSize() noexcept {
static_assert(IsPowerOfTwo(Alignment),
static_assert(IsPowerOfTwo(Alignment.mSize),
"Alignment is not a power-of-two number");
return sizeof(Allocation) + Alignment - (sizeof(Allocation) % Alignment);
}
Expand Down
30 changes: 15 additions & 15 deletions source/Allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace Langulus::Fractalloc

// Align pointer to the alignment LANGULUS was built with
auto ptr = reinterpret_cast<T*>(
(reinterpret_cast<Size>(base) + Alignment)
& ~(Alignment - Size {1})
(reinterpret_cast<Offset>(base) + Alignment)
& ~(Alignment - Offset {1})
);

// Place the entry there
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace Langulus::Fractalloc
if (memory) {
#if LANGULUS_FEATURE(MEMORY_STATISTICS)
Instance.mStatistics.mEntries += 1;
Instance.mStatistics.mBytesAllocatedByFrontend += memory->GetTotalSize();
Instance.mStatistics.mBytesAllocatedByFrontend.mSize += memory->GetTotalSize();
#endif
return memory;
}
Expand Down Expand Up @@ -155,8 +155,8 @@ namespace Langulus::Fractalloc
// New size is bigger, precautions must be taken
if (previous->mPool->Reallocate(previous, size)) {
#if LANGULUS_FEATURE(MEMORY_STATISTICS)
Instance.mStatistics.mBytesAllocatedByFrontend -= oldSize;
Instance.mStatistics.mBytesAllocatedByFrontend += previous->GetTotalSize();
Instance.mStatistics.mBytesAllocatedByFrontend.mSize -= oldSize;
Instance.mStatistics.mBytesAllocatedByFrontend.mSize += previous->GetTotalSize();
#endif
return previous;
}
Expand All @@ -180,7 +180,7 @@ namespace Langulus::Fractalloc
"Deallocating an allocation used from multiple places");

#if LANGULUS_FEATURE(MEMORY_STATISTICS)
Instance.mStatistics.mBytesAllocatedByFrontend -= entry->GetTotalSize();
Instance.mStatistics.mBytesAllocatedByFrontend.mSize -= entry->GetTotalSize();
Instance.mStatistics.mEntries -= 1;
#endif

Expand Down Expand Up @@ -373,12 +373,12 @@ namespace Langulus::Fractalloc

// Finally, check all other size pool chains
// (pointer could be a member of differently sized type)
for (Size i = 0; i < sizebucket; ++i) {
for (Offset i = 0; i < sizebucket; ++i) {
result = Instance.FindInChain(memory, Instance.mSizePoolChain[i]);
if (result)
return result;
}
for (Size i = sizebucket + 1; i < SizeBuckets; ++i) {
for (Offset i = sizebucket + 1; i < SizeBuckets; ++i) {
result = Instance.FindInChain(memory, Instance.mSizePoolChain[i]);
if (result)
return result;
Expand Down Expand Up @@ -490,11 +490,11 @@ namespace Langulus::Fractalloc

// Finally, check all other size pool chains
// (pointer could be a member of differently sized type)
for (Size i = 0; i < sizebucket; ++i) {
for (Offset i = 0; i < sizebucket; ++i) {
if (Instance.ContainedInChain(memory, Instance.mSizePoolChain[i]))
return true;
}
for (Size i = sizebucket + 1; i < SizeBuckets; ++i) {
for (Offset i = sizebucket + 1; i < SizeBuckets; ++i) {
if (Instance.ContainedInChain(memory, Instance.mSizePoolChain[i]))
return true;
}
Expand Down Expand Up @@ -623,7 +623,7 @@ namespace Langulus::Fractalloc
Logger::Info(ecounter, "] ", Logger::Green, entry->mAllocatedBytes, " bytes, ");
Logger::Append(entry->mReferences, " references: `");
auto raw = entry->GetBlockStart();
for (Size i = 0; i < ::std::min(Size {32}, entry->mAllocatedBytes); ++i) {
for (Offset i = 0; i < ::std::min(Size {32}, entry->mAllocatedBytes); ++i) {
if (::isprint(raw[i].mValue))
Logger::Append(static_cast<char>(raw[i].mValue));
else
Expand Down Expand Up @@ -659,7 +659,7 @@ namespace Langulus::Fractalloc
}

// Dump every size pool chain
for (Size size = 0; size < sizeof(Size) * 8; ++size) {
for (Offset size = 0; size < sizeof(Size) * 8; ++size) {
if (not Instance.mSizePoolChain[size])
continue;

Expand Down Expand Up @@ -706,16 +706,16 @@ namespace Langulus::Fractalloc
/// Account for a newly allocated pool
/// @param pool - the pool to account for
void Allocator::Statistics::AddPool(const Pool* pool) noexcept {
mBytesAllocatedByBackend += pool->GetTotalSize();
mBytesAllocatedByFrontend += pool->GetAllocatedByFrontend();
mBytesAllocatedByBackend.mSize += pool->GetTotalSize();
mBytesAllocatedByFrontend.mSize += pool->GetAllocatedByFrontend();
++mPools;
++mEntries;
}

/// Account for a removed pool
/// @param pool - the pool to account for
void Allocator::Statistics::DelPool(const Pool* pool) noexcept {
mBytesAllocatedByBackend -= pool->GetTotalSize();
mBytesAllocatedByBackend.mSize -= pool->GetTotalSize();
--mPools;
}

Expand Down
18 changes: 9 additions & 9 deletions source/Pool.inl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace Langulus::Fractalloc
, mThreshold {size}
, mThresholdPrevious {size}
, mThresholdMin {Roof2cexpr(meta
? meta->mAllocationPage
: Allocation::GetMinAllocation())}
? meta->mAllocationPage.mSize
: Allocation::GetMinAllocation().mSize)}
, mMeta {meta}
, mHandle {memory} {
mMemory = GetPoolStart<Byte>();
Expand Down Expand Up @@ -88,7 +88,7 @@ namespace Langulus::Fractalloc
/// @return the number of bytes to allocate for use in the pool
LANGULUS(INLINED)
constexpr Size Pool::GetNewAllocationSize(const Size& size) noexcept {
constexpr Size minimum {Pool::DefaultPoolSize + Pool::GetSize()};
constexpr auto minimum = Pool::DefaultPoolSize + Pool::GetSize();
return ::std::max(size + Pool::GetSize(), minimum);
}

Expand Down Expand Up @@ -130,7 +130,7 @@ namespace Langulus::Fractalloc
constexpr Offset one {1};

// Check if we can add a new entry
const auto bytesWithPadding = Allocation::GetNewAllocationSize(bytes);
const Offset bytesWithPadding = Allocation::GetNewAllocationSize(bytes);
if (not CanContain(bytesWithPadding)) UNLIKELY()
return nullptr;

Expand All @@ -154,7 +154,7 @@ namespace Langulus::Fractalloc
if (mNextEntry >= mMemoryEnd) UNLIKELY() {
// Reset carriage and shift level when it goes beyond
mThresholdPrevious = mThreshold;
mThreshold >>= one;
mThreshold.mSize >>= one;
mNextEntry = mMemory + mThreshold;
}
}
Expand All @@ -170,7 +170,7 @@ namespace Langulus::Fractalloc
LANGULUS_ASSUME(DevAssumes,
mAllocatedByFrontend + bytesWithPadding >= mAllocatedByFrontend,
"Frontend byte counter overflow");
mAllocatedByFrontend += bytesWithPadding;
mAllocatedByFrontend.mSize += bytesWithPadding;
return newEntry;
}

Expand All @@ -185,7 +185,7 @@ namespace Langulus::Fractalloc
LANGULUS_ASSUME(DevAssumes, mAllocatedByFrontend >= entry->GetTotalSize(),
"Bad frontend allocation size");

mAllocatedByFrontend -= entry->GetTotalSize();
mAllocatedByFrontend.mSize -= entry->GetTotalSize();
entry->mReferences = 0;

if (0 == mAllocatedByFrontend) {
Expand Down Expand Up @@ -233,15 +233,15 @@ namespace Langulus::Fractalloc
// traverse abd stitch here?
}

mAllocatedByFrontend += addition;
mAllocatedByFrontend.mSize += addition;
}
else {
// We're shrinking the entry
// No checks required
const auto removal = entry->mAllocatedBytes - bytes;
LANGULUS_ASSUME(DevAssumes, mAllocatedByFrontend >= removal,
"Bad frontend allocation size");
mAllocatedByFrontend -= removal;
mAllocatedByFrontend.mSize -= removal;

//TODO: keep track of size distrubution,
// shrink min threshold if all leading buckets go empty
Expand Down

0 comments on commit 82a6d6e

Please sign in to comment.