-
Notifications
You must be signed in to change notification settings - Fork 197
/
php_apc.c
839 lines (705 loc) · 21.9 KB
/
php_apc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
+----------------------------------------------------------------------+
| APC |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Cowgill <[email protected]> |
| Rasmus Lerdorf <[email protected]> |
+----------------------------------------------------------------------+
This software was contributed to PHP by Community Connect Inc. in 2002
and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
Future revisions and derivatives of this source code must acknowledge
Community Connect Inc. as the original contributor of this module by
leaving this note intact in the source code.
All other licensing and usage conditions are those of the PHP Group.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "apc_cache.h"
#include "apc_iterator.h"
#include "apc_sma.h"
#include "apc_lock.h"
#include "apc_mutex.h"
#include "apc_strings.h"
#include "apc_time.h"
#include "php_globals.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
#include "ext/standard/flock_compat.h"
#include "ext/standard/md5.h"
#include "ext/standard/php_var.h"
#if PHP_VERSION_ID >= 80000
# include "php_apc_arginfo.h"
#else
# include "php_apc_legacy_arginfo.h"
#endif
#include "php74_shim.h"
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#include "SAPI.h"
#include "php_apc.h"
#ifdef HAVE_SIGACTION
#include "apc_signal.h"
#endif
/* {{{ ZEND_DECLARE_MODULE_GLOBALS(apcu) */
ZEND_DECLARE_MODULE_GLOBALS(apcu)
/* True globals */
apc_cache_t* apc_user_cache = NULL;
/* External APC SMA */
apc_sma_t apc_sma;
#define X(str) zend_string *apc_str_ ## str;
APC_STRINGS
#undef X
/* Global init functions */
static void php_apc_init_globals(zend_apcu_globals* apcu_globals)
{
apcu_globals->initialized = 0;
apcu_globals->slam_defense = 0;
apcu_globals->smart = 0;
apcu_globals->preload_path = NULL;
apcu_globals->coredump_unmap = 0;
apcu_globals->use_request_time = 0;
apcu_globals->serializer_name = NULL;
apcu_globals->entry_level = 0;
}
/* }}} */
/* {{{ PHP_INI */
static PHP_INI_MH(OnUpdateShmSegments) /* {{{ */
{
zend_long shm_segments = ZEND_STRTOL(new_value->val, NULL, 10);
#ifdef APC_MMAP
if (shm_segments != 1) {
php_error_docref(NULL, E_WARNING, "apc.shm_segments setting ignored in MMAP mode");
}
APCG(shm_segments) = 1;
#else
APCG(shm_segments) = shm_segments;
#endif
return SUCCESS;
}
/* }}} */
static PHP_INI_MH(OnUpdateShmSize) /* {{{ */
{
#if PHP_VERSION_ID >= 80200
zend_long s = zend_ini_parse_quantity_warn(new_value, entry->name);
#else
zend_long s = zend_atol(new_value->val, new_value->len);
#endif
if (s <= 0) {
return FAILURE;
}
if (s < Z_L(1048576)) {
/* if it's less than 1Mb, they are probably using the old syntax */
php_error_docref(
NULL, E_WARNING, "apc.shm_size now uses M/G suffixes, please update your ini files");
s = s * Z_L(1048576);
}
APCG(shm_size) = s;
return SUCCESS;
}
/* }}} */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("apc.enabled", "1", PHP_INI_SYSTEM, OnUpdateBool, enabled, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.shm_segments", "1", PHP_INI_SYSTEM, OnUpdateShmSegments, shm_segments, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.shm_size", "32M", PHP_INI_SYSTEM, OnUpdateShmSize, shm_size, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.entries_hint", "4096", PHP_INI_SYSTEM, OnUpdateLong, entries_hint, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.gc_ttl", "3600", PHP_INI_SYSTEM, OnUpdateLong, gc_ttl, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.ttl", "0", PHP_INI_SYSTEM, OnUpdateLong, ttl, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.smart", "0", PHP_INI_SYSTEM, OnUpdateLong, smart, zend_apcu_globals, apcu_globals)
#ifdef APC_MMAP
STD_PHP_INI_ENTRY("apc.mmap_file_mask", NULL, PHP_INI_SYSTEM, OnUpdateString, mmap_file_mask, zend_apcu_globals, apcu_globals)
#endif
STD_PHP_INI_BOOLEAN("apc.enable_cli", "0", PHP_INI_SYSTEM, OnUpdateBool, enable_cli, zend_apcu_globals, apcu_globals)
STD_PHP_INI_BOOLEAN("apc.slam_defense", "0", PHP_INI_SYSTEM, OnUpdateBool, slam_defense, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.preload_path", (char*)NULL, PHP_INI_SYSTEM, OnUpdateString, preload_path, zend_apcu_globals, apcu_globals)
STD_PHP_INI_BOOLEAN("apc.coredump_unmap", "0", PHP_INI_SYSTEM, OnUpdateBool, coredump_unmap, zend_apcu_globals, apcu_globals)
STD_PHP_INI_BOOLEAN("apc.use_request_time", "0", PHP_INI_ALL, OnUpdateBool, use_request_time, zend_apcu_globals, apcu_globals)
STD_PHP_INI_ENTRY("apc.serializer", "php", PHP_INI_SYSTEM, OnUpdateStringUnempty, serializer_name, zend_apcu_globals, apcu_globals)
PHP_INI_END()
/* }}} */
zend_bool apc_is_enabled(void)
{
return APCG(enabled);
}
/* {{{ PHP_MINFO_FUNCTION(apcu) */
static PHP_MINFO_FUNCTION(apcu)
{
php_info_print_table_start();
php_info_print_table_row(2, "APCu Support", APCG(enabled) ? "Enabled" : "Disabled");
php_info_print_table_row(2, "Version", PHP_APCU_VERSION);
#ifdef APC_DEBUG
php_info_print_table_row(2, "APCu Debugging", "Enabled");
#else
php_info_print_table_row(2, "APCu Debugging", "Disabled");
#endif
#ifdef APC_MMAP
php_info_print_table_row(2, "MMAP Support", "Enabled");
php_info_print_table_row(2, "MMAP File Mask", APCG(mmap_file_mask));
#else
php_info_print_table_row(2, "MMAP Support", "Disabled");
#endif
if (APCG(enabled)) {
apc_serializer_t *serializer = NULL;
smart_str names = {0,};
int i;
for( i = 0, serializer = apc_get_serializers();
serializer->name != NULL;
serializer++, i++) {
if (i != 0) {
smart_str_appends(&names, ", ");
}
smart_str_appends(&names, serializer->name);
}
if (names.s) {
smart_str_0(&names);
php_info_print_table_row(2, "Serialization Support", names.s->val);
smart_str_free(&names);
} else {
php_info_print_table_row(2, "Serialization Support", "Broken");
}
} else {
php_info_print_table_row(2, "Serialization Support", "Disabled");
}
php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION(apcu) */
static PHP_MINIT_FUNCTION(apcu)
{
#if defined(ZTS) && defined(COMPILE_DL_APCU)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
ZEND_INIT_MODULE_GLOBALS(apcu, php_apc_init_globals, NULL);
REGISTER_INI_ENTRIES();
#define X(str) \
apc_str_ ## str = zend_new_interned_string( \
zend_string_init(#str, sizeof(#str) - 1, 1));
APC_STRINGS
#undef X
/* locks initialized regardless of settings */
apc_lock_init();
APC_MUTEX_INIT();
/* Disable APC in cli mode unless overridden by apc.enable_cli */
if (!APCG(enable_cli) && !strcmp(sapi_module.name, "cli")) {
APCG(enabled) = 0;
}
/* only run initialization if APC is enabled */
if (APCG(enabled)) {
if (!APCG(initialized)) {
#ifdef APC_MMAP
char *mmap_file_mask = APCG(mmap_file_mask);
#else
char *mmap_file_mask = NULL;
#endif
/* ensure this runs only once */
APCG(initialized) = 1;
/* initialize shared memory allocator */
apc_sma_init(
&apc_sma, (void **) &apc_user_cache, (apc_sma_expunge_f) apc_cache_default_expunge,
APCG(shm_segments), APCG(shm_size), mmap_file_mask);
REGISTER_LONG_CONSTANT(APC_SERIALIZER_CONSTANT, (zend_long)&_apc_register_serializer, CONST_PERSISTENT | CONST_CS);
/* register default serializer */
_apc_register_serializer(
"php", APC_SERIALIZER_NAME(php), APC_UNSERIALIZER_NAME(php), NULL);
/* test out the constant function pointer */
assert(apc_get_serializers()->name != NULL);
/* create user cache */
apc_user_cache = apc_cache_create(
&apc_sma,
apc_find_serializer(APCG(serializer_name)),
APCG(entries_hint), APCG(gc_ttl), APCG(ttl), APCG(smart), APCG(slam_defense));
/* preload data from path specified in configuration */
if (APCG(preload_path)) {
apc_cache_preload(
apc_user_cache, APCG(preload_path));
}
}
}
/* initialize iterator object */
apc_iterator_init(module_number);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION(apcu) */
static PHP_MSHUTDOWN_FUNCTION(apcu)
{
#define X(str) zend_string_release(apc_str_ ## str);
APC_STRINGS
#undef X
/* locks shutdown regardless of settings */
apc_lock_cleanup();
APC_MUTEX_CLEANUP();
/* only shut down if APC is enabled */
if (APCG(enabled)) {
if (APCG(initialized)) {
/* Detach cache and shared memory allocator from shared memory. */
apc_cache_detach(apc_user_cache);
apc_sma_detach(&apc_sma);
APCG(initialized) = 0;
}
#ifdef HAVE_SIGACTION
apc_shutdown_signals();
#endif
}
apc_iterator_shutdown(module_number);
UNREGISTER_INI_ENTRIES();
return SUCCESS;
} /* }}} */
/* {{{ PHP_RINIT_FUNCTION(apcu) */
static PHP_RINIT_FUNCTION(apcu)
{
#if defined(ZTS) && defined(COMPILE_DL_APCU)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
APCG(request_time) = 0;
if (APCG(enabled)) {
if (APCG(serializer_name)) {
/* Avoid race conditions between MINIT of apc and serializer exts like igbinary */
apc_cache_serializer(apc_user_cache, APCG(serializer_name));
}
#ifdef HAVE_SIGACTION
apc_set_signals();
#endif
}
return SUCCESS;
}
/* }}} */
/* {{{ proto void apcu_clear_cache() */
PHP_FUNCTION(apcu_clear_cache)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
apc_cache_clear(apc_user_cache);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto array apcu_cache_info([bool limited]) */
PHP_FUNCTION(apcu_cache_info)
{
zend_bool limited = 0;
ZEND_PARSE_PARAMETERS_START(0,1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(limited)
ZEND_PARSE_PARAMETERS_END();
if (!apc_cache_info(return_value, apc_user_cache, limited)) {
php_error_docref(NULL, E_WARNING, "No APC info available. Perhaps APC is not enabled? Check apc.enabled in your ini file");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto array apcu_key_info(string key) */
PHP_FUNCTION(apcu_key_info)
{
zend_string *key;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(key)
ZEND_PARSE_PARAMETERS_END();
apc_cache_stat(apc_user_cache, key, return_value);
} /* }}} */
/* {{{ proto array apcu_sma_info([bool limited]) */
PHP_FUNCTION(apcu_sma_info)
{
apc_sma_info_t* info;
zval block_lists;
int i;
zend_bool limited = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(limited)
ZEND_PARSE_PARAMETERS_END();
info = apc_sma_info(&apc_sma, limited);
if (!info) {
php_error_docref(NULL, E_WARNING, "No APC SMA info available. Perhaps APC is disabled via apc.enabled?");
RETURN_FALSE;
}
array_init(return_value);
add_assoc_long(return_value, "num_seg", info->num_seg);
add_assoc_double(return_value, "seg_size", (double)info->seg_size);
add_assoc_double(return_value, "avail_mem", (double)apc_sma_get_avail_mem(&apc_sma));
if (limited) {
apc_sma_free_info(&apc_sma, info);
return;
}
array_init(&block_lists);
for (i = 0; i < info->num_seg; i++) {
apc_sma_link_t* p;
zval list;
array_init(&list);
for (p = info->list[i]; p != NULL; p = p->next) {
zval link;
array_init(&link);
add_assoc_long(&link, "size", p->size);
add_assoc_long(&link, "offset", p->offset);
add_next_index_zval(&list, &link);
}
add_next_index_zval(&block_lists, &list);
}
add_assoc_zval(return_value, "block_lists", &block_lists);
apc_sma_free_info(&apc_sma, info);
}
/* }}} */
/* {{{ php_apc_update */
zend_bool php_apc_update(
zend_string *key, apc_cache_atomic_updater_t updater, void *data,
zend_bool insert_if_not_found, time_t ttl)
{
if (APCG(serializer_name)) {
/* Avoid race conditions between MINIT of apc and serializer exts like igbinary */
apc_cache_serializer(apc_user_cache, APCG(serializer_name));
}
return apc_cache_atomic_update_long(apc_user_cache, key, updater, data, insert_if_not_found, ttl);
}
/* }}} */
/* {{{ apc_store_helper(INTERNAL_FUNCTION_PARAMETERS, const zend_bool exclusive)
*/
static void apc_store_helper(INTERNAL_FUNCTION_PARAMETERS, const zend_bool exclusive)
{
zval *key;
zval *val = NULL;
zend_long ttl = 0L;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_ZVAL(key)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(val)
Z_PARAM_LONG(ttl)
ZEND_PARSE_PARAMETERS_END();
if (APCG(serializer_name)) {
/* Avoid race conditions between MINIT of apc and serializer exts like igbinary */
apc_cache_serializer(apc_user_cache, APCG(serializer_name));
}
/* TODO: Port to array|string for PHP 8? */
if (Z_TYPE_P(key) == IS_ARRAY) {
zval *hentry;
zend_string *hkey;
zend_ulong hkey_idx;
HashTable* hash = Z_ARRVAL_P(key);
/* We only insert keys that failed */
zval fail_zv;
ZVAL_LONG(&fail_zv, -1);
array_init(return_value);
ZEND_HASH_FOREACH_KEY_VAL(hash, hkey_idx, hkey, hentry) {
ZVAL_DEREF(hentry);
if (hkey) {
zend_string_addref(hkey);
} else {
hkey = zend_long_to_str(hkey_idx);
}
if (!apc_cache_store(apc_user_cache, hkey, hentry, (uint32_t) ttl, exclusive)) {
zend_symtable_add_new(Z_ARRVAL_P(return_value), hkey, &fail_zv);
}
zend_string_release(hkey);
} ZEND_HASH_FOREACH_END();
return;
} else if (Z_TYPE_P(key) == IS_STRING) {
if (!val) {
/* nothing to store */
RETURN_FALSE;
}
RETURN_BOOL(apc_cache_store(apc_user_cache, Z_STR_P(key), val, (uint32_t) ttl, exclusive));
} else {
apc_warning("apc_store expects key parameter to be a string or an array of key/value pairs.");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool apcu_enabled(void)
returns true when apcu is usable in the current environment */
PHP_FUNCTION(apcu_enabled) {
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_BOOL(APCG(enabled));
}
/* }}} */
/* {{{ proto int apcu_store(mixed key, mixed var [, long ttl ])
*/
PHP_FUNCTION(apcu_store) {
apc_store_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto int apcu_add(mixed key, mixed var [, long ttl ])
*/
PHP_FUNCTION(apcu_add) {
apc_store_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ php_inc_updater */
struct php_inc_updater_args {
zend_long step;
zend_long rval;
};
static zend_bool php_inc_updater(apc_cache_t *cache, zend_long *entry, void *data) {
struct php_inc_updater_args *args = (struct php_inc_updater_args *) data;
args->rval = ATOMIC_ADD(*entry, args->step);
return 1;
}
/* {{{ proto long apcu_inc(string key [, long step [, bool& success [, long ttl]]])
*/
PHP_FUNCTION(apcu_inc) {
zend_string *key;
struct php_inc_updater_args args;
zend_long step = 1, ttl = 0;
zval *success = NULL;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STR(key)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(step)
Z_PARAM_ZVAL(success)
Z_PARAM_LONG(ttl)
ZEND_PARSE_PARAMETERS_END();
args.step = step;
if (php_apc_update(key, php_inc_updater, &args, 1, ttl)) {
if (success) {
ZEND_TRY_ASSIGN_REF_TRUE(success);
}
RETURN_LONG(args.rval);
}
if (success) {
ZEND_TRY_ASSIGN_REF_FALSE(success);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto long apcu_dec(string key [, long step [, bool &success [, long ttl]]])
*/
PHP_FUNCTION(apcu_dec) {
zend_string *key;
struct php_inc_updater_args args;
zend_long step = 1, ttl = 0;
zval *success = NULL;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STR(key)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(step)
Z_PARAM_ZVAL(success)
Z_PARAM_LONG(ttl)
ZEND_PARSE_PARAMETERS_END();
args.step = 0 - step;
if (php_apc_update(key, php_inc_updater, &args, 1, ttl)) {
if (success) {
ZEND_TRY_ASSIGN_REF_TRUE(success);
}
RETURN_LONG(args.rval);
}
if (success) {
ZEND_TRY_ASSIGN_REF_FALSE(success);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ php_cas_updater */
static zend_bool php_cas_updater(apc_cache_t *cache, zend_long *entry, void *data) {
zend_long *vals = (zend_long *) data;
zend_long old = vals[0];
zend_long new = vals[1];
return ATOMIC_CAS(*entry, old, new);
}
/* }}} */
/* {{{ proto int apcu_cas(string key, int old, int new)
*/
PHP_FUNCTION(apcu_cas) {
zend_string *key;
zend_long vals[2];
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STR(key)
Z_PARAM_LONG(vals[0])
Z_PARAM_LONG(vals[1])
ZEND_PARSE_PARAMETERS_END();
if (APCG(serializer_name)) {
/* Avoid race conditions between MINIT of apc and serializer exts like igbinary */
apc_cache_serializer(apc_user_cache, APCG(serializer_name));
}
RETURN_BOOL(apc_cache_atomic_update_long(apc_user_cache, key, php_cas_updater, &vals, 0, 0));
}
/* }}} */
/* {{{ proto mixed apcu_fetch(mixed key[, bool &success])
*/
PHP_FUNCTION(apcu_fetch) {
zval *key;
zval *success = NULL;
time_t t;
int result;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(key)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(success)
ZEND_PARSE_PARAMETERS_END();
t = apc_time();
if (Z_TYPE_P(key) != IS_STRING && Z_TYPE_P(key) != IS_ARRAY) {
convert_to_string(key);
}
/* TODO: Port to array|string for PHP 8? */
if (Z_TYPE_P(key) == IS_STRING) {
result = apc_cache_fetch(apc_user_cache, Z_STR_P(key), t, return_value);
} else if (Z_TYPE_P(key) == IS_ARRAY) {
zval *hentry;
array_init(return_value);
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(key), hentry) {
ZVAL_DEREF(hentry);
if (Z_TYPE_P(hentry) == IS_STRING) {
zval result_entry;
ZVAL_UNDEF(&result_entry);
if (apc_cache_fetch(apc_user_cache, Z_STR_P(hentry), t, &result_entry)) {
zend_hash_update(Z_ARRVAL_P(return_value), Z_STR_P(hentry), &result_entry);
}
} else {
apc_warning("apc_fetch() expects a string or array of strings.");
}
} ZEND_HASH_FOREACH_END();
result = 1;
} else {
apc_warning("apc_fetch() expects a string or array of strings.");
result = 0;
}
if (success) {
ZEND_TRY_ASSIGN_REF_BOOL(success, result);
}
if (!result) {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto mixed apcu_exists(mixed key)
*/
PHP_FUNCTION(apcu_exists) {
zval *key;
time_t t;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(key)
ZEND_PARSE_PARAMETERS_END();
t = apc_time();
if (Z_TYPE_P(key) != IS_STRING && Z_TYPE_P(key) != IS_ARRAY) {
convert_to_string(key);
}
/* TODO: Port to array|string for PHP 8? */
if (Z_TYPE_P(key) == IS_STRING) {
RETURN_BOOL(apc_cache_exists(apc_user_cache, Z_STR_P(key), t));
} else if (Z_TYPE_P(key) == IS_ARRAY) {
zval *hentry;
zval true_zv;
ZVAL_TRUE(&true_zv);
array_init(return_value);
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(key), hentry) {
ZVAL_DEREF(hentry);
if (Z_TYPE_P(hentry) == IS_STRING) {
if (apc_cache_exists(apc_user_cache, Z_STR_P(hentry), t)) {
zend_hash_add_new(Z_ARRVAL_P(return_value), Z_STR_P(hentry), &true_zv);
}
} else {
apc_warning(
"apc_exists() expects a string or array of strings.");
}
} ZEND_HASH_FOREACH_END();
} else {
apc_warning("apc_exists() expects a string or array of strings.");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto mixed apcu_delete(mixed keys)
*/
PHP_FUNCTION(apcu_delete) {
zval *keys;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(keys)
ZEND_PARSE_PARAMETERS_END();
if (Z_TYPE_P(keys) == IS_STRING) {
RETURN_BOOL(apc_cache_delete(apc_user_cache, Z_STR_P(keys)));
} else if (Z_TYPE_P(keys) == IS_ARRAY) {
zval *hentry;
array_init(return_value);
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(keys), hentry) {
ZVAL_DEREF(hentry);
if (Z_TYPE_P(hentry) != IS_STRING) {
apc_warning("apc_delete() expects a string, array of strings, or APCIterator instance");
add_next_index_zval(return_value, hentry);
Z_TRY_ADDREF_P(hentry);
} else if (apc_cache_delete(apc_user_cache, Z_STR_P(hentry)) != 1) {
add_next_index_zval(return_value, hentry);
Z_TRY_ADDREF_P(hentry);
}
} ZEND_HASH_FOREACH_END();
} else if (Z_TYPE_P(keys) == IS_OBJECT) {
RETURN_BOOL(apc_iterator_delete(keys) != 0);
} else {
apc_warning("apc_delete() expects a string, array of strings, or APCIterator instance");
RETURN_FALSE;
}
}
PHP_FUNCTION(apcu_entry) {
zend_string *key;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
zend_long ttl = 0L;
zend_long now = apc_time();
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(key)
Z_PARAM_FUNC(fci, fcc)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(ttl)
ZEND_PARSE_PARAMETERS_END();
apc_cache_entry(apc_user_cache, key, &fci, &fcc, ttl, now, return_value);
}
/* }}} */
#ifdef APC_DEBUG
/* This function is used to test TTL behavior without having to perform sleeps. */
PHP_FUNCTION(apcu_inc_request_time) {
zend_long by = 1;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(by)
ZEND_PARSE_PARAMETERS_END();
if (!APCG(use_request_time)) {
php_error_docref(NULL, E_WARNING,
"Trying to increment request time while use_request_time is disabled");
return;
}
/* Ensure APCG(request_time) is primed */
(void) apc_time();
APCG(request_time) += by;
}
#endif
/* {{{ module definition structure */
zend_module_entry apcu_module_entry = {
STANDARD_MODULE_HEADER,
PHP_APCU_EXTNAME,
ext_functions,
PHP_MINIT(apcu),
PHP_MSHUTDOWN(apcu),
PHP_RINIT(apcu),
NULL,
PHP_MINFO(apcu),
PHP_APCU_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_APCU
ZEND_GET_MODULE(apcu)
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
#endif
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim>600: noexpandtab sw=4 ts=4 sts=4 fdm=marker
* vim<600: noexpandtab sw=4 ts=4 sts=4
*/