Selaa lähdekoodia

KBuffer: Add set_size() and LogStream operator<<

It will be useful to be able to set the "public" size of a KBuffer.
It can still have a different amount of memory allocated internally.
Andreas Kling 6 vuotta sitten
vanhempi
commit
f58b0c245d
1 muutettua tiedostoa jossa 14 lisäystä ja 0 poistoa
  1. 14 0
      Kernel/KBuffer.h

+ 14 - 0
Kernel/KBuffer.h

@@ -11,6 +11,7 @@
 // severely limited kmalloc heap.
 
 #include <AK/Assertions.h>
+#include <AK/LogStream.h>
 #include <Kernel/VM/MemoryManager.h>
 #include <Kernel/VM/Region.h>
 
@@ -35,6 +36,12 @@ public:
     size_t size() const { return m_size; }
     size_t capacity() const { return m_region->size(); }
 
+    void set_size(size_t size)
+    {
+        ASSERT(size <= capacity());
+        m_size = size;
+    }
+
 private:
     explicit KBufferImpl(NonnullRefPtr<Region>&& region, size_t size)
         : m_size(size)
@@ -63,6 +70,8 @@ public:
     size_t size() const { return m_impl->size(); }
     size_t capacity() const { return m_impl->size(); }
 
+    void set_size(size_t size) { m_impl->set_size(size); }
+
     const KBufferImpl& impl() const { return m_impl; }
 
     KBuffer(const ByteBuffer& buffer)
@@ -78,3 +87,8 @@ private:
 
     NonnullRefPtr<KBufferImpl> m_impl;
 };
+
+inline const LogStream& operator<<(const LogStream& stream, const KBuffer& value)
+{
+    return stream << StringView(value.data(), value.size());
+}