Beatmup
memory.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <cstddef>
21 #include <cstdint>
22 
23 namespace Beatmup {
24  /**
25  Aligned memory buffer
26  */
27  class AlignedMemory {
28  private:
29  void* rawAddr;
30  void* alignAddr;
31  AlignedMemory(const AlignedMemory&) = delete;
32  public:
33  static const size_t DEFAULT_ALIGNMENT; //!< default number of bytes to align the address
34 
35  AlignedMemory() : rawAddr(nullptr), alignAddr(nullptr) {}
36  AlignedMemory(size_t size, size_t align = DEFAULT_ALIGNMENT);
37  AlignedMemory(size_t size, int value, size_t align = DEFAULT_ALIGNMENT);
39 
41 
42  inline void* operator()() { return alignAddr; }
43 
44  inline const void* operator()() const { return alignAddr; }
45 
46  template<typename datatype> inline datatype* ptr(int offset = 0) {
47  return static_cast<datatype*>(alignAddr) + offset;
48  }
49 
50  template<typename datatype> inline const datatype* ptr(int offset = 0) const {
51  return static_cast<const datatype*>(alignAddr) + offset;
52  }
53 
54  template<typename datatype> inline datatype& at(int offset) {
55  return *(static_cast<datatype*>(alignAddr) + offset);
56  }
57 
58  template<typename datatype> inline const datatype& at(int offset) const {
59  return *(static_cast<datatype*>(alignAddr) + offset);
60  }
61 
62  inline unsigned char operator[](int offset) const { return at<unsigned char>(offset); }
63 
64  inline operator bool() const { return rawAddr != nullptr; }
65 
66  /**
67  Frees the allocated memory.
68  */
69  void free();
70 
71  /**
72  Returns the size of available (free) operating memory in bytes
73  */
74  static uint64_t available();
75 
76  /**
77  Returns the size of total operating memory in bytes
78  */
79  static uint64_t total();
80  };
81 }
Aligned memory buffer.
Definition: memory.h:27
void free()
Frees the allocated memory.
Definition: memory.cpp:104
const void * operator()() const
Definition: memory.h:44
unsigned char operator[](int offset) const
Definition: memory.h:62
datatype * ptr(int offset=0)
Definition: memory.h:46
static uint64_t available()
Returns the size of available (free) operating memory in bytes.
Definition: memory.cpp:43
AlignedMemory & operator=(AlignedMemory &&)
Definition: memory.cpp:93
const datatype & at(int offset) const
Definition: memory.h:58
datatype & at(int offset)
Definition: memory.h:54
static const size_t DEFAULT_ALIGNMENT
default number of bytes to align the address
Definition: memory.h:33
void * operator()()
Definition: memory.h:42
const datatype * ptr(int offset=0) const
Definition: memory.h:50
static uint64_t total()
Returns the size of total operating memory in bytes.
Definition: memory.cpp:56
AlignedMemory(const AlignedMemory &)=delete
jlong jobject size