source: trunk/processor/foot.m4 @ 1455

Last change on this file since 1455 was 1455, checked in by moo, 13 months ago

relocate: update proto to like memcpy/store/restore, update test code

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1define(`DEFINE_STORE_API', `
2EXPORTED_FUNCTION(`$1 *xc_processor_store_$1(xc_shm_t *shm, xc_allocator_t *allocator, $1 *src TSRMLS_DC)') dnl {{{
3{
4    $1 *dst;
5    xc_processor_t processor;
6
7    memset(&processor, 0, sizeof(processor));
8    processor.reference = 1;
9    processor.shm = shm;
10    processor.allocator = allocator;
11
12    IFAUTOCHECK(`xc_stack_init(&processor.allocsizes);')
13
14    /* calc size */ {
15        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
16        if (processor.reference) {
17            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
18        }
19
20        processor.size = 0;
21        /* allocate */
22        processor.size = ALIGN(processor.size + sizeof(src[0]));
23
24        xc_calc_$1(&processor, src TSRMLS_CC);
25        if (processor.reference) {
26            zend_hash_destroy(&processor.zvalptrs);
27        }
28        zend_hash_destroy(&processor.strings);
29    }
30    ifelse(
31        `$1', `xc_entry_data_php_t', `SRC(`size')',
32        `', `', SRC(`entry.size')) = processor.size;
33    ifelse(
34        `$1', `xc_entry_var_t', `SRC(`have_references') = processor.have_references;',
35        `$1', `xc_entry_data_php_t', `SRC(`have_references') = processor.have_references;'
36    )
37
38    IFAUTOCHECK(`xc_stack_reverse(&processor.allocsizes);')
39    /* store {{{ */
40    {
41        IFAUTOCHECK(`char *oldp;')
42        zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
43        if (processor.reference) {
44            zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
45        }
46
47        /* allocator :) */
48        processor.p = (char *) processor.allocator->vtable->malloc(processor.allocator, processor.size);
49        if (processor.p == NULL) {
50            dst = NULL;
51            goto err_alloc;
52        }
53        IFAUTOCHECK(`oldp = processor.p;')
54        assert(processor.p == (char *) ALIGN(processor.p));
55
56        /* allocate */
57        dst = ($1 *) processor.p;
58        processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
59
60        xc_store_$1(&processor, dst, src TSRMLS_CC);
61        IFAUTOCHECK(` {
62            size_t unexpected = processor.p - oldp;
63            size_t expected = processor.size;
64            if (unexpected != processor.size) {
65                fprintf(stderr, "unexpected:%lu - expected:%lu = %ld != 0\n", (unsigned long) unexpected, (unsigned long) expected, (long) unexpected - expected);
66                abort();
67            }
68        }')
69err_alloc:
70        if (processor.reference) {
71            zend_hash_destroy(&processor.zvalptrs);
72        }
73        zend_hash_destroy(&processor.strings);
74    }
75    /* }}} */
76
77    IFAUTOCHECK(`xc_stack_destroy(&processor.allocsizes);')
78
79    return dst;
80}
81dnl }}}
82')
83DEFINE_STORE_API(`xc_entry_var_t')
84DEFINE_STORE_API(`xc_entry_php_t')
85DEFINE_STORE_API(`xc_entry_data_php_t')
86EXPORTED_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 {{{
87{
88    xc_processor_t processor;
89
90    memset(&processor, 0, sizeof(processor));
91    xc_restore_xc_entry_php_t(&processor, dst, src TSRMLS_CC);
92
93    return dst;
94}
95dnl }}}
96EXPORTED_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 {{{
97{
98    xc_processor_t processor;
99
100    memset(&processor, 0, sizeof(processor));
101    processor.readonly_protection = readonly_protection;
102    /* this function is used for php data only */
103    if (SRC(`have_references')) {
104        processor.reference = 1;
105    }
106    processor.entry_php_src = entry_php;
107
108    if (processor.reference) {
109        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
110    }
111    xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
112    if (processor.reference) {
113        zend_hash_destroy(&processor.zvalptrs);
114    }
115    return dst;
116}
117dnl }}}
118EXPORTED_FUNCTION(`zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC)') dnl {{{
119{
120    xc_processor_t processor;
121
122    memset(&processor, 0, sizeof(processor));
123    processor.reference = have_references;
124
125    if (processor.reference) {
126        zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
127        dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
128        zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
129    }
130    xc_restore_zval(&processor, dst, src TSRMLS_CC);
131    if (processor.reference) {
132        zend_hash_destroy(&processor.zvalptrs);
133    }
134
135    return dst;
136}
137dnl }}}
138define(`DEFINE_RELOCATE_API', `
139/* src = readable element, before memcpy if any
140 * dst = writable element, after memcpy if any
141 * virtual_src = brother pointers relatived to this address, before relocation
142 * virtual_dst = brother pointers relatived to this address, after  relocation
143 */
144EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
145{
146    char *old_address = 0; /* unkown X used later */
147    ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);
148
149    /* diff to new_ptr */
150    ptrdiff_t ptrdiff = ptrsub(dst, src);
151    ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);
152    assert(ptradd($1 *, src, ptrdiff) == dst);
153
154    xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
155}
156dnl }}}
157')
158DEFINE_RELOCATE_API(`xc_entry_var_t')
159DEFINE_RELOCATE_API(`xc_entry_php_t')
160DEFINE_RELOCATE_API(`xc_entry_data_php_t')
161EXPORTED(`#ifdef HAVE_XCACHE_DPRINT')
162EXPORTED_FUNCTION(`void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC)') dnl {{{
163{
164    IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
165    xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
166}
167dnl }}}
168EXPORTED(`#endif')
Note: See TracBrowser for help on using the repository browser.