1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::alloc::{alloc, dealloc, Layout};
use std::ffi::c_char;
/// Allocate memory for a string of given size (including null terminator)
/// Returns a pointer to writable memory that TypeScript can write to
///
/// # Safety
/// The returned pointer must be released with `diffsol_free_string` using the
/// same `size`. The caller must not read or write beyond the allocated buffer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn diffsol_alloc_string(size: usize) -> *mut c_char {
if size == 0 {
return std::ptr::null_mut();
}
// Create a Vec with the requested capacity and length
let mut buffer = vec![0u8; size];
let ptr = buffer.as_mut_ptr();
std::mem::forget(buffer); // Prevent the Vec from being dropped
ptr as *mut c_char
}
/// Allocate aligned memory for arbitrary data
///
/// # Safety
/// The returned pointer must be released with `diffsol_free` using the same
/// `size` and `align`. `align` must be a valid alignment value.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn diffsol_alloc(size: usize, align: usize) -> *mut u8 {
if size == 0 {
return std::ptr::null_mut();
}
let align = if align == 0 { 1 } else { align };
let layout = match Layout::from_size_align(size, align) {
Ok(layout) => layout,
Err(_) => return std::ptr::null_mut(),
};
let ptr = unsafe { alloc(layout) };
if ptr.is_null() {
return std::ptr::null_mut();
}
ptr
}
/// Free aligned memory allocated by diffsol_alloc
///
/// # Safety
/// `ptr` must be either null or a pointer returned by `diffsol_alloc` with the
/// same `size` and `align` values.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn diffsol_free(ptr: *mut u8, size: usize, align: usize) {
if ptr.is_null() || size == 0 {
return;
}
let align = if align == 0 { 1 } else { align };
let layout = match Layout::from_size_align(size, align) {
Ok(layout) => layout,
Err(_) => return,
};
unsafe { dealloc(ptr, layout) };
}
/// Free memory allocated by diffsol_alloc_string
///
/// # Safety
/// `ptr` must be either null or a pointer returned by `diffsol_alloc_string`
/// with the same `size`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn diffsol_free_string(ptr: *mut c_char, size: usize) {
if ptr.is_null() || size == 0 {
return;
}
// Reconstruct the Vec from the raw pointer and drop it
let _ = unsafe { Vec::from_raw_parts(ptr as *mut u8, size, size) };
// Vec will be dropped here, freeing the memory
}