20 #ifndef KERNEL_UTILITIES_RANGELIST_H 21 #define KERNEL_UTILITIES_RANGELIST_H 23 #include "pedigree/kernel/compiler.h" 24 #include "pedigree/kernel/processor/types.h" 25 #include "pedigree/kernel/utilities/StaticString.h" 26 #include "pedigree/kernel/utilities/Vector.h" 34 template <
typename T,
bool Reversed = false>
43 inline RangeList(
bool preferUsed) : m_List(), m_bPreferUsed(preferUsed)
49 RangeList(T Address, T Length,
bool XXX,
bool preferUsed =
false)
50 : m_List(), m_bPreferUsed(preferUsed)
53 m_List.pushBack(range);
65 Range(T Address, T Length) : address(Address), length(Length)
76 return (address == other.
address) && (length == other.
length);
85 void free(T address, T length,
bool merge =
true);
91 bool allocate(T length, T &address);
96 bool allocateSpecific(T address, T length);
103 return m_List.count();
106 Range getRange(
size_t index)
const;
112 void dump(
void (*emit_line)(
const char *s))
const;
122 typedef typename decltype(m_List)::ConstIterator ConstIterator;
123 typedef typename decltype(m_List)::ReverseIterator ReverseIterator;
125 typename decltype(m_List)::ConstReverseIterator ConstReverseIterator;
131 template <
typename T,
bool Reversed>
137 for (ConstIterator it = other.
m_List.begin(); it != other.
m_List.end();
140 Range *pRange =
new Range((*it)->address, (*it)->length);
145 template <
typename T,
bool Reversed>
152 for (ConstIterator it = other.
m_List.begin(); it != other.
m_List.end();
155 Range *pRange =
new Range((*it)->address, (*it)->length);
162 template <
typename T,
bool Reversed>
168 ConstIterator end = m_List.
end();
171 bool needsNew =
true;
172 for (; cur != end; ++cur)
175 if (((*cur)->address + (*cur)->length) == address)
178 (*cur)->length += length;
183 else if ((*cur)->address == (address + length))
186 (*cur)->address -= length;
187 (*cur)->length += length;
206 bool front = Reversed;
221 template <
typename T,
bool Reversed>
224 bool bSuccess =
false;
226 for (
int i = 0; i < 2; ++i)
228 auto it = Reversed ? m_List.rbegin() : m_List.
begin();
229 auto end = Reversed ? m_List.rend() : m_List.
end();
231 for (; it != end; ++it)
233 if ((*it)->length < length)
241 T offset = (*it)->length - length;
242 address = (*it)->address + offset;
246 address = (*it)->address;
247 (*it)->address += length;
249 (*it)->length -= length;
279 template <
typename T,
bool Reversed>
282 bool bSuccess =
false;
283 for (
int i = 0; i < 2; ++i)
288 if ((*cur)->address == address && (*cur)->length == length)
298 (*cur)->address < address &&
299 ((*cur)->address + (*cur)->length) == (address + length))
301 (*cur)->length -= length;
307 else if ((*cur)->address == address && (*cur)->length > length)
309 (*cur)->address += length;
310 (*cur)->length -= length;
317 (*cur)->address < address &&
318 ((*cur)->address + (*cur)->length) > (address + length))
323 (*cur)->address + (*cur)->length - address - length);
324 (*cur)->length = address - (*cur)->address;
346 template <
typename T,
bool Reversed>
350 if (index >= m_List.
size())
353 return Range(*m_List[index]);
356 template <
typename T,
bool Reversed>
362 template <
typename T,
bool Reversed>
365 for (
size_t i = 0; i < m_List.
count(); ++i)
372 template <
typename T,
bool Reversed>
375 if (m_List.
count() < 2)
380 for (
size_t i = 0; i < (m_List.
count() - 1);)
383 Range *cur = m_List[i];
384 Range *next = m_List[i + 1];
386 uintptr_t cur_address = cur->
address;
387 uintptr_t next_address = next->
address;
388 size_t cur_len = cur->
length;
389 size_t next_len = next->
length;
391 if ((cur_address + cur_len) == next_address)
398 else if ((next_address + next_len) == cur_address)
412 template <
typename T,
bool Reversed>
415 for (
size_t i = 0; i < m_List.
count(); ++i)
417 const Range *range = m_List[i];
420 str.append(
"range ");
421 str.append(range->
address, 16, 16,
'0');
425 str.append(range->
length, 10);
426 str.append(
" bytes)");
427 emit_line(static_cast<const char *>(str));
void pushBack(const T &value)
Range(T Address, T Length)
A vector / dynamic array.
void free(T address, T length, bool merge=true)
Range getRange(size_t index) const
bool allocate(T length, T &address)
void dump(void(*emit_line)(const char *s)) const
bool allocateSpecific(T address, T length)
An iterator applicable for many data structures.
void pushFront(const T &value)
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
RangeList(bool preferUsed)
void clear(bool freeMem=false)
RangeList(T Address, T Length, bool XXX, bool preferUsed=false)