lib.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //! **Ralloc:** The memory efficient allocator.
  2. //!
  3. //! This crates define the user space allocator for Redox, which emphasizes performance and memory
  4. //! efficiency.
  5. //!
  6. //! # Ralloc seems to reimplement everything. Why?
  7. //!
  8. //! Memory allocators cannot depend on libraries or primitives, which allocates. This is a
  9. //! relatively strong condition, which means that you are forced to rewrite primitives and make
  10. //! sure no allocation ever happens.
  11. #![cfg_attr(feature = "clippy", feature(plugin))]
  12. #![cfg_attr(feature = "clippy", plugin(clippy))]
  13. #![no_std]
  14. #![feature(alloc, allocator_api, const_fn, core_intrinsics, stmt_expr_attributes, drop_types_in_const,
  15. nonzero, optin_builtin_traits, type_ascription, thread_local, linkage,
  16. try_from, const_unsafe_cell_new, const_atomic_bool_new, const_nonzero_new,
  17. const_atomic_ptr_new)]
  18. #![warn(missing_docs, cast_precision_loss, cast_sign_loss, cast_possible_wrap,
  19. cast_possible_truncation, filter_map, if_not_else, items_after_statements,
  20. invalid_upcast_comparisons, mutex_integer, nonminimal_bool, shadow_same, shadow_unrelated,
  21. single_match_else, string_add, string_add_assign, wrong_pub_self_convention)]
  22. extern crate alloc;
  23. extern crate ralloc_shim as shim;
  24. #[macro_use]
  25. mod log;
  26. #[macro_use]
  27. #[cfg(feature = "tls")]
  28. mod tls;
  29. #[macro_use]
  30. mod unborrow;
  31. mod allocator;
  32. mod block;
  33. mod bookkeeper;
  34. mod brk;
  35. mod cell;
  36. mod fail;
  37. mod lazy_init;
  38. mod leak;
  39. mod prelude;
  40. mod ptr;
  41. mod sync;
  42. mod vec;
  43. use alloc::heap::{Alloc, AllocErr, Layout, CannotReallocInPlace};
  44. pub use allocator::{alloc, free, realloc, realloc_inplace};
  45. pub use brk::sbrk;
  46. pub use fail::set_oom_handler;
  47. #[cfg(feature = "tls")]
  48. pub use fail::set_thread_oom_handler;
  49. pub struct Allocator;
  50. unsafe impl<'a> Alloc for &'a Allocator {
  51. unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
  52. Ok(allocator::alloc(layout.size(), layout.align()))
  53. }
  54. unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
  55. allocator::free(ptr, layout.size());
  56. }
  57. unsafe fn realloc(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<*mut u8, AllocErr> {
  58. Ok(allocator::realloc(ptr, layout.size(), new_layout.size(), new_layout.align()))
  59. }
  60. unsafe fn grow_in_place(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> {
  61. if allocator::realloc_inplace(ptr, layout.size(), new_layout.size()).is_ok() {
  62. Ok(())
  63. } else {
  64. Err(CannotReallocInPlace)
  65. }
  66. }
  67. unsafe fn shrink_in_place(&mut self, ptr: *mut u8, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> {
  68. if allocator::realloc_inplace(ptr, layout.size(), new_layout.size()).is_ok() {
  69. Ok(())
  70. } else {
  71. Err(CannotReallocInPlace)
  72. }
  73. }
  74. fn usable_size(&self, layout: &Layout) -> (usize, usize) {
  75. // Yay! It matches exactly.
  76. (layout.size(), layout.size())
  77. }
  78. }