33 _alignment(alignment),
34 _max_size_ptr((char*)ptr) {
36 void* aligned_ptr = std::align(_alignment, 1, ptr,
size);
37 size_t size_after_align =
38 (((
char*)ptr +
size) - (
char*)aligned_ptr) / _alignment;
39 size_after_align *= _alignment;
40 _free_segments.push_back(
41 std::pair<void*, size_t>(aligned_ptr, size_after_align));
49 std::pair<void*, size_t> tmp =
Alloc(
size *
sizeof(T));
50 return std::pair<T*, size_t>((T*)tmp.first, tmp.first ?
size : 0);
57 if (
size % _alignment)
size += _alignment -
size % _alignment;
59 for (
size_t i = 0; i < _free_segments.size(); ++i) {
60 void* ptr = std::align(_alignment,
size, _free_segments[i].first,
61 _free_segments[i].second);
63 char* end_ptr = (
char*)ptr +
size;
64 if (end_ptr > _max_size_ptr) _max_size_ptr = end_ptr;
66 _free_segments[i].first = end_ptr;
67 _free_segments[i].second -=
size;
68 return std::pair<void*, size_t>(ptr,
size);
71 return std::pair<void*, size_t>(
nullptr, 0);
77 for (
const auto& s : _free_segments)
78 if (s.second >
size)
size = s.second;
85 void Free(
const std::pair<T*, size_t>& segment) {
86 size_t size =
sizeof(T) * segment.second;
87 if (
size % _alignment)
size += _alignment -
size % _alignment;
89 Free(std::pair<void*, size_t>(segment.first,
size));
93 void Free(
const std::pair<void*, size_t>& segment) {
95 if ((
char*)segment.first < (
char*)_ptr ||
96 (
char*)segment.first + segment.second > (
char*)_ptr + _size)
97 throw std::runtime_error(
"free(): segment is out of bounds");
101 for (i = 0; i < _free_segments.size(); ++i) {
102 if ((
char*)segment.first < (
char*)_free_segments[i].first)
105 _free_segments.insert(_free_segments.begin() + i, segment);
109 auto seg = _free_segments[0];
110 char* end_ptr = (
char*)seg.first + seg.second;
112 for (
size_t i = 1; i < _free_segments.size(); ++i) {
113 const auto& seg_i = _free_segments[i];
115 if (end_ptr == (
char*)seg_i.first) {
117 seg.second += seg_i.second;
118 end_ptr = (
char*)seg.first + seg.second;
120 _free_segments[
count] = seg;
121 seg = _free_segments[i];
122 end_ptr = (
char*)seg.first + seg.second;
126 _free_segments[
count] = seg;
128 _free_segments.resize(
count);
132 for (
size_t i = 1; i < _free_segments.size(); ++i) {
133 char* prev_end_ptr = (
char*)_free_segments[i - 1].first +
134 _free_segments[i - 1].second;
135 if (prev_end_ptr > (
char*)_free_segments[i].first) {
136 throw std::runtime_error(
137 "free(): Overlapping free segments found after "
145 size_t MaxUsed()
const {
return _max_size_ptr - (
char*)_ptr; }
152 return _free_segments;
158 std::cerr <<
"ptr " << (
void*)s.first <<
"\t size " << s.second
159 <<
"\t end " << (
void*)((
char*)s.first + s.second) <<
"\n";
168 enum internal_config { DEBUG = 0 };
177 const size_t _alignment;
184 std::vector<std::pair<void*, size_t>> _free_segments;
A class for managing memory segments within a memory allocation.
Definition: MemoryAllocation.h:21
void Free(const std::pair< T *, size_t > &segment)
Frees a previously returned segment.
Definition: MemoryAllocation.h:85
static void PrintSegment(const std::pair< T *, size_t > &s)
Prints the segment. Meant for debugging.
Definition: MemoryAllocation.h:157
size_t MaxUsed() const
Returns the peak memory usage in bytes.
Definition: MemoryAllocation.h:145
MemoryAllocation(void *ptr, size_t size, size_t alignment=1)
Definition: MemoryAllocation.h:30
void Free(const std::pair< void *, size_t > &segment)
Frees a previously returned segment.
Definition: MemoryAllocation.h:93
void PrintFreeSegments() const
Prints all free segments. Meant for debugging.
Definition: MemoryAllocation.h:163
std::pair< void *, size_t > Alloc(size_t size)
Definition: MemoryAllocation.h:55
std::pair< T *, size_t > Alloc(size_t size)
Definition: MemoryAllocation.h:48
const std::vector< std::pair< void *, size_t > > & FreeSegments() const
Returns the list of free segments.
Definition: MemoryAllocation.h:151
size_t Alignment() const
Returns the alignment in bytes.
Definition: MemoryAllocation.h:148
std::pair< void *, size_t > AllocLargestSegment()
Returns the largest free segment.
Definition: MemoryAllocation.h:75
Definition: PinholeCameraIntrinsic.cpp:16