Skip to content

Commit ef0c7e6

Browse files
committed
Only use FCC for SQLite3 user defined functions
1 parent 19e194e commit ef0c7e6

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

ext/sqlite3/php_sqlite3_structs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ typedef struct _php_sqlite3_func {
5252
const char *func_name;
5353
int argc;
5454

55-
zval func, step, fini;
56-
struct php_sqlite3_fci afunc, astep, afini;
55+
zend_fcall_info_cache func;
56+
zend_fcall_info_cache step;
57+
zend_fcall_info_cache fini;
5758
} php_sqlite3_func;
5859

5960
/* Structure for SQLite collation function */

ext/sqlite3/sqlite3.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -727,29 +727,23 @@ PHP_METHOD(SQLite3, querySingle)
727727
}
728728
/* }}} */
729729

730-
static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg) /* {{{ */
730+
static int sqlite3_do_callback(zend_fcall_info_cache *fcc, uint32_t argc, sqlite3_value **argv, sqlite3_context *context, int is_agg) /* {{{ */
731731
{
732732
zval *zargs = NULL;
733733
zval retval;
734-
int i;
735-
int ret;
736-
int fake_argc;
734+
uint32_t i;
735+
uint32_t fake_argc;
736+
zend_result ret = SUCCESS;
737737
php_sqlite3_agg_context *agg_context = NULL;
738738

739+
// TODO Check where this arg is used
739740
if (is_agg) {
740741
is_agg = 2;
741742
}
742743

743744
fake_argc = argc + is_agg;
744745

745-
fc->fci.size = sizeof(fc->fci);
746-
ZVAL_COPY_VALUE(&fc->fci.function_name, cb);
747-
fc->fci.object = NULL;
748-
fc->fci.retval = &retval;
749-
fc->fci.param_count = fake_argc;
750-
751746
/* build up the params */
752-
753747
if (fake_argc) {
754748
zargs = (zval *)safe_emalloc(fake_argc, sizeof(zval), 0);
755749
}
@@ -791,11 +785,7 @@ static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, s
791785
}
792786
}
793787

794-
fc->fci.params = zargs;
795-
796-
if ((ret = zend_call_function(&fc->fci, &fc->fcc)) == FAILURE) {
797-
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
798-
}
788+
zend_call_known_fcc(fcc, &retval, fake_argc, zargs, /* named_params */ NULL);
799789

800790
if (is_agg) {
801791
zval_ptr_dtor(&zargs[0]);
@@ -872,7 +862,7 @@ static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite
872862
{
873863
php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
874864

875-
sqlite3_do_callback(&func->afunc, &func->func, argc, argv, context, 0);
865+
sqlite3_do_callback(&func->func, argc, argv, context, 0);
876866
}
877867
/* }}}*/
878868

@@ -883,7 +873,7 @@ static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite
883873

884874
agg_context->row_count++;
885875

886-
sqlite3_do_callback(&func->astep, &func->step, argc, argv, context, 1);
876+
sqlite3_do_callback(&func->step, argc, argv, context, 1);
887877
}
888878
/* }}} */
889879

@@ -894,7 +884,7 @@ static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
894884

895885
agg_context->row_count = 0;
896886

897-
sqlite3_do_callback(&func->afini, &func->fini, 0, NULL, context, 1);
887+
sqlite3_do_callback(&func->fini, 0, NULL, context, 1);
898888
}
899889
/* }}} */
900890

@@ -974,7 +964,8 @@ PHP_METHOD(SQLite3, createFunction)
974964
if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, flags | SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
975965
func->func_name = estrdup(sql_func);
976966

977-
ZVAL_COPY(&func->func, &fci.function_name);
967+
zend_fcc_addref(&fcc);
968+
memcpy(&func->func, &fcc, sizeof(zend_fcall_info_cache));
978969

979970
func->argc = sql_func_num_args;
980971
func->next = db_obj->funcs;
@@ -1016,8 +1007,10 @@ PHP_METHOD(SQLite3, createAggregate)
10161007
if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, NULL, php_sqlite3_callback_step, php_sqlite3_callback_final) == SQLITE_OK) {
10171008
func->func_name = estrdup(sql_func);
10181009

1019-
ZVAL_COPY(&func->step, &step_fci.function_name);
1020-
ZVAL_COPY(&func->fini, &fini_fci.function_name);
1010+
zend_fcc_addref(&step_fcc);
1011+
zend_fcc_addref(&fini_fcc);
1012+
memcpy(&func->step, &step_fcc, sizeof(zend_fcall_info_cache));
1013+
memcpy(&func->fini, &fini_fcc, sizeof(zend_fcall_info_cache));
10211014

10221015
func->argc = sql_func_num_args;
10231016
func->next = db_obj->funcs;
@@ -2202,14 +2195,17 @@ static void php_sqlite3_object_free_storage(zend_object *object) /* {{{ */
22022195

22032196
efree((char*)func->func_name);
22042197

2205-
if (!Z_ISUNDEF(func->func)) {
2206-
zval_ptr_dtor(&func->func);
2198+
if (ZEND_FCC_INITIALIZED(func->func)) {
2199+
zend_fcc_dtor(&func->func);
2200+
memcpy(&func->func, &empty_fcall_info_cache, sizeof(zend_fcall_info_cache));
22072201
}
2208-
if (!Z_ISUNDEF(func->step)) {
2209-
zval_ptr_dtor(&func->step);
2202+
if (ZEND_FCC_INITIALIZED(func->step)) {
2203+
zend_fcc_dtor(&func->step);
2204+
memcpy(&func->step, &empty_fcall_info_cache, sizeof(zend_fcall_info_cache));
22102205
}
2211-
if (!Z_ISUNDEF(func->fini)) {
2212-
zval_ptr_dtor(&func->fini);
2206+
if (ZEND_FCC_INITIALIZED(func->fini)) {
2207+
zend_fcc_dtor(&func->fini);
2208+
memcpy(&func->fini, &empty_fcall_info_cache, sizeof(zend_fcall_info_cache));
22132209
}
22142210
efree(func);
22152211
}

0 commit comments

Comments
 (0)