source: trunk/processor/foot.m4

Last change on this file was 1470, checked in by moo, 5 months ago

processor: abstract store target (storage)

  • Property svn:eol-style set to native
File size: 5.4 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
15    IFAUTOCHECK(`xc_stack_init(&processor.allocsizes);')
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
41    IFAUTOCHECK(`xc_stack_reverse(&processor.allocsizes);')
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
80    IFAUTOCHECK(`xc_stack_destroy(&processor.allocsizes);')
81
82    return dst;
83}
84dnl }}}
85')
86DEFINE_STORE_API(`xc_entry_var_t')
87DEFINE_STORE_API(`xc_entry_php_t')
88DEFINE_STORE_API(`xc_entry_data_php_t')
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 }}}
121EXPORTED_FUNCTION(`zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC)') dnl {{{
122{
123    xc_processor_t processor;
124
125    memset(&processor, 0, sizeof(processor));
[1468]126    processor.handle_reference = have_references;
[1444]127
[1468]128    if (processor.handle_reference) {
[1444]129        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
130        dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
131        zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
132    }
133    xc_restore_zval(&processor, dst, src TSRMLS_CC);
[1468]134    if (processor.handle_reference) {
[1444]135        zend_hash_destroy(&processor.zvalptrs);
136    }
137
138    return dst;
139}
140dnl }}}
141define(`DEFINE_RELOCATE_API', `
[1455]142/* src = readable element, before memcpy if any
143 * dst = writable element, after memcpy if any
144 * virtual_src = brother pointers relatived to this address, before relocation
145 * virtual_dst = brother pointers relatived to this address, after  relocation
[1453]146 */
[1455]147EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
[1444]148{
[1453]149    char *old_address = 0; /* unkown X used later */
[1455]150    ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);
[1446]151
[1448]152    /* diff to new_ptr */
[1456]153    ptrdiff_t ptrdiff = ptrsub(dst, virtual_src);
[1455]154    ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);
[1446]155
[1455]156    xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
[1444]157}
158dnl }}}
159')
160DEFINE_RELOCATE_API(`xc_entry_var_t')
161DEFINE_RELOCATE_API(`xc_entry_php_t')
162DEFINE_RELOCATE_API(`xc_entry_data_php_t')
163EXPORTED(`#ifdef HAVE_XCACHE_DPRINT')
164EXPORTED_FUNCTION(`void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC)') dnl {{{
165{
166    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
167    xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
168}
169dnl }}}
170EXPORTED(`#endif')
Note: See TracBrowser for help on using the repository browser.