Skip to content

Commit

Permalink
Remove tripple allocation for interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Dusan Malusev <dusan@dusanmalusev.dev>
  • Loading branch information
CodeLieutenant committed Jan 10, 2024
1 parent 0ddfb07 commit 89c1c69
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
23 changes: 14 additions & 9 deletions phper-sys/c/php_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,31 @@ zend_class_entry phper_init_class_entry_ex(const char *class_name,
}

zend_class_entry *
phper_register_class_entry_ex(zend_class_entry ce, zend_class_entry *parent,
phper_register_class_entry_ex(zend_class_entry *ce, zend_class_entry *parent,
const zend_function_entry *functions) {
ce.info.internal.builtin_functions = functions;
ce->info.internal.builtin_functions = functions;

if (parent == NULL) {
return zend_register_internal_class(&ce);
return zend_register_internal_class(ce);
}

return zend_register_internal_class_ex(&ce, parent);
return zend_register_internal_class_ex(ce, parent);
}

zend_class_entry *
phper_init_interface_entry_ex(const char *class_name, size_t class_name_len,
const zend_function_entry *functions) {
zend_class_entry phper_init_interface_entry_ex(const char *class_name,
size_t class_name_len) {
zend_class_entry class_ce = {0};
class_ce.name = zend_string_init_interned(class_name, class_name_len, true);
class_ce.default_object_handlers = &std_object_handlers;
class_ce.info.internal.builtin_functions = functions;

return zend_register_internal_interface(&class_ce);
return class_ce;
}

zend_class_entry *
phper_register_interface_entry_ex(zend_class_entry *ce,
const zend_function_entry *functions) {
ce->info.internal.builtin_functions = functions;
return zend_register_internal_interface(ce);
}

bool phper_instanceof_function(const zend_class_entry *instance_ce,
Expand Down
11 changes: 7 additions & 4 deletions phper-sys/include/phper.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,19 @@ zend_class_entry phper_init_class_entry_ex(const char *class_name,
size_t class_name_len);

zend_class_entry *
phper_register_class_entry_ex(zend_class_entry ce, zend_class_entry *parent,
phper_register_class_entry_ex(zend_class_entry *ce, zend_class_entry *parent,
const zend_function_entry *functions);

zend_class_entry *
phper_init_interface_entry_ex(const char *class_name, size_t class_name_len,
const zend_function_entry *functions);
zend_class_entry phper_init_interface_entry_ex(const char *class_name,
size_t class_name_len);

bool phper_instanceof_function(const zend_class_entry *instance_ce,
const zend_class_entry *ce);

zend_class_entry *
phper_register_interface_entry_ex(zend_class_entry *ce,
const zend_function_entry *functions);

zend_string *phper_get_function_or_method_name(const zend_function *func);
zend_string *phper_get_function_name(const zend_function *func);
bool phper_call_user_function(HashTable *function_table, zval *object,
Expand Down
6 changes: 3 additions & 3 deletions phper/src/classes/entity.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{any::Any, marker::PhantomData, mem::zeroed, ptr::null_mut, rc::Rc};

use phper_sys::{
phper_get_create_object, phper_init_class_entry_ex, zend_class_entry, zend_class_implements,
zend_function_entry, phper_register_class_entry_ex,
phper_get_create_object, phper_init_class_entry_ex, phper_register_class_entry_ex,
zend_class_entry, zend_class_implements, zend_function_entry,
};

use crate::{
Expand Down Expand Up @@ -311,7 +311,7 @@ impl crate::modules::Registerer for ClassEntity {
.unwrap_or(null_mut());

let class_ce =
phper_register_class_entry_ex(self.class, parent, self.function_entries());
phper_register_class_entry_ex(&mut self.class, parent, self.function_entries());

if let Some(bind_class) = self.bind_class {
bind_class.bind(class_ce);
Expand Down
18 changes: 9 additions & 9 deletions phper/src/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ use crate::{
objects::{StateObj, StateObject, ZObject},
sys::*,
types::Scalar,
utils::ensure_end_with_zero,
values::ZVal,
};
use std::{
any::Any,
convert::TryInto,
ffi::CString,
mem::{size_of, zeroed},
os::raw::c_int,
ptr::null_mut,
Expand Down Expand Up @@ -177,7 +175,7 @@ pub(crate) type StateCloner = dyn Fn(*const dyn Any) -> *mut dyn Any;

/// Builder for registering interface.
pub struct InterfaceEntity {
interface_name: CString,
interface: zend_class_entry,
method_entities: Vec<MethodEntity>,
extends: Vec<Box<dyn Fn() -> &'static ClassEntry>>,
bind_interface: Option<&'static StaticInterface>,
Expand All @@ -186,8 +184,13 @@ pub struct InterfaceEntity {
impl InterfaceEntity {
/// Construct a new `InterfaceEntity` with interface name.
pub fn new(interface_name: impl AsRef<str>) -> Self {
let interface_name = interface_name.as_ref();
let interface_name_len = interface_name.len();

Self {
interface_name: ensure_end_with_zero(interface_name),
interface: unsafe {
phper_init_class_entry_ex(interface_name.as_ptr().cast(), interface_name_len)
},
method_entities: Vec::new(),
extends: Vec::new(),
bind_interface: None,
Expand Down Expand Up @@ -247,11 +250,8 @@ impl InterfaceEntity {
impl crate::modules::Registerer for InterfaceEntity {
fn register(&mut self, _: i32) -> Result<(), Box<dyn std::error::Error>> {
unsafe {
let class_ce = phper_init_interface_entry_ex(
self.interface_name.as_ptr().cast(),
self.interface_name.as_bytes().len(),
self.function_entries(),
);
let class_ce =
phper_register_interface_entry_ex(&mut self.interface, self.function_entries());

if let Some(bind_interface) = self.bind_interface {
bind_interface.bind(class_ce);
Expand Down

0 comments on commit 89c1c69

Please sign in to comment.