allocator.cc
Go to the documentation of this file.
1 /*
2  * allocator implementation
3  *
4  * Copyright (C) Thomas Escher, Kai Lingemann
5  *
6  * Released under the GPL version 3.
7  *
8  */
9 
10 #include "slam6d/allocator.h"
11 
12 using std::vector;
13 #include <iostream>
14 using std::cout;
15 using std::endl;
16 #include <stdexcept>
17 using std::runtime_error;
18 
19 #include <cstring> //memset
20 
21 
22 
23 ChunkAllocator::ChunkAllocator(unsigned int _csize) :
24  chunksize(_csize), index(_csize), memsize(0), wastedspace(0)
25 {}
26 
28 {
29  for(unsigned int i = 0; i < mem.size(); i++) {
30  delete[] mem[i];
31  }
32 }
33 
35 {
36  cout << "Alloc'd " << memsize/(1024*1024.0) << " Mb " << endl;
37  cout << " wasted " << wastedspace/(1024*1024.0) << " Mb " << endl;
38 }
39 
40 unsigned char* ChunkAllocator::allocate(unsigned int size)
41 {
42  unsigned char* chunk;
43  if (size + index > chunksize) {
44  // create new chunk
46  // check for oversize
47  if (chunksize > size) {
48  chunk = new unsigned char[chunksize];
49  memset(chunk, 0, chunksize);
51  } else {
52  chunk = new unsigned char[size];
53  memset(chunk, 0, size);
54  memsize+=size;
55  }
56  mem.push_back(chunk);
57  index = 0;
58  } else {
59  // use last chunk
60  chunk = mem.back();
61  chunk = chunk + index;
62  }
63  index += size;
64  return chunk;
65 }
66 
67 
68 
70  chunksize(_csize), memsize(0)
71 {}
72 
74 {
75  for(unsigned int i = 0; i < mem.size(); i++) {
76  delete[] mem[i];
77  }
78 }
79 
81 {
82  cout << "Alloc'd " << memsize/(1024*1024.0) << " Mb " << endl;
83 
84  unsigned long int wastedspace = 0;
85  for(unsigned int i = 0; i < index.size(); i++) {
86  if(index[i] < chunksize) {
87  wastedspace += chunksize - index[i];
88  }
89  }
90  cout << "wasted " << wastedspace/(1024*1024.0) << " Mb " << endl;
91 }
92 
93 unsigned char* PackedChunkAllocator::allocate(unsigned int size)
94 {
95  unsigned char* chunk;
96  for (unsigned int i = 0; i < index.size(); i++) {
97  if ( !(size + index[i] > chunksize) ) {
98  // found a suitable entry
99  chunk = mem[i];
100  chunk = chunk + index[i]; // pointer to free byte
101  index[i] += size; // increment index
102  return chunk;
103  }
104  }
105  // no chunk is large enough... make new one
106  if (chunksize > size) {
107  chunk = new unsigned char[chunksize];
108  memset(chunk, 0, chunksize);
109  memsize += chunksize;
110  } else { // in case the requested memory is larger than our chunks, make a single chunk thats large enough
111  chunk = new unsigned char[size];
112  memset(chunk, 0, size);
113  memsize += size;
114  }
115  mem.push_back(chunk);
116  index.push_back(size);
117  return chunk;
118 }
119 
120 SequentialAllocator::SequentialAllocator(unsigned char* base_ptr, unsigned int max_size) :
121  m_base_ptr(base_ptr), m_size(max_size), m_index(0)
122 {}
123 
125 {}
126 
128 {
129  cout << "Using " << m_index << " of " << m_size << " bytes." << endl;
130 }
131 
132 unsigned char* SequentialAllocator::allocate(unsigned int size)
133 {
134  if(m_index + size > m_size) {
135  throw runtime_error("SequentialAllocator memory overflow");
136  }
137  unsigned char* r = m_base_ptr + m_index;
138  m_index += size;
139  return r;
140 }
SequentialAllocator::allocate
unsigned char * allocate(unsigned int size)
Definition: allocator.cc:132
ChunkAllocator::printSize
void printSize() const
Definition: allocator.cc:34
SequentialAllocator::~SequentialAllocator
~SequentialAllocator()
Definition: allocator.cc:124
ChunkAllocator::ChunkAllocator
ChunkAllocator(unsigned int _csize=(1<< 20))
Definition: allocator.cc:23
ChunkAllocator::wastedspace
unsigned long int wastedspace
Definition: allocator.h:39
ChunkAllocator::chunksize
const unsigned int chunksize
Definition: allocator.h:36
ChunkAllocator::index
unsigned int index
Definition: allocator.h:37
PackedChunkAllocator::memsize
unsigned long int memsize
Definition: allocator.h:55
ChunkAllocator::~ChunkAllocator
~ChunkAllocator()
Definition: allocator.cc:27
PackedChunkAllocator::PackedChunkAllocator
PackedChunkAllocator(unsigned int _csize=(1<< 20))
Definition: allocator.cc:69
SequentialAllocator::SequentialAllocator
SequentialAllocator(unsigned char *base_ptr, unsigned int max_size)
Handle a preallocated memory up to max_size.
Definition: allocator.cc:120
PackedChunkAllocator::~PackedChunkAllocator
~PackedChunkAllocator()
Definition: allocator.cc:73
SequentialAllocator::m_base_ptr
unsigned char * m_base_ptr
Definition: allocator.h:69
PackedChunkAllocator::allocate
unsigned char * allocate(unsigned int size)
Definition: allocator.cc:93
PackedChunkAllocator::index
std::vector< unsigned int > index
Definition: allocator.h:53
PackedChunkAllocator::chunksize
const unsigned int chunksize
Definition: allocator.h:54
SequentialAllocator::m_index
unsigned int m_index
Definition: allocator.h:70
ChunkAllocator::allocate
unsigned char * allocate(unsigned int size)
Definition: allocator.cc:40
allocator.h
allocator object that gets chunks of memory and then hands parts of them to a user
ChunkAllocator::mem
std::vector< unsigned char * > mem
Definition: allocator.h:35
SequentialAllocator::m_size
unsigned int m_size
Definition: allocator.h:70
ChunkAllocator::memsize
unsigned long int memsize
Definition: allocator.h:38
SequentialAllocator::printSize
void printSize() const
Definition: allocator.cc:127
PackedChunkAllocator::mem
std::vector< unsigned char * > mem
Definition: allocator.h:52
PackedChunkAllocator::printSize
void printSize() const
Definition: allocator.cc:80


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:22