From 15657f09168622d75a9729feb036f791d74840e2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 16 Mar 2019 13:12:13 +0100 Subject: [PATCH] AK: Remove Buffer since it was only ever instantiated with T=byte. Instead make a specialized AK::ByteBufferImpl class for the backing store of AK::ByteBuffer. This reduces template bloat. --- AK/Buffer.h | 143 ---------------------------------------- AK/ByteBuffer.h | 139 +++++++++++++++++++++++++++++++++++--- Kernel/Ext2FileSystem.h | 1 - 3 files changed, 130 insertions(+), 153 deletions(-) delete mode 100644 AK/Buffer.h diff --git a/AK/Buffer.h b/AK/Buffer.h deleted file mode 100644 index 1d6f9dc5e11..00000000000 --- a/AK/Buffer.h +++ /dev/null @@ -1,143 +0,0 @@ -#pragma once - -#include "Assertions.h" -#include "Retainable.h" -#include "RetainPtr.h" -#include "StdLibExtras.h" -#include "kmalloc.h" - -namespace AK { - -template -class Buffer : public Retainable> { -public: - static Retained create_uninitialized(ssize_t count); - static Retained create_zeroed(ssize_t count); - static Retained copy(const T*, ssize_t count); - static Retained wrap(T*, ssize_t count); - static Retained adopt(T*, ssize_t count); - - ~Buffer() { clear(); } - - void clear() - { - if (!m_elements) - return; - if (m_owned) - kfree(m_elements); - m_elements = nullptr; - } - - T& operator[](ssize_t i) { ASSERT(i < m_size); return m_elements[i]; } - const T& operator[](ssize_t i) const { ASSERT(i < m_size); return m_elements[i]; } - bool is_empty() const { return !m_size; } - ssize_t size() const { return m_size; } - - T* pointer() { return m_elements; } - const T* pointer() const { return m_elements; } - - T* offset_pointer(ssize_t offset) { return m_elements + offset; } - const T* offset_pointer(ssize_t offset) const { return m_elements + offset; } - - const void* end_pointer() const { return m_elements + m_size; } - - // NOTE: trim() does not reallocate. - void trim(ssize_t size) - { - ASSERT(size <= m_size); - m_size = size; - } - - void grow(ssize_t size); - -private: - enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt }; - explicit Buffer(ssize_t); // For ConstructionMode=Uninitialized - Buffer(const T*, ssize_t, ConstructionMode); // For ConstructionMode=Copy - Buffer(T*, ssize_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt - Buffer() { } - - T* m_elements { nullptr }; - ssize_t m_size { 0 }; - bool m_owned { false }; -}; - -template -inline Buffer::Buffer(ssize_t size) - : m_size(size) -{ - m_elements = static_cast(kmalloc(size * sizeof(T))); - m_owned = true; -} - -template -inline Buffer::Buffer(const T* elements, ssize_t size, ConstructionMode mode) - : m_size(size) -{ - ASSERT(mode == Copy); - m_elements = static_cast(kmalloc(size * sizeof(T))); - memcpy(m_elements, elements, size * sizeof(T)); - m_owned = true; -} - -template -inline Buffer::Buffer(T* elements, ssize_t size, ConstructionMode mode) - : m_elements(elements) - , m_size(size) -{ - if (mode == Adopt) { - m_owned = true; - } else if (mode == Wrap) { - m_owned = false; - } -} - -template -inline void Buffer::grow(ssize_t size) -{ - ASSERT(size > m_size); - ASSERT(m_owned); - T* new_elements = static_cast(kmalloc(size * sizeof(T))); - memcpy(new_elements, m_elements, m_size * sizeof(T)); - T* old_elements = m_elements; - m_elements = new_elements; - m_size = size; - kfree(old_elements); -} - -template -inline Retained> Buffer::create_uninitialized(ssize_t size) -{ - return ::adopt(*new Buffer(size)); -} - -template -inline Retained> Buffer::create_zeroed(ssize_t size) -{ - auto buffer = ::adopt(*new Buffer(size)); - memset(buffer->pointer(), 0, size); - return buffer; -} - -template -inline Retained> Buffer::copy(const T* elements, ssize_t size) -{ - return ::adopt(*new Buffer(elements, size, Copy)); -} - -template -inline Retained> Buffer::wrap(T* elements, ssize_t size) -{ - return ::adopt(*new Buffer(elements, size, Wrap)); -} - -template -inline Retained> Buffer::adopt(T* elements, ssize_t size) -{ - return ::adopt(*new Buffer(elements, size, Adopt)); -} - -} - -using AK::Buffer; - diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index ca44e58d999..c1f26d9bf00 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -1,11 +1,66 @@ #pragma once -#include "Buffer.h" #include "Types.h" #include "StdLibExtras.h" +#include +#include +#include namespace AK { +class ByteBufferImpl : public Retainable { +public: + static Retained create_uninitialized(int size); + static Retained create_zeroed(int); + static Retained copy(const void*, int); + static Retained wrap(void*, int); + static Retained adopt(void*, int); + + ~ByteBufferImpl() { clear(); } + + void clear() + { + if (!m_data) + return; + if (m_owned) + kfree(m_data); + m_data = nullptr; + } + + byte& operator[](int i) { ASSERT(i < m_size); return m_data[i]; } + const byte& operator[](int i) const { ASSERT(i < m_size); return m_data[i]; } + bool is_empty() const { return !m_size; } + int size() const { return m_size; } + + byte* pointer() { return m_data; } + const byte* pointer() const { return m_data; } + + byte* offset_pointer(int offset) { return m_data + offset; } + const byte* offset_pointer(int offset) const { return m_data + offset; } + + const void* end_pointer() const { return m_data + m_size; } + + // NOTE: trim() does not reallocate. + void trim(int size) + { + ASSERT(size <= m_size); + m_size = size; + } + + void grow(int size); + +private: + enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt }; + explicit ByteBufferImpl(int); // For ConstructionMode=Uninitialized + ByteBufferImpl(const void*, int, ConstructionMode); // For ConstructionMode=Copy + ByteBufferImpl(void*, int, ConstructionMode); // For ConstructionMode=Wrap/Adopt + ByteBufferImpl() { } + + byte* m_data { nullptr }; + int m_size { 0 }; + bool m_owned { false }; +}; + class ByteBuffer { public: ByteBuffer() { } @@ -30,11 +85,11 @@ public: return *this; } - static ByteBuffer create_uninitialized(ssize_t size) { return ByteBuffer(Buffer::create_uninitialized(size)); } - static ByteBuffer create_zeroed(ssize_t size) { return ByteBuffer(Buffer::create_zeroed(size)); } - static ByteBuffer copy(const byte* data, ssize_t size) { return ByteBuffer(Buffer::copy(data, size)); } - static ByteBuffer wrap(byte* data, ssize_t size) { return ByteBuffer(Buffer::wrap(data, size)); } - static ByteBuffer adopt(byte* data, ssize_t size) { return ByteBuffer(Buffer::adopt(data, size)); } + static ByteBuffer create_uninitialized(ssize_t size) { return ByteBuffer(ByteBufferImpl::create_uninitialized(size)); } + static ByteBuffer create_zeroed(ssize_t size) { return ByteBuffer(ByteBufferImpl::create_zeroed(size)); } + static ByteBuffer copy(const byte* data, ssize_t size) { return ByteBuffer(ByteBufferImpl::copy(data, size)); } + static ByteBuffer wrap(byte* data, ssize_t size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); } + static ByteBuffer adopt(byte* data, ssize_t size) { return ByteBuffer(ByteBufferImpl::adopt(data, size)); } ~ByteBuffer() { clear(); } void clear() { m_impl = nullptr; } @@ -77,20 +132,86 @@ public: void grow(ssize_t size) { if (!m_impl) - m_impl = Buffer::create_uninitialized(size); + m_impl = ByteBufferImpl::create_uninitialized(size); else m_impl->grow(size); } private: - explicit ByteBuffer(RetainPtr>&& impl) + explicit ByteBuffer(RetainPtr&& impl) : m_impl(move(impl)) { } - RetainPtr> m_impl; + RetainPtr m_impl; }; +inline ByteBufferImpl::ByteBufferImpl(int size) + : m_size(size) +{ + m_data = static_cast(kmalloc(size)); + m_owned = true; +} + +inline ByteBufferImpl::ByteBufferImpl(const void* data, int size, ConstructionMode mode) + : m_size(size) +{ + ASSERT(mode == Copy); + m_data = static_cast(kmalloc(size)); + memcpy(m_data, data, size); + m_owned = true; +} + +inline ByteBufferImpl::ByteBufferImpl(void* data, ssize_t size, ConstructionMode mode) + : m_data(static_cast(data)) + , m_size(size) +{ + if (mode == Adopt) { + m_owned = true; + } else if (mode == Wrap) { + m_owned = false; + } +} + +inline void ByteBufferImpl::grow(ssize_t size) +{ + ASSERT(size > m_size); + ASSERT(m_owned); + byte* new_data = static_cast(kmalloc(size)); + memcpy(new_data, m_data, m_size); + byte* old_data = m_data; + m_data = new_data; + m_size = size; + kfree(old_data); +} + +inline Retained ByteBufferImpl::create_uninitialized(int size) +{ + return ::adopt(*new ByteBufferImpl(size)); +} + +inline Retained ByteBufferImpl::create_zeroed(int size) +{ + auto buffer = ::adopt(*new ByteBufferImpl(size)); + memset(buffer->pointer(), 0, size); + return buffer; +} + +inline Retained ByteBufferImpl::copy(const void* data, int size) +{ + return ::adopt(*new ByteBufferImpl(data, size, Copy)); +} + +inline Retained ByteBufferImpl::wrap(void* data, int size) +{ + return ::adopt(*new ByteBufferImpl(data, size, Wrap)); +} + +inline Retained ByteBufferImpl::adopt(void* data, int size) +{ + return ::adopt(*new ByteBufferImpl(data, size, Adopt)); +} + } using AK::ByteBuffer; diff --git a/Kernel/Ext2FileSystem.h b/Kernel/Ext2FileSystem.h index cbd280a29ef..48af843b5d3 100644 --- a/Kernel/Ext2FileSystem.h +++ b/Kernel/Ext2FileSystem.h @@ -2,7 +2,6 @@ #include "DiskBackedFileSystem.h" #include "UnixTypes.h" -#include #include #include "ext2_fs.h"