forked from krakjoe/apcu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapc_cache.c
1721 lines (1394 loc) · 45.6 KB
/
apc_cache.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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Cowgill <[email protected]> |
| Rasmus Lerdorf <[email protected]> |
| Arun C. Murthy <[email protected]> |
| Gopal Vijayaraghavan <[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.
*/
/* $Id: apc_cache.c 328743 2012-12-12 07:58:32Z ab $ */
#include "apc_cache.h"
#include "apc_sma.h"
#include "apc_globals.h"
#include "php_scandir.h"
#include "SAPI.h"
#include "TSRM.h"
#include "php_main.h"
#include "ext/standard/md5.h"
#include "ext/standard/php_var.h"
#include "ext/standard/php_smart_str.h"
typedef void* (*ht_copy_fun_t)(void*, void*, apc_context_t* TSRMLS_DC);
typedef int (*ht_check_copy_fun_t)(Bucket*, va_list);
#define CHECK(p) { if ((p) == NULL) return NULL; }
static APC_HOTSPOT zval* my_copy_zval(zval* dst, const zval* src, apc_context_t* ctxt TSRMLS_DC);
static HashTable* my_copy_hashtable_ex(HashTable*, HashTable* TSRMLS_DC, ht_copy_fun_t, int, apc_context_t*, ht_check_copy_fun_t, ...);
#define my_copy_hashtable( dst, src, copy_fn, holds_ptr, ctxt) \
my_copy_hashtable_ex(dst, src TSRMLS_CC, copy_fn, holds_ptr, ctxt, NULL)
/* {{{ make_prime */
static int const primes[] = {
257, /* 256 */
521, /* 512 */
1031, /* 1024 */
2053, /* 2048 */
3079, /* 3072 */
4099, /* 4096 */
5147, /* 5120 */
6151, /* 6144 */
7177, /* 7168 */
8209, /* 8192 */
9221, /* 9216 */
10243, /* 10240 */
11273, /* 11264 */
12289, /* 12288 */
13313, /* 13312 */
14341, /* 14336 */
15361, /* 15360 */
16411, /* 16384 */
17417, /* 17408 */
18433, /* 18432 */
19457, /* 19456 */
20483, /* 20480 */
30727, /* 30720 */
40961, /* 40960 */
61441, /* 61440 */
81929, /* 81920 */
122887,/* 122880 */
163841,/* 163840 */
245771,/* 245760 */
327689,/* 327680 */
491527,/* 491520 */
655373,/* 655360 */
983063,/* 983040 */
0 /* sentinel */
};
static int make_prime(int n)
{
int *k = (int*)primes;
while(*k) {
if((*k) > n) return *k;
k++;
}
return *(k-1);
}
/* }}} */
/* {{{ make_slot */
apc_cache_slot_t* make_slot(apc_cache_t* cache, apc_cache_key_t *key, apc_cache_entry_t* value, apc_cache_slot_t* next, time_t t TSRMLS_DC)
{
apc_cache_slot_t* p = NULL;
/* allocate slot */
if ((p = value->pool->palloc(value->pool, sizeof(apc_cache_slot_t) TSRMLS_CC))) {
/* copy identifier */
char* strkey = (char*) apc_pmemcpy(
key->str, key->len,
value->pool TSRMLS_CC
);
if (strkey) {
/* set idenfieir */
key->str = strkey;
/* set slot data */
p->key = key[0];
p->value = value;
/* set slot relation */
p->next = next;
/* set slot defaults */
p->nhits = 0;
p->ctime = t;
p->atime = t;
p->dtime = 0;
}
}
return p;
}
/* }}} */
/* {{{ free_slot */
static void free_slot(apc_cache_slot_t* slot TSRMLS_DC)
{
/* destroy slot pool */
apc_pool_destroy(
slot->value->pool TSRMLS_CC);
}
/* }}} */
/* {{{ apc_cache_hash_slot
Note: These calculations can and should be done outside of a lock */
static void apc_cache_hash_slot(apc_cache_t* cache,
char *str,
zend_uint len,
zend_ulong* hash,
zend_ulong* slot) {
(*hash) = zend_inline_hash_func(str, len);
(*slot) = (*hash) % (cache->nslots);
} /* }}} */
/* {{{ apc_cache_remove_slot */
PHP_APCU_API void apc_cache_remove_slot(apc_cache_t* cache, apc_cache_slot_t** slot TSRMLS_DC)
{
apc_cache_slot_t* dead = *slot;
/* think here is safer */
*slot = (*slot)->next;
/* adjust header info */
if (cache->header->mem_size)
cache->header->mem_size -= dead->value->mem_size;
if (cache->header->nentries)
cache->header->nentries--;
/* remove if there are no references */
if (dead->value->ref_count <= 0) {
free_slot(dead TSRMLS_CC);
} else {
/* add to gc if there are still refs */
dead->next = cache->header->gc;
dead->dtime = time(0);
cache->header->gc = dead;
}
}
/* }}} */
/* {{{ apc_cache_gc */
PHP_APCU_API void apc_cache_gc(apc_cache_t* cache TSRMLS_DC)
{
/* This function scans the list of removed cache entries and deletes any
* entry whose reference count is zero or that has been on the gc
* list for more than cache->gc_ttl seconds
* (we issue a warning in the latter case).
*/
if (!cache || !cache->header->gc) {
return;
}
{
apc_cache_slot_t** slot = &cache->header->gc;
while (*slot != NULL) {
time_t now = time(0);
time_t gc_sec = cache->gc_ttl ? (now - (*slot)->dtime) : 0;
if (!(*slot)->value->ref_count || gc_sec > (time_t)cache->gc_ttl) {
apc_cache_slot_t* dead = *slot;
/* good ol' whining */
if (dead->value->ref_count > 0) {
apc_debug(
"GC cache entry '%s' was on gc-list for %d seconds" TSRMLS_CC,
dead->key.str, gc_sec
);
}
/* set next slot */
*slot = dead->next;
/* free slot */
free_slot(
dead TSRMLS_CC);
/* next */
continue;
} else {
slot = &(*slot)->next;
}
}
}
}
/* }}} */
/* {{{ php serializer */
PHP_APCU_API int APC_SERIALIZER_NAME(php) (APC_SERIALIZER_ARGS)
{
smart_str strbuf = {0};
php_serialize_data_t var_hash;
PHP_VAR_SERIALIZE_INIT(var_hash);
php_var_serialize(&strbuf, (zval**)&value, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
if(strbuf.c) {
*buf = (unsigned char*)strbuf.c;
*buf_len = strbuf.len;
smart_str_0(&strbuf);
return 1;
}
return 0;
} /* }}} */
/* {{{ php unserializer */
PHP_APCU_API int APC_UNSERIALIZER_NAME(php) (APC_UNSERIALIZER_ARGS)
{
const unsigned char *tmp = buf;
php_unserialize_data_t var_hash;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
if(!php_var_unserialize(value, &tmp, buf + buf_len, &var_hash TSRMLS_CC)) {
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
zval_dtor(*value);
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Error at offset %ld of %ld bytes", (long)(tmp - buf), (long)buf_len);
(*value)->type = IS_NULL;
return 0;
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
return 1;
} /* }}} */
/* {{{ apc_cache_create */
PHP_APCU_API apc_cache_t* apc_cache_create(apc_sma_t* sma, apc_serializer_t* serializer, int size_hint, int gc_ttl, int ttl, long smart, zend_bool defend TSRMLS_DC) {
apc_cache_t* cache;
int cache_size;
int nslots;
/* calculate number of slots */
nslots = make_prime(size_hint > 0 ? size_hint : 2000);
/* allocate pointer by normal means */
cache = (apc_cache_t*) apc_emalloc(sizeof(apc_cache_t) TSRMLS_CC);
/* calculate cache size for shm allocation */
cache_size = sizeof(apc_cache_header_t) + nslots*sizeof(apc_cache_slot_t*);
/* allocate shm */
cache->shmaddr = sma->smalloc(cache_size TSRMLS_CC);
if(!cache->shmaddr) {
apc_error("Unable to allocate shared memory for cache structures. (Perhaps your shared memory size isn't large enough?). " TSRMLS_CC);
return NULL;
}
/* zero shm */
memset(cache->shmaddr, 0, cache_size);
/* set default header */
cache->header = (apc_cache_header_t*) cache->shmaddr;
cache->header->nhits = 0;
cache->header->nmisses = 0;
cache->header->nentries = 0;
cache->header->nexpunges = 0;
cache->header->gc = NULL;
cache->header->stime = time(NULL);
cache->header->state |= APC_CACHE_ST_NONE;
/* set cache options */
cache->slots = (apc_cache_slot_t**) (((char*) cache->shmaddr) + sizeof(apc_cache_header_t));
cache->sma = sma;
cache->serializer = serializer;
cache->nslots = nslots;
cache->gc_ttl = gc_ttl;
cache->ttl = ttl;
cache->smart = smart;
cache->defend = defend;
/* header lock */
CREATE_LOCK(&cache->header->lock);
/* zero slots */
memset(cache->slots, 0, sizeof(apc_cache_slot_t*)*nslots);
return cache;
} /* }}} */
/* {{{ apc_cache_store */
PHP_APCU_API zend_bool apc_cache_store(apc_cache_t* cache, char *strkey, zend_uint keylen, const zval *val, const zend_uint ttl, const zend_bool exclusive TSRMLS_DC) {
apc_cache_entry_t *entry;
apc_cache_key_t key;
time_t t;
apc_context_t ctxt={0,};
zend_bool ret = 0;
t = apc_time();
/* initialize a context suitable for making an insert */
if (apc_cache_make_context(cache, &ctxt, APC_CONTEXT_SHARE, APC_SMALL_POOL, APC_COPY_IN, 0 TSRMLS_CC)) {
/* initialize the key for insertion */
if (apc_cache_make_key(&key, strkey, keylen TSRMLS_CC)) {
/* run cache defense */
if (!apc_cache_defense(cache, &key TSRMLS_CC)) {
/* initialize the entry for insertion */
if ((entry = apc_cache_make_entry(&ctxt, &key, val, ttl TSRMLS_CC))) {
/* execute an insertion */
if (apc_cache_insert(cache, key, entry, &ctxt, t, exclusive TSRMLS_CC)) {
ret = 1;
}
}
}
}
/* in any case of failure the context should be destroyed */
if (!ret) {
apc_cache_destroy_context(&ctxt TSRMLS_CC);
}
}
return ret;
} /* }}} */
/* {{{ data_unserialize */
static zval* data_unserialize(const char *filename TSRMLS_DC)
{
zval* retval;
long len = 0;
struct stat sb;
char *contents, *tmp;
FILE *fp;
php_unserialize_data_t var_hash = {0,};
if(VCWD_STAT(filename, &sb) == -1) {
return NULL;
}
fp = fopen(filename, "rb");
len = sizeof(char)*sb.st_size;
tmp = contents = malloc(len);
if(!contents) {
fclose(fp);
return NULL;
}
if(fread(contents, 1, len, fp) < 1) {
fclose(fp);
free(contents);
return NULL;
}
MAKE_STD_ZVAL(retval);
PHP_VAR_UNSERIALIZE_INIT(var_hash);
/* I wish I could use json */
if(!php_var_unserialize(&retval, (const unsigned char**)&tmp, (const unsigned char*)(contents+len), &var_hash TSRMLS_CC)) {
fclose(fp);
zval_ptr_dtor(&retval);
return NULL;
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
free(contents);
fclose(fp);
return retval;
}
static int apc_load_data(apc_cache_t* cache, const char *data_file TSRMLS_DC)
{
char *p;
char key[MAXPATHLEN] = {0,};
unsigned int key_len;
zval *data;
p = strrchr(data_file, DEFAULT_SLASH);
if(p && p[1]) {
strlcpy(key, p+1, sizeof(key));
p = strrchr(key, '.');
if(p) {
p[0] = '\0';
key_len = strlen(key)+1;
data = data_unserialize(data_file TSRMLS_CC);
if(data) {
apc_cache_store(cache, key, key_len, data, 0, 1 TSRMLS_CC);
}
return 1;
}
}
return 0;
}
/* {{{ apc_cache_preload shall load the prepared data files in path into the specified cache */
PHP_APCU_API zend_bool apc_cache_preload(apc_cache_t* cache, const char *path TSRMLS_DC)
{
#ifndef ZTS
zend_bool result = 0;
char file[MAXPATHLEN]={0,};
int ndir, i;
char *p = NULL;
struct dirent **namelist = NULL;
if ((ndir = php_scandir(path, &namelist, 0, php_alphasort)) > 0) {
for (i = 0; i < ndir; i++) {
/* check for extension */
if (!(p = strrchr(namelist[i]->d_name, '.'))
|| (p && strcmp(p, ".data"))) {
free(namelist[i]);
continue;
}
snprintf(file, MAXPATHLEN, "%s%c%s",
path, DEFAULT_SLASH, namelist[i]->d_name);
if(apc_load_data(cache, file TSRMLS_CC)) {
result = 1;
}
free(namelist[i]);
}
free(namelist);
}
return result;
#else
apc_error("Cannot load data from apc.preload_path=%s in thread-safe mode" TSRMLS_CC, path);
return 0;
#endif
} /* }}} */
/* {{{ apc_cache_release */
PHP_APCU_API void apc_cache_release(apc_cache_t* cache, apc_cache_entry_t* entry TSRMLS_DC)
{
entry->ref_count--;
}
/* }}} */
/* {{{ apc_cache_destroy */
PHP_APCU_API void apc_cache_destroy(apc_cache_t* cache TSRMLS_DC)
{
if (!cache) {
return;
}
/* destroy lock */
DESTROY_LOCK(&cache->header->lock);
/* XXX this is definitely a leak, but freeing this causes all the apache
children to freeze. It might be because the segment is shared between
several processes. To figure out is how to free this safely. */
/*apc_sma_free(cache->shmaddr);*/
apc_efree(cache TSRMLS_CC);
}
/* }}} */
/* {{{ apc_cache_real_expunge */
PHP_APCU_API void apc_cache_real_expunge(apc_cache_t* cache TSRMLS_DC) {
/* increment counter */
cache->header->nexpunges++;
/* expunge */
{
zend_ulong i;
for (i = 0; i < cache->nslots; i++) {
apc_cache_slot_t* p = cache->slots[i];
while (p) {
apc_cache_remove_slot(cache, &p TSRMLS_CC);
}
cache->slots[i] = NULL;
}
}
/* set new time so counters make sense */
cache->header->stime = apc_time();
/* reset counters */
cache->header->ninserts = 0;
cache->header->nentries = 0;
cache->header->nhits = 0;
cache->header->nmisses = 0;
/* resets lastkey */
memset(&cache->header->lastkey, 0, sizeof(apc_cache_key_t));
} /* }}} */
/* {{{ apc_cache_clear */
PHP_APCU_API void apc_cache_clear(apc_cache_t* cache TSRMLS_DC)
{
/* check there is a cache and it is not busy */
if(!cache || apc_cache_busy(cache TSRMLS_CC)) {
return;
}
/* lock header */
APC_LOCK(cache->header);
/* set busy */
cache->header->state |= APC_CACHE_ST_BUSY;
/* expunge cache */
apc_cache_real_expunge(cache TSRMLS_CC);
/* set info */
cache->header->stime = apc_time();
cache->header->nexpunges = 0;
/* unset busy */
cache->header->state &= ~APC_CACHE_ST_BUSY;
/* unlock header */
APC_UNLOCK(cache->header);
}
/* }}} */
/* {{{ apc_cache_default_expunge */
PHP_APCU_API void apc_cache_default_expunge(apc_cache_t* cache, size_t size TSRMLS_DC)
{
time_t t;
size_t suitable = 0L;
size_t available = 0L;
t = apc_time();
/* check there is a cache, and it is not busy */
if(!cache || apc_cache_busy(cache TSRMLS_CC)) {
return;
}
/* get the lock for header */
APC_LOCK(cache->header);
/* update state in header */
cache->header->state |= APC_CACHE_ST_BUSY;
/* make suitable selection */
suitable = (cache->smart > 0L) ? (size_t) (cache->smart * size) : (size_t) (cache->sma->size/2);
/* gc */
apc_cache_gc(cache TSRMLS_CC);
/* get available */
available = cache->sma->get_avail_mem();
/* perform expunge processing */
if(!cache->ttl) {
/* check it is necessary to expunge */
if (available < suitable) {
apc_cache_real_expunge(cache TSRMLS_CC);
}
} else {
apc_cache_slot_t **slot;
/* check that expunge is necessary */
if (available < suitable) {
zend_ulong i;
/* look for junk */
for (i = 0; i < cache->nslots; i++) {
slot = &cache->slots[i];
while (*slot) {
/*
* Entry TTL has precedence over cache TTL
*/
if((*slot)->value->ttl) {
if((time_t) ((*slot)->ctime + (*slot)->value->ttl) < t) {
apc_cache_remove_slot(cache, slot TSRMLS_CC);
continue;
}
} else if(cache->ttl) {
if((time_t) ((*slot)->ctime + cache->ttl) < t) {
apc_cache_remove_slot(cache, slot TSRMLS_CC);
continue;
}
}
/* grab next slot */
slot = &(*slot)->next;
}
}
/* if the cache now has space, then reset last key */
if (cache->sma->get_avail_size(size)) {
/* wipe lastkey */
memset(&cache->header->lastkey, 0, sizeof(apc_cache_key_t));
} else {
/* with not enough space left in cache, we are forced to expunge */
apc_cache_real_expunge(cache TSRMLS_CC);
}
}
}
/* we are done */
cache->header->state &= ~APC_CACHE_ST_BUSY;
/* unlock header */
APC_UNLOCK(cache->header);
}
/* }}} */
/* {{{ apc_cache_make_context */
PHP_APCU_API zend_bool apc_cache_make_context(apc_cache_t* cache,
apc_context_t* context,
apc_context_type context_type,
apc_pool_type pool_type,
apc_copy_type copy_type,
uint force_update TSRMLS_DC) {
switch (context_type) {
case APC_CONTEXT_SHARE: {
return apc_cache_make_context_ex(
context,
cache->serializer,
(apc_malloc_t) cache->sma->smalloc,
cache->sma->sfree,
cache->sma->protect,
cache->sma->unprotect,
pool_type, copy_type, force_update TSRMLS_CC
);
} break;
case APC_CONTEXT_NOSHARE: {
return apc_cache_make_context_ex(
context,
cache->serializer,
apc_php_malloc, apc_php_free, NULL, NULL,
pool_type, copy_type, force_update TSRMLS_CC
);
} break;
case APC_CONTEXT_NONE:
/* never used, just to make gcc warning free */
break;
}
return 0;
} /* }}} */
/* {{{ apc_cache_make_context_ex */
PHP_APCU_API zend_bool apc_cache_make_context_ex(apc_context_t* context,
apc_serializer_t* serializer,
apc_malloc_t _malloc,
apc_free_t _free,
apc_protect_t _protect,
apc_unprotect_t _unprotect,
apc_pool_type pool_type,
apc_copy_type copy_type,
uint force_update TSRMLS_DC) {
/* attempt to create the pool */
context->pool = apc_pool_create(
pool_type, _malloc, _free, _protect, _unprotect TSRMLS_CC
);
if (!context->pool) {
apc_warning("Unable to allocate memory for pool." TSRMLS_CC);
return 0;
}
/* set context information */
context->serializer = serializer;
context->copy = copy_type;
context->force_update = force_update;
/* set this to avoid memory errors */
memset(&context->copied, 0, sizeof(HashTable));
return 1;
} /* }}} */
/* {{{ apc_context_destroy */
PHP_APCU_API zend_bool apc_cache_destroy_context(apc_context_t* context TSRMLS_DC) {
if (!context->pool) {
return 0;
}
apc_pool_destroy(context->pool TSRMLS_CC);
return 1;
} /* }}} */
/* {{{ apc_cache_insert */
PHP_APCU_API zend_bool apc_cache_insert(apc_cache_t* cache,
apc_cache_key_t key,
apc_cache_entry_t* value,
apc_context_t* ctxt,
time_t t,
zend_bool exclusive TSRMLS_DC)
{
zend_bool result = 0;
/* at least */
if (!value) {
return result;
}
/* check we are able to deal with this request */
if (!cache || apc_cache_busy(cache TSRMLS_CC)) {
return result;
}
/* lock header */
APC_LOCK(cache->header);
/* process deleted list */
apc_cache_gc(cache TSRMLS_CC);
/* make the insertion */
{
apc_cache_slot_t** slot;
/*
* select appropriate slot ...
*/
slot = &cache->slots[key.h % cache->nslots];
while (*slot) {
/* check for a match by hash and string */
if (((*slot)->key.h == key.h) && (!memcmp((*slot)->key.str, key.str, key.len))) {
/*
* At this point we have found the user cache entry. If we are doing
* an exclusive insert (apc_add) we are going to bail right away if
* the user entry already exists and it has no ttl, or
* there is a ttl and the entry has not timed out yet.
*/
if(exclusive) {
if (!(*slot)->value->ttl || (time_t) ((*slot)->ctime + (*slot)->value->ttl) >= t) {
goto nothing;
}
}
apc_cache_remove_slot(cache, slot TSRMLS_CC);
break;
} else
/*
* This is a bit nasty. The idea here is to do runtime cleanup of the linked list of
* slot entries so we don't always have to skip past a bunch of stale entries. We check
* for staleness here and get rid of them by first checking to see if the cache has a global
* access ttl on it and removing entries that haven't been accessed for ttl seconds and secondly
* we see if the entry has a hard ttl on it and remove it if it has been around longer than its ttl
*/
if((cache->ttl && (time_t)(*slot)->atime < (t - (time_t)cache->ttl)) ||
((*slot)->value->ttl && (time_t) ((*slot)->ctime + (*slot)->value->ttl) < t)) {
apc_cache_remove_slot(cache, slot TSRMLS_CC);
continue;
}
/* set next slot */
slot = &(*slot)->next;
}
if ((*slot = make_slot(cache, &key, value, *slot, t TSRMLS_CC)) != NULL) {
/* set value size from pool size */
value->mem_size = ctxt->pool->size;
cache->header->mem_size += ctxt->pool->size;
cache->header->nentries++;
cache->header->ninserts++;
} else {
goto nothing;
}
}
/* unlock and return succesfull */
APC_UNLOCK(cache->header);
return 1;
/* bail */
nothing:
APC_UNLOCK(cache->header);
return 0;
}
/* }}} */
/* {{{ apc_cache_find */
PHP_APCU_API apc_cache_entry_t* apc_cache_find(apc_cache_t* cache, char *strkey, zend_uint keylen, time_t t TSRMLS_DC)
{
/* check we are able to deal with the request */
if(!cache || apc_cache_busy(cache TSRMLS_CC)) {
return NULL;
}
/* we only declare a volatile we need */
{
apc_cache_slot_t** slot;
zend_ulong h, s;
volatile apc_cache_entry_t* value = NULL;
/* calculate hash and slot */
apc_cache_hash_slot(cache, strkey, keylen, &h, &s);
/* read lock header */
APC_RLOCK(cache->header);
/* find head */
slot = &cache->slots[s];
while (*slot) {
/* check for a matching key by has and identifier */
if ((h == (*slot)->key.h) && !memcmp((*slot)->key.str, strkey, keylen)) {
/* Check to make sure this entry isn't expired by a hard TTL */
if((*slot)->value->ttl && (time_t) ((*slot)->ctime + (*slot)->value->ttl) < t) {
/* increment misses on cache */
cache->header->nmisses++;
/* unlock header */
APC_RUNLOCK(cache->header);
return NULL;
}
/* Otherwise we are fine, increase counters and return the cache entry */
(*slot)->nhits++;
(*slot)->value->ref_count++;
(*slot)->atime = t;
/* set cache num hits */
cache->header->nhits++;
/* grab value */
value = (*slot)->value;
/* unlock header */
APC_RUNLOCK(cache->header);
return (apc_cache_entry_t*)value;
}
/* next */
slot = &(*slot)->next;
}
/* not found, so increment misses */
cache->header->nmisses++;
/* unlock header */
APC_RUNLOCK(cache->header);
}
return NULL;
}
/* }}} */
/* {{{ apc_cache_fetch */
PHP_APCU_API zend_bool apc_cache_fetch(apc_cache_t* cache, char* strkey, zend_uint keylen, time_t t, zval **dst TSRMLS_DC)
{
apc_cache_entry_t *entry;
zend_bool ret = 0;
/* find the entry */
if ((entry = apc_cache_find(cache, strkey, keylen, t TSRMLS_CC))) {
/* context for copying out */
apc_context_t ctxt = {0, };
/* create unpool context */
if (apc_cache_make_context(cache, &ctxt, APC_CONTEXT_NOSHARE, APC_UNPOOL, APC_COPY_OUT, 0 TSRMLS_CC)) {
/* copy to destination */
apc_cache_fetch_zval(&ctxt, *dst, entry->val TSRMLS_CC);
/* release entry */
apc_cache_release(
cache, entry TSRMLS_CC);
/* destroy context */
apc_cache_destroy_context(&ctxt TSRMLS_CC );
/* set result */
ret = 1;
}
}
return ret;
} /* }}} */
/* {{{ apc_cache_exists */
PHP_APCU_API apc_cache_entry_t* apc_cache_exists(apc_cache_t* cache, char *strkey, zend_uint keylen, time_t t TSRMLS_DC)
{
if(apc_cache_busy(cache TSRMLS_CC))
{
/* cache cleanup in progress */
return NULL;
}
/* we only declare volatiles we need */
{
apc_cache_slot_t** slot;
volatile apc_cache_entry_t* value = NULL;
zend_ulong h, s;
/* get hash and slot */
apc_cache_hash_slot(cache, strkey, keylen, &h, &s);
/* read lock header */
APC_RLOCK(cache->header);
/* find head */
slot = &cache->slots[s];
while (*slot) {
/* check for match by hash and identifier */
if ((h == (*slot)->key.h) &&
!memcmp((*slot)->key.str, strkey, keylen)) {
/* Check to make sure this entry isn't expired by a hard TTL */
if((*slot)->value->ttl && (time_t) ((*slot)->ctime + (*slot)->value->ttl) < t) {
/* marked as a miss */
cache->header->nmisses++;
/* unlock header */
APC_RUNLOCK(cache->header);
return NULL;
}
/* Return the cache entry ptr */
value = (*slot)->value;
/* unlock header */
APC_RUNLOCK(cache->header);
return (apc_cache_entry_t*)value;
}
slot = &(*slot)->next;
}
/* unlock header */
APC_RUNLOCK(cache->header);
}
return NULL;
}
/* }}} */
/* {{{ apc_cache_update */
PHP_APCU_API zend_bool apc_cache_update(apc_cache_t* cache, char *strkey, zend_uint keylen, apc_cache_updater_t updater, void* data TSRMLS_DC)
{
apc_cache_slot_t** slot;
zend_bool retval = 0;
zend_ulong h, s;
if(apc_cache_busy(cache TSRMLS_CC))
{
/* cannot service request right now */
return 0;
}