Skip to content

Commit c858655

Browse files
committed
phper-alloc: add Allocator and EBox.
1 parent 92cedd6 commit c858655

File tree

7 files changed

+24
-47
lines changed

7 files changed

+24
-47
lines changed

phper-alloc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ keywords = ["php", "alloc"]
1111
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1212

1313
[dependencies]
14+
phper-sys = { version = "0.2", path = "../phper-sys" }

phper-alloc/src/lib.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
use std::alloc::{GlobalAlloc, Layout, System};
1+
#![feature(allocator_api)]
22

3-
struct MyAllocator;
3+
use std::alloc::{AllocRef, Layout, AllocError};
4+
use std::ptr::{NonNull, slice_from_raw_parts_mut};
5+
use phper_sys::{_emalloc, _efree};
46

5-
unsafe impl GlobalAlloc for MyAllocator {
6-
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
7-
eprintln!("GlobalAlloc::alloc");
8-
System.alloc(layout)
7+
pub type EBox<T> = Box<T, Allocator>;
8+
9+
pub struct Allocator;
10+
11+
unsafe impl AllocRef for Allocator {
12+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
13+
unsafe {
14+
let ptr = _emalloc(layout.size());
15+
if ptr.is_null() {
16+
Err(AllocError)
17+
} else {
18+
let ptr = slice_from_raw_parts_mut(ptr.cast(), layout.size());
19+
Ok(NonNull::new_unchecked(ptr))
20+
}
21+
}
922
}
1023

11-
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
12-
eprintln!("GlobalAlloc::dealloc");
13-
System.dealloc(ptr, layout)
24+
unsafe fn dealloc(&self, ptr: NonNull<u8>, _layout: Layout) {
25+
_efree(ptr.as_ptr().cast());
1426
}
1527
}
16-
17-
//#[global_allocator]
18-
//static GLOBAL: MyAllocator = MyAllocator;

phper-sys/build.rs

-27
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,6 @@ fn main() {
1313

1414
let php_config = env::var("PHP_CONFIG").unwrap_or("php-config".to_string());
1515

16-
// Generate php const.
17-
18-
let php_bin = execute_command(&[php_config.as_str(), "--php-binary"]);
19-
let php_info = execute_command(&[php_bin.as_str(), "-i"]);
20-
21-
let php_extension_build = php_info
22-
.lines()
23-
.find_map(|line| {
24-
if line.starts_with("PHP Extension Build") {
25-
Some(
26-
line.chars()
27-
.skip_while(|c| *c != 'A')
28-
.collect::<String>()
29-
.trim()
30-
.to_owned(),
31-
)
32-
} else {
33-
None
34-
}
35-
})
36-
.expect("Can't found the field `PHP Extension Build`");
37-
38-
println!(
39-
"cargo:rustc-env=PHP_EXTENSION_BUILD={}",
40-
php_extension_build
41-
);
42-
4316
// Generate bindgen file.
4417
let includes = execute_command(&[php_config.as_str(), "--includes"]);
4518
let includes = includes.split(' ').collect::<Vec<_>>();

phper-sys/src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22
#![allow(non_camel_case_types)]
33
#![allow(non_snake_case)]
44

5-
use std::ffi::CStr;
6-
use std::os::raw::c_char;
7-
use std::ptr::null;
8-
95
include!(concat!(env!("OUT_DIR"), "/php_bindings.rs"));

phper/src/function.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::sys::{
2-
c_str, zend_execute_data, zend_function_entry, zend_internal_arg_info, zval,
3-
InternalRawFunction,
2+
zend_execute_data, zend_function_entry, zend_internal_arg_info, zval,
43
};
54

65
use std::ffi::{c_void, CStr};

phper/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub extern crate phper_sys as sys;
3131
mod macros;
3232

3333
pub use phper_macros::*;
34-
pub use phper_sys::c_str_ptr;
3534

3635
mod arg;
3736
mod function;

phper/src/module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::sys::{
2-
c_str, zend_function_entry, zend_module_entry, PHP_EXTENSION_BUILD, USING_ZTS, ZEND_DEBUG,
2+
zend_function_entry, zend_module_entry, USING_ZTS, ZEND_DEBUG,
33
ZEND_MODULE_API_NO,
44
};
55
use crate::{functions_into_boxed_entries, Function, FunctionArray};

0 commit comments

Comments
 (0)