Skip to content

Commit ebedaef

Browse files
committed
Work free for strings.
1 parent a75d629 commit ebedaef

File tree

18 files changed

+212
-252
lines changed

18 files changed

+212
-252
lines changed

examples/http-client/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn make_client_class() -> DynamicClass<Option<Client>> {
8484
"get",
8585
Visibility::Public,
8686
|this, arguments| {
87-
let url = arguments[0].as_string()?;
87+
let url = arguments[0].to_string()?;
8888
let client = this.as_state().as_ref().unwrap();
8989
let request_builder = client.get(url);
9090
let mut object =
@@ -100,7 +100,7 @@ pub fn make_client_class() -> DynamicClass<Option<Client>> {
100100
"post",
101101
Visibility::Public,
102102
|this, arguments| {
103-
let url = arguments[0].as_string()?;
103+
let url = arguments[0].to_string()?;
104104
let client = this.as_state().as_ref().unwrap();
105105
let request_builder = client.post(url);
106106
let mut object =

examples/http-server/src/response.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub fn make_response_class() -> DynamicClass<Response<Body>> {
2626
|this, arguments| {
2727
let response: &mut Response<Body> = this.as_mut_state();
2828
response.headers_mut().insert(
29-
HeaderName::from_bytes(arguments[0].as_string()?.as_bytes())?,
30-
HeaderValue::from_bytes(arguments[1].as_string()?.as_bytes())?,
29+
HeaderName::from_bytes(arguments[0].to_string()?.as_bytes())?,
30+
HeaderValue::from_bytes(arguments[1].to_string()?.as_bytes())?,
3131
);
3232
Ok::<_, HttpServerError>(())
3333
},

examples/http-server/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn make_server_class() -> DynamicClass<Option<Builder<AddrIncoming>>> {
3939
"__construct",
4040
Visibility::Public,
4141
|this, arguments| {
42-
let host = arguments[0].as_string()?;
42+
let host = arguments[0].to_string()?;
4343
let port = arguments[1].as_long()?;
4444
this.set_property("host", Val::new(&*host));
4545
this.set_property("port", Val::new(port));

phper-alloc/src/lib.rs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,15 @@ use std::{
1818
ops::{Deref, DerefMut},
1919
};
2020

21-
// TODO Add ERc, for refcounted type.
22-
23-
/// The item which can be placed into container [EBox].
24-
pub trait EAllocatable {
25-
/// The method to free the heap allocated by `emalloc`, should call `efree`
26-
/// at the end.
27-
///
28-
/// # Safety
29-
unsafe fn free(ptr: *mut Self) {
30-
_efree(ptr.cast());
31-
}
32-
}
33-
3421
/// The Box which use php `emalloc` and `efree` to manage memory.
3522
///
3623
/// TODO now feature `allocator_api` is still unstable, implement myself, use
3724
/// Box<T, Alloc> later.
38-
pub struct EBox<T: EAllocatable> {
25+
pub struct EBox<T> {
3926
ptr: *mut T,
4027
}
4128

42-
impl<T: EAllocatable> EBox<T> {
29+
impl<T> EBox<T> {
4330
/// Allocates heap memory using `emalloc` then places `x` into it.
4431
///
4532
/// # Panic
@@ -48,7 +35,7 @@ impl<T: EAllocatable> EBox<T> {
4835
pub fn new(x: T) -> Self {
4936
unsafe {
5037
assert_ne!(size_of::<T>(), 0);
51-
let ptr: *mut T = _emalloc(size_of::<T>().try_into().unwrap()).cast();
38+
let ptr: *mut T = phper_emalloc(size_of::<T>().try_into().unwrap()).cast();
5239
ptr.write(x);
5340
Self { ptr }
5441
}
@@ -73,45 +60,25 @@ impl<T: EAllocatable> EBox<T> {
7360
}
7461
}
7562

76-
impl<T: EAllocatable> Deref for EBox<T> {
63+
impl<T> Deref for EBox<T> {
7764
type Target = T;
7865

7966
fn deref(&self) -> &Self::Target {
8067
unsafe { self.ptr.as_ref().unwrap() }
8168
}
8269
}
8370

84-
impl<T: EAllocatable> DerefMut for EBox<T> {
71+
impl<T> DerefMut for EBox<T> {
8572
fn deref_mut(&mut self) -> &mut Self::Target {
8673
unsafe { self.ptr.as_mut().unwrap() }
8774
}
8875
}
8976

90-
impl<T: EAllocatable> Drop for EBox<T> {
77+
impl<T> Drop for EBox<T> {
9178
fn drop(&mut self) {
9279
unsafe {
93-
<T>::free(self.ptr);
80+
self.ptr.drop_in_place();
81+
phper_efree(self.ptr.cast());
9482
}
9583
}
9684
}
97-
98-
unsafe impl<T: EAllocatable> Send for EBox<T> {}
99-
100-
// TODO Write Erc for gc_refcounted holding types.
101-
// pub trait ERcAble {
102-
// // Increment the reference count;
103-
// fn incr(&mut self);
104-
//
105-
// /// Decrement the reference count and return old count.
106-
// fn decr(&mut self) -> usize;
107-
// }
108-
//
109-
// pub struct ERc<T> {
110-
// value: T,
111-
// }
112-
//
113-
// impl<T> ERc<T> {
114-
// pub fn new(value: T) -> Self {
115-
// Self { value }
116-
// }
117-
// }

phper-sys/build.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use bindgen::Builder;
1212
use std::{env, ffi::OsStr, fmt::Debug, path::PathBuf, process::Command};
1313

1414
fn main() {
15-
println!("cargo:rerun-if-changed=php_wrapper.h");
1615
println!("cargo:rerun-if-changed=php_wrapper.c");
1716
println!("cargo:rerun-if-env-changed=PHP_CONFIG");
1817

@@ -83,7 +82,7 @@ fn main() {
8382
});
8483

8584
let bindings = Builder::default()
86-
.header("php_wrapper.h")
85+
.header("php_wrapper.c")
8786
.clang_args(&includes)
8887
.blocklist_function("__acosf64x")
8988
.blocklist_function("__acosf64x")

phper-sys/php_wrapper.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
99
// See the Mulan PSL v2 for more details.
1010

11-
#include "php_wrapper.h"
11+
#include <stdbool.h>
12+
#include <php.h>
13+
#include <php_ini.h>
14+
#include <ext/standard/info.h>
15+
#include <zend_exceptions.h>
16+
#include <main/SAPI.h>
17+
18+
typedef ZEND_INI_MH(phper_zend_ini_mh);
19+
20+
zend_string *zend_new_interned_string_(zend_string *str);
21+
zend_class_entry phper_init_class_entry_ex(const char *class_name, size_t class_name_len, const zend_function_entry *functions);
22+
zend_uchar phper_zval_get_type(const zval* pz);
1223

1324
zend_string *phper_zend_new_interned_string(zend_string *str) {
1425
return zend_new_interned_string(str);
@@ -202,3 +213,19 @@ zval *phper_execute_data_call_arg(zend_execute_data *execute_data, int index) {
202213
int phper_z_res_handle_p(const zval *val) {
203214
return Z_RES_HANDLE_P(val);
204215
}
216+
217+
int phper_zstr_len(const zend_string *s) {
218+
return ZSTR_LEN(s);
219+
}
220+
221+
const char *phper_zstr_val(const zend_string *s) {
222+
return ZSTR_VAL(s);
223+
}
224+
225+
void *phper_emalloc(size_t size) {
226+
return emalloc(size);
227+
}
228+
229+
void phper_efree(void *ptr) {
230+
return efree(ptr);
231+
}

phper-sys/php_wrapper.h

Lines changed: 0 additions & 81 deletions
This file was deleted.

phper/src/arrays.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//! Apis relate to [crate::sys::zend_array].
1212
1313
use crate::{
14-
alloc::{EAllocatable, EBox},
15-
strings::ZendString,
14+
alloc::EBox,
15+
strings::{ZStr, ZString},
1616
sys::*,
1717
values::Val,
1818
};
@@ -199,14 +199,14 @@ impl Array {
199199
}
200200
}
201201

202-
impl EAllocatable for Array {
203-
unsafe fn free(ptr: *mut Self) {
204-
(*ptr).inner.gc.refcount -= 1;
205-
if (*ptr).inner.gc.refcount == 0 {
206-
zend_array_destroy(ptr.cast());
207-
}
208-
}
209-
}
202+
// impl EAllocatable for Array {
203+
// unsafe fn free(ptr: *mut Self) {
204+
// (*ptr).inner.gc.refcount -= 1;
205+
// if (*ptr).inner.gc.refcount == 0 {
206+
// zend_array_destroy(ptr.cast());
207+
// }
208+
// }
209+
// }
210210

211211
impl Drop for Array {
212212
fn drop(&mut self) {
@@ -235,8 +235,8 @@ impl<'a> Iterator for Iter<'a> {
235235
let key = if (*bucket).key.is_null() {
236236
Key::Index((*bucket).h)
237237
} else {
238-
let s = ZendString::from_ptr((*bucket).key).unwrap();
239-
let s = s.as_str().unwrap();
238+
let s = ZStr::from_ptr((*bucket).key);
239+
let s = s.to_str().unwrap();
240240
Key::Str(s)
241241
};
242242

phper/src/classes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
errors::{ClassNotFoundError, InitializeObjectError, StateTypeError},
1717
functions::{Argument, Function, FunctionEntity, FunctionEntry, Method},
1818
objects::{ExtendObject, Object},
19-
strings::ZendString,
19+
strings::{ZStr, ZString},
2020
sys::*,
2121
types::Scalar,
2222
values::{SetVal, Val},
@@ -235,7 +235,7 @@ impl<T: 'static> ClassEntry<T> {
235235
let ptr = self.as_ptr() as *mut _;
236236
let mut val = Val::undef();
237237
if !phper_object_init_ex(val.as_mut_ptr(), ptr) {
238-
Err(InitializeObjectError::new(self.get_name().as_str()?.to_owned()).into())
238+
Err(InitializeObjectError::new(self.get_name().to_str()?.to_owned()).into())
239239
} else {
240240
let object = (*val.as_mut_ptr()).value.obj;
241241
forget(val);
@@ -245,8 +245,8 @@ impl<T: 'static> ClassEntry<T> {
245245
}
246246
}
247247

248-
pub fn get_name(&self) -> &ZendString {
249-
unsafe { ZendString::from_ptr(self.inner.name).unwrap() }
248+
pub fn get_name(&self) -> &ZStr {
249+
unsafe { ZStr::from_ptr(self.inner.name) }
250250
}
251251

252252
pub fn has_method(&self, method_name: &str) -> bool {

0 commit comments

Comments
 (0)