2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-09-07 17:19:47 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-08-04 22:04:27 +03:00
|
|
|
#pragma once
|
|
|
|
|
2021-09-07 17:19:47 +03:00
|
|
|
// KBuffer: Memory buffer backed by a kernel region.
|
2019-08-05 12:11:00 +03:00
|
|
|
//
|
|
|
|
// The memory is allocated via the global kernel-only page allocator, rather than via
|
|
|
|
// kmalloc() which is what ByteBuffer/Vector/etc will use.
|
|
|
|
//
|
|
|
|
// This makes KBuffer a little heavier to allocate, but much better for large and/or
|
|
|
|
// long-lived allocations, since they don't put all that weight and pressure on the
|
|
|
|
// severely limited kmalloc heap.
|
|
|
|
|
2019-08-04 22:04:27 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-03-08 14:33:14 +03:00
|
|
|
#include <AK/Memory.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2019-08-04 22:04:27 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-09-07 17:19:47 +03:00
|
|
|
class [[nodiscard]] KBuffer {
|
2019-08-04 22:04:27 +03:00
|
|
|
public:
|
2022-04-11 01:08:07 +03:00
|
|
|
static ErrorOr<NonnullOwnPtr<KBuffer>> try_create_with_size(StringView name, size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, AllocationStrategy strategy = AllocationStrategy::Reserve)
|
2019-08-04 22:04:27 +03:00
|
|
|
{
|
2021-12-24 17:22:11 +03:00
|
|
|
auto rounded_size = TRY(Memory::page_round_up(size));
|
|
|
|
auto region = TRY(MM.allocate_kernel_region(rounded_size, name, access, strategy));
|
2021-09-07 17:19:47 +03:00
|
|
|
return TRY(adopt_nonnull_own_or_enomem(new (nothrow) KBuffer { size, move(region) }));
|
2019-08-04 22:04:27 +03:00
|
|
|
}
|
|
|
|
|
2022-04-11 01:08:07 +03:00
|
|
|
static ErrorOr<NonnullOwnPtr<KBuffer>> try_create_with_bytes(StringView name, ReadonlyBytes bytes, Memory::Region::Access access = Memory::Region::Access::ReadWrite, AllocationStrategy strategy = AllocationStrategy::Reserve)
|
2020-12-18 16:09:14 +03:00
|
|
|
{
|
2022-04-11 01:08:07 +03:00
|
|
|
auto buffer = TRY(try_create_with_size(name, bytes.size(), access, strategy));
|
2021-09-07 17:19:47 +03:00
|
|
|
memcpy(buffer->data(), bytes.data(), bytes.size());
|
|
|
|
return buffer;
|
2020-12-18 16:09:14 +03:00
|
|
|
}
|
|
|
|
|
2021-02-15 02:25:18 +03:00
|
|
|
[[nodiscard]] u8* data() { return m_region->vaddr().as_ptr(); }
|
2021-09-07 17:19:47 +03:00
|
|
|
[[nodiscard]] u8 const* data() const { return m_region->vaddr().as_ptr(); }
|
2021-02-15 02:25:18 +03:00
|
|
|
[[nodiscard]] size_t size() const { return m_size; }
|
|
|
|
[[nodiscard]] size_t capacity() const { return m_region->size(); }
|
2019-08-04 22:04:27 +03:00
|
|
|
|
2021-09-08 19:29:52 +03:00
|
|
|
[[nodiscard]] ReadonlyBytes bytes() const { return { data(), size() }; }
|
|
|
|
[[nodiscard]] Bytes bytes() { return { data(), size() }; }
|
|
|
|
|
2019-08-06 08:31:52 +03:00
|
|
|
void set_size(size_t size)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(size <= capacity());
|
2019-08-06 08:31:52 +03:00
|
|
|
m_size = size;
|
|
|
|
}
|
|
|
|
|
2019-08-04 22:04:27 +03:00
|
|
|
private:
|
2021-09-07 17:19:47 +03:00
|
|
|
explicit KBuffer(size_t size, NonnullOwnPtr<Memory::Region> region)
|
2019-08-04 22:04:27 +03:00
|
|
|
: m_size(size)
|
|
|
|
, m_region(move(region))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t m_size { 0 };
|
2021-08-06 14:49:36 +03:00
|
|
|
NonnullOwnPtr<Memory::Region> m_region;
|
2019-08-04 22:04:27 +03:00
|
|
|
};
|
2019-08-05 12:06:21 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|