/src/liballoc_jemalloc/lib.rs

https://gitlab.com/0072016/0072016-rusty · Rust · 124 lines · 88 code · 13 blank · 23 comment · 2 complexity · 3aa2ef01b9c6f332fabff29e02ac6ee9 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. #![crate_name = "alloc_jemalloc"]
  11. #![crate_type = "rlib"]
  12. #![no_std]
  13. #![allocator]
  14. #![unstable(feature = "alloc_jemalloc",
  15. reason = "this library is unlikely to be stabilized in its current \
  16. form or name",
  17. issue = "27783")]
  18. #![cfg_attr(not(stage0), deny(warnings))]
  19. #![feature(allocator)]
  20. #![feature(libc)]
  21. #![feature(staged_api)]
  22. extern crate libc;
  23. use libc::{c_int, c_void, size_t};
  24. // Linkage directives to pull in jemalloc and its dependencies.
  25. //
  26. // On some platforms we need to be sure to link in `pthread` which jemalloc
  27. // depends on, and specifically on android we need to also link to libgcc.
  28. // Currently jemalloc is compiled with gcc which will generate calls to
  29. // intrinsics that are libgcc specific (e.g. those intrinsics aren't present in
  30. // libcompiler-rt), so link that in to get that support.
  31. #[link(name = "jemalloc", kind = "static")]
  32. #[cfg_attr(target_os = "android", link(name = "gcc"))]
  33. #[cfg_attr(all(not(windows),
  34. not(target_os = "android"),
  35. not(target_env = "musl")),
  36. link(name = "pthread"))]
  37. extern "C" {
  38. fn je_mallocx(size: size_t, flags: c_int) -> *mut c_void;
  39. fn je_rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
  40. fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
  41. fn je_sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
  42. fn je_nallocx(size: size_t, flags: c_int) -> size_t;
  43. }
  44. // The minimum alignment guaranteed by the architecture. This value is used to
  45. // add fast paths for low alignment values. In practice, the alignment is a
  46. // constant at the call site and the branch will be optimized out.
  47. #[cfg(all(any(target_arch = "arm",
  48. target_arch = "mips",
  49. target_arch = "mipsel",
  50. target_arch = "powerpc")))]
  51. const MIN_ALIGN: usize = 8;
  52. #[cfg(all(any(target_arch = "x86",
  53. target_arch = "x86_64",
  54. target_arch = "aarch64",
  55. target_arch = "powerpc64",
  56. target_arch = "powerpc64le")))]
  57. const MIN_ALIGN: usize = 16;
  58. // MALLOCX_ALIGN(a) macro
  59. fn mallocx_align(a: usize) -> c_int {
  60. a.trailing_zeros() as c_int
  61. }
  62. fn align_to_flags(align: usize) -> c_int {
  63. if align <= MIN_ALIGN {
  64. 0
  65. } else {
  66. mallocx_align(align)
  67. }
  68. }
  69. #[no_mangle]
  70. pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
  71. let flags = align_to_flags(align);
  72. unsafe { je_mallocx(size as size_t, flags) as *mut u8 }
  73. }
  74. #[no_mangle]
  75. pub extern "C" fn __rust_reallocate(ptr: *mut u8,
  76. _old_size: usize,
  77. size: usize,
  78. align: usize)
  79. -> *mut u8 {
  80. let flags = align_to_flags(align);
  81. unsafe { je_rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8 }
  82. }
  83. #[no_mangle]
  84. pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
  85. _old_size: usize,
  86. size: usize,
  87. align: usize)
  88. -> usize {
  89. let flags = align_to_flags(align);
  90. unsafe { je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
  91. }
  92. #[no_mangle]
  93. pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
  94. let flags = align_to_flags(align);
  95. unsafe { je_sdallocx(ptr as *mut c_void, old_size as size_t, flags) }
  96. }
  97. #[no_mangle]
  98. pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
  99. let flags = align_to_flags(align);
  100. unsafe { je_nallocx(size as size_t, flags) as usize }
  101. }
  102. // These symbols are used by jemalloc on android but the really old android
  103. // we're building on doesn't have them defined, so just make sure the symbols
  104. // are available.
  105. #[no_mangle]
  106. #[cfg(target_os = "android")]
  107. pub extern fn pthread_atfork(_prefork: *mut u8,
  108. _postfork_parent: *mut u8,
  109. _postfork_child: *mut u8) -> i32 {
  110. 0
  111. }