KBuffer.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. // KBuffer: Statically sized kernel-only memory buffer.
  8. //
  9. // A KBuffer is a value-type convenience class that wraps a NonnullRefPtr<KBufferImpl>.
  10. // The memory is allocated via the global kernel-only page allocator, rather than via
  11. // kmalloc() which is what ByteBuffer/Vector/etc will use.
  12. //
  13. // This makes KBuffer a little heavier to allocate, but much better for large and/or
  14. // long-lived allocations, since they don't put all that weight and pressure on the
  15. // severely limited kmalloc heap.
  16. #include <AK/Assertions.h>
  17. #include <AK/ByteBuffer.h>
  18. #include <AK/Memory.h>
  19. #include <AK/StringView.h>
  20. #include <Kernel/Memory/MemoryManager.h>
  21. #include <Kernel/Memory/Region.h>
  22. namespace Kernel {
  23. class KBufferImpl : public RefCounted<KBufferImpl> {
  24. public:
  25. static RefPtr<KBufferImpl> try_create_with_size(size_t size, Memory::Region::Access access, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
  26. {
  27. auto region = MM.allocate_kernel_region(Memory::page_round_up(size), name, access, strategy);
  28. if (!region)
  29. return nullptr;
  30. return adopt_ref_if_nonnull(new (nothrow) KBufferImpl(region.release_nonnull(), size, strategy));
  31. }
  32. static RefPtr<KBufferImpl> try_create_with_bytes(ReadonlyBytes bytes, Memory::Region::Access access, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
  33. {
  34. auto region = MM.allocate_kernel_region(Memory::page_round_up(bytes.size()), name, access, strategy);
  35. if (!region)
  36. return nullptr;
  37. memcpy(region->vaddr().as_ptr(), bytes.data(), bytes.size());
  38. return adopt_ref_if_nonnull(new (nothrow) KBufferImpl(region.release_nonnull(), bytes.size(), strategy));
  39. }
  40. static RefPtr<KBufferImpl> create_with_size(size_t size, Memory::Region::Access access, StringView name, AllocationStrategy strategy = AllocationStrategy::Reserve)
  41. {
  42. return try_create_with_size(size, access, name, strategy);
  43. }
  44. static RefPtr<KBufferImpl> copy(const void* data, size_t size, Memory::Region::Access access, StringView name)
  45. {
  46. auto buffer = create_with_size(size, access, name, AllocationStrategy::AllocateNow);
  47. if (!buffer)
  48. return {};
  49. memcpy(buffer->data(), data, size);
  50. return buffer;
  51. }
  52. [[nodiscard]] bool expand(size_t new_capacity)
  53. {
  54. auto new_region = MM.allocate_kernel_region(Memory::page_round_up(new_capacity), m_region->name(), m_region->access(), m_allocation_strategy);
  55. if (!new_region)
  56. return false;
  57. if (m_size > 0)
  58. memcpy(new_region->vaddr().as_ptr(), data(), min(m_region->size(), m_size));
  59. m_region = new_region.release_nonnull();
  60. return true;
  61. }
  62. [[nodiscard]] u8* data() { return m_region->vaddr().as_ptr(); }
  63. [[nodiscard]] const u8* data() const { return m_region->vaddr().as_ptr(); }
  64. [[nodiscard]] size_t size() const { return m_size; }
  65. [[nodiscard]] size_t capacity() const { return m_region->size(); }
  66. void set_size(size_t size)
  67. {
  68. VERIFY(size <= capacity());
  69. m_size = size;
  70. }
  71. [[nodiscard]] Memory::Region const& region() const { return *m_region; }
  72. [[nodiscard]] Memory::Region& region() { return *m_region; }
  73. private:
  74. explicit KBufferImpl(NonnullOwnPtr<Memory::Region>&& region, size_t size, AllocationStrategy strategy)
  75. : m_size(size)
  76. , m_allocation_strategy(strategy)
  77. , m_region(move(region))
  78. {
  79. }
  80. size_t m_size { 0 };
  81. AllocationStrategy m_allocation_strategy { AllocationStrategy::Reserve };
  82. NonnullOwnPtr<Memory::Region> m_region;
  83. };
  84. class [[nodiscard]] KBuffer {
  85. public:
  86. explicit KBuffer(RefPtr<KBufferImpl>&& impl)
  87. : m_impl(move(impl))
  88. {
  89. }
  90. [[nodiscard]] static OwnPtr<KBuffer> try_create_with_size(size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
  91. {
  92. auto impl = KBufferImpl::try_create_with_size(size, access, name, strategy);
  93. if (!impl)
  94. return {};
  95. return adopt_own_if_nonnull(new (nothrow) KBuffer(impl.release_nonnull()));
  96. }
  97. [[nodiscard]] static OwnPtr<KBuffer> try_create_with_bytes(ReadonlyBytes bytes, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
  98. {
  99. auto impl = KBufferImpl::try_create_with_bytes(bytes, access, name, strategy);
  100. if (!impl)
  101. return {};
  102. return adopt_own_if_nonnull(new (nothrow) KBuffer(impl.release_nonnull()));
  103. }
  104. [[nodiscard]] static KBuffer copy(const void* data, size_t size, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer")
  105. {
  106. return KBuffer(KBufferImpl::copy(data, size, access, name));
  107. }
  108. [[nodiscard]] bool is_null() const { return !m_impl; }
  109. [[nodiscard]] u8* data() { return m_impl ? m_impl->data() : nullptr; }
  110. [[nodiscard]] const u8* data() const { return m_impl ? m_impl->data() : nullptr; }
  111. [[nodiscard]] size_t size() const { return m_impl ? m_impl->size() : 0; }
  112. [[nodiscard]] size_t capacity() const { return m_impl ? m_impl->capacity() : 0; }
  113. [[nodiscard]] void* end_pointer() { return data() + size(); }
  114. [[nodiscard]] const void* end_pointer() const { return data() + size(); }
  115. void set_size(size_t size) { m_impl->set_size(size); }
  116. [[nodiscard]] KBufferImpl& impl() { return *m_impl; }
  117. [[nodiscard]] const KBufferImpl& impl() const { return *m_impl; }
  118. [[nodiscard]] RefPtr<KBufferImpl> take_impl() { return move(m_impl); }
  119. KBuffer(const ByteBuffer& buffer, Memory::Region::Access access = Memory::Region::Access::ReadWrite, StringView name = "KBuffer")
  120. : m_impl(KBufferImpl::copy(buffer.data(), buffer.size(), access, name))
  121. {
  122. }
  123. private:
  124. RefPtr<KBufferImpl> m_impl;
  125. };
  126. }