AK: Add IntrusiveList::take_last()

This commit is contained in:
Andreas Kling 2020-11-24 16:37:55 +01:00
parent 3e3a72f2a2
commit c33d71c5ff
Notes: sideshowbarker 2024-07-19 01:17:10 +09:00
2 changed files with 25 additions and 13 deletions

View file

@ -56,6 +56,7 @@ public:
T* last() const;
T* take_first();
T* take_last();
class Iterator {
public:
@ -233,6 +234,16 @@ inline T* IntrusiveList<T, member>::take_first()
return nullptr;
}
template<class T, IntrusiveListNode T::*member>
inline T* IntrusiveList<T, member>::take_last()
{
if (auto* ptr = last()) {
remove(*ptr);
return ptr;
}
return nullptr;
}
template<class T, IntrusiveListNode T::*member>
inline T* IntrusiveList<T, member>::last() const
{

View file

@ -96,9 +96,9 @@ public:
bool read_LEB128_unsigned(size_t& result)
{
const auto backup = m_offset;
result = 0;
size_t shift = 0;
result = 0;
size_t num_bytes = 0;
while (true) {
if (eof()) {
m_offset = backup;
@ -106,11 +106,12 @@ public:
return false;
}
const u8 byte = m_bytes[m_offset++];
result |= (byte & 0x7f) << shift;
if ((byte & 0x80) == 0)
const u8 byte = m_bytes[m_offset];
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
++m_offset;
if (!(byte & (1 << 7)))
break;
shift += 7;
++num_bytes;
}
return true;
@ -121,11 +122,9 @@ public:
const auto backup = m_offset;
result = 0;
size_t shift = 0;
size_t num_bytes = 0;
u8 byte = 0;
size_t size = sizeof(ssize_t) * 8;
do {
if (eof()) {
m_offset = backup;
@ -133,13 +132,15 @@ public:
return false;
}
byte = m_bytes[m_offset++];
result |= (byte & 0x7f) << shift;
byte = m_bytes[m_offset];
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
++m_offset;
++num_bytes;
} while (byte & (1 << 7));
if (shift < size && (byte & 0x40)) {
if (num_bytes * 7 < sizeof(size_t) * 4 && (byte & 0x40)) {
// sign extend
result |= (0xffffffffu << shift);
result |= ((size_t)(-1) << (num_bytes * 7));
}
return true;