23#ifndef MEMORY_ALIGNED_ATOMIC_H
24#define MEMORY_ALIGNED_ATOMIC_H
33#include <sys/sysctl.h>
37#elif defined(__linux__)
52 size_t sizeof_line_size =
sizeof(line_size);
53 sysctlbyname(
"hw.cachelinesize", &line_size, &sizeof_line_size, 0, 0);
62 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *
buffer = 0;
68 for (i = 0; i !=
buffer_size /
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
71 line_size =
buffer[i].Cache.LineSize;
80#elif defined(__linux__)
82 long size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
83 if (size == -1)
return 64;
88 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size",
"r");
90 fscanf(
p,
"%ld", &size);
95 return static_cast<size_t>(size);
117 size_t size{
static_cast<size_t>(std::ceil(
static_cast<double>(
sizeof(T)) /
118 static_cast<double>(csize))) *
133 static const size_t size{memory::_cacheline_for<T>()};
322 if (this->m_underlying !=
nullptr) {
323 this->m_underlying->~atomic();
326 this->m_storage = rhs.m_storage;
327 this->m_storage_size = rhs.m_storage_size;
328 this->m_underlying = rhs.m_underlying;
329 rhs.m_storage =
nullptr;
330 rhs.m_storage_size = 0;
331 rhs.m_underlying =
nullptr;
336 if (this->m_underlying !=
nullptr) {
337 this->m_underlying->~atomic();
340 this->m_storage =
nullptr;
341 this->m_storage_size = 0;
342 this->m_underlying =
nullptr;
348 if (this->m_underlying !=
nullptr) {
349 this->m_underlying->~atomic();
352 this->m_storage = rhs.m_storage;
353 this->m_storage_size = rhs.m_storage_size;
354 this->m_underlying = rhs.m_underlying;
355 rhs.m_storage =
nullptr;
356 rhs.m_storage_size = 0;
357 rhs.m_underlying =
nullptr;
363 assert(this->m_underlying !=
nullptr);
364 this->m_underlying->store(rhs, std::memory_order_seq_cst);
370 assert(this->m_underlying !=
nullptr);
371 return this->m_underlying->load(std::memory_order_relaxed);
376 return this->m_underlying ==
nullptr;
381 return this->m_underlying !=
nullptr;
386 if (this->m_underlying ==
nullptr)
return false;
387 return this->m_underlying->load(std::memory_order_relaxed) == rhs;
392 return !((*this) == rhs);
397 assert(this->m_underlying !=
nullptr);
398 return this->m_underlying;
403 assert(this->m_underlying !=
nullptr);
404 return this->m_underlying;
409 assert(this->m_underlying !=
nullptr);
410 return *this->m_underlying;
415 assert(this->m_underlying !=
nullptr);
416 return *this->m_underlying;
421 return sizeof(std::atomic<T>);
426 return this->m_storage_size;
Templated class that encapsulates an std::atomic within a byte buffer that is padded to the processor...
Definition: aligned_atomic.h:152
Aligned_atomic< T > & operator=(Aligned_atomic< T > const &rhs)=delete
Aligned_atomic(Aligned_atomic< T > &&rhs)
Definition: aligned_atomic.h:321
size_t allocated_size() const
Definition: aligned_atomic.h:425
const std::atomic< T > * operator->() const
Definition: aligned_atomic.h:402
Aligned_atomic< T > & operator=(Aligned_atomic< T > &&rhs)
Definition: aligned_atomic.h:346
virtual ~Aligned_atomic()
Definition: aligned_atomic.h:335
bool operator!=(std::nullptr_t rhs) const
Inequality operator to determine if the underlying storage memory is initialized.
Definition: aligned_atomic.h:380
bool operator==(T rhs) const
Equality operator for determining if the value stored in the underlying std::atomic equals the passed...
Definition: aligned_atomic.h:385
std::atomic< T > * operator->()
Definition: aligned_atomic.h:396
Aligned_atomic(Aligned_atomic< T > const &rhs)=delete
Aligned_atomic< T > & operator=(T rhs)
Definition: aligned_atomic.h:362
size_t size() const
Definition: aligned_atomic.h:420
Aligned_atomic()
Definition: aligned_atomic.h:308
std::atomic< T > & operator*()
Definition: aligned_atomic.h:408
size_t m_storage_size
The size of the byte buffer.
Definition: aligned_atomic.h:299
void * m_storage
The byte buffer to use as underlying storage.
Definition: aligned_atomic.h:301
bool operator==(std::nullptr_t rhs) const
Equality operator to determine if the underlying storage memory is initialized.
Definition: aligned_atomic.h:375
const std::atomic< T > & operator*() const
Definition: aligned_atomic.h:414
Aligned_atomic(T value)
Definition: aligned_atomic.h:315
std::atomic< T > * m_underlying
The pointer to the underlying std::atomic<T> object.
Definition: aligned_atomic.h:303
bool operator!=(T rhs) const
Inequality operator for determining if the value stored in the underlying std::atomic differs from th...
Definition: aligned_atomic.h:391
const char * p
Definition: ctype-mb.cc:1236
#define malloc(A)
Definition: lexyy.cc:914
#define free(A)
Definition: lexyy.cc:915
void * my_aligned_malloc(size_t size, size_t alignment)
Function allocates size bytes and returns a pointer to the allocated memory.
Definition: my_aligned_malloc.cc:37
void my_aligned_free(void *ptr)
Free allocated memory using my_aligned_malloc function.
Definition: my_aligned_malloc.cc:62
const std::string FILE("FILE")
std::unordered_map< meta::Metadata, Data_extension, meta::Metadata::Hash > Cache
Definition: cache.h:37
Definition: aligned_atomic.h:43
static size_t cache_line_size()
Definition: aligned_atomic.h:101
static size_t _cache_line_size()
Calculates and returns the size of the CPU cache line.
Definition: aligned_atomic.h:99
static size_t minimum_cacheline_for()
Retrieves the amount of bytes, multiple of the current cacheline size, needed to store an element of ...
Definition: aligned_atomic.h:132
static size_t _cacheline_for()
Retrieves the amount of bytes, multiple of the current cacheline size, needed to store an element of ...
Definition: aligned_atomic.h:115
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:419
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:314
Definition: varlen_sort.h:183