source: trunk/processor/foot.m4

Last change on this file was 1578, checked in by moo, 5 weeks ago

cacher: fix more var caching leaks

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1dnl {{{ xc_store_target
2EXPORT(`typedef void *(*xc_processor_storage_allocator_t)(void *data, size_t size);')
3EXPORT(`typedef struct { xc_processor_storage_allocator_t allocator; void *allocator_data; ptrdiff_t relocatediff; } xc_processor_storage_t;')
4dnl }}}
5define(`DEFINE_STORE_API', `
6EXPORTED_FUNCTION(`$1 *xc_processor_store_$1(const xc_processor_storage_t *storage, $1 *src TSRMLS_DC)') dnl {{{
7{
8    $1 *dst;
9    xc_processor_t processor;
10
11    memset(&processor, 0, sizeof(processor));
12    processor.handle_reference = 1;
13    processor.relocatediff = storage->relocatediff;
14
15    IFAUTOCHECK(`xc_vector_init(unsigned long, &processor.allocsizes);')
16
17    /* calc size */ {
18        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
19        if (processor.handle_reference) {
20            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
21        }
22
23        processor.size = 0;
24        /* allocate */
25        processor.size = ALIGN(processor.size + sizeof(src[0]));
26
27        xc_calc_$1(&processor, src TSRMLS_CC);
28        if (processor.handle_reference) {
29            zend_hash_destroy(&processor.zvalptrs);
30        }
31        zend_hash_destroy(&processor.strings);
32    }
33    ifelse(
34        `$1', `xc_entry_data_php_t', `SRC(`size')',
35        `', `', SRC(`entry.size')) = processor.size;
36    ifelse(
37        `$1', `xc_entry_var_t', `SRC(`have_references') = processor.have_references;',
38        `$1', `xc_entry_data_php_t', `SRC(`have_references') = processor.have_references;'
39    )
40
41    IFAUTOCHECK(`xc_vector_reverse(&processor.allocsizes);')
42    /* store {{{ */
43    {
44        IFAUTOCHECK(`char *oldp;')
45        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
46        if (processor.handle_reference) {
47            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
48        }
49
50        /* allocator :) */
51        processor.p = (char *) storage->allocator(storage->allocator_data, processor.size);
52        if (processor.p == NULL) {
53            dst = NULL;
54            goto err_alloc;
55        }
56        IFAUTOCHECK(`oldp = processor.p;')
57        assert(processor.p == (char *) ALIGN(processor.p));
58
59        /* allocate */
60        dst = ($1 *) processor.p;
61        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
62
63        xc_store_$1(&processor, dst, src TSRMLS_CC);
64        IFAUTOCHECK(` {
65            size_t unexpected = processor.p - oldp;
66            size_t expected = processor.size;
67            if (unexpected != processor.size) {
68                fprintf(stderr, "unexpected:%lu - expected:%lu = %ld != 0\n", (unsigned long) unexpected, (unsigned long) expected, (long) unexpected - expected);
69                abort();
70            }
71        }')
72err_alloc:
73        if (processor.handle_reference) {
74            zend_hash_destroy(&processor.zvalptrs);
75        }
76        zend_hash_destroy(&processor.strings);
77    }
78    /* }}} */
79
80    IFAUTOCHECK(`xc_vector_destroy(&processor.allocsizes);')
81
82    return dst;
83}
84dnl }}}
85')
86DEFINE_STORE_API(`xc_entry_php_t')
87DEFINE_STORE_API(`xc_entry_data_php_t')
88DEFINE_STORE_API(`xc_entry_var_t')
89EXPORTED_FUNCTION(`void xc_processor_restore_xc_entry_php_t(xc_entry_php_t *dst, const xc_entry_php_t *src TSRMLS_DC)') dnl {{{
90{
91    xc_processor_t processor;
92
93    memset(&processor, 0, sizeof(processor));
94    xc_restore_xc_entry_php_t(&processor, dst, src TSRMLS_CC);
95}
96dnl }}}
97EXPORTED_FUNCTION(`void xc_processor_restore_xc_entry_data_php_t(const xc_entry_php_t *entry_php, xc_entry_data_php_t *dst, const xc_entry_data_php_t *src, zend_bool readonly_protection TSRMLS_DC)') dnl {{{
98{
99    xc_processor_t processor;
100
101    memset(&processor, 0, sizeof(processor));
102    processor.readonly_protection = readonly_protection;
103    /* this function is used for php data only */
104    if (SRC(`have_references')) {
105        processor.handle_reference = 1;
106    }
107    processor.entry_php_src = entry_php;
108
109    if (processor.handle_reference) {
110        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
111    }
112    xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
113    if (processor.handle_reference) {
114        zend_hash_destroy(&processor.zvalptrs);
115    }
116}
117dnl }}}
118EXPORTED_FUNCTION(`void xc_processor_restore_var(zval *dst, zval **dst_ptr, const xc_entry_var_t *src, zend_class_entry **index_to_ce TSRMLS_DC)') dnl {{{
119{
120    xc_processor_t processor;
121
122    memset(&processor, 0, sizeof(processor));
123    processor.handle_reference = src->have_references;
124
125    if (processor.handle_reference) {
126        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
127    }
128    processor.index_to_ce = index_to_ce;
129
130#ifdef ZEND_ENGINE_2
131    if (src->objects_count) {
132        int i;
133        size_t size = sizeof(processor.object_handles[0]) * src->objects_count;
134        processor.object_handles = emalloc(size);
135        IFAUTOCHECK(`xc_memsetptr(processor.object_handles, (void *) -1, size);')
136        /* other objects in $object->properties after $object, reverse order is necessary */
137        for (i = src->objects_count - 1; i >= 0; --i) {
138            zend_object *object = emalloc(sizeof(*object));
139            processor.object_handles[i] = zend_objects_store_put(object, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
140            xc_restore_zend_object(&processor, object, &src->objects[i] TSRMLS_CC);
141        }
142    }
143#endif
144    if (dst_ptr) {
145        zval_ptr_dtor(dst_ptr);
146        xc_restore_zval_ptr(&processor, dst_ptr, &src->value TSRMLS_CC);
147    }
148    else {
149        zval_dtor(dst);
150        xc_restore_zval(&processor, dst, src->value TSRMLS_CC);
151    }
152    if (processor.handle_reference) {
153        zend_hash_destroy(&processor.zvalptrs);
154    }
155
156#ifdef ZEND_ENGINE_2
157    if (src->objects_count) {
158        zend_uint i;
159        for (i = 0; i < src->objects_count; ++i) {
160            zend_objects_store_del_ref_by_handle(processor.object_handles[i] TSRMLS_CC);
161        }
162        efree(processor.object_handles);
163    }
164#endif
165}
166dnl }}}
167define(`DEFINE_RELOCATE_API', `
168/* src = readable element, before memcpy if any
169 * dst = writable element, after memcpy if any
170 * virtual_src = brother pointers relatived to this address, before relocation
171 * virtual_dst = brother pointers relatived to this address, after  relocation
172 */
173EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
174{
175    char *old_address = 0; /* unkown X used later */
176    ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);
177
178    /* diff to new_ptr */
179    ptrdiff_t ptrdiff = ptrsub(dst, virtual_src);
180    ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);
181
182    xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
183}
184dnl }}}
185')
186DEFINE_RELOCATE_API(`xc_entry_var_t')
187DEFINE_RELOCATE_API(`xc_entry_php_t')
188DEFINE_RELOCATE_API(`xc_entry_data_php_t')
189EXPORTED(`#ifdef HAVE_XCACHE_DPRINT')
190EXPORTED_FUNCTION(`void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC)') dnl {{{
191{
192    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
193    xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
194}
195dnl }}}
196EXPORTED(`#endif')
Note: See TracBrowser for help on using the repository browser.