Skip to content

Commit 9ccb725

Browse files
authored
add libc api feature (#198)
1 parent ed3a6c9 commit 9ccb725

7 files changed

Lines changed: 54 additions & 21 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
target
22
Cargo.lock
33
.idea
4+
5+
# vscode dirs
6+
.vscode/
7+
.vs/

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ lto = ["snmalloc-sys/lto"]
3333
notls = ["snmalloc-sys/notls"]
3434
stats = ["snmalloc-sys/stats"]
3535
usewait-on-address = ["snmalloc-sys/usewait-on-address"]
36+
libc-api = ["snmalloc-sys/libc-api"]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ are listed at
3939
- `lto`: Links with InterProceduralOptimization/LinkTimeOptimization
4040
- `notls`: Enables to be loaded dynamically, thus disable tls.
4141
- `stats`: Enables allocation statistics.
42+
- `libc-api`: Enables libc API backed by snmalloc.
4243

4344
**To get the crates compiled, you need to choose either `1mib` or `16mib` to determine the chunk configuration**
4445

snmalloc-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ lto = []
3232
notls = []
3333
stats = []
3434
usewait-on-address = []
35+
libc-api = []

snmalloc-sys/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct BuildFeatures {
6060
stats: bool,
6161
android_lld: bool,
6262
local_dynamic_tls: bool,
63+
libc_api: bool,
6364
}
6465

6566
impl BuildConfig {
@@ -277,6 +278,7 @@ impl BuildFeatures {
277278
stats: cfg!(feature = "stats"),
278279
android_lld: cfg!(feature = "android-lld"),
279280
local_dynamic_tls: cfg!(feature = "local_dynamic_tls"),
281+
libc_api: cfg!(feature = "libc-api"),
280282
}
281283
}
282284
}
@@ -378,7 +380,8 @@ fn configure_platform(config: &mut BuildConfig) {
378380
.define("SNMALLOC_QEMU_WORKAROUND", if config.features.qemu { "ON" } else { "OFF" })
379381
.define("SNMALLOC_ENABLE_DYNAMIC_LOADING", if config.features.notls { "ON" } else { "OFF" })
380382
.define("SNMALLOC_USE_WAIT_ON_ADDRESS", if config.features.wait_on_address { "1" } else { "0" })
381-
.define("USE_SNMALLOC_STATS", if config.features.stats { "ON" } else { "OFF" });
383+
.define("USE_SNMALLOC_STATS", if config.features.stats { "ON" } else { "OFF" })
384+
.define("SNMALLOC_RUST_LIBC_API", if config.features.libc_api { "ON" } else { "OFF" });
382385

383386
// Android configuration
384387
if config.target.contains("android") {

snmalloc-sys/snmalloc

Submodule snmalloc updated 80 files

snmalloc-sys/src/lib.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,21 @@ extern "C" {
3939
new_size: usize,
4040
) -> *mut c_void;
4141

42+
/// Return the available bytes in a memory block.
43+
pub fn sn_rust_usable_size(p: *const c_void) -> usize;
44+
}
45+
46+
#[cfg(feature = "libc-api")]
47+
extern "C" {
4248
/// Allocate `count` items of `size` length each.
4349
/// Returns `null` if `count * size` overflows or on out-of-memory.
4450
/// All items are initialized to zero.
45-
pub fn calloc(count: usize, size: usize) -> *mut c_void;
51+
pub fn sn_calloc(count: usize, size: usize) -> *mut c_void;
4652

4753
/// Allocate `size` bytes.
4854
/// Returns pointer to the allocated memory or null if out of memory.
4955
/// Returns a unique pointer if called with `size` 0.
50-
pub fn malloc(size: usize) -> *mut c_void;
56+
pub fn sn_malloc(size: usize) -> *mut c_void;
5157

5258
/// Re-allocate memory to `newsize` bytes.
5359
/// Return pointer to the allocated memory or null if out of memory. If null
@@ -57,18 +63,19 @@ extern "C" {
5763
/// If `p` is null, it behaves as [`sn_malloc`]. If `newsize` is larger than
5864
/// the original `size` allocated for `p`, the bytes after `size` are
5965
/// uninitialized.
60-
pub fn realloc(p: *mut c_void, newsize: usize) -> *mut c_void;
66+
pub fn sn_realloc(p: *mut c_void, newsize: usize) -> *mut c_void;
6167

6268
/// Free previously allocated memory.
6369
/// The pointer `p` must have been allocated before (or be null).
64-
pub fn free(p: *mut c_void);
70+
pub fn sn_free(p: *mut c_void);
6571

6672
/// Return the available bytes in a memory block.
67-
pub fn sn_rust_usable_size(p: *const c_void) -> usize;
73+
pub fn sn_malloc_usable_size(p: *const c_void) -> usize;
74+
6875
}
6976

7077
#[cfg(test)]
71-
mod tests {
78+
mod rust_tests {
7279
use super::*;
7380

7481
#[test]
@@ -90,19 +97,6 @@ mod tests {
9097
unsafe { sn_rust_dealloc(ptr as *mut c_void, 8, 8) };
9198
}
9299

93-
#[test]
94-
fn it_frees_memory_sn_malloc() {
95-
let ptr = unsafe { malloc(8) } as *mut u8;
96-
unsafe { free(ptr as *mut c_void) };
97-
}
98-
99-
#[test]
100-
fn it_frees_memory_sn_realloc() {
101-
let ptr = unsafe { malloc(8) } as *mut u8;
102-
let ptr = unsafe { realloc(ptr as *mut c_void, 8) } as *mut u8;
103-
unsafe { free(ptr as *mut c_void) };
104-
}
105-
106100
#[test]
107101
fn it_reallocs_correctly() {
108102
let mut ptr = unsafe { sn_rust_alloc(8, 8) } as *mut u8;
@@ -126,3 +120,32 @@ mod tests {
126120
unsafe { sn_rust_dealloc(ptr as *mut c_void, 32, 8) };
127121
}
128122
}
123+
124+
#[cfg(all(test, feature = "libc-api"))]
125+
mod libc_tests {
126+
use super::*;
127+
128+
#[test]
129+
fn it_frees_memory_sn_malloc() {
130+
let ptr = unsafe { sn_malloc(8) } as *mut u8;
131+
unsafe { sn_free(ptr as *mut c_void) };
132+
}
133+
134+
#[test]
135+
fn it_frees_memory_sn_realloc() {
136+
let ptr = unsafe { sn_malloc(8) } as *mut u8;
137+
let ptr = unsafe { sn_realloc(ptr as *mut c_void, 8) } as *mut u8;
138+
unsafe { sn_free(ptr as *mut c_void) };
139+
}
140+
141+
#[test]
142+
fn it_calculates_malloc_usable_size() {
143+
let ptr = unsafe { sn_malloc(32) } as *mut u8;
144+
let usable_size = unsafe { sn_malloc_usable_size(ptr as *mut c_void) };
145+
assert!(
146+
usable_size >= 32,
147+
"usable_size should at least equal to the allocated size"
148+
);
149+
unsafe { sn_free(ptr as *mut c_void) };
150+
}
151+
}

0 commit comments

Comments
 (0)