Berserk
Memory.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_MEMORY_HPP
29 #define BERSERK_MEMORY_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Typedefs.hpp>
33 
34 #include <cstdlib>
35 #include <cstring>
36 #include <memory>
37 
39 
44 class Memory final {
45 public:
46  static const size_t ALIGNMENT = 16;
47  static const size_t KiB = 1024;
48  static const size_t MiB = 1024 * KiB;
49 
58  static void Copy(void *destination, const void *source, size_t sizeInBytes) {
59  if (sizeInBytes > 0)
60  std::memcpy(destination, source, sizeInBytes);
61  }
62 
71  static void Set(void *destination, int value, size_t sizeInBytes) {
72  if (sizeInBytes > 0)
73  std::memset(destination, value, sizeInBytes);
74  }
75 
85  static int Compare(const void *a, const void *b, size_t sizeInBytes) {
86  return std::memcmp(a, b, sizeInBytes);
87  }
88 
95  static void *Allocate(size_t sizeInBytes) {
96  return std::malloc(sizeInBytes);
97  }
98 
106  static void *Reallocate(void *memory, size_t sizeInBytes) {
107  return std::realloc(memory, sizeInBytes);
108  }
109 
114  static void Deallocate(void *memory) {
115  std::free(memory);
116  }
117 
119  static size_t GetAllocateCalls() { return 0; }
120 
122  static size_t GetDeallocateCalls() { return 0; }
123 
132  static size_t AlignSize(size_t size, size_t alignment) {
133  return size + (size % alignment == 0 ? 0 : alignment - (size % alignment));
134  }
135 };
136 
138 
139 #endif//BERSERK_MEMORY_HPP
#define BRK_NS_END
Definition: Config.hpp:48
System memory wrapper.
Definition: Memory.hpp:44
static size_t GetAllocateCalls()
Definition: Memory.hpp:119
static void Copy(void *destination, const void *source, size_t sizeInBytes)
Definition: Memory.hpp:58
static size_t AlignSize(size_t size, size_t alignment)
Definition: Memory.hpp:132
static const size_t MiB
Definition: Memory.hpp:48
static int Compare(const void *a, const void *b, size_t sizeInBytes)
Definition: Memory.hpp:85
static void Set(void *destination, int value, size_t sizeInBytes)
Definition: Memory.hpp:71
static size_t GetDeallocateCalls()
Definition: Memory.hpp:122
static void * Allocate(size_t sizeInBytes)
Definition: Memory.hpp:95
static const size_t KiB
Definition: Memory.hpp:47
static const size_t ALIGNMENT
Definition: Memory.hpp:46
static void * Reallocate(void *memory, size_t sizeInBytes)
Definition: Memory.hpp:106
static void Deallocate(void *memory)
Definition: Memory.hpp:114
Definition: GLDevice.cpp:46