php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
sljitExecAllocatorCore.c
Go to the documentation of this file.
1/*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27/*
28 This file contains a simple executable memory allocator
29
30 It is assumed, that executable code blocks are usually medium (or sometimes
31 large) memory blocks, and the allocator is not too frequently called (less
32 optimized than other allocators). Thus, using it as a generic allocator is
33 not suggested.
34
35 How does it work:
36 Memory is allocated in continuous memory areas called chunks by alloc_chunk()
37 Chunk format:
38 [ block ][ block ] ... [ block ][ block terminator ]
39
40 All blocks and the block terminator is started with block_header. The block
41 header contains the size of the previous and the next block. These sizes
42 can also contain special values.
43 Block size:
44 0 - The block is a free_block, with a different size member.
45 1 - The block is a block terminator.
46 n - The block is used at the moment, and the value contains its size.
47 Previous block size:
48 0 - This is the first block of the memory chunk.
49 n - The size of the previous block.
50
51 Using these size values we can go forward or backward on the block chain.
52 The unused blocks are stored in a chain list pointed by free_blocks. This
53 list is useful if we need to find a suitable memory area when the allocator
54 is called.
55
56 When a block is freed, the new free block is connected to its adjacent free
57 blocks if possible.
58
59 [ free block ][ used block ][ free block ]
60 and "used block" is freed, the three blocks are connected together:
61 [ one big free block ]
62*/
63
64/* Expected functions:
65 alloc_chunk / free_chunk :
66 * allocate executable system memory chunks
67 * the size is always divisible by CHUNK_SIZE
68 SLJIT_ALLOCATOR_LOCK / SLJIT_ALLOCATOR_UNLOCK :
69 * provided as part of sljitUtils
70 * only the allocator requires this lock, sljit is fully thread safe
71 as it only uses local variables
72
73 Supported defines:
74 SLJIT_HAS_CHUNK_HEADER - (optional) sljit_chunk_header is defined
75 SLJIT_HAS_EXECUTABLE_OFFSET - (optional) has executable offset data
76 SLJIT_UPDATE_WX_FLAGS - (optional) update WX flags
77*/
78
79#ifdef SLJIT_HAS_CHUNK_HEADER
80#define CHUNK_HEADER_SIZE (sizeof(struct sljit_chunk_header))
81#else /* !SLJIT_HAS_CHUNK_HEADER */
82#define CHUNK_HEADER_SIZE 0
83#endif /* SLJIT_HAS_CHUNK_HEADER */
84
85#ifndef SLJIT_UPDATE_WX_FLAGS
86#define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec)
87#endif /* SLJIT_UPDATE_WX_FLAGS */
88
89#ifndef CHUNK_SIZE
90/* 64 KByte if not specified. */
91#define CHUNK_SIZE (sljit_uw)0x10000
92#endif /* CHUNK_SIZE */
93
97#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
98 sljit_sw executable_offset;
99#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
100};
101
108
109#define AS_BLOCK_HEADER(base, offset) \
110 ((struct block_header*)(((sljit_u8*)base) + offset))
111#define AS_FREE_BLOCK(base, offset) \
112 ((struct free_block*)(((sljit_u8*)base) + offset))
113#define MEM_START(base) ((void*)((base) + 1))
114#define CHUNK_MASK (~(CHUNK_SIZE - 1))
115#define ALIGN_SIZE(size) (((size) + sizeof(struct block_header) + 7u) & ~(sljit_uw)7)
116#define CHUNK_EXTRA_SIZE (sizeof(struct block_header) + CHUNK_HEADER_SIZE)
117
118static struct free_block* free_blocks;
119static sljit_uw allocated_size;
120static sljit_uw total_size;
121
122static SLJIT_INLINE void sljit_insert_free_block(struct free_block *free_block, sljit_uw size)
123{
126
127 free_block->next = free_blocks;
129 if (free_blocks)
130 free_blocks->prev = free_block;
131 free_blocks = free_block;
132}
133
134static SLJIT_INLINE void sljit_remove_free_block(struct free_block *free_block)
135{
136 if (free_block->next)
138
139 if (free_block->prev)
141 else {
142 SLJIT_ASSERT(free_blocks == free_block);
143 free_blocks = free_block->next;
144 }
145}
146
148{
149 struct block_header *header;
150 struct block_header *next_header;
151 struct free_block *free_block;
152 sljit_uw chunk_size;
153
154#ifdef SLJIT_HAS_CHUNK_HEADER
155 struct sljit_chunk_header *chunk_header;
156#else /* !SLJIT_HAS_CHUNK_HEADER */
157 void *chunk_header;
158#endif /* SLJIT_HAS_CHUNK_HEADER */
159
160#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
161 sljit_sw executable_offset;
162#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
163
164 if (size < (64 - sizeof(struct block_header)))
165 size = (64 - sizeof(struct block_header));
167
168 SLJIT_ALLOCATOR_LOCK();
169 free_block = free_blocks;
170 while (free_block) {
171 if (free_block->size >= size) {
172 chunk_size = free_block->size;
174 if (chunk_size > size + 64) {
175 /* We just cut a block from the end of the free block. */
176 chunk_size -= size;
177 free_block->size = chunk_size;
178 header = AS_BLOCK_HEADER(free_block, chunk_size);
179 header->prev_size = chunk_size;
180#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
181 header->executable_offset = free_block->header.executable_offset;
182#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
184 } else {
185 sljit_remove_free_block(free_block);
186 header = (struct block_header*)free_block;
187 size = chunk_size;
188 }
189 allocated_size += size;
190 header->size = size;
191 SLJIT_ALLOCATOR_UNLOCK();
192 return MEM_START(header);
193 }
195 }
196
197 chunk_size = (size + CHUNK_EXTRA_SIZE + CHUNK_SIZE - 1) & CHUNK_MASK;
198
199 chunk_header = alloc_chunk(chunk_size);
200 if (!chunk_header) {
201 SLJIT_ALLOCATOR_UNLOCK();
202 return NULL;
203 }
204
205#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
206 executable_offset = (sljit_sw)((sljit_u8*)chunk_header->executable - (sljit_u8*)chunk_header);
207#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
208
209 chunk_size -= CHUNK_EXTRA_SIZE;
210 total_size += chunk_size;
211
212 header = (struct block_header*)(((sljit_u8*)chunk_header) + CHUNK_HEADER_SIZE);
213
214 header->prev_size = 0;
215#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
216 header->executable_offset = executable_offset;
217#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
218
219 if (chunk_size > size + 64) {
220 /* Cut the allocated space into a free and a used block. */
221 allocated_size += size;
222 header->size = size;
223 chunk_size -= size;
224
227#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
228 free_block->header.executable_offset = executable_offset;
229#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
230 sljit_insert_free_block(free_block, chunk_size);
231 next_header = AS_BLOCK_HEADER(free_block, chunk_size);
232 } else {
233 /* All space belongs to this allocation. */
234 allocated_size += chunk_size;
235 header->size = chunk_size;
236 next_header = AS_BLOCK_HEADER(header, chunk_size);
237 }
238 next_header->size = 1;
239 next_header->prev_size = chunk_size;
240#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
241 next_header->executable_offset = executable_offset;
242#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
243 SLJIT_ALLOCATOR_UNLOCK();
244 return MEM_START(header);
245}
246
248{
249 struct block_header *header;
250 struct free_block *free_block;
251
252 SLJIT_ALLOCATOR_LOCK();
253 header = AS_BLOCK_HEADER(ptr, -(sljit_sw)sizeof(struct block_header));
254#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
255 header = AS_BLOCK_HEADER(header, -header->executable_offset);
256#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
257 allocated_size -= header->size;
258
260
261 /* Connecting free blocks together if possible. */
262
263 /* If header->prev_size == 0, free_block will equal to header.
264 In this case, free_block->header.size will be > 0. */
267 free_block->size += header->size;
269 header->prev_size = free_block->size;
270 } else {
271 free_block = (struct free_block*)header;
272 sljit_insert_free_block(free_block, header->size);
273 }
274
276 if (SLJIT_UNLIKELY(!header->size)) {
277 free_block->size += ((struct free_block*)header)->size;
278 sljit_remove_free_block((struct free_block*)header);
280 header->prev_size = free_block->size;
281 }
282
283 /* The whole chunk is free. */
284 if (SLJIT_UNLIKELY(!free_block->header.prev_size && header->size == 1)) {
285 /* If this block is freed, we still have (allocated_size / 2) free space. */
286 if (total_size - free_block->size > (allocated_size * 3 / 2)) {
287 total_size -= free_block->size;
288 sljit_remove_free_block(free_block);
290 }
291 }
292
294 SLJIT_ALLOCATOR_UNLOCK();
295}
296
298{
299 struct free_block* free_block;
300 struct free_block* next_free_block;
301
302 SLJIT_ALLOCATOR_LOCK();
304
305 free_block = free_blocks;
306 while (free_block) {
307 next_free_block = free_block->next;
310 total_size -= free_block->size;
311 sljit_remove_free_block(free_block);
313 }
314 free_block = next_free_block;
315 }
316
317 SLJIT_ASSERT(total_size || (!total_size && !free_blocks));
319 SLJIT_ALLOCATOR_UNLOCK();
320}
321
322#ifdef SLJIT_HAS_EXECUTABLE_OFFSET
323SLJIT_API_FUNC_ATTRIBUTE sljit_sw sljit_exec_offset(void *code)
324{
325 return ((struct block_header*)SLJIT_CODE_TO_PTR(code))[-1].executable_offset;
326}
327#endif /* SLJIT_HAS_EXECUTABLE_OFFSET */
header(string $header, bool $replace=true, int $response_code=0)
new_type size
Definition ffi.c:4365
void * ptr
Definition ffi.c:3814
#define NULL
Definition gdcache.h:45
#define CHUNK_SIZE
#define SLJIT_UNLIKELY(x)
#define SLJIT_API_FUNC_ATTRIBUTE
unsigned int sljit_uw
unsigned char sljit_u8
#define SLJIT_ASSERT(x)
#define SLJIT_INLINE
int sljit_sw
#define CHUNK_MASK
#define CHUNK_EXTRA_SIZE
#define CHUNK_HEADER_SIZE
SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void *ptr)
#define AS_BLOCK_HEADER(base, offset)
SLJIT_API_FUNC_ATTRIBUTE void * sljit_malloc_exec(sljit_uw size)
#define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec)
#define AS_FREE_BLOCK(base, offset)
#define MEM_START(base)
#define ALIGN_SIZE(size)
SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void)
#define SLJIT_CODE_TO_PTR(code)
Definition sljitLir.c:326
struct free_block * prev
struct block_header header
struct free_block * next