summaryrefslogtreecommitdiff
path: root/Zend/zend_arena.h
blob: a44082e52fb9c78c50cd4ba40ec2762fb7c230ad (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.00 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.zend.com/license/2_00.txt.                                |
   | If you did not receive a copy of the Zend license and are unable to  |
   | obtain it through the world-wide-web, please send a note to          |
   | license@zend.com so we can mail you a copy immediately.              |
   +----------------------------------------------------------------------+
   | Authors: Dmitry Stogov <dmitry@php.net>                              |
   +----------------------------------------------------------------------+
*/

#ifndef _ZEND_ARENA_H_
#define _ZEND_ARENA_H_

#include "zend.h"

#ifndef ZEND_TRACK_ARENA_ALLOC

typedef struct _zend_arena zend_arena;

struct _zend_arena {
	char		*ptr;
	char		*end;
	zend_arena  *prev;
};

static zend_always_inline zend_arena* zend_arena_create(size_t size)
{
	zend_arena *arena = (zend_arena*)emalloc(size);

	arena->ptr = (char*) arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena));
	arena->end = (char*) arena + size;
	arena->prev = NULL;
	return arena;
}

static zend_always_inline void zend_arena_destroy(zend_arena *arena)
{
	do {
		zend_arena *prev = arena->prev;
		efree(arena);
		arena = prev;
	} while (arena);
}

static zend_always_inline void* zend_arena_alloc(zend_arena **arena_ptr, size_t size)
{
	zend_arena *arena = *arena_ptr;
	char *ptr = arena->ptr;

	size = ZEND_MM_ALIGNED_SIZE(size);

	if (EXPECTED(size <= (size_t)(arena->end - ptr))) {
		arena->ptr = ptr + size;
	} else {
		size_t arena_size =
			UNEXPECTED((size + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))) > (size_t)(arena->end - (char*) arena)) ?
				(size + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))) :
				(size_t)(arena->end - (char*) arena);
		zend_arena *new_arena = (zend_arena*)emalloc(arena_size);

		ptr = (char*) new_arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena));
		new_arena->ptr = (char*) new_arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena)) + size;
		new_arena->end = (char*) new_arena + arena_size;
		new_arena->prev = arena;
		*arena_ptr = new_arena;
	}

	return (void*) ptr;
}

static zend_always_inline void* zend_arena_calloc(zend_arena **arena_ptr, size_t count, size_t unit_size)
{
	bool overflow;
	size_t size;
	void *ret;

	size = zend_safe_address(unit_size, count, 0, &overflow);
	if (UNEXPECTED(overflow)) {
		zend_error(E_ERROR, "Possible integer overflow in zend_arena_calloc() (%zu * %zu)", unit_size, count);
	}
	ret = zend_arena_alloc(arena_ptr, size);
	memset(ret, 0, size);
	return ret;
}

static zend_always_inline void* zend_arena_checkpoint(zend_arena *arena)
{
	return arena->ptr;
}

static zend_always_inline void zend_arena_release(zend_arena **arena_ptr, void *checkpoint)
{
	zend_arena *arena = *arena_ptr;

	while (UNEXPECTED((char*)checkpoint > arena->end) ||
	       UNEXPECTED((char*)checkpoint <= (char*)arena)) {
		zend_arena *prev = arena->prev;
		efree(arena);
		*arena_ptr = arena = prev;
	}
	ZEND_ASSERT((char*)checkpoint > (char*)arena && (char*)checkpoint <= arena->end);
	arena->ptr = (char*)checkpoint;
}

static zend_always_inline bool zend_arena_contains(zend_arena *arena, void *ptr)
{
	while (arena) {
		if ((char*)ptr > (char*)arena && (char*)ptr <= arena->ptr) {
			return 1;
		}
		arena = arena->prev;
	}
	return 0;
}

#else

/* Use normal allocations and keep track of them for mass-freeing.
 * This is intended for use with asan/valgrind. */

typedef struct _zend_arena zend_arena;

struct _zend_arena {
	void **ptr;
	void **end;
	struct _zend_arena *prev;
	void *ptrs[0];
};

#define ZEND_TRACKED_ARENA_SIZE 1000

static zend_always_inline zend_arena *zend_arena_create(size_t _size)
{
	zend_arena *arena = (zend_arena*) emalloc(
		sizeof(zend_arena) + sizeof(void *) * ZEND_TRACKED_ARENA_SIZE);
	arena->ptr = &arena->ptrs[0];
	arena->end = &arena->ptrs[ZEND_TRACKED_ARENA_SIZE];
	arena->prev = NULL;
	return arena;
}

static zend_always_inline void zend_arena_destroy(zend_arena *arena)
{
	do {
		zend_arena *prev = arena->prev;
		void **ptr;
		for (ptr = arena->ptrs; ptr < arena->ptr; ptr++) {
			efree(*ptr);
		}
		efree(arena);
		arena = prev;
	} while (arena);
}

static zend_always_inline void *zend_arena_alloc(zend_arena **arena_ptr, size_t size)
{
	zend_arena *arena = *arena_ptr;
	if (arena->ptr == arena->end) {
		*arena_ptr = zend_arena_create(0);
		(*arena_ptr)->prev = arena;
		arena = *arena_ptr;
	}

	return *arena->ptr++ = emalloc(size);
}

static zend_always_inline void* zend_arena_calloc(zend_arena **arena_ptr, size_t count, size_t unit_size)
{
	bool overflow;
	size_t size;
	void *ret;

	size = zend_safe_address(unit_size, count, 0, &overflow);
	if (UNEXPECTED(overflow)) {
		zend_error(E_ERROR, "Possible integer overflow in zend_arena_calloc() (%zu * %zu)", unit_size, count);
	}
	ret = zend_arena_alloc(arena_ptr, size);
	memset(ret, 0, size);
	return ret;
}

static zend_always_inline void* zend_arena_checkpoint(zend_arena *arena)
{
	return arena->ptr;
}

static zend_always_inline void zend_arena_release(zend_arena **arena_ptr, void *checkpoint)
{
	while (1) {
		zend_arena *arena = *arena_ptr;
		zend_arena *prev = arena->prev;
		while (1) {
			if (arena->ptr == (void **) checkpoint) {
				return;
			}
			if (arena->ptr == arena->ptrs) {
				break;
			}
			arena->ptr--;
			efree(*arena->ptr);
		}
		efree(arena);
		*arena_ptr = prev;
		ZEND_ASSERT(*arena_ptr);
	}
}

static zend_always_inline bool zend_arena_contains(zend_arena *arena, void *ptr)
{
	/* TODO: Dummy */
	return 1;
}

#endif

#endif /* _ZEND_ARENA_H_ */