52 _alignment(alignment),
53 _max_size_ptr((char*)ptr) {
55 void* aligned_ptr = std::align(_alignment, 1, ptr, size);
56 size_t size_after_align =
57 (((
char*)ptr + size) - (
char*)aligned_ptr) / _alignment;
58 size_after_align *= _alignment;
59 _free_segments.push_back(
60 std::pair<void*, size_t>(aligned_ptr, size_after_align));
68 std::pair<void*, size_t> tmp =
Alloc(size *
sizeof(T));
69 return std::pair<T*, size_t>((T*)tmp.first, tmp.first ? size : 0);
76 if (size % _alignment) size += _alignment - size % _alignment;
78 for (
size_t i = 0; i < _free_segments.size(); ++i) {
79 void* ptr = std::align(_alignment, size, _free_segments[i].first,
80 _free_segments[i].second);
82 char* end_ptr = (
char*)ptr + size;
83 if (end_ptr > _max_size_ptr) _max_size_ptr = end_ptr;
85 _free_segments[i].first = end_ptr;
86 _free_segments[i].second -=
size;
87 return std::pair<void*, size_t>(ptr,
size);
90 return std::pair<void*, size_t>(
nullptr, 0);
96 for (
const auto& s : _free_segments)
97 if (s.second > size) size = s.second;
104 void Free(
const std::pair<T*, size_t>& segment) {
105 size_t size =
sizeof(T) * segment.second;
106 if (size % _alignment) size += _alignment - size % _alignment;
108 Free(std::pair<void*, size_t>(segment.first, size));
112 void Free(
const std::pair<void*, size_t>& segment) {
114 if ((
char*)segment.first < (
char*)_ptr ||
115 (
char*)segment.first + segment.second > (
char*)_ptr + _size)
116 throw std::runtime_error(
"free(): segment is out of bounds");
120 for (i = 0; i < _free_segments.size(); ++i) {
121 if ((
char*)segment.first < (
char*)_free_segments[i].first)
124 _free_segments.insert(_free_segments.begin() + i, segment);
128 auto seg = _free_segments[0];
129 char* end_ptr = (
char*)seg.first + seg.second;
131 for (
size_t i = 1; i < _free_segments.size(); ++i) {
132 const auto& seg_i = _free_segments[i];
134 if (end_ptr == (
char*)seg_i.first) {
136 seg.second += seg_i.second;
137 end_ptr = (
char*)seg.first + seg.second;
139 _free_segments[
count] = seg;
140 seg = _free_segments[i];
141 end_ptr = (
char*)seg.first + seg.second;
145 _free_segments[
count] = seg;
147 _free_segments.resize(
count);
151 for (
size_t i = 1; i < _free_segments.size(); ++i) {
152 char* prev_end_ptr = (
char*)_free_segments[i - 1].first +
153 _free_segments[i - 1].second;
154 if (prev_end_ptr > (
char*)_free_segments[i].first) {
155 throw std::runtime_error(
156 "free(): Overlapping free segments found after " 164 size_t MaxUsed()
const {
return _max_size_ptr - (
char*)_ptr; }
171 return _free_segments;
177 std::cerr <<
"ptr " << (
void*)s.first <<
"\t size " << s.second
178 <<
"\t end " << (
void*)((
char*)s.first + s.second) <<
"\n";
187 enum internal_config { DEBUG = 0 };
196 const size_t _alignment;
203 std::vector<std::pair<void*, size_t>> _free_segments;
A class for managing memory segments within a memory allocation.
Definition: MemoryAllocation.h:40
static void PrintSegment(const std::pair< T *, size_t > &s)
Prints the segment. Meant for debugging.
Definition: MemoryAllocation.h:176
void Free(const std::pair< void *, size_t > &segment)
Frees a previously returned segment.
Definition: MemoryAllocation.h:112
std::pair< void *, size_t > AllocLargestSegment()
Returns the largest free segment.
Definition: MemoryAllocation.h:94
int size
Definition: FilePCD.cpp:59
std::pair< T *, size_t > Alloc(size_t size)
Definition: MemoryAllocation.h:67
const std::vector< std::pair< void *, size_t > > & FreeSegments() const
Returns the list of free segments.
Definition: MemoryAllocation.h:170
void Free(const std::pair< T *, size_t > &segment)
Frees a previously returned segment.
Definition: MemoryAllocation.h:104
size_t MaxUsed() const
Returns the peak memory usage in bytes.
Definition: MemoryAllocation.h:164
int count
Definition: FilePCD.cpp:61
void PrintFreeSegments() const
Prints all free segments. Meant for debugging.
Definition: MemoryAllocation.h:182
Definition: PinholeCameraIntrinsic.cpp:35
std::pair< void *, size_t > Alloc(size_t size)
Definition: MemoryAllocation.h:74
MemoryAllocation(void *ptr, size_t size, size_t alignment=1)
Definition: MemoryAllocation.h:49
size_t Alignment() const
Returns the alignment in bytes.
Definition: MemoryAllocation.h:167