source: trunk/processor/head.m4 @ 56

Last change on this file since 56 was 56, checked in by moo, 9 years ago

brings 5.0.x support back

File size: 8.7 KB
Line 
1dnl {{{ === program start ========================================
2divert(0)
3#include <string.h>
4#include <stdio.h>
5
6#include "php.h"
7#include "zend_compile.h"
8#include "zend_API.h"
9#include "zend_ini.h"
10
11#include "xcache.h"
12#include "align.h"
13#include "const_string.h"
14#include "processor.h"
15#include "stack.h"
16#include "xcache_globals.h"
17
18define(`SIZEOF_zend_uint', `sizeof(zend_uint)')
19define(`COUNTOF_zend_uint', `1')
20define(`SIZEOF_int', `sizeof(int)')
21define(`COUNTOF_int', `1')
22define(`SIZEOF_zend_function', `sizeof(zend_function)')
23define(`COUNTOF_zend_function', `1')
24define(`SIZEOF_zval_ptr', `sizeof(zval_ptr)')
25define(`COUNTOF_zval_ptr', `1')
26define(`SIZEOF_xc_entry_name_t', `sizeof(xc_entry_name_t)')
27define(`COUNTOF_xc_entry_name_t', `1')
28
29ifdef(`XCACHE_ENABLE_TEST', `
30#undef NDEBUG
31#include <assert.h>
32m4_errprint(`AUTOCHECK INFO: runtime autocheck Enabled (debug build)')
33', `
34m4_errprint(`AUTOCHECK INFO: runtime autocheck Disabled (optimized build)')
35')
36sinclude(builddir`/structinfo.m4')
37
38#ifndef NDEBUG
39#   undef inline
40#define inline
41#endif
42
43typedef zval *zval_ptr;
44typedef zend_uchar zval_data_type;
45
46#define MAX_DUP_STR_LEN 256
47dnl }}}
48/* export: typedef struct _processor_t processor_t; :export {{{ */
49struct _processor_t {
50    char *p;
51    zend_uint size;
52    HashTable strings;
53    HashTable zvalptrs;
54    zend_bool reference; /* enable if to deal with reference */
55    const xc_entry_t *xce_src;
56    const xc_entry_t *xce_dst;
57    const zend_class_entry *cache_ce;
58    zend_uint cache_class_num;
59
60    const zend_op          *active_opcodes_src;
61    zend_op                *active_opcodes_dst;
62    const zend_class_entry *active_class_entry_src;
63    zend_class_entry       *active_class_entry_dst;
64    zend_uint               active_class_num;
65
66    zend_bool readonly_protection; /* wheather it's present */
67IFASSERT(xc_stack_t allocsizes;)
68};
69/* }}} */
70#ifdef XCACHE_HAVE_DPRINT
71static void xc_dprint_indent(int indent) /* {{{ */
72{
73    int i;
74    for (i = 0; i < indent; i ++) {
75        fprintf(stderr, "  ");
76    }
77}
78#endif
79/* }}} */
80/* {{{ xc_calc_string_n */
81REDEF(`KIND', `calc')
82static inline void xc_calc_string_n(processor_t *processor, zend_uchar type, char *str, long size IFASSERT(`, int relayline')) {
83    pushdef(`__LINE__', `relayline')
84    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
85
86    if (realsize > MAX_DUP_STR_LEN) {
87        ALLOC(, char, realsize)
88    }
89    else if (zend_u_hash_add(&processor->strings, type, str, size, (void*)&str, sizeof(char*), NULL) == SUCCESS) {
90        /* new string */
91        ALLOC(, char, realsize)
92    }
93    IFASSERT(`
94        else {
95            dnl fprintf(stderr, "dupstr %s\n", str);
96        }
97    ')
98    popdef(`__LINE__')
99}
100/* }}} */
101/* {{{ xc_store_string_n */
102REDEF(`KIND', `store')
103static inline char *xc_store_string_n(processor_t *processor, zend_uchar type, char *str, long size IFASSERT(`, int relayline')) {
104    pushdef(`__LINE__', `relayline')
105    int realsize = UNISW(size, (type == IS_UNICODE) ? UBYTES(size) : size);
106    char *s;
107
108    if (realsize > MAX_DUP_STR_LEN) {
109        ALLOC(s, char, realsize)
110        memcpy(s, str, realsize);
111    }
112    else if (zend_u_hash_find(&processor->strings, type, str, size, (void*)&s) != SUCCESS) {
113        /* new string */
114        ALLOC(s, char, realsize)
115        memcpy(s, str, realsize);
116        zend_u_hash_add(&processor->strings, type, str, size, (void*)&s, sizeof(char*), NULL);
117    }
118    else {
119        s = *(char**)s;
120    }
121    return s;
122    popdef(`__LINE__')
123}
124/* }}} */
125/* {{{ xc_get_class_num
126 * return class_index + 1
127 */
128static zend_uint xc_get_class_num(processor_t *processor, zend_class_entry *ce) {
129    zend_uint i;
130    const xc_entry_t *xce = processor->xce_src;
131    zend_class_entry *ceptr;
132
133    if (processor->cache_ce == ce) {
134        return processor->cache_class_num;
135    }
136    for (i = 0; i < xce->data.php->classinfo_cnt; i ++) {
137        ceptr = CestToCePtr(xce->data.php->classinfos[i].cest);
138        if (ZCEP_REFCOUNT_PTR(ceptr) == ZCEP_REFCOUNT_PTR(ce)) {
139            processor->cache_ce = ceptr;
140            processor->cache_class_num = i + 1;
141            return i + 1;
142        }
143    }
144    assert(0);
145    return (zend_uint) -1;
146}
147/* }}} */
148/* {{{ xc_get_class */
149#ifdef ZEND_ENGINE_2
150static zend_class_entry *xc_get_class(processor_t *processor, zend_uint class_num) {
151    /* must be parent or currrent class */
152    assert(class_num <= processor->active_class_num);
153    return CestToCePtr(processor->xce_dst->data.php->classinfos[class_num - 1].cest);
154}
155#endif
156/* }}} */
157#ifdef ZEND_ENGINE_2
158/* fix method on store */
159static void xc_fix_method(processor_t *processor, zend_op_array *dst) /* {{{ */
160{
161    zend_function *zf = (zend_function *) dst;
162    zend_class_entry *ce = processor->active_class_entry_dst;
163
164    /* Fixing up the default functions for objects here since
165     * we need to compare with the newly allocated functions
166     *
167     * caveat: a sub-class method can have the same name as the
168     * parent~s constructor and create problems.
169     */
170
171    if (zf->common.fn_flags & ZEND_ACC_CTOR) {
172        if (!ce->constructor) {
173            ce->constructor = zf;
174        }
175    }
176    else if (zf->common.fn_flags & ZEND_ACC_DTOR) {
177        ce->destructor = zf;
178    }
179    else if (zf->common.fn_flags & ZEND_ACC_CLONE) {
180        ce->clone = zf;
181    }
182    else {
183#define SET_IF_SAME_NAME(member) \
184        do { \
185            if(!strcasecmp(zf->common.function_name, #member)) { \
186                ce->member = zf; \
187            } \
188        } \
189        while(0)
190        /* if(ce->member && !strcmp(zf->common.function_name, ce->member->common.function_name)) { \ */
191
192        SET_IF_SAME_NAME(__get);
193        SET_IF_SAME_NAME(__set);
194#ifdef ZEND_ENGINE_2_1
195        SET_IF_SAME_NAME(__unset);
196        SET_IF_SAME_NAME(__isset);
197#endif
198        SET_IF_SAME_NAME(__call);
199#if PHP_MAJOR_VERSION >= 6
200        SET_IF_SAME_NAME(__tostring);
201#endif
202
203#undef SET_IF_SAME_NAME
204    }
205}
206/* }}} */
207#endif
208dnl ================ export API
209/* export: xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC); :export {{{ */
210xc_entry_t *xc_processor_store_xc_entry_t(xc_entry_t *src TSRMLS_DC) {
211    xc_entry_t *dst;
212    processor_t processor;
213
214    memset(&processor, 0, sizeof(processor));
215    if (src->type == XC_TYPE_VAR) {
216        processor.reference = 1;
217    }
218
219    IFASSERT(`xc_stack_init(&processor.allocsizes);')
220
221    /* calc size */ {
222        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
223        if (processor.reference) {
224            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
225        }
226
227        processor.size = 0;
228        /* allocate */
229        processor.size = ALIGN(processor.size + sizeof(src[0]));
230
231        xc_calc_xc_entry_t(&processor, src TSRMLS_CC);
232        if (processor.reference) {
233            zend_hash_destroy(&processor.zvalptrs);
234        }
235        zend_hash_destroy(&processor.strings);
236    }
237    src->size = processor.size;
238
239    IFASSERT(`xc_stack_reverse(&processor.allocsizes);')
240    /* store {{{ */
241    {
242        IFASSERT(`char *oldp;')
243        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
244        if (processor.reference) {
245            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
246        }
247
248        /* mem :) */
249        processor.p = (char *)xc_mem_malloc(src->cache->mem, processor.size);
250        if (processor.p == NULL) {
251            dst = NULL;
252            goto err_alloc;
253            return NULL;
254        }
255        IFASSERT(`oldp = processor.p;')
256        assert(processor.p == (char *) ALIGN(processor.p));
257
258        /* allocate */
259        dst = (xc_entry_t *) processor.p;
260        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
261
262        xc_store_xc_entry_t(&processor, dst, src TSRMLS_CC);
263        IFASSERT(` {
264            int real = processor.p - oldp;
265            int should = processor.size;
266            if (real != processor.size) {
267                fprintf(stderr, "real %d - should %d = %d\n", real, should, real - should);
268                abort();
269            }
270        }')
271err_alloc:
272        if (processor.reference) {
273            zend_hash_destroy(&processor.zvalptrs);
274        }
275        zend_hash_destroy(&processor.strings);
276    }
277    /* }}} */
278
279    IFASSERT(`xc_stack_destroy(&processor.allocsizes);')
280
281    return dst;
282}
283/* }}} */
284/* export: xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC); :export {{{ */
285xc_entry_t *xc_processor_restore_xc_entry_t(xc_entry_t *dst, const xc_entry_t *src, zend_bool readonly_protection TSRMLS_DC) {
286    processor_t processor;
287
288    memset(&processor, 0, sizeof(processor));
289    processor.readonly_protection = readonly_protection;
290
291    xc_restore_xc_entry_t(&processor, dst, src TSRMLS_CC);
292    return dst;
293}
294/* }}} */
295/* export: zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC); :export {{{ */
296zval *xc_processor_restore_zval(zval *dst, const zval *src TSRMLS_DC) {
297    processor_t processor;
298
299    memset(&processor, 0, sizeof(processor));
300    processor.reference = 1;
301
302    zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
303    dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
304    zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
305    xc_restore_zval(&processor, dst, src TSRMLS_CC);
306    zend_hash_destroy(&processor.zvalptrs);
307
308    return dst;
309}
310/* }}} */
311/* export: void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC); :export {{{ */
312#ifdef XCACHE_HAVE_DPRINT
313void xc_dprint(xc_entry_t *src, int indent TSRMLS_DC) {
314    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_t:src");')
315    xc_dprint_xc_entry_t(src, indent TSRMLS_CC);
316}
317#endif
318/* }}} */
Note: See TracBrowser for help on using the repository browser.