PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/zend_alloc.c

http://github.com/php/php-src
C | 3023 lines | 2512 code | 293 blank | 218 comment | 492 complexity | 1793377e467f135e1b541e26914c0eef MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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. * zend_alloc is designed to be a modern CPU cache friendly memory manager
  22. * for PHP. Most ideas are taken from jemalloc and tcmalloc implementations.
  23. *
  24. * All allocations are split into 3 categories:
  25. *
  26. * Huge - the size is greater than CHUNK size (~2M by default), allocation is
  27. * performed using mmap(). The result is aligned on 2M boundary.
  28. *
  29. * Large - a number of 4096K pages inside a CHUNK. Large blocks
  30. * are always aligned on page boundary.
  31. *
  32. * Small - less than 3/4 of page size. Small sizes are rounded up to nearest
  33. * greater predefined small size (there are 30 predefined sizes:
  34. * 8, 16, 24, 32, ... 3072). Small blocks are allocated from
  35. * RUNs. Each RUN is allocated as a single or few following pages.
  36. * Allocation inside RUNs implemented using linked list of free
  37. * elements. The result is aligned to 8 bytes.
  38. *
  39. * zend_alloc allocates memory from OS by CHUNKs, these CHUNKs and huge memory
  40. * blocks are always aligned to CHUNK boundary. So it's very easy to determine
  41. * the CHUNK owning the certain pointer. Regular CHUNKs reserve a single
  42. * page at start for special purpose. It contains bitset of free pages,
  43. * few bitset for available runs of predefined small sizes, map of pages that
  44. * keeps information about usage of each page in this CHUNK, etc.
  45. *
  46. * zend_alloc provides familiar emalloc/efree/erealloc API, but in addition it
  47. * provides specialized and optimized routines to allocate blocks of predefined
  48. * sizes (e.g. emalloc_2(), emallc_4(), ..., emalloc_large(), etc)
  49. * The library uses C preprocessor tricks that substitute calls to emalloc()
  50. * with more specialized routines when the requested size is known.
  51. */
  52. #include "zend.h"
  53. #include "zend_alloc.h"
  54. #include "zend_globals.h"
  55. #include "zend_operators.h"
  56. #include "zend_multiply.h"
  57. #include "zend_bitset.h"
  58. #include <signal.h>
  59. #ifdef HAVE_UNISTD_H
  60. # include <unistd.h>
  61. #endif
  62. #ifdef ZEND_WIN32
  63. # include <wincrypt.h>
  64. # include <process.h>
  65. # include "win32/winutil.h"
  66. #endif
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <string.h>
  70. #include <sys/types.h>
  71. #include <sys/stat.h>
  72. #include <limits.h>
  73. #include <fcntl.h>
  74. #include <errno.h>
  75. #ifndef _WIN32
  76. # include <sys/mman.h>
  77. # ifndef MAP_ANON
  78. # ifdef MAP_ANONYMOUS
  79. # define MAP_ANON MAP_ANONYMOUS
  80. # endif
  81. # endif
  82. # ifndef MAP_FAILED
  83. # define MAP_FAILED ((void*)-1)
  84. # endif
  85. # ifndef MAP_POPULATE
  86. # define MAP_POPULATE 0
  87. # endif
  88. # if defined(_SC_PAGESIZE) || (_SC_PAGE_SIZE)
  89. # define REAL_PAGE_SIZE _real_page_size
  90. static size_t _real_page_size = ZEND_MM_PAGE_SIZE;
  91. # endif
  92. # ifdef MAP_ALIGNED_SUPER
  93. # define MAP_HUGETLB MAP_ALIGNED_SUPER
  94. # endif
  95. #endif
  96. #ifndef REAL_PAGE_SIZE
  97. # define REAL_PAGE_SIZE ZEND_MM_PAGE_SIZE
  98. #endif
  99. /* NetBSD has an mremap() function with a signature that is incompatible with Linux (WTF?),
  100. * so pretend it doesn't exist. */
  101. #ifndef __linux__
  102. # undef HAVE_MREMAP
  103. #endif
  104. #ifndef __APPLE__
  105. # define ZEND_MM_FD -1
  106. #else
  107. /* Mac allows to track anonymous page via vmmap per TAG id.
  108. * user land applications are allowed to take from 240 to 255.
  109. */
  110. # define ZEND_MM_FD (250<<24)
  111. #endif
  112. #ifndef ZEND_MM_STAT
  113. # define ZEND_MM_STAT 1 /* track current and peak memory usage */
  114. #endif
  115. #ifndef ZEND_MM_LIMIT
  116. # define ZEND_MM_LIMIT 1 /* support for user-defined memory limit */
  117. #endif
  118. #ifndef ZEND_MM_CUSTOM
  119. # define ZEND_MM_CUSTOM 1 /* support for custom memory allocator */
  120. /* USE_ZEND_ALLOC=0 may switch to system malloc() */
  121. #endif
  122. #ifndef ZEND_MM_STORAGE
  123. # define ZEND_MM_STORAGE 1 /* support for custom memory storage */
  124. #endif
  125. #ifndef ZEND_MM_ERROR
  126. # define ZEND_MM_ERROR 1 /* report system errors */
  127. #endif
  128. #ifndef ZEND_MM_CHECK
  129. # define ZEND_MM_CHECK(condition, message) do { \
  130. if (UNEXPECTED(!(condition))) { \
  131. zend_mm_panic(message); \
  132. } \
  133. } while (0)
  134. #endif
  135. typedef uint32_t zend_mm_page_info; /* 4-byte integer */
  136. typedef zend_ulong zend_mm_bitset; /* 4-byte or 8-byte integer */
  137. #define ZEND_MM_ALIGNED_OFFSET(size, alignment) \
  138. (((size_t)(size)) & ((alignment) - 1))
  139. #define ZEND_MM_ALIGNED_BASE(size, alignment) \
  140. (((size_t)(size)) & ~((alignment) - 1))
  141. #define ZEND_MM_SIZE_TO_NUM(size, alignment) \
  142. (((size_t)(size) + ((alignment) - 1)) / (alignment))
  143. #define ZEND_MM_BITSET_LEN (sizeof(zend_mm_bitset) * 8) /* 32 or 64 */
  144. #define ZEND_MM_PAGE_MAP_LEN (ZEND_MM_PAGES / ZEND_MM_BITSET_LEN) /* 16 or 8 */
  145. typedef zend_mm_bitset zend_mm_page_map[ZEND_MM_PAGE_MAP_LEN]; /* 64B */
  146. #define ZEND_MM_IS_FRUN 0x00000000
  147. #define ZEND_MM_IS_LRUN 0x40000000
  148. #define ZEND_MM_IS_SRUN 0x80000000
  149. #define ZEND_MM_LRUN_PAGES_MASK 0x000003ff
  150. #define ZEND_MM_LRUN_PAGES_OFFSET 0
  151. #define ZEND_MM_SRUN_BIN_NUM_MASK 0x0000001f
  152. #define ZEND_MM_SRUN_BIN_NUM_OFFSET 0
  153. #define ZEND_MM_SRUN_FREE_COUNTER_MASK 0x01ff0000
  154. #define ZEND_MM_SRUN_FREE_COUNTER_OFFSET 16
  155. #define ZEND_MM_NRUN_OFFSET_MASK 0x01ff0000
  156. #define ZEND_MM_NRUN_OFFSET_OFFSET 16
  157. #define ZEND_MM_LRUN_PAGES(info) (((info) & ZEND_MM_LRUN_PAGES_MASK) >> ZEND_MM_LRUN_PAGES_OFFSET)
  158. #define ZEND_MM_SRUN_BIN_NUM(info) (((info) & ZEND_MM_SRUN_BIN_NUM_MASK) >> ZEND_MM_SRUN_BIN_NUM_OFFSET)
  159. #define ZEND_MM_SRUN_FREE_COUNTER(info) (((info) & ZEND_MM_SRUN_FREE_COUNTER_MASK) >> ZEND_MM_SRUN_FREE_COUNTER_OFFSET)
  160. #define ZEND_MM_NRUN_OFFSET(info) (((info) & ZEND_MM_NRUN_OFFSET_MASK) >> ZEND_MM_NRUN_OFFSET_OFFSET)
  161. #define ZEND_MM_FRUN() ZEND_MM_IS_FRUN
  162. #define ZEND_MM_LRUN(count) (ZEND_MM_IS_LRUN | ((count) << ZEND_MM_LRUN_PAGES_OFFSET))
  163. #define ZEND_MM_SRUN(bin_num) (ZEND_MM_IS_SRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET))
  164. #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))
  165. #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))
  166. #define ZEND_MM_BINS 30
  167. typedef struct _zend_mm_page zend_mm_page;
  168. typedef struct _zend_mm_bin zend_mm_bin;
  169. typedef struct _zend_mm_free_slot zend_mm_free_slot;
  170. typedef struct _zend_mm_chunk zend_mm_chunk;
  171. typedef struct _zend_mm_huge_list zend_mm_huge_list;
  172. int zend_mm_use_huge_pages = 0;
  173. /*
  174. * Memory is retrieved from OS by chunks of fixed size 2MB.
  175. * Inside chunk it's managed by pages of fixed size 4096B.
  176. * So each chunk consists from 512 pages.
  177. * The first page of each chunk is reserved for chunk header.
  178. * It contains service information about all pages.
  179. *
  180. * free_pages - current number of free pages in this chunk
  181. *
  182. * free_tail - number of continuous free pages at the end of chunk
  183. *
  184. * free_map - bitset (a bit for each page). The bit is set if the corresponding
  185. * page is allocated. Allocator for "lage sizes" may easily find a
  186. * free page (or a continuous number of pages) searching for zero
  187. * bits.
  188. *
  189. * map - contains service information for each page. (32-bits for each
  190. * page).
  191. * usage:
  192. * (2 bits)
  193. * FRUN - free page,
  194. * LRUN - first page of "large" allocation
  195. * SRUN - first page of a bin used for "small" allocation
  196. *
  197. * lrun_pages:
  198. * (10 bits) number of allocated pages
  199. *
  200. * srun_bin_num:
  201. * (5 bits) bin number (e.g. 0 for sizes 0-2, 1 for 3-4,
  202. * 2 for 5-8, 3 for 9-16 etc) see zend_alloc_sizes.h
  203. */
  204. struct _zend_mm_heap {
  205. #if ZEND_MM_CUSTOM
  206. int use_custom_heap;
  207. #endif
  208. #if ZEND_MM_STORAGE
  209. zend_mm_storage *storage;
  210. #endif
  211. #if ZEND_MM_STAT
  212. size_t size; /* current memory usage */
  213. size_t peak; /* peak memory usage */
  214. #endif
  215. zend_mm_free_slot *free_slot[ZEND_MM_BINS]; /* free lists for small sizes */
  216. #if ZEND_MM_STAT || ZEND_MM_LIMIT
  217. size_t real_size; /* current size of allocated pages */
  218. #endif
  219. #if ZEND_MM_STAT
  220. size_t real_peak; /* peak size of allocated pages */
  221. #endif
  222. #if ZEND_MM_LIMIT
  223. size_t limit; /* memory limit */
  224. int overflow; /* memory overflow flag */
  225. #endif
  226. zend_mm_huge_list *huge_list; /* list of huge allocated blocks */
  227. zend_mm_chunk *main_chunk;
  228. zend_mm_chunk *cached_chunks; /* list of unused chunks */
  229. int chunks_count; /* number of allocated chunks */
  230. int peak_chunks_count; /* peak number of allocated chunks for current request */
  231. int cached_chunks_count; /* number of cached chunks */
  232. double avg_chunks_count; /* average number of chunks allocated per request */
  233. int last_chunks_delete_boundary; /* number of chunks after last deletion */
  234. int last_chunks_delete_count; /* number of deletion over the last boundary */
  235. #if ZEND_MM_CUSTOM
  236. union {
  237. struct {
  238. void *(*_malloc)(size_t);
  239. void (*_free)(void*);
  240. void *(*_realloc)(void*, size_t);
  241. } std;
  242. struct {
  243. void *(*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  244. void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  245. void *(*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  246. } debug;
  247. } custom_heap;
  248. HashTable *tracked_allocs;
  249. #endif
  250. };
  251. struct _zend_mm_chunk {
  252. zend_mm_heap *heap;
  253. zend_mm_chunk *next;
  254. zend_mm_chunk *prev;
  255. uint32_t free_pages; /* number of free pages */
  256. uint32_t free_tail; /* number of free pages at the end of chunk */
  257. uint32_t num;
  258. char reserve[64 - (sizeof(void*) * 3 + sizeof(uint32_t) * 3)];
  259. zend_mm_heap heap_slot; /* used only in main chunk */
  260. zend_mm_page_map free_map; /* 512 bits or 64 bytes */
  261. zend_mm_page_info map[ZEND_MM_PAGES]; /* 2 KB = 512 * 4 */
  262. };
  263. struct _zend_mm_page {
  264. char bytes[ZEND_MM_PAGE_SIZE];
  265. };
  266. /*
  267. * bin - is one or few continuous pages (up to 8) used for allocation of
  268. * a particular "small size".
  269. */
  270. struct _zend_mm_bin {
  271. char bytes[ZEND_MM_PAGE_SIZE * 8];
  272. };
  273. struct _zend_mm_free_slot {
  274. zend_mm_free_slot *next_free_slot;
  275. };
  276. struct _zend_mm_huge_list {
  277. void *ptr;
  278. size_t size;
  279. zend_mm_huge_list *next;
  280. #if ZEND_DEBUG
  281. zend_mm_debug_info dbg;
  282. #endif
  283. };
  284. #define ZEND_MM_PAGE_ADDR(chunk, page_num) \
  285. ((void*)(((zend_mm_page*)(chunk)) + (page_num)))
  286. #define _BIN_DATA_SIZE(num, size, elements, pages, x, y) size,
  287. static const uint32_t bin_data_size[] = {
  288. ZEND_MM_BINS_INFO(_BIN_DATA_SIZE, x, y)
  289. };
  290. #define _BIN_DATA_ELEMENTS(num, size, elements, pages, x, y) elements,
  291. static const uint32_t bin_elements[] = {
  292. ZEND_MM_BINS_INFO(_BIN_DATA_ELEMENTS, x, y)
  293. };
  294. #define _BIN_DATA_PAGES(num, size, elements, pages, x, y) pages,
  295. static const uint32_t bin_pages[] = {
  296. ZEND_MM_BINS_INFO(_BIN_DATA_PAGES, x, y)
  297. };
  298. #if ZEND_DEBUG
  299. ZEND_COLD void zend_debug_alloc_output(char *format, ...)
  300. {
  301. char output_buf[256];
  302. va_list args;
  303. va_start(args, format);
  304. vsprintf(output_buf, format, args);
  305. va_end(args);
  306. #ifdef ZEND_WIN32
  307. OutputDebugString(output_buf);
  308. #else
  309. fprintf(stderr, "%s", output_buf);
  310. #endif
  311. }
  312. #endif
  313. static ZEND_COLD ZEND_NORETURN void zend_mm_panic(const char *message)
  314. {
  315. fprintf(stderr, "%s\n", message);
  316. /* See http://support.microsoft.com/kb/190351 */
  317. #ifdef ZEND_WIN32
  318. fflush(stderr);
  319. #endif
  320. #if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
  321. kill(getpid(), SIGSEGV);
  322. #endif
  323. exit(1);
  324. }
  325. static ZEND_COLD ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap,
  326. const char *format,
  327. size_t limit,
  328. #if ZEND_DEBUG
  329. const char *filename,
  330. uint32_t lineno,
  331. #endif
  332. size_t size)
  333. {
  334. heap->overflow = 1;
  335. zend_try {
  336. zend_error_noreturn(E_ERROR,
  337. format,
  338. limit,
  339. #if ZEND_DEBUG
  340. filename,
  341. lineno,
  342. #endif
  343. size);
  344. } zend_catch {
  345. } zend_end_try();
  346. heap->overflow = 0;
  347. zend_bailout();
  348. exit(1);
  349. }
  350. #ifdef _WIN32
  351. void
  352. stderr_last_error(char *msg)
  353. {
  354. DWORD err = GetLastError();
  355. char *buf = php_win32_error_to_msg(err);
  356. if (!buf[0]) {
  357. fprintf(stderr, "\n%s: [0x%08lx]\n", msg, err);
  358. }
  359. else {
  360. fprintf(stderr, "\n%s: [0x%08lx] %s\n", msg, err, buf);
  361. }
  362. php_win32_error_msg_free(buf);
  363. }
  364. #endif
  365. /*****************/
  366. /* OS Allocation */
  367. /*****************/
  368. #ifndef HAVE_MREMAP
  369. static void *zend_mm_mmap_fixed(void *addr, size_t size)
  370. {
  371. #ifdef _WIN32
  372. return VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  373. #else
  374. int flags = MAP_PRIVATE | MAP_ANON;
  375. #if defined(MAP_EXCL)
  376. flags |= MAP_FIXED | MAP_EXCL;
  377. #endif
  378. /* MAP_FIXED leads to discarding of the old mapping, so it can't be used. */
  379. void *ptr = mmap(addr, size, PROT_READ | PROT_WRITE, flags /*| MAP_POPULATE | MAP_HUGETLB*/, ZEND_MM_FD, 0);
  380. if (ptr == MAP_FAILED) {
  381. #if ZEND_MM_ERROR && !defined(MAP_EXCL)
  382. fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno));
  383. #endif
  384. return NULL;
  385. } else if (ptr != addr) {
  386. if (munmap(ptr, size) != 0) {
  387. #if ZEND_MM_ERROR
  388. fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
  389. #endif
  390. }
  391. return NULL;
  392. }
  393. return ptr;
  394. #endif
  395. }
  396. #endif
  397. static void *zend_mm_mmap(size_t size)
  398. {
  399. #ifdef _WIN32
  400. void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  401. if (ptr == NULL) {
  402. #if ZEND_MM_ERROR
  403. stderr_last_error("VirtualAlloc() failed");
  404. #endif
  405. return NULL;
  406. }
  407. return ptr;
  408. #else
  409. void *ptr;
  410. #ifdef MAP_HUGETLB
  411. if (zend_mm_use_huge_pages && size == ZEND_MM_CHUNK_SIZE) {
  412. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_HUGETLB, -1, 0);
  413. if (ptr != MAP_FAILED) {
  414. return ptr;
  415. }
  416. }
  417. #endif
  418. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, ZEND_MM_FD, 0);
  419. if (ptr == MAP_FAILED) {
  420. #if ZEND_MM_ERROR
  421. fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno));
  422. #endif
  423. return NULL;
  424. }
  425. return ptr;
  426. #endif
  427. }
  428. static void zend_mm_munmap(void *addr, size_t size)
  429. {
  430. #ifdef _WIN32
  431. if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
  432. #if ZEND_MM_ERROR
  433. stderr_last_error("VirtualFree() failed");
  434. #endif
  435. }
  436. #else
  437. if (munmap(addr, size) != 0) {
  438. #if ZEND_MM_ERROR
  439. fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
  440. #endif
  441. }
  442. #endif
  443. }
  444. /***********/
  445. /* Bitmask */
  446. /***********/
  447. /* number of trailing set (1) bits */
  448. static zend_always_inline int zend_mm_bitset_nts(zend_mm_bitset bitset)
  449. {
  450. #if (defined(__GNUC__) || __has_builtin(__builtin_ctzl)) && SIZEOF_ZEND_LONG == SIZEOF_LONG && defined(PHP_HAVE_BUILTIN_CTZL)
  451. return __builtin_ctzl(~bitset);
  452. #elif (defined(__GNUC__) || __has_builtin(__builtin_ctzll)) && defined(PHP_HAVE_BUILTIN_CTZLL)
  453. return __builtin_ctzll(~bitset);
  454. #elif defined(_WIN32)
  455. unsigned long index;
  456. #if defined(_WIN64)
  457. if (!BitScanForward64(&index, ~bitset)) {
  458. #else
  459. if (!BitScanForward(&index, ~bitset)) {
  460. #endif
  461. /* undefined behavior */
  462. return 32;
  463. }
  464. return (int)index;
  465. #else
  466. int n;
  467. if (bitset == (zend_mm_bitset)-1) return ZEND_MM_BITSET_LEN;
  468. n = 0;
  469. #if SIZEOF_ZEND_LONG == 8
  470. if (sizeof(zend_mm_bitset) == 8) {
  471. if ((bitset & 0xffffffff) == 0xffffffff) {n += 32; bitset = bitset >> Z_UL(32);}
  472. }
  473. #endif
  474. if ((bitset & 0x0000ffff) == 0x0000ffff) {n += 16; bitset = bitset >> 16;}
  475. if ((bitset & 0x000000ff) == 0x000000ff) {n += 8; bitset = bitset >> 8;}
  476. if ((bitset & 0x0000000f) == 0x0000000f) {n += 4; bitset = bitset >> 4;}
  477. if ((bitset & 0x00000003) == 0x00000003) {n += 2; bitset = bitset >> 2;}
  478. return n + (bitset & 1);
  479. #endif
  480. }
  481. static zend_always_inline int zend_mm_bitset_is_set(zend_mm_bitset *bitset, int bit)
  482. {
  483. return ZEND_BIT_TEST(bitset, bit);
  484. }
  485. static zend_always_inline void zend_mm_bitset_set_bit(zend_mm_bitset *bitset, int bit)
  486. {
  487. bitset[bit / ZEND_MM_BITSET_LEN] |= (Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
  488. }
  489. static zend_always_inline void zend_mm_bitset_reset_bit(zend_mm_bitset *bitset, int bit)
  490. {
  491. bitset[bit / ZEND_MM_BITSET_LEN] &= ~(Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
  492. }
  493. static zend_always_inline void zend_mm_bitset_set_range(zend_mm_bitset *bitset, int start, int len)
  494. {
  495. if (len == 1) {
  496. zend_mm_bitset_set_bit(bitset, start);
  497. } else {
  498. int pos = start / ZEND_MM_BITSET_LEN;
  499. int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
  500. int bit = start & (ZEND_MM_BITSET_LEN - 1);
  501. zend_mm_bitset tmp;
  502. if (pos != end) {
  503. /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */
  504. tmp = (zend_mm_bitset)-1 << bit;
  505. bitset[pos++] |= tmp;
  506. while (pos != end) {
  507. /* set all bits */
  508. bitset[pos++] = (zend_mm_bitset)-1;
  509. }
  510. end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
  511. /* set bits from "0" to "end" */
  512. tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
  513. bitset[pos] |= tmp;
  514. } else {
  515. end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
  516. /* set bits from "bit" to "end" */
  517. tmp = (zend_mm_bitset)-1 << bit;
  518. tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
  519. bitset[pos] |= tmp;
  520. }
  521. }
  522. }
  523. static zend_always_inline void zend_mm_bitset_reset_range(zend_mm_bitset *bitset, int start, int len)
  524. {
  525. if (len == 1) {
  526. zend_mm_bitset_reset_bit(bitset, start);
  527. } else {
  528. int pos = start / ZEND_MM_BITSET_LEN;
  529. int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
  530. int bit = start & (ZEND_MM_BITSET_LEN - 1);
  531. zend_mm_bitset tmp;
  532. if (pos != end) {
  533. /* reset bits from "bit" to ZEND_MM_BITSET_LEN-1 */
  534. tmp = ~((Z_UL(1) << bit) - 1);
  535. bitset[pos++] &= ~tmp;
  536. while (pos != end) {
  537. /* set all bits */
  538. bitset[pos++] = 0;
  539. }
  540. end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
  541. /* reset bits from "0" to "end" */
  542. tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
  543. bitset[pos] &= ~tmp;
  544. } else {
  545. end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
  546. /* reset bits from "bit" to "end" */
  547. tmp = (zend_mm_bitset)-1 << bit;
  548. tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
  549. bitset[pos] &= ~tmp;
  550. }
  551. }
  552. }
  553. static zend_always_inline int zend_mm_bitset_is_free_range(zend_mm_bitset *bitset, int start, int len)
  554. {
  555. if (len == 1) {
  556. return !zend_mm_bitset_is_set(bitset, start);
  557. } else {
  558. int pos = start / ZEND_MM_BITSET_LEN;
  559. int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
  560. int bit = start & (ZEND_MM_BITSET_LEN - 1);
  561. zend_mm_bitset tmp;
  562. if (pos != end) {
  563. /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */
  564. tmp = (zend_mm_bitset)-1 << bit;
  565. if ((bitset[pos++] & tmp) != 0) {
  566. return 0;
  567. }
  568. while (pos != end) {
  569. /* set all bits */
  570. if (bitset[pos++] != 0) {
  571. return 0;
  572. }
  573. }
  574. end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
  575. /* set bits from "0" to "end" */
  576. tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
  577. return (bitset[pos] & tmp) == 0;
  578. } else {
  579. end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
  580. /* set bits from "bit" to "end" */
  581. tmp = (zend_mm_bitset)-1 << bit;
  582. tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
  583. return (bitset[pos] & tmp) == 0;
  584. }
  585. }
  586. }
  587. /**********/
  588. /* Chunks */
  589. /**********/
  590. static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment)
  591. {
  592. void *ptr = zend_mm_mmap(size);
  593. if (ptr == NULL) {
  594. return NULL;
  595. } else if (ZEND_MM_ALIGNED_OFFSET(ptr, alignment) == 0) {
  596. #ifdef MADV_HUGEPAGE
  597. if (zend_mm_use_huge_pages) {
  598. madvise(ptr, size, MADV_HUGEPAGE);
  599. }
  600. #endif
  601. return ptr;
  602. } else {
  603. size_t offset;
  604. /* chunk has to be aligned */
  605. zend_mm_munmap(ptr, size);
  606. ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE);
  607. #ifdef _WIN32
  608. offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
  609. zend_mm_munmap(ptr, size + alignment - REAL_PAGE_SIZE);
  610. ptr = zend_mm_mmap_fixed((void*)((char*)ptr + (alignment - offset)), size);
  611. offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
  612. if (offset != 0) {
  613. zend_mm_munmap(ptr, size);
  614. return NULL;
  615. }
  616. return ptr;
  617. #else
  618. offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
  619. if (offset != 0) {
  620. offset = alignment - offset;
  621. zend_mm_munmap(ptr, offset);
  622. ptr = (char*)ptr + offset;
  623. alignment -= offset;
  624. }
  625. if (alignment > REAL_PAGE_SIZE) {
  626. zend_mm_munmap((char*)ptr + size, alignment - REAL_PAGE_SIZE);
  627. }
  628. # ifdef MADV_HUGEPAGE
  629. if (zend_mm_use_huge_pages) {
  630. madvise(ptr, size, MADV_HUGEPAGE);
  631. }
  632. # endif
  633. #endif
  634. return ptr;
  635. }
  636. }
  637. static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignment)
  638. {
  639. #if ZEND_MM_STORAGE
  640. if (UNEXPECTED(heap->storage)) {
  641. void *ptr = heap->storage->handlers.chunk_alloc(heap->storage, size, alignment);
  642. ZEND_ASSERT(((zend_uintptr_t)((char*)ptr + (alignment-1)) & (alignment-1)) == (zend_uintptr_t)ptr);
  643. return ptr;
  644. }
  645. #endif
  646. return zend_mm_chunk_alloc_int(size, alignment);
  647. }
  648. static void zend_mm_chunk_free(zend_mm_heap *heap, void *addr, size_t size)
  649. {
  650. #if ZEND_MM_STORAGE
  651. if (UNEXPECTED(heap->storage)) {
  652. heap->storage->handlers.chunk_free(heap->storage, addr, size);
  653. return;
  654. }
  655. #endif
  656. zend_mm_munmap(addr, size);
  657. }
  658. static int zend_mm_chunk_truncate(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size)
  659. {
  660. #if ZEND_MM_STORAGE
  661. if (UNEXPECTED(heap->storage)) {
  662. if (heap->storage->handlers.chunk_truncate) {
  663. return heap->storage->handlers.chunk_truncate(heap->storage, addr, old_size, new_size);
  664. } else {
  665. return 0;
  666. }
  667. }
  668. #endif
  669. #ifndef _WIN32
  670. zend_mm_munmap((char*)addr + new_size, old_size - new_size);
  671. return 1;
  672. #else
  673. return 0;
  674. #endif
  675. }
  676. static int zend_mm_chunk_extend(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size)
  677. {
  678. #if ZEND_MM_STORAGE
  679. if (UNEXPECTED(heap->storage)) {
  680. if (heap->storage->handlers.chunk_extend) {
  681. return heap->storage->handlers.chunk_extend(heap->storage, addr, old_size, new_size);
  682. } else {
  683. return 0;
  684. }
  685. }
  686. #endif
  687. #ifdef HAVE_MREMAP
  688. /* We don't use MREMAP_MAYMOVE due to alignment requirements. */
  689. void *ptr = mremap(addr, old_size, new_size, 0);
  690. if (ptr == MAP_FAILED) {
  691. return 0;
  692. }
  693. /* Sanity check: The mapping shouldn't have moved. */
  694. ZEND_ASSERT(ptr == addr);
  695. return 1;
  696. #elif !defined(_WIN32)
  697. return (zend_mm_mmap_fixed((char*)addr + old_size, new_size - old_size) != NULL);
  698. #else
  699. return 0;
  700. #endif
  701. }
  702. static zend_always_inline void zend_mm_chunk_init(zend_mm_heap *heap, zend_mm_chunk *chunk)
  703. {
  704. chunk->heap = heap;
  705. chunk->next = heap->main_chunk;
  706. chunk->prev = heap->main_chunk->prev;
  707. chunk->prev->next = chunk;
  708. chunk->next->prev = chunk;
  709. /* mark first pages as allocated */
  710. chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
  711. chunk->free_tail = ZEND_MM_FIRST_PAGE;
  712. /* the younger chunks have bigger number */
  713. chunk->num = chunk->prev->num + 1;
  714. /* mark first pages as allocated */
  715. chunk->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1;
  716. chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
  717. }
  718. /***********************/
  719. /* Huge Runs (forward) */
  720. /***********************/
  721. static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  722. static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  723. static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  724. #if ZEND_DEBUG
  725. 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);
  726. #else
  727. 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);
  728. #endif
  729. /**************/
  730. /* Large Runs */
  731. /**************/
  732. #if ZEND_DEBUG
  733. 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)
  734. #else
  735. static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
  736. #endif
  737. {
  738. zend_mm_chunk *chunk = heap->main_chunk;
  739. uint32_t page_num, len;
  740. int steps = 0;
  741. while (1) {
  742. if (UNEXPECTED(chunk->free_pages < pages_count)) {
  743. goto not_found;
  744. #if 0
  745. } else if (UNEXPECTED(chunk->free_pages + chunk->free_tail == ZEND_MM_PAGES)) {
  746. if (UNEXPECTED(ZEND_MM_PAGES - chunk->free_tail < pages_count)) {
  747. goto not_found;
  748. } else {
  749. page_num = chunk->free_tail;
  750. goto found;
  751. }
  752. } else if (0) {
  753. /* First-Fit Search */
  754. int free_tail = chunk->free_tail;
  755. zend_mm_bitset *bitset = chunk->free_map;
  756. zend_mm_bitset tmp = *(bitset++);
  757. int i = 0;
  758. while (1) {
  759. /* skip allocated blocks */
  760. while (tmp == (zend_mm_bitset)-1) {
  761. i += ZEND_MM_BITSET_LEN;
  762. if (i == ZEND_MM_PAGES) {
  763. goto not_found;
  764. }
  765. tmp = *(bitset++);
  766. }
  767. /* find first 0 bit */
  768. page_num = i + zend_mm_bitset_nts(tmp);
  769. /* reset bits from 0 to "bit" */
  770. tmp &= tmp + 1;
  771. /* skip free blocks */
  772. while (tmp == 0) {
  773. i += ZEND_MM_BITSET_LEN;
  774. len = i - page_num;
  775. if (len >= pages_count) {
  776. goto found;
  777. } else if (i >= free_tail) {
  778. goto not_found;
  779. }
  780. tmp = *(bitset++);
  781. }
  782. /* find first 1 bit */
  783. len = (i + zend_ulong_ntz(tmp)) - page_num;
  784. if (len >= pages_count) {
  785. goto found;
  786. }
  787. /* set bits from 0 to "bit" */
  788. tmp |= tmp - 1;
  789. }
  790. #endif
  791. } else {
  792. /* Best-Fit Search */
  793. int best = -1;
  794. uint32_t best_len = ZEND_MM_PAGES;
  795. uint32_t free_tail = chunk->free_tail;
  796. zend_mm_bitset *bitset = chunk->free_map;
  797. zend_mm_bitset tmp = *(bitset++);
  798. uint32_t i = 0;
  799. while (1) {
  800. /* skip allocated blocks */
  801. while (tmp == (zend_mm_bitset)-1) {
  802. i += ZEND_MM_BITSET_LEN;
  803. if (i == ZEND_MM_PAGES) {
  804. if (best > 0) {
  805. page_num = best;
  806. goto found;
  807. } else {
  808. goto not_found;
  809. }
  810. }
  811. tmp = *(bitset++);
  812. }
  813. /* find first 0 bit */
  814. page_num = i + zend_mm_bitset_nts(tmp);
  815. /* reset bits from 0 to "bit" */
  816. tmp &= tmp + 1;
  817. /* skip free blocks */
  818. while (tmp == 0) {
  819. i += ZEND_MM_BITSET_LEN;
  820. if (i >= free_tail || i == ZEND_MM_PAGES) {
  821. len = ZEND_MM_PAGES - page_num;
  822. if (len >= pages_count && len < best_len) {
  823. chunk->free_tail = page_num + pages_count;
  824. goto found;
  825. } else {
  826. /* set accurate value */
  827. chunk->free_tail = page_num;
  828. if (best > 0) {
  829. page_num = best;
  830. goto found;
  831. } else {
  832. goto not_found;
  833. }
  834. }
  835. }
  836. tmp = *(bitset++);
  837. }
  838. /* find first 1 bit */
  839. len = i + zend_ulong_ntz(tmp) - page_num;
  840. if (len >= pages_count) {
  841. if (len == pages_count) {
  842. goto found;
  843. } else if (len < best_len) {
  844. best_len = len;
  845. best = page_num;
  846. }
  847. }
  848. /* set bits from 0 to "bit" */
  849. tmp |= tmp - 1;
  850. }
  851. }
  852. not_found:
  853. if (chunk->next == heap->main_chunk) {
  854. get_chunk:
  855. if (heap->cached_chunks) {
  856. heap->cached_chunks_count--;
  857. chunk = heap->cached_chunks;
  858. heap->cached_chunks = chunk->next;
  859. } else {
  860. #if ZEND_MM_LIMIT
  861. if (UNEXPECTED(ZEND_MM_CHUNK_SIZE > heap->limit - heap->real_size)) {
  862. if (zend_mm_gc(heap)) {
  863. goto get_chunk;
  864. } else if (heap->overflow == 0) {
  865. #if ZEND_DEBUG
  866. 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);
  867. #else
  868. 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);
  869. #endif
  870. return NULL;
  871. }
  872. }
  873. #endif
  874. chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
  875. if (UNEXPECTED(chunk == NULL)) {
  876. /* insufficient memory */
  877. if (zend_mm_gc(heap) &&
  878. (chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE)) != NULL) {
  879. /* pass */
  880. } else {
  881. #if !ZEND_MM_LIMIT
  882. zend_mm_safe_error(heap, "Out of memory");
  883. #elif ZEND_DEBUG
  884. 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);
  885. #else
  886. zend_mm_safe_error(heap, "Out of memory (allocated %zu) (tried to allocate %zu bytes)", heap->real_size, ZEND_MM_PAGE_SIZE * pages_count);
  887. #endif
  888. return NULL;
  889. }
  890. }
  891. #if ZEND_MM_STAT
  892. do {
  893. size_t size = heap->real_size + ZEND_MM_CHUNK_SIZE;
  894. size_t peak = MAX(heap->real_peak, size);
  895. heap->real_size = size;
  896. heap->real_peak = peak;
  897. } while (0);
  898. #elif ZEND_MM_LIMIT
  899. heap->real_size += ZEND_MM_CHUNK_SIZE;
  900. #endif
  901. }
  902. heap->chunks_count++;
  903. if (heap->chunks_count > heap->peak_chunks_count) {
  904. heap->peak_chunks_count = heap->chunks_count;
  905. }
  906. zend_mm_chunk_init(heap, chunk);
  907. page_num = ZEND_MM_FIRST_PAGE;
  908. len = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
  909. goto found;
  910. } else {
  911. chunk = chunk->next;
  912. steps++;
  913. }
  914. }
  915. found:
  916. if (steps > 2 && pages_count < 8) {
  917. /* move chunk into the head of the linked-list */
  918. chunk->prev->next = chunk->next;
  919. chunk->next->prev = chunk->prev;
  920. chunk->next = heap->main_chunk->next;
  921. chunk->prev = heap->main_chunk;
  922. chunk->prev->next = chunk;
  923. chunk->next->prev = chunk;
  924. }
  925. /* mark run as allocated */
  926. chunk->free_pages -= pages_count;
  927. zend_mm_bitset_set_range(chunk->free_map, page_num, pages_count);
  928. chunk->map[page_num] = ZEND_MM_LRUN(pages_count);
  929. if (page_num == chunk->free_tail) {
  930. chunk->free_tail = page_num + pages_count;
  931. }
  932. return ZEND_MM_PAGE_ADDR(chunk, page_num);
  933. }
  934. 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)
  935. {
  936. int pages_count = (int)ZEND_MM_SIZE_TO_NUM(size, ZEND_MM_PAGE_SIZE);
  937. #if ZEND_DEBUG
  938. void *ptr = zend_mm_alloc_pages(heap, pages_count, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  939. #else
  940. void *ptr = zend_mm_alloc_pages(heap, pages_count ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  941. #endif
  942. #if ZEND_MM_STAT
  943. do {
  944. size_t size = heap->size + pages_count * ZEND_MM_PAGE_SIZE;
  945. size_t peak = MAX(heap->peak, size);
  946. heap->size = size;
  947. heap->peak = peak;
  948. } while (0);
  949. #endif
  950. return ptr;
  951. }
  952. #if ZEND_DEBUG
  953. 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)
  954. {
  955. return zend_mm_alloc_large_ex(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  956. }
  957. #else
  958. 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)
  959. {
  960. return zend_mm_alloc_large_ex(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  961. }
  962. #endif
  963. static zend_always_inline void zend_mm_delete_chunk(zend_mm_heap *heap, zend_mm_chunk *chunk)
  964. {
  965. chunk->next->prev = chunk->prev;
  966. chunk->prev->next = chunk->next;
  967. heap->chunks_count--;
  968. if (heap->chunks_count + heap->cached_chunks_count < heap->avg_chunks_count + 0.1
  969. || (heap->chunks_count == heap->last_chunks_delete_boundary
  970. && heap->last_chunks_delete_count >= 4)) {
  971. /* delay deletion */
  972. heap->cached_chunks_count++;
  973. chunk->next = heap->cached_chunks;
  974. heap->cached_chunks = chunk;
  975. } else {
  976. #if ZEND_MM_STAT || ZEND_MM_LIMIT
  977. heap->real_size -= ZEND_MM_CHUNK_SIZE;
  978. #endif
  979. if (!heap->cached_chunks) {
  980. if (heap->chunks_count != heap->last_chunks_delete_boundary) {
  981. heap->last_chunks_delete_boundary = heap->chunks_count;
  982. heap->last_chunks_delete_count = 0;
  983. } else {
  984. heap->last_chunks_delete_count++;
  985. }
  986. }
  987. if (!heap->cached_chunks || chunk->num > heap->cached_chunks->num) {
  988. zend_mm_chunk_free(heap, chunk, ZEND_MM_CHUNK_SIZE);
  989. } else {
  990. //TODO: select the best chunk to delete???
  991. chunk->next = heap->cached_chunks->next;
  992. zend_mm_chunk_free(heap, heap->cached_chunks, ZEND_MM_CHUNK_SIZE);
  993. heap->cached_chunks = chunk;
  994. }
  995. }
  996. }
  997. 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)
  998. {
  999. chunk->free_pages += pages_count;
  1000. zend_mm_bitset_reset_range(chunk->free_map, page_num, pages_count);
  1001. chunk->map[page_num] = 0;
  1002. if (chunk->free_tail == page_num + pages_count) {
  1003. /* this setting may be not accurate */
  1004. chunk->free_tail = page_num;
  1005. }
  1006. if (free_chunk && chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE) {
  1007. zend_mm_delete_chunk(heap, chunk);
  1008. }
  1009. }
  1010. static zend_never_inline void zend_mm_free_pages(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count)
  1011. {
  1012. zend_mm_free_pages_ex(heap, chunk, page_num, pages_count, 1);
  1013. }
  1014. static zend_always_inline void zend_mm_free_large(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count)
  1015. {
  1016. #if ZEND_MM_STAT
  1017. heap->size -= pages_count * ZEND_MM_PAGE_SIZE;
  1018. #endif
  1019. zend_mm_free_pages(heap, chunk, page_num, pages_count);
  1020. }
  1021. /**************/
  1022. /* Small Runs */
  1023. /**************/
  1024. /* higher set bit number (0->N/A, 1->1, 2->2, 4->3, 8->4, 127->7, 128->8 etc) */
  1025. static zend_always_inline int zend_mm_small_size_to_bit(int size)
  1026. {
  1027. #if (defined(__GNUC__) || __has_builtin(__builtin_clz)) && defined(PHP_HAVE_BUILTIN_CLZ)
  1028. return (__builtin_clz(size) ^ 0x1f) + 1;
  1029. #elif defined(_WIN32)
  1030. unsigned long index;
  1031. if (!BitScanReverse(&index, (unsigned long)size)) {
  1032. /* undefined behavior */
  1033. return 64;
  1034. }
  1035. return (((31 - (int)index) ^ 0x1f) + 1);
  1036. #else
  1037. int n = 16;
  1038. if (size <= 0x00ff) {n -= 8; size = size << 8;}
  1039. if (size <= 0x0fff) {n -= 4; size = size << 4;}
  1040. if (size <= 0x3fff) {n -= 2; size = size << 2;}
  1041. if (size <= 0x7fff) {n -= 1;}
  1042. return n;
  1043. #endif
  1044. }
  1045. #ifndef MAX
  1046. # define MAX(a, b) (((a) > (b)) ? (a) : (b))
  1047. #endif
  1048. #ifndef MIN
  1049. # define MIN(a, b) (((a) < (b)) ? (a) : (b))
  1050. #endif
  1051. static zend_always_inline int zend_mm_small_size_to_bin(size_t size)
  1052. {
  1053. #if 0
  1054. int n;
  1055. /*0, 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12*/
  1056. static const int f1[] = { 3, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9};
  1057. static const int f2[] = { 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24};
  1058. if (UNEXPECTED(size <= 2)) return 0;
  1059. n = zend_mm_small_size_to_bit(size - 1);
  1060. return ((size-1) >> f1[n]) + f2[n];
  1061. #else
  1062. unsigned int t1, t2;
  1063. if (size <= 64) {
  1064. /* we need to support size == 0 ... */
  1065. return (size - !!size) >> 3;
  1066. } else {
  1067. t1 = size - 1;
  1068. t2 = zend_mm_small_size_to_bit(t1) - 3;
  1069. t1 = t1 >> t2;
  1070. t2 = t2 - 3;
  1071. t2 = t2 << 2;
  1072. return (int)(t1 + t2);
  1073. }
  1074. #endif
  1075. }
  1076. #define ZEND_MM_SMALL_SIZE_TO_BIN(size) zend_mm_small_size_to_bin(size)
  1077. 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)
  1078. {
  1079. zend_mm_chunk *chunk;
  1080. int page_num;
  1081. zend_mm_bin *bin;
  1082. zend_mm_free_slot *p, *end;
  1083. #if ZEND_DEBUG
  1084. 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);
  1085. #else
  1086. bin = (zend_mm_bin*)zend_mm_alloc_pages(heap, bin_pages[bin_num] ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1087. #endif
  1088. if (UNEXPECTED(bin == NULL)) {
  1089. /* insufficient memory */
  1090. return NULL;
  1091. }
  1092. chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(bin, ZEND_MM_CHUNK_SIZE);
  1093. page_num = ZEND_MM_ALIGNED_OFFSET(bin, ZEND_MM_CHUNK_SIZE) / ZEND_MM_PAGE_SIZE;
  1094. chunk->map[page_num] = ZEND_MM_SRUN(bin_num);
  1095. if (bin_pages[bin_num] > 1) {
  1096. uint32_t i = 1;
  1097. do {
  1098. chunk->map[page_num+i] = ZEND_MM_NRUN(bin_num, i);
  1099. i++;
  1100. } while (i < bin_pages[bin_num]);
  1101. }
  1102. /* create a linked list of elements from 1 to last */
  1103. end = (zend_mm_free_slot*)((char*)bin + (bin_data_size[bin_num] * (bin_elements[bin_num] - 1)));
  1104. heap->free_slot[bin_num] = p = (zend_mm_free_slot*)((char*)bin + bin_data_size[bin_num]);
  1105. do {
  1106. p->next_free_slot = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]);
  1107. #if ZEND_DEBUG
  1108. do {
  1109. 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)));
  1110. dbg->size = 0;
  1111. } while (0);
  1112. #endif
  1113. p = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]);
  1114. } while (p != end);
  1115. /* terminate list using NULL */
  1116. p->next_free_slot = NULL;
  1117. #if ZEND_DEBUG
  1118. do {
  1119. 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)));
  1120. dbg->size = 0;
  1121. } while (0);
  1122. #endif
  1123. /* return first element */
  1124. return (char*)bin;
  1125. }
  1126. 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)
  1127. {
  1128. #if ZEND_MM_STAT
  1129. do {
  1130. size_t size = heap->size + bin_data_size[bin_num];
  1131. size_t peak = MAX(heap->peak, size);
  1132. heap->size = size;
  1133. heap->peak = peak;
  1134. } while (0);
  1135. #endif
  1136. if (EXPECTED(heap->free_slot[bin_num] != NULL)) {
  1137. zend_mm_free_slot *p = heap->free_slot[bin_num];
  1138. heap->free_slot[bin_num] = p->next_free_slot;
  1139. return (void*)p;
  1140. } else {
  1141. return zend_mm_alloc_small_slow(heap, bin_num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1142. }
  1143. }
  1144. static zend_always_inline void zend_mm_free_small(zend_mm_heap *heap, void *ptr, int bin_num)
  1145. {
  1146. zend_mm_free_slot *p;
  1147. #if ZEND_MM_STAT
  1148. heap->size -= bin_data_size[bin_num];
  1149. #endif
  1150. #if ZEND_DEBUG
  1151. do {
  1152. 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)));
  1153. dbg->size = 0;
  1154. } while (0);
  1155. #endif
  1156. p = (zend_mm_free_slot*)ptr;
  1157. p->next_free_slot = heap->free_slot[bin_num];
  1158. heap->free_slot[bin_num] = p;
  1159. }
  1160. /********/
  1161. /* Heap */
  1162. /********/
  1163. #if ZEND_DEBUG
  1164. static zend_always_inline zend_mm_debug_info *zend_mm_get_debug_info(zend_mm_heap *heap, void *ptr)
  1165. {
  1166. size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
  1167. zend_mm_chunk *chunk;
  1168. int page_num;
  1169. zend_mm_page_info info;
  1170. ZEND_MM_CHECK(page_offset != 0, "zend_mm_heap corrupted");
  1171. chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
  1172. page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
  1173. info = chunk->map[page_num];
  1174. ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
  1175. if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
  1176. int bin_num = ZEND_MM_SRUN_BIN_NUM(info);
  1177. return (zend_mm_debug_info*)((char*)ptr + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
  1178. } else /* if (info & ZEND_MM_IS_LRUN) */ {
  1179. int pages_count = ZEND_MM_LRUN_PAGES(info);
  1180. return (zend_mm_debug_info*)((char*)ptr + ZEND_MM_PAGE_SIZE * pages_count - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
  1181. }
  1182. }
  1183. #endif
  1184. 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)
  1185. {
  1186. void *ptr;
  1187. #if ZEND_DEBUG
  1188. size_t real_size = size;
  1189. zend_mm_debug_info *dbg;
  1190. /* special handling for zero-size allocation */
  1191. size = MAX(size, 1);
  1192. size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
  1193. if (UNEXPECTED(size < real_size)) {
  1194. 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)));
  1195. return NULL;
  1196. }
  1197. #endif
  1198. if (EXPECTED(size <= ZEND_MM_MAX_SMALL_SIZE)) {
  1199. ptr = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1200. #if ZEND_DEBUG
  1201. dbg = zend_mm_get_debug_info(heap, ptr);
  1202. dbg->size = real_size;
  1203. dbg->filename = __zend_filename;
  1204. dbg->orig_filename = __zend_orig_filename;
  1205. dbg->lineno = __zend_lineno;
  1206. dbg->orig_lineno = __zend_orig_lineno;
  1207. #endif
  1208. return ptr;
  1209. } else if (EXPECTED(size <= ZEND_MM_MAX_LARGE_SIZE)) {
  1210. ptr = zend_mm_alloc_large(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1211. #if ZEND_DEBUG
  1212. dbg = zend_mm_get_debug_info(heap, ptr);
  1213. dbg->size = real_size;
  1214. dbg->filename = __zend_filename;
  1215. dbg->orig_filename = __zend_orig_filename;
  1216. dbg->lineno = __zend_lineno;
  1217. dbg->orig_lineno = __zend_orig_lineno;
  1218. #endif
  1219. return ptr;
  1220. } else {
  1221. #if ZEND_DEBUG
  1222. size = real_size;
  1223. #endif
  1224. return zend_mm_alloc_huge(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1225. }
  1226. }
  1227. static zend_always_inline void zend_mm_free_heap(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
  1228. {
  1229. size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
  1230. if (UNEXPECTED(page_offset == 0)) {
  1231. if (ptr != NULL) {
  1232. zend_mm_free_huge(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1233. }
  1234. } else {
  1235. zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
  1236. int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
  1237. zend_mm_page_info info = chunk->map[page_num];
  1238. ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
  1239. if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
  1240. zend_mm_free_small(heap, ptr, ZEND_MM_SRUN_BIN_NUM(info));
  1241. } else /* if (info & ZEND_MM_IS_LRUN) */ {
  1242. int pages_count = ZEND_MM_LRUN_PAGES(info);
  1243. ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
  1244. zend_mm_free_large(heap, chunk, page_num, pages_count);
  1245. }
  1246. }
  1247. }
  1248. static size_t zend_mm_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
  1249. {
  1250. size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
  1251. if (UNEXPECTED(page_offset == 0)) {
  1252. return zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1253. } else {
  1254. zend_mm_chunk *chunk;
  1255. #if 0 && ZEND_DEBUG
  1256. zend_mm_debug_info *dbg = zend_mm_get_debug_info(heap, ptr);
  1257. return dbg->size;
  1258. #else
  1259. int page_num;
  1260. zend_mm_page_info info;
  1261. chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
  1262. page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
  1263. info = chunk->map[page_num];
  1264. ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
  1265. if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
  1266. return bin_data_size[ZEND_MM_SRUN_BIN_NUM(info)];
  1267. } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ {
  1268. return ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE;
  1269. }
  1270. #endif
  1271. }
  1272. }
  1273. 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)
  1274. {
  1275. void *ret;
  1276. #if ZEND_MM_STAT
  1277. do {
  1278. size_t orig_peak = heap->peak;
  1279. #endif
  1280. ret = zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1281. memcpy(ret, ptr, copy_size);
  1282. zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1283. #if ZEND_MM_STAT
  1284. heap->peak = MAX(orig_peak, heap->size);
  1285. } while (0);
  1286. #endif
  1287. return ret;
  1288. }
  1289. 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)
  1290. {
  1291. size_t old_size;
  1292. size_t new_size;
  1293. #if ZEND_DEBUG
  1294. size_t real_size;
  1295. #endif
  1296. old_size = zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1297. #if ZEND_DEBUG
  1298. real_size = size;
  1299. size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
  1300. #endif
  1301. if (size > ZEND_MM_MAX_LARGE_SIZE) {
  1302. #if ZEND_DEBUG
  1303. size = real_size;
  1304. #endif
  1305. #ifdef ZEND_WIN32
  1306. /* On Windows we don't have ability to extend huge blocks in-place.
  1307. * We allocate them with 2MB size granularity, to avoid many
  1308. * reallocations when they are extended by small pieces
  1309. */
  1310. new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE));
  1311. #else
  1312. new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
  1313. #endif
  1314. if (new_size == old_size) {
  1315. #if ZEND_DEBUG
  1316. zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1317. #else
  1318. zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1319. #endif
  1320. return ptr;
  1321. } else if (new_size < old_size) {
  1322. /* unmup tail */
  1323. if (zend_mm_chunk_truncate(heap, ptr, old_size, new_size)) {
  1324. #if ZEND_MM_STAT || ZEND_MM_LIMIT
  1325. heap->real_size -= old_size - new_size;
  1326. #endif
  1327. #if ZEND_MM_STAT
  1328. heap->size -= old_size - new_size;
  1329. #endif
  1330. #if ZEND_DEBUG
  1331. zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1332. #else
  1333. zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1334. #endif
  1335. return ptr;
  1336. }
  1337. } else /* if (new_size > old_size) */ {
  1338. #if ZEND_MM_LIMIT
  1339. if (UNEXPECTED(new_size - old_size > heap->limit - heap->real_size)) {
  1340. if (zend_mm_gc(heap) && new_size - old_size <= heap->limit - heap->real_size) {
  1341. /* pass */
  1342. } else if (heap->overflow == 0) {
  1343. #if ZEND_DEBUG
  1344. 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);
  1345. #else
  1346. zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, size);
  1347. #endif
  1348. return NULL;
  1349. }
  1350. }
  1351. #endif
  1352. /* try to map tail right after this block */
  1353. if (zend_mm_chunk_extend(heap, ptr, old_size, new_size)) {
  1354. #if ZEND_MM_STAT || ZEND_MM_LIMIT
  1355. heap->real_size += new_size - old_size;
  1356. #endif
  1357. #if ZEND_MM_STAT
  1358. heap->real_peak = MAX(heap->real_peak, heap->real_size);
  1359. heap->size += new_size - old_size;
  1360. heap->peak = MAX(heap->peak, heap->size);
  1361. #endif
  1362. #if ZEND_DEBUG
  1363. zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1364. #else
  1365. zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1366. #endif
  1367. return ptr;
  1368. }
  1369. }
  1370. }
  1371. return zend_mm_realloc_slow(heap, ptr, size, MIN(old_size, copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1372. }
  1373. 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)
  1374. {
  1375. size_t page_offset;
  1376. size_t old_size;
  1377. size_t new_size;
  1378. void *ret;
  1379. #if ZEND_DEBUG
  1380. zend_mm_debug_info *dbg;
  1381. #endif
  1382. page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
  1383. if (UNEXPECTED(page_offset == 0)) {
  1384. if (EXPECTED(ptr == NULL)) {
  1385. return _zend_mm_alloc(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1386. } else {
  1387. return zend_mm_realloc_huge(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1388. }
  1389. } else {
  1390. zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
  1391. int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
  1392. zend_mm_page_info info = chunk->map[page_num];
  1393. #if ZEND_DEBUG
  1394. size_t real_size = size;
  1395. size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
  1396. #endif
  1397. ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
  1398. if (info & ZEND_MM_IS_SRUN) {
  1399. int old_bin_num = ZEND_MM_SRUN_BIN_NUM(info);
  1400. do {
  1401. old_size = bin_data_size[old_bin_num];
  1402. /* Check if requested size fits into current bin */
  1403. if (size <= old_size) {
  1404. /* Check if truncation is necessary */
  1405. if (old_bin_num > 0 && size < bin_data_size[old_bin_num - 1]) {
  1406. /* truncation */
  1407. ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1408. copy_size = use_copy_size ? MIN(size, copy_size) : size;
  1409. memcpy(ret, ptr, copy_size);
  1410. zend_mm_free_small(heap, ptr, old_bin_num);
  1411. } else {
  1412. /* reallocation in-place */
  1413. ret = ptr;
  1414. }
  1415. } else if (size <= ZEND_MM_MAX_SMALL_SIZE) {
  1416. /* small extension */
  1417. #if ZEND_MM_STAT
  1418. do {
  1419. size_t orig_peak = heap->peak;
  1420. #endif
  1421. ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
  1422. copy_size = use_copy_size ? MIN(old_size, copy_size) : old_size;
  1423. memcpy(ret, ptr, copy_size);
  1424. zend_mm_free_small(heap, ptr, old_bin_num);
  1425. #if ZEND_MM_STAT
  1426. heap->peak = MAX(orig_peak, heap->size);
  1427. } while (0);
  1428. #endif
  1429. } else {
  1430. /* slow reallocation */
  1431. break;
  1432. }
  1433. #if ZEND_DEBUG
  1434. dbg = zend_mm_get_debug_info(heap, ret);
  1435. dbg->size = real_size;
  1436. dbg->filename = __zend_filename;
  1437. dbg->orig_filename = __zend_orig_filename;
  1438. dbg->lineno = __zend_lineno;
  1439. dbg->orig_lineno = __zend_orig_lineno;
  1440. #endif
  1441. return ret;
  1442. } while (0);
  1443. } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ {
  1444. ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
  1445. old_size = ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE;
  1446. if (size > ZEND_MM_MAX_SMALL_SIZE &&

Large files files are truncated, but you can click here to view the full file