CppCommon  1.0.4.1
C++ Common Library
memory.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_MEMORY_MEMORY_H
10 #define CPPCOMMON_MEMORY_MEMORY_H
11 
12 #include "errors/exceptions.h"
13 
14 #include <algorithm>
15 #include <cassert>
16 #include <cstddef>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <cstring>
20 #include <limits>
21 #include <memory>
22 
23 namespace CppCommon {
24 
26 
31 class Memory
32 {
33 public:
34  Memory() = delete;
35  Memory(const Memory&) = delete;
36  Memory(Memory&&) = delete;
37  ~Memory() = delete;
38 
39  Memory& operator=(const Memory&) = delete;
40  Memory& operator=(Memory&&) = delete;
41 
43  static int64_t RamTotal();
45  static int64_t RamFree();
46 
48 
53  static bool IsZero(const void* buffer, size_t size) noexcept;
54 
56 
60  static bool IsValidAlignment(size_t alignment) noexcept;
61 
63 
68  template <typename T>
69  static bool IsAligned(const T* address, size_t alignment = alignof(T)) noexcept;
70 
72 
78  template <typename T>
79  static T* Align(const T* address, size_t alignment = alignof(T), bool upwards = true) noexcept;
80 
82 
86  static void ZeroFill(void* buffer, size_t size);
88 
92  static void RandomFill(void* buffer, size_t size);
94 
98  static void CryptoFill(void* buffer, size_t size);
99 };
100 
103 } // namespace CppCommon
104 
105 #include "memory.inl"
106 
107 #endif // CPPCOMMON_MEMORY_MEMORY_H
Memory management static class.
Definition: memory.h:32
static bool IsZero(const void *buffer, size_t size) noexcept
Is the given memory buffer filled with zeros?
Definition: memory.cpp:95
static bool IsValidAlignment(size_t alignment) noexcept
Is the given alignment valid?
Definition: memory.inl:13
static void CryptoFill(void *buffer, size_t size)
Fill the given memory buffer with cryptographic strong random bytes.
Definition: memory.cpp:136
static int64_t RamFree()
Free RAM in bytes.
Definition: memory.cpp:59
Memory(Memory &&)=delete
static bool IsAligned(const T *address, size_t alignment=alignof(T)) noexcept
Is the given pointer aligned?
Definition: memory.inl:19
Memory(const Memory &)=delete
Memory & operator=(Memory &&)=delete
static int64_t RamTotal()
Total RAM in bytes.
Definition: memory.cpp:33
static T * Align(const T *address, size_t alignment=alignof(T), bool upwards=true) noexcept
Align pointer (upwards or downwards)
Definition: memory.inl:29
Memory & operator=(const Memory &)=delete
static void ZeroFill(void *buffer, size_t size)
Fill the given memory buffer with zeros.
Definition: memory.cpp:104
static void RandomFill(void *buffer, size_t size)
Fill the given memory buffer with random bytes.
Definition: memory.cpp:117
Exceptions definition.
C++ Common project definitions.
Definition: token_bucket.h:15