KBuffer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #pragma once
  27. // KBuffer: Statically sized kernel-only memory buffer.
  28. //
  29. // A KBuffer is a value-type convenience class that wraps a NonnullRefPtr<KBufferImpl>.
  30. // The memory is allocated via the global kernel-only page allocator, rather than via
  31. // kmalloc() which is what ByteBuffer/Vector/etc will use.
  32. //
  33. // This makes KBuffer a little heavier to allocate, but much better for large and/or
  34. // long-lived allocations, since they don't put all that weight and pressure on the
  35. // severely limited kmalloc heap.
  36. #include <AK/Assertions.h>
  37. #include <AK/ByteBuffer.h>
  38. #include <AK/LogStream.h>
  39. #include <AK/Memory.h>
  40. #include <AK/StringView.h>
  41. #include <Kernel/VM/MemoryManager.h>
  42. #include <Kernel/VM/Region.h>
  43. namespace Kernel {
  44. class KBufferImpl : public RefCounted<KBufferImpl> {
  45. public:
  46. static NonnullRefPtr<KBufferImpl> create_with_size(size_t size, u8 access, const char* name)
  47. {
  48. auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(size), name, access, false, false);
  49. ASSERT(region);
  50. return adopt(*new KBufferImpl(region.release_nonnull(), size));
  51. }
  52. static NonnullRefPtr<KBufferImpl> copy(const void* data, size_t size, u8 access, const char* name)
  53. {
  54. auto buffer = create_with_size(size, access, name);
  55. buffer->region().commit();
  56. memcpy(buffer->data(), data, size);
  57. return buffer;
  58. }
  59. u8* data() { return m_region->vaddr().as_ptr(); }
  60. const u8* data() const { return m_region->vaddr().as_ptr(); }
  61. size_t size() const { return m_size; }
  62. size_t capacity() const { return m_region->size(); }
  63. void set_size(size_t size)
  64. {
  65. ASSERT(size <= capacity());
  66. m_size = size;
  67. }
  68. const Region& region() const { return *m_region; }
  69. Region& region() { return *m_region; }
  70. private:
  71. explicit KBufferImpl(NonnullOwnPtr<Region>&& region, size_t size)
  72. : m_size(size)
  73. , m_region(move(region))
  74. {
  75. }
  76. size_t m_size { 0 };
  77. NonnullOwnPtr<Region> m_region;
  78. };
  79. class KBuffer {
  80. public:
  81. static KBuffer create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer")
  82. {
  83. return KBuffer(KBufferImpl::create_with_size(size, access, name));
  84. }
  85. static KBuffer copy(const void* data, size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer")
  86. {
  87. return KBuffer(KBufferImpl::copy(data, size, access, name));
  88. }
  89. u8* data() { return m_impl->data(); }
  90. const u8* data() const { return m_impl->data(); }
  91. size_t size() const { return m_impl->size(); }
  92. size_t capacity() const { return m_impl->capacity(); }
  93. void* end_pointer() { return data() + size(); }
  94. const void* end_pointer() const { return data() + size(); }
  95. void set_size(size_t size) { m_impl->set_size(size); }
  96. const KBufferImpl& impl() const { return m_impl; }
  97. KBuffer(const ByteBuffer& buffer, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer")
  98. : m_impl(KBufferImpl::copy(buffer.data(), buffer.size(), access, name))
  99. {
  100. }
  101. private:
  102. explicit KBuffer(NonnullRefPtr<KBufferImpl>&& impl)
  103. : m_impl(move(impl))
  104. {
  105. }
  106. NonnullRefPtr<KBufferImpl> m_impl;
  107. };
  108. inline const LogStream& operator<<(const LogStream& stream, const KBuffer& value)
  109. {
  110. return stream << StringView(value.data(), value.size());
  111. }
  112. }