/rust/liballoc_system/lib.rs

https://gitlab.com/aw1231/redox · Rust · 85 lines · 61 code · 12 blank · 12 comment · 0 complexity · a7e5a8628f72e150b38e2342fb757297 MD5 · raw file

  1. // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. #![cfg_attr(stage0, feature(custom_attribute))]
  11. #![crate_name = "alloc_system"]
  12. #![crate_type = "rlib"]
  13. #![staged_api]
  14. #![no_std]
  15. #![cfg_attr(not(stage0), allocator)]
  16. #![unstable(feature = "alloc_system",
  17. reason = "this library is unlikely to be stabilized in its current \
  18. form or name",
  19. issue = "27783")]
  20. #![feature(allocator)]
  21. #![feature(asm)]
  22. #![feature(no_std)]
  23. #![feature(staged_api)]
  24. // The minimum alignment guaranteed by the architecture. This value is used to
  25. // add fast paths for low alignment values. In practice, the alignment is a
  26. // constant at the call site and the branch will be optimized out.
  27. #[cfg(all(any(target_arch = "arm",
  28. target_arch = "mips",
  29. target_arch = "mipsel",
  30. target_arch = "powerpc")))]
  31. const MIN_ALIGN: usize = 8;
  32. #[cfg(all(any(target_arch = "x86",
  33. target_arch = "x86_64",
  34. target_arch = "aarch64")))]
  35. const MIN_ALIGN: usize = 16;
  36. extern {
  37. fn memmove(dst: *mut u8, src: *const u8, size: usize);
  38. }
  39. use syscall::common::*;
  40. #[path="../../src/syscall/"]
  41. mod syscall {
  42. pub mod common;
  43. }
  44. unsafe fn syscall(mut eax: u32, ebx: u32, ecx: u32, edx: u32) -> u32 {
  45. asm!("int 0x80"
  46. : "={eax}"(eax)
  47. : "{eax}"(eax), "{ebx}"(ebx), "{ecx}"(ecx), "{edx}"(edx)
  48. : "memory"
  49. : "intel", "volatile");
  50. return eax;
  51. }
  52. #[no_mangle]
  53. pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
  54. unsafe { syscall(SYS_ALLOC, size as u32, 0, 0) as *mut u8 }
  55. }
  56. #[no_mangle]
  57. pub extern fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
  58. unsafe { syscall(SYS_UNALLOC, ptr as u32, 0, 0); }
  59. }
  60. #[no_mangle]
  61. pub extern fn __rust_reallocate(ptr: *mut u8, old_size: usize, size: usize,
  62. align: usize) -> *mut u8 {
  63. unsafe { syscall(SYS_REALLOC, ptr as u32, size as u32, 0) as *mut u8 }
  64. }
  65. #[no_mangle]
  66. pub extern fn __rust_reallocate_inplace(ptr: *mut u8, old_size: usize,
  67. size: usize, align: usize) -> usize {
  68. unsafe { syscall(SYS_REALLOC_INPLACE, ptr as u32, size as u32, 0) as usize }
  69. }
  70. #[no_mangle]
  71. pub extern fn __rust_usable_size(size: usize, align: usize) -> usize {
  72. size
  73. }