mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
memory.h
Go to the documentation of this file.
1 #ifndef MF_UTILITY_MEMORY_H_
2 #define MF_UTILITY_MEMORY_H_
3 
4 #include "../common.h"
5 #include <cassert>
6 #include <cstddef>
7 
8 namespace mf {
9 
11 std::size_t system_page_size();
12 
13 
15 template<typename T>
16 std::size_t round_up_to_fit_system_page_size(std::size_t n) {
17  MF_EXPECTS(system_page_size() % sizeof(T) == 0);
18  std::size_t page_capacity = system_page_size() / sizeof(T);
19  std::size_t remaining = page_capacity - (n % page_capacity);
20  return n + remaining;
21 }
22 
24 inline std::size_t raw_round_up_to_fit_system_page_size(std::size_t len) {
25  return round_up_to_fit_system_page_size<byte>(len);
26 }
27 
28 enum class memory_usage_advice {
29  normal,
30  sequential,
31  random
32 };
33 
35 void set_memory_usage_advice(void* ptr, std::size_t, memory_usage_advice);
36 
37 
40 public:
41  void* raw_allocate(std::size_t size, std::size_t align = 1);
42  void raw_deallocate(void* ptr, std::size_t size);
43 };
44 
45 
47 
51 public:
52  void* raw_allocate(std::size_t size, std::size_t align = 1);
53  void raw_deallocate(void* ptr, std::size_t size);
54 };
55 
56 
58 
62 public:
63  void* raw_allocate(std::size_t size, std::size_t align = 1);
64  void raw_deallocate(void* ptr, std::size_t size);
65 };
66 
67 
68 /*
69 template<typename T>
70 class ring_allocator : private raw_ring_allocator {
71 public:
72  using value_type = T;
73  using pointer = T*;
74 
75  pointer allocate(std::size_t n) {
76  void* raw_ptr = raw_allocate(n * sizeof(T), alignof(T));
77  return reinterpret_cast<pointer>(raw_ptr);
78  }
79 
80  void deallocate(pointer ptr, std::size_t n) {
81  void* raw_ptr = reinterpret_cast<void*>(ptr);
82  raw_deallocate(raw_ptr, n * sizeof(T));
83  }
84 
85  friend bool operator==(const ring_allocator&, const ring_allocator&) noexcept { return true; }
86  friend bool operator!=(const ring_allocator&, const ring_allocator&) noexcept { return false; }
87 };
88 */
89 
90 
91 }
92 
93 #endif
std::size_t raw_round_up_to_fit_system_page_size(std::size_t len)
Round number of bytes len up to a multiple of the system page size.
Definition: memory.h:24
void raw_deallocate(void *ptr, std::size_t size)
void * raw_allocate(std::size_t size, std::size_t align=1)
memory_usage_advice
Definition: memory.h:28
Ring allocator, allocates ring buffer memory.
Definition: memory.h:50
void * raw_allocate(std::size_t size, std::size_t align=1)
void raw_deallocate(void *ptr, std::size_t size)
#define MF_EXPECTS(condition)
Definition: common.h:27
Raw allocator, allocates given number of bytes.
Definition: memory.h:39
void raw_deallocate(void *ptr, std::size_t size)
void set_memory_usage_advice(void *ptr, std::size_t, memory_usage_advice)
Provide hint to operating system on how memory at ptr will be accessed.
std::size_t system_page_size()
Get page size of operating system, in bytes.
std::size_t round_up_to_fit_system_page_size(std::size_t n)
Round n up so that T[n] has a size that is a multiple of the system page size.
Definition: memory.h:16
void * raw_allocate(std::size_t size, std::size_t align=1)
Null allocator, allocates virtual memory which is not used.
Definition: memory.h:61