source: trunk/processor/foot.m4 @ 1470

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

processor: abstract store target (storage)

  • Property svn:eol-style set to native
File size: 5.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_stack_init(&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_stack_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_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')) {
107        processor.handle_reference = 1;
108    }
109    processor.entry_php_src = entry_php;
110
111    if (processor.handle_reference) {
112        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
113    }
114    xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
115    if (processor.handle_reference) {
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));
126    processor.handle_reference = have_references;
127
128    if (processor.handle_reference) {
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);
134    if (processor.handle_reference) {
135        zend_hash_destroy(&processor.zvalptrs);
136    }
137
138    return dst;
139}
140dnl }}}
141define(`DEFINE_RELOCATE_API', `
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
146 */
147EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
148{
149    char *old_address = 0; /* unkown X used later */
150    ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);
151
152    /* diff to new_ptr */
153    ptrdiff_t ptrdiff = ptrsub(dst, virtual_src);
154    ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);
155
156    xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
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.