Beatmup
memory.cpp
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 #include "memory.h"
20 #include "exception.h"
21 #include "debug.h"
22 #include <cstdlib>
23 #include <cstdint>
24 #include <cstring>
25 
26 
27 #if BEATMUP_PLATFORM_WINDOWS
28  #include <windows.h>
29  #undef min
30  #undef max
31 #else
32  #include <unistd.h>
33  #include <sys/statfs.h>
34  #include <sys/sysinfo.h>
35 #endif
36 
37 
38 using namespace Beatmup;
39 
40 const size_t AlignedMemory::DEFAULT_ALIGNMENT = 8;
41 
42 
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 }
55 
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 }
68 
69 
70 AlignedMemory::AlignedMemory(size_t size, size_t align) {
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 }
80 
81 
82 AlignedMemory::AlignedMemory(size_t size, int value, size_t align): AlignedMemory(size, align) {
83  memset(alignAddr, value, size);
84 }
85 
86 
88  if (rawAddr)
89  std::free(rawAddr);
90 }
91 
92 
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 }
102 
103 
105  if (rawAddr) {
106  std::free(rawAddr);
107  rawAddr = nullptr;
108  alignAddr = nullptr;
109  }
110 }
Aligned memory buffer.
Definition: memory.h:27
void free()
Frees the allocated memory.
Definition: memory.cpp:104
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
static const size_t DEFAULT_ALIGNMENT
default number of bytes to align the address
Definition: memory.h:33
static uint64_t total()
Returns the size of total operating memory in bytes.
Definition: memory.cpp:56
#define BEATMUP_DEBUG_I(...)
Definition: debug.h:33
jlong jobject size