lib.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //! A slab allocator implementation for objects less than a page-size (4 KiB or 2MiB).
  2. //!
  3. //! # Overview
  4. //!
  5. //! The organization is as follows:
  6. //!
  7. //! * A `ZoneAllocator` manages many `SCAllocator` and can
  8. //! satisfy requests for different allocation sizes.
  9. //! * A `SCAllocator` allocates objects of exactly one size.
  10. //! It stores the objects and meta-data in one or multiple `AllocablePage` objects.
  11. //! * A trait `AllocablePage` that defines the page-type from which we allocate objects.
  12. //!
  13. //! Lastly, it provides two default `AllocablePage` implementations `ObjectPage` and `LargeObjectPage`:
  14. //! * A `ObjectPage` that is 4 KiB in size and contains allocated objects and associated meta-data.
  15. //! * A `LargeObjectPage` that is 2 MiB in size and contains allocated objects and associated meta-data.
  16. //!
  17. //!
  18. //! # Implementing GlobalAlloc
  19. //! See the [global alloc](https://github.com/gz/rust-slabmalloc/tree/master/examples/global_alloc.rs) example.
  20. #![allow(unused_features)]
  21. #![cfg_attr(test, feature(test, c_void_variant))]
  22. #![no_std]
  23. #![crate_name = "slabmalloc"]
  24. #![crate_type = "lib"]
  25. #![feature(maybe_uninit_as_bytes)]
  26. #![deny(clippy::all)]
  27. #![allow(clippy::needless_return)]
  28. extern crate alloc;
  29. mod pages;
  30. mod sc;
  31. mod zone;
  32. pub use pages::*;
  33. pub use sc::*;
  34. pub use zone::*;
  35. #[cfg(test)]
  36. #[macro_use]
  37. extern crate std;
  38. #[cfg(test)]
  39. extern crate test;
  40. #[cfg(test)]
  41. mod tests;
  42. use core::alloc::Layout;
  43. use core::fmt;
  44. use core::mem;
  45. use core::ptr::{self, NonNull};
  46. use log::trace;
  47. /// How many bytes in the page are used by allocator meta-data.
  48. const OBJECT_PAGE_METADATA_OVERHEAD: usize = 80;
  49. /// How many bytes a [`ObjectPage`] is.
  50. const OBJECT_PAGE_SIZE: usize = 4096;
  51. type VAddr = usize;
  52. /// Error that can be returned for `allocation` and `deallocation` requests.
  53. #[derive(Debug)]
  54. pub enum AllocationError {
  55. /// Can't satisfy the allocation request for Layout because the allocator
  56. /// does not have enough memory (you may be able to `refill` it).
  57. OutOfMemory,
  58. /// Allocator can't deal with the provided size of the Layout.
  59. InvalidLayout,
  60. }
  61. /// Allocator trait to be implemented by users of slabmalloc to provide memory to slabmalloc.
  62. ///
  63. /// # Safety
  64. /// Needs to adhere to safety requirements of a rust allocator (see GlobalAlloc et. al.).
  65. pub unsafe trait Allocator<'a> {
  66. fn allocate(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocationError>;
  67. /// # Safety
  68. /// The caller must ensure that the memory is valid and that the layout is correct.
  69. unsafe fn deallocate(
  70. &mut self,
  71. ptr: NonNull<u8>,
  72. layout: Layout,
  73. slab_callback: &'static dyn CallBack,
  74. ) -> Result<(), AllocationError>;
  75. /// Refill the allocator with a [`ObjectPage`].
  76. ///
  77. /// # Safety
  78. /// TBD (this API needs to change anyways, likely new page should be a raw pointer)
  79. unsafe fn refill(
  80. &mut self,
  81. layout: Layout,
  82. new_page: &'a mut ObjectPage<'a>,
  83. ) -> Result<(), AllocationError>;
  84. }
  85. /// 将slab_page归还Buddy的回调函数
  86. pub trait CallBack: Send + Sync {
  87. /// # Safety
  88. /// The caller must ensure that the memory is valid and that the size is correct.
  89. unsafe fn free_slab_page(&self, _: *mut u8, _: usize) {}
  90. }