lib.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #![no_std]
  22. #![crate_name = "slabmalloc"]
  23. #![crate_type = "lib"]
  24. #![feature(maybe_uninit_as_bytes)]
  25. extern crate alloc;
  26. mod pages;
  27. mod sc;
  28. mod zone;
  29. pub use pages::*;
  30. pub use sc::*;
  31. pub use zone::*;
  32. use core::alloc::Layout;
  33. use core::fmt;
  34. use core::ptr::{self, NonNull};
  35. use log::trace;
  36. /// How many bytes in the page are used by allocator meta-data.
  37. const OBJECT_PAGE_METADATA_OVERHEAD: usize = 80;
  38. /// How many bytes a [`ObjectPage`] is.
  39. const OBJECT_PAGE_SIZE: usize = 4096;
  40. type VAddr = usize;
  41. /// Error that can be returned for `allocation` and `deallocation` requests.
  42. #[derive(Debug)]
  43. pub enum AllocationError {
  44. /// Can't satisfy the allocation request for Layout because the allocator
  45. /// does not have enough memory (you may be able to `refill` it).
  46. OutOfMemory,
  47. /// Allocator can't deal with the provided size of the Layout.
  48. InvalidLayout,
  49. }
  50. /// Allocator trait to be implemented by users of slabmalloc to provide memory to slabmalloc.
  51. ///
  52. /// # Safety
  53. /// Needs to adhere to safety requirements of a rust allocator (see GlobalAlloc et. al.).
  54. pub unsafe trait Allocator<'a> {
  55. fn allocate(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocationError>;
  56. /// # Safety
  57. /// The caller must ensure that the memory is valid and that the layout is correct.
  58. unsafe fn deallocate(
  59. &mut self,
  60. ptr: NonNull<u8>,
  61. layout: Layout,
  62. slab_callback: &'static dyn CallBack,
  63. ) -> Result<(), AllocationError>;
  64. /// Refill the allocator with a [`ObjectPage`].
  65. ///
  66. /// # Safety
  67. /// TBD (this API needs to change anyways, likely new page should be a raw pointer)
  68. unsafe fn refill(
  69. &mut self,
  70. layout: Layout,
  71. new_page: &'a mut ObjectPage<'a>,
  72. ) -> Result<(), AllocationError>;
  73. }
  74. /// 将slab_page归还Buddy的回调函数
  75. pub trait CallBack: Send + Sync {
  76. /// # Safety
  77. /// The caller must ensure that the memory is valid and that the size is correct.
  78. unsafe fn free_slab_page(&self, _: *mut u8, _: usize) {}
  79. }