Skip to content

Commit ea75ce1

Browse files
committed
cleanup
1 parent 086efde commit ea75ce1

File tree

2 files changed

+132
-137
lines changed

2 files changed

+132
-137
lines changed

ext/ffi/ffi.c

Lines changed: 130 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -356,32 +356,6 @@ static ffi_type *zend_ffi_get_type(zend_ffi_type *type) /* {{{ */
356356
}
357357
/* }}} */
358358

359-
static zend_ffi_cdata* zend_ffi_cdata_create(void *ptr, zend_ffi_type *type) /* {{{ */
360-
{
361-
zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
362-
363-
zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
364-
cdata->std.handlers =
365-
(type->kind < ZEND_FFI_TYPE_POINTER) ?
366-
&zend_ffi_cdata_value_handlers :
367-
&zend_ffi_cdata_handlers;
368-
cdata->type = type;
369-
cdata->flags = 0;
370-
cdata->ptr = ptr;
371-
return cdata;
372-
}
373-
/* }}} */
374-
375-
static zend_ffi_ctype* zend_ffi_ctype_create(zend_ffi_type *type) /* {{{ */
376-
{
377-
zend_ffi_ctype *ctype = emalloc(sizeof(zend_ffi_ctype));
378-
379-
zend_ffi_object_init(&ctype->std, zend_ffi_ctype_ce);
380-
ctype->type = type;
381-
return ctype;
382-
}
383-
/* }}} */
384-
385359
static zend_never_inline zend_ffi_cdata *zend_ffi_cdata_to_zval_slow(void *ptr, zend_ffi_type *type, zend_ffi_flags flags) /* {{{ */
386360
{
387361
zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
@@ -2903,6 +2877,119 @@ static void *dlsym_loaded(char *symbol)
29032877
# define DL_FETCH_SYMBOL(h, s) (h == NULL ? dlsym_loaded(s) : GetProcAddress(h, s))
29042878
#endif
29052879

2880+
static zend_ffi_cdata* zend_ffi_cdata_create(void *ptr, zend_ffi_type *type) /* {{{ */
2881+
{
2882+
zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
2883+
2884+
zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
2885+
cdata->std.handlers =
2886+
(type->kind < ZEND_FFI_TYPE_POINTER) ?
2887+
&zend_ffi_cdata_value_handlers :
2888+
&zend_ffi_cdata_handlers;
2889+
cdata->type = type;
2890+
cdata->flags = 0;
2891+
cdata->ptr = ptr;
2892+
return cdata;
2893+
}
2894+
/* }}} */
2895+
2896+
static zend_ffi_ctype* zend_ffi_ctype_create(zend_ffi_type *type) /* {{{ */
2897+
{
2898+
zend_ffi_ctype *ctype = emalloc(sizeof(zend_ffi_ctype));
2899+
2900+
zend_ffi_object_init(&ctype->std, zend_ffi_ctype_ce);
2901+
ctype->type = type;
2902+
return ctype;
2903+
}
2904+
/* }}} */
2905+
2906+
static void zend_ffi_type_print(FILE *f, const zend_ffi_type *type) /* {{{ */
2907+
{
2908+
zend_ffi_ctype_name_buf buf;
2909+
2910+
buf.start = buf.end = buf.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
2911+
if (!zend_ffi_ctype_name(&buf, ZEND_FFI_TYPE(type))) {
2912+
} else {
2913+
fwrite(buf.start, buf.end - buf.start, 1, f);
2914+
}
2915+
}
2916+
2917+
static bool zend_ffi_cache_type_get(zend_string *type_def, zend_ffi_dcl *dcl) /* {{{ */
2918+
{
2919+
if (ffi_api.cache_type_get) {
2920+
zend_ffi_dcl *cached_dcl = ffi_api.cache_type_get(type_def, FFI_G(symbols));
2921+
2922+
if (cached_dcl) {
2923+
memcpy(dcl, cached_dcl, sizeof(zend_ffi_dcl));
2924+
return true;
2925+
}
2926+
}
2927+
return false;
2928+
}
2929+
/* }}} */
2930+
2931+
static void zend_ffi_cache_type_add(zend_string *type_def, zend_ffi_dcl *dcl) /* {{{ */
2932+
{
2933+
if (ffi_api.cache_type_add) {
2934+
zend_ffi_dcl *cached_dcl = ffi_api.cache_type_add(type_def, dcl, FFI_G(symbols));
2935+
2936+
if (cached_dcl) {
2937+
if (ZEND_FFI_TYPE_IS_OWNED(dcl->type)) {
2938+
_zend_ffi_type_dtor(dcl->type);
2939+
}
2940+
memcpy(dcl, cached_dcl, sizeof(zend_ffi_dcl));
2941+
}
2942+
}
2943+
}
2944+
/* }}} */
2945+
2946+
static zend_ffi* zend_ffi_cache_scope_get(zend_string *code, DL_HANDLE handle) /* {{{ */
2947+
{
2948+
if (ffi_api.cache_scope_get) {
2949+
zend_ffi_scope *scope = ffi_api.cache_scope_get(code);
2950+
2951+
if (scope) {
2952+
zend_ffi *ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
2953+
ffi->lib = handle;
2954+
ffi->symbols = scope->symbols;
2955+
ffi->tags = scope->tags;
2956+
ffi->persistent = true;
2957+
return ffi;
2958+
}
2959+
}
2960+
2961+
return NULL;
2962+
}
2963+
/* }}} */
2964+
2965+
static bool zend_ffi_cache_scope_add(zend_string *code) /* {{{ */
2966+
{
2967+
if (ffi_api.cache_scope_add) {
2968+
zend_ffi_scope scope, *cached_scope;
2969+
2970+
scope.symbols = FFI_G(symbols);
2971+
scope.tags = FFI_G(tags);
2972+
cached_scope = ffi_api.cache_scope_add(code, &scope);
2973+
if (cached_scope) {
2974+
if (FFI_G(symbols)) {
2975+
zend_hash_destroy(FFI_G(symbols));
2976+
efree(FFI_G(symbols));
2977+
FFI_G(symbols) = NULL;
2978+
}
2979+
if (FFI_G(tags)) {
2980+
zend_hash_destroy(FFI_G(tags));
2981+
efree(FFI_G(tags));
2982+
FFI_G(tags) = NULL;
2983+
}
2984+
FFI_G(symbols) = cached_scope->symbols;
2985+
FFI_G(tags) = cached_scope->tags;
2986+
return true;
2987+
}
2988+
}
2989+
return false;
2990+
}
2991+
/* }}} */
2992+
29062993
ZEND_METHOD(FFI, cdef) /* {{{ */
29072994
{
29082995
zend_string *code = NULL;
@@ -2949,16 +3036,9 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
29493036
FFI_G(tags) = NULL;
29503037

29513038
if (code && ZSTR_LEN(code)) {
2952-
if (ffi_api.cache_scope_get) {
2953-
zend_ffi_scope *scope = ffi_api.cache_scope_get(code);
2954-
if (scope) {
2955-
ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
2956-
ffi->lib = handle;
2957-
ffi->symbols = scope->symbols;
2958-
ffi->tags = scope->tags;
2959-
ffi->persistent = true;
2960-
RETURN_OBJ(&ffi->std);
2961-
}
3039+
ffi = zend_ffi_cache_scope_get(code, handle);
3040+
if (ffi) {
3041+
RETURN_OBJ(&ffi->std);
29623042
}
29633043

29643044
/* Parse C definitions */
@@ -3004,28 +3084,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
30043084
} ZEND_HASH_FOREACH_END();
30053085
}
30063086

3007-
if (ffi_api.cache_scope_add) {
3008-
zend_ffi_scope scope, *cached_scope;
3009-
3010-
scope.symbols = FFI_G(symbols);
3011-
scope.tags = FFI_G(tags);
3012-
cached_scope = ffi_api.cache_scope_add(code, &scope);
3013-
if (cached_scope) {
3014-
if (FFI_G(symbols)) {
3015-
zend_hash_destroy(FFI_G(symbols));
3016-
efree(FFI_G(symbols));
3017-
FFI_G(symbols) = NULL;
3018-
}
3019-
if (FFI_G(tags)) {
3020-
zend_hash_destroy(FFI_G(tags));
3021-
efree(FFI_G(tags));
3022-
FFI_G(tags) = NULL;
3023-
}
3024-
FFI_G(symbols) = cached_scope->symbols;
3025-
FFI_G(tags) = cached_scope->tags;
3026-
persistent = true;
3027-
}
3028-
}
3087+
persistent = zend_ffi_cache_scope_add(code);
30293088
}
30303089

30313090
ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
@@ -3276,14 +3335,9 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
32763335
close(fd);
32773336
ZSTR_VAL(code)[code_size] = 0;
32783337

3279-
if (!preload && ffi_api.cache_scope_get) {
3280-
zend_ffi_scope *scope = ffi_api.cache_scope_get(code);
3281-
if (scope) {
3282-
ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
3283-
ffi->lib = handle;
3284-
ffi->symbols = scope->symbols;
3285-
ffi->tags = scope->tags;
3286-
ffi->persistent = true;
3338+
if (!preload) {
3339+
ffi = zend_ffi_cache_scope_get(code, handle);
3340+
if (ffi) {
32873341
return ffi;
32883342
}
32893343
}
@@ -3487,27 +3541,7 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
34873541
ffi->tags = scope->tags;
34883542
ffi->persistent = 1;
34893543
} else {
3490-
if (ffi_api.cache_scope_add) {
3491-
zend_ffi_scope scope, *cached_scope;
3492-
3493-
scope.symbols = FFI_G(symbols);
3494-
scope.tags = FFI_G(tags);
3495-
cached_scope = ffi_api.cache_scope_add(code, &scope);
3496-
if (cached_scope) {
3497-
if (FFI_G(symbols)) {
3498-
zend_hash_destroy(FFI_G(symbols));
3499-
efree(FFI_G(symbols));
3500-
FFI_G(symbols) = NULL;
3501-
}
3502-
if (FFI_G(tags)) {
3503-
zend_hash_destroy(FFI_G(tags));
3504-
efree(FFI_G(tags));
3505-
FFI_G(tags) = NULL;
3506-
}
3507-
FFI_G(symbols) = cached_scope->symbols;
3508-
FFI_G(tags) = cached_scope->tags;
3509-
}
3510-
}
3544+
persistent = zend_ffi_cache_scope_add(code);
35113545

35123546
ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
35133547
ffi->lib = handle;
@@ -3800,7 +3834,6 @@ ZEND_METHOD(FFI, new) /* {{{ */
38003834

38013835
if (type_def) {
38023836
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
3803-
zend_ffi_dcl *cached_dcl;
38043837

38053838
if (!is_static_call) {
38063839
zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
@@ -3815,9 +3848,8 @@ ZEND_METHOD(FFI, new) /* {{{ */
38153848

38163849
FFI_G(default_type_attr) = 0;
38173850

3818-
if (ffi_api.cache_type_get
3819-
&& (cached_dcl = ffi_api.cache_type_get(type_def, FFI_G(symbols)))) {
3820-
memcpy(&dcl, cached_dcl, sizeof(zend_ffi_dcl));
3851+
if (zend_ffi_cache_type_get(type_def, &dcl)) {
3852+
/* pass */
38213853
} else if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) == FAILURE) {
38223854
zend_ffi_type_dtor(dcl.type);
38233855
if (clean_tags && FFI_G(tags)) {
@@ -3836,15 +3868,7 @@ ZEND_METHOD(FFI, new) /* {{{ */
38363868
zend_ffi_tags_cleanup(&dcl);
38373869
}
38383870

3839-
if (zend_ffi_api->cache_type_add) {
3840-
cached_dcl = zend_ffi_api->cache_type_add(type_def, &dcl, FFI_G(symbols));
3841-
if (cached_dcl) {
3842-
if (ZEND_FFI_TYPE_IS_OWNED(dcl.type)) {
3843-
_zend_ffi_type_dtor(dcl.type);
3844-
}
3845-
memcpy(&dcl, cached_dcl, sizeof(zend_ffi_dcl));
3846-
}
3847-
}
3871+
zend_ffi_cache_type_add(type_def, &dcl);
38483872

38493873
if (clean_symbols && FFI_G(symbols)) {
38503874
zend_hash_destroy(FFI_G(symbols));
@@ -3965,7 +3989,6 @@ ZEND_METHOD(FFI, cast) /* {{{ */
39653989

39663990
if (type_def) {
39673991
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
3968-
zend_ffi_dcl *cached_dcl;
39693992

39703993
if (!is_static_call) {
39713994
zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
@@ -3980,9 +4003,8 @@ ZEND_METHOD(FFI, cast) /* {{{ */
39804003

39814004
FFI_G(default_type_attr) = 0;
39824005

3983-
if (ffi_api.cache_type_get
3984-
&& (cached_dcl = ffi_api.cache_type_get(type_def, FFI_G(symbols)))) {
3985-
memcpy(&dcl, cached_dcl, sizeof(zend_ffi_dcl));
4006+
if (zend_ffi_cache_type_get(type_def, &dcl)) {
4007+
/* pass */
39864008
} else if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) == FAILURE) {
39874009
zend_ffi_type_dtor(dcl.type);
39884010
if (clean_tags && FFI_G(tags)) {
@@ -4001,15 +4023,7 @@ ZEND_METHOD(FFI, cast) /* {{{ */
40014023
zend_ffi_tags_cleanup(&dcl);
40024024
}
40034025

4004-
if (ffi_api.cache_type_add) {
4005-
cached_dcl = ffi_api.cache_type_add(type_def, &dcl, FFI_G(symbols));
4006-
if (cached_dcl) {
4007-
if (ZEND_FFI_TYPE_IS_OWNED(dcl.type)) {
4008-
_zend_ffi_type_dtor(dcl.type);
4009-
}
4010-
memcpy(&dcl, cached_dcl, sizeof(zend_ffi_dcl));
4011-
}
4012-
}
4026+
zend_ffi_cache_type_add(type_def, &dcl);
40134027

40144028
if (clean_symbols && FFI_G(symbols)) {
40154029
zend_hash_destroy(FFI_G(symbols));
@@ -4138,7 +4152,6 @@ ZEND_METHOD(FFI, type) /* {{{ */
41384152
{
41394153
zend_ffi_ctype *ctype;
41404154
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
4141-
zend_ffi_dcl *cached_dcl;
41424155
zend_string *type_def;
41434156
bool is_static_call = Z_TYPE(EX(This)) != IS_OBJECT;
41444157

@@ -4167,9 +4180,8 @@ ZEND_METHOD(FFI, type) /* {{{ */
41674180

41684181
FFI_G(default_type_attr) = 0;
41694182

4170-
if (ffi_api.cache_type_get
4171-
&& (cached_dcl = ffi_api.cache_type_get(type_def, FFI_G(symbols)))) {
4172-
memcpy(&dcl, cached_dcl, sizeof(zend_ffi_dcl));
4183+
if (zend_ffi_cache_type_get(type_def, &dcl)) {
4184+
/* pass */
41734185
} else if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) == FAILURE) {
41744186
zend_ffi_type_dtor(dcl.type);
41754187
if (clean_tags && FFI_G(tags)) {
@@ -4188,15 +4200,7 @@ ZEND_METHOD(FFI, type) /* {{{ */
41884200
zend_ffi_tags_cleanup(&dcl);
41894201
}
41904202

4191-
if (ffi_api.cache_type_add) {
4192-
cached_dcl = ffi_api.cache_type_add(type_def, &dcl, FFI_G(symbols));
4193-
if (cached_dcl) {
4194-
if (ZEND_FFI_TYPE_IS_OWNED(dcl.type)) {
4195-
_zend_ffi_type_dtor(dcl.type);
4196-
}
4197-
memcpy(&dcl, cached_dcl, sizeof(zend_ffi_dcl));
4198-
}
4199-
}
4203+
zend_ffi_cache_type_add(type_def, &dcl);
42004204

42014205
if (clean_symbols && FFI_G(symbols)) {
42024206
zend_hash_destroy(FFI_G(symbols));
@@ -5324,17 +5328,6 @@ static ZEND_INI_DISP(zend_ffi_enable_displayer_cb) /* {{{ */
53245328
}
53255329
/* }}} */
53265330

5327-
static void zend_ffi_type_print(FILE *f, const zend_ffi_type *type) /* {{{ */
5328-
{
5329-
zend_ffi_ctype_name_buf buf;
5330-
5331-
buf.start = buf.end = buf.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
5332-
if (!zend_ffi_ctype_name(&buf, ZEND_FFI_TYPE(type))) {
5333-
} else {
5334-
fwrite(buf.start, buf.end - buf.start, 1, f);
5335-
}
5336-
}
5337-
53385331
ZEND_INI_BEGIN()
53395332
ZEND_INI_ENTRY_EX("ffi.enable", "preload", ZEND_INI_SYSTEM, OnUpdateFFIEnable, zend_ffi_enable_displayer_cb)
53405333
STD_ZEND_INI_ENTRY("ffi.preload", NULL, ZEND_INI_SYSTEM, OnUpdateString, preload, zend_ffi_globals, ffi_globals)

ext/ffi/php_ffi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,13 @@ struct _zend_ffi_api {
418418
zend_class_entry *cdata_ce;
419419
zend_class_entry *ctype_ce;
420420

421+
/* ext/ffi interface for ext/opcache */
421422
zend_ffi_cdata* (*cdata_create)(void *ptr, zend_ffi_type *type);
422423
zend_ffi_ctype* (*ctype_create)(zend_ffi_type *type);
423424
void (*type_print)(FILE *f, const zend_ffi_type *type);
424425
bool (*is_compatible_type)(zend_ffi_type *dst_type, zend_ffi_type *src_type);
425426

427+
/* ext/opcache interface for ext/ffi */
426428
zend_ffi_dcl* (*cache_type_get)(zend_string *str, void *context);
427429
zend_ffi_dcl* (*cache_type_add)(zend_string *str, zend_ffi_dcl *dcl, void *context);
428430
zend_ffi_scope* (*cache_scope_get)(zend_string *str);

0 commit comments

Comments
 (0)