@@ -356,32 +356,6 @@ static ffi_type *zend_ffi_get_type(zend_ffi_type *type) /* {{{ */
356
356
}
357
357
/* }}} */
358
358
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
-
385
359
static zend_never_inline zend_ffi_cdata * zend_ffi_cdata_to_zval_slow (void * ptr , zend_ffi_type * type , zend_ffi_flags flags ) /* {{{ */
386
360
{
387
361
zend_ffi_cdata * cdata = emalloc (sizeof (zend_ffi_cdata ));
@@ -2903,6 +2877,119 @@ static void *dlsym_loaded(char *symbol)
2903
2877
# define DL_FETCH_SYMBOL (h , s ) (h == NULL ? dlsym_loaded(s) : GetProcAddress(h, s))
2904
2878
#endif
2905
2879
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
+
2906
2993
ZEND_METHOD (FFI , cdef ) /* {{{ */
2907
2994
{
2908
2995
zend_string * code = NULL ;
@@ -2949,16 +3036,9 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
2949
3036
FFI_G (tags ) = NULL ;
2950
3037
2951
3038
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 );
2962
3042
}
2963
3043
2964
3044
/* Parse C definitions */
@@ -3004,28 +3084,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
3004
3084
} ZEND_HASH_FOREACH_END ();
3005
3085
}
3006
3086
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 );
3029
3088
}
3030
3089
3031
3090
ffi = (zend_ffi * )zend_ffi_new (zend_ffi_ce );
@@ -3276,14 +3335,9 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
3276
3335
close (fd );
3277
3336
ZSTR_VAL (code )[code_size ] = 0 ;
3278
3337
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 ) {
3287
3341
return ffi ;
3288
3342
}
3289
3343
}
@@ -3487,27 +3541,7 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
3487
3541
ffi -> tags = scope -> tags ;
3488
3542
ffi -> persistent = 1 ;
3489
3543
} 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 );
3511
3545
3512
3546
ffi = (zend_ffi * )zend_ffi_new (zend_ffi_ce );
3513
3547
ffi -> lib = handle ;
@@ -3800,7 +3834,6 @@ ZEND_METHOD(FFI, new) /* {{{ */
3800
3834
3801
3835
if (type_def ) {
3802
3836
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT ;
3803
- zend_ffi_dcl * cached_dcl ;
3804
3837
3805
3838
if (!is_static_call ) {
3806
3839
zend_ffi * ffi = (zend_ffi * )Z_OBJ (EX (This ));
@@ -3815,9 +3848,8 @@ ZEND_METHOD(FFI, new) /* {{{ */
3815
3848
3816
3849
FFI_G (default_type_attr ) = 0 ;
3817
3850
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 */
3821
3853
} else if (zend_ffi_parse_type (ZSTR_VAL (type_def ), ZSTR_LEN (type_def ), & dcl ) == FAILURE ) {
3822
3854
zend_ffi_type_dtor (dcl .type );
3823
3855
if (clean_tags && FFI_G (tags )) {
@@ -3836,15 +3868,7 @@ ZEND_METHOD(FFI, new) /* {{{ */
3836
3868
zend_ffi_tags_cleanup (& dcl );
3837
3869
}
3838
3870
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 );
3848
3872
3849
3873
if (clean_symbols && FFI_G (symbols )) {
3850
3874
zend_hash_destroy (FFI_G (symbols ));
@@ -3965,7 +3989,6 @@ ZEND_METHOD(FFI, cast) /* {{{ */
3965
3989
3966
3990
if (type_def ) {
3967
3991
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT ;
3968
- zend_ffi_dcl * cached_dcl ;
3969
3992
3970
3993
if (!is_static_call ) {
3971
3994
zend_ffi * ffi = (zend_ffi * )Z_OBJ (EX (This ));
@@ -3980,9 +4003,8 @@ ZEND_METHOD(FFI, cast) /* {{{ */
3980
4003
3981
4004
FFI_G (default_type_attr ) = 0 ;
3982
4005
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 */
3986
4008
} else if (zend_ffi_parse_type (ZSTR_VAL (type_def ), ZSTR_LEN (type_def ), & dcl ) == FAILURE ) {
3987
4009
zend_ffi_type_dtor (dcl .type );
3988
4010
if (clean_tags && FFI_G (tags )) {
@@ -4001,15 +4023,7 @@ ZEND_METHOD(FFI, cast) /* {{{ */
4001
4023
zend_ffi_tags_cleanup (& dcl );
4002
4024
}
4003
4025
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 );
4013
4027
4014
4028
if (clean_symbols && FFI_G (symbols )) {
4015
4029
zend_hash_destroy (FFI_G (symbols ));
@@ -4138,7 +4152,6 @@ ZEND_METHOD(FFI, type) /* {{{ */
4138
4152
{
4139
4153
zend_ffi_ctype * ctype ;
4140
4154
zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT ;
4141
- zend_ffi_dcl * cached_dcl ;
4142
4155
zend_string * type_def ;
4143
4156
bool is_static_call = Z_TYPE (EX (This )) != IS_OBJECT ;
4144
4157
@@ -4167,9 +4180,8 @@ ZEND_METHOD(FFI, type) /* {{{ */
4167
4180
4168
4181
FFI_G (default_type_attr ) = 0 ;
4169
4182
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 */
4173
4185
} else if (zend_ffi_parse_type (ZSTR_VAL (type_def ), ZSTR_LEN (type_def ), & dcl ) == FAILURE ) {
4174
4186
zend_ffi_type_dtor (dcl .type );
4175
4187
if (clean_tags && FFI_G (tags )) {
@@ -4188,15 +4200,7 @@ ZEND_METHOD(FFI, type) /* {{{ */
4188
4200
zend_ffi_tags_cleanup (& dcl );
4189
4201
}
4190
4202
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 );
4200
4204
4201
4205
if (clean_symbols && FFI_G (symbols )) {
4202
4206
zend_hash_destroy (FFI_G (symbols ));
@@ -5324,17 +5328,6 @@ static ZEND_INI_DISP(zend_ffi_enable_displayer_cb) /* {{{ */
5324
5328
}
5325
5329
/* }}} */
5326
5330
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
-
5338
5331
ZEND_INI_BEGIN ()
5339
5332
ZEND_INI_ENTRY_EX ("ffi.enable" , "preload" , ZEND_INI_SYSTEM , OnUpdateFFIEnable , zend_ffi_enable_displayer_cb )
5340
5333
STD_ZEND_INI_ENTRY ("ffi.preload" , NULL , ZEND_INI_SYSTEM , OnUpdateString , preload , zend_ffi_globals , ffi_globals )
0 commit comments