Skip to content

Commit 740d3df

Browse files
Optimized nstd.alloc for Unix-like systems.
1 parent ff3db11 commit 740d3df

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ default = ["std", "nstd_core"]
2525
std = []
2626
clib = []
2727
asm = []
28-
nstd_alloc = ["nstd_os_windows_alloc"]
28+
nstd_alloc = ["nstd_os_unix_alloc", "nstd_os_windows_alloc"]
2929
nstd_core = []
3030
nstd_cstring = ["nstd_alloc", "nstd_core", "nstd_vec"]
3131
nstd_fs = ["nstd_core", "nstd_io", "nstd_string", "nstd_vec", "std"]

src/alloc.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//! Low level memory allocation.
2-
#[cfg(not(target_os = "windows"))]
2+
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
33
extern crate alloc;
4+
#[cfg(target_family = "unix")]
5+
use crate::os::unix::alloc::{
6+
nstd_os_unix_alloc_allocate, nstd_os_unix_alloc_allocate_zeroed, nstd_os_unix_alloc_deallocate,
7+
nstd_os_unix_alloc_reallocate,
8+
};
49
#[cfg(target_os = "windows")]
510
use crate::os::windows::alloc::{
611
nstd_os_windows_alloc_allocate, nstd_os_windows_alloc_allocate_zeroed,
@@ -70,12 +75,16 @@ impl NSTDAllocError {
7075
#[inline]
7176
#[cfg_attr(feature = "clib", no_mangle)]
7277
pub unsafe extern "C" fn nstd_alloc_allocate(size: NSTDUInt) -> NSTDAnyMut {
73-
#[cfg(not(target_os = "windows"))]
78+
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
7479
{
7580
use alloc::alloc::Layout;
7681
let layout = Layout::from_size_align_unchecked(size, 1);
7782
alloc::alloc::alloc(layout).cast()
7883
}
84+
#[cfg(target_family = "unix")]
85+
{
86+
nstd_os_unix_alloc_allocate(size)
87+
}
7988
#[cfg(target_os = "windows")]
8089
{
8190
nstd_os_windows_alloc_allocate(size)
@@ -114,12 +123,16 @@ pub unsafe extern "C" fn nstd_alloc_allocate(size: NSTDUInt) -> NSTDAnyMut {
114123
#[inline]
115124
#[cfg_attr(feature = "clib", no_mangle)]
116125
pub unsafe extern "C" fn nstd_alloc_allocate_zeroed(size: NSTDUInt) -> NSTDAnyMut {
117-
#[cfg(not(target_os = "windows"))]
126+
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
118127
{
119128
use alloc::alloc::Layout;
120129
let layout = Layout::from_size_align_unchecked(size, 1);
121130
alloc::alloc::alloc_zeroed(layout).cast()
122131
}
132+
#[cfg(target_family = "unix")]
133+
{
134+
nstd_os_unix_alloc_allocate_zeroed(size)
135+
}
123136
#[cfg(target_os = "windows")]
124137
{
125138
nstd_os_windows_alloc_allocate_zeroed(size)
@@ -175,13 +188,16 @@ pub unsafe extern "C" fn nstd_alloc_allocate_zeroed(size: NSTDUInt) -> NSTDAnyMu
175188
/// ```
176189
#[inline]
177190
#[cfg_attr(feature = "clib", no_mangle)]
178-
#[cfg_attr(target_os = "windows", allow(unused_variables))]
191+
#[cfg_attr(
192+
any(target_family = "unix", target_os = "windows"),
193+
allow(unused_variables)
194+
)]
179195
pub unsafe extern "C" fn nstd_alloc_reallocate(
180196
ptr: &mut NSTDAnyMut,
181197
size: NSTDUInt,
182198
new_size: NSTDUInt,
183199
) -> NSTDAllocError {
184-
#[cfg(not(target_os = "windows"))]
200+
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
185201
{
186202
use alloc::alloc::Layout;
187203
let layout = Layout::from_size_align_unchecked(size, 1);
@@ -192,6 +208,13 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
192208
}
193209
NSTDAllocError::NSTD_ALLOC_ERROR_OUT_OF_MEMORY
194210
}
211+
#[cfg(target_family = "unix")]
212+
{
213+
match nstd_os_unix_alloc_reallocate(ptr, new_size) {
214+
0 => NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
215+
_ => NSTDAllocError::NSTD_ALLOC_ERROR_OUT_OF_MEMORY,
216+
}
217+
}
195218
#[cfg(target_os = "windows")]
196219
{
197220
NSTDAllocError::from_windows(nstd_os_windows_alloc_reallocate(ptr, new_size))
@@ -225,16 +248,23 @@ pub unsafe extern "C" fn nstd_alloc_reallocate(
225248
/// ```
226249
#[inline]
227250
#[cfg_attr(feature = "clib", no_mangle)]
228-
#[cfg_attr(target_os = "windows", allow(unused_variables))]
251+
#[cfg_attr(
252+
any(target_family = "unix", target_os = "windows"),
253+
allow(unused_variables)
254+
)]
229255
pub unsafe extern "C" fn nstd_alloc_deallocate(ptr: &mut NSTDAnyMut, size: NSTDUInt) {
230-
#[cfg(not(target_os = "windows"))]
256+
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
231257
{
232258
use crate::NSTD_NULL;
233259
use alloc::alloc::Layout;
234260
let layout = Layout::from_size_align_unchecked(size, 1);
235261
alloc::alloc::dealloc((*ptr).cast(), layout);
236262
*ptr = NSTD_NULL;
237263
}
264+
#[cfg(target_family = "unix")]
265+
{
266+
nstd_os_unix_alloc_deallocate(ptr);
267+
}
238268
#[cfg(target_os = "windows")]
239269
{
240270
nstd_os_windows_alloc_deallocate(ptr);

0 commit comments

Comments
 (0)