forked from laruence/php-apc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_apc.c
1726 lines (1475 loc) · 55 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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Cowgill <dcowgill@communityconnect.com> |
| Rasmus Lerdorf <rasmus@php.net> |
+----------------------------------------------------------------------+
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.
*/
/* $Id: php_apc.c 329725 2013-03-10 15:58:26Z laruence $ */
#include "apc_zend.h"
#include "apc_cache.h"
#include "apc_iterator.h"
#include "apc_main.h"
#include "apc_sma.h"
#include "apc_lock.h"
#include "apc_bin.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"
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#include "SAPI.h"
#include "rfc1867.h"
#include "php_apc.h"
#include "ext/standard/md5.h"
#if HAVE_SIGACTION
#include "apc_signal.h"
#endif
/* {{{ PHP_FUNCTION declarations */
PHP_FUNCTION(apc_cache_info);
PHP_FUNCTION(apc_clear_cache);
PHP_FUNCTION(apc_sma_info);
PHP_FUNCTION(apc_store);
PHP_FUNCTION(apc_fetch);
PHP_FUNCTION(apc_delete);
PHP_FUNCTION(apc_delete_file);
PHP_FUNCTION(apc_compile_file);
PHP_FUNCTION(apc_define_constants);
PHP_FUNCTION(apc_load_constants);
PHP_FUNCTION(apc_add);
PHP_FUNCTION(apc_inc);
PHP_FUNCTION(apc_dec);
PHP_FUNCTION(apc_cas);
PHP_FUNCTION(apc_bin_dump);
PHP_FUNCTION(apc_bin_load);
PHP_FUNCTION(apc_bin_dumpfile);
PHP_FUNCTION(apc_bin_loadfile);
PHP_FUNCTION(apc_exists);
/* }}} */
/* {{{ ZEND_DECLARE_MODULE_GLOBALS(apc) */
ZEND_DECLARE_MODULE_GLOBALS(apc)
/* True globals */
apc_cache_t* apc_cache = NULL;
apc_cache_t* apc_user_cache = NULL;
static void php_apc_init_globals(zend_apc_globals* apc_globals TSRMLS_DC)
{
apc_globals->filters = NULL;
apc_globals->compiled_filters = NULL;
apc_globals->initialized = 0;
apc_globals->cache_stack = apc_stack_create(0 TSRMLS_CC);
apc_globals->cache_by_default = 1;
apc_globals->fpstat = 1;
apc_globals->canonicalize = 1;
apc_globals->stat_ctime = 0;
apc_globals->write_lock = 1;
apc_globals->slam_defense = 1;
apc_globals->report_autofilter = 0;
apc_globals->include_once = 0;
apc_globals->apc_optimize_function = NULL;
#ifdef MULTIPART_EVENT_FORMDATA
apc_globals->rfc1867 = 0;
memset(&(apc_globals->rfc1867_data), 0, sizeof(apc_rfc1867_data));
#endif
memset(&apc_globals->copied_zvals, 0, sizeof(HashTable));
apc_globals->force_file_update = 0;
apc_globals->coredump_unmap = 0;
apc_globals->preload_path = NULL;
apc_globals->use_request_time = 1;
apc_globals->lazy_class_table = NULL;
apc_globals->lazy_function_table = NULL;
apc_globals->serializer_name = NULL;
apc_globals->serializer = NULL;
apc_globals->compiler_hook_func_table = NULL;
apc_globals->compiler_hook_class_table = NULL;
apc_globals->compile_nesting = 0;
apc_globals->enable_opcode_cache = 1;
}
static void php_apc_shutdown_globals(zend_apc_globals* apc_globals TSRMLS_DC)
{
/* deallocate the ignore patterns */
if (apc_globals->filters != NULL) {
int i;
for (i=0; apc_globals->filters[i] != NULL; i++) {
apc_efree(apc_globals->filters[i] TSRMLS_CC);
}
apc_efree(apc_globals->filters TSRMLS_CC);
}
/* the stack should be empty */
assert(apc_stack_size(apc_globals->cache_stack) == 0);
/* apc cleanup */
apc_stack_destroy(apc_globals->cache_stack TSRMLS_CC);
/* the rest of the globals are cleaned up in apc_module_shutdown() */
}
static long apc_atol(const char *str, int str_len)
{
#if PHP_MAJOR_VERSION >= 6 || PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3
return zend_atol(str, str_len);
#else
/* Re-implement zend_atol() for 5.2.x */
long retval;
if (!str_len) {
str_len = strlen(str);
}
retval = strtol(str, NULL, 0);
if (str_len > 0) {
switch (str[str_len - 1]) {
case 'g':
case 'G':
retval *= 1024;
/* break intentionally missing */
case 'm':
case 'M':
retval *= 1024;
/* break intentionally missing */
case 'k':
case 'K':
retval *= 1024;
break;
}
}
return retval;
#endif
}
/* }}} */
/* {{{ PHP_INI */
static PHP_INI_MH(OnUpdate_filters) /* {{{ */
{
APCG(filters) = apc_tokenize(new_value, ',' TSRMLS_CC);
return SUCCESS;
}
/* }}} */
static PHP_INI_MH(OnUpdateShmSegments) /* {{{ */
{
#if APC_MMAP
if(zend_atoi(new_value, new_value_length)!=1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "apc.shm_segments setting ignored in MMAP mode");
}
APCG(shm_segments) = 1;
#else
APCG(shm_segments) = zend_atoi(new_value, new_value_length);
#endif
return SUCCESS;
}
/* }}} */
static PHP_INI_MH(OnUpdateShmSize) /* {{{ */
{
long s = apc_atol(new_value, new_value_length);
if(s <= 0) {
return FAILURE;
}
if(s < 1048576L) {
/* if it's less than 1Mb, they are probably using the old syntax */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "apc.shm_size now uses M/G suffixes, please update your ini files");
s = s * 1048576L;
}
APCG(shm_size) = s;
return SUCCESS;
}
/* }}} */
#ifdef MULTIPART_EVENT_FORMDATA
static PHP_INI_MH(OnUpdateRfc1867Freq) /* {{{ */
{
int tmp;
tmp = zend_atoi(new_value, new_value_length);
if(tmp < 0) {
apc_error("rfc1867_freq must be greater than or equal to zero." TSRMLS_CC);
return FAILURE;
}
if(new_value[new_value_length-1] == '%') {
if(tmp > 100) {
apc_error("rfc1867_freq cannot be over 100%%" TSRMLS_CC);
return FAILURE;
}
APCG(rfc1867_freq) = tmp / 100.0;
} else {
APCG(rfc1867_freq) = tmp;
}
return SUCCESS;
}
/* }}} */
#endif
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("apc.enabled", "1", PHP_INI_SYSTEM, OnUpdateBool, enabled, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.shm_segments", "1", PHP_INI_SYSTEM, OnUpdateShmSegments, shm_segments, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.shm_size", "32M", PHP_INI_SYSTEM, OnUpdateShmSize, shm_size, zend_apc_globals, apc_globals)
#ifdef ZEND_ENGINE_2_4
STD_PHP_INI_ENTRY("apc.shm_strings_buffer", "4M", PHP_INI_SYSTEM, OnUpdateLong, shm_strings_buffer, zend_apc_globals, apc_globals)
#endif
STD_PHP_INI_BOOLEAN("apc.include_once_override", "0", PHP_INI_SYSTEM, OnUpdateBool, include_once, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.num_files_hint", "1000", PHP_INI_SYSTEM, OnUpdateLong, num_files_hint, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.user_entries_hint", "4096", PHP_INI_SYSTEM, OnUpdateLong, user_entries_hint, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.gc_ttl", "3600", PHP_INI_SYSTEM, OnUpdateLong, gc_ttl, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.ttl", "0", PHP_INI_SYSTEM, OnUpdateLong, ttl, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.user_ttl", "0", PHP_INI_SYSTEM, OnUpdateLong, user_ttl, zend_apc_globals, apc_globals)
#if APC_MMAP
STD_PHP_INI_ENTRY("apc.mmap_file_mask", NULL, PHP_INI_SYSTEM, OnUpdateString, mmap_file_mask, zend_apc_globals, apc_globals)
#endif
PHP_INI_ENTRY("apc.filters", NULL, PHP_INI_SYSTEM, OnUpdate_filters)
STD_PHP_INI_BOOLEAN("apc.cache_by_default", "1", PHP_INI_ALL, OnUpdateBool, cache_by_default, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.file_update_protection", "2", PHP_INI_SYSTEM, OnUpdateLong,file_update_protection, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.enable_cli", "0", PHP_INI_SYSTEM, OnUpdateBool, enable_cli, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.max_file_size", "1M", PHP_INI_SYSTEM, OnUpdateLong, max_file_size, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.stat", "1", PHP_INI_SYSTEM, OnUpdateBool, fpstat, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.canonicalize", "1", PHP_INI_SYSTEM, OnUpdateBool, canonicalize, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.stat_ctime", "0", PHP_INI_SYSTEM, OnUpdateBool, stat_ctime, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.write_lock", "1", PHP_INI_SYSTEM, OnUpdateBool, write_lock, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.slam_defense", "1", PHP_INI_SYSTEM, OnUpdateBool, slam_defense, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.report_autofilter", "0", PHP_INI_SYSTEM, OnUpdateBool, report_autofilter,zend_apc_globals, apc_globals)
#ifdef MULTIPART_EVENT_FORMDATA
STD_PHP_INI_BOOLEAN("apc.rfc1867", "0", PHP_INI_SYSTEM, OnUpdateBool, rfc1867, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.rfc1867_prefix", "upload_", PHP_INI_SYSTEM, OnUpdateStringUnempty, rfc1867_prefix, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.rfc1867_name", "APC_UPLOAD_PROGRESS", PHP_INI_SYSTEM, OnUpdateStringUnempty, rfc1867_name, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.rfc1867_freq", "0", PHP_INI_SYSTEM, OnUpdateRfc1867Freq, rfc1867_freq, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.rfc1867_ttl", "3600", PHP_INI_SYSTEM, OnUpdateLong, rfc1867_ttl, zend_apc_globals, apc_globals)
#endif
STD_PHP_INI_BOOLEAN("apc.coredump_unmap", "0", PHP_INI_SYSTEM, OnUpdateBool, coredump_unmap, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.preload_path", (char*)NULL, PHP_INI_SYSTEM, OnUpdateString, preload_path, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.file_md5", "0", PHP_INI_SYSTEM, OnUpdateBool, file_md5, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.use_request_time", "1", PHP_INI_ALL, OnUpdateBool, use_request_time, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.lazy_functions", "0", PHP_INI_SYSTEM, OnUpdateBool, lazy_functions, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.lazy_classes", "0", PHP_INI_SYSTEM, OnUpdateBool, lazy_classes, zend_apc_globals, apc_globals)
STD_PHP_INI_ENTRY("apc.serializer", "default", PHP_INI_SYSTEM, OnUpdateStringUnempty, serializer_name, zend_apc_globals, apc_globals)
STD_PHP_INI_BOOLEAN("apc.enable_opcode_cache", "1", PHP_INI_SYSTEM, OnUpdateBool, enable_opcode_cache, zend_apc_globals, apc_globals)
PHP_INI_END()
/* }}} */
/* {{{ PHP_MINFO_FUNCTION(apc) */
static PHP_MINFO_FUNCTION(apc)
{
apc_serializer_t *serializer = NULL;
smart_str names = {0,};
int i;
php_info_print_table_start();
php_info_print_table_header(2, "APC Support", APCG(enabled) ? "enabled" : "disabled");
php_info_print_table_row(2, "Version", PHP_APC_VERSION);
#ifdef __DEBUG_APC__
php_info_print_table_row(2, "APC Debugging", "Enabled");
#else
php_info_print_table_row(2, "APC Debugging", "Disabled");
#endif
#if 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
php_info_print_table_row(2, "Locking type", APC_LOCK_TYPE);
for( i = 0, serializer = apc_get_serializers(TSRMLS_C);
serializer->name != NULL;
serializer++, i++) {
if(i != 0) smart_str_appends(&names, ", ");
smart_str_appends(&names, serializer->name);
}
if(names.c) {
smart_str_0(&names);
php_info_print_table_row(2, "Serialization Support", names.c);
smart_str_free(&names);
} else {
php_info_print_table_row(2, "Serialization Support", "broken");
}
php_info_print_table_row(2, "Revision", "$Revision: 329725 $");
php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
#ifdef MULTIPART_EVENT_FORMDATA
extern int apc_rfc1867_progress(unsigned int event, void *event_data, void **extra TSRMLS_DC);
#endif
/* {{{ PHP_MINIT_FUNCTION(apc) */
static PHP_MINIT_FUNCTION(apc)
{
ZEND_INIT_MODULE_GLOBALS(apc, php_apc_init_globals, php_apc_shutdown_globals);
REGISTER_INI_ENTRIES();
/* Disable APC in cli mode unless overridden by apc.enable_cli */
if(!APCG(enable_cli) && !strcmp(sapi_module.name, "cli")) {
APCG(enabled) = 0;
}
if (APCG(enabled)) {
if(APCG(initialized)) {
apc_process_init(module_number TSRMLS_CC);
} else {
apc_module_init(module_number TSRMLS_CC);
apc_zend_init(TSRMLS_C);
apc_process_init(module_number TSRMLS_CC);
#ifdef MULTIPART_EVENT_FORMDATA
/* File upload progress tracking */
if(APCG(rfc1867)) {
php_rfc1867_callback = apc_rfc1867_progress;
}
#endif
apc_iterator_init(module_number TSRMLS_CC);
}
zend_register_long_constant("APC_BIN_VERIFY_MD5", sizeof("APC_BIN_VERIFY_MD5"), APC_BIN_VERIFY_MD5, (CONST_CS | CONST_PERSISTENT), module_number TSRMLS_CC);
zend_register_long_constant("APC_BIN_VERIFY_CRC32", sizeof("APC_BIN_VERIFY_CRC32"), APC_BIN_VERIFY_CRC32, (CONST_CS | CONST_PERSISTENT), module_number TSRMLS_CC);
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION(apc) */
static PHP_MSHUTDOWN_FUNCTION(apc)
{
if(APCG(enabled)) {
apc_process_shutdown(TSRMLS_C);
apc_zend_shutdown(TSRMLS_C);
apc_module_shutdown(TSRMLS_C);
#ifndef ZTS
php_apc_shutdown_globals(&apc_globals);
#endif
#if HAVE_SIGACTION
apc_shutdown_signals(TSRMLS_C);
#endif
}
#ifdef ZTS
ts_free_id(apc_globals_id);
#endif
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION(apc) */
static PHP_RINIT_FUNCTION(apc)
{
if(APCG(enabled)) {
apc_request_init(TSRMLS_C);
#if HAVE_SIGACTION
apc_set_signals(TSRMLS_C);
#endif
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION(apc) */
static PHP_RSHUTDOWN_FUNCTION(apc)
{
if(APCG(enabled)) {
apc_request_shutdown(TSRMLS_C);
}
return SUCCESS;
}
/* }}} */
/* {{{ proto array apc_cache_info([string type [, bool limited]]) */
PHP_FUNCTION(apc_cache_info)
{
zval* info;
char *cache_type;
int ct_len;
zend_bool limited = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &cache_type, &ct_len, &limited) == FAILURE) {
return;
}
if(ZEND_NUM_ARGS()) {
if(!strcasecmp(cache_type,"user")) {
info = apc_cache_info(apc_user_cache, limited TSRMLS_CC);
} else if(!strcasecmp(cache_type,"filehits")) {
#ifdef APC_FILEHITS
RETVAL_ZVAL(APCG(filehits), 1, 0);
return;
#else
RETURN_FALSE;
#endif
} else {
info = apc_cache_info(apc_cache, limited TSRMLS_CC);
}
} else {
info = apc_cache_info(apc_cache, limited TSRMLS_CC);
}
if(!info) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No APC info available. Perhaps APC is not enabled? Check apc.enabled in your ini file");
RETURN_FALSE;
}
RETURN_ZVAL(info, 0, 1);
}
/* }}} */
/* {{{ proto void apc_clear_cache([string cache]) */
PHP_FUNCTION(apc_clear_cache)
{
char *cache_type;
int ct_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cache_type, &ct_len) == FAILURE) {
return;
}
if(ct_len) {
if(!strcasecmp(cache_type, "user")) {
apc_cache_clear(apc_user_cache TSRMLS_CC);
RETURN_TRUE;
}
}
apc_cache_clear(apc_cache TSRMLS_CC);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto array apc_sma_info([bool limited]) */
PHP_FUNCTION(apc_sma_info)
{
apc_sma_info_t* info;
zval* block_lists;
int i;
zend_bool limited = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &limited) == FAILURE) {
return;
}
info = apc_sma_info(limited TSRMLS_CC);
if(!info) {
php_error_docref(NULL TSRMLS_CC, 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());
if(limited) {
apc_sma_free_info(info TSRMLS_CC);
return;
}
#if ALLOC_DISTRIBUTION
{
size_t *adist = apc_sma_get_alloc_distribution();
zval* list;
ALLOC_INIT_ZVAL(list);
array_init(list);
for(i=0; i<30; i++) {
add_next_index_long(list, adist[i]);
}
add_assoc_zval(return_value, "adist", list);
}
#endif
ALLOC_INIT_ZVAL(block_lists);
array_init(block_lists);
for (i = 0; i < info->num_seg; i++) {
apc_sma_link_t* p;
zval* list;
ALLOC_INIT_ZVAL(list);
array_init(list);
for (p = info->list[i]; p != NULL; p = p->next) {
zval* link;
ALLOC_INIT_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(info TSRMLS_CC);
}
/* }}} */
/* {{{ _apc_update */
int _apc_update(char *strkey, int strkey_len, apc_cache_updater_t updater, void* data TSRMLS_DC)
{
if(!APCG(enabled)) {
return 0;
}
if (!APCG(serializer) && APCG(serializer_name)) {
/* Avoid race conditions between MINIT of apc and serializer exts like igbinary */
APCG(serializer) = apc_find_serializer(APCG(serializer_name) TSRMLS_CC);
}
HANDLE_BLOCK_INTERRUPTIONS();
APCG(current_cache) = apc_user_cache;
if (!_apc_cache_user_update(apc_user_cache, strkey, strkey_len + 1, updater, data TSRMLS_CC)) {
HANDLE_UNBLOCK_INTERRUPTIONS();
return 0;
}
APCG(current_cache) = NULL;
HANDLE_UNBLOCK_INTERRUPTIONS();
return 1;
}
/* }}} */
/* {{{ _apc_store */
int _apc_store(char *strkey, int strkey_len, const zval *val, const unsigned int ttl, const int exclusive TSRMLS_DC) {
apc_cache_entry_t *entry;
apc_cache_key_t key;
time_t t;
apc_context_t ctxt={0,};
int ret = 1;
t = apc_time();
if(!APCG(enabled)) return 0;
if (!APCG(serializer) && APCG(serializer_name)) {
/* Avoid race conditions between MINIT of apc and serializer exts like igbinary */
APCG(serializer) = apc_find_serializer(APCG(serializer_name) TSRMLS_CC);
}
HANDLE_BLOCK_INTERRUPTIONS();
APCG(current_cache) = apc_user_cache;
ctxt.pool = apc_pool_create(APC_SMALL_POOL, apc_sma_malloc, apc_sma_free, apc_sma_protect, apc_sma_unprotect TSRMLS_CC);
if (!ctxt.pool) {
HANDLE_UNBLOCK_INTERRUPTIONS();
apc_warning("Unable to allocate memory for pool." TSRMLS_CC);
return 0;
}
ctxt.copy = APC_COPY_IN_USER;
ctxt.force_update = 0;
if(!ctxt.pool) {
ret = 0;
goto nocache;
}
if (!apc_cache_make_user_key(&key, strkey, strkey_len, t)) {
goto freepool;
}
if (apc_cache_is_last_key(apc_user_cache, &key, t TSRMLS_CC)) {
goto freepool;
}
if (!(entry = apc_cache_make_user_entry(strkey, strkey_len, val, &ctxt, ttl TSRMLS_CC))) {
goto freepool;
}
if (!apc_cache_user_insert(apc_user_cache, key, entry, &ctxt, t, exclusive TSRMLS_CC)) {
freepool:
apc_pool_destroy(ctxt.pool TSRMLS_CC);
ret = 0;
}
nocache:
APCG(current_cache) = NULL;
HANDLE_UNBLOCK_INTERRUPTIONS();
return ret;
}
/* }}} */
/* {{{ apc_store_helper(INTERNAL_FUNCTION_PARAMETERS, const int exclusive)
*/
static void apc_store_helper(INTERNAL_FUNCTION_PARAMETERS, const int exclusive)
{
zval *key = NULL;
zval *val = NULL;
long ttl = 0L;
HashTable *hash;
HashPosition hpos;
zval **hentry;
char *hkey=NULL;
uint hkey_len;
ulong hkey_idx;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zl", &key, &val, &ttl) == FAILURE) {
return;
}
if (!key) RETURN_FALSE;
if (Z_TYPE_P(key) == IS_ARRAY) {
hash = Z_ARRVAL_P(key);
array_init(return_value);
zend_hash_internal_pointer_reset_ex(hash, &hpos);
while(zend_hash_get_current_data_ex(hash, (void**)&hentry, &hpos) == SUCCESS) {
zend_hash_get_current_key_ex(hash, &hkey, &hkey_len, &hkey_idx, 0, &hpos);
if (hkey) {
if(!_apc_store(hkey, hkey_len, *hentry, (unsigned int)ttl, exclusive TSRMLS_CC)) {
add_assoc_long_ex(return_value, hkey, hkey_len, -1); /* -1: insertion error */
}
hkey = NULL;
} else {
add_index_long(return_value, hkey_idx, -1); /* -1: insertion error */
}
zend_hash_move_forward_ex(hash, &hpos);
}
return;
} else if (Z_TYPE_P(key) == IS_STRING) {
if (!val) RETURN_FALSE;
if(_apc_store(Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, val, (unsigned int)ttl, exclusive TSRMLS_CC))
RETURN_TRUE;
} else {
apc_warning("apc_store expects key parameter to be a string or an array of key/value pairs." TSRMLS_CC);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto int apc_store(mixed key, mixed var [, long ttl ])
*/
PHP_FUNCTION(apc_store) {
apc_store_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto int apc_add(mixed key, mixed var [, long ttl ])
*/
PHP_FUNCTION(apc_add) {
apc_store_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ inc_updater */
struct _inc_update_args {
long step;
long lval;
};
static int inc_updater(apc_cache_t* cache, apc_cache_entry_t* entry, void* data) {
struct _inc_update_args *args = (struct _inc_update_args*) data;
zval* val = entry->data.user.val;
if(Z_TYPE_P(val) == IS_LONG) {
Z_LVAL_P(val) += args->step;
args->lval = Z_LVAL_P(val);
return 1;
}
return 0;
}
/* }}} */
/* {{{ proto long apc_inc(string key [, long step [, bool& success]])
*/
PHP_FUNCTION(apc_inc) {
char *strkey;
int strkey_len;
struct _inc_update_args args = {1L, -1};
zval *success = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz", &strkey, &strkey_len, &(args.step), &success) == FAILURE) {
return;
}
if (success) {
zval_dtor(success);
}
if(_apc_update(strkey, strkey_len, inc_updater, &args TSRMLS_CC)) {
if(success) ZVAL_TRUE(success);
RETURN_LONG(args.lval);
}
if(success) ZVAL_FALSE(success);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto long apc_dec(string key [, long step [, bool &success]])
*/
PHP_FUNCTION(apc_dec) {
char *strkey;
int strkey_len;
struct _inc_update_args args = {1L, -1};
zval *success = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz", &strkey, &strkey_len, &(args.step), &success) == FAILURE) {
return;
}
if (success) {
zval_dtor(success);
}
args.step = args.step * -1;
if(_apc_update(strkey, strkey_len, inc_updater, &args TSRMLS_CC)) {
if(success) ZVAL_TRUE(success);
RETURN_LONG(args.lval);
}
if(success) ZVAL_FALSE(success);
RETURN_FALSE;
}
/* }}} */
/* {{{ cas_updater */
static int cas_updater(apc_cache_t* cache, apc_cache_entry_t* entry, void* data) {
long* vals = ((long*)data);
long old = vals[0];
long new = vals[1];
zval* val = entry->data.user.val;
if(Z_TYPE_P(val) == IS_LONG) {
if(Z_LVAL_P(val) == old) {
Z_LVAL_P(val) = new;
return 1;
}
}
return 0;
}
/* }}} */
/* {{{ proto int apc_cas(string key, int old, int new)
*/
PHP_FUNCTION(apc_cas) {
char *strkey;
int strkey_len;
long vals[2];
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &strkey, &strkey_len, &vals[0], &vals[1]) == FAILURE) {
return;
}
if(_apc_update(strkey, strkey_len, cas_updater, &vals TSRMLS_CC)) RETURN_TRUE;
RETURN_FALSE;
}
/* }}} */
void *apc_erealloc_wrapper(void *ptr, size_t size) {
return _erealloc(ptr, size, 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
}
/* {{{ proto mixed apc_fetch(mixed key[, bool &success])
*/
PHP_FUNCTION(apc_fetch) {
zval *key;
zval *success = NULL;
HashTable *hash;
HashPosition hpos;
zval **hentry;
zval *result;
zval *result_entry;
char *strkey;
int strkey_len;
apc_cache_entry_t* entry;
time_t t;
apc_context_t ctxt = {0,};
if(!APCG(enabled)) RETURN_FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &key, &success) == FAILURE) {
return;
}
t = apc_time();
if (success) {
ZVAL_BOOL(success, 0);
}
ctxt.pool = apc_pool_create(APC_UNPOOL, apc_php_malloc, apc_php_free, NULL, NULL TSRMLS_CC);
if (!ctxt.pool) {
apc_warning("Unable to allocate memory for pool." TSRMLS_CC);
RETURN_FALSE;
}
ctxt.copy = APC_COPY_OUT_USER;
ctxt.force_update = 0;
if(Z_TYPE_P(key) != IS_STRING && Z_TYPE_P(key) != IS_ARRAY) {
convert_to_string(key);
}
if(Z_TYPE_P(key) == IS_STRING) {
strkey = Z_STRVAL_P(key);
strkey_len = Z_STRLEN_P(key);
if(!strkey_len) RETURN_FALSE;
entry = apc_cache_user_find(apc_user_cache, strkey, (strkey_len + 1), t TSRMLS_CC);
if(entry) {
/* deep-copy returned shm zval to emalloc'ed return_value */
apc_cache_fetch_zval(return_value, entry->data.user.val, &ctxt TSRMLS_CC);
apc_cache_release(apc_user_cache, entry TSRMLS_CC);
} else {
goto freepool;
}
} else if(Z_TYPE_P(key) == IS_ARRAY) {
hash = Z_ARRVAL_P(key);
MAKE_STD_ZVAL(result);
array_init(result);
zend_hash_internal_pointer_reset_ex(hash, &hpos);
while(zend_hash_get_current_data_ex(hash, (void**)&hentry, &hpos) == SUCCESS) {
if(Z_TYPE_PP(hentry) != IS_STRING) {
apc_warning("apc_fetch() expects a string or array of strings." TSRMLS_CC);
goto freepool;
}
entry = apc_cache_user_find(apc_user_cache, Z_STRVAL_PP(hentry), (Z_STRLEN_PP(hentry) + 1), t TSRMLS_CC);
if(entry) {
/* deep-copy returned shm zval to emalloc'ed return_value */
MAKE_STD_ZVAL(result_entry);
apc_cache_fetch_zval(result_entry, entry->data.user.val, &ctxt TSRMLS_CC);
apc_cache_release(apc_user_cache, entry TSRMLS_CC);
zend_hash_add(Z_ARRVAL_P(result), Z_STRVAL_PP(hentry), Z_STRLEN_PP(hentry) +1, &result_entry, sizeof(zval*), NULL);
} /* don't set values we didn't find */
zend_hash_move_forward_ex(hash, &hpos);
}
RETVAL_ZVAL(result, 0, 1);
} else {
apc_warning("apc_fetch() expects a string or array of strings." TSRMLS_CC);
freepool:
apc_pool_destroy(ctxt.pool TSRMLS_CC);
RETURN_FALSE;
}
if (success) {
ZVAL_BOOL(success, 1);
}
apc_pool_destroy(ctxt.pool TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto mixed apc_exists(mixed key)
*/
PHP_FUNCTION(apc_exists) {
zval *key;
HashTable *hash;
HashPosition hpos;
zval **hentry;
char *strkey;
int strkey_len;
apc_cache_entry_t* entry;
zval *result;
zval *result_entry;
time_t t;
if(!APCG(enabled)) RETURN_FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &key) == FAILURE) {
return;
}
t = apc_time();
if(Z_TYPE_P(key) != IS_STRING && Z_TYPE_P(key) != IS_ARRAY) {
convert_to_string(key);
}
if(Z_TYPE_P(key) == IS_STRING) {
strkey = Z_STRVAL_P(key);
strkey_len = Z_STRLEN_P(key);
if(!strkey_len) RETURN_FALSE;
entry = apc_cache_user_exists(apc_user_cache, strkey, strkey_len + 1, t TSRMLS_CC);
if(entry) {
RETURN_TRUE;
}
} else if(Z_TYPE_P(key) == IS_ARRAY) {
hash = Z_ARRVAL_P(key);
MAKE_STD_ZVAL(result);
array_init(result);
zend_hash_internal_pointer_reset_ex(hash, &hpos);
while(zend_hash_get_current_data_ex(hash, (void**)&hentry, &hpos) == SUCCESS) {
if(Z_TYPE_PP(hentry) != IS_STRING) {
apc_warning("apc_exists() expects a string or array of strings." TSRMLS_CC);
RETURN_FALSE;
}
entry = apc_cache_user_exists(apc_user_cache, Z_STRVAL_PP(hentry), Z_STRLEN_PP(hentry) + 1, t TSRMLS_CC);
if(entry) {
MAKE_STD_ZVAL(result_entry);
ZVAL_BOOL(result_entry, 1);
zend_hash_add(Z_ARRVAL_P(result), Z_STRVAL_PP(hentry), Z_STRLEN_PP(hentry) +1, &result_entry, sizeof(zval*), NULL);
} /* don't set values we didn't find */
zend_hash_move_forward_ex(hash, &hpos);
}
RETURN_ZVAL(result, 0, 1);
} else {
apc_warning("apc_exists() expects a string or array of strings." TSRMLS_CC);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto mixed apc_delete(mixed keys)
*/
PHP_FUNCTION(apc_delete) {
zval *keys;
if(!APCG(enabled)) RETURN_FALSE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &keys) == FAILURE) {
return;
}
if (Z_TYPE_P(keys) == IS_STRING) {
if (!Z_STRLEN_P(keys)) RETURN_FALSE;
if(apc_cache_user_delete(apc_user_cache, Z_STRVAL_P(keys), (Z_STRLEN_P(keys) + 1) TSRMLS_CC)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
} else if (Z_TYPE_P(keys) == IS_ARRAY) {
HashTable *hash = Z_ARRVAL_P(keys);
HashPosition hpos;
zval **hentry;
array_init(return_value);
zend_hash_internal_pointer_reset_ex(hash, &hpos);
while(zend_hash_get_current_data_ex(hash, (void**)&hentry, &hpos) == SUCCESS) {
if(Z_TYPE_PP(hentry) != IS_STRING) {
apc_warning("apc_delete() expects a string, array of strings, or APCIterator instance." TSRMLS_CC);
add_next_index_zval(return_value, *hentry);
Z_ADDREF_PP(hentry);
} else if(apc_cache_user_delete(apc_user_cache, Z_STRVAL_PP(hentry), (Z_STRLEN_PP(hentry) + 1) TSRMLS_CC) != 1) {
add_next_index_zval(return_value, *hentry);
Z_ADDREF_PP(hentry);
}
zend_hash_move_forward_ex(hash, &hpos);