1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-30 19:23:07 +03:00

feat(): change ref to atomic with ptr to atomic

This commit is contained in:
drrtuy
2024-12-08 22:23:33 +00:00
parent 71ed9cabe0
commit 397b3ff729
4 changed files with 19 additions and 19 deletions

View File

@ -459,7 +459,7 @@ class ResourceManager
template<typename T> template<typename T>
allocators::CountingAllocator<T> getAllocator() allocators::CountingAllocator<T> getAllocator()
{ {
return allocators::CountingAllocator<T>(totalUmMemLimit); return allocators::CountingAllocator<T>(&totalUmMemLimit);
} }
private: private:

View File

@ -47,7 +47,7 @@ class CountingAllocatorTest : public ::testing::Test
// Constructor // Constructor
CountingAllocatorTest() 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 3: Allocator equality based on shared counter
TEST_F(CountingAllocatorTest, AllocatorEquality) TEST_F(CountingAllocatorTest, AllocatorEquality)
{ {
CountingAllocator<TestClass> allocator1(allocatedMemory); CountingAllocator<TestClass> allocator1(&allocatedMemory);
CountingAllocator<TestClass> allocator2(allocatedMemory); CountingAllocator<TestClass> allocator2(&allocatedMemory);
EXPECT_TRUE(allocator1 == allocator2); EXPECT_TRUE(allocator1 == allocator2);
std::atomic<int64_t> anotherCounter(0); std::atomic<int64_t> anotherCounter(0);
CountingAllocator<TestClass> allocator3(anotherCounter); CountingAllocator<TestClass> allocator3(&anotherCounter);
EXPECT_FALSE(allocator1 == allocator3); EXPECT_FALSE(allocator1 == allocator3);
} }

View File

@ -360,7 +360,7 @@ class RGDataTest : public ::testing::Test
{ {
protected: protected:
RGDataTest() RGDataTest()
: allocatedMemory(MemoryAllowance), alloc(allocatedMemory, MemoryAllowance / 100) {} : allocatedMemory(MemoryAllowance), alloc(&allocatedMemory, MemoryAllowance / 100) {}
void SetUp() override void SetUp() override
{ {
rg = setupRG({execplan::CalpontSystemCatalog::VARCHAR, execplan::CalpontSystemCatalog::UDECIMAL, rg = setupRG({execplan::CalpontSystemCatalog::VARCHAR, execplan::CalpontSystemCatalog::UDECIMAL,

View File

@ -38,13 +38,13 @@ public:
using value_type = T; using value_type = T;
// Constructor accepting a reference to an atomic counter // Constructor accepting a reference to an atomic counter
explicit CountingAllocator(std::atomic<int64_t>& memoryLimit, const uint64_t lowerBound = MemoryLimitLowerBound) noexcept explicit CountingAllocator(std::atomic<int64_t>* memoryLimit, const uint64_t lowerBound = MemoryLimitLowerBound) noexcept
: memoryLimitRef_(memoryLimit), memoryLimitLowerBound(lowerBound) {} : memoryLimit_(memoryLimit), memoryLimitLowerBound(lowerBound) {}
// Copy constructor (template to allow conversion between different types) // Copy constructor (template to allow conversion between different types)
template <typename U> template <typename U>
CountingAllocator(const CountingAllocator<U>& other) noexcept CountingAllocator(const CountingAllocator<U>& other) noexcept
: memoryLimitRef_(other.memoryLimitRef_) {} : memoryLimit_(other.memoryLimit_) {}
// Allocate memory for n objects of type T // Allocate memory for n objects of type T
@ -52,15 +52,15 @@ public:
typename std::enable_if<!std::is_array<U>::value, U*>::type typename std::enable_if<!std::is_array<U>::value, U*>::type
allocate(std::size_t n) 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) { 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(); throw std::bad_alloc();
} }
T* ptr = static_cast<T*>(::operator new(n * sizeof(T))); T* ptr = static_cast<T*>(::operator new(n * sizeof(T)));
// std::cout << "[Allocate] " << n * sizeof(T) << " bytes at " << static_cast<void*>(ptr) // std::cout << "[Allocate] " << n * sizeof(T) << " bytes at " << static_cast<void*>(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; // std::cout << std::dec;
return ptr; return ptr;
} }
@ -69,15 +69,15 @@ public:
typename std::enable_if<std::is_array<U>::value, typename std::remove_extent<U>::type*>::type typename std::enable_if<std::is_array<U>::value, typename std::remove_extent<U>::type*>::type
allocate(std::size_t n) 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) { 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(); throw std::bad_alloc();
} }
T ptr = static_cast<T>(::operator new[](n)); T ptr = static_cast<T>(::operator new[](n));
// std::cout << "[Allocate] " << n * sizeof(T) << " bytes at " << static_cast<void*>(ptr) // std::cout << "[Allocate] " << n * sizeof(T) << " bytes at " << static_cast<void*>(ptr)
// << ". current timit: " << std::dec << memoryLimitRef_.load() << std::hex << " bytes.\n"; // << ". current timit: " << std::dec << memoryLimit_.load() << std::hex << " bytes.\n";
return ptr; return ptr;
} }
@ -85,9 +85,9 @@ public:
void deallocate(T* ptr, std::size_t n) noexcept void deallocate(T* ptr, std::size_t n) noexcept
{ {
::operator delete(ptr); ::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<void*>(ptr) // std::cout << "[Deallocate] " << n * sizeof(T) << " bytes from " << static_cast<void*>(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; // std::cout << std::dec;
} }
@ -95,7 +95,7 @@ public:
template <typename U> template <typename U>
bool operator==(const CountingAllocator<U>& other) const noexcept bool operator==(const CountingAllocator<U>& other) const noexcept
{ {
return &memoryLimitRef_ == &other.memoryLimitRef_; return memoryLimit_ == other.memoryLimit_;
} }
template <typename U> template <typename U>
@ -105,7 +105,7 @@ public:
} }
private: private:
std::atomic<int64_t>& memoryLimitRef_; std::atomic<int64_t>* memoryLimit_ = nullptr;
int64_t memoryLimitLowerBound = 0; int64_t memoryLimitLowerBound = 0;
// Grant access to other instances of CountingAllocator with different types // Grant access to other instances of CountingAllocator with different types