/src/php-src/Zend/zend_alloc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to version 2.00 of the Zend license, | |
8 | | | that is bundled with this package in the file LICENSE, and is | |
9 | | | available through the world-wide-web at the following url: | |
10 | | | http://www.zend.com/license/2_00.txt. | |
11 | | | If you did not receive a copy of the Zend license and are unable to | |
12 | | | obtain it through the world-wide-web, please send a note to | |
13 | | | license@zend.com so we can mail you a copy immediately. | |
14 | | +----------------------------------------------------------------------+ |
15 | | | Authors: Andi Gutmans <andi@php.net> | |
16 | | | Zeev Suraski <zeev@php.net> | |
17 | | | Dmitry Stogov <dmitry@php.net> | |
18 | | +----------------------------------------------------------------------+ |
19 | | */ |
20 | | |
21 | | /* |
22 | | * zend_alloc is designed to be a modern CPU cache friendly memory manager |
23 | | * for PHP. Most ideas are taken from jemalloc and tcmalloc implementations. |
24 | | * |
25 | | * All allocations are split into 3 categories: |
26 | | * |
27 | | * Huge - the size is greater than CHUNK size (~2M by default), allocation is |
28 | | * performed using mmap(). The result is aligned on 2M boundary. |
29 | | * |
30 | | * Large - a number of 4096K pages inside a CHUNK. Large blocks |
31 | | * are always aligned on page boundary. |
32 | | * |
33 | | * Small - less than 3/4 of page size. Small sizes are rounded up to nearest |
34 | | * greater predefined small size (there are 30 predefined sizes: |
35 | | * 8, 16, 24, 32, ... 3072). Small blocks are allocated from |
36 | | * RUNs. Each RUN is allocated as a single or few following pages. |
37 | | * Allocation inside RUNs implemented using linked list of free |
38 | | * elements. The result is aligned to 8 bytes. |
39 | | * |
40 | | * zend_alloc allocates memory from OS by CHUNKs, these CHUNKs and huge memory |
41 | | * blocks are always aligned to CHUNK boundary. So it's very easy to determine |
42 | | * the CHUNK owning the certain pointer. Regular CHUNKs reserve a single |
43 | | * page at start for special purpose. It contains bitset of free pages, |
44 | | * few bitset for available runs of predefined small sizes, map of pages that |
45 | | * keeps information about usage of each page in this CHUNK, etc. |
46 | | * |
47 | | * zend_alloc provides familiar emalloc/efree/erealloc API, but in addition it |
48 | | * provides specialized and optimized routines to allocate blocks of predefined |
49 | | * sizes (e.g. emalloc_2(), emallc_4(), ..., emalloc_large(), etc) |
50 | | * The library uses C preprocessor tricks that substitute calls to emalloc() |
51 | | * with more specialized routines when the requested size is known. |
52 | | */ |
53 | | |
54 | | #include "zend.h" |
55 | | #include "zend_alloc.h" |
56 | | #include "zend_globals.h" |
57 | | #include "zend_operators.h" |
58 | | #include "zend_multiply.h" |
59 | | #include "zend_bitset.h" |
60 | | #include <signal.h> |
61 | | |
62 | | #ifdef HAVE_UNISTD_H |
63 | | # include <unistd.h> |
64 | | #endif |
65 | | |
66 | | #ifdef ZEND_WIN32 |
67 | | # include <wincrypt.h> |
68 | | # include <process.h> |
69 | | # include "win32/winutil.h" |
70 | | #endif |
71 | | |
72 | | #include <stdio.h> |
73 | | #include <stdlib.h> |
74 | | #include <string.h> |
75 | | |
76 | | #include <sys/types.h> |
77 | | #include <sys/stat.h> |
78 | | #include <limits.h> |
79 | | #include <fcntl.h> |
80 | | #include <errno.h> |
81 | | |
82 | | #ifndef _WIN32 |
83 | | # include <sys/mman.h> |
84 | | # ifndef MAP_ANON |
85 | | # ifdef MAP_ANONYMOUS |
86 | | # define MAP_ANON MAP_ANONYMOUS |
87 | | # endif |
88 | | # endif |
89 | | # ifndef MAP_FAILED |
90 | | # define MAP_FAILED ((void*)-1) |
91 | | # endif |
92 | | # ifndef MAP_POPULATE |
93 | | # define MAP_POPULATE 0 |
94 | | # endif |
95 | | # if defined(_SC_PAGESIZE) || (_SC_PAGE_SIZE) |
96 | 3.00k | # define REAL_PAGE_SIZE _real_page_size |
97 | | static size_t _real_page_size = ZEND_MM_PAGE_SIZE; |
98 | | # endif |
99 | | # ifdef MAP_ALIGNED_SUPER |
100 | | # define MAP_HUGETLB MAP_ALIGNED_SUPER |
101 | | # endif |
102 | | #endif |
103 | | |
104 | | #ifndef REAL_PAGE_SIZE |
105 | | # define REAL_PAGE_SIZE ZEND_MM_PAGE_SIZE |
106 | | #endif |
107 | | |
108 | | /* NetBSD has an mremap() function with a signature that is incompatible with Linux (WTF?), |
109 | | * so pretend it doesn't exist. */ |
110 | | #ifndef __linux__ |
111 | | # undef HAVE_MREMAP |
112 | | #endif |
113 | | |
114 | | #ifndef __APPLE__ |
115 | 0 | # define ZEND_MM_FD -1 |
116 | | #else |
117 | | /* Mac allows to track anonymous page via vmmap per TAG id. |
118 | | * user land applications are allowed to take from 240 to 255. |
119 | | */ |
120 | | # define ZEND_MM_FD (250<<24) |
121 | | #endif |
122 | | |
123 | | #ifndef ZEND_MM_STAT |
124 | | # define ZEND_MM_STAT 1 /* track current and peak memory usage */ |
125 | | #endif |
126 | | #ifndef ZEND_MM_LIMIT |
127 | | # define ZEND_MM_LIMIT 1 /* support for user-defined memory limit */ |
128 | | #endif |
129 | | #ifndef ZEND_MM_CUSTOM |
130 | | # define ZEND_MM_CUSTOM 1 /* support for custom memory allocator */ |
131 | | /* USE_ZEND_ALLOC=0 may switch to system malloc() */ |
132 | | #endif |
133 | | #ifndef ZEND_MM_STORAGE |
134 | | # define ZEND_MM_STORAGE 1 /* support for custom memory storage */ |
135 | | #endif |
136 | | #ifndef ZEND_MM_ERROR |
137 | | # define ZEND_MM_ERROR 1 /* report system errors */ |
138 | | #endif |
139 | | |
140 | | #ifndef ZEND_MM_CHECK |
141 | 0 | # define ZEND_MM_CHECK(condition, message) do { \ |
142 | 0 | if (UNEXPECTED(!(condition))) { \ |
143 | 0 | zend_mm_panic(message); \ |
144 | 0 | } \ |
145 | 0 | } while (0) |
146 | | #endif |
147 | | |
148 | | typedef uint32_t zend_mm_page_info; /* 4-byte integer */ |
149 | | typedef zend_ulong zend_mm_bitset; /* 4-byte or 8-byte integer */ |
150 | | |
151 | | #define ZEND_MM_ALIGNED_OFFSET(size, alignment) \ |
152 | 0 | (((size_t)(size)) & ((alignment) - 1)) |
153 | | #define ZEND_MM_ALIGNED_BASE(size, alignment) \ |
154 | 0 | (((size_t)(size)) & ~((alignment) - 1)) |
155 | | #define ZEND_MM_SIZE_TO_NUM(size, alignment) \ |
156 | 0 | (((size_t)(size) + ((alignment) - 1)) / (alignment)) |
157 | | |
158 | 0 | #define ZEND_MM_BITSET_LEN (sizeof(zend_mm_bitset) * 8) /* 32 or 64 */ |
159 | | #define ZEND_MM_PAGE_MAP_LEN (ZEND_MM_PAGES / ZEND_MM_BITSET_LEN) /* 16 or 8 */ |
160 | | |
161 | | typedef zend_mm_bitset zend_mm_page_map[ZEND_MM_PAGE_MAP_LEN]; /* 64B */ |
162 | | |
163 | | #define ZEND_MM_IS_FRUN 0x00000000 |
164 | 0 | #define ZEND_MM_IS_LRUN 0x40000000 |
165 | 0 | #define ZEND_MM_IS_SRUN 0x80000000 |
166 | | |
167 | 0 | #define ZEND_MM_LRUN_PAGES_MASK 0x000003ff |
168 | 0 | #define ZEND_MM_LRUN_PAGES_OFFSET 0 |
169 | | |
170 | 0 | #define ZEND_MM_SRUN_BIN_NUM_MASK 0x0000001f |
171 | 0 | #define ZEND_MM_SRUN_BIN_NUM_OFFSET 0 |
172 | | |
173 | 0 | #define ZEND_MM_SRUN_FREE_COUNTER_MASK 0x01ff0000 |
174 | 0 | #define ZEND_MM_SRUN_FREE_COUNTER_OFFSET 16 |
175 | | |
176 | 0 | #define ZEND_MM_NRUN_OFFSET_MASK 0x01ff0000 |
177 | 0 | #define ZEND_MM_NRUN_OFFSET_OFFSET 16 |
178 | | |
179 | 0 | #define ZEND_MM_LRUN_PAGES(info) (((info) & ZEND_MM_LRUN_PAGES_MASK) >> ZEND_MM_LRUN_PAGES_OFFSET) |
180 | 0 | #define ZEND_MM_SRUN_BIN_NUM(info) (((info) & ZEND_MM_SRUN_BIN_NUM_MASK) >> ZEND_MM_SRUN_BIN_NUM_OFFSET) |
181 | 0 | #define ZEND_MM_SRUN_FREE_COUNTER(info) (((info) & ZEND_MM_SRUN_FREE_COUNTER_MASK) >> ZEND_MM_SRUN_FREE_COUNTER_OFFSET) |
182 | 0 | #define ZEND_MM_NRUN_OFFSET(info) (((info) & ZEND_MM_NRUN_OFFSET_MASK) >> ZEND_MM_NRUN_OFFSET_OFFSET) |
183 | | |
184 | | #define ZEND_MM_FRUN() ZEND_MM_IS_FRUN |
185 | 0 | #define ZEND_MM_LRUN(count) (ZEND_MM_IS_LRUN | ((count) << ZEND_MM_LRUN_PAGES_OFFSET)) |
186 | 0 | #define ZEND_MM_SRUN(bin_num) (ZEND_MM_IS_SRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET)) |
187 | 0 | #define ZEND_MM_SRUN_EX(bin_num, count) (ZEND_MM_IS_SRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET) | ((count) << ZEND_MM_SRUN_FREE_COUNTER_OFFSET)) |
188 | 0 | #define ZEND_MM_NRUN(bin_num, offset) (ZEND_MM_IS_SRUN | ZEND_MM_IS_LRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET) | ((offset) << ZEND_MM_NRUN_OFFSET_OFFSET)) |
189 | | |
190 | 0 | #define ZEND_MM_BINS 30 |
191 | | |
192 | | typedef struct _zend_mm_page zend_mm_page; |
193 | | typedef struct _zend_mm_bin zend_mm_bin; |
194 | | typedef struct _zend_mm_free_slot zend_mm_free_slot; |
195 | | typedef struct _zend_mm_chunk zend_mm_chunk; |
196 | | typedef struct _zend_mm_huge_list zend_mm_huge_list; |
197 | | |
198 | | int zend_mm_use_huge_pages = 0; |
199 | | |
200 | | /* |
201 | | * Memory is retrieved from OS by chunks of fixed size 2MB. |
202 | | * Inside chunk it's managed by pages of fixed size 4096B. |
203 | | * So each chunk consists from 512 pages. |
204 | | * The first page of each chunk is reserved for chunk header. |
205 | | * It contains service information about all pages. |
206 | | * |
207 | | * free_pages - current number of free pages in this chunk |
208 | | * |
209 | | * free_tail - number of continuous free pages at the end of chunk |
210 | | * |
211 | | * free_map - bitset (a bit for each page). The bit is set if the corresponding |
212 | | * page is allocated. Allocator for "lage sizes" may easily find a |
213 | | * free page (or a continuous number of pages) searching for zero |
214 | | * bits. |
215 | | * |
216 | | * map - contains service information for each page. (32-bits for each |
217 | | * page). |
218 | | * usage: |
219 | | * (2 bits) |
220 | | * FRUN - free page, |
221 | | * LRUN - first page of "large" allocation |
222 | | * SRUN - first page of a bin used for "small" allocation |
223 | | * |
224 | | * lrun_pages: |
225 | | * (10 bits) number of allocated pages |
226 | | * |
227 | | * srun_bin_num: |
228 | | * (5 bits) bin number (e.g. 0 for sizes 0-2, 1 for 3-4, |
229 | | * 2 for 5-8, 3 for 9-16 etc) see zend_alloc_sizes.h |
230 | | */ |
231 | | |
232 | | struct _zend_mm_heap { |
233 | | #if ZEND_MM_CUSTOM |
234 | | int use_custom_heap; |
235 | | #endif |
236 | | #if ZEND_MM_STORAGE |
237 | | zend_mm_storage *storage; |
238 | | #endif |
239 | | #if ZEND_MM_STAT |
240 | | size_t size; /* current memory usage */ |
241 | | size_t peak; /* peak memory usage */ |
242 | | #endif |
243 | | zend_mm_free_slot *free_slot[ZEND_MM_BINS]; /* free lists for small sizes */ |
244 | | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
245 | | size_t real_size; /* current size of allocated pages */ |
246 | | #endif |
247 | | #if ZEND_MM_STAT |
248 | | size_t real_peak; /* peak size of allocated pages */ |
249 | | #endif |
250 | | #if ZEND_MM_LIMIT |
251 | | size_t limit; /* memory limit */ |
252 | | int overflow; /* memory overflow flag */ |
253 | | #endif |
254 | | |
255 | | zend_mm_huge_list *huge_list; /* list of huge allocated blocks */ |
256 | | |
257 | | zend_mm_chunk *main_chunk; |
258 | | zend_mm_chunk *cached_chunks; /* list of unused chunks */ |
259 | | int chunks_count; /* number of allocated chunks */ |
260 | | int peak_chunks_count; /* peak number of allocated chunks for current request */ |
261 | | int cached_chunks_count; /* number of cached chunks */ |
262 | | double avg_chunks_count; /* average number of chunks allocated per request */ |
263 | | int last_chunks_delete_boundary; /* number of chunks after last deletion */ |
264 | | int last_chunks_delete_count; /* number of deletion over the last boundary */ |
265 | | #if ZEND_MM_CUSTOM |
266 | | union { |
267 | | struct { |
268 | | void *(*_malloc)(size_t); |
269 | | void (*_free)(void*); |
270 | | void *(*_realloc)(void*, size_t); |
271 | | } std; |
272 | | struct { |
273 | | void *(*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
274 | | void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
275 | | void *(*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
276 | | } debug; |
277 | | } custom_heap; |
278 | | HashTable *tracked_allocs; |
279 | | #endif |
280 | | }; |
281 | | |
282 | | struct _zend_mm_chunk { |
283 | | zend_mm_heap *heap; |
284 | | zend_mm_chunk *next; |
285 | | zend_mm_chunk *prev; |
286 | | uint32_t free_pages; /* number of free pages */ |
287 | | uint32_t free_tail; /* number of free pages at the end of chunk */ |
288 | | uint32_t num; |
289 | | char reserve[64 - (sizeof(void*) * 3 + sizeof(uint32_t) * 3)]; |
290 | | zend_mm_heap heap_slot; /* used only in main chunk */ |
291 | | zend_mm_page_map free_map; /* 512 bits or 64 bytes */ |
292 | | zend_mm_page_info map[ZEND_MM_PAGES]; /* 2 KB = 512 * 4 */ |
293 | | }; |
294 | | |
295 | | struct _zend_mm_page { |
296 | | char bytes[ZEND_MM_PAGE_SIZE]; |
297 | | }; |
298 | | |
299 | | /* |
300 | | * bin - is one or few continuous pages (up to 8) used for allocation of |
301 | | * a particular "small size". |
302 | | */ |
303 | | struct _zend_mm_bin { |
304 | | char bytes[ZEND_MM_PAGE_SIZE * 8]; |
305 | | }; |
306 | | |
307 | | struct _zend_mm_free_slot { |
308 | | zend_mm_free_slot *next_free_slot; |
309 | | }; |
310 | | |
311 | | struct _zend_mm_huge_list { |
312 | | void *ptr; |
313 | | size_t size; |
314 | | zend_mm_huge_list *next; |
315 | | #if ZEND_DEBUG |
316 | | zend_mm_debug_info dbg; |
317 | | #endif |
318 | | }; |
319 | | |
320 | | #define ZEND_MM_PAGE_ADDR(chunk, page_num) \ |
321 | 0 | ((void*)(((zend_mm_page*)(chunk)) + (page_num))) |
322 | | |
323 | | #define _BIN_DATA_SIZE(num, size, elements, pages, x, y) size, |
324 | | static const uint32_t bin_data_size[] = { |
325 | | ZEND_MM_BINS_INFO(_BIN_DATA_SIZE, x, y) |
326 | | }; |
327 | | |
328 | | #define _BIN_DATA_ELEMENTS(num, size, elements, pages, x, y) elements, |
329 | | static const uint32_t bin_elements[] = { |
330 | | ZEND_MM_BINS_INFO(_BIN_DATA_ELEMENTS, x, y) |
331 | | }; |
332 | | |
333 | | #define _BIN_DATA_PAGES(num, size, elements, pages, x, y) pages, |
334 | | static const uint32_t bin_pages[] = { |
335 | | ZEND_MM_BINS_INFO(_BIN_DATA_PAGES, x, y) |
336 | | }; |
337 | | |
338 | | #if ZEND_DEBUG |
339 | | ZEND_COLD void zend_debug_alloc_output(char *format, ...) |
340 | 0 | { |
341 | 0 | char output_buf[256]; |
342 | 0 | va_list args; |
343 | |
|
344 | 0 | va_start(args, format); |
345 | 0 | vsprintf(output_buf, format, args); |
346 | 0 | va_end(args); |
347 | |
|
348 | | #ifdef ZEND_WIN32 |
349 | | OutputDebugString(output_buf); |
350 | | #else |
351 | 0 | fprintf(stderr, "%s", output_buf); |
352 | 0 | #endif |
353 | 0 | } |
354 | | #endif |
355 | | |
356 | | static ZEND_COLD ZEND_NORETURN void zend_mm_panic(const char *message) |
357 | 0 | { |
358 | 0 | fprintf(stderr, "%s\n", message); |
359 | | /* See http://support.microsoft.com/kb/190351 */ |
360 | | #ifdef ZEND_WIN32 |
361 | | fflush(stderr); |
362 | | #endif |
363 | 0 | #if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID) |
364 | 0 | kill(getpid(), SIGSEGV); |
365 | 0 | #endif |
366 | 0 | exit(1); |
367 | 0 | } |
368 | | |
369 | | static ZEND_COLD ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap, |
370 | | const char *format, |
371 | | size_t limit, |
372 | | #if ZEND_DEBUG |
373 | | const char *filename, |
374 | | uint32_t lineno, |
375 | | #endif |
376 | | size_t size) |
377 | 27 | { |
378 | | |
379 | 27 | heap->overflow = 1; |
380 | 27 | zend_try { |
381 | 27 | zend_error_noreturn(E_ERROR, |
382 | 27 | format, |
383 | 27 | limit, |
384 | 27 | #if ZEND_DEBUG |
385 | 27 | filename, |
386 | 27 | lineno, |
387 | 27 | #endif |
388 | 27 | size); |
389 | 27 | } zend_catch { |
390 | 0 | } zend_end_try(); |
391 | 0 | heap->overflow = 0; |
392 | 0 | zend_bailout(); |
393 | 0 | exit(1); |
394 | 27 | } |
395 | | |
396 | | #ifdef _WIN32 |
397 | | void |
398 | | stderr_last_error(char *msg) |
399 | | { |
400 | | DWORD err = GetLastError(); |
401 | | char *buf = php_win32_error_to_msg(err); |
402 | | |
403 | | if (!buf[0]) { |
404 | | fprintf(stderr, "\n%s: [0x%08lx]\n", msg, err); |
405 | | } |
406 | | else { |
407 | | fprintf(stderr, "\n%s: [0x%08lx] %s\n", msg, err, buf); |
408 | | } |
409 | | |
410 | | php_win32_error_msg_free(buf); |
411 | | } |
412 | | #endif |
413 | | |
414 | | /*****************/ |
415 | | /* OS Allocation */ |
416 | | /*****************/ |
417 | | |
418 | | #ifndef HAVE_MREMAP |
419 | | static void *zend_mm_mmap_fixed(void *addr, size_t size) |
420 | | { |
421 | | #ifdef _WIN32 |
422 | | return VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
423 | | #else |
424 | | int flags = MAP_PRIVATE | MAP_ANON; |
425 | | #if defined(MAP_EXCL) |
426 | | flags |= MAP_FIXED | MAP_EXCL; |
427 | | #endif |
428 | | /* MAP_FIXED leads to discarding of the old mapping, so it can't be used. */ |
429 | | void *ptr = mmap(addr, size, PROT_READ | PROT_WRITE, flags /*| MAP_POPULATE | MAP_HUGETLB*/, ZEND_MM_FD, 0); |
430 | | |
431 | | if (ptr == MAP_FAILED) { |
432 | | #if ZEND_MM_ERROR && !defined(MAP_EXCL) |
433 | | fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno)); |
434 | | #endif |
435 | | return NULL; |
436 | | } else if (ptr != addr) { |
437 | | if (munmap(ptr, size) != 0) { |
438 | | #if ZEND_MM_ERROR |
439 | | fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno)); |
440 | | #endif |
441 | | } |
442 | | return NULL; |
443 | | } |
444 | | return ptr; |
445 | | #endif |
446 | | } |
447 | | #endif |
448 | | |
449 | | static void *zend_mm_mmap(size_t size) |
450 | 0 | { |
451 | | #ifdef _WIN32 |
452 | | void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
453 | | |
454 | | if (ptr == NULL) { |
455 | | #if ZEND_MM_ERROR |
456 | | stderr_last_error("VirtualAlloc() failed"); |
457 | | #endif |
458 | | return NULL; |
459 | | } |
460 | | return ptr; |
461 | | #else |
462 | 0 | void *ptr; |
463 | |
|
464 | 0 | #ifdef MAP_HUGETLB |
465 | 0 | if (zend_mm_use_huge_pages && size == ZEND_MM_CHUNK_SIZE) { |
466 | 0 | ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_HUGETLB, -1, 0); |
467 | 0 | if (ptr != MAP_FAILED) { |
468 | 0 | return ptr; |
469 | 0 | } |
470 | 0 | } |
471 | 0 | #endif |
472 | | |
473 | 0 | ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, ZEND_MM_FD, 0); |
474 | |
|
475 | 0 | if (ptr == MAP_FAILED) { |
476 | 0 | #if ZEND_MM_ERROR |
477 | 0 | fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno)); |
478 | 0 | #endif |
479 | 0 | return NULL; |
480 | 0 | } |
481 | 0 | return ptr; |
482 | 0 | #endif |
483 | 0 | } |
484 | | |
485 | | static void zend_mm_munmap(void *addr, size_t size) |
486 | 0 | { |
487 | | #ifdef _WIN32 |
488 | | if (VirtualFree(addr, 0, MEM_RELEASE) == 0) { |
489 | | #if ZEND_MM_ERROR |
490 | | stderr_last_error("VirtualFree() failed"); |
491 | | #endif |
492 | | } |
493 | | #else |
494 | 0 | if (munmap(addr, size) != 0) { |
495 | 0 | #if ZEND_MM_ERROR |
496 | 0 | fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno)); |
497 | 0 | #endif |
498 | 0 | } |
499 | 0 | #endif |
500 | 0 | } |
501 | | |
502 | | /***********/ |
503 | | /* Bitmask */ |
504 | | /***********/ |
505 | | |
506 | | /* number of trailing set (1) bits */ |
507 | | static zend_always_inline int zend_mm_bitset_nts(zend_mm_bitset bitset) |
508 | 0 | { |
509 | 0 | #if (defined(__GNUC__) || __has_builtin(__builtin_ctzl)) && SIZEOF_ZEND_LONG == SIZEOF_LONG && defined(PHP_HAVE_BUILTIN_CTZL) |
510 | 0 | return __builtin_ctzl(~bitset); |
511 | | #elif (defined(__GNUC__) || __has_builtin(__builtin_ctzll)) && defined(PHP_HAVE_BUILTIN_CTZLL) |
512 | | return __builtin_ctzll(~bitset); |
513 | | #elif defined(_WIN32) |
514 | | unsigned long index; |
515 | | |
516 | | #if defined(_WIN64) |
517 | | if (!BitScanForward64(&index, ~bitset)) { |
518 | | #else |
519 | | if (!BitScanForward(&index, ~bitset)) { |
520 | | #endif |
521 | | /* undefined behavior */ |
522 | | return 32; |
523 | | } |
524 | | |
525 | | return (int)index; |
526 | | #else |
527 | | int n; |
528 | | |
529 | | if (bitset == (zend_mm_bitset)-1) return ZEND_MM_BITSET_LEN; |
530 | | |
531 | | n = 0; |
532 | | #if SIZEOF_ZEND_LONG == 8 |
533 | | if (sizeof(zend_mm_bitset) == 8) { |
534 | | if ((bitset & 0xffffffff) == 0xffffffff) {n += 32; bitset = bitset >> Z_UL(32);} |
535 | | } |
536 | | #endif |
537 | | if ((bitset & 0x0000ffff) == 0x0000ffff) {n += 16; bitset = bitset >> 16;} |
538 | | if ((bitset & 0x000000ff) == 0x000000ff) {n += 8; bitset = bitset >> 8;} |
539 | | if ((bitset & 0x0000000f) == 0x0000000f) {n += 4; bitset = bitset >> 4;} |
540 | | if ((bitset & 0x00000003) == 0x00000003) {n += 2; bitset = bitset >> 2;} |
541 | | return n + (bitset & 1); |
542 | | #endif |
543 | 0 | } |
544 | | |
545 | | static zend_always_inline int zend_mm_bitset_is_set(zend_mm_bitset *bitset, int bit) |
546 | 0 | { |
547 | 0 | return ZEND_BIT_TEST(bitset, bit); |
548 | 0 | } |
549 | | |
550 | | static zend_always_inline void zend_mm_bitset_set_bit(zend_mm_bitset *bitset, int bit) |
551 | 0 | { |
552 | 0 | bitset[bit / ZEND_MM_BITSET_LEN] |= (Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1))); |
553 | 0 | } |
554 | | |
555 | | static zend_always_inline void zend_mm_bitset_reset_bit(zend_mm_bitset *bitset, int bit) |
556 | 0 | { |
557 | 0 | bitset[bit / ZEND_MM_BITSET_LEN] &= ~(Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1))); |
558 | 0 | } |
559 | | |
560 | | static zend_always_inline void zend_mm_bitset_set_range(zend_mm_bitset *bitset, int start, int len) |
561 | 0 | { |
562 | 0 | if (len == 1) { |
563 | 0 | zend_mm_bitset_set_bit(bitset, start); |
564 | 0 | } else { |
565 | 0 | int pos = start / ZEND_MM_BITSET_LEN; |
566 | 0 | int end = (start + len - 1) / ZEND_MM_BITSET_LEN; |
567 | 0 | int bit = start & (ZEND_MM_BITSET_LEN - 1); |
568 | 0 | zend_mm_bitset tmp; |
569 | |
|
570 | 0 | if (pos != end) { |
571 | | /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */ |
572 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
573 | 0 | bitset[pos++] |= tmp; |
574 | 0 | while (pos != end) { |
575 | | /* set all bits */ |
576 | 0 | bitset[pos++] = (zend_mm_bitset)-1; |
577 | 0 | } |
578 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
579 | | /* set bits from "0" to "end" */ |
580 | 0 | tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
581 | 0 | bitset[pos] |= tmp; |
582 | 0 | } else { |
583 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
584 | | /* set bits from "bit" to "end" */ |
585 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
586 | 0 | tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
587 | 0 | bitset[pos] |= tmp; |
588 | 0 | } |
589 | 0 | } |
590 | 0 | } |
591 | | |
592 | | static zend_always_inline void zend_mm_bitset_reset_range(zend_mm_bitset *bitset, int start, int len) |
593 | 0 | { |
594 | 0 | if (len == 1) { |
595 | 0 | zend_mm_bitset_reset_bit(bitset, start); |
596 | 0 | } else { |
597 | 0 | int pos = start / ZEND_MM_BITSET_LEN; |
598 | 0 | int end = (start + len - 1) / ZEND_MM_BITSET_LEN; |
599 | 0 | int bit = start & (ZEND_MM_BITSET_LEN - 1); |
600 | 0 | zend_mm_bitset tmp; |
601 | |
|
602 | 0 | if (pos != end) { |
603 | | /* reset bits from "bit" to ZEND_MM_BITSET_LEN-1 */ |
604 | 0 | tmp = ~((Z_UL(1) << bit) - 1); |
605 | 0 | bitset[pos++] &= ~tmp; |
606 | 0 | while (pos != end) { |
607 | | /* set all bits */ |
608 | 0 | bitset[pos++] = 0; |
609 | 0 | } |
610 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
611 | | /* reset bits from "0" to "end" */ |
612 | 0 | tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
613 | 0 | bitset[pos] &= ~tmp; |
614 | 0 | } else { |
615 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
616 | | /* reset bits from "bit" to "end" */ |
617 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
618 | 0 | tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
619 | 0 | bitset[pos] &= ~tmp; |
620 | 0 | } |
621 | 0 | } |
622 | 0 | } |
623 | | |
624 | | static zend_always_inline int zend_mm_bitset_is_free_range(zend_mm_bitset *bitset, int start, int len) |
625 | 0 | { |
626 | 0 | if (len == 1) { |
627 | 0 | return !zend_mm_bitset_is_set(bitset, start); |
628 | 0 | } else { |
629 | 0 | int pos = start / ZEND_MM_BITSET_LEN; |
630 | 0 | int end = (start + len - 1) / ZEND_MM_BITSET_LEN; |
631 | 0 | int bit = start & (ZEND_MM_BITSET_LEN - 1); |
632 | 0 | zend_mm_bitset tmp; |
633 | |
|
634 | 0 | if (pos != end) { |
635 | | /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */ |
636 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
637 | 0 | if ((bitset[pos++] & tmp) != 0) { |
638 | 0 | return 0; |
639 | 0 | } |
640 | 0 | while (pos != end) { |
641 | | /* set all bits */ |
642 | 0 | if (bitset[pos++] != 0) { |
643 | 0 | return 0; |
644 | 0 | } |
645 | 0 | } |
646 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
647 | | /* set bits from "0" to "end" */ |
648 | 0 | tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
649 | 0 | return (bitset[pos] & tmp) == 0; |
650 | 0 | } else { |
651 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
652 | | /* set bits from "bit" to "end" */ |
653 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
654 | 0 | tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
655 | 0 | return (bitset[pos] & tmp) == 0; |
656 | 0 | } |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | | /**********/ |
661 | | /* Chunks */ |
662 | | /**********/ |
663 | | |
664 | | static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment) |
665 | 0 | { |
666 | 0 | void *ptr = zend_mm_mmap(size); |
667 | |
|
668 | 0 | if (ptr == NULL) { |
669 | 0 | return NULL; |
670 | 0 | } else if (ZEND_MM_ALIGNED_OFFSET(ptr, alignment) == 0) { |
671 | 0 | #ifdef MADV_HUGEPAGE |
672 | 0 | if (zend_mm_use_huge_pages) { |
673 | 0 | madvise(ptr, size, MADV_HUGEPAGE); |
674 | 0 | } |
675 | 0 | #endif |
676 | 0 | return ptr; |
677 | 0 | } else { |
678 | 0 | size_t offset; |
679 | | |
680 | | /* chunk has to be aligned */ |
681 | 0 | zend_mm_munmap(ptr, size); |
682 | 0 | ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE); |
683 | | #ifdef _WIN32 |
684 | | offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment); |
685 | | zend_mm_munmap(ptr, size + alignment - REAL_PAGE_SIZE); |
686 | | ptr = zend_mm_mmap_fixed((void*)((char*)ptr + (alignment - offset)), size); |
687 | | offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment); |
688 | | if (offset != 0) { |
689 | | zend_mm_munmap(ptr, size); |
690 | | return NULL; |
691 | | } |
692 | | return ptr; |
693 | | #else |
694 | 0 | offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment); |
695 | 0 | if (offset != 0) { |
696 | 0 | offset = alignment - offset; |
697 | 0 | zend_mm_munmap(ptr, offset); |
698 | 0 | ptr = (char*)ptr + offset; |
699 | 0 | alignment -= offset; |
700 | 0 | } |
701 | 0 | if (alignment > REAL_PAGE_SIZE) { |
702 | 0 | zend_mm_munmap((char*)ptr + size, alignment - REAL_PAGE_SIZE); |
703 | 0 | } |
704 | 0 | # ifdef MADV_HUGEPAGE |
705 | 0 | if (zend_mm_use_huge_pages) { |
706 | 0 | madvise(ptr, size, MADV_HUGEPAGE); |
707 | 0 | } |
708 | 0 | # endif |
709 | 0 | #endif |
710 | 0 | return ptr; |
711 | 0 | } |
712 | 0 | } |
713 | | |
714 | | static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignment) |
715 | 0 | { |
716 | 0 | #if ZEND_MM_STORAGE |
717 | 0 | if (UNEXPECTED(heap->storage)) { |
718 | 0 | void *ptr = heap->storage->handlers.chunk_alloc(heap->storage, size, alignment); |
719 | 0 | ZEND_ASSERT(((zend_uintptr_t)((char*)ptr + (alignment-1)) & (alignment-1)) == (zend_uintptr_t)ptr); |
720 | 0 | return ptr; |
721 | 0 | } |
722 | 0 | #endif |
723 | 0 | return zend_mm_chunk_alloc_int(size, alignment); |
724 | 0 | } |
725 | | |
726 | | static void zend_mm_chunk_free(zend_mm_heap *heap, void *addr, size_t size) |
727 | 0 | { |
728 | 0 | #if ZEND_MM_STORAGE |
729 | 0 | if (UNEXPECTED(heap->storage)) { |
730 | 0 | heap->storage->handlers.chunk_free(heap->storage, addr, size); |
731 | 0 | return; |
732 | 0 | } |
733 | 0 | #endif |
734 | 0 | zend_mm_munmap(addr, size); |
735 | 0 | } |
736 | | |
737 | | static int zend_mm_chunk_truncate(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size) |
738 | 0 | { |
739 | 0 | #if ZEND_MM_STORAGE |
740 | 0 | if (UNEXPECTED(heap->storage)) { |
741 | 0 | if (heap->storage->handlers.chunk_truncate) { |
742 | 0 | return heap->storage->handlers.chunk_truncate(heap->storage, addr, old_size, new_size); |
743 | 0 | } else { |
744 | 0 | return 0; |
745 | 0 | } |
746 | 0 | } |
747 | 0 | #endif |
748 | 0 | #ifndef _WIN32 |
749 | 0 | zend_mm_munmap((char*)addr + new_size, old_size - new_size); |
750 | 0 | return 1; |
751 | | #else |
752 | | return 0; |
753 | | #endif |
754 | 0 | } |
755 | | |
756 | | static int zend_mm_chunk_extend(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size) |
757 | 0 | { |
758 | 0 | #if ZEND_MM_STORAGE |
759 | 0 | if (UNEXPECTED(heap->storage)) { |
760 | 0 | if (heap->storage->handlers.chunk_extend) { |
761 | 0 | return heap->storage->handlers.chunk_extend(heap->storage, addr, old_size, new_size); |
762 | 0 | } else { |
763 | 0 | return 0; |
764 | 0 | } |
765 | 0 | } |
766 | 0 | #endif |
767 | 0 | #ifdef HAVE_MREMAP |
768 | | /* We don't use MREMAP_MAYMOVE due to alignment requirements. */ |
769 | 0 | void *ptr = mremap(addr, old_size, new_size, 0); |
770 | 0 | if (ptr == MAP_FAILED) { |
771 | 0 | return 0; |
772 | 0 | } |
773 | | /* Sanity check: The mapping shouldn't have moved. */ |
774 | 0 | ZEND_ASSERT(ptr == addr); |
775 | 0 | return 1; |
776 | | #elif !defined(_WIN32) |
777 | | return (zend_mm_mmap_fixed((char*)addr + old_size, new_size - old_size) != NULL); |
778 | | #else |
779 | | return 0; |
780 | | #endif |
781 | 0 | } |
782 | | |
783 | | static zend_always_inline void zend_mm_chunk_init(zend_mm_heap *heap, zend_mm_chunk *chunk) |
784 | 0 | { |
785 | 0 | chunk->heap = heap; |
786 | 0 | chunk->next = heap->main_chunk; |
787 | 0 | chunk->prev = heap->main_chunk->prev; |
788 | 0 | chunk->prev->next = chunk; |
789 | 0 | chunk->next->prev = chunk; |
790 | | /* mark first pages as allocated */ |
791 | 0 | chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
792 | 0 | chunk->free_tail = ZEND_MM_FIRST_PAGE; |
793 | | /* the younger chunks have bigger number */ |
794 | 0 | chunk->num = chunk->prev->num + 1; |
795 | | /* mark first pages as allocated */ |
796 | 0 | chunk->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1; |
797 | 0 | chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
798 | 0 | } |
799 | | |
800 | | /***********************/ |
801 | | /* Huge Runs (forward) */ |
802 | | /***********************/ |
803 | | |
804 | | static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
805 | | static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
806 | | static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
807 | | |
808 | | #if ZEND_DEBUG |
809 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size, size_t dbg_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
810 | | #else |
811 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
812 | | #endif |
813 | | |
814 | | /**************/ |
815 | | /* Large Runs */ |
816 | | /**************/ |
817 | | |
818 | | #if ZEND_DEBUG |
819 | | static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
820 | | #else |
821 | | static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
822 | | #endif |
823 | 0 | { |
824 | 0 | zend_mm_chunk *chunk = heap->main_chunk; |
825 | 0 | uint32_t page_num, len; |
826 | 0 | int steps = 0; |
827 | |
|
828 | 0 | while (1) { |
829 | 0 | if (UNEXPECTED(chunk->free_pages < pages_count)) { |
830 | 0 | goto not_found; |
831 | | #if 0 |
832 | | } else if (UNEXPECTED(chunk->free_pages + chunk->free_tail == ZEND_MM_PAGES)) { |
833 | | if (UNEXPECTED(ZEND_MM_PAGES - chunk->free_tail < pages_count)) { |
834 | | goto not_found; |
835 | | } else { |
836 | | page_num = chunk->free_tail; |
837 | | goto found; |
838 | | } |
839 | | } else if (0) { |
840 | | /* First-Fit Search */ |
841 | | int free_tail = chunk->free_tail; |
842 | | zend_mm_bitset *bitset = chunk->free_map; |
843 | | zend_mm_bitset tmp = *(bitset++); |
844 | | int i = 0; |
845 | | |
846 | | while (1) { |
847 | | /* skip allocated blocks */ |
848 | | while (tmp == (zend_mm_bitset)-1) { |
849 | | i += ZEND_MM_BITSET_LEN; |
850 | | if (i == ZEND_MM_PAGES) { |
851 | | goto not_found; |
852 | | } |
853 | | tmp = *(bitset++); |
854 | | } |
855 | | /* find first 0 bit */ |
856 | | page_num = i + zend_mm_bitset_nts(tmp); |
857 | | /* reset bits from 0 to "bit" */ |
858 | | tmp &= tmp + 1; |
859 | | /* skip free blocks */ |
860 | | while (tmp == 0) { |
861 | | i += ZEND_MM_BITSET_LEN; |
862 | | len = i - page_num; |
863 | | if (len >= pages_count) { |
864 | | goto found; |
865 | | } else if (i >= free_tail) { |
866 | | goto not_found; |
867 | | } |
868 | | tmp = *(bitset++); |
869 | | } |
870 | | /* find first 1 bit */ |
871 | | len = (i + zend_ulong_ntz(tmp)) - page_num; |
872 | | if (len >= pages_count) { |
873 | | goto found; |
874 | | } |
875 | | /* set bits from 0 to "bit" */ |
876 | | tmp |= tmp - 1; |
877 | | } |
878 | | #endif |
879 | 0 | } else { |
880 | | /* Best-Fit Search */ |
881 | 0 | int best = -1; |
882 | 0 | uint32_t best_len = ZEND_MM_PAGES; |
883 | 0 | uint32_t free_tail = chunk->free_tail; |
884 | 0 | zend_mm_bitset *bitset = chunk->free_map; |
885 | 0 | zend_mm_bitset tmp = *(bitset++); |
886 | 0 | uint32_t i = 0; |
887 | |
|
888 | 0 | while (1) { |
889 | | /* skip allocated blocks */ |
890 | 0 | while (tmp == (zend_mm_bitset)-1) { |
891 | 0 | i += ZEND_MM_BITSET_LEN; |
892 | 0 | if (i == ZEND_MM_PAGES) { |
893 | 0 | if (best > 0) { |
894 | 0 | page_num = best; |
895 | 0 | goto found; |
896 | 0 | } else { |
897 | 0 | goto not_found; |
898 | 0 | } |
899 | 0 | } |
900 | 0 | tmp = *(bitset++); |
901 | 0 | } |
902 | | /* find first 0 bit */ |
903 | 0 | page_num = i + zend_mm_bitset_nts(tmp); |
904 | | /* reset bits from 0 to "bit" */ |
905 | 0 | tmp &= tmp + 1; |
906 | | /* skip free blocks */ |
907 | 0 | while (tmp == 0) { |
908 | 0 | i += ZEND_MM_BITSET_LEN; |
909 | 0 | if (i >= free_tail || i == ZEND_MM_PAGES) { |
910 | 0 | len = ZEND_MM_PAGES - page_num; |
911 | 0 | if (len >= pages_count && len < best_len) { |
912 | 0 | chunk->free_tail = page_num + pages_count; |
913 | 0 | goto found; |
914 | 0 | } else { |
915 | | /* set accurate value */ |
916 | 0 | chunk->free_tail = page_num; |
917 | 0 | if (best > 0) { |
918 | 0 | page_num = best; |
919 | 0 | goto found; |
920 | 0 | } else { |
921 | 0 | goto not_found; |
922 | 0 | } |
923 | 0 | } |
924 | 0 | } |
925 | 0 | tmp = *(bitset++); |
926 | 0 | } |
927 | | /* find first 1 bit */ |
928 | 0 | len = i + zend_ulong_ntz(tmp) - page_num; |
929 | 0 | if (len >= pages_count) { |
930 | 0 | if (len == pages_count) { |
931 | 0 | goto found; |
932 | 0 | } else if (len < best_len) { |
933 | 0 | best_len = len; |
934 | 0 | best = page_num; |
935 | 0 | } |
936 | 0 | } |
937 | | /* set bits from 0 to "bit" */ |
938 | 0 | tmp |= tmp - 1; |
939 | 0 | } |
940 | 0 | } |
941 | |
|
942 | 0 | not_found: |
943 | 0 | if (chunk->next == heap->main_chunk) { |
944 | 0 | get_chunk: |
945 | 0 | if (heap->cached_chunks) { |
946 | 0 | heap->cached_chunks_count--; |
947 | 0 | chunk = heap->cached_chunks; |
948 | 0 | heap->cached_chunks = chunk->next; |
949 | 0 | } else { |
950 | 0 | #if ZEND_MM_LIMIT |
951 | 0 | if (UNEXPECTED(ZEND_MM_CHUNK_SIZE > heap->limit - heap->real_size)) { |
952 | 0 | if (zend_mm_gc(heap)) { |
953 | 0 | goto get_chunk; |
954 | 0 | } else if (heap->overflow == 0) { |
955 | 0 | #if ZEND_DEBUG |
956 | 0 | zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", heap->limit, __zend_filename, __zend_lineno, size); |
957 | | #else |
958 | | zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, ZEND_MM_PAGE_SIZE * pages_count); |
959 | | #endif |
960 | 0 | return NULL; |
961 | 0 | } |
962 | 0 | } |
963 | 0 | #endif |
964 | 0 | chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE); |
965 | 0 | if (UNEXPECTED(chunk == NULL)) { |
966 | | /* insufficient memory */ |
967 | 0 | if (zend_mm_gc(heap) && |
968 | 0 | (chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE)) != NULL) { |
969 | | /* pass */ |
970 | 0 | } else { |
971 | | #if !ZEND_MM_LIMIT |
972 | | zend_mm_safe_error(heap, "Out of memory"); |
973 | | #elif ZEND_DEBUG |
974 | 0 | zend_mm_safe_error(heap, "Out of memory (allocated %zu) at %s:%d (tried to allocate %zu bytes)", heap->real_size, __zend_filename, __zend_lineno, size); |
975 | | #else |
976 | | zend_mm_safe_error(heap, "Out of memory (allocated %zu) (tried to allocate %zu bytes)", heap->real_size, ZEND_MM_PAGE_SIZE * pages_count); |
977 | | #endif |
978 | 0 | return NULL; |
979 | 0 | } |
980 | 0 | } |
981 | 0 | #if ZEND_MM_STAT |
982 | 0 | do { |
983 | 0 | size_t size = heap->real_size + ZEND_MM_CHUNK_SIZE; |
984 | 0 | size_t peak = MAX(heap->real_peak, size); |
985 | 0 | heap->real_size = size; |
986 | 0 | heap->real_peak = peak; |
987 | 0 | } while (0); |
988 | | #elif ZEND_MM_LIMIT |
989 | | heap->real_size += ZEND_MM_CHUNK_SIZE; |
990 | | |
991 | | #endif |
992 | 0 | } |
993 | 0 | heap->chunks_count++; |
994 | 0 | if (heap->chunks_count > heap->peak_chunks_count) { |
995 | 0 | heap->peak_chunks_count = heap->chunks_count; |
996 | 0 | } |
997 | 0 | zend_mm_chunk_init(heap, chunk); |
998 | 0 | page_num = ZEND_MM_FIRST_PAGE; |
999 | 0 | len = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
1000 | 0 | goto found; |
1001 | 0 | } else { |
1002 | 0 | chunk = chunk->next; |
1003 | 0 | steps++; |
1004 | 0 | } |
1005 | 0 | } |
1006 | |
|
1007 | 0 | found: |
1008 | 0 | if (steps > 2 && pages_count < 8) { |
1009 | | /* move chunk into the head of the linked-list */ |
1010 | 0 | chunk->prev->next = chunk->next; |
1011 | 0 | chunk->next->prev = chunk->prev; |
1012 | 0 | chunk->next = heap->main_chunk->next; |
1013 | 0 | chunk->prev = heap->main_chunk; |
1014 | 0 | chunk->prev->next = chunk; |
1015 | 0 | chunk->next->prev = chunk; |
1016 | 0 | } |
1017 | | /* mark run as allocated */ |
1018 | 0 | chunk->free_pages -= pages_count; |
1019 | 0 | zend_mm_bitset_set_range(chunk->free_map, page_num, pages_count); |
1020 | 0 | chunk->map[page_num] = ZEND_MM_LRUN(pages_count); |
1021 | 0 | if (page_num == chunk->free_tail) { |
1022 | 0 | chunk->free_tail = page_num + pages_count; |
1023 | 0 | } |
1024 | 0 | return ZEND_MM_PAGE_ADDR(chunk, page_num); |
1025 | 0 | } |
1026 | | |
1027 | | static zend_always_inline void *zend_mm_alloc_large_ex(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1028 | 0 | { |
1029 | 0 | int pages_count = (int)ZEND_MM_SIZE_TO_NUM(size, ZEND_MM_PAGE_SIZE); |
1030 | 0 | #if ZEND_DEBUG |
1031 | 0 | void *ptr = zend_mm_alloc_pages(heap, pages_count, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1032 | | #else |
1033 | | void *ptr = zend_mm_alloc_pages(heap, pages_count ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1034 | | #endif |
1035 | 0 | #if ZEND_MM_STAT |
1036 | 0 | do { |
1037 | 0 | size_t size = heap->size + pages_count * ZEND_MM_PAGE_SIZE; |
1038 | 0 | size_t peak = MAX(heap->peak, size); |
1039 | 0 | heap->size = size; |
1040 | 0 | heap->peak = peak; |
1041 | 0 | } while (0); |
1042 | 0 | #endif |
1043 | 0 | return ptr; |
1044 | 0 | } |
1045 | | |
1046 | | #if ZEND_DEBUG |
1047 | | static zend_never_inline void *zend_mm_alloc_large(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1048 | 0 | { |
1049 | 0 | return zend_mm_alloc_large_ex(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1050 | 0 | } |
1051 | | #else |
1052 | | static zend_never_inline void *zend_mm_alloc_large(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1053 | | { |
1054 | | return zend_mm_alloc_large_ex(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1055 | | } |
1056 | | #endif |
1057 | | |
1058 | | static zend_always_inline void zend_mm_delete_chunk(zend_mm_heap *heap, zend_mm_chunk *chunk) |
1059 | 0 | { |
1060 | 0 | chunk->next->prev = chunk->prev; |
1061 | 0 | chunk->prev->next = chunk->next; |
1062 | 0 | heap->chunks_count--; |
1063 | 0 | if (heap->chunks_count + heap->cached_chunks_count < heap->avg_chunks_count + 0.1 |
1064 | 0 | || (heap->chunks_count == heap->last_chunks_delete_boundary |
1065 | 0 | && heap->last_chunks_delete_count >= 4)) { |
1066 | | /* delay deletion */ |
1067 | 0 | heap->cached_chunks_count++; |
1068 | 0 | chunk->next = heap->cached_chunks; |
1069 | 0 | heap->cached_chunks = chunk; |
1070 | 0 | } else { |
1071 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1072 | 0 | heap->real_size -= ZEND_MM_CHUNK_SIZE; |
1073 | 0 | #endif |
1074 | 0 | if (!heap->cached_chunks) { |
1075 | 0 | if (heap->chunks_count != heap->last_chunks_delete_boundary) { |
1076 | 0 | heap->last_chunks_delete_boundary = heap->chunks_count; |
1077 | 0 | heap->last_chunks_delete_count = 0; |
1078 | 0 | } else { |
1079 | 0 | heap->last_chunks_delete_count++; |
1080 | 0 | } |
1081 | 0 | } |
1082 | 0 | if (!heap->cached_chunks || chunk->num > heap->cached_chunks->num) { |
1083 | 0 | zend_mm_chunk_free(heap, chunk, ZEND_MM_CHUNK_SIZE); |
1084 | 0 | } else { |
1085 | | //TODO: select the best chunk to delete??? |
1086 | 0 | chunk->next = heap->cached_chunks->next; |
1087 | 0 | zend_mm_chunk_free(heap, heap->cached_chunks, ZEND_MM_CHUNK_SIZE); |
1088 | 0 | heap->cached_chunks = chunk; |
1089 | 0 | } |
1090 | 0 | } |
1091 | 0 | } |
1092 | | |
1093 | | static zend_always_inline void zend_mm_free_pages_ex(zend_mm_heap *heap, zend_mm_chunk *chunk, uint32_t page_num, uint32_t pages_count, int free_chunk) |
1094 | 0 | { |
1095 | 0 | chunk->free_pages += pages_count; |
1096 | 0 | zend_mm_bitset_reset_range(chunk->free_map, page_num, pages_count); |
1097 | 0 | chunk->map[page_num] = 0; |
1098 | 0 | if (chunk->free_tail == page_num + pages_count) { |
1099 | | /* this setting may be not accurate */ |
1100 | 0 | chunk->free_tail = page_num; |
1101 | 0 | } |
1102 | 0 | if (free_chunk && chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE) { |
1103 | 0 | zend_mm_delete_chunk(heap, chunk); |
1104 | 0 | } |
1105 | 0 | } |
1106 | | |
1107 | | static zend_never_inline void zend_mm_free_pages(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count) |
1108 | 0 | { |
1109 | 0 | zend_mm_free_pages_ex(heap, chunk, page_num, pages_count, 1); |
1110 | 0 | } |
1111 | | |
1112 | | static zend_always_inline void zend_mm_free_large(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count) |
1113 | 0 | { |
1114 | 0 | #if ZEND_MM_STAT |
1115 | 0 | heap->size -= pages_count * ZEND_MM_PAGE_SIZE; |
1116 | 0 | #endif |
1117 | 0 | zend_mm_free_pages(heap, chunk, page_num, pages_count); |
1118 | 0 | } |
1119 | | |
1120 | | /**************/ |
1121 | | /* Small Runs */ |
1122 | | /**************/ |
1123 | | |
1124 | | /* higher set bit number (0->N/A, 1->1, 2->2, 4->3, 8->4, 127->7, 128->8 etc) */ |
1125 | | static zend_always_inline int zend_mm_small_size_to_bit(int size) |
1126 | 0 | { |
1127 | 0 | #if (defined(__GNUC__) || __has_builtin(__builtin_clz)) && defined(PHP_HAVE_BUILTIN_CLZ) |
1128 | 0 | return (__builtin_clz(size) ^ 0x1f) + 1; |
1129 | | #elif defined(_WIN32) |
1130 | | unsigned long index; |
1131 | | |
1132 | | if (!BitScanReverse(&index, (unsigned long)size)) { |
1133 | | /* undefined behavior */ |
1134 | | return 64; |
1135 | | } |
1136 | | |
1137 | | return (((31 - (int)index) ^ 0x1f) + 1); |
1138 | | #else |
1139 | | int n = 16; |
1140 | | if (size <= 0x00ff) {n -= 8; size = size << 8;} |
1141 | | if (size <= 0x0fff) {n -= 4; size = size << 4;} |
1142 | | if (size <= 0x3fff) {n -= 2; size = size << 2;} |
1143 | | if (size <= 0x7fff) {n -= 1;} |
1144 | | return n; |
1145 | | #endif |
1146 | 0 | } |
1147 | | |
1148 | | #ifndef MAX |
1149 | | # define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
1150 | | #endif |
1151 | | |
1152 | | #ifndef MIN |
1153 | | # define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
1154 | | #endif |
1155 | | |
1156 | | static zend_always_inline int zend_mm_small_size_to_bin(size_t size) |
1157 | 0 | { |
1158 | | #if 0 |
1159 | | int n; |
1160 | | /*0, 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12*/ |
1161 | | static const int f1[] = { 3, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9}; |
1162 | | static const int f2[] = { 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24}; |
1163 | | |
1164 | | if (UNEXPECTED(size <= 2)) return 0; |
1165 | | n = zend_mm_small_size_to_bit(size - 1); |
1166 | | return ((size-1) >> f1[n]) + f2[n]; |
1167 | | #else |
1168 | 0 | unsigned int t1, t2; |
1169 | |
|
1170 | 0 | if (size <= 64) { |
1171 | | /* we need to support size == 0 ... */ |
1172 | 0 | return (size - !!size) >> 3; |
1173 | 0 | } else { |
1174 | 0 | t1 = size - 1; |
1175 | 0 | t2 = zend_mm_small_size_to_bit(t1) - 3; |
1176 | 0 | t1 = t1 >> t2; |
1177 | 0 | t2 = t2 - 3; |
1178 | 0 | t2 = t2 << 2; |
1179 | 0 | return (int)(t1 + t2); |
1180 | 0 | } |
1181 | 0 | #endif |
1182 | 0 | } |
1183 | | |
1184 | 0 | #define ZEND_MM_SMALL_SIZE_TO_BIN(size) zend_mm_small_size_to_bin(size) |
1185 | | |
1186 | | static zend_never_inline void *zend_mm_alloc_small_slow(zend_mm_heap *heap, uint32_t bin_num ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1187 | 0 | { |
1188 | 0 | zend_mm_chunk *chunk; |
1189 | 0 | int page_num; |
1190 | 0 | zend_mm_bin *bin; |
1191 | 0 | zend_mm_free_slot *p, *end; |
1192 | |
|
1193 | 0 | #if ZEND_DEBUG |
1194 | 0 | bin = (zend_mm_bin*)zend_mm_alloc_pages(heap, bin_pages[bin_num], bin_data_size[bin_num] ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1195 | | #else |
1196 | | bin = (zend_mm_bin*)zend_mm_alloc_pages(heap, bin_pages[bin_num] ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1197 | | #endif |
1198 | 0 | if (UNEXPECTED(bin == NULL)) { |
1199 | | /* insufficient memory */ |
1200 | 0 | return NULL; |
1201 | 0 | } |
1202 | | |
1203 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(bin, ZEND_MM_CHUNK_SIZE); |
1204 | 0 | page_num = ZEND_MM_ALIGNED_OFFSET(bin, ZEND_MM_CHUNK_SIZE) / ZEND_MM_PAGE_SIZE; |
1205 | 0 | chunk->map[page_num] = ZEND_MM_SRUN(bin_num); |
1206 | 0 | if (bin_pages[bin_num] > 1) { |
1207 | 0 | uint32_t i = 1; |
1208 | |
|
1209 | 0 | do { |
1210 | 0 | chunk->map[page_num+i] = ZEND_MM_NRUN(bin_num, i); |
1211 | 0 | i++; |
1212 | 0 | } while (i < bin_pages[bin_num]); |
1213 | 0 | } |
1214 | | |
1215 | | /* create a linked list of elements from 1 to last */ |
1216 | 0 | end = (zend_mm_free_slot*)((char*)bin + (bin_data_size[bin_num] * (bin_elements[bin_num] - 1))); |
1217 | 0 | heap->free_slot[bin_num] = p = (zend_mm_free_slot*)((char*)bin + bin_data_size[bin_num]); |
1218 | 0 | do { |
1219 | 0 | p->next_free_slot = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]); |
1220 | 0 | #if ZEND_DEBUG |
1221 | 0 | do { |
1222 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1223 | 0 | dbg->size = 0; |
1224 | 0 | } while (0); |
1225 | 0 | #endif |
1226 | 0 | p = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]); |
1227 | 0 | } while (p != end); |
1228 | | |
1229 | | /* terminate list using NULL */ |
1230 | 0 | p->next_free_slot = NULL; |
1231 | 0 | #if ZEND_DEBUG |
1232 | 0 | do { |
1233 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1234 | 0 | dbg->size = 0; |
1235 | 0 | } while (0); |
1236 | 0 | #endif |
1237 | | |
1238 | | /* return first element */ |
1239 | 0 | return bin; |
1240 | 0 | } |
1241 | | |
1242 | | static zend_always_inline void *zend_mm_alloc_small(zend_mm_heap *heap, int bin_num ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1243 | 0 | { |
1244 | 0 | #if ZEND_MM_STAT |
1245 | 0 | do { |
1246 | 0 | size_t size = heap->size + bin_data_size[bin_num]; |
1247 | 0 | size_t peak = MAX(heap->peak, size); |
1248 | 0 | heap->size = size; |
1249 | 0 | heap->peak = peak; |
1250 | 0 | } while (0); |
1251 | 0 | #endif |
1252 | |
|
1253 | 0 | if (EXPECTED(heap->free_slot[bin_num] != NULL)) { |
1254 | 0 | zend_mm_free_slot *p = heap->free_slot[bin_num]; |
1255 | 0 | heap->free_slot[bin_num] = p->next_free_slot; |
1256 | 0 | return p; |
1257 | 0 | } else { |
1258 | 0 | return zend_mm_alloc_small_slow(heap, bin_num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1259 | 0 | } |
1260 | 0 | } |
1261 | | |
1262 | | static zend_always_inline void zend_mm_free_small(zend_mm_heap *heap, void *ptr, int bin_num) |
1263 | 0 | { |
1264 | 0 | zend_mm_free_slot *p; |
1265 | |
|
1266 | 0 | #if ZEND_MM_STAT |
1267 | 0 | heap->size -= bin_data_size[bin_num]; |
1268 | 0 | #endif |
1269 | |
|
1270 | 0 | #if ZEND_DEBUG |
1271 | 0 | do { |
1272 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)ptr + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1273 | 0 | dbg->size = 0; |
1274 | 0 | } while (0); |
1275 | 0 | #endif |
1276 | |
|
1277 | 0 | p = (zend_mm_free_slot*)ptr; |
1278 | 0 | p->next_free_slot = heap->free_slot[bin_num]; |
1279 | 0 | heap->free_slot[bin_num] = p; |
1280 | 0 | } |
1281 | | |
1282 | | /********/ |
1283 | | /* Heap */ |
1284 | | /********/ |
1285 | | |
1286 | | #if ZEND_DEBUG |
1287 | | static zend_always_inline zend_mm_debug_info *zend_mm_get_debug_info(zend_mm_heap *heap, void *ptr) |
1288 | 0 | { |
1289 | 0 | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1290 | 0 | zend_mm_chunk *chunk; |
1291 | 0 | int page_num; |
1292 | 0 | zend_mm_page_info info; |
1293 | |
|
1294 | 0 | ZEND_MM_CHECK(page_offset != 0, "zend_mm_heap corrupted"); |
1295 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1296 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1297 | 0 | info = chunk->map[page_num]; |
1298 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1299 | 0 | if (EXPECTED(info & ZEND_MM_IS_SRUN)) { |
1300 | 0 | int bin_num = ZEND_MM_SRUN_BIN_NUM(info); |
1301 | 0 | return (zend_mm_debug_info*)((char*)ptr + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1302 | 0 | } else /* if (info & ZEND_MM_IS_LRUN) */ { |
1303 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(info); |
1304 | |
|
1305 | 0 | return (zend_mm_debug_info*)((char*)ptr + ZEND_MM_PAGE_SIZE * pages_count - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1306 | 0 | } |
1307 | 0 | } |
1308 | | #endif |
1309 | | |
1310 | | static zend_always_inline void *zend_mm_alloc_heap(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1311 | 0 | { |
1312 | 0 | void *ptr; |
1313 | 0 | #if ZEND_DEBUG |
1314 | 0 | size_t real_size = size; |
1315 | 0 | zend_mm_debug_info *dbg; |
1316 | | |
1317 | | /* special handling for zero-size allocation */ |
1318 | 0 | size = MAX(size, 1); |
1319 | 0 | size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)); |
1320 | 0 | if (UNEXPECTED(size < real_size)) { |
1321 | 0 | zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu + %zu)", ZEND_MM_ALIGNED_SIZE(real_size), ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1322 | 0 | return NULL; |
1323 | 0 | } |
1324 | 0 | #endif |
1325 | 0 | if (EXPECTED(size <= ZEND_MM_MAX_SMALL_SIZE)) { |
1326 | 0 | ptr = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1327 | 0 | #if ZEND_DEBUG |
1328 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1329 | 0 | dbg->size = real_size; |
1330 | 0 | dbg->filename = __zend_filename; |
1331 | 0 | dbg->orig_filename = __zend_orig_filename; |
1332 | 0 | dbg->lineno = __zend_lineno; |
1333 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1334 | 0 | #endif |
1335 | 0 | return ptr; |
1336 | 0 | } else if (EXPECTED(size <= ZEND_MM_MAX_LARGE_SIZE)) { |
1337 | 0 | ptr = zend_mm_alloc_large(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1338 | 0 | #if ZEND_DEBUG |
1339 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1340 | 0 | dbg->size = real_size; |
1341 | 0 | dbg->filename = __zend_filename; |
1342 | 0 | dbg->orig_filename = __zend_orig_filename; |
1343 | 0 | dbg->lineno = __zend_lineno; |
1344 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1345 | 0 | #endif |
1346 | 0 | return ptr; |
1347 | 0 | } else { |
1348 | 0 | #if ZEND_DEBUG |
1349 | 0 | size = real_size; |
1350 | 0 | #endif |
1351 | 0 | return zend_mm_alloc_huge(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1352 | 0 | } |
1353 | 0 | } |
1354 | | |
1355 | | static zend_always_inline void zend_mm_free_heap(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1356 | 0 | { |
1357 | 0 | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1358 | |
|
1359 | 0 | if (UNEXPECTED(page_offset == 0)) { |
1360 | 0 | if (ptr != NULL) { |
1361 | 0 | zend_mm_free_huge(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1362 | 0 | } |
1363 | 0 | } else { |
1364 | 0 | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1365 | 0 | int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1366 | 0 | zend_mm_page_info info = chunk->map[page_num]; |
1367 | |
|
1368 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1369 | 0 | if (EXPECTED(info & ZEND_MM_IS_SRUN)) { |
1370 | 0 | zend_mm_free_small(heap, ptr, ZEND_MM_SRUN_BIN_NUM(info)); |
1371 | 0 | } else /* if (info & ZEND_MM_IS_LRUN) */ { |
1372 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(info); |
1373 | |
|
1374 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted"); |
1375 | 0 | zend_mm_free_large(heap, chunk, page_num, pages_count); |
1376 | 0 | } |
1377 | 0 | } |
1378 | 0 | } |
1379 | | |
1380 | | static size_t zend_mm_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1381 | 0 | { |
1382 | 0 | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1383 | |
|
1384 | 0 | if (UNEXPECTED(page_offset == 0)) { |
1385 | 0 | return zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1386 | 0 | } else { |
1387 | 0 | zend_mm_chunk *chunk; |
1388 | | #if 0 && ZEND_DEBUG |
1389 | | zend_mm_debug_info *dbg = zend_mm_get_debug_info(heap, ptr); |
1390 | | return dbg->size; |
1391 | | #else |
1392 | 0 | int page_num; |
1393 | 0 | zend_mm_page_info info; |
1394 | |
|
1395 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1396 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1397 | 0 | info = chunk->map[page_num]; |
1398 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1399 | 0 | if (EXPECTED(info & ZEND_MM_IS_SRUN)) { |
1400 | 0 | return bin_data_size[ZEND_MM_SRUN_BIN_NUM(info)]; |
1401 | 0 | } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ { |
1402 | 0 | return ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE; |
1403 | 0 | } |
1404 | 0 | #endif |
1405 | 0 | } |
1406 | 0 | } |
1407 | | |
1408 | | static zend_never_inline void *zend_mm_realloc_slow(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1409 | 0 | { |
1410 | 0 | void *ret; |
1411 | |
|
1412 | 0 | #if ZEND_MM_STAT |
1413 | 0 | do { |
1414 | 0 | size_t orig_peak = heap->peak; |
1415 | 0 | #endif |
1416 | 0 | ret = zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1417 | 0 | memcpy(ret, ptr, copy_size); |
1418 | 0 | zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1419 | 0 | #if ZEND_MM_STAT |
1420 | 0 | heap->peak = MAX(orig_peak, heap->size); |
1421 | 0 | } while (0); |
1422 | 0 | #endif |
1423 | 0 | return ret; |
1424 | 0 | } |
1425 | | |
1426 | | static zend_never_inline void *zend_mm_realloc_huge(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1427 | 0 | { |
1428 | 0 | size_t old_size; |
1429 | 0 | size_t new_size; |
1430 | 0 | #if ZEND_DEBUG |
1431 | 0 | size_t real_size; |
1432 | 0 | #endif |
1433 | |
|
1434 | 0 | old_size = zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1435 | 0 | #if ZEND_DEBUG |
1436 | 0 | real_size = size; |
1437 | 0 | size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)); |
1438 | 0 | #endif |
1439 | 0 | if (size > ZEND_MM_MAX_LARGE_SIZE) { |
1440 | 0 | #if ZEND_DEBUG |
1441 | 0 | size = real_size; |
1442 | 0 | #endif |
1443 | | #ifdef ZEND_WIN32 |
1444 | | /* On Windows we don't have ability to extend huge blocks in-place. |
1445 | | * We allocate them with 2MB size granularity, to avoid many |
1446 | | * reallocations when they are extended by small pieces |
1447 | | */ |
1448 | | new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE)); |
1449 | | #else |
1450 | 0 | new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE); |
1451 | 0 | #endif |
1452 | 0 | if (new_size == old_size) { |
1453 | 0 | #if ZEND_DEBUG |
1454 | 0 | zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1455 | | #else |
1456 | | zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1457 | | #endif |
1458 | 0 | return ptr; |
1459 | 0 | } else if (new_size < old_size) { |
1460 | | /* unmup tail */ |
1461 | 0 | if (zend_mm_chunk_truncate(heap, ptr, old_size, new_size)) { |
1462 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1463 | 0 | heap->real_size -= old_size - new_size; |
1464 | 0 | #endif |
1465 | 0 | #if ZEND_MM_STAT |
1466 | 0 | heap->size -= old_size - new_size; |
1467 | 0 | #endif |
1468 | 0 | #if ZEND_DEBUG |
1469 | 0 | zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1470 | | #else |
1471 | | zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1472 | | #endif |
1473 | 0 | return ptr; |
1474 | 0 | } |
1475 | 0 | } else /* if (new_size > old_size) */ { |
1476 | 0 | #if ZEND_MM_LIMIT |
1477 | 0 | if (UNEXPECTED(new_size - old_size > heap->limit - heap->real_size)) { |
1478 | 0 | if (zend_mm_gc(heap) && new_size - old_size <= heap->limit - heap->real_size) { |
1479 | | /* pass */ |
1480 | 0 | } else if (heap->overflow == 0) { |
1481 | 0 | #if ZEND_DEBUG |
1482 | 0 | zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", heap->limit, __zend_filename, __zend_lineno, size); |
1483 | | #else |
1484 | | zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, size); |
1485 | | #endif |
1486 | 0 | return NULL; |
1487 | 0 | } |
1488 | 0 | } |
1489 | 0 | #endif |
1490 | | /* try to map tail right after this block */ |
1491 | 0 | if (zend_mm_chunk_extend(heap, ptr, old_size, new_size)) { |
1492 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1493 | 0 | heap->real_size += new_size - old_size; |
1494 | 0 | #endif |
1495 | 0 | #if ZEND_MM_STAT |
1496 | 0 | heap->real_peak = MAX(heap->real_peak, heap->real_size); |
1497 | 0 | heap->size += new_size - old_size; |
1498 | 0 | heap->peak = MAX(heap->peak, heap->size); |
1499 | 0 | #endif |
1500 | 0 | #if ZEND_DEBUG |
1501 | 0 | zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1502 | | #else |
1503 | | zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1504 | | #endif |
1505 | 0 | return ptr; |
1506 | 0 | } |
1507 | 0 | } |
1508 | 0 | } |
1509 | | |
1510 | 0 | return zend_mm_realloc_slow(heap, ptr, size, MIN(old_size, copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1511 | 0 | } |
1512 | | |
1513 | | static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *ptr, size_t size, zend_bool use_copy_size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1514 | 0 | { |
1515 | 0 | size_t page_offset; |
1516 | 0 | size_t old_size; |
1517 | 0 | size_t new_size; |
1518 | 0 | void *ret; |
1519 | 0 | #if ZEND_DEBUG |
1520 | 0 | zend_mm_debug_info *dbg; |
1521 | 0 | #endif |
1522 | |
|
1523 | 0 | page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1524 | 0 | if (UNEXPECTED(page_offset == 0)) { |
1525 | 0 | if (EXPECTED(ptr == NULL)) { |
1526 | 0 | return _zend_mm_alloc(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1527 | 0 | } else { |
1528 | 0 | return zend_mm_realloc_huge(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1529 | 0 | } |
1530 | 0 | } else { |
1531 | 0 | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1532 | 0 | int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1533 | 0 | zend_mm_page_info info = chunk->map[page_num]; |
1534 | 0 | #if ZEND_DEBUG |
1535 | 0 | size_t real_size = size; |
1536 | |
|
1537 | 0 | size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)); |
1538 | 0 | #endif |
1539 | |
|
1540 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1541 | 0 | if (info & ZEND_MM_IS_SRUN) { |
1542 | 0 | int old_bin_num = ZEND_MM_SRUN_BIN_NUM(info); |
1543 | |
|
1544 | 0 | do { |
1545 | 0 | old_size = bin_data_size[old_bin_num]; |
1546 | | |
1547 | | /* Check if requested size fits into current bin */ |
1548 | 0 | if (size <= old_size) { |
1549 | | /* Check if truncation is necessary */ |
1550 | 0 | if (old_bin_num > 0 && size < bin_data_size[old_bin_num - 1]) { |
1551 | | /* truncation */ |
1552 | 0 | ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1553 | 0 | copy_size = use_copy_size ? MIN(size, copy_size) : size; |
1554 | 0 | memcpy(ret, ptr, copy_size); |
1555 | 0 | zend_mm_free_small(heap, ptr, old_bin_num); |
1556 | 0 | } else { |
1557 | | /* reallocation in-place */ |
1558 | 0 | ret = ptr; |
1559 | 0 | } |
1560 | 0 | } else if (size <= ZEND_MM_MAX_SMALL_SIZE) { |
1561 | | /* small extension */ |
1562 | |
|
1563 | 0 | #if ZEND_MM_STAT |
1564 | 0 | do { |
1565 | 0 | size_t orig_peak = heap->peak; |
1566 | 0 | #endif |
1567 | 0 | ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1568 | 0 | copy_size = use_copy_size ? MIN(old_size, copy_size) : old_size; |
1569 | 0 | memcpy(ret, ptr, copy_size); |
1570 | 0 | zend_mm_free_small(heap, ptr, old_bin_num); |
1571 | 0 | #if ZEND_MM_STAT |
1572 | 0 | heap->peak = MAX(orig_peak, heap->size); |
1573 | 0 | } while (0); |
1574 | 0 | #endif |
1575 | 0 | } else { |
1576 | | /* slow reallocation */ |
1577 | 0 | break; |
1578 | 0 | } |
1579 | | |
1580 | 0 | #if ZEND_DEBUG |
1581 | 0 | dbg = zend_mm_get_debug_info(heap, ret); |
1582 | 0 | dbg->size = real_size; |
1583 | 0 | dbg->filename = __zend_filename; |
1584 | 0 | dbg->orig_filename = __zend_orig_filename; |
1585 | 0 | dbg->lineno = __zend_lineno; |
1586 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1587 | 0 | #endif |
1588 | 0 | return ret; |
1589 | 0 | } while (0); |
1590 | |
|
1591 | 0 | } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ { |
1592 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted"); |
1593 | 0 | old_size = ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE; |
1594 | 0 | if (size > ZEND_MM_MAX_SMALL_SIZE && size <= ZEND_MM_MAX_LARGE_SIZE) { |
1595 | 0 | new_size = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE); |
1596 | 0 | if (new_size == old_size) { |
1597 | 0 | #if ZEND_DEBUG |
1598 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1599 | 0 | dbg->size = real_size; |
1600 | 0 | dbg->filename = __zend_filename; |
1601 | 0 | dbg->orig_filename = __zend_orig_filename; |
1602 | 0 | dbg->lineno = __zend_lineno; |
1603 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1604 | 0 | #endif |
1605 | 0 | return ptr; |
1606 | 0 | } else if (new_size < old_size) { |
1607 | | /* free tail pages */ |
1608 | 0 | int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE); |
1609 | 0 | int rest_pages_count = (int)((old_size - new_size) / ZEND_MM_PAGE_SIZE); |
1610 | |
|
1611 | 0 | #if ZEND_MM_STAT |
1612 | 0 | heap->size -= rest_pages_count * ZEND_MM_PAGE_SIZE; |
1613 | 0 | #endif |
1614 | 0 | chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count); |
1615 | 0 | chunk->free_pages += rest_pages_count; |
1616 | 0 | zend_mm_bitset_reset_range(chunk->free_map, page_num + new_pages_count, rest_pages_count); |
1617 | 0 | #if ZEND_DEBUG |
1618 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1619 | 0 | dbg->size = real_size; |
1620 | 0 | dbg->filename = __zend_filename; |
1621 | 0 | dbg->orig_filename = __zend_orig_filename; |
1622 | 0 | dbg->lineno = __zend_lineno; |
1623 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1624 | 0 | #endif |
1625 | 0 | return ptr; |
1626 | 0 | } else /* if (new_size > old_size) */ { |
1627 | 0 | int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE); |
1628 | 0 | int old_pages_count = (int)(old_size / ZEND_MM_PAGE_SIZE); |
1629 | | |
1630 | | /* try to allocate tail pages after this block */ |
1631 | 0 | if (page_num + new_pages_count <= ZEND_MM_PAGES && |
1632 | 0 | zend_mm_bitset_is_free_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count)) { |
1633 | 0 | #if ZEND_MM_STAT |
1634 | 0 | do { |
1635 | 0 | size_t size = heap->size + (new_size - old_size); |
1636 | 0 | size_t peak = MAX(heap->peak, size); |
1637 | 0 | heap->size = size; |
1638 | 0 | heap->peak = peak; |
1639 | 0 | } while (0); |
1640 | 0 | #endif |
1641 | 0 | chunk->free_pages -= new_pages_count - old_pages_count; |
1642 | 0 | zend_mm_bitset_set_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count); |
1643 | 0 | chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count); |
1644 | 0 | #if ZEND_DEBUG |
1645 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1646 | 0 | dbg->size = real_size; |
1647 | 0 | dbg->filename = __zend_filename; |
1648 | 0 | dbg->orig_filename = __zend_orig_filename; |
1649 | 0 | dbg->lineno = __zend_lineno; |
1650 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1651 | 0 | #endif |
1652 | 0 | return ptr; |
1653 | 0 | } |
1654 | 0 | } |
1655 | 0 | } |
1656 | 0 | } |
1657 | 0 | #if ZEND_DEBUG |
1658 | 0 | size = real_size; |
1659 | 0 | #endif |
1660 | 0 | } |
1661 | |
|
1662 | 0 | copy_size = MIN(old_size, copy_size); |
1663 | 0 | return zend_mm_realloc_slow(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1664 | 0 | } |
1665 | | |
1666 | | /*********************/ |
1667 | | /* Huge Runs (again) */ |
1668 | | /*********************/ |
1669 | | |
1670 | | #if ZEND_DEBUG |
1671 | | static void zend_mm_add_huge_block(zend_mm_heap *heap, void *ptr, size_t size, size_t dbg_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1672 | | #else |
1673 | | static void zend_mm_add_huge_block(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1674 | | #endif |
1675 | 0 | { |
1676 | 0 | zend_mm_huge_list *list = (zend_mm_huge_list*)zend_mm_alloc_heap(heap, sizeof(zend_mm_huge_list) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1677 | 0 | list->ptr = ptr; |
1678 | 0 | list->size = size; |
1679 | 0 | list->next = heap->huge_list; |
1680 | 0 | #if ZEND_DEBUG |
1681 | 0 | list->dbg.size = dbg_size; |
1682 | 0 | list->dbg.filename = __zend_filename; |
1683 | 0 | list->dbg.orig_filename = __zend_orig_filename; |
1684 | 0 | list->dbg.lineno = __zend_lineno; |
1685 | 0 | list->dbg.orig_lineno = __zend_orig_lineno; |
1686 | 0 | #endif |
1687 | 0 | heap->huge_list = list; |
1688 | 0 | } |
1689 | | |
1690 | | static size_t zend_mm_del_huge_block(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1691 | 0 | { |
1692 | 0 | zend_mm_huge_list *prev = NULL; |
1693 | 0 | zend_mm_huge_list *list = heap->huge_list; |
1694 | 0 | while (list != NULL) { |
1695 | 0 | if (list->ptr == ptr) { |
1696 | 0 | size_t size; |
1697 | |
|
1698 | 0 | if (prev) { |
1699 | 0 | prev->next = list->next; |
1700 | 0 | } else { |
1701 | 0 | heap->huge_list = list->next; |
1702 | 0 | } |
1703 | 0 | size = list->size; |
1704 | 0 | zend_mm_free_heap(heap, list ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1705 | 0 | return size; |
1706 | 0 | } |
1707 | 0 | prev = list; |
1708 | 0 | list = list->next; |
1709 | 0 | } |
1710 | 0 | ZEND_MM_CHECK(0, "zend_mm_heap corrupted"); |
1711 | 0 | return 0; |
1712 | 0 | } |
1713 | | |
1714 | | static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1715 | 0 | { |
1716 | 0 | zend_mm_huge_list *list = heap->huge_list; |
1717 | 0 | while (list != NULL) { |
1718 | 0 | if (list->ptr == ptr) { |
1719 | 0 | return list->size; |
1720 | 0 | } |
1721 | 0 | list = list->next; |
1722 | 0 | } |
1723 | 0 | ZEND_MM_CHECK(0, "zend_mm_heap corrupted"); |
1724 | 0 | return 0; |
1725 | 0 | } |
1726 | | |
1727 | | #if ZEND_DEBUG |
1728 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size, size_t dbg_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1729 | | #else |
1730 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1731 | | #endif |
1732 | 0 | { |
1733 | 0 | zend_mm_huge_list *list = heap->huge_list; |
1734 | 0 | while (list != NULL) { |
1735 | 0 | if (list->ptr == ptr) { |
1736 | 0 | list->size = size; |
1737 | 0 | #if ZEND_DEBUG |
1738 | 0 | list->dbg.size = dbg_size; |
1739 | 0 | list->dbg.filename = __zend_filename; |
1740 | 0 | list->dbg.orig_filename = __zend_orig_filename; |
1741 | 0 | list->dbg.lineno = __zend_lineno; |
1742 | 0 | list->dbg.orig_lineno = __zend_orig_lineno; |
1743 | 0 | #endif |
1744 | 0 | return; |
1745 | 0 | } |
1746 | 0 | list = list->next; |
1747 | 0 | } |
1748 | 0 | } |
1749 | | |
1750 | | static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1751 | 0 | { |
1752 | | #ifdef ZEND_WIN32 |
1753 | | /* On Windows we don't have ability to extend huge blocks in-place. |
1754 | | * We allocate them with 2MB size granularity, to avoid many |
1755 | | * reallocations when they are extended by small pieces |
1756 | | */ |
1757 | | size_t alignment = MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE); |
1758 | | #else |
1759 | 0 | size_t alignment = REAL_PAGE_SIZE; |
1760 | 0 | #endif |
1761 | 0 | size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, alignment); |
1762 | 0 | void *ptr; |
1763 | |
|
1764 | 0 | if (UNEXPECTED(new_size < size)) { |
1765 | 0 | zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu + %zu)", size, alignment); |
1766 | 0 | } |
1767 | | |
1768 | 0 | #if ZEND_MM_LIMIT |
1769 | 0 | if (UNEXPECTED(new_size > heap->limit - heap->real_size)) { |
1770 | 0 | if (zend_mm_gc(heap) && new_size <= heap->limit - heap->real_size) { |
1771 | | /* pass */ |
1772 | 0 | } else if (heap->overflow == 0) { |
1773 | 0 | #if ZEND_DEBUG |
1774 | 0 | zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", heap->limit, __zend_filename, __zend_lineno, size); |
1775 | | #else |
1776 | | zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, size); |
1777 | | #endif |
1778 | 0 | return NULL; |
1779 | 0 | } |
1780 | 0 | } |
1781 | 0 | #endif |
1782 | 0 | ptr = zend_mm_chunk_alloc(heap, new_size, ZEND_MM_CHUNK_SIZE); |
1783 | 0 | if (UNEXPECTED(ptr == NULL)) { |
1784 | | /* insufficient memory */ |
1785 | 0 | if (zend_mm_gc(heap) && |
1786 | 0 | (ptr = zend_mm_chunk_alloc(heap, new_size, ZEND_MM_CHUNK_SIZE)) != NULL) { |
1787 | | /* pass */ |
1788 | 0 | } else { |
1789 | | #if !ZEND_MM_LIMIT |
1790 | | zend_mm_safe_error(heap, "Out of memory"); |
1791 | | #elif ZEND_DEBUG |
1792 | 0 | zend_mm_safe_error(heap, "Out of memory (allocated %zu) at %s:%d (tried to allocate %zu bytes)", heap->real_size, __zend_filename, __zend_lineno, size); |
1793 | | #else |
1794 | | zend_mm_safe_error(heap, "Out of memory (allocated %zu) (tried to allocate %zu bytes)", heap->real_size, size); |
1795 | | #endif |
1796 | 0 | return NULL; |
1797 | 0 | } |
1798 | 0 | } |
1799 | 0 | #if ZEND_DEBUG |
1800 | 0 | zend_mm_add_huge_block(heap, ptr, new_size, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1801 | | #else |
1802 | | zend_mm_add_huge_block(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1803 | | #endif |
1804 | 0 | #if ZEND_MM_STAT |
1805 | 0 | do { |
1806 | 0 | size_t size = heap->real_size + new_size; |
1807 | 0 | size_t peak = MAX(heap->real_peak, size); |
1808 | 0 | heap->real_size = size; |
1809 | 0 | heap->real_peak = peak; |
1810 | 0 | } while (0); |
1811 | 0 | do { |
1812 | 0 | size_t size = heap->size + new_size; |
1813 | 0 | size_t peak = MAX(heap->peak, size); |
1814 | 0 | heap->size = size; |
1815 | 0 | heap->peak = peak; |
1816 | 0 | } while (0); |
1817 | | #elif ZEND_MM_LIMIT |
1818 | | heap->real_size += new_size; |
1819 | | #endif |
1820 | 0 | return ptr; |
1821 | 0 | } |
1822 | | |
1823 | | static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1824 | 0 | { |
1825 | 0 | size_t size; |
1826 | |
|
1827 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE) == 0, "zend_mm_heap corrupted"); |
1828 | 0 | size = zend_mm_del_huge_block(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1829 | 0 | zend_mm_chunk_free(heap, ptr, size); |
1830 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1831 | 0 | heap->real_size -= size; |
1832 | 0 | #endif |
1833 | 0 | #if ZEND_MM_STAT |
1834 | 0 | heap->size -= size; |
1835 | 0 | #endif |
1836 | 0 | } |
1837 | | |
1838 | | /******************/ |
1839 | | /* Initialization */ |
1840 | | /******************/ |
1841 | | |
1842 | | static zend_mm_heap *zend_mm_init(void) |
1843 | 0 | { |
1844 | 0 | zend_mm_chunk *chunk = (zend_mm_chunk*)zend_mm_chunk_alloc_int(ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE); |
1845 | 0 | zend_mm_heap *heap; |
1846 | |
|
1847 | 0 | if (UNEXPECTED(chunk == NULL)) { |
1848 | 0 | #if ZEND_MM_ERROR |
1849 | | #ifdef _WIN32 |
1850 | | stderr_last_error("Can't initialize heap"); |
1851 | | #else |
1852 | 0 | fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno)); |
1853 | 0 | #endif |
1854 | 0 | #endif |
1855 | 0 | return NULL; |
1856 | 0 | } |
1857 | 0 | heap = &chunk->heap_slot; |
1858 | 0 | chunk->heap = heap; |
1859 | 0 | chunk->next = chunk; |
1860 | 0 | chunk->prev = chunk; |
1861 | 0 | chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
1862 | 0 | chunk->free_tail = ZEND_MM_FIRST_PAGE; |
1863 | 0 | chunk->num = 0; |
1864 | 0 | chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1; |
1865 | 0 | chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
1866 | 0 | heap->main_chunk = chunk; |
1867 | 0 | heap->cached_chunks = NULL; |
1868 | 0 | heap->chunks_count = 1; |
1869 | 0 | heap->peak_chunks_count = 1; |
1870 | 0 | heap->cached_chunks_count = 0; |
1871 | 0 | heap->avg_chunks_count = 1.0; |
1872 | 0 | heap->last_chunks_delete_boundary = 0; |
1873 | 0 | heap->last_chunks_delete_count = 0; |
1874 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1875 | 0 | heap->real_size = ZEND_MM_CHUNK_SIZE; |
1876 | 0 | #endif |
1877 | 0 | #if ZEND_MM_STAT |
1878 | 0 | heap->real_peak = ZEND_MM_CHUNK_SIZE; |
1879 | 0 | heap->size = 0; |
1880 | 0 | heap->peak = 0; |
1881 | 0 | #endif |
1882 | 0 | #if ZEND_MM_LIMIT |
1883 | 0 | heap->limit = ((size_t)Z_L(-1) >> (size_t)Z_L(1)); |
1884 | 0 | heap->overflow = 0; |
1885 | 0 | #endif |
1886 | 0 | #if ZEND_MM_CUSTOM |
1887 | 0 | heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE; |
1888 | 0 | #endif |
1889 | 0 | #if ZEND_MM_STORAGE |
1890 | 0 | heap->storage = NULL; |
1891 | 0 | #endif |
1892 | 0 | heap->huge_list = NULL; |
1893 | 0 | return heap; |
1894 | 0 | } |
1895 | | |
1896 | | ZEND_API size_t zend_mm_gc(zend_mm_heap *heap) |
1897 | 0 | { |
1898 | 0 | zend_mm_free_slot *p, **q; |
1899 | 0 | zend_mm_chunk *chunk; |
1900 | 0 | size_t page_offset; |
1901 | 0 | int page_num; |
1902 | 0 | zend_mm_page_info info; |
1903 | 0 | uint32_t i, free_counter; |
1904 | 0 | int has_free_pages; |
1905 | 0 | size_t collected = 0; |
1906 | |
|
1907 | 0 | #if ZEND_MM_CUSTOM |
1908 | 0 | if (heap->use_custom_heap) { |
1909 | 0 | return 0; |
1910 | 0 | } |
1911 | 0 | #endif |
1912 | | |
1913 | 0 | for (i = 0; i < ZEND_MM_BINS; i++) { |
1914 | 0 | has_free_pages = 0; |
1915 | 0 | p = heap->free_slot[i]; |
1916 | 0 | while (p != NULL) { |
1917 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(p, ZEND_MM_CHUNK_SIZE); |
1918 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1919 | 0 | page_offset = ZEND_MM_ALIGNED_OFFSET(p, ZEND_MM_CHUNK_SIZE); |
1920 | 0 | ZEND_ASSERT(page_offset != 0); |
1921 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1922 | 0 | info = chunk->map[page_num]; |
1923 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
1924 | 0 | if (info & ZEND_MM_IS_LRUN) { |
1925 | 0 | page_num -= ZEND_MM_NRUN_OFFSET(info); |
1926 | 0 | info = chunk->map[page_num]; |
1927 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
1928 | 0 | ZEND_ASSERT(!(info & ZEND_MM_IS_LRUN)); |
1929 | 0 | } |
1930 | 0 | ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(info) == i); |
1931 | 0 | free_counter = ZEND_MM_SRUN_FREE_COUNTER(info) + 1; |
1932 | 0 | if (free_counter == bin_elements[i]) { |
1933 | 0 | has_free_pages = 1; |
1934 | 0 | } |
1935 | 0 | chunk->map[page_num] = ZEND_MM_SRUN_EX(i, free_counter); |
1936 | 0 | p = p->next_free_slot; |
1937 | 0 | } |
1938 | |
|
1939 | 0 | if (!has_free_pages) { |
1940 | 0 | continue; |
1941 | 0 | } |
1942 | | |
1943 | 0 | q = &heap->free_slot[i]; |
1944 | 0 | p = *q; |
1945 | 0 | while (p != NULL) { |
1946 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(p, ZEND_MM_CHUNK_SIZE); |
1947 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1948 | 0 | page_offset = ZEND_MM_ALIGNED_OFFSET(p, ZEND_MM_CHUNK_SIZE); |
1949 | 0 | ZEND_ASSERT(page_offset != 0); |
1950 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1951 | 0 | info = chunk->map[page_num]; |
1952 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
1953 | 0 | if (info & ZEND_MM_IS_LRUN) { |
1954 | 0 | page_num -= ZEND_MM_NRUN_OFFSET(info); |
1955 | 0 | info = chunk->map[page_num]; |
1956 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
1957 | 0 | ZEND_ASSERT(!(info & ZEND_MM_IS_LRUN)); |
1958 | 0 | } |
1959 | 0 | ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(info) == i); |
1960 | 0 | if (ZEND_MM_SRUN_FREE_COUNTER(info) == bin_elements[i]) { |
1961 | | /* remove from cache */ |
1962 | 0 | p = p->next_free_slot; |
1963 | 0 | *q = p; |
1964 | 0 | } else { |
1965 | 0 | q = &p->next_free_slot; |
1966 | 0 | p = *q; |
1967 | 0 | } |
1968 | 0 | } |
1969 | 0 | } |
1970 | |
|
1971 | 0 | chunk = heap->main_chunk; |
1972 | 0 | do { |
1973 | 0 | i = ZEND_MM_FIRST_PAGE; |
1974 | 0 | while (i < chunk->free_tail) { |
1975 | 0 | if (zend_mm_bitset_is_set(chunk->free_map, i)) { |
1976 | 0 | info = chunk->map[i]; |
1977 | 0 | if (info & ZEND_MM_IS_SRUN) { |
1978 | 0 | int bin_num = ZEND_MM_SRUN_BIN_NUM(info); |
1979 | 0 | int pages_count = bin_pages[bin_num]; |
1980 | |
|
1981 | 0 | if (ZEND_MM_SRUN_FREE_COUNTER(info) == bin_elements[bin_num]) { |
1982 | | /* all elements are free */ |
1983 | 0 | zend_mm_free_pages_ex(heap, chunk, i, pages_count, 0); |
1984 | 0 | collected += pages_count; |
1985 | 0 | } else { |
1986 | | /* reset counter */ |
1987 | 0 | chunk->map[i] = ZEND_MM_SRUN(bin_num); |
1988 | 0 | } |
1989 | 0 | i += bin_pages[bin_num]; |
1990 | 0 | } else /* if (info & ZEND_MM_IS_LRUN) */ { |
1991 | 0 | i += ZEND_MM_LRUN_PAGES(info); |
1992 | 0 | } |
1993 | 0 | } else { |
1994 | 0 | i++; |
1995 | 0 | } |
1996 | 0 | } |
1997 | 0 | if (chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE) { |
1998 | 0 | zend_mm_chunk *next_chunk = chunk->next; |
1999 | |
|
2000 | 0 | zend_mm_delete_chunk(heap, chunk); |
2001 | 0 | chunk = next_chunk; |
2002 | 0 | } else { |
2003 | 0 | chunk = chunk->next; |
2004 | 0 | } |
2005 | 0 | } while (chunk != heap->main_chunk); |
2006 | |
|
2007 | 0 | return collected * ZEND_MM_PAGE_SIZE; |
2008 | 0 | } |
2009 | | |
2010 | | #if ZEND_DEBUG |
2011 | | /******************/ |
2012 | | /* Leak detection */ |
2013 | | /******************/ |
2014 | | |
2015 | | static zend_long zend_mm_find_leaks_small(zend_mm_chunk *p, uint32_t i, uint32_t j, zend_leak_info *leak) |
2016 | 0 | { |
2017 | 0 | int empty = 1; |
2018 | 0 | zend_long count = 0; |
2019 | 0 | int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]); |
2020 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] * (j + 1) - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2021 | |
|
2022 | 0 | while (j < bin_elements[bin_num]) { |
2023 | 0 | if (dbg->size != 0) { |
2024 | 0 | if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) { |
2025 | 0 | count++; |
2026 | 0 | dbg->size = 0; |
2027 | 0 | dbg->filename = NULL; |
2028 | 0 | dbg->lineno = 0; |
2029 | 0 | } else { |
2030 | 0 | empty = 0; |
2031 | 0 | } |
2032 | 0 | } |
2033 | 0 | j++; |
2034 | 0 | dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]); |
2035 | 0 | } |
2036 | 0 | if (empty) { |
2037 | 0 | zend_mm_bitset_reset_range(p->free_map, i, bin_pages[bin_num]); |
2038 | 0 | } |
2039 | 0 | return count; |
2040 | 0 | } |
2041 | | |
2042 | | static zend_long zend_mm_find_leaks(zend_mm_heap *heap, zend_mm_chunk *p, uint32_t i, zend_leak_info *leak) |
2043 | 0 | { |
2044 | 0 | zend_long count = 0; |
2045 | |
|
2046 | 0 | do { |
2047 | 0 | while (i < p->free_tail) { |
2048 | 0 | if (zend_mm_bitset_is_set(p->free_map, i)) { |
2049 | 0 | if (p->map[i] & ZEND_MM_IS_SRUN) { |
2050 | 0 | int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]); |
2051 | 0 | count += zend_mm_find_leaks_small(p, i, 0, leak); |
2052 | 0 | i += bin_pages[bin_num]; |
2053 | 0 | } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ { |
2054 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]); |
2055 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * (i + pages_count) - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2056 | |
|
2057 | 0 | if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) { |
2058 | 0 | count++; |
2059 | 0 | } |
2060 | 0 | zend_mm_bitset_reset_range(p->free_map, i, pages_count); |
2061 | 0 | i += pages_count; |
2062 | 0 | } |
2063 | 0 | } else { |
2064 | 0 | i++; |
2065 | 0 | } |
2066 | 0 | } |
2067 | 0 | p = p->next; |
2068 | 0 | i = ZEND_MM_FIRST_PAGE; |
2069 | 0 | } while (p != heap->main_chunk); |
2070 | 0 | return count; |
2071 | 0 | } |
2072 | | |
2073 | | static zend_long zend_mm_find_leaks_huge(zend_mm_heap *heap, zend_mm_huge_list *list) |
2074 | 0 | { |
2075 | 0 | zend_long count = 0; |
2076 | 0 | zend_mm_huge_list *prev = list; |
2077 | 0 | zend_mm_huge_list *p = list->next; |
2078 | |
|
2079 | 0 | while (p) { |
2080 | 0 | if (p->dbg.filename == list->dbg.filename && p->dbg.lineno == list->dbg.lineno) { |
2081 | 0 | prev->next = p->next; |
2082 | 0 | zend_mm_chunk_free(heap, p->ptr, p->size); |
2083 | 0 | zend_mm_free_heap(heap, p, NULL, 0, NULL, 0); |
2084 | 0 | count++; |
2085 | 0 | } else { |
2086 | 0 | prev = p; |
2087 | 0 | } |
2088 | 0 | p = prev->next; |
2089 | 0 | } |
2090 | |
|
2091 | 0 | return count; |
2092 | 0 | } |
2093 | | |
2094 | | static void zend_mm_check_leaks(zend_mm_heap *heap) |
2095 | 0 | { |
2096 | 0 | zend_mm_huge_list *list; |
2097 | 0 | zend_mm_chunk *p; |
2098 | 0 | zend_leak_info leak; |
2099 | 0 | zend_long repeated = 0; |
2100 | 0 | uint32_t total = 0; |
2101 | 0 | uint32_t i, j; |
2102 | | |
2103 | | /* find leaked huge blocks and free them */ |
2104 | 0 | list = heap->huge_list; |
2105 | 0 | while (list) { |
2106 | 0 | zend_mm_huge_list *q = list; |
2107 | |
|
2108 | 0 | leak.addr = list->ptr; |
2109 | 0 | leak.size = list->dbg.size; |
2110 | 0 | leak.filename = list->dbg.filename; |
2111 | 0 | leak.orig_filename = list->dbg.orig_filename; |
2112 | 0 | leak.lineno = list->dbg.lineno; |
2113 | 0 | leak.orig_lineno = list->dbg.orig_lineno; |
2114 | |
|
2115 | 0 | zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL); |
2116 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak); |
2117 | 0 | repeated = zend_mm_find_leaks_huge(heap, list); |
2118 | 0 | total += 1 + repeated; |
2119 | 0 | if (repeated) { |
2120 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated); |
2121 | 0 | } |
2122 | |
|
2123 | 0 | heap->huge_list = list = list->next; |
2124 | 0 | zend_mm_chunk_free(heap, q->ptr, q->size); |
2125 | 0 | zend_mm_free_heap(heap, q, NULL, 0, NULL, 0); |
2126 | 0 | } |
2127 | | |
2128 | | /* for each chunk */ |
2129 | 0 | p = heap->main_chunk; |
2130 | 0 | do { |
2131 | 0 | i = ZEND_MM_FIRST_PAGE; |
2132 | 0 | while (i < p->free_tail) { |
2133 | 0 | if (zend_mm_bitset_is_set(p->free_map, i)) { |
2134 | 0 | if (p->map[i] & ZEND_MM_IS_SRUN) { |
2135 | 0 | int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]); |
2136 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2137 | |
|
2138 | 0 | j = 0; |
2139 | 0 | while (j < bin_elements[bin_num]) { |
2140 | 0 | if (dbg->size != 0) { |
2141 | 0 | leak.addr = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] * j); |
2142 | 0 | leak.size = dbg->size; |
2143 | 0 | leak.filename = dbg->filename; |
2144 | 0 | leak.orig_filename = dbg->orig_filename; |
2145 | 0 | leak.lineno = dbg->lineno; |
2146 | 0 | leak.orig_lineno = dbg->orig_lineno; |
2147 | |
|
2148 | 0 | zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL); |
2149 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak); |
2150 | |
|
2151 | 0 | dbg->size = 0; |
2152 | 0 | dbg->filename = NULL; |
2153 | 0 | dbg->lineno = 0; |
2154 | |
|
2155 | 0 | repeated = zend_mm_find_leaks_small(p, i, j + 1, &leak) + |
2156 | 0 | zend_mm_find_leaks(heap, p, i + bin_pages[bin_num], &leak); |
2157 | 0 | total += 1 + repeated; |
2158 | 0 | if (repeated) { |
2159 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated); |
2160 | 0 | } |
2161 | 0 | } |
2162 | 0 | dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]); |
2163 | 0 | j++; |
2164 | 0 | } |
2165 | 0 | i += bin_pages[bin_num]; |
2166 | 0 | } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ { |
2167 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]); |
2168 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * (i + pages_count) - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2169 | |
|
2170 | 0 | leak.addr = (void*)((char*)p + ZEND_MM_PAGE_SIZE * i); |
2171 | 0 | leak.size = dbg->size; |
2172 | 0 | leak.filename = dbg->filename; |
2173 | 0 | leak.orig_filename = dbg->orig_filename; |
2174 | 0 | leak.lineno = dbg->lineno; |
2175 | 0 | leak.orig_lineno = dbg->orig_lineno; |
2176 | |
|
2177 | 0 | zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL); |
2178 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak); |
2179 | |
|
2180 | 0 | zend_mm_bitset_reset_range(p->free_map, i, pages_count); |
2181 | |
|
2182 | 0 | repeated = zend_mm_find_leaks(heap, p, i + pages_count, &leak); |
2183 | 0 | total += 1 + repeated; |
2184 | 0 | if (repeated) { |
2185 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated); |
2186 | 0 | } |
2187 | 0 | i += pages_count; |
2188 | 0 | } |
2189 | 0 | } else { |
2190 | 0 | i++; |
2191 | 0 | } |
2192 | 0 | } |
2193 | 0 | p = p->next; |
2194 | 0 | } while (p != heap->main_chunk); |
2195 | 0 | if (total) { |
2196 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &total); |
2197 | 0 | } |
2198 | 0 | } |
2199 | | #endif |
2200 | | |
2201 | | #if ZEND_MM_CUSTOM |
2202 | | static void *tracked_malloc(size_t size); |
2203 | | static void tracked_free_all(); |
2204 | | #endif |
2205 | | |
2206 | | void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent) |
2207 | 364k | { |
2208 | 364k | zend_mm_chunk *p; |
2209 | 364k | zend_mm_huge_list *list; |
2210 | | |
2211 | 364k | #if ZEND_MM_CUSTOM |
2212 | 364k | if (heap->use_custom_heap) { |
2213 | 364k | if (heap->custom_heap.std._malloc == tracked_malloc) { |
2214 | 364k | if (silent) { |
2215 | 28.3k | tracked_free_all(); |
2216 | 28.3k | } |
2217 | 364k | zend_hash_clean(heap->tracked_allocs); |
2218 | 364k | if (full) { |
2219 | 0 | zend_hash_destroy(heap->tracked_allocs); |
2220 | 0 | free(heap->tracked_allocs); |
2221 | | /* Make sure the heap free below does not use tracked_free(). */ |
2222 | 0 | heap->custom_heap.std._free = free; |
2223 | 0 | } |
2224 | 364k | heap->size = 0; |
2225 | 364k | } |
2226 | | |
2227 | 364k | if (full) { |
2228 | 0 | if (ZEND_DEBUG && heap->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) { |
2229 | 0 | heap->custom_heap.debug._free(heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC); |
2230 | 0 | } else { |
2231 | 0 | heap->custom_heap.std._free(heap); |
2232 | 0 | } |
2233 | 0 | } |
2234 | 364k | return; |
2235 | 364k | } |
2236 | 0 | #endif |
2237 | | |
2238 | 0 | #if ZEND_DEBUG |
2239 | 0 | if (!silent) { |
2240 | 0 | zend_mm_check_leaks(heap); |
2241 | 0 | } |
2242 | 0 | #endif |
2243 | | |
2244 | | /* free huge blocks */ |
2245 | 0 | list = heap->huge_list; |
2246 | 0 | heap->huge_list = NULL; |
2247 | 0 | while (list) { |
2248 | 0 | zend_mm_huge_list *q = list; |
2249 | 0 | list = list->next; |
2250 | 0 | zend_mm_chunk_free(heap, q->ptr, q->size); |
2251 | 0 | } |
2252 | | |
2253 | | /* move all chunks except of the first one into the cache */ |
2254 | 0 | p = heap->main_chunk->next; |
2255 | 0 | while (p != heap->main_chunk) { |
2256 | 0 | zend_mm_chunk *q = p->next; |
2257 | 0 | p->next = heap->cached_chunks; |
2258 | 0 | heap->cached_chunks = p; |
2259 | 0 | p = q; |
2260 | 0 | heap->chunks_count--; |
2261 | 0 | heap->cached_chunks_count++; |
2262 | 0 | } |
2263 | |
|
2264 | 0 | if (full) { |
2265 | | /* free all cached chunks */ |
2266 | 0 | while (heap->cached_chunks) { |
2267 | 0 | p = heap->cached_chunks; |
2268 | 0 | heap->cached_chunks = p->next; |
2269 | 0 | zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE); |
2270 | 0 | } |
2271 | | /* free the first chunk */ |
2272 | 0 | zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE); |
2273 | 0 | } else { |
2274 | | /* free some cached chunks to keep average count */ |
2275 | 0 | heap->avg_chunks_count = (heap->avg_chunks_count + (double)heap->peak_chunks_count) / 2.0; |
2276 | 0 | while ((double)heap->cached_chunks_count + 0.9 > heap->avg_chunks_count && |
2277 | 0 | heap->cached_chunks) { |
2278 | 0 | p = heap->cached_chunks; |
2279 | 0 | heap->cached_chunks = p->next; |
2280 | 0 | zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE); |
2281 | 0 | heap->cached_chunks_count--; |
2282 | 0 | } |
2283 | | /* clear cached chunks */ |
2284 | 0 | p = heap->cached_chunks; |
2285 | 0 | while (p != NULL) { |
2286 | 0 | zend_mm_chunk *q = p->next; |
2287 | 0 | memset(p, 0, sizeof(zend_mm_chunk)); |
2288 | 0 | p->next = q; |
2289 | 0 | p = q; |
2290 | 0 | } |
2291 | | |
2292 | | /* reinitialize the first chunk and heap */ |
2293 | 0 | p = heap->main_chunk; |
2294 | 0 | p->heap = &p->heap_slot; |
2295 | 0 | p->next = p; |
2296 | 0 | p->prev = p; |
2297 | 0 | p->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
2298 | 0 | p->free_tail = ZEND_MM_FIRST_PAGE; |
2299 | 0 | p->num = 0; |
2300 | |
|
2301 | 0 | #if ZEND_MM_STAT |
2302 | 0 | heap->size = heap->peak = 0; |
2303 | 0 | #endif |
2304 | 0 | memset(heap->free_slot, 0, sizeof(heap->free_slot)); |
2305 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
2306 | 0 | heap->real_size = ZEND_MM_CHUNK_SIZE; |
2307 | 0 | #endif |
2308 | 0 | #if ZEND_MM_STAT |
2309 | 0 | heap->real_peak = ZEND_MM_CHUNK_SIZE; |
2310 | 0 | #endif |
2311 | 0 | heap->chunks_count = 1; |
2312 | 0 | heap->peak_chunks_count = 1; |
2313 | 0 | heap->last_chunks_delete_boundary = 0; |
2314 | 0 | heap->last_chunks_delete_count = 0; |
2315 | |
|
2316 | 0 | memset(p->free_map, 0, sizeof(p->free_map) + sizeof(p->map)); |
2317 | 0 | p->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1; |
2318 | 0 | p->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
2319 | 0 | } |
2320 | 0 | } |
2321 | | |
2322 | | /**************/ |
2323 | | /* PUBLIC API */ |
2324 | | /**************/ |
2325 | | |
2326 | | ZEND_API void* ZEND_FASTCALL _zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2327 | 0 | { |
2328 | 0 | return zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2329 | 0 | } |
2330 | | |
2331 | | ZEND_API void ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2332 | 0 | { |
2333 | 0 | zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2334 | 0 | } |
2335 | | |
2336 | | void* ZEND_FASTCALL _zend_mm_realloc(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2337 | 0 | { |
2338 | 0 | return zend_mm_realloc_heap(heap, ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2339 | 0 | } |
2340 | | |
2341 | | void* ZEND_FASTCALL _zend_mm_realloc2(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2342 | 0 | { |
2343 | 0 | return zend_mm_realloc_heap(heap, ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2344 | 0 | } |
2345 | | |
2346 | | ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2347 | 0 | { |
2348 | 0 | return zend_mm_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2349 | 0 | } |
2350 | | |
2351 | | /**********************/ |
2352 | | /* Allocation Manager */ |
2353 | | /**********************/ |
2354 | | |
2355 | | typedef struct _zend_alloc_globals { |
2356 | | zend_mm_heap *mm_heap; |
2357 | | } zend_alloc_globals; |
2358 | | |
2359 | | #ifdef ZTS |
2360 | | static int alloc_globals_id; |
2361 | | static size_t alloc_globals_offset; |
2362 | | # define AG(v) ZEND_TSRMG_FAST(alloc_globals_offset, zend_alloc_globals *, v) |
2363 | | #else |
2364 | 554M | # define AG(v) (alloc_globals.v) |
2365 | | static zend_alloc_globals alloc_globals; |
2366 | | #endif |
2367 | | |
2368 | | ZEND_API bool is_zend_mm(void) |
2369 | 51 | { |
2370 | 51 | #if ZEND_MM_CUSTOM |
2371 | 51 | return !AG(mm_heap)->use_custom_heap; |
2372 | | #else |
2373 | | return 1; |
2374 | | #endif |
2375 | 51 | } |
2376 | | |
2377 | | ZEND_API bool is_zend_ptr(const void *ptr) |
2378 | 0 | { |
2379 | 0 | #if ZEND_MM_CUSTOM |
2380 | 0 | if (AG(mm_heap)->use_custom_heap) { |
2381 | 0 | return 0; |
2382 | 0 | } |
2383 | 0 | #endif |
2384 | | |
2385 | 0 | if (AG(mm_heap)->main_chunk) { |
2386 | 0 | zend_mm_chunk *chunk = AG(mm_heap)->main_chunk; |
2387 | |
|
2388 | 0 | do { |
2389 | 0 | if (ptr >= (void*)chunk |
2390 | 0 | && ptr < (void*)((char*)chunk + ZEND_MM_CHUNK_SIZE)) { |
2391 | 0 | return 1; |
2392 | 0 | } |
2393 | 0 | chunk = chunk->next; |
2394 | 0 | } while (chunk != AG(mm_heap)->main_chunk); |
2395 | 0 | } |
2396 | |
|
2397 | 0 | if (AG(mm_heap)->huge_list) { |
2398 | 0 | zend_mm_huge_list *block = AG(mm_heap)->huge_list; |
2399 | |
|
2400 | 0 | do { |
2401 | 0 | if (ptr >= (void*)block |
2402 | 0 | && ptr < (void*)((char*)block + block->size)) { |
2403 | 0 | return 1; |
2404 | 0 | } |
2405 | 0 | block = block->next; |
2406 | 0 | } while (block != AG(mm_heap)->huge_list); |
2407 | 0 | } |
2408 | 0 | return 0; |
2409 | 0 | } |
2410 | | |
2411 | | #if ZEND_MM_CUSTOM |
2412 | | |
2413 | | static ZEND_COLD void* ZEND_FASTCALL _malloc_custom(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2414 | 76.3M | { |
2415 | 76.3M | if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) { |
2416 | 0 | return AG(mm_heap)->custom_heap.debug._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2417 | 76.3M | } else { |
2418 | 76.3M | return AG(mm_heap)->custom_heap.std._malloc(size); |
2419 | 76.3M | } |
2420 | 76.3M | } |
2421 | | |
2422 | | static ZEND_COLD void ZEND_FASTCALL _efree_custom(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2423 | 79.6M | { |
2424 | 79.6M | if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) { |
2425 | 0 | AG(mm_heap)->custom_heap.debug._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2426 | 79.6M | } else { |
2427 | 79.6M | AG(mm_heap)->custom_heap.std._free(ptr); |
2428 | 79.6M | } |
2429 | 79.6M | } |
2430 | | |
2431 | | static ZEND_COLD void* ZEND_FASTCALL _realloc_custom(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2432 | 29.1M | { |
2433 | 29.1M | if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) { |
2434 | 0 | return AG(mm_heap)->custom_heap.debug._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2435 | 29.1M | } else { |
2436 | 29.1M | return AG(mm_heap)->custom_heap.std._realloc(ptr, size); |
2437 | 29.1M | } |
2438 | 29.1M | } |
2439 | | #endif |
2440 | | |
2441 | | #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P) |
2442 | | #undef _emalloc |
2443 | | |
2444 | | #if ZEND_MM_CUSTOM |
2445 | | # define ZEND_MM_CUSTOM_ALLOCATOR(size) do { \ |
2446 | | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \ |
2447 | | return _malloc_custom(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \ |
2448 | | } \ |
2449 | | } while (0) |
2450 | | # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) do { \ |
2451 | | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \ |
2452 | | _efree_custom(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \ |
2453 | | return; \ |
2454 | | } \ |
2455 | | } while (0) |
2456 | | #else |
2457 | | # define ZEND_MM_CUSTOM_ALLOCATOR(size) |
2458 | | # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) |
2459 | | #endif |
2460 | | |
2461 | | # define _ZEND_BIN_ALLOCATOR(_num, _size, _elements, _pages, x, y) \ |
2462 | | ZEND_API void* ZEND_FASTCALL _emalloc_ ## _size(void) { \ |
2463 | | ZEND_MM_CUSTOM_ALLOCATOR(_size); \ |
2464 | | return zend_mm_alloc_small(AG(mm_heap), _num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \ |
2465 | | } |
2466 | | |
2467 | | ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR, x, y) |
2468 | | |
2469 | | ZEND_API void* ZEND_FASTCALL _emalloc_large(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2470 | | { |
2471 | | ZEND_MM_CUSTOM_ALLOCATOR(size); |
2472 | | return zend_mm_alloc_large_ex(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2473 | | } |
2474 | | |
2475 | | ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size) |
2476 | | { |
2477 | | ZEND_MM_CUSTOM_ALLOCATOR(size); |
2478 | | return zend_mm_alloc_huge(AG(mm_heap), size); |
2479 | | } |
2480 | | |
2481 | | #if ZEND_DEBUG |
2482 | | # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \ |
2483 | | ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \ |
2484 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \ |
2485 | | { \ |
2486 | | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); \ |
2487 | | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \ |
2488 | | int page_num = page_offset / ZEND_MM_PAGE_SIZE; \ |
2489 | | ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \ |
2490 | | ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_SRUN); \ |
2491 | | ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(chunk->map[page_num]) == _num); \ |
2492 | | zend_mm_free_small(AG(mm_heap), ptr, _num); \ |
2493 | | } \ |
2494 | | } |
2495 | | #else |
2496 | | # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \ |
2497 | | ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \ |
2498 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \ |
2499 | | { \ |
2500 | | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \ |
2501 | | ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \ |
2502 | | zend_mm_free_small(AG(mm_heap), ptr, _num); \ |
2503 | | } \ |
2504 | | } |
2505 | | #endif |
2506 | | |
2507 | | ZEND_MM_BINS_INFO(_ZEND_BIN_FREE, x, y) |
2508 | | |
2509 | | ZEND_API void ZEND_FASTCALL _efree_large(void *ptr, size_t size) |
2510 | | { |
2511 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); |
2512 | | { |
2513 | | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
2514 | | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
2515 | | int page_num = page_offset / ZEND_MM_PAGE_SIZE; |
2516 | | uint32_t pages_count = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE) / ZEND_MM_PAGE_SIZE; |
2517 | | |
2518 | | ZEND_MM_CHECK(chunk->heap == AG(mm_heap) && ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted"); |
2519 | | ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_LRUN); |
2520 | | ZEND_ASSERT(ZEND_MM_LRUN_PAGES(chunk->map[page_num]) == pages_count); |
2521 | | zend_mm_free_large(AG(mm_heap), chunk, page_num, pages_count); |
2522 | | } |
2523 | | } |
2524 | | |
2525 | | ZEND_API void ZEND_FASTCALL _efree_huge(void *ptr, size_t size) |
2526 | | { |
2527 | | |
2528 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); |
2529 | | zend_mm_free_huge(AG(mm_heap), ptr); |
2530 | | } |
2531 | | #endif |
2532 | | |
2533 | | ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2534 | 76.3M | { |
2535 | 76.3M | #if ZEND_MM_CUSTOM |
2536 | 76.3M | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
2537 | 76.3M | return _malloc_custom(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2538 | 76.3M | } |
2539 | 0 | #endif |
2540 | 0 | return zend_mm_alloc_heap(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2541 | 0 | } |
2542 | | |
2543 | | ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2544 | 79.6M | { |
2545 | 79.6M | #if ZEND_MM_CUSTOM |
2546 | 79.6M | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
2547 | 79.6M | _efree_custom(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2548 | 79.6M | return; |
2549 | 79.6M | } |
2550 | 0 | #endif |
2551 | 0 | zend_mm_free_heap(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2552 | 0 | } |
2553 | | |
2554 | | ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2555 | 29.1M | { |
2556 | 29.1M | #if ZEND_MM_CUSTOM |
2557 | 29.1M | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
2558 | 29.1M | return _realloc_custom(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2559 | 29.1M | } |
2560 | 0 | #endif |
2561 | 0 | return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2562 | 0 | } |
2563 | | |
2564 | | ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2565 | 49.0k | { |
2566 | 49.0k | #if ZEND_MM_CUSTOM |
2567 | 49.0k | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
2568 | 49.0k | return _realloc_custom(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2569 | 49.0k | } |
2570 | 0 | #endif |
2571 | 0 | return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2572 | 0 | } |
2573 | | |
2574 | | ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2575 | 0 | { |
2576 | 0 | #if ZEND_MM_CUSTOM |
2577 | 0 | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
2578 | 0 | return 0; |
2579 | 0 | } |
2580 | 0 | #endif |
2581 | 0 | return zend_mm_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2582 | 0 | } |
2583 | | |
2584 | | ZEND_API void* ZEND_FASTCALL _safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2585 | 1.45M | { |
2586 | 1.45M | return _emalloc(zend_safe_address_guarded(nmemb, size, offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2587 | 1.45M | } |
2588 | | |
2589 | | ZEND_API void* ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset) |
2590 | 0 | { |
2591 | 0 | return pemalloc(zend_safe_address_guarded(nmemb, size, offset), 1); |
2592 | 0 | } |
2593 | | |
2594 | | ZEND_API void* ZEND_FASTCALL _safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2595 | 1.33M | { |
2596 | 1.33M | return _erealloc(ptr, zend_safe_address_guarded(nmemb, size, offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2597 | 1.33M | } |
2598 | | |
2599 | | ZEND_API void* ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset) |
2600 | 0 | { |
2601 | 0 | return perealloc(ptr, zend_safe_address_guarded(nmemb, size, offset), 1); |
2602 | 0 | } |
2603 | | |
2604 | | ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2605 | 8.41M | { |
2606 | 8.41M | void *p; |
2607 | | |
2608 | 8.41M | size = zend_safe_address_guarded(nmemb, size, 0); |
2609 | 8.41M | p = _emalloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2610 | 8.41M | memset(p, 0, size); |
2611 | 8.41M | return p; |
2612 | 8.41M | } |
2613 | | |
2614 | | ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2615 | 23.3M | { |
2616 | 23.3M | size_t length; |
2617 | 23.3M | char *p; |
2618 | | |
2619 | 23.3M | length = strlen(s); |
2620 | 23.3M | if (UNEXPECTED(length + 1 == 0)) { |
2621 | 0 | zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (1 * %zu + 1)", length); |
2622 | 0 | } |
2623 | 23.3M | p = (char *) _emalloc(length + 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2624 | 23.3M | memcpy(p, s, length+1); |
2625 | 23.3M | return p; |
2626 | 23.3M | } |
2627 | | |
2628 | | ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2629 | 965k | { |
2630 | 965k | char *p; |
2631 | | |
2632 | 965k | if (UNEXPECTED(length + 1 == 0)) { |
2633 | 0 | zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (1 * %zu + 1)", length); |
2634 | 0 | } |
2635 | 965k | p = (char *) _emalloc(length + 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2636 | 965k | memcpy(p, s, length); |
2637 | 965k | p[length] = 0; |
2638 | 965k | return p; |
2639 | 965k | } |
2640 | | |
2641 | | |
2642 | | ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length) |
2643 | 0 | { |
2644 | 0 | char *p; |
2645 | |
|
2646 | 0 | if (UNEXPECTED(length + 1 == 0)) { |
2647 | 0 | zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (1 * %zu + 1)", length); |
2648 | 0 | } |
2649 | 0 | p = (char *) malloc(length + 1); |
2650 | 0 | if (UNEXPECTED(p == NULL)) { |
2651 | 0 | return p; |
2652 | 0 | } |
2653 | 0 | if (EXPECTED(length)) { |
2654 | 0 | memcpy(p, s, length); |
2655 | 0 | } |
2656 | 0 | p[length] = 0; |
2657 | 0 | return p; |
2658 | 0 | } |
2659 | | |
2660 | | |
2661 | | ZEND_API void zend_set_memory_limit(size_t memory_limit) |
2662 | 16.7k | { |
2663 | 16.7k | #if ZEND_MM_LIMIT |
2664 | 16.7k | AG(mm_heap)->limit = (memory_limit >= ZEND_MM_CHUNK_SIZE) ? memory_limit : ZEND_MM_CHUNK_SIZE; |
2665 | 16.7k | #endif |
2666 | 16.7k | } |
2667 | | |
2668 | | ZEND_API size_t zend_memory_usage(bool real_usage) |
2669 | 2.07k | { |
2670 | 2.07k | #if ZEND_MM_STAT |
2671 | 2.07k | if (real_usage) { |
2672 | 1.96k | return AG(mm_heap)->real_size; |
2673 | 110 | } else { |
2674 | 110 | size_t usage = AG(mm_heap)->size; |
2675 | 110 | return usage; |
2676 | 110 | } |
2677 | 0 | #endif |
2678 | 0 | return 0; |
2679 | 0 | } |
2680 | | |
2681 | | ZEND_API size_t zend_memory_peak_usage(bool real_usage) |
2682 | 0 | { |
2683 | 0 | #if ZEND_MM_STAT |
2684 | 0 | if (real_usage) { |
2685 | 0 | return AG(mm_heap)->real_peak; |
2686 | 0 | } else { |
2687 | 0 | return AG(mm_heap)->peak; |
2688 | 0 | } |
2689 | 0 | #endif |
2690 | 0 | return 0; |
2691 | 0 | } |
2692 | | |
2693 | | ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown) |
2694 | 364k | { |
2695 | 364k | zend_mm_shutdown(AG(mm_heap), full_shutdown, silent); |
2696 | 364k | } |
2697 | | |
2698 | | #if ZEND_MM_CUSTOM |
2699 | 105M | static zend_always_inline void tracked_add(zend_mm_heap *heap, void *ptr, size_t size) { |
2700 | 105M | zval size_zv; |
2701 | 105M | zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2; |
2702 | 105M | ZEND_ASSERT((void *) (uintptr_t) (h << ZEND_MM_ALIGNMENT_LOG2) == ptr); |
2703 | 105M | ZVAL_LONG(&size_zv, size); |
2704 | 105M | zend_hash_index_add_new(heap->tracked_allocs, h, &size_zv); |
2705 | 105M | } |
2706 | | |
2707 | 104M | static zend_always_inline zval *tracked_get_size_zv(zend_mm_heap *heap, void *ptr) { |
2708 | 104M | zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2; |
2709 | 104M | zval *size_zv = zend_hash_index_find(heap->tracked_allocs, h); |
2710 | 104M | ZEND_ASSERT(size_zv && "Trying to free pointer not allocated through ZendMM"); |
2711 | 104M | return size_zv; |
2712 | 104M | } |
2713 | | |
2714 | 104M | static zend_always_inline void tracked_check_limit(zend_mm_heap *heap, size_t add_size) { |
2715 | 104M | if (add_size > heap->limit - heap->size && !heap->overflow) { |
2716 | 27 | #if ZEND_DEBUG |
2717 | 27 | zend_mm_safe_error(heap, |
2718 | 27 | "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", |
2719 | 27 | heap->limit, "file", 0, add_size); |
2720 | | #else |
2721 | | zend_mm_safe_error(heap, |
2722 | | "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", |
2723 | | heap->limit, add_size); |
2724 | | #endif |
2725 | 27 | } |
2726 | 104M | } |
2727 | | |
2728 | | static void *tracked_malloc(size_t size) |
2729 | 76.3M | { |
2730 | 76.3M | zend_mm_heap *heap = AG(mm_heap); |
2731 | 76.3M | tracked_check_limit(heap, size); |
2732 | | |
2733 | 76.3M | void *ptr = __zend_malloc(size); |
2734 | 76.3M | tracked_add(heap, ptr, size); |
2735 | 76.3M | heap->size += size; |
2736 | 76.3M | return ptr; |
2737 | 76.3M | } |
2738 | | |
2739 | 79.6M | static void tracked_free(void *ptr) { |
2740 | 79.6M | if (!ptr) { |
2741 | 1.07M | return; |
2742 | 1.07M | } |
2743 | | |
2744 | 78.5M | zend_mm_heap *heap = AG(mm_heap); |
2745 | 78.5M | zval *size_zv = tracked_get_size_zv(heap, ptr); |
2746 | 78.5M | heap->size -= Z_LVAL_P(size_zv); |
2747 | 78.5M | zend_hash_del_bucket(heap->tracked_allocs, (Bucket *) size_zv); |
2748 | 78.5M | free(ptr); |
2749 | 78.5M | } |
2750 | | |
2751 | 29.1M | static void *tracked_realloc(void *ptr, size_t new_size) { |
2752 | 29.1M | zend_mm_heap *heap = AG(mm_heap); |
2753 | 29.1M | zval *old_size_zv = NULL; |
2754 | 29.1M | size_t old_size = 0; |
2755 | 29.1M | if (ptr) { |
2756 | 25.8M | old_size_zv = tracked_get_size_zv(heap, ptr); |
2757 | 25.8M | old_size = Z_LVAL_P(old_size_zv); |
2758 | 25.8M | } |
2759 | | |
2760 | 29.1M | if (new_size > old_size) { |
2761 | 27.6M | tracked_check_limit(heap, new_size - old_size); |
2762 | 27.6M | } |
2763 | | |
2764 | | /* Delete information about old allocation only after checking the memory limit. */ |
2765 | 29.1M | if (old_size_zv) { |
2766 | 25.8M | zend_hash_del_bucket(heap->tracked_allocs, (Bucket *) old_size_zv); |
2767 | 25.8M | } |
2768 | | |
2769 | 29.1M | ptr = __zend_realloc(ptr, new_size); |
2770 | 29.1M | tracked_add(heap, ptr, new_size); |
2771 | 29.1M | heap->size += new_size - old_size; |
2772 | 29.1M | return ptr; |
2773 | 29.1M | } |
2774 | | |
2775 | 28.3k | static void tracked_free_all() { |
2776 | 28.3k | HashTable *tracked_allocs = AG(mm_heap)->tracked_allocs; |
2777 | 28.3k | zend_ulong h; |
2778 | 5.40M | ZEND_HASH_FOREACH_NUM_KEY(tracked_allocs, h) { |
2779 | 814k | void *ptr = (void *) (uintptr_t) (h << ZEND_MM_ALIGNMENT_LOG2); |
2780 | 5.40M | free(ptr); |
2781 | 814k | } ZEND_HASH_FOREACH_END(); |
2782 | 28.3k | } |
2783 | | #endif |
2784 | | |
2785 | | static void alloc_globals_ctor(zend_alloc_globals *alloc_globals) |
2786 | 3.00k | { |
2787 | 3.00k | char *tmp; |
2788 | | |
2789 | 3.00k | #if ZEND_MM_CUSTOM |
2790 | 3.00k | tmp = getenv("USE_ZEND_ALLOC"); |
2791 | 3.00k | if (tmp && !zend_atoi(tmp, 0)) { |
2792 | 3.00k | zend_bool tracked = (tmp = getenv("USE_TRACKED_ALLOC")) && zend_atoi(tmp, 0); |
2793 | 3.00k | zend_mm_heap *mm_heap = alloc_globals->mm_heap = malloc(sizeof(zend_mm_heap)); |
2794 | 3.00k | memset(mm_heap, 0, sizeof(zend_mm_heap)); |
2795 | 3.00k | mm_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD; |
2796 | 3.00k | mm_heap->limit = ((size_t)Z_L(-1) >> (size_t)Z_L(1)); |
2797 | 3.00k | mm_heap->overflow = 0; |
2798 | | |
2799 | 3.00k | if (!tracked) { |
2800 | | /* Use system allocator. */ |
2801 | 0 | mm_heap->custom_heap.std._malloc = __zend_malloc; |
2802 | 0 | mm_heap->custom_heap.std._free = free; |
2803 | 0 | mm_heap->custom_heap.std._realloc = __zend_realloc; |
2804 | 3.00k | } else { |
2805 | | /* Use system allocator and track allocations for auto-free. */ |
2806 | 3.00k | mm_heap->custom_heap.std._malloc = tracked_malloc; |
2807 | 3.00k | mm_heap->custom_heap.std._free = tracked_free; |
2808 | 3.00k | mm_heap->custom_heap.std._realloc = tracked_realloc; |
2809 | 3.00k | mm_heap->tracked_allocs = malloc(sizeof(HashTable)); |
2810 | 3.00k | zend_hash_init(mm_heap->tracked_allocs, 1024, NULL, NULL, 1); |
2811 | 3.00k | } |
2812 | 3.00k | return; |
2813 | 3.00k | } |
2814 | 0 | #endif |
2815 | | |
2816 | 0 | tmp = getenv("USE_ZEND_ALLOC_HUGE_PAGES"); |
2817 | 0 | if (tmp && zend_atoi(tmp, 0)) { |
2818 | 0 | zend_mm_use_huge_pages = 1; |
2819 | 0 | } |
2820 | 0 | alloc_globals->mm_heap = zend_mm_init(); |
2821 | 0 | } |
2822 | | |
2823 | | #ifdef ZTS |
2824 | | static void alloc_globals_dtor(zend_alloc_globals *alloc_globals) |
2825 | | { |
2826 | | zend_mm_shutdown(alloc_globals->mm_heap, 1, 1); |
2827 | | } |
2828 | | #endif |
2829 | | |
2830 | | ZEND_API void start_memory_manager(void) |
2831 | 3.00k | { |
2832 | | #ifdef ZTS |
2833 | | ts_allocate_fast_id(&alloc_globals_id, &alloc_globals_offset, sizeof(zend_alloc_globals), (ts_allocate_ctor) alloc_globals_ctor, (ts_allocate_dtor) alloc_globals_dtor); |
2834 | | #else |
2835 | 3.00k | alloc_globals_ctor(&alloc_globals); |
2836 | 3.00k | #endif |
2837 | 3.00k | #ifndef _WIN32 |
2838 | 3.00k | # if defined(_SC_PAGESIZE) |
2839 | 3.00k | REAL_PAGE_SIZE = sysconf(_SC_PAGESIZE); |
2840 | | # elif defined(_SC_PAGE_SIZE) |
2841 | | REAL_PAGE_SIZE = sysconf(_SC_PAGE_SIZE); |
2842 | | # endif |
2843 | 3.00k | #endif |
2844 | 3.00k | } |
2845 | | |
2846 | | ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap) |
2847 | 0 | { |
2848 | 0 | zend_mm_heap *old_heap; |
2849 | |
|
2850 | 0 | old_heap = AG(mm_heap); |
2851 | 0 | AG(mm_heap) = (zend_mm_heap*)new_heap; |
2852 | 0 | return (zend_mm_heap*)old_heap; |
2853 | 0 | } |
2854 | | |
2855 | | ZEND_API zend_mm_heap *zend_mm_get_heap(void) |
2856 | 0 | { |
2857 | 0 | return AG(mm_heap); |
2858 | 0 | } |
2859 | | |
2860 | | ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap) |
2861 | 0 | { |
2862 | 0 | #if ZEND_MM_CUSTOM |
2863 | 0 | return AG(mm_heap)->use_custom_heap; |
2864 | | #else |
2865 | | return 0; |
2866 | | #endif |
2867 | 0 | } |
2868 | | |
2869 | | ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap, |
2870 | | void* (*_malloc)(size_t), |
2871 | | void (*_free)(void*), |
2872 | | void* (*_realloc)(void*, size_t)) |
2873 | 0 | { |
2874 | 0 | #if ZEND_MM_CUSTOM |
2875 | 0 | zend_mm_heap *_heap = (zend_mm_heap*)heap; |
2876 | |
|
2877 | 0 | if (!_malloc && !_free && !_realloc) { |
2878 | 0 | _heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE; |
2879 | 0 | } else { |
2880 | 0 | _heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD; |
2881 | 0 | _heap->custom_heap.std._malloc = _malloc; |
2882 | 0 | _heap->custom_heap.std._free = _free; |
2883 | 0 | _heap->custom_heap.std._realloc = _realloc; |
2884 | 0 | } |
2885 | 0 | #endif |
2886 | 0 | } |
2887 | | |
2888 | | ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap, |
2889 | | void* (**_malloc)(size_t), |
2890 | | void (**_free)(void*), |
2891 | | void* (**_realloc)(void*, size_t)) |
2892 | 0 | { |
2893 | 0 | #if ZEND_MM_CUSTOM |
2894 | 0 | zend_mm_heap *_heap = (zend_mm_heap*)heap; |
2895 | |
|
2896 | 0 | if (heap->use_custom_heap) { |
2897 | 0 | *_malloc = _heap->custom_heap.std._malloc; |
2898 | 0 | *_free = _heap->custom_heap.std._free; |
2899 | 0 | *_realloc = _heap->custom_heap.std._realloc; |
2900 | 0 | } else { |
2901 | 0 | *_malloc = NULL; |
2902 | 0 | *_free = NULL; |
2903 | 0 | *_realloc = NULL; |
2904 | 0 | } |
2905 | | #else |
2906 | | *_malloc = NULL; |
2907 | | *_free = NULL; |
2908 | | *_realloc = NULL; |
2909 | | #endif |
2910 | 0 | } |
2911 | | |
2912 | | #if ZEND_DEBUG |
2913 | | ZEND_API void zend_mm_set_custom_debug_handlers(zend_mm_heap *heap, |
2914 | | void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
2915 | | void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
2916 | | void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)) |
2917 | 0 | { |
2918 | 0 | #if ZEND_MM_CUSTOM |
2919 | 0 | zend_mm_heap *_heap = (zend_mm_heap*)heap; |
2920 | |
|
2921 | 0 | _heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_DEBUG; |
2922 | 0 | _heap->custom_heap.debug._malloc = _malloc; |
2923 | 0 | _heap->custom_heap.debug._free = _free; |
2924 | 0 | _heap->custom_heap.debug._realloc = _realloc; |
2925 | 0 | #endif |
2926 | 0 | } |
2927 | | #endif |
2928 | | |
2929 | | ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap) |
2930 | 0 | { |
2931 | 0 | #if ZEND_MM_STORAGE |
2932 | 0 | return heap->storage; |
2933 | | #else |
2934 | | return NULL |
2935 | | #endif |
2936 | 0 | } |
2937 | | |
2938 | | ZEND_API zend_mm_heap *zend_mm_startup(void) |
2939 | 0 | { |
2940 | 0 | return zend_mm_init(); |
2941 | 0 | } |
2942 | | |
2943 | | ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void *data, size_t data_size) |
2944 | 0 | { |
2945 | 0 | #if ZEND_MM_STORAGE |
2946 | 0 | zend_mm_storage tmp_storage, *storage; |
2947 | 0 | zend_mm_chunk *chunk; |
2948 | 0 | zend_mm_heap *heap; |
2949 | |
|
2950 | 0 | memcpy((zend_mm_handlers*)&tmp_storage.handlers, handlers, sizeof(zend_mm_handlers)); |
2951 | 0 | tmp_storage.data = data; |
2952 | 0 | chunk = (zend_mm_chunk*)handlers->chunk_alloc(&tmp_storage, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE); |
2953 | 0 | if (UNEXPECTED(chunk == NULL)) { |
2954 | 0 | #if ZEND_MM_ERROR |
2955 | | #ifdef _WIN32 |
2956 | | stderr_last_error("Can't initialize heap"); |
2957 | | #else |
2958 | 0 | fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno)); |
2959 | 0 | #endif |
2960 | 0 | #endif |
2961 | 0 | return NULL; |
2962 | 0 | } |
2963 | 0 | heap = &chunk->heap_slot; |
2964 | 0 | chunk->heap = heap; |
2965 | 0 | chunk->next = chunk; |
2966 | 0 | chunk->prev = chunk; |
2967 | 0 | chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
2968 | 0 | chunk->free_tail = ZEND_MM_FIRST_PAGE; |
2969 | 0 | chunk->num = 0; |
2970 | 0 | chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1; |
2971 | 0 | chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
2972 | 0 | heap->main_chunk = chunk; |
2973 | 0 | heap->cached_chunks = NULL; |
2974 | 0 | heap->chunks_count = 1; |
2975 | 0 | heap->peak_chunks_count = 1; |
2976 | 0 | heap->cached_chunks_count = 0; |
2977 | 0 | heap->avg_chunks_count = 1.0; |
2978 | 0 | heap->last_chunks_delete_boundary = 0; |
2979 | 0 | heap->last_chunks_delete_count = 0; |
2980 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
2981 | 0 | heap->real_size = ZEND_MM_CHUNK_SIZE; |
2982 | 0 | #endif |
2983 | 0 | #if ZEND_MM_STAT |
2984 | 0 | heap->real_peak = ZEND_MM_CHUNK_SIZE; |
2985 | 0 | heap->size = 0; |
2986 | 0 | heap->peak = 0; |
2987 | 0 | #endif |
2988 | 0 | #if ZEND_MM_LIMIT |
2989 | 0 | heap->limit = (Z_L(-1) >> Z_L(1)); |
2990 | 0 | heap->overflow = 0; |
2991 | 0 | #endif |
2992 | 0 | #if ZEND_MM_CUSTOM |
2993 | 0 | heap->use_custom_heap = 0; |
2994 | 0 | #endif |
2995 | 0 | heap->storage = &tmp_storage; |
2996 | 0 | heap->huge_list = NULL; |
2997 | 0 | memset(heap->free_slot, 0, sizeof(heap->free_slot)); |
2998 | 0 | storage = _zend_mm_alloc(heap, sizeof(zend_mm_storage) + data_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_CC); |
2999 | 0 | if (!storage) { |
3000 | 0 | handlers->chunk_free(&tmp_storage, chunk, ZEND_MM_CHUNK_SIZE); |
3001 | 0 | #if ZEND_MM_ERROR |
3002 | | #ifdef _WIN32 |
3003 | | stderr_last_error("Can't initialize heap"); |
3004 | | #else |
3005 | 0 | fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno)); |
3006 | 0 | #endif |
3007 | 0 | #endif |
3008 | 0 | return NULL; |
3009 | 0 | } |
3010 | 0 | memcpy(storage, &tmp_storage, sizeof(zend_mm_storage)); |
3011 | 0 | if (data) { |
3012 | 0 | storage->data = (void*)(((char*)storage + sizeof(zend_mm_storage))); |
3013 | 0 | memcpy(storage->data, data, data_size); |
3014 | 0 | } |
3015 | 0 | heap->storage = storage; |
3016 | 0 | return heap; |
3017 | | #else |
3018 | | return NULL; |
3019 | | #endif |
3020 | 0 | } |
3021 | | |
3022 | | static ZEND_COLD ZEND_NORETURN void zend_out_of_memory(void) |
3023 | 0 | { |
3024 | 0 | fprintf(stderr, "Out of memory\n"); |
3025 | 0 | exit(1); |
3026 | 0 | } |
3027 | | |
3028 | | ZEND_API void * __zend_malloc(size_t len) |
3029 | 92.5M | { |
3030 | 92.5M | void *tmp = malloc(len); |
3031 | 92.5M | if (EXPECTED(tmp || !len)) { |
3032 | 92.5M | return tmp; |
3033 | 92.5M | } |
3034 | 0 | zend_out_of_memory(); |
3035 | 0 | } |
3036 | | |
3037 | | ZEND_API void * __zend_calloc(size_t nmemb, size_t len) |
3038 | 6.01k | { |
3039 | 6.01k | void *tmp; |
3040 | | |
3041 | 6.01k | len = zend_safe_address_guarded(nmemb, len, 0); |
3042 | 6.01k | tmp = __zend_malloc(len); |
3043 | 6.01k | memset(tmp, 0, len); |
3044 | 6.01k | return tmp; |
3045 | 6.01k | } |
3046 | | |
3047 | | ZEND_API void * __zend_realloc(void *p, size_t len) |
3048 | 29.3M | { |
3049 | 29.3M | p = realloc(p, len); |
3050 | 29.3M | if (EXPECTED(p || !len)) { |
3051 | 29.3M | return p; |
3052 | 29.3M | } |
3053 | 0 | zend_out_of_memory(); |
3054 | 0 | } |
3055 | | |
3056 | | #ifdef ZTS |
3057 | | size_t zend_mm_globals_size(void) |
3058 | | { |
3059 | | return sizeof(zend_alloc_globals); |
3060 | | } |
3061 | | #endif |