diff options
author | Andreas Kling <kling@serenityos.org> | 2020-11-24 16:37:55 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-24 16:37:55 +0100 |
commit | c33d71c5ffda4e7f887d93a2645a28ffc7dad55c (patch) | |
tree | 61146f032798775595c904a5d2b9167d9c5c4ccf | |
parent | 3e3a72f2a2a15ab70643ac816918c2083af717dd (diff) | |
download | serenity-c33d71c5ffda4e7f887d93a2645a28ffc7dad55c.zip |
AK: Add IntrusiveList::take_last()
-rw-r--r-- | AK/IntrusiveList.h | 11 | ||||
-rw-r--r-- | 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: @@ -234,6 +235,16 @@ inline T* IntrusiveList<T, member>::take_first() } 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 { return m_storage.m_last ? node_to_value(*m_storage.m_last) : nullptr; 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<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; |