Audacity 3.2.0
Classes | Macros | Typedefs | Functions
MemoryX.h File Reference
#include <memory>
#include <new>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <limits>
#include <cstdint>
#include <algorithm>
Include dependency graph for MemoryX.h:

Go to the source code of this file.

Classes

class  ArrayOf< X >
 This simplifies arrays of arrays, each array separately allocated with NEW[] But it might be better to use std::Array<ArrayOf<X>, N> for some small constant N Or use just one array when sub-arrays have a common size and are not large. More...
 
class  ArraysOf< X >
 
struct  freer
 
struct  Destroyer< T >
 A deleter class to supply the second template parameter of unique_ptr for classes like wxWindow that should be sent a message called Destroy rather than be deleted directly. More...
 
struct  Finally< F >
 "finally" as in The C++ Programming Language, 4th ed., p. 358 Useful for defining ad-hoc RAII actions. typical usage: auto cleanup = finally([&]{ ... code; ... }); More...
 
struct  RestoreValue< T >
 Structure used by ValueRestorer. More...
 
class  ValueRestorer< T >
 Set a variable temporarily in a scope. More...
 
struct  CopyableValueRestorer< T >
 
struct  SharedNonInterfering< T >
 Workaround for std::make_shared not working on macOs with over-alignment. More...
 
struct  NonInterfering< T >
 
struct  AtomicUniquePointer< T >
 

Macros

#define safenew   new
 
#define QUANTIZED_TIME(time, rate)   (floor(((double)(time) * (rate)) + 0.5) / (rate))
 
#define DB_TO_LINEAR(x)   (pow(10.0, (x) / 20.0))
 
#define LINEAR_TO_DB(x)   (20.0 * log10(x))
 
#define MAX_AUDIO   (1. - 1./(1<<15))
 

Typedefs

template<typename T >
using MallocPtr = std::unique_ptr< T, freer >
 
template<typename Character = char>
using MallocString = std::unique_ptr< Character[], freer >
 
template<typename T >
using Destroy_ptr = std::unique_ptr< T, Destroyer< T > >
 a convenience for using Destroyer More...
 

Functions

template<typename F >
Finally< F > finally (F f)
 Function template with type deduction lets you construct Finally without typing any angle brackets. More...
 
template<typename F >
 Finally (F) -> Finally< F >
 
template<typename T >
ValueRestorer< T > valueRestorer (T &var)
 inline functions provide convenient parameter type deduction More...
 
template<typename T >
ValueRestorer< T > valueRestorer (T &var, const T &newValue)
 
struct UTILITY_API alignas (64) NonInterferingBase
 Non-template helper for class template NonInterfering. More...
 
bool IsLittleEndian () noexcept
 Check that machine is little-endian. More...
 
template<typename IntType >
constexpr IntType SwapIntBytes (IntType value) noexcept
 Swap bytes in an integer. More...
 

Macro Definition Documentation

◆ DB_TO_LINEAR

#define DB_TO_LINEAR (   x)    (pow(10.0, (x) / 20.0))

Definition at line 338 of file MemoryX.h.

◆ LINEAR_TO_DB

#define LINEAR_TO_DB (   x)    (20.0 * log10(x))

Definition at line 339 of file MemoryX.h.

◆ MAX_AUDIO

#define MAX_AUDIO   (1. - 1./(1<<15))

Definition at line 341 of file MemoryX.h.

◆ QUANTIZED_TIME

#define QUANTIZED_TIME (   time,
  rate 
)    (floor(((double)(time) * (rate)) + 0.5) / (rate))

Definition at line 336 of file MemoryX.h.

◆ safenew

#define safenew   new

Definition at line 10 of file MemoryX.h.

Typedef Documentation

◆ Destroy_ptr

template<typename T >
using Destroy_ptr = std::unique_ptr<T, Destroyer<T> >

a convenience for using Destroyer

Definition at line 164 of file MemoryX.h.

◆ MallocPtr

template<typename T >
using MallocPtr = std::unique_ptr< T, freer >

A useful alias for holding the result of malloc

Definition at line 142 of file MemoryX.h.

◆ MallocString

template<typename Character = char>
using MallocString = std::unique_ptr< Character[], freer >

A useful alias for holding the result of strup and similar

Definition at line 148 of file MemoryX.h.

Function Documentation

◆ alignas()

struct UTILITY_API alignas ( 64  )

Non-template helper for class template NonInterfering.

If a structure contains any members with large alignment, this base class may also allow it to work in macOS builds under current limitations of the C++17 standard implementation.

Definition at line 265 of file MemoryX.h.

