root/tags/1.2-beta1/xcache.c

Revision 236, 66.7 kB (checked in by moo, 2 years ago)

better debugging info

Line 
1
2#if 0
3#define DEBUG
4#endif
5
6#if 0
7#define SHOW_DPRINT
8#endif
9
10/* {{{ macros */
11#include <stdlib.h>
12#include <stdio.h>
13#include <string.h>
14
15#include <signal.h>
16
17#include "php.h"
18#include "ext/standard/info.h"
19#include "ext/standard/md5.h"
20#include "ext/standard/php_math.h"
21#include "zend_extensions.h"
22#include "SAPI.h"
23
24#include "xcache.h"
25#include "optimizer.h"
26#include "coverager.h"
27#include "disassembler.h"
28#include "align.h"
29#include "stack.h"
30#include "xcache_globals.h"
31#include "processor.h"
32#include "utils.h"
33#include "const_string.h"
34#include "opcode_spec.h"
35
36#ifdef DEBUG
37#   undef NDEBUG
38#   undef inline
39#   define inline
40#else
41#   ifndef NDEBUG
42#       define NDEBUG
43#   endif
44#endif
45#include <assert.h>
46
47#define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > pentry->ctime + (pentry)->ttl)
48#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
49#define LOCK(x) xc_lock(x->lck)
50#define UNLOCK(x) xc_unlock(x->lck)
51
52#define ENTER_LOCK_EX(x) \
53    xc_lock(x->lck); \
54    zend_try { \
55        do
56#define LEAVE_LOCK_EX(x) \
57        while (0); \
58    } zend_catch { \
59        catched = 1; \
60    } zend_end_try(); \
61    xc_unlock(x->lck)
62
63#define ENTER_LOCK(x) do { \
64    int catched = 0; \
65    ENTER_LOCK_EX(x)
66#define LEAVE_LOCK(x) \
67    LEAVE_LOCK_EX(x); \
68    if (catched) { \
69        zend_bailout(); \
70    } \
71} while(0)
72
73/* }}} */
74
75/* {{{ globals */
76static char *xc_shm_scheme = NULL;
77static char *xc_mmap_path = NULL;
78static char *xc_coredump_dir = NULL;
79
80static xc_hash_t xc_php_hcache = {0};
81static xc_hash_t xc_php_hentry = {0};
82static xc_hash_t xc_var_hcache = {0};
83static xc_hash_t xc_var_hentry = {0};
84
85static zend_ulong xc_php_ttl    = 0;
86static zend_ulong xc_var_maxttl = 0;
87
88enum { xc_deletes_gc_interval = 120 };
89static zend_ulong xc_php_gc_interval = 0;
90static zend_ulong xc_var_gc_interval = 0;
91
92/* total size */
93static zend_ulong xc_php_size  = 0;
94static zend_ulong xc_var_size  = 0;
95
96static xc_cache_t **xc_php_caches = NULL;
97static xc_cache_t **xc_var_caches = NULL;
98
99static zend_bool xc_initized = 0;
100static zend_compile_file_t *origin_compile_file;
101
102static zend_bool xc_test = 0;
103static zend_bool xc_readonly_protection = 0;
104
105zend_bool xc_have_op_array_ctor = 0;
106
107static zend_bool xc_module_gotup = 0;
108static zend_bool xc_zend_extension_gotup = 0;
109#if !COMPILE_DL_XCACHE
110#   define zend_extension_entry xcache_zend_extension_entry
111#endif
112ZEND_DLEXPORT zend_extension zend_extension_entry;
113ZEND_DECLARE_MODULE_GLOBALS(xcache);
114/* }}} */
115
116/* any function in *_dmz is only safe be called within locked(single thread) area */
117
118static inline int xc_entry_equal_dmz(xc_entry_t *a, xc_entry_t *b) /* {{{ */
119{
120    /* this function isn't required but can be in dmz */
121
122    if (a->type != b->type) {
123        return 0;
124    }
125    switch (a->type) {
126        case XC_TYPE_PHP:
127#ifdef HAVE_INODE
128            do {
129                xc_entry_data_php_t *ap = a->data.php;
130                xc_entry_data_php_t *bp = b->data.php;
131                if (ap->inode) {
132                    return ap->inode == bp->inode
133                        && ap->device == bp->device;
134                }
135            } while(0);
136#endif
137            /* fall */
138
139        case XC_TYPE_VAR:
140            do {
141#ifdef IS_UNICODE
142                if (a->name_type == IS_UNICODE) {
143                    if (a->name.ustr.len != b->name.ustr.len) {
144                        return 0;
145                    }
146                    return memcmp(a->name.ustr.val, b->name.ustr.val, (a->name.ustr.len + 1) * sizeof(UChar)) == 0;
147                }
148                else {
149                    return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0;
150                }
151#else
152                return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0;
153#endif
154
155            } while(0);
156        default:
157            assert(0);
158    }
159    return 0;
160}
161/* }}} */
162static void xc_entry_free_real_dmz(volatile xc_entry_t *xce) /* {{{ */
163{
164    xce->cache->mem->handlers->free(xce->cache->mem, (xc_entry_t *)xce);
165}
166/* }}} */
167static void xc_entry_add_dmz(xc_entry_t *xce) /* {{{ */
168{
169    xc_entry_t **head = &(xce->cache->entries[xce->hvalue]);
170    xce->next = *head;
171    *head = xce;
172    xce->cache->entries_count ++;
173}
174/* }}} */
175static xc_entry_t *xc_entry_store_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
176{
177    xc_entry_t *stored_xce;
178
179    xce->hits  = 0;
180    xce->ctime = XG(request_time);
181    xce->atime = XG(request_time);
182    stored_xce = xc_processor_store_xc_entry_t(xce TSRMLS_CC);
183    if (stored_xce) {
184        xc_entry_add_dmz(stored_xce);
185        return stored_xce;
186    }
187    else {
188        xce->cache->ooms ++;
189        return NULL;
190    }
191}
192/* }}} */
193static void xc_entry_free_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
194{
195    xce->cache->entries_count --;
196    if (xce->refcount == 0) {
197        xc_entry_free_real_dmz(xce);
198    }
199    else {
200        xce->next = xce->cache->deletes;
201        xce->cache->deletes = xce;
202        xce->dtime = XG(request_time);
203        xce->cache->deletes_count ++;
204    }
205    return;
206}
207/* }}} */
208static void xc_entry_remove_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
209{
210    xc_entry_t **pp = &(xce->cache->entries[xce->hvalue]);
211    xc_entry_t *p;
212    for (p = *pp; p; pp = &(p->next), p = p->next) {
213        if (xc_entry_equal_dmz(xce, p)) {
214            /* unlink */
215            *pp = p->next;
216            xc_entry_free_dmz(xce TSRMLS_CC);
217            return;
218        }
219    }
220    assert(0);
221}
222/* }}} */
223static xc_entry_t *xc_entry_find_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
224{
225    xc_entry_t *p;
226    for (p = xce->cache->entries[xce->hvalue]; p; p = p->next) {
227        if (xc_entry_equal_dmz(xce, p)) {
228            if (p->type == XC_TYPE_VAR || /* PHP */ p->data.php->mtime == xce->data.php->mtime) {
229                p->hits ++;
230                p->atime = XG(request_time);
231                return p;
232            }
233            else {
234                xc_entry_remove_dmz(p TSRMLS_CC);
235                return NULL;
236            }
237        }
238    }
239    return NULL;
240}
241/* }}} */
242static void xc_entry_hold_php_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
243{
244#ifdef DEBUG
245    fprintf(stderr, "hold %s\n", ZSTR_S(xce->name));
246#endif
247    xce->refcount ++;
248    xc_stack_push(&XG(php_holds)[xce->cache->cacheid], (void *)xce);
249}
250/* }}} */
251#if 0
252static void xc_entry_hold_var_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */
253{
254    xce->refcount ++;
255    xc_stack_push(&XG(var_holds)[xce->cache->cacheid], (void *)xce);
256}
257/* }}} */
258#endif
259
260/* helper function that loop through each entry */
261#define XC_ENTRY_APPLY_FUNC(name) int name(xc_entry_t *entry TSRMLS_DC)
262typedef XC_ENTRY_APPLY_FUNC((*cache_apply_dmz_func_t));
263static void xc_entry_apply_dmz(xc_cache_t *cache, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */
264{
265    xc_entry_t *p, **pp;
266    int i, c;
267
268    for (i = 0, c = cache->hentry->size; i < c; i ++) {
269        pp = &(cache->entries[i]);
270        for (p = *pp; p; p = *pp) {
271            if (apply_func(p TSRMLS_CC)) {
272                /* unlink */
273                *pp = p->next;
274                xc_entry_free_dmz(p TSRMLS_CC);
275            }
276            else {
277                pp = &(p->next);
278            }
279        }
280    }
281}
282/* }}} */
283
284#define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC)
285/* call graph:
286 * xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_php_entry_dmz
287 * xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_var_entry_dmz
288 */
289static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_dmz) /* {{{ */
290{
291#ifdef DEBUG
292    fprintf(stderr, "ttl %d, %d %d\n", XG(request_time), entry->atime, xc_php_ttl);
293#endif
294    if (XG(request_time) > entry->atime + xc_php_ttl) {
295        return 1;
296    }
297    return 0;
298}
299/* }}} */
300static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_dmz) /* {{{ */
301{
302    if (VAR_ENTRY_EXPIRED(entry)) {
303        return 1;
304    }
305    return 0;
306}
307/* }}} */
308static void xc_gc_expires_one(xc_cache_t *cache, zend_ulong gc_interval, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */
309{
310#ifdef DEBUG
311    fprintf(stderr, "interval %d, %d %d\n", XG(request_time), cache->last_gc_expires, gc_interval);
312#endif
313    if (XG(request_time) - cache->last_gc_expires >= gc_interval) {
314        ENTER_LOCK(cache) {
315            if (XG(request_time) - cache->last_gc_expires >= gc_interval) {
316                cache->last_gc_expires = XG(request_time);
317                xc_entry_apply_dmz(cache, apply_func TSRMLS_CC);
318            }
319        } LEAVE_LOCK(cache);
320    }
321}
322/* }}} */
323static void xc_gc_expires_php(TSRMLS_D) /* {{{ */
324{
325    int i, c;
326
327    if (!xc_php_ttl || !xc_php_gc_interval) {
328        return;
329    }
330
331    for (i = 0, c = xc_php_hcache.size; i < c; i ++) {
332        xc_gc_expires_one(xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_dmz TSRMLS_CC);
333    }
334}
335/* }}} */
336static void xc_gc_expires_var(TSRMLS_D) /* {{{ */
337{
338    int i, c;
339
340    if (!xc_var_gc_interval) {
341        return;
342    }
343
344    for (i = 0, c = xc_var_hcache.size; i < c; i ++) {
345        xc_gc_expires_one(xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_dmz TSRMLS_CC);
346    }
347}
348/* }}} */
349
350static XC_CACHE_APPLY_FUNC(xc_gc_delete_dmz) /* {{{ */
351{
352    xc_entry_t *p, **pp;
353
354    pp = &cache->deletes;
355    for (p = *pp; p; p = *pp) {
356        if (XG(request_time) - p->dtime > 3600) {
357            p->refcount = 0;
358            /* issue warning here */
359        }
360        if (p->refcount == 0) {
361            /* unlink */
362            *pp = p->next;
363            cache->deletes_count --;
364            xc_entry_free_real_dmz(p);
365        }
366        else {
367            pp = &(p->next);
368        }
369    }
370}
371/* }}} */
372static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */
373{
374    if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) {
375        ENTER_LOCK(cache) {
376            if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) {
377                cache->last_gc_deletes = XG(request_time);
378                xc_gc_delete_dmz(cache TSRMLS_CC);
379            }
380        } LEAVE_LOCK(cache);
381    }
382}
383/* }}} */
384static void xc_gc_deletes(TSRMLS_D) /* {{{ */
385{
386    int i, c;
387
388    for (i = 0, c = xc_php_hcache.size; i < c; i ++) {
389        xc_gc_deletes_one(xc_php_caches[i] TSRMLS_CC);
390    }
391
392    for (i = 0, c = xc_var_hcache.size; i < c; i ++) {
393        xc_gc_deletes_one(xc_var_caches[i] TSRMLS_CC);
394    }
395}
396/* }}} */
397
398/* helper functions for user functions */
399static void xc_fillinfo_dmz(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */
400{
401    zval *blocks;
402    const xc_block_t *b;
403#ifndef NDEBUG
404    xc_memsize_t avail = 0;
405#endif
406    xc_mem_t *mem = cache->mem;
407    const xc_mem_handlers_t *handlers = mem->handlers;
408    zend_ulong interval = (cachetype == XC_TYPE_PHP) ? xc_php_gc_interval : xc_var_gc_interval;
409
410    add_assoc_long_ex(return_value, ZEND_STRS("slots"),     cache->hentry->size);
411    add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cache->compiling);
412    add_assoc_long_ex(return_value, ZEND_STRS("misses"),    cache->misses);
413    add_assoc_long_ex(return_value, ZEND_STRS("hits"),      cache->hits);
414    add_assoc_long_ex(return_value, ZEND_STRS("clogs"),     cache->clogs);
415    add_assoc_long_ex(return_value, ZEND_STRS("ooms"),      cache->ooms);
416
417    add_assoc_long_ex(return_value, ZEND_STRS("cached"),    cache->entries_count);
418    add_assoc_long_ex(return_value, ZEND_STRS("deleted"),   cache->deletes_count);
419    if (interval) {
420        add_assoc_long_ex(return_value, ZEND_STRS("gc"),    (cache->last_gc_expires + interval) - XG(request_time));
421    }
422    else {
423        add_assoc_null_ex(return_value, ZEND_STRS("gc"));
424    }
425
426    MAKE_STD_ZVAL(blocks);
427    array_init(blocks);
428
429    add_assoc_long_ex(return_value, ZEND_STRS("size"),  handlers->size(mem));
430    add_assoc_long_ex(return_value, ZEND_STRS("avail"), handlers->avail(mem));
431    add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection);
432
433    for (b = handlers->freeblock_first(mem); b; b = handlers->freeblock_next(b)) {
434        zval *bi;
435
436        MAKE_STD_ZVAL(bi);
437        array_init(bi);
438
439        add_assoc_long_ex(bi, ZEND_STRS("size"),   handlers->block_size(b));
440        add_assoc_long_ex(bi, ZEND_STRS("offset"), handlers->block_offset(mem, b));
441        add_next_index_zval(blocks, bi);
442#ifndef NDEBUG
443        avail += handlers->block_size(b);
444#endif
445    }
446    add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks);
447    assert(avail == handlers->avail(mem));
448}
449/* }}} */
450static void xc_fillentry_dmz(xc_entry_t *entry, int del, zval *list TSRMLS_DC) /* {{{ */
451{
452    zval* ei;
453    xc_entry_data_php_t *php;
454    xc_entry_data_var_t *var;
455
456    ALLOC_INIT_ZVAL(ei);
457    array_init(ei);
458
459    add_assoc_long_ex(ei, ZEND_STRS("size"),     entry->size);
460    add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry->refcount);
461    add_assoc_long_ex(ei, ZEND_STRS("hits"),     entry->hits);
462    add_assoc_long_ex(ei, ZEND_STRS("ctime"),    entry->ctime);
463    add_assoc_long_ex(ei, ZEND_STRS("atime"),    entry->atime);
464    if (del) {
465        add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime);
466    }
467#ifdef IS_UNICODE
468    do {
469        zval *zv;
470        ALLOC_INIT_ZVAL(zv);
471        switch (entry->name_type) {
472            case IS_UNICODE:
473                    ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1);
474                break;
475            case IS_STRING:
476                ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1);
477                break;
478            default:
479                assert(0);
480        }
481        zv->type = entry->name_type;
482        add_assoc_zval_ex(ei, ZEND_STRS("name"), zv);
483    } while (0);
484#else
485    add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len, 1);
486#endif
487    switch (entry->type) {
488        case XC_TYPE_PHP:
489            php = entry->data.php;
490            add_assoc_long_ex(ei, ZEND_STRS("sourcesize"),    php->sourcesize);
491#ifdef HAVE_INODE
492            add_assoc_long_ex(ei, ZEND_STRS("device"),        php->device);
493            add_assoc_long_ex(ei, ZEND_STRS("inode"),         php->inode);
494#endif
495            add_assoc_long_ex(ei, ZEND_STRS("mtime"),         php->mtime);
496
497#ifdef HAVE_XCACHE_CONSTANT
498            add_assoc_long_ex(ei, ZEND_STRS("constinfo_cnt"), php->constinfo_cnt);
499#endif
500            add_assoc_long_ex(ei, ZEND_STRS("function_cnt"),  php->funcinfo_cnt);
501            add_assoc_long_ex(ei, ZEND_STRS("class_cnt"),     php->classinfo_cnt);
502            break;
503        case XC_TYPE_VAR:
504            var = entry->data.var;
505            break;
506
507        default:
508            assert(0);
509    }
510
511    add_next_index_zval(list, ei);
512}
513/* }}} */
514static void xc_filllist_dmz(xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */
515{
516    zval* list;
517    int i, c;
518    xc_entry_t *e;
519
520    ALLOC_INIT_ZVAL(list);
521    array_init(list);
522
523    for (i = 0, c = cache->hentry->size; i < c; i ++) {
524        for (e = cache->entries[i]; e; e = e->next) {
525            xc_fillentry_dmz(e, 0, list TSRMLS_CC);
526        }
527    }
528    add_assoc_zval(return_value, "cache_list", list);
529
530    ALLOC_INIT_ZVAL(list);
531    array_init(list);
532    for (e = cache->deletes; e; e = e->next) {
533        xc_fillentry_dmz(e, 1, list TSRMLS_CC);
534    }
535    add_assoc_zval(return_value, "deleted_list", list);
536}
537/* }}} */
538
539static zend_op_array *xc_entry_install(xc_entry_t *xce, zend_file_handle *h TSRMLS_DC) /* {{{ */
540{
541    zend_uint i;
542    xc_entry_data_php_t *p = xce->data.php;
543#ifndef ZEND_ENGINE_2
544    /* new ptr which is stored inside CG(class_table) */
545    xc_cest_t **new_cest_ptrs = (xc_cest_t **)do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt);
546#endif
547
548    CG(active_op_array) = p->op_array;
549
550#ifdef HAVE_XCACHE_CONSTANT
551    /* install constant */
552    for (i = 0; i < p->constinfo_cnt; i ++) {
553        xc_constinfo_t *ci = &p->constinfos[i];
554        xc_install_constant(xce->name.str.val, &ci->constant,
555                UNISW(0, ci->type), ci->key, ci->key_size TSRMLS_CC);
556    }
557#endif
558
559    /* install function */
560    for (i = 0; i < p->funcinfo_cnt; i ++) {
561        xc_funcinfo_t  *fi = &p->funcinfos[i];
562        xc_install_function(xce->name.str.val, &fi->func,
563                UNISW(0, fi->type), fi->key, fi->key_size TSRMLS_CC);
564    }
565
566    /* install class */
567    for (i = 0; i < p->classinfo_cnt; i ++) {
568        xc_classinfo_t *ci = &p->classinfos[i];
569#ifndef ZEND_ENGINE_2
570        zend_class_entry *ce = CestToCePtr(ci->cest);
571        /* fix pointer to the be which inside class_table */
572        if (ce->parent) {
573            zend_uint class_idx = (/* class_num */ (int) (long) ce->parent) - 1;
574            assert(class_idx < i);
575            ci->cest.parent = new_cest_ptrs[class_idx];
576        }
577        new_cest_ptrs[i] =
578#endif
579        xc_install_class(xce->name.str.val, &ci->cest, ci->oplineno,
580                UNISW(0, ci->type), ci->key, ci->key_size TSRMLS_CC);
581    }
582
583    i = 1;
584    zend_hash_add(&EG(included_files), xce->name.str.val, xce->name.str.len+1, (void *)&i, sizeof(int), NULL);
585    if (h) {
586        zend_llist_add_element(&CG(open_files), h);
587    }
588
589#ifndef ZEND_ENGINE_2
590    free_alloca(new_cest_ptrs);
591#endif
592    return p->op_array;
593}
594/* }}} */
595
596static inline void xc_entry_unholds_real(xc_stack_t *holds, xc_cache_t **caches, int cachecount TSRMLS_DC) /* {{{ */
597{
598    int i;
599    xc_stack_t *s;
600    xc_cache_t *cache;
601    xc_entry_t *xce;
602
603    for (i = 0; i < cachecount; i ++) {
604        s = &holds[i];
605#ifdef DEBUG
606        fprintf(stderr, "holded %d\n", xc_stack_size(s));
607#endif
608        if (xc_stack_size(s)) {
609            cache = ((xc_entry_t *)xc_stack_top(s))->cache;
610            ENTER_LOCK(cache) {
611                while (xc_stack_size(s)) {
612                    xce = (xc_entry_t*) xc_stack_pop(s);
613#ifdef DEBUG
614                    fprintf(stderr, "unhold %s\n", ZSTR_S(xce->name));
615#endif
616                    xce->refcount --;
617                    assert(xce->refcount >= 0);
618                }
619            } LEAVE_LOCK(cache);
620        }
621    }
622}
623/* }}} */
624static void xc_entry_unholds(TSRMLS_D) /* {{{ */
625{
626    xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC);
627    xc_entry_unholds_real(XG(var_holds), xc_var_caches, xc_var_hcache.size TSRMLS_CC);
628}
629/* }}} */
630static int xc_stat(const char *filename, const char *include_path, struct stat *pbuf TSRMLS_DC) /* {{{ */
631{
632    char filepath[MAXPATHLEN];
633    char *paths, *path;
634    char *tokbuf;
635    int size = strlen(include_path) + 1;
636    char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' };
637
638    paths = (char *)do_alloca(size);
639    memcpy(paths, include_path, size);
640
641    for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) {
642        if (snprintf(filepath, sizeof(filepath), "%s/%s", path, filename) >= MAXPATHLEN - 1) {
643            continue;
644        }
645        if (VCWD_STAT(filepath, pbuf) == 0) {
646            free_alloca(paths);
647            return 0;
648        }
649    }
650
651    free_alloca(paths);
652
653    return 1;
654}
655/* }}} */
656
657#define HASH(i) (i)
658#define HASH_USTR_L(t, s, l) HASH(zend_u_inline_hash_func(t, s, (l + 1) * sizeof(UChar)))
659#define HASH_STR_L(s, l) HASH(zend_inline_hash_func(s, l + 1))
660#define HASH_STR(s) HASH_STR_L(s, strlen(s) + 1)
661#define HASH_NUM(n) HASH(n)
662static inline xc_hash_value_t xc_entry_hash_name(xc_entry_t *xce TSRMLS_DC) /* {{{ */
663{
664    return UNISW(NOTHING, UG(unicode) ? HASH_USTR_L(xce->name_type, xce->name.uni.val, xce->name.uni.len) :)
665        HASH_STR_L(xce->name.str.val, xce->name.str.len);
666}
667/* }}} */
668#define xc_entry_hash_var xc_entry_hash_name
669static inline xc_hash_value_t xc_entry_hash_php(xc_entry_t *xce TSRMLS_DC) /* {{{ */
670{
671#ifdef HAVE_INODE
672    if (xce->data.php->inode) {
673        return HASH(xce->data.php->device + xce->data.php->inode);
674    }
675#endif
676    return xc_entry_hash_name(xce TSRMLS_CC);
677}
678/* }}} */
679static int xc_entry_init_key_php(xc_entry_t *xce, char *filename, char *opened_path_buffer TSRMLS_DC) /* {{{ */
680{
681    struct stat buf, *pbuf;
682    xc_hash_value_t hv;
683    int cacheid;
684    xc_entry_data_php_t *php;
685    char *ptr;
686
687    if (!filename || !SG(request_info).path_translated) {
688        return 0;
689    }
690
691    php = xce->data.php;
692
693    if (XG(stat)) {
694        if (strcmp(SG(request_info).path_translated, filename) == 0) {
695            /* sapi has already done this stat() for us */
696            pbuf = sapi_get_stat(TSRMLS_C);
697            if (pbuf) {
698                goto stat_done;
699            }
700        }
701
702        /* absolute path */
703        pbuf = &buf;
704        if (IS_ABSOLUTE_PATH(filename, strlen(filename))) {
705            if (VCWD_STAT(filename, pbuf) != 0) {
706                return 0;
707            }
708            goto stat_done;
709        }
710
711        /* relative path */
712        if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) {
713            ptr = filename + 1;
714            if (*ptr == '.') {
715                while (*(++ptr) == '.');
716                if (!IS_SLASH(*ptr)) {
717                    goto not_relative_path;
718                }   
719            }
720
721            if (VCWD_STAT(filename, pbuf) != 0) {
722                return 0;
723            }
724            goto stat_done;
725        }
726not_relative_path:
727
728        /* use include_path */
729        if (xc_stat(filename, PG(include_path), pbuf TSRMLS_CC) != 0) {   
730            return 0;
731        }
732
733        /* fall */
734
735stat_done:
736        if (XG(request_time) - pbuf->st_mtime < 2 && !xc_test) {
737          Â