From c33d71c5ffda4e7f887d93a2645a28ffc7dad55c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 24 Nov 2020 16:37:55 +0100 Subject: [PATCH] AK: Add IntrusiveList::take_last() --- AK/IntrusiveList.h | 11 +++++++++++ AK/MemoryStream.h | 27 ++++++++++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/AK/IntrusiveList.h b/AK/IntrusiveList.h index 09f35737d7..46045f7f22 100644 --- a/AK/IntrusiveList.h +++ b/AK/IntrusiveList.h @@ -56,6 +56,7 @@ public: T* last() const; T* take_first(); + T* take_last(); class Iterator { public: @@ -233,6 +234,16 @@ inline T* IntrusiveList::take_first() return nullptr; } +template +inline T* IntrusiveList::take_last() +{ + if (auto* ptr = last()) { + remove(*ptr); + return ptr; + } + return nullptr; +} + template inline T* IntrusiveList::last() const { diff --git a/AK/MemoryStream.h b/AK/MemoryStream.h index 26618637eb..e1b8a12d5c 100644 --- a/AK/MemoryStream.h +++ b/AK/MemoryStream.h @@ -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(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(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;