MemoryHandler.hpp
Go to the documentation of this file.
1 #ifndef MEMORY_HANDLER
2 #define MEMORY_HANDLER
3 
4 #include <iostream>
5 #include <vector>
6 #include <string.h> //memset
7 
9 { private:
10  std::vector<unsigned char*> m_memory;
11  std::vector<size_t> m_used;
12  // chunk capacity;
13  size_t m_capacity;
14 
15 
16  public:
17  ChunkMemoryHandler(size_t size = 131072) : m_used(0), m_capacity(size)
18  {
19  unsigned char* mem = (unsigned char*)malloc(size);
20  m_memory.push_back(mem);
21  memset(mem, 0, size);
22  m_used.push_back(0);
23  }
24 
25 
27  {
28  for(size_t i = 0; i < m_memory.size(); ++i)
29  {
30  free(m_memory[i]);
31  }
32  }
33  template <typename T>
34  void* alloc(size_t required, long& offset)
35  {
36  required = (sizeof(T) * required);
37  for(size_t i = 0; i < m_memory.size(); ++i)
38  {
39  size_t used = m_used[i];
40  if((m_capacity - used) >= required)
41  {
42  m_used[i] += required;
43 
44  return m_memory[i] + used;
45  }
46  }
47 
48 
49  ssize_t size = (required > m_capacity) ? required : m_capacity;
50 
51  unsigned char* mem = (unsigned char*)malloc(size);
52  memset(mem, 0, size);
53  m_memory.push_back(mem);
54  m_used.push_back(required);
55  return mem;
56  }
57 
58 
59 };
60 
62 {
63  public:
64  // 1 MiB default
65  BlockMemoryHandler(size_t size = 32) : m_size(size), m_used(0), m_capacity(size)
66  {
67  m_memory = (unsigned char*)malloc(m_size);
68  m_address = m_memory;
69  }
70 
71  size_t m_size;
72  size_t m_used;
73  size_t m_capacity;
74  unsigned char* m_memory;
75  unsigned char* m_address;
76 
77  template <typename T>
78  void* alloc(size_t required, long& offset)
79  {
80  required = (sizeof(T) * required);
81 
82  if((m_capacity - m_used) < required)
83  {
84  long addSize = (required > m_size) ? required : m_size;
85  m_capacity += addSize;
86 
87  unsigned char* tmp = (unsigned char*)realloc(m_memory, m_capacity);
88 
89  // check if realloc failed
90  if(NULL == tmp)
91  {
92  return tmp;
93  }
94 
95  m_memory = tmp;
96 
97  if(m_memory != m_address)
98  {
99  offset = m_memory - m_address;
100  m_address = m_memory;
101  }
102  }
103 
104  unsigned char* ptr = &m_memory[m_used];
105  memset(ptr, 0, required);
106  m_used += required;
107  return (void*)ptr;
108  }
109 
110  void shrinkToFit()
111  {
112  m_memory = (unsigned char*)realloc(m_memory, m_used);
113  if(m_memory != m_address)
114  {
115  std::cout << "FUCK WHY" << std::endl;
116  }
117  }
118 
120  {
121  free(m_memory);
122  m_memory = NULL;
123  m_address = NULL;
124  }
125 
126 };
127 
128 #endif
void * alloc(size_t required, long &offset)
unsigned char * m_memory
virtual ~ChunkMemoryHandler()
BlockMemoryHandler(size_t size=32)
unsigned char * m_address
std::vector< unsigned char * > m_memory
virtual ~BlockMemoryHandler()
std::vector< size_t > m_used
void * alloc(size_t required, long &offset)
#define NULL
Definition: mydefs.hpp:141
ChunkMemoryHandler(size_t size=131072)


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:08