Serenity Operating System
at hosted 131 lines 4.7 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29// KBuffer: Statically sized kernel-only memory buffer. 30// 31// A KBuffer is a value-type convenience class that wraps a NonnullRefPtr<KBufferImpl>. 32// The memory is allocated via the global kernel-only page allocator, rather than via 33// kmalloc() which is what ByteBuffer/Vector/etc will use. 34// 35// This makes KBuffer a little heavier to allocate, but much better for large and/or 36// long-lived allocations, since they don't put all that weight and pressure on the 37// severely limited kmalloc heap. 38 39#include <AK/Assertions.h> 40#include <AK/ByteBuffer.h> 41#include <AK/LogStream.h> 42#include <AK/Memory.h> 43#include <AK/StringView.h> 44#include <Kernel/VM/MemoryManager.h> 45#include <Kernel/VM/Region.h> 46 47namespace Kernel { 48 49class KBufferImpl : public RefCounted<KBufferImpl> { 50public: 51 static NonnullRefPtr<KBufferImpl> create_with_size(size_t size, u8 access, const char* name) 52 { 53 auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(size), name, access, false, false); 54 ASSERT(region); 55 return adopt(*new KBufferImpl(region.release_nonnull(), size)); 56 } 57 58 static NonnullRefPtr<KBufferImpl> copy(const void* data, size_t size, u8 access, const char* name) 59 { 60 auto buffer = create_with_size(size, access, name); 61 buffer->region().commit(); 62 memcpy(buffer->data(), data, size); 63 return buffer; 64 } 65 66 u8* data() { return m_region->vaddr().as_ptr(); } 67 const u8* data() const { return m_region->vaddr().as_ptr(); } 68 size_t size() const { return m_size; } 69 size_t capacity() const { return m_region->size(); } 70 71 void set_size(size_t size) 72 { 73 ASSERT(size <= capacity()); 74 m_size = size; 75 } 76 77 const Region& region() const { return *m_region; } 78 Region& region() { return *m_region; } 79 80private: 81 explicit KBufferImpl(NonnullOwnPtr<Region>&& region, size_t size) 82 : m_size(size) 83 , m_region(move(region)) 84 { 85 } 86 87 size_t m_size { 0 }; 88 NonnullOwnPtr<Region> m_region; 89}; 90 91class KBuffer { 92public: 93 static KBuffer create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") 94 { 95 return KBuffer(KBufferImpl::create_with_size(size, access, name)); 96 } 97 98 static KBuffer copy(const void* data, size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") 99 { 100 return KBuffer(KBufferImpl::copy(data, size, access, name)); 101 } 102 103 u8* data() { return m_impl->data(); } 104 const u8* data() const { return m_impl->data(); } 105 size_t size() const { return m_impl->size(); } 106 size_t capacity() const { return m_impl->capacity(); } 107 108 void set_size(size_t size) { m_impl->set_size(size); } 109 110 const KBufferImpl& impl() const { return m_impl; } 111 112 KBuffer(const ByteBuffer& buffer, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") 113 : m_impl(KBufferImpl::copy(buffer.data(), buffer.size(), access, name)) 114 { 115 } 116 117private: 118 explicit KBuffer(NonnullRefPtr<KBufferImpl>&& impl) 119 : m_impl(move(impl)) 120 { 121 } 122 123 NonnullRefPtr<KBufferImpl> m_impl; 124}; 125 126inline const LogStream& operator<<(const LogStream& stream, const KBuffer& value) 127{ 128 return stream << StringView(value.data(), value.size()); 129} 130 131}