Beatmup
Beatmup::AlignedMemory Class Reference

Aligned memory buffer. More...

#include <memory.h>

Public Member Functions

 AlignedMemory ()
 
 AlignedMemory (size_t size, size_t align=DEFAULT_ALIGNMENT)
 
 AlignedMemory (size_t size, int value, size_t align=DEFAULT_ALIGNMENT)
 
 ~AlignedMemory ()
 
AlignedMemoryoperator= (AlignedMemory &&)
 
void * operator() ()
 
const void * operator() () const
 
template<typename datatype >
datatype * ptr (int offset=0)
 
template<typename datatype >
const datatype * ptr (int offset=0) const
 
template<typename datatype >
datatype & at (int offset)
 
template<typename datatype >
const datatype & at (int offset) const
 
unsigned char operator[] (int offset) const
 
 operator bool () const
 
void free ()
 Frees the allocated memory. More...
 

Static Public Member Functions

static uint64_t available ()
 Returns the size of available (free) operating memory in bytes. More...
 
static uint64_t total ()
 Returns the size of total operating memory in bytes. More...
 

Static Public Attributes

static const size_t DEFAULT_ALIGNMENT = 8
 default number of bytes to align the address More...
 

Private Member Functions

 AlignedMemory (const AlignedMemory &)=delete
 

Private Attributes

void * rawAddr
 
void * alignAddr
 

Detailed Description

Aligned memory buffer.

Definition at line 27 of file memory.h.

Constructor & Destructor Documentation

◆ AlignedMemory() [1/4]

Beatmup::AlignedMemory::AlignedMemory ( const AlignedMemory )
privatedelete

◆ AlignedMemory() [2/4]

Beatmup::AlignedMemory::AlignedMemory ( )
inline

Definition at line 35 of file memory.h.

35 : rawAddr(nullptr), alignAddr(nullptr) {}

◆ AlignedMemory() [3/4]

AlignedMemory::AlignedMemory ( size_t  size,
size_t  align = DEFAULT_ALIGNMENT 
)

Definition at line 70 of file memory.cpp.

70  {
71 #ifdef BEATMUP_DEBUG
72  DebugAssertion::check(size > 0, "Trying to allocate zero memory");
73  BEATMUP_DEBUG_I("Allocating %lu Kbytes (%lu MB free)...", (unsigned long)(size / 1024), (unsigned long)(available() / 1048576));
74 #endif
75  rawAddr = malloc(size + align);
76  alignAddr = reinterpret_cast<void*>(
77  reinterpret_cast<std::uintptr_t>(rawAddr) + (align - reinterpret_cast<std::uintptr_t>(rawAddr) % align)
78  );
79 }
static uint64_t available()
Returns the size of available (free) operating memory in bytes.
Definition: memory.cpp:43
#define BEATMUP_DEBUG_I(...)
Definition: debug.h:33
jlong jobject size

◆ AlignedMemory() [4/4]

AlignedMemory::AlignedMemory ( size_t  size,
int  value,
size_t  align = DEFAULT_ALIGNMENT 
)

Definition at line 82 of file memory.cpp.

82  : AlignedMemory(size, align) {
83  memset(alignAddr, value, size);
84 }

◆ ~AlignedMemory()

AlignedMemory::~AlignedMemory ( )

Definition at line 87 of file memory.cpp.

87  {
88  if (rawAddr)
89  std::free(rawAddr);
90 }

Member Function Documentation

◆ operator=()

AlignedMemory & AlignedMemory::operator= ( AlignedMemory &&  other)

Definition at line 93 of file memory.cpp.

93  {
94  if (rawAddr)
95  std::free(rawAddr);
96  rawAddr = other.rawAddr;
97  alignAddr = other.alignAddr;
98  other.rawAddr = nullptr;
99  other.alignAddr = nullptr;
100  return *this;
101 }

◆ operator()() [1/2]

void* Beatmup::AlignedMemory::operator() ( )
inline

Definition at line 42 of file memory.h.

42 { return alignAddr; }

◆ operator()() [2/2]

const void* Beatmup::AlignedMemory::operator() ( ) const
inline

Definition at line 44 of file memory.h.

44 { return alignAddr; }

◆ ptr() [1/2]

template<typename datatype >
datatype* Beatmup::AlignedMemory::ptr ( int  offset = 0)
inline

Definition at line 46 of file memory.h.

46  {
47  return static_cast<datatype*>(alignAddr) + offset;
48  }

◆ ptr() [2/2]

template<typename datatype >
const datatype* Beatmup::AlignedMemory::ptr ( int  offset = 0) const
inline

Definition at line 50 of file memory.h.

50  {
51  return static_cast<const datatype*>(alignAddr) + offset;
52  }

◆ at() [1/2]

template<typename datatype >
datatype& Beatmup::AlignedMemory::at ( int  offset)
inline

Definition at line 54 of file memory.h.

54  {
55  return *(static_cast<datatype*>(alignAddr) + offset);
56  }

◆ at() [2/2]

template<typename datatype >
const datatype& Beatmup::AlignedMemory::at ( int  offset) const
inline

Definition at line 58 of file memory.h.

58  {
59  return *(static_cast<datatype*>(alignAddr) + offset);
60  }

◆ operator[]()

unsigned char Beatmup::AlignedMemory::operator[] ( int  offset) const
inline

Definition at line 62 of file memory.h.

62 { return at<unsigned char>(offset); }

◆ operator bool()

Beatmup::AlignedMemory::operator bool ( ) const
inline

Definition at line 64 of file memory.h.

64 { return rawAddr != nullptr; }

◆ free()

void AlignedMemory::free ( )

Frees the allocated memory.

Definition at line 104 of file memory.cpp.

104  {
105  if (rawAddr) {
106  std::free(rawAddr);
107  rawAddr = nullptr;
108  alignAddr = nullptr;
109  }
110 }

◆ available()

uint64_t AlignedMemory::available ( )
static

Returns the size of available (free) operating memory in bytes.

Definition at line 43 of file memory.cpp.

43  {
44 #if BEATMUP_PLATFORM_WINDOWS
45  MEMORYSTATUSEX status;
46  status.dwLength = sizeof(status);
47  GlobalMemoryStatusEx(&status);
48  return status.ullAvailPhys;
49 #else
50  struct sysinfo info;
51  sysinfo(&info);
52  return info.freeram * info.mem_unit;
53 #endif
54 }

◆ total()

uint64_t AlignedMemory::total ( )
static

Returns the size of total operating memory in bytes.

Definition at line 56 of file memory.cpp.

56  {
57 #if BEATMUP_PLATFORM_WINDOWS
58  MEMORYSTATUSEX status;
59  status.dwLength = sizeof(status);
60  GlobalMemoryStatusEx(&status);
61  return status.ullTotalPhys;
62 #else
63  struct sysinfo info;
64  sysinfo(&info);
65  return info.totalram * info.mem_unit;
66 #endif
67 }

Member Data Documentation

◆ rawAddr

void* Beatmup::AlignedMemory::rawAddr
private

Definition at line 29 of file memory.h.

◆ alignAddr

void* Beatmup::AlignedMemory::alignAddr
private

Definition at line 30 of file memory.h.

◆ DEFAULT_ALIGNMENT

const size_t AlignedMemory::DEFAULT_ALIGNMENT = 8
static

default number of bytes to align the address

Definition at line 33 of file memory.h.


The documentation for this class was generated from the following files: