Audacity 3.2.0
MemoryX.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 MemoryX.cpp
6
7 Paul Licameli
8
9 **********************************************************************/
10
11#include <cstdint>
12#include "MemoryX.h"
13
14// Make the symbol table non-empty
15UTILITY_API void lib_utility_dummy_symbol()
16{}
17
18constexpr auto sizeof_align_val = sizeof(std::align_val_t);
19
20void *NonInterferingBase::operator new(std::size_t count, std::align_val_t al)
21{
22 using namespace std;
23 // Get an allocation with sufficient extra space to remember the alignment
24 // (And to do that, adjust the alignment to be not less than the alignment of
25 // an alignment value!).
26 // Also increase the allocation by one entire alignment.
27 al = max( al, static_cast<align_val_t>( alignof(align_val_t) ) );
28 const auto al_as_size = static_cast<size_t>(al);
29 auto ptr = static_cast<char*>(
30 ::operator new( count + sizeof_align_val + al_as_size ) );
31
32 // Adjust the pointer to a properly aligned one, with a space just before it
33 // to remember the adjustment
34 ptr += sizeof_align_val;
35 auto integer = reinterpret_cast<uintptr_t>(ptr);
36 const auto partial = integer % al_as_size;
37 auto adjustment = partial ? al_as_size - partial : 0;
38 integer += adjustment;
39 ptr = reinterpret_cast<char*>(integer);
40
41 // Remember the adjustment
42 *(reinterpret_cast<size_t*>(ptr) - 1) = adjustment;
43
44 return ptr;
45}
46
47void NonInterferingBase::operator delete(void *ptr, std::align_val_t al)
48{
49 // Find the adjustment
50 auto adjustment = *(reinterpret_cast<size_t*>(ptr) - 1);
51 // Apply the adjustment
52 auto p = reinterpret_cast<char*>(ptr) - adjustment - sizeof_align_val;
53 // Call through to default operator
54 ::operator delete(p);
55}
UTILITY_API void lib_utility_dummy_symbol()
Definition: MemoryX.cpp:15
constexpr auto sizeof_align_val
Definition: MemoryX.cpp:18
STL namespace.