kmalloc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <Kernel/Debug.h>
  9. #include <LibC/limits.h>
  10. #define KMALLOC_SCRUB_BYTE 0xbb
  11. #define KFREE_SCRUB_BYTE 0xaa
  12. #define MAKE_ALIGNED_ALLOCATED(type, alignment) \
  13. public: \
  14. [[nodiscard]] void* operator new(size_t) \
  15. { \
  16. void* ptr = kmalloc_aligned<alignment>(sizeof(type)); \
  17. VERIFY(ptr); \
  18. return ptr; \
  19. } \
  20. [[nodiscard]] void* operator new(size_t, const std::nothrow_t&) noexcept { return kmalloc_aligned<alignment>(sizeof(type)); } \
  21. void operator delete(void* ptr) noexcept { kfree_aligned(ptr); } \
  22. \
  23. private:
  24. // The C++ standard specifies that the nothrow allocation tag should live in the std namespace.
  25. // Otherwise, `new (std::nothrow)` calls wouldn't get resolved.
  26. namespace std {
  27. struct nothrow_t {
  28. explicit nothrow_t() = default;
  29. };
  30. extern const nothrow_t nothrow;
  31. enum class align_val_t : size_t {};
  32. };
  33. void kmalloc_init();
  34. [[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] void* kmalloc_impl(size_t);
  35. [[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] void* kmalloc_eternal(size_t);
  36. void kfree(void*);
  37. void kfree_sized(void*, size_t);
  38. struct kmalloc_stats {
  39. size_t bytes_allocated;
  40. size_t bytes_free;
  41. size_t bytes_eternal;
  42. size_t kmalloc_call_count;
  43. size_t kfree_call_count;
  44. };
  45. void get_kmalloc_stats(kmalloc_stats&);
  46. extern bool g_dump_kmalloc_stacks;
  47. inline void* operator new(size_t, void* p) { return p; }
  48. inline void* operator new[](size_t, void* p) { return p; }
  49. [[nodiscard]] void* operator new(size_t size);
  50. [[nodiscard]] void* operator new(size_t size, const std::nothrow_t&) noexcept;
  51. [[nodiscard]] void* operator new(size_t size, std::align_val_t);
  52. [[nodiscard]] void* operator new(size_t size, std::align_val_t, const std::nothrow_t&) noexcept;
  53. void operator delete(void* ptr) noexcept DISALLOW("All deletes in the kernel should have a known size.");
  54. void operator delete(void* ptr, size_t) noexcept;
  55. void operator delete(void* ptr, std::align_val_t) noexcept DISALLOW("All deletes in the kernel should have a known size.");
  56. void operator delete(void* ptr, size_t, std::align_val_t) noexcept;
  57. [[nodiscard]] void* operator new[](size_t size);
  58. [[nodiscard]] void* operator new[](size_t size, const std::nothrow_t&) noexcept;
  59. void operator delete[](void* ptrs) noexcept DISALLOW("All deletes in the kernel should have a known size.");
  60. void operator delete[](void* ptr, size_t) noexcept;
  61. [[gnu::malloc, gnu::alloc_size(1)]] void* kmalloc(size_t);
  62. template<size_t ALIGNMENT>
  63. [[gnu::malloc, gnu::alloc_size(1)]] inline void* kmalloc_aligned(size_t size)
  64. {
  65. static_assert(ALIGNMENT > sizeof(ptrdiff_t));
  66. static_assert(ALIGNMENT <= 4096);
  67. void* ptr = kmalloc(size + ALIGNMENT + sizeof(ptrdiff_t));
  68. if (ptr == nullptr)
  69. return ptr;
  70. size_t max_addr = (size_t)ptr + ALIGNMENT;
  71. void* aligned_ptr = (void*)(max_addr - (max_addr % ALIGNMENT));
  72. ((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
  73. return aligned_ptr;
  74. }
  75. inline void kfree_aligned(void* ptr)
  76. {
  77. if (ptr == nullptr)
  78. return;
  79. kfree((u8*)ptr - ((const ptrdiff_t*)ptr)[-1]);
  80. }
  81. size_t kmalloc_good_size(size_t);
  82. void kmalloc_enable_expand();