diff --git a/dbcon/joblist/resourcemanager.h b/dbcon/joblist/resourcemanager.h index 809d44d3a..9bf223e63 100644 --- a/dbcon/joblist/resourcemanager.h +++ b/dbcon/joblist/resourcemanager.h @@ -459,7 +459,7 @@ class ResourceManager template allocators::CountingAllocator getAllocator() { - return allocators::CountingAllocator(totalUmMemLimit); + return allocators::CountingAllocator(&totalUmMemLimit); } private: diff --git a/tests/counting_allocator.cpp b/tests/counting_allocator.cpp index b8e32809e..6fe34155f 100644 --- a/tests/counting_allocator.cpp +++ b/tests/counting_allocator.cpp @@ -47,7 +47,7 @@ class CountingAllocatorTest : public ::testing::Test // Constructor CountingAllocatorTest() - : allocatedMemory(MemoryAllowance), allocator(allocatedMemory, MemoryAllowance / 100) + : allocatedMemory(MemoryAllowance), allocator(&allocatedMemory, MemoryAllowance / 100) { } @@ -79,12 +79,12 @@ TEST_F(CountingAllocatorTest, Deallocation) // Test 3: Allocator equality based on shared counter TEST_F(CountingAllocatorTest, AllocatorEquality) { - CountingAllocator allocator1(allocatedMemory); - CountingAllocator allocator2(allocatedMemory); + CountingAllocator allocator1(&allocatedMemory); + CountingAllocator allocator2(&allocatedMemory); EXPECT_TRUE(allocator1 == allocator2); std::atomic anotherCounter(0); - CountingAllocator allocator3(anotherCounter); + CountingAllocator allocator3(&anotherCounter); EXPECT_FALSE(allocator1 == allocator3); } diff --git a/tests/rowgroup-tests.cpp b/tests/rowgroup-tests.cpp index dd906ee83..e99aa1f72 100644 --- a/tests/rowgroup-tests.cpp +++ b/tests/rowgroup-tests.cpp @@ -360,7 +360,7 @@ class RGDataTest : public ::testing::Test { protected: RGDataTest() - : allocatedMemory(MemoryAllowance), alloc(allocatedMemory, MemoryAllowance / 100) {} + : allocatedMemory(MemoryAllowance), alloc(&allocatedMemory, MemoryAllowance / 100) {} void SetUp() override { rg = setupRG({execplan::CalpontSystemCatalog::VARCHAR, execplan::CalpontSystemCatalog::UDECIMAL, diff --git a/utils/common/countingallocator.h b/utils/common/countingallocator.h index ea23d41ba..8f1d83724 100644 --- a/utils/common/countingallocator.h +++ b/utils/common/countingallocator.h @@ -38,13 +38,13 @@ public: using value_type = T; // Constructor accepting a reference to an atomic counter - explicit CountingAllocator(std::atomic& memoryLimit, const uint64_t lowerBound = MemoryLimitLowerBound) noexcept - : memoryLimitRef_(memoryLimit), memoryLimitLowerBound(lowerBound) {} + explicit CountingAllocator(std::atomic* memoryLimit, const uint64_t lowerBound = MemoryLimitLowerBound) noexcept + : memoryLimit_(memoryLimit), memoryLimitLowerBound(lowerBound) {} // Copy constructor (template to allow conversion between different types) template CountingAllocator(const CountingAllocator& other) noexcept - : memoryLimitRef_(other.memoryLimitRef_) {} + : memoryLimit_(other.memoryLimit_) {} // Allocate memory for n objects of type T @@ -52,15 +52,15 @@ public: typename std::enable_if::value, U*>::type allocate(std::size_t n) { - auto memCounted = memoryLimitRef_.fetch_sub(n * sizeof(T), std::memory_order_relaxed); + auto memCounted = memoryLimit_->fetch_sub(n * sizeof(T), std::memory_order_relaxed); if (memCounted < memoryLimitLowerBound) { - memoryLimitRef_.fetch_add(n * sizeof(T), std::memory_order_relaxed); + memoryLimit_->fetch_add(n * sizeof(T), std::memory_order_relaxed); throw std::bad_alloc(); } T* ptr = static_cast(::operator new(n * sizeof(T))); // std::cout << "[Allocate] " << n * sizeof(T) << " bytes at " << static_cast(ptr) - // << ". current timit: " << std::dec << memoryLimitRef_.load() << std::hex << " bytes.\n"; + // << ". current timit: " << std::dec << memoryLimit_.load() << std::hex << " bytes.\n"; // std::cout << std::dec; return ptr; } @@ -69,15 +69,15 @@ public: typename std::enable_if::value, typename std::remove_extent::type*>::type allocate(std::size_t n) { - auto memCounted = memoryLimitRef_.fetch_sub(n * sizeof(T), std::memory_order_relaxed); + auto memCounted = memoryLimit_->fetch_sub(n * sizeof(T), std::memory_order_relaxed); if (memCounted < memoryLimitLowerBound) { - memoryLimitRef_.fetch_add(n * sizeof(T), std::memory_order_relaxed); + memoryLimit_->fetch_add(n * sizeof(T), std::memory_order_relaxed); throw std::bad_alloc(); } T ptr = static_cast(::operator new[](n)); // std::cout << "[Allocate] " << n * sizeof(T) << " bytes at " << static_cast(ptr) - // << ". current timit: " << std::dec << memoryLimitRef_.load() << std::hex << " bytes.\n"; + // << ". current timit: " << std::dec << memoryLimit_.load() << std::hex << " bytes.\n"; return ptr; } @@ -85,9 +85,9 @@ public: void deallocate(T* ptr, std::size_t n) noexcept { ::operator delete(ptr); - memoryLimitRef_.fetch_add(n * sizeof(T), std::memory_order_relaxed); + memoryLimit_->fetch_add(n * sizeof(T), std::memory_order_relaxed); // std::cout << "[Deallocate] " << n * sizeof(T) << " bytes from " << static_cast(ptr) - // << ". current timit: " << std::dec << memoryLimitRef_.load() << std::hex << " bytes.\n"; + // << ". current timit: " << std::dec << memoryLimit_.load() << std::hex << " bytes.\n"; // std::cout << std::dec; } @@ -95,7 +95,7 @@ public: template bool operator==(const CountingAllocator& other) const noexcept { - return &memoryLimitRef_ == &other.memoryLimitRef_; + return memoryLimit_ == other.memoryLimit_; } template @@ -105,7 +105,7 @@ public: } private: - std::atomic& memoryLimitRef_; + std::atomic* memoryLimit_ = nullptr; int64_t memoryLimitLowerBound = 0; // Grant access to other instances of CountingAllocator with different types