source: svn/trunk/processor/foot.m4 @ 1558

Last change on this file since 1558 was 1558, checked in by Xuefer, 5 years ago

closes #342: object caching

  • Property svn:eol-style set to native
File size: 6.3 KB
RevLine 
[1470]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 }}}
[1444]5define(`DEFINE_STORE_API', `
[1470]6EXPORTED_FUNCTION(`$1 *xc_processor_store_$1(const xc_processor_storage_t *storage, $1 *src TSRMLS_DC)') dnl {{{
[1444]7{
8    $1 *dst;
9    xc_processor_t processor;
10
11    memset(&processor, 0, sizeof(processor));
[1468]12    processor.handle_reference = 1;
[1470]13    processor.relocatediff = storage->relocatediff;
[1444]14
[1551]15    IFAUTOCHECK(`xc_vector_init(unsigned long, &processor.allocsizes, 0);')
[1444]16
17    /* calc size */ {
18        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
[1468]19        if (processor.handle_reference) {
[1444]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);
[1468]28        if (processor.handle_reference) {
[1444]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
[1551]41    IFAUTOCHECK(`xc_vector_reverse(&processor.allocsizes);')
[1444]42    /* store {{{ */
43    {
44        IFAUTOCHECK(`char *oldp;')
45        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
[1468]46        if (processor.handle_reference) {
[1444]47            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
48        }
49
50        /* allocator :) */
[1470]51        processor.p = (char *) storage->allocator(storage->allocator_data, processor.size);
[1444]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:
[1468]73        if (processor.handle_reference) {
[1444]74            zend_hash_destroy(&processor.zvalptrs);
75        }
76        zend_hash_destroy(&processor.strings);
77    }
78    /* }}} */
79
[1551]80    IFAUTOCHECK(`xc_vector_destroy(&processor.allocsizes);')
[1444]81
82    return dst;
83}
84dnl }}}
85')
86DEFINE_STORE_API(`xc_entry_php_t')
87DEFINE_STORE_API(`xc_entry_data_php_t')
[1558]88DEFINE_STORE_API(`xc_entry_var_t')
[1444]89EXPORTED_FUNCTION(`xc_entry_php_t *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
96    return dst;
97}
98dnl }}}
99EXPORTED_FUNCTION(`xc_entry_data_php_t *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 {{{
100{
101    xc_processor_t processor;
102
103    memset(&processor, 0, sizeof(processor));
104    processor.readonly_protection = readonly_protection;
105    /* this function is used for php data only */
106    if (SRC(`have_references')) {
[1468]107        processor.handle_reference = 1;
[1444]108    }
109    processor.entry_php_src = entry_php;
110
[1468]111    if (processor.handle_reference) {
[1444]112        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
113    }
114    xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
[1468]115    if (processor.handle_reference) {
[1444]116        zend_hash_destroy(&processor.zvalptrs);
117    }
118    return dst;
119}
120dnl }}}
[1558]121EXPORTED_FUNCTION(`zval *xc_processor_restore_var(zval *dst, const xc_entry_var_t *src TSRMLS_DC)') dnl {{{
[1444]122{
123    xc_processor_t processor;
[1558]124    size_t i;
[1444]125
126    memset(&processor, 0, sizeof(processor));
[1558]127    processor.handle_reference = src->have_references;
[1444]128
[1468]129    if (processor.handle_reference) {
[1444]130        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
131        dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
[1558]132        zend_hash_add(&processor.zvalptrs, (char *)src->value, sizeof(src->value), (void *) &dst, sizeof(dst), NULL);
[1444]133    }
[1558]134    processor.entry_var_src = src;
135
136#ifdef ZEND_ENGINE_2
137    if (src->objects_count) {
138        processor.object_handles = emalloc(sizeof(*processor.object_handles) * src->objects_count);
139        xc_vector_init(zend_object_handle, &processor.objects, 0);
140        for (i = 0; i < src->objects_count; ++i) {
141            zend_object *object = emalloc(sizeof(*object));
142            xc_restore_zend_object(&processor, object, &src->objects[i] TSRMLS_CC);
143            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);
144        }
145    }
146#endif
147    xc_restore_zval(&processor, dst, src->value TSRMLS_CC);
[1468]148    if (processor.handle_reference) {
[1444]149        zend_hash_destroy(&processor.zvalptrs);
150    }
151
[1558]152#ifdef ZEND_ENGINE_2
153    if (src->objects_count) {
154        for (i = 0; i < src->objects_count; ++i) {
155            zend_objects_store_del_ref_by_handle_ex(processor.object_handles[i], NULL TSRMLS_CC);
156        }
157        efree(processor.object_handles);
158    }
159#endif
160
[1444]161    return dst;
162}
163dnl }}}
164define(`DEFINE_RELOCATE_API', `
[1455]165/* src = readable element, before memcpy if any
166 * dst = writable element, after memcpy if any
167 * virtual_src = brother pointers relatived to this address, before relocation
168 * virtual_dst = brother pointers relatived to this address, after  relocation
[1453]169 */
[1455]170EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
[1444]171{
[1453]172    char *old_address = 0; /* unkown X used later */
[1455]173    ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);
[1446]174
[1448]175    /* diff to new_ptr */
[1456]176    ptrdiff_t ptrdiff = ptrsub(dst, virtual_src);
[1455]177    ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);
[1446]178
[1455]179    xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
[1444]180}
181dnl }}}
182')
183DEFINE_RELOCATE_API(`xc_entry_var_t')
184DEFINE_RELOCATE_API(`xc_entry_php_t')
185DEFINE_RELOCATE_API(`xc_entry_data_php_t')
186EXPORTED(`#ifdef HAVE_XCACHE_DPRINT')
187EXPORTED_FUNCTION(`void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC)') dnl {{{
188{
189    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
190    xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
191}
192dnl }}}
193EXPORTED(`#endif')
Note: See TracBrowser for help on using the repository browser.