summaryrefslogtreecommitdiff
path: root/processor/foot.m4
blob: 9d1fb294d93acffa8a495cea512c280fb4d01e97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
dnl {{{ xc_store_target
EXPORT(`typedef void *(*xc_processor_storage_allocator_t)(void *data, size_t size);')
EXPORT(`typedef struct { xc_processor_storage_allocator_t allocator; void *allocator_data; ptrdiff_t relocatediff; } xc_processor_storage_t;')
dnl }}}
define(`DEFINE_STORE_API', `
EXPORTED_FUNCTION(`$1 *xc_processor_store_$1(const xc_processor_storage_t *storage, $1 *src TSRMLS_DC)') dnl {{{
{
	$1 *dst;
	xc_processor_t processor;

	memset(&processor, 0, sizeof(processor));
	processor.handle_reference = 1;
	processor.relocatediff = storage->relocatediff;

	IFAUTOCHECK(`xc_vector_init(unsigned long, &processor.allocsizes);')

	/* calc size */ {
		zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
		if (processor.handle_reference) {
			zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
		}

		processor.size = 0;
		/* allocate */
		processor.size = ALIGN(processor.size + sizeof(src[0]));

		xc_calc_$1(&processor, src TSRMLS_CC);
		if (processor.handle_reference) {
			zend_hash_destroy(&processor.zvalptrs);
		}
		zend_hash_destroy(&processor.strings);
	}
	ifelse(
		`$1', `xc_entry_data_php_t', `SRC(`size')',
		`', `', SRC(`entry.size')) = processor.size;
	ifelse(
		`$1', `xc_entry_var_t', `SRC(`have_references') = processor.have_references;',
		`$1', `xc_entry_data_php_t', `SRC(`have_references') = processor.have_references;'
	)

	IFAUTOCHECK(`xc_vector_reverse(&processor.allocsizes);')
	/* store {{{ */
	{
		IFAUTOCHECK(`char *oldp;')
		zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
		if (processor.handle_reference) {
			zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
		}

		/* allocator :) */
		processor.p = (char *) storage->allocator(storage->allocator_data, processor.size);
		if (processor.p == NULL) {
			dst = NULL;
			goto err_alloc;
		}
		IFAUTOCHECK(`oldp = processor.p;')
		assert(processor.p == (char *) ALIGN(processor.p));

		/* allocate */
		dst = ($1 *) processor.p;
		processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));

		xc_store_$1(&processor, dst, src TSRMLS_CC);
		IFAUTOCHECK(` {
			size_t unexpected = processor.p - oldp;
			size_t expected = processor.size;
			if (unexpected != processor.size) {
				fprintf(stderr, "unexpected:%lu - expected:%lu = %ld != 0\n", (unsigned long) unexpected, (unsigned long) expected, (long) unexpected - expected);
				abort();
			}
		}')
err_alloc:
		if (processor.handle_reference) {
			zend_hash_destroy(&processor.zvalptrs);
		}
		zend_hash_destroy(&processor.strings);
	}
	/* }}} */

	IFAUTOCHECK(`xc_vector_destroy(&processor.allocsizes);')

	return dst;
}
dnl }}}
')
DEFINE_STORE_API(`xc_entry_php_t')
DEFINE_STORE_API(`xc_entry_data_php_t')
DEFINE_STORE_API(`xc_entry_var_t')
EXPORTED_FUNCTION(`void xc_processor_restore_xc_entry_php_t(xc_entry_php_t *dst, const xc_entry_php_t *src TSRMLS_DC)') dnl {{{
{
	xc_processor_t processor;

	memset(&processor, 0, sizeof(processor));
	xc_restore_xc_entry_php_t(&processor, dst, src TSRMLS_CC);
}
dnl }}}
EXPORTED_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 {{{
{
	xc_processor_t processor;

	memset(&processor, 0, sizeof(processor));
	processor.readonly_protection = readonly_protection;
	/* this function is used for php data only */
	if (SRC(`have_references')) {
		processor.handle_reference = 1;
	}
	processor.entry_php_src = entry_php;

	if (processor.handle_reference) {
		zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
	}
	xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
	if (processor.handle_reference) {
		zend_hash_destroy(&processor.zvalptrs);
	}
}
dnl }}}
EXPORTED_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 {{{
{
	xc_processor_t processor;

	memset(&processor, 0, sizeof(processor));
	processor.handle_reference = src->have_references;

	if (processor.handle_reference) {
		zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
	}
	processor.index_to_ce = index_to_ce;

#ifdef ZEND_ENGINE_2
	if (src->objects_count) {
		int i;
		size_t size = sizeof(processor.object_handles[0]) * src->objects_count;
		processor.object_handles = emalloc(size);
		IFAUTOCHECK(`xc_memsetptr(processor.object_handles, (void *) -1, size);')
		/* other objects in $object->properties after $object, reverse order is necessary */
		for (i = src->objects_count - 1; i >= 0; --i) {
			zend_object *object = emalloc(sizeof(*object));
			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);
			xc_restore_zend_object(&processor, object, &src->objects[i] TSRMLS_CC);
		}
	}
#endif
	if (dst_ptr) {
		zval_ptr_dtor(dst_ptr);
		xc_restore_zval_ptr(&processor, dst_ptr, &src->value TSRMLS_CC);
	}
	else {
		zval_dtor(dst);
		xc_restore_zval(&processor, dst, src->value TSRMLS_CC);
	}
	if (processor.handle_reference) {
		zend_hash_destroy(&processor.zvalptrs);
	}

#ifdef ZEND_ENGINE_2
	if (src->objects_count) {
		zend_uint i;
		for (i = 0; i < src->objects_count; ++i) {
			zend_objects_store_del_ref_by_handle(processor.object_handles[i] TSRMLS_CC);
		}
		efree(processor.object_handles);
	}
#endif
}
dnl }}}
define(`DEFINE_RELOCATE_API', `
/* src = readable element, before memcpy if any
 * dst = writable element, after memcpy if any
 * virtual_src = brother pointers relatived to this address, before relocation
 * virtual_dst = brother pointers relatived to this address, after  relocation
 */
EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
{
	char *old_address = 0; /* unkown X used later */
	ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);

	/* diff to new_ptr */
	ptrdiff_t ptrdiff = ptrsub(dst, virtual_src);
	ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);

	xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
}
dnl }}}
')
DEFINE_RELOCATE_API(`xc_entry_var_t')
DEFINE_RELOCATE_API(`xc_entry_php_t')
DEFINE_RELOCATE_API(`xc_entry_data_php_t')
EXPORTED(`#ifdef HAVE_XCACHE_DPRINT')
EXPORTED_FUNCTION(`void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC)') dnl {{{
{
	IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
	xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
}
dnl }}}
EXPORTED(`#endif')