pkgcraft/
free.rs

1use std::ffi::{c_char, c_void, CString};
2
3/// Free a string.
4///
5/// # Safety
6/// The argument must be a string pointer or NULL.
7#[no_mangle]
8pub unsafe extern "C" fn pkgcraft_str_free(s: *mut c_char) {
9    if !s.is_null() {
10        unsafe { drop(CString::from_raw(s)) };
11    }
12}
13
14/// Free an array of strings.
15///
16/// # Safety
17/// The argument must be a pointer to a string array or NULL along with the length of the array.
18#[no_mangle]
19pub unsafe extern "C" fn pkgcraft_str_array_free(strs: *mut *mut c_char, len: usize) {
20    if !strs.is_null() {
21        unsafe {
22            for s in Vec::from_raw_parts(strs, len, len) {
23                drop(CString::from_raw(s));
24            }
25        }
26    }
27}
28
29/// Free an array without dropping the objects inside it.
30///
31/// # Safety
32/// The array objects should be explicitly dropped using other methods otherwise they will leak.
33#[no_mangle]
34pub unsafe extern "C" fn pkgcraft_array_free(array: *mut *mut c_void, len: usize) {
35    if !array.is_null() {
36        unsafe { Vec::from_raw_parts(array, len, len) };
37    }
38}