lib.rs 2.6 KB

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