Skip to content

Commit 4b27acf

Browse files
committed
Use the new FCC API
1 parent 8e4363d commit 4b27acf

File tree

3 files changed

+63
-67
lines changed

3 files changed

+63
-67
lines changed

ext/pdo_sqlite/pdo_sqlite.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,23 +335,18 @@ PHP_METHOD(PdoSqlite, openBlob)
335335
static int php_sqlite_collation_callback(void *context, int string1_len, const void *string1,
336336
int string2_len, const void *string2)
337337
{
338-
int ret;
338+
int ret = 0;
339339
zval zargs[2];
340340
zval retval;
341341
struct pdo_sqlite_collation *collation = (struct pdo_sqlite_collation*) context;
342342

343-
collation->fc.fci.size = sizeof(collation->fc.fci);
344-
ZVAL_COPY_VALUE(&collation->fc.fci.function_name, &collation->callback);
345-
collation->fc.fci.object = NULL;
346-
collation->fc.fci.retval = &retval;
347-
348343
// Prepare the arguments.
349344
ZVAL_STRINGL(&zargs[0], (char *) string1, string1_len);
350345
ZVAL_STRINGL(&zargs[1], (char *) string2, string2_len);
351-
collation->fc.fci.param_count = 2;
352-
collation->fc.fci.params = zargs;
353346

354-
if ((ret = zend_call_function(&collation->fc.fci, &collation->fc.fcc)) == FAILURE) {
347+
zend_call_known_fcc(&collation->callback, &retval, /* argc */ 2, zargs, /* named_params */ NULL);
348+
349+
if (UNEXPECTED(EG(exception))) {
355350
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
356351
} else if (!Z_ISUNDEF(retval)) {
357352
if (Z_TYPE(retval) != IS_LONG) {
@@ -361,7 +356,6 @@ static int php_sqlite_collation_callback(void *context, int string1_len, const v
361356
zend_string_release(func_name);
362357
return FAILURE;
363358
}
364-
ret = 0;
365359
if (Z_LVAL(retval) > 0) {
366360
ret = 1;
367361
} else if (Z_LVAL(retval) < 0) {

ext/pdo_sqlite/php_pdo_sqlite_int.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,23 @@ typedef struct {
2626
char *errmsg;
2727
} pdo_sqlite_error_info;
2828

29-
struct pdo_sqlite_fci {
30-
zend_fcall_info fci;
31-
zend_fcall_info_cache fcc;
32-
};
33-
3429
struct pdo_sqlite_func {
3530
struct pdo_sqlite_func *next;
3631

37-
zval func, step, fini;
3832
int argc;
3933
const char *funcname;
4034

4135
/* accelerated callback references */
42-
struct pdo_sqlite_fci afunc, astep, afini;
36+
zend_fcall_info_cache func;
37+
zend_fcall_info_cache step;
38+
zend_fcall_info_cache fini;
4339
};
4440

4541
struct pdo_sqlite_collation {
4642
struct pdo_sqlite_collation *next;
4743

4844
const char *name;
49-
zval callback;
50-
struct pdo_sqlite_fci fc;
45+
zend_fcall_info_cache callback;
5146
};
5247

5348
typedef struct {

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ static void pdo_sqlite_cleanup_callbacks(pdo_sqlite_db_handle *H)
112112
}
113113

114114
efree((char*)func->funcname);
115-
if (!Z_ISUNDEF(func->func)) {
116-
zval_ptr_dtor(&func->func);
115+
if (ZEND_FCC_INITIALIZED(func->func)) {
116+
zend_fcc_dtor(&func->func);
117117
}
118-
if (!Z_ISUNDEF(func->step)) {
119-
zval_ptr_dtor(&func->step);
118+
if (ZEND_FCC_INITIALIZED(func->step)) {
119+
zend_fcc_dtor(&func->step);
120120
}
121-
if (!Z_ISUNDEF(func->fini)) {
122-
zval_ptr_dtor(&func->fini);
121+
if (ZEND_FCC_INITIALIZED(func->fini)) {
122+
zend_fcc_dtor(&func->fini);
123123
}
124124
efree(func);
125125
}
@@ -139,8 +139,8 @@ static void pdo_sqlite_cleanup_callbacks(pdo_sqlite_db_handle *H)
139139
}
140140

141141
efree((char*)collation->name);
142-
if (!Z_ISUNDEF(collation->callback)) {
143-
zval_ptr_dtor(&collation->callback);
142+
if (ZEND_FCC_INITIALIZED(collation->callback)) {
143+
zend_fcc_dtor(&collation->callback);
144144
}
145145
efree(collation);
146146
}
@@ -309,12 +309,12 @@ typedef struct {
309309
zend_long row;
310310
} aggregate_context;
311311

312-
static int do_callback(struct pdo_sqlite_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg)
312+
static int do_callback(zend_fcall_info_cache *fcc, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg)
313313
{
314314
zval *zargs = NULL;
315315
zval retval;
316316
int i;
317-
int ret;
317+
int ret = SUCCESS;
318318
int fake_argc;
319319
aggregate_context *agg_context = NULL;
320320

@@ -324,14 +324,7 @@ static int do_callback(struct pdo_sqlite_fci *fc, zval *cb, int argc, sqlite3_va
324324

325325
fake_argc = argc + is_agg;
326326

327-
fc->fci.size = sizeof(fc->fci);
328-
ZVAL_COPY_VALUE(&fc->fci.function_name, cb);
329-
fc->fci.object = NULL;
330-
fc->fci.retval = &retval;
331-
fc->fci.param_count = fake_argc;
332-
333327
/* build up the params */
334-
335328
if (fake_argc) {
336329
zargs = safe_emalloc(fake_argc, sizeof(zval), 0);
337330
}
@@ -372,11 +365,7 @@ static int do_callback(struct pdo_sqlite_fci *fc, zval *cb, int argc, sqlite3_va
372365
}
373366
}
374367

375-
fc->fci.params = zargs;
376-
377-
if ((ret = zend_call_function(&fc->fci, &fc->fcc)) == FAILURE) {
378-
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
379-
}
368+
zend_call_known_fcc(fcc, &retval, fake_argc, zargs, /* named_params */ NULL);
380369

381370
/* clean up the params */
382371
if (zargs) {
@@ -445,41 +434,35 @@ static void php_sqlite3_func_step_callback(sqlite3_context *context, int argc, s
445434
{
446435
struct pdo_sqlite_func *func = (struct pdo_sqlite_func*)sqlite3_user_data(context);
447436

448-
do_callback(&func->astep, &func->step, argc, argv, context, 1);
437+
do_callback(&func->step, argc, argv, context, 1);
449438
}
450439

451440
static void php_sqlite3_func_final_callback(sqlite3_context *context)
452441
{
453442
struct pdo_sqlite_func *func = (struct pdo_sqlite_func*)sqlite3_user_data(context);
454443

455-
do_callback(&func->afini, &func->fini, 0, NULL, context, 1);
444+
do_callback(&func->fini, 0, NULL, context, 1);
456445
}
457446

458447
static int php_sqlite3_collation_callback(void *context, int string1_len, const void *string1, int string2_len, const void *string2)
459448
{
460-
int ret;
449+
int ret = 0;
461450
zval zargs[2];
462451
zval retval;
463452
struct pdo_sqlite_collation *collation = (struct pdo_sqlite_collation*) context;
464453

465-
collation->fc.fci.size = sizeof(collation->fc.fci);
466-
ZVAL_COPY_VALUE(&collation->fc.fci.function_name, &collation->callback);
467-
collation->fc.fci.object = NULL;
468-
collation->fc.fci.retval = &retval;
469-
470454
/* Prepare the arguments. */
471455
ZVAL_STRINGL(&zargs[0], (char *) string1, string1_len);
472456
ZVAL_STRINGL(&zargs[1], (char *) string2, string2_len);
473-
collation->fc.fci.param_count = 2;
474-
collation->fc.fci.params = zargs;
475457

476-
if ((ret = zend_call_function(&collation->fc.fci, &collation->fc.fcc)) == FAILURE) {
458+
zend_call_known_fcc(&collation->callback, &retval, /* argc */ 2, zargs, /* named_params */ NULL);
459+
460+
if (UNEXPECTED(EG(exception))) {
477461
php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
478462
} else if (!Z_ISUNDEF(retval)) {
479463
if (Z_TYPE(retval) != IS_LONG) {
480464
convert_to_long(&retval);
481465
}
482-
ret = 0;
483466
if (Z_LVAL(retval) > 0) {
484467
ret = 1;
485468
} else if (Z_LVAL(retval) < 0) {
@@ -498,7 +481,7 @@ static void php_sqlite3_func_callback(sqlite3_context *context, int argc, sqlite
498481
{
499482
struct pdo_sqlite_func *func = (struct pdo_sqlite_func*)sqlite3_user_data(context);
500483

501-
do_callback(&func->afunc, &func->func, argc, argv, context, 0);
484+
do_callback(&func->func, argc, argv, context, 0);
502485
}
503486

504487
void pdo_sqlite_create_function_internal(INTERNAL_FUNCTION_PARAMETERS)
@@ -516,7 +499,7 @@ void pdo_sqlite_create_function_internal(INTERNAL_FUNCTION_PARAMETERS)
516499

517500
ZEND_PARSE_PARAMETERS_START(2, 4)
518501
Z_PARAM_STRING(func_name, func_name_len)
519-
Z_PARAM_FUNC(fci, fcc)
502+
Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(fci, fcc)
520503
Z_PARAM_OPTIONAL
521504
Z_PARAM_LONG(argc)
522505
Z_PARAM_LONG(flags)
@@ -533,7 +516,7 @@ void pdo_sqlite_create_function_internal(INTERNAL_FUNCTION_PARAMETERS)
533516
if (ret == SQLITE_OK) {
534517
func->funcname = estrdup(func_name);
535518

536-
ZVAL_COPY(&func->func, &fci.function_name);
519+
zend_fcc_dup(&func->func, &fcc);
537520

538521
func->argc = argc;
539522

@@ -544,6 +527,7 @@ void pdo_sqlite_create_function_internal(INTERNAL_FUNCTION_PARAMETERS)
544527
}
545528

546529
efree(func);
530+
zend_release_fcall_info_cache(&fcc);
547531
RETURN_FALSE;
548532
}
549533

@@ -566,14 +550,15 @@ void pdo_sqlite_create_aggregate_internal(INTERNAL_FUNCTION_PARAMETERS)
566550
pdo_dbh_t *dbh;
567551
pdo_sqlite_db_handle *H;
568552
int ret;
553+
bool is_throw = false;
569554

570555
ZEND_PARSE_PARAMETERS_START(3, 4)
571556
Z_PARAM_STRING(func_name, func_name_len)
572-
Z_PARAM_FUNC(step_fci, step_fcc)
573-
Z_PARAM_FUNC(fini_fci, fini_fcc)
557+
Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(step_fci, step_fcc)
558+
Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(fini_fci, fini_fcc)
574559
Z_PARAM_OPTIONAL
575560
Z_PARAM_LONG(argc)
576-
ZEND_PARSE_PARAMETERS_END();
561+
ZEND_PARSE_PARAMETERS_END_EX(is_throw = true; goto error;);
577562

578563
dbh = Z_PDO_DBH_P(ZEND_THIS);
579564
PDO_CONSTRUCT_CHECK;
@@ -587,9 +572,9 @@ void pdo_sqlite_create_aggregate_internal(INTERNAL_FUNCTION_PARAMETERS)
587572
if (ret == SQLITE_OK) {
588573
func->funcname = estrdup(func_name);
589574

590-
ZVAL_COPY(&func->step, &step_fci.function_name);
575+
zend_fcc_dup(&func->step, &step_fcc);
591576

592-
ZVAL_COPY(&func->fini, &fini_fci.function_name);
577+
zend_fcc_dup(&func->fini, &fini_fcc);
593578

594579
func->argc = argc;
595580

@@ -600,6 +585,18 @@ void pdo_sqlite_create_aggregate_internal(INTERNAL_FUNCTION_PARAMETERS)
600585
}
601586

602587
efree(func);
588+
589+
error:
590+
if (ZEND_FCC_INITIALIZED(step_fcc)) {
591+
zend_release_fcall_info_cache(&step_fcc);
592+
}
593+
if (ZEND_FCC_INITIALIZED(fini_fcc)) {
594+
zend_release_fcall_info_cache(&fini_fcc);
595+
}
596+
597+
if (is_throw) {
598+
RETURN_THROWS();
599+
}
603600
RETURN_FALSE;
604601
}
605602

@@ -641,7 +638,7 @@ void pdo_sqlite_create_collation_internal(INTERNAL_FUNCTION_PARAMETERS, pdo_sqli
641638

642639
ZEND_PARSE_PARAMETERS_START(2, 2)
643640
Z_PARAM_STRING(collation_name, collation_name_len)
644-
Z_PARAM_FUNC(fci, fcc)
641+
Z_PARAM_FUNC_NO_TRAMPOLINE_FREE(fci, fcc)
645642
ZEND_PARSE_PARAMETERS_END();
646643

647644
dbh = Z_PDO_DBH_P(ZEND_THIS);
@@ -655,14 +652,16 @@ void pdo_sqlite_create_collation_internal(INTERNAL_FUNCTION_PARAMETERS, pdo_sqli
655652
if (ret == SQLITE_OK) {
656653
collation->name = estrdup(collation_name);
657654

658-
ZVAL_COPY(&collation->callback, &fci.function_name);
655+
zend_fcc_dup(&collation->callback, &fcc);
659656

660657
collation->next = H->collations;
661658
H->collations = collation;
662659

663660
RETURN_TRUE;
664661
}
665662

663+
zend_release_fcall_info_cache(&fcc);
664+
666665
if (UNEXPECTED(EG(exception))) {
667666
RETURN_THROWS();
668667
}
@@ -706,15 +705,23 @@ static void pdo_sqlite_get_gc(pdo_dbh_t *dbh, zend_get_gc_buffer *gc_buffer)
706705

707706
struct pdo_sqlite_func *func = H->funcs;
708707
while (func) {
709-
zend_get_gc_buffer_add_zval(gc_buffer, &func->func);
710-
zend_get_gc_buffer_add_zval(gc_buffer, &func->step);
711-
zend_get_gc_buffer_add_zval(gc_buffer, &func->fini);
708+
if (ZEND_FCC_INITIALIZED(func->func)) {
709+
zend_get_gc_buffer_add_fcc(gc_buffer, &func->func);
710+
}
711+
if (ZEND_FCC_INITIALIZED(func->step)) {
712+
zend_get_gc_buffer_add_fcc(gc_buffer, &func->step);
713+
}
714+
if (ZEND_FCC_INITIALIZED(func->fini)) {
715+
zend_get_gc_buffer_add_fcc(gc_buffer, &func->fini);
716+
}
712717
func = func->next;
713718
}
714719

715720
struct pdo_sqlite_collation *collation = H->collations;
716721
while (collation) {
717-
zend_get_gc_buffer_add_zval(gc_buffer, &collation->callback);
722+
if (ZEND_FCC_INITIALIZED(collation->callback)) {
723+
zend_get_gc_buffer_add_fcc(gc_buffer, &collation->callback);
724+
}
718725
collation = collation->next;
719726
}
720727
}

0 commit comments

Comments
 (0)