source: trunk/processor/head.m4 @ 1

Last change on this file since 1 was 1, checked in by moo, 8 years ago

initial import to online

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