24#ifndef MEMORY_ALIGNED_ATOMIC_H
25#define MEMORY_ALIGNED_ATOMIC_H
34#include <sys/sysctl.h>
38#elif defined(__linux__)
53 size_t sizeof_line_size =
sizeof(line_size);
54 sysctlbyname(
"hw.cachelinesize", &line_size, &sizeof_line_size,
nullptr, 0);
63 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *
buffer =
nullptr;
65 GetLogicalProcessorInformation(
nullptr, &
buffer_size);
69 for (i = 0; i !=
buffer_size /
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
72 line_size =
buffer[i].Cache.LineSize;
81#elif defined(__GLIBC__)
83 long size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
84 if (
size == -1)
return 64;
89 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size",
"r");
91 if (fscanf(
p,
"%ld", &
size) != 1)
size = 0;
96 if (
size > 0)
return static_cast<size_t>(
size);
119 size_t size{
static_cast<size_t>(std::ceil(
static_cast<double>(
sizeof(T)) /
120 static_cast<double>(csize))) *
135 static const size_t size{memory::_cacheline_for<T>()};
303 template <
typename Accessor_type>
331 if (this->m_underlying !=
nullptr) {
332 this->m_underlying->~atomic();
335 this->m_storage = rhs.m_storage;
336 this->m_storage_size = rhs.m_storage_size;
337 this->m_underlying = rhs.m_underlying;
338 rhs.m_storage =
nullptr;
339 rhs.m_storage_size = 0;
340 rhs.m_underlying =
nullptr;
345 if (this->m_underlying !=
nullptr) {
346 this->m_underlying->~atomic();
349 this->m_storage =
nullptr;
350 this->m_storage_size = 0;
351 this->m_underlying =
nullptr;
357 if (this->m_underlying !=
nullptr) {
358 this->m_underlying->~atomic();
361 this->m_storage = rhs.m_storage;
362 this->m_storage_size = rhs.m_storage_size;
363 this->m_underlying = rhs.m_underlying;
364 rhs.m_storage =
nullptr;
365 rhs.m_storage_size = 0;
366 rhs.m_underlying =
nullptr;
372 assert(this->m_underlying !=
nullptr);
373 this->m_underlying->store(rhs, std::memory_order_seq_cst);
379 assert(this->m_underlying !=
nullptr);
380 return this->m_underlying->load(std::memory_order_relaxed);
385 return this->m_underlying ==
nullptr;
390 return this->m_underlying !=
nullptr;
395 if (this->m_underlying ==
nullptr)
return false;
396 return this->m_underlying->load(std::memory_order_relaxed) == rhs;
401 return !((*this) == rhs);
406 assert(this->m_underlying !=
nullptr);
407 return this->m_underlying;
412 assert(this->m_underlying !=
nullptr);
413 return this->m_underlying;
418 assert(this->m_underlying !=
nullptr);
419 return *this->m_underlying;
424 assert(this->m_underlying !=
nullptr);
425 return *this->m_underlying;
430 return sizeof(std::atomic<T>);
435 return this->m_storage_size;
Template that may access Aligned_atomic internals.
Definition: aligned_atomic.h:141
Templated class that encapsulates an std::atomic within a byte buffer that is padded to the processor...
Definition: aligned_atomic.h:158
Aligned_atomic< T > & operator=(Aligned_atomic< T > const &rhs)=delete
Aligned_atomic(Aligned_atomic< T > &&rhs)
Definition: aligned_atomic.h:330
size_t allocated_size() const
Definition: aligned_atomic.h:434
const std::atomic< T > * operator->() const
Definition: aligned_atomic.h:411
Aligned_atomic< T > & operator=(Aligned_atomic< T > &&rhs)
Definition: aligned_atomic.h:355
virtual ~Aligned_atomic()
Definition: aligned_atomic.h:344
bool operator!=(std::nullptr_t rhs) const
Inequality operator to determine if the underlying storage memory is initialized.
Definition: aligned_atomic.h:389
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:394
std::atomic< T > * operator->()
Definition: aligned_atomic.h:405
Aligned_atomic(Aligned_atomic< T > const &rhs)=delete
Aligned_atomic< T > & operator=(T rhs)
Definition: aligned_atomic.h:371
size_t size() const
Definition: aligned_atomic.h:429
Aligned_atomic()
Definition: aligned_atomic.h:317
std::atomic< T > & operator*()
Definition: aligned_atomic.h:417
size_t m_storage_size
The size of the byte buffer.
Definition: aligned_atomic.h:308
void * m_storage
The byte buffer to use as underlying storage.
Definition: aligned_atomic.h:310
bool operator==(std::nullptr_t rhs) const
Equality operator to determine if the underlying storage memory is initialized.
Definition: aligned_atomic.h:384
const std::atomic< T > & operator*() const
Definition: aligned_atomic.h:423
Aligned_atomic(T value)
Definition: aligned_atomic.h:324
std::atomic< T > * m_underlying
The pointer to the underlying std::atomic<T> object.
Definition: aligned_atomic.h:312
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:400
const char * p
Definition: ctype-mb.cc:1225
#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:38
void my_aligned_free(void *ptr)
Free allocated memory using my_aligned_malloc function.
Definition: my_aligned_malloc.cc:63
const std::string FILE("FILE")
std::unordered_map< meta::Metadata, Data_extension, meta::Metadata::Hash > Cache
Definition: cache.h:37
Definition: aligned_atomic.h:44
static size_t cache_line_size()
Definition: aligned_atomic.h:103
static size_t _cache_line_size()
Calculates and returns the size of the CPU cache line.
Definition: aligned_atomic.h:101
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:134
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:117
size_t size(const char *const c)
Definition: base64.h:46
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:313
Definition: gcs_xcom_synode.h:64