Skip to content

feat(alloc): add estrdup #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions allowed_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bind! {
_call_user_function_impl,
_efree,
_emalloc,
_estrdup,
_zend_executor_globals,
_sapi_globals_struct,
_sapi_module_struct,
Expand Down
9 changes: 9 additions & 0 deletions docsrs_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,15 @@ extern "C" {
__zend_orig_lineno: u32,
);
}
extern "C" {
pub fn _estrdup(
s: *const ::std::os::raw::c_char,
__zend_filename: *const ::std::os::raw::c_char,
__zend_lineno: u32,
__zend_orig_filename: *const ::std::os::raw::c_char,
__zend_orig_lineno: u32,
) -> *mut ::std::os::raw::c_char;
}
extern "C" {
pub fn __zend_malloc(len: usize) -> *mut ::std::os::raw::c_void;
}
Expand Down
71 changes: 69 additions & 2 deletions src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

use cfg_if::cfg_if;

use crate::ffi::{_efree, _emalloc};
use std::{alloc::Layout, ffi::c_void};
use crate::ffi::{_efree, _emalloc, _estrdup};
use std::{
alloc::Layout,
ffi::{c_char, c_void, CString},
};

/// Uses the PHP memory allocator to allocate request-bound memory.
///
Expand Down Expand Up @@ -62,3 +65,67 @@ pub unsafe fn efree(ptr: *mut u8) {
}
}
}

/// Duplicates a string using the PHP memory manager.
///
/// # Parameters
///
/// * `string` - The string to duplicate, which can be any type that can be
/// converted into a `Vec<u8>`.
///
/// # Returns
///
/// A pointer to the duplicated string in the PHP memory manager.
pub fn estrdup(string: impl Into<Vec<u8>>) -> *mut c_char {
let string = unsafe { CString::from_vec_unchecked(string.into()) }.into_raw();

let result = unsafe {
cfg_if! {
if #[cfg(php_debug)] {
#[allow(clippy::used_underscore_items)]
_estrdup(string, std::ptr::null_mut(), 0, std::ptr::null_mut(), 0)
} else {
#[allow(clippy::used_underscore_items)]
_estrdup(string)
}
}
};

drop(unsafe { CString::from_raw(string) });
result
}

#[cfg(test)]
#[cfg(feature = "embed")]
mod test {
use super::*;
use crate::embed::Embed;
use std::ffi::CStr;

#[test]
fn test_emalloc() {
Embed::run(|| {
let layout = Layout::from_size_align(16, 8).expect("should create layout");
let ptr = emalloc(layout);
assert!(!ptr.is_null());
unsafe { efree(ptr) };
});
}

#[test]
fn test_estrdup() {
Embed::run(|| {
let original = "Hello, world!";
let duplicated = estrdup(original);
assert!(!duplicated.is_null());

let duplicated_str = unsafe { CStr::from_ptr(duplicated) };
assert_eq!(
duplicated_str.to_str().expect("should convert to str"),
original
);

unsafe { efree(duplicated.cast::<u8>()) }
});
}
}