275 {
276 static void *operator new(std::size_t count, std::align_val_t al);
277 static void operator delete(void *ptr, std::align_val_t al);
278
279#if defined (_MSC_VER) && defined(_DEBUG)
280 // Versions that work in the presence of the DEBUG_NEW macro.
281 // Ignore the arguments supplied by the macro and forward to the
282 // other overloads.
283 static void *operator new(
284 std::size_t count, std::align_val_t al, int, const char *, int)
285 { return operator new(count, al); }
286 static void operator delete(
287 void *ptr, std::align_val_t al, int, const char *, int)
288 { return operator delete(ptr, al); }
289#endif
290};

◆ finally()

template<typename F >
Finally< F > finally ( f)

Function template with type deduction lets you construct Finally without typing any angle brackets.

Definition at line 184 of file MemoryX.h.

185{
186 return Finally<F>(f);
187}
"finally" as in The C++ Programming Language, 4th ed., p. 358 Useful for defining ad-hoc RAII actions...
Definition: MemoryX.h:175

◆ Finally()

template<typename F >
Finally ( ) -> Finally< F >

C++17 deduction guide allows even simpler syntax: Finally Do{[&]{ Stuff(); }};

Don't omit Do or some other variable name! Otherwise, the execution of the body is immediate, not delayed to the end of the enclosing scope.

◆ IsLittleEndian()

bool IsLittleEndian ( )
inlinenoexcept

Check that machine is little-endian.

Definition at line 368 of file MemoryX.h.

369{
370 const std::uint32_t x = 1u;
371 return static_cast<const unsigned char*>(static_cast<const void*>(&x))[0];
372 // We will assume the same for other widths!
373}

Referenced by audacity::cloud::audiocom::sync::RemoteProjectSnapshot::OnProjectBlobDownloaded(), audacity::cloud::audiocom::sync::ProjectCloudExtension::OnUpdateSaved(), and OpusExportProcessor::OggPacket::Write().

Here is the caller graph for this function:

◆ SwapIntBytes()

template<typename IntType >
constexpr IntType SwapIntBytes ( IntType  value)
constexprnoexcept

Swap bytes in an integer.

Definition at line 377 of file MemoryX.h.

378{
379 static_assert(std::is_integral<IntType>::value, "Integral type required");
380
381 constexpr auto size = sizeof(IntType);
382
383 static_assert(
384 size == 1 || size == 2 || size == 4 || size == 8, "Unsupported size");
385
386 if constexpr (size == 1)
387 return value;
388 else if constexpr (size == 2)
389 return (value >> 8) | (value << 8);
390 else if constexpr (size == 4) // On x86, this (and 64 bit version) is a single instruction! (At least, clang is smart enough to do that)
391 return ((value >> 24) & 0xFF) | ((value >> 8) & 0xFF00) |
392 ((value << 8) & 0xFF0000) | ((value << 24) & 0xFF000000);
393 else if constexpr (size == 8)
394 return ((value >> 56) & 0xFF) | ((value >> 40) & 0xFF00) |
395 ((value >> 24) & 0xFF0000) | ((value >> 8) & 0xFF000000) |
396 ((value << 8) & 0xFF00000000) | ((value << 24) & 0xFF0000000000) |
397 ((value << 40) & 0xFF000000000000) |
398 ((value << 56) & 0xFF00000000000000);
399
400 // Unreachable
401 return value;
402}

References size.

Referenced by audacity::cloud::audiocom::sync::RemoteProjectSnapshot::OnProjectBlobDownloaded(), audacity::cloud::audiocom::sync::ProjectCloudExtension::OnUpdateSaved(), and OpusExportProcessor::OggPacket::Write().

Here is the caller graph for this function:

◆ valueRestorer() [1/2]

template<typename T >
ValueRestorer< T > valueRestorer ( T &  var)

inline functions provide convenient parameter type deduction

Definition at line 253 of file MemoryX.h.

254{ return ValueRestorer< T >{ var }; }
Set a variable temporarily in a scope.
Definition: MemoryX.h:214

Referenced by MacroCommands::ApplyMacro(), AudacityCommand::DoAudacityCommand(), EffectBase::DoEffect(), EffectPreview(), MenuCreator::FilterKeyEvent(), Importer::Import(), EffectNoiseReduction::Dialog::OnPreview(), NyquistBase::Process(), VSTInstance::RealtimeInitialize(), and AudacityCommand::ShowInterface().

Here is the caller graph for this function:

◆ valueRestorer() [2/2]

template<typename T >
ValueRestorer< T > valueRestorer ( T &  var,
const T &  newValue 
)

Definition at line 257 of file MemoryX.h.

258{ return ValueRestorer< T >{ var, newValue }; }