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);
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;
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
ChunkMemoryHandler::m_capacity
size_t m_capacity
Definition: MemoryHandler.hpp:13
BlockMemoryHandler
Definition: MemoryHandler.hpp:61
BlockMemoryHandler::~BlockMemoryHandler
virtual ~BlockMemoryHandler()
Definition: MemoryHandler.hpp:119
BlockMemoryHandler::m_address
unsigned char * m_address
Definition: MemoryHandler.hpp:75
ChunkMemoryHandler::~ChunkMemoryHandler
virtual ~ChunkMemoryHandler()
Definition: MemoryHandler.hpp:26
BlockMemoryHandler::m_size
size_t m_size
Definition: MemoryHandler.hpp:71
NULL
#define NULL
Definition: mydefs.hpp:141
ChunkMemoryHandler
Definition: MemoryHandler.hpp:8
BlockMemoryHandler::m_used
size_t m_used
Definition: MemoryHandler.hpp:72
BlockMemoryHandler::m_memory
unsigned char * m_memory
Definition: MemoryHandler.hpp:74
BlockMemoryHandler::BlockMemoryHandler
BlockMemoryHandler(size_t size=32)
Definition: MemoryHandler.hpp:65
BlockMemoryHandler::shrinkToFit
void shrinkToFit()
Definition: MemoryHandler.hpp:110
ChunkMemoryHandler::m_used
std::vector< size_t > m_used
Definition: MemoryHandler.hpp:11
ChunkMemoryHandler::ChunkMemoryHandler
ChunkMemoryHandler(size_t size=131072)
Definition: MemoryHandler.hpp:17
ChunkMemoryHandler::m_memory
std::vector< unsigned char * > m_memory
Definition: MemoryHandler.hpp:10
BlockMemoryHandler::m_capacity
size_t m_capacity
Definition: MemoryHandler.hpp:73
BlockMemoryHandler::alloc
void * alloc(size_t required, long &offset)
Definition: MemoryHandler.hpp:78
ChunkMemoryHandler::alloc
void * alloc(size_t required, long &offset)
Definition: MemoryHandler.hpp:34


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 Wed Mar 2 2022 00:37:24