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