mmtk/util/malloc/
library.rs

1// Export one of the malloc libraries.
2
3#[cfg(feature = "malloc_jemalloc")]
4pub use self::jemalloc::*;
5#[cfg(not(any(feature = "malloc_jemalloc", feature = "malloc_mimalloc",)))]
6pub use self::libc_malloc::*;
7#[cfg(feature = "malloc_mimalloc")]
8pub use self::mimalloc::*;
9
10/// When we count page usage of library malloc, we assume they allocate in pages. For some malloc implementations,
11/// they may use a larger page (e.g. mimalloc's 64K page). For libraries that we are not sure, we assume they use
12/// normal 4k pages.
13pub const BYTES_IN_MALLOC_PAGE: usize = 1 << LOG_BYTES_IN_MALLOC_PAGE;
14
15// Different malloc libraries
16
17// TODO: We should conditinally include some methods in the module, such as posix extension and GNU extension.
18
19#[cfg(feature = "malloc_jemalloc")]
20mod jemalloc {
21    // Normal 4K page
22    pub const LOG_BYTES_IN_MALLOC_PAGE: u8 = crate::util::constants::LOG_BYTES_IN_PAGE;
23    // ANSI C
24    pub use jemalloc_sys::{calloc, free, malloc, realloc};
25    // Posix
26    pub use jemalloc_sys::posix_memalign;
27    // GNU
28    pub use jemalloc_sys::malloc_usable_size;
29}
30
31#[cfg(feature = "malloc_mimalloc")]
32mod mimalloc {
33    // Normal 4K page accounting
34    pub const LOG_BYTES_IN_MALLOC_PAGE: u8 = crate::util::constants::LOG_BYTES_IN_PAGE;
35    // ANSI C
36    pub use mimalloc_sys::{
37        mi_calloc as calloc, mi_free as free, mi_malloc as malloc, mi_realloc as realloc,
38    };
39    // Posix
40    pub use mimalloc_sys::mi_posix_memalign as posix_memalign;
41    // GNU
42    pub use mimalloc_sys::mi_malloc_usable_size as malloc_usable_size;
43}
44
45/// If no malloc lib is specified, use the libc implementation
46#[cfg(not(any(feature = "malloc_jemalloc", feature = "malloc_mimalloc",)))]
47mod libc_malloc {
48    // Normal 4K page
49    pub const LOG_BYTES_IN_MALLOC_PAGE: u8 = crate::util::constants::LOG_BYTES_IN_PAGE;
50    // ANSI C
51    pub use libc::{calloc, free, malloc, realloc};
52    // Posix
53    pub use libc::posix_memalign;
54    // GNU
55    #[cfg(target_os = "linux")]
56    pub use libc::malloc_usable_size;
57    #[cfg(target_os = "macos")]
58    extern "C" {
59        pub fn malloc_size(ptr: *const libc::c_void) -> usize;
60    }
61    #[cfg(target_os = "macos")]
62    pub use self::malloc_size as malloc_usable_size;
63}