PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_alloc.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 498 696 71.6 %
Date: 2014-12-13 Functions: 63 115 54.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 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@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : /*
      24             :  * zend_alloc is designed to be a modern CPU cache friendly memory manager
      25             :  * for PHP. Most ideas are taken from jemalloc and tcmalloc implementations.
      26             :  *
      27             :  * All allocations are split into 3 categories:
      28             :  *
      29             :  * Huge  - the size is greater than CHUNK size (~2M by default), allocation is
      30             :  *         performed using mmap(). The result is aligned on 2M boundary.
      31             :  *
      32             :  * Large - a number of 4096K pages inside a CHUNK. Large blocks
      33             :  *         are always aligned on page boundary.
      34             :  *
      35             :  * Small - less than 3/4 of page size. Small sizes are rounded up to nearest
      36             :  *         greater predefined small size (there are 30 predefined sizes:
      37             :  *         8, 16, 24, 32, ... 3072). Small blocks are allocated from
      38             :  *         RUNs. Each RUN is allocated as a single or few following pages.
      39             :  *         Allocation inside RUNs implemented using linked list of free
      40             :  *         elements. The result is aligned to 8 bytes.
      41             :  *
      42             :  * zend_alloc allocates memory from OS by CHUNKs, these CHUNKs and huge memory
      43             :  * blocks are always aligned to CHUNK boundary. So it's very easy to determine
      44             :  * the CHUNK owning the certain pointer. Regular CHUNKs reserve a single
      45             :  * page at start for special purpose. It contains bitset of free pages,
      46             :  * few bitset for available runs of predefined small sizes, map of pages that
      47             :  * keeps information about usage of each page in this CHUNK, etc.
      48             :  *
      49             :  * zend_alloc provides familiar emalloc/efree/erealloc API, but in addition it
      50             :  * provides specialized and optimized routines to allocate blocks of predefined
      51             :  * sizes (e.g. emalloc_2(), emallc_4(), ..., emalloc_large(), etc)
      52             :  * The library uses C preprocessor tricks that substitute calls to emalloc()
      53             :  * with more specialized routines when the requested size is known.
      54             :  */
      55             : 
      56             : #include "zend.h"
      57             : #include "zend_alloc.h"
      58             : #include "zend_globals.h"
      59             : #include "zend_operators.h"
      60             : #include "zend_multiply.h"
      61             : 
      62             : #ifdef HAVE_SIGNAL_H
      63             : # include <signal.h>
      64             : #endif
      65             : #ifdef HAVE_UNISTD_H
      66             : # include <unistd.h>
      67             : #endif
      68             : 
      69             : #ifdef ZEND_WIN32
      70             : # include <wincrypt.h>
      71             : # include <process.h>
      72             : #endif
      73             : 
      74             : #include <stdio.h>
      75             : #include <stdlib.h>
      76             : #include <string.h>
      77             : 
      78             : #include <sys/types.h>
      79             : #include <sys/stat.h>
      80             : #if HAVE_LIMITS_H
      81             : #include <limits.h>
      82             : #endif
      83             : #include <fcntl.h>
      84             : #include <errno.h>
      85             : 
      86             : #ifndef _WIN32
      87             : # ifdef HAVE_MREMAP
      88             : #  ifndef _GNU_SOURCE
      89             : #   define _GNU_SOURCE
      90             : #  endif
      91             : #  ifndef __USE_GNU
      92             : #   define __USE_GNU
      93             : #  endif
      94             : # endif
      95             : # include <sys/mman.h>
      96             : # ifndef MAP_ANON
      97             : #  ifdef MAP_ANONYMOUS
      98             : #   define MAP_ANON MAP_ANONYMOUS
      99             : #  endif
     100             : # endif
     101             : # ifndef MREMAP_MAYMOVE
     102             : #  define MREMAP_MAYMOVE 0
     103             : # endif
     104             : # ifndef MAP_FAILED
     105             : #  define MAP_FAILED ((void*)-1)
     106             : # endif
     107             : # ifndef MAP_POPULATE
     108             : #  define MAP_POPULATE 0
     109             : # endif
     110             : #  if defined(_SC_PAGESIZE) || (_SC_PAGE_SIZE)
     111             : #    define REAL_PAGE_SIZE _real_page_size
     112             : static size_t _real_page_size = ZEND_MM_PAGE_SIZE;
     113             : #  endif
     114             : #endif
     115             : 
     116             : #ifndef REAL_PAGE_SIZE
     117             : # define REAL_PAGE_SIZE ZEND_MM_PAGE_SIZE
     118             : #endif
     119             : 
     120             : #ifndef ZEND_MM_STAT
     121             : # define ZEND_MM_STAT 1    /* track current and peak memory usage            */
     122             : #endif
     123             : #ifndef ZEND_MM_LIMIT
     124             : # define ZEND_MM_LIMIT 1   /* support for user-defined memory limit          */
     125             : #endif
     126             : #ifndef ZEND_MM_CUSTOM
     127             : # define ZEND_MM_CUSTOM 1  /* support for custom memory allocator            */
     128             :                            /* USE_ZEND_ALLOC=0 may switch to system malloc() */
     129             : #endif
     130             : #ifndef ZEND_MM_STORAGE
     131             : # define ZEND_MM_STORAGE 1 /* support for custom memory storage              */
     132             : #endif
     133             : #ifndef ZEND_MM_ERROR
     134             : # define ZEND_MM_ERROR 1   /* report system errors                           */
     135             : #endif
     136             : 
     137             : #ifndef ZEND_MM_CHECK
     138             : # define ZEND_MM_CHECK(condition, message)  do { \
     139             :                 if (UNEXPECTED(!(condition))) { \
     140             :                         zend_mm_panic(message); \
     141             :                 } \
     142             :         } while (0)
     143             : #endif
     144             : 
     145             : typedef uint32_t   zend_mm_page_info; /* 4-byte integer */
     146             : typedef zend_ulong zend_mm_bitset;    /* 4-byte or 8-byte integer */
     147             : 
     148             : #define ZEND_MM_ALIGNED_OFFSET(size, alignment) \
     149             :         (((size_t)(size)) & ((alignment) - 1))
     150             : #define ZEND_MM_ALIGNED_BASE(size, alignment) \
     151             :         (((size_t)(size)) & ~((alignment) - 1))
     152             : #define ZEND_MM_ALIGNED_SIZE_EX(size, alignment) \
     153             :         (((size_t)(size) + ((alignment) - 1)) & ~((alignment) - 1))
     154             : #define ZEND_MM_SIZE_TO_NUM(size, alignment) \
     155             :         (((size_t)(size) + ((alignment) - 1)) / (alignment))
     156             : 
     157             : #define ZEND_MM_BITSET_LEN              (sizeof(zend_mm_bitset) * 8)       /* 32 or 64 */
     158             : #define ZEND_MM_PAGE_MAP_LEN    (ZEND_MM_PAGES / ZEND_MM_BITSET_LEN) /* 16 or 8 */
     159             : 
     160             : typedef zend_mm_bitset zend_mm_page_map[ZEND_MM_PAGE_MAP_LEN];     /* 64B */
     161             : 
     162             : #define ZEND_MM_IS_FRUN                  0x00000000
     163             : #define ZEND_MM_IS_LRUN                  0x40000000
     164             : #define ZEND_MM_IS_SRUN                  0x80000000
     165             : 
     166             : #define ZEND_MM_LRUN_PAGES_MASK          0x000003ff
     167             : #define ZEND_MM_LRUN_PAGES_OFFSET        0
     168             : 
     169             : #define ZEND_MM_SRUN_BIN_NUM_MASK        0x0000001f
     170             : #define ZEND_MM_SRUN_BIN_NUM_OFFSET      0
     171             : 
     172             : #define ZEND_MM_LRUN_PAGES(info)         (((info) & ZEND_MM_LRUN_PAGES_MASK) >> ZEND_MM_LRUN_PAGES_OFFSET)
     173             : #define ZEND_MM_SRUN_BIN_NUM(info)       (((info) & ZEND_MM_SRUN_BIN_NUM_MASK) >> ZEND_MM_SRUN_BIN_NUM_OFFSET)
     174             : 
     175             : #define ZEND_MM_FRUN()                   ZEND_MM_IS_FRUN
     176             : #define ZEND_MM_LRUN(count)              (ZEND_MM_IS_LRUN | ((count) << ZEND_MM_LRUN_PAGES_OFFSET))
     177             : #define ZEND_MM_SRUN(bin_num)            (ZEND_MM_IS_SRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET))
     178             : 
     179             : #define ZEND_MM_BINS 30
     180             : 
     181             : typedef struct  _zend_mm_page      zend_mm_page;
     182             : typedef struct  _zend_mm_bin       zend_mm_bin;
     183             : typedef struct  _zend_mm_free_slot zend_mm_free_slot;
     184             : typedef struct  _zend_mm_chunk     zend_mm_chunk;
     185             : typedef struct  _zend_mm_huge_list zend_mm_huge_list;
     186             : 
     187             : #ifdef _WIN64
     188             : # define PTR_FMT "0x%0.16I64x"
     189             : #elif SIZEOF_LONG == 8
     190             : # define PTR_FMT "0x%0.16lx"
     191             : #else
     192             : # define PTR_FMT "0x%0.8lx"
     193             : #endif
     194             : 
     195             : /*
     196             :  * Memory is retrived from OS by chunks of fixed size 2MB.
     197             :  * Inside chunk it's managed by pages of fixed size 4096B.
     198             :  * So each chunk consists from 512 pages.
     199             :  * The first page of each chunk is reseved for chunk header.
     200             :  * It contains service information about all pages.
     201             :  *
     202             :  * free_pages - current number of free pages in this chunk
     203             :  *
     204             :  * free_tail  - number of continuous free pages at the end of chunk
     205             :  *
     206             :  * free_map   - bitset (a bit for each page). The bit is set if the corresponding
     207             :  *              page is allocated. Allocator for "lage sizes" may easily find a
     208             :  *              free page (or a continuous number of pages) searching for zero
     209             :  *              bits.
     210             :  *
     211             :  * map        - contains service information for each page. (32-bits for each
     212             :  *              page).
     213             :  *    usage:
     214             :  *                              (2 bits)
     215             :  *                              FRUN - free page,
     216             :  *              LRUN - first page of "large" allocation
     217             :  *              SRUN - first page of a bin used for "small" allocation
     218             :  *
     219             :  *    lrun_pages:
     220             :  *              (10 bits) number of allocated pages
     221             :  *
     222             :  *    srun_bin_num:
     223             :  *              (5 bits) bin number (e.g. 0 for sizes 0-2, 1 for 3-4,
     224             :  *               2 for 5-8, 3 for 9-16 etc) see zend_alloc_sizes.h
     225             :  */
     226             : 
     227             : struct _zend_mm_heap {
     228             : #if ZEND_MM_CUSTOM
     229             :         int                use_custom_heap;
     230             : #endif
     231             : #if ZEND_MM_STORAGE
     232             :         zend_mm_storage   *storage;
     233             : #endif
     234             : #if ZEND_MM_STAT
     235             :         size_t             size;                    /* current memory usage */
     236             :         size_t             peak;                    /* peak memory usage */
     237             : #endif
     238             :         zend_mm_free_slot *free_slot[ZEND_MM_BINS]; /* free lists for small sizes */
     239             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
     240             :         size_t             real_size;               /* current size of allocated pages */
     241             : #endif
     242             : #if ZEND_MM_STAT
     243             :         size_t             real_peak;               /* peak size of allocated pages */
     244             : #endif
     245             : #if ZEND_MM_LIMIT
     246             :         size_t             limit;                   /* memory limit */
     247             :         int                overflow;                /* memory overflow flag */
     248             : #endif
     249             : 
     250             :         zend_mm_huge_list *huge_list;               /* list of huge allocated blocks */
     251             : 
     252             :         zend_mm_chunk     *main_chunk;
     253             :         zend_mm_chunk     *cached_chunks;                       /* list of unused chunks */
     254             :         int                chunks_count;                        /* number of alocated chunks */
     255             :         int                peak_chunks_count;           /* peak number of allocated chunks for current request */
     256             :         int                cached_chunks_count;         /* number of cached chunks */
     257             :         double             avg_chunks_count;            /* average number of chunks allocated per request */
     258             : #if ZEND_MM_CUSTOM
     259             :         void              *(*_malloc)(size_t);
     260             :         void               (*_free)(void*);
     261             :         void              *(*_realloc)(void*, size_t);
     262             : #endif
     263             : };
     264             : 
     265             : struct _zend_mm_chunk {
     266             :         zend_mm_heap      *heap;
     267             :         zend_mm_chunk     *next;
     268             :         zend_mm_chunk     *prev;
     269             :         int                free_pages;                          /* number of free pages */
     270             :         int                free_tail;               /* number of free pages at the end of chunk */
     271             :         int                num;
     272             :         char               reserve[64 - (sizeof(void*) * 3 + sizeof(int) * 3)];
     273             :         zend_mm_heap       heap_slot;               /* used only in main chunk */
     274             :         zend_mm_page_map   free_map;                /* 512 bits or 64 bytes */
     275             :         zend_mm_page_info  map[ZEND_MM_PAGES];      /* 2 KB = 512 * 4 */
     276             : };
     277             : 
     278             : struct _zend_mm_page {
     279             :         char               bytes[ZEND_MM_PAGE_SIZE];
     280             : };
     281             : 
     282             : /*
     283             :  * bin - is one or few continuous pages (up to 8) used for allocation of
     284             :  * a particular "small size".
     285             :  */
     286             : struct _zend_mm_bin {
     287             :         char               bytes[ZEND_MM_PAGE_SIZE * 8];
     288             : };
     289             : 
     290             : #if ZEND_DEBUG
     291             : typedef struct _zend_mm_debug_info {
     292             :         size_t             size;
     293             :         const char        *filename;
     294             :         const char        *orig_filename;
     295             :         uint               lineno;
     296             :         uint               orig_lineno;
     297             : } zend_mm_debug_info;
     298             : #endif
     299             : 
     300             : struct _zend_mm_free_slot {
     301             :         zend_mm_free_slot *next_free_slot;
     302             : };
     303             : 
     304             : struct _zend_mm_huge_list {
     305             :         void              *ptr;
     306             :         size_t             size;
     307             :         zend_mm_huge_list *next;
     308             : #if ZEND_DEBUG
     309             :         zend_mm_debug_info dbg;
     310             : #endif
     311             : };
     312             : 
     313             : #define ZEND_MM_PAGE_ADDR(chunk, page_num) \
     314             :         ((void*)(((zend_mm_page*)(chunk)) + (page_num)))
     315             : 
     316             : #define _BIN_DATA_SIZE(num, size, elements, pages, x, y) size,
     317             : static const unsigned int bin_data_size[] = {
     318             :   ZEND_MM_BINS_INFO(_BIN_DATA_SIZE, x, y)
     319             : };
     320             : 
     321             : #define _BIN_DATA_ELEMENTS(num, size, elements, pages, x, y) elements,
     322             : static const int bin_elements[] = {
     323             :   ZEND_MM_BINS_INFO(_BIN_DATA_ELEMENTS, x, y)
     324             : };
     325             : 
     326             : #define _BIN_DATA_PAGES(num, size, elements, pages, x, y) pages,
     327             : static const int bin_pages[] = {
     328             :   ZEND_MM_BINS_INFO(_BIN_DATA_PAGES, x, y)
     329             : };
     330             : 
     331             : #if ZEND_DEBUG
     332             : void zend_debug_alloc_output(char *format, ...)
     333             : {
     334             :         char output_buf[256];
     335             :         va_list args;
     336             : 
     337             :         va_start(args, format);
     338             :         vsprintf(output_buf, format, args);
     339             :         va_end(args);
     340             : 
     341             : #ifdef ZEND_WIN32
     342             :         OutputDebugString(output_buf);
     343             : #else
     344             :         fprintf(stderr, "%s", output_buf);
     345             : #endif
     346             : }
     347             : #endif
     348             : 
     349           0 : static ZEND_NORETURN void zend_mm_panic(const char *message)
     350             : {
     351           0 :         fprintf(stderr, "%s\n", message);
     352             : /* See http://support.microsoft.com/kb/190351 */
     353             : #ifdef PHP_WIN32
     354             :         fflush(stderr);
     355             : #endif
     356             : #if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
     357             :         kill(getpid(), SIGSEGV);
     358             : #endif
     359           0 :         exit(1);
     360             : }
     361             : 
     362           0 : static ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap,
     363             :         const char *format,
     364             :         size_t limit,
     365             : #if ZEND_DEBUG
     366             :         const char *filename,
     367             :         uint lineno,
     368             : #endif
     369             :         size_t size)
     370             : {
     371             :         TSRMLS_FETCH();
     372             : 
     373           0 :         heap->overflow = 1;
     374           0 :         zend_try {
     375           0 :                 zend_error_noreturn(E_ERROR,
     376             :                         format,
     377             :                         limit,
     378             : #if ZEND_DEBUG
     379             :                         filename,
     380             :                         lineno,
     381             : #endif
     382             :                         size);
     383           0 :         } zend_catch {
     384           0 :         }  zend_end_try();
     385           0 :         heap->overflow = 0;
     386           0 :         zend_bailout();
     387           0 :         exit(1);
     388             : }
     389             : 
     390             : #ifdef _WIN32
     391             : void
     392             : stderr_last_error(char *msg)
     393             : {
     394             :         LPSTR buf = NULL;
     395             :         DWORD err = GetLastError();
     396             : 
     397             :         if (!FormatMessage(
     398             :                         FORMAT_MESSAGE_ALLOCATE_BUFFER |
     399             :                         FORMAT_MESSAGE_FROM_SYSTEM |
     400             :                         FORMAT_MESSAGE_IGNORE_INSERTS,
     401             :                         NULL,
     402             :                         err,
     403             :                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
     404             :                         (LPSTR)&buf,
     405             :                 0, NULL)) {
     406             :                 fprintf(stderr, "\n%s: [0x%08lx]\n", msg, err);
     407             :         }
     408             :         else {
     409             :                 fprintf(stderr, "\n%s: [0x%08lx] %s\n", msg, err, buf);
     410             :         }
     411             : }
     412             : #endif
     413             : 
     414             : /*****************/
     415             : /* OS Allocation */
     416             : /*****************/
     417             : 
     418           0 : static void *zend_mm_mmap_fixed(void *addr, size_t size)
     419             : {
     420             : #ifdef _WIN32
     421             :         return VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
     422             : #else
     423             :         /* MAP_FIXED leads to discarding of the old mapping, so it can't be used. */
     424           0 :         void *ptr = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON /*| MAP_POPULATE | MAP_HUGETLB*/, -1, 0);
     425             : 
     426           0 :         if (ptr == MAP_FAILED) {
     427             : #if ZEND_MM_ERROR
     428           0 :                 fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno));
     429             : #endif
     430           0 :                 return NULL;
     431           0 :         } else if (ptr != addr) {
     432           0 :                 if (munmap(ptr, size) != 0) {
     433             : #if ZEND_MM_ERROR
     434           0 :                         fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
     435             : #endif
     436             :                 }
     437           0 :                 return NULL;
     438             :         }
     439           0 :         return ptr;
     440             : #endif
     441             : }
     442             : 
     443          34 : static void *zend_mm_mmap(size_t size)
     444             : {
     445             : #ifdef _WIN32
     446             :         void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
     447             : 
     448             :         if (ptr == NULL) {
     449             : #if ZEND_MM_ERROR
     450             :                 stderr_last_error("VirtualAlloc() failed");
     451             : #endif
     452             :                 return NULL;
     453             :         }
     454             :         return ptr;
     455             : #else
     456          34 :         void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON /*| MAP_POPULATE | MAP_HUGETLB*/, -1, 0);
     457             : 
     458          34 :         if (ptr == MAP_FAILED) {
     459             : #if ZEND_MM_ERROR
     460           0 :                 fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno));
     461             : #endif
     462           0 :                 return NULL;
     463             :         }
     464          34 :         return ptr;
     465             : #endif
     466             : }
     467             : 
     468          66 : static void zend_mm_munmap(void *addr, size_t size)
     469             : {
     470             : #ifdef _WIN32
     471             :         if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
     472             : #if ZEND_MM_ERROR
     473             :                 stderr_last_error("VirtualFree() failed");
     474             : #endif
     475             :         }
     476             : #else
     477          66 :         if (munmap(addr, size) != 0) {
     478             : #if ZEND_MM_ERROR
     479           0 :                 fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
     480             : #endif
     481             :         }
     482             : #endif
     483          66 : }
     484             : 
     485             : /***********/
     486             : /* Bitmask */
     487             : /***********/
     488             : 
     489             : /* number of trailing set (1) bits */
     490             : static zend_always_inline int zend_mm_bitset_nts(zend_mm_bitset bitset)
     491             : {
     492             : #if defined(__GNUC__)
     493             : # if SIZEOF_ZEND_LONG == SIZEOF_LONG
     494     1962420 :         return __builtin_ctzl(~bitset);
     495             : # else
     496             :         return __builtin_ctzll(~bitset);
     497             : # endif
     498             : #elif defined(_WIN32)
     499             :         unsigned long index;
     500             : 
     501             : #if defined(_WIN64)
     502             :         if (!BitScanForward64(&index, ~bitset)) {
     503             : #else
     504             :         if (!BitScanForward(&index, ~bitset)) {
     505             : #endif
     506             :                 /* undefined behavior */
     507             :                 return 32;
     508             :         }
     509             : 
     510             :         return (int)index;
     511             : #else
     512             :         int n;
     513             : 
     514             :         if (bitset == (zend_mm_bitset)-1) return ZEND_MM_BITSET_LEN;
     515             : 
     516             :         n = 0;
     517             : #if SIZEOF_ZEND_LONG == 8
     518             :         if (sizeof(zend_mm_bitset) == 8) {
     519             :                 if ((bitset & 0xffffffff) == 0xffffffff) {n += 32; bitset = bitset >> Z_UL(32);}
     520             :         }
     521             : #endif
     522             :         if ((bitset & 0x0000ffff) == 0x0000ffff) {n += 16; bitset = bitset >> 16;}
     523             :         if ((bitset & 0x000000ff) == 0x000000ff) {n +=  8; bitset = bitset >>  8;}
     524             :         if ((bitset & 0x0000000f) == 0x0000000f) {n +=  4; bitset = bitset >>  4;}
     525             :         if ((bitset & 0x00000003) == 0x00000003) {n +=  2; bitset = bitset >>  2;}
     526             :         return n + (bitset & 1);
     527             : #endif
     528             : }
     529             : 
     530             : /* number of trailing zero bits (0x01 -> 1; 0x40 -> 6; 0x00 -> LEN) */
     531             : static zend_always_inline int zend_mm_bitset_ntz(zend_mm_bitset bitset)
     532             : {
     533             : #if defined(__GNUC__)
     534             : # if SIZEOF_ZEND_LONG == SIZEOF_LONG
     535     1592021 :         return __builtin_ctzl(bitset);
     536             : # else
     537             :         return __builtin_ctzll(bitset);
     538             : # endif
     539             : #elif defined(_WIN32)
     540             :         unsigned long index;
     541             : 
     542             : #if defined(_WIN64)
     543             :         if (!BitScanForward64(&index, bitset)) {
     544             : #else
     545             :         if (!BitScanForward(&index, bitset)) {
     546             : #endif
     547             :                 /* undefined behavior */
     548             :                 return 32;
     549             :         }
     550             : 
     551             :         return (int)index;
     552             : #else
     553             :         int n;
     554             : 
     555             :         if (bitset == (zend_mm_bitset)0) return ZEND_MM_BITSET_LEN;
     556             : 
     557             :         n = 1;
     558             : #if SIZEOF_ZEND_LONG == 8
     559             :         if (sizeof(zend_mm_bitset) == 8) {
     560             :                 if ((bitset & 0xffffffff) == 0) {n += 32; bitset = bitset >> Z_UL(32);}
     561             :         }
     562             : #endif
     563             :         if ((bitset & 0x0000ffff) == 0) {n += 16; bitset = bitset >> 16;}
     564             :         if ((bitset & 0x000000ff) == 0) {n +=  8; bitset = bitset >>  8;}
     565             :         if ((bitset & 0x0000000f) == 0) {n +=  4; bitset = bitset >>  4;}
     566             :         if ((bitset & 0x00000003) == 0) {n +=  2; bitset = bitset >>  2;}
     567             :         return n - (bitset & 1);
     568             : #endif
     569             : }
     570             : 
     571             : static zend_always_inline int zend_mm_bitset_find_zero(zend_mm_bitset *bitset, int size)
     572             : {
     573             :         int i = 0;
     574             : 
     575             :         do {
     576             :                 zend_mm_bitset tmp = bitset[i];
     577             :                 if (tmp != (zend_mm_bitset)-1) {
     578             :                         return i * ZEND_MM_BITSET_LEN + zend_mm_bitset_nts(tmp);
     579             :                 }
     580             :                 i++;
     581             :         } while (i < size);
     582             :         return -1;
     583             : }
     584             : 
     585             : static zend_always_inline int zend_mm_bitset_find_one(zend_mm_bitset *bitset, int size)
     586             : {
     587             :         int i = 0;
     588             : 
     589             :         do {
     590             :                 zend_mm_bitset tmp = bitset[i];
     591             :                 if (tmp != 0) {
     592             :                         return i * ZEND_MM_BITSET_LEN + zend_mm_bitset_ntz(tmp);
     593             :                 }
     594             :                 i++;
     595             :         } while (i < size);
     596             :         return -1;
     597             : }
     598             : 
     599             : static zend_always_inline int zend_mm_bitset_find_zero_and_set(zend_mm_bitset *bitset, int size)
     600             : {
     601             :         int i = 0;
     602             : 
     603             :         do {
     604             :                 zend_mm_bitset tmp = bitset[i];
     605             :                 if (tmp != (zend_mm_bitset)-1) {
     606             :                         int n = zend_mm_bitset_nts(tmp);
     607             :                         bitset[i] |= Z_UL(1) << n;
     608             :                         return i * ZEND_MM_BITSET_LEN + n;
     609             :                 }
     610             :                 i++;
     611             :         } while (i < size);
     612             :         return -1;
     613             : }
     614             : 
     615             : static zend_always_inline int zend_mm_bitset_is_set(zend_mm_bitset *bitset, int bit)
     616             : {
     617        2779 :         return (bitset[bit / ZEND_MM_BITSET_LEN] & (Z_L(1) << (bit & (ZEND_MM_BITSET_LEN-1)))) != 0;
     618             : }
     619             : 
     620             : static zend_always_inline void zend_mm_bitset_set_bit(zend_mm_bitset *bitset, int bit)
     621             : {
     622       12141 :         bitset[bit / ZEND_MM_BITSET_LEN] |= (Z_L(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
     623             : }
     624             : 
     625             : static zend_always_inline void zend_mm_bitset_reset_bit(zend_mm_bitset *bitset, int bit)
     626             : {
     627        8379 :         bitset[bit / ZEND_MM_BITSET_LEN] &= ~(Z_L(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
     628             : }
     629             : 
     630             : static zend_always_inline void zend_mm_bitset_set_range(zend_mm_bitset *bitset, int start, int len)
     631             : {
     632      913070 :         if (len == 1) {
     633             :                 zend_mm_bitset_set_bit(bitset, start);
     634             :         } else {
     635      900929 :                 int pos = start / ZEND_MM_BITSET_LEN;
     636      900929 :                 int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
     637      900929 :                 int bit = start & (ZEND_MM_BITSET_LEN - 1);
     638             :                 zend_mm_bitset tmp;
     639             : 
     640      900929 :                 if (pos != end) {
     641             :                         /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */
     642       34577 :                         tmp = (zend_mm_bitset)-1 << bit;
     643       34577 :                         bitset[pos++] |= tmp;
     644       34595 :                         while (pos != end) {
     645             :                                 /* set all bits */
     646          18 :                                 bitset[pos++] = (zend_mm_bitset)-1;
     647             :                         }
     648       34577 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     649             :                         /* set bits from "0" to "end" */
     650       34577 :                         tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     651       34577 :                         bitset[pos] |= tmp;
     652             :                 } else {
     653      866352 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     654             :                         /* set bits from "bit" to "end" */
     655      866352 :                         tmp = (zend_mm_bitset)-1 << bit;
     656      866352 :                         tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     657      866352 :                         bitset[pos] |= tmp;
     658             :                 }
     659             :         }
     660             : }
     661             : 
     662             : static zend_always_inline void zend_mm_bitset_reset_range(zend_mm_bitset *bitset, int start, int len)
     663             : {
     664      910462 :         if (len == 1) {
     665             :                 zend_mm_bitset_reset_bit(bitset, start);
     666             :         } else {
     667      902083 :                 int pos = start / ZEND_MM_BITSET_LEN;
     668      902083 :                 int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
     669      902083 :                 int bit = start & (ZEND_MM_BITSET_LEN - 1);
     670             :                 zend_mm_bitset tmp;
     671             : 
     672      902083 :                 if (pos != end) {
     673             :                         /* reset bits from "bit" to ZEND_MM_BITSET_LEN-1 */
     674       34604 :                         tmp = ~((Z_L(1) << bit) - 1);
     675       34604 :                         bitset[pos++] &= ~tmp;
     676       34629 :                         while (pos != end) {
     677             :                                 /* set all bits */
     678          25 :                                 bitset[pos++] = 0;
     679             :                         }
     680       34604 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     681             :                         /* reset bits from "0" to "end" */
     682       34604 :                         tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     683       34604 :                         bitset[pos] &= ~tmp;
     684             :                 } else {
     685      867479 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     686             :                         /* reset bits from "bit" to "end" */
     687      867479 :                         tmp = (zend_mm_bitset)-1 << bit;
     688      867479 :                         tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     689      867479 :                         bitset[pos] &= ~tmp;
     690             :                 }
     691             :         }
     692             : }
     693             : 
     694             : static zend_always_inline int zend_mm_bitset_is_free_range(zend_mm_bitset *bitset, int start, int len)
     695             : {
     696        3098 :         if (len == 1) {
     697        2779 :                 return !zend_mm_bitset_is_set(bitset, start);
     698             :         } else {
     699         319 :                 int pos = start / ZEND_MM_BITSET_LEN;
     700         319 :                 int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
     701         319 :                 int bit = start & (ZEND_MM_BITSET_LEN - 1);
     702             :                 zend_mm_bitset tmp;
     703             : 
     704         319 :                 if (pos != end) {
     705             :                         /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */
     706           9 :                         tmp = (zend_mm_bitset)-1 << bit;
     707           9 :                         if ((bitset[pos++] & tmp) != 0) {
     708           6 :                                 return 0;
     709             :                         }
     710           4 :                         while (pos != end) {
     711             :                                 /* set all bits */
     712           1 :                                 if (bitset[pos++] != 0) {
     713           0 :                                         return 0;
     714             :                                 }
     715             :                         }
     716           3 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     717             :                         /* set bits from "0" to "end" */
     718           3 :                         tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     719           3 :                         return (bitset[pos] & tmp) == 0;
     720             :                 } else {
     721         310 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     722             :                         /* set bits from "bit" to "end" */
     723         310 :                         tmp = (zend_mm_bitset)-1 << bit;
     724         310 :                         tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     725         310 :                         return (bitset[pos] & tmp) == 0;
     726             :                 }
     727             :         }
     728             : }
     729             : 
     730             : /**********/
     731             : /* Chunks */
     732             : /**********/
     733             : 
     734          18 : static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment)
     735             : {
     736          18 :         void *ptr = zend_mm_mmap(size);
     737             : 
     738          18 :         if (ptr == NULL) {
     739           0 :                 return NULL;
     740          18 :         } else if (ZEND_MM_ALIGNED_OFFSET(ptr, alignment) == 0) {
     741             : #ifdef MADV_HUGEPAGE
     742             :             madvise(ptr, size, MADV_HUGEPAGE);
     743             : #endif
     744           2 :                 return ptr;
     745             :         } else {
     746             :                 size_t offset;
     747             : 
     748             :                 /* chunk has to be aligned */
     749          16 :                 zend_mm_munmap(ptr, size);
     750          16 :                 ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE);
     751             : #ifdef _WIN32
     752             :                 offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
     753             :                 zend_mm_munmap(ptr, size + alignment - REAL_PAGE_SIZE);
     754             :                 ptr = zend_mm_mmap_fixed((void*)((char*)ptr + (alignment - offset)), size);
     755             :                 offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
     756             :                 if (offset != 0) {
     757             :                         zend_mm_munmap(ptr, size);
     758             :                         return NULL;
     759             :                 }
     760             :                 return ptr;
     761             : #else
     762          16 :                 offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
     763          16 :                 if (offset != 0) {
     764          16 :                         offset = alignment - offset;
     765          16 :                         zend_mm_munmap(ptr, offset);
     766          16 :                         ptr = (char*)ptr + offset;
     767          16 :                         alignment -= offset;
     768             :                 }
     769          16 :                 if (alignment > REAL_PAGE_SIZE) {
     770          16 :                         zend_mm_munmap((char*)ptr + size, alignment - REAL_PAGE_SIZE);
     771             :                 }
     772             : # ifdef MADV_HUGEPAGE
     773             :             madvise(ptr, size, MADV_HUGEPAGE);
     774             : # endif
     775             : #endif
     776          16 :                 return ptr;
     777             :         }
     778             : }
     779             : 
     780           8 : static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignment)
     781             : {
     782             : #if ZEND_MM_STORAGE
     783           8 :         if (UNEXPECTED(heap->storage)) {
     784           0 :                 void *ptr = heap->storage->chunk_alloc(heap->storage, size, alignment);
     785             :                 ZEND_ASSERT(((zend_uintptr_t)((char*)ptr + (alignment-1)) & (alignment-1)) == (zend_uintptr_t)ptr);
     786           0 :                 return ptr;
     787             :         }
     788             : #endif
     789           8 :         return zend_mm_chunk_alloc_int(size, alignment);
     790             : }
     791             : 
     792          18 : static void zend_mm_chunk_free(zend_mm_heap *heap, void *addr, size_t size)
     793             : {
     794             : #if ZEND_MM_STORAGE
     795          18 :         if (UNEXPECTED(heap->storage)) {
     796           0 :                 heap->storage->chunk_free(heap->storage, addr, size);
     797           0 :                 return;
     798             :         }
     799             : #endif
     800          18 :         zend_mm_munmap(addr, size);
     801             : }
     802             : 
     803           0 : static void zend_mm_chunk_truncate(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size)
     804             : {
     805             : #if ZEND_MM_STORAGE
     806           0 :         if (UNEXPECTED(heap->storage)) {
     807           0 :                 heap->storage->chunk_truncate(heap->storage, addr, old_size, new_size);
     808           0 :                 return;
     809             :         }
     810             : #endif
     811           0 :         zend_mm_munmap((char*)addr + new_size, old_size - new_size);
     812             : }
     813             : 
     814             : static zend_always_inline void zend_mm_chunk_init(zend_mm_heap *heap, zend_mm_chunk *chunk)
     815             : {
     816           7 :         chunk->heap = heap;
     817           7 :         chunk->next = heap->main_chunk;
     818           7 :         chunk->prev = heap->main_chunk->prev;
     819           7 :         chunk->prev->next = chunk;
     820           7 :         chunk->next->prev = chunk;
     821             :         /* mark first pages as allocated */
     822           7 :         chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
     823           7 :         chunk->free_tail = ZEND_MM_FIRST_PAGE;
     824             :         /* the younger chunks have bigger number */
     825           7 :         chunk->num = chunk->prev->num + 1;
     826             :         /* mark first pages as allocated */
     827           7 :         chunk->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1;
     828           7 :         chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
     829             : }
     830             : 
     831             : /***********************/
     832             : /* Huge Runs (forward) */
     833             : /***********************/
     834             : 
     835             : static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     836             : static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     837             : static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     838             : 
     839             : #if ZEND_DEBUG
     840             : 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);
     841             : #else
     842             : 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);
     843             : #endif
     844             : 
     845             : /**************/
     846             : /* Large Runs */
     847             : /**************/
     848             : 
     849             : #if ZEND_DEBUG
     850             : static void *zend_mm_alloc_pages(zend_mm_heap *heap, int pages_count, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
     851             : #else
     852      910671 : static void *zend_mm_alloc_pages(zend_mm_heap *heap, int pages_count ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
     853             : #endif
     854             : {
     855      910671 :         zend_mm_chunk *chunk = heap->main_chunk;
     856             :         int page_num, len;
     857             : 
     858             :         while (1) {
     859     2369365 :                 if (UNEXPECTED(chunk->free_pages < pages_count)) {
     860     1458692 :                         goto not_found;
     861             : #if 0
     862             :                 } else if (UNEXPECTED(chunk->free_pages + chunk->free_tail == ZEND_MM_PAGES)) {
     863             :                         if (UNEXPECTED(ZEND_MM_PAGES - chunk->free_tail < pages_count)) {
     864             :                                 goto not_found;
     865             :                         } else {
     866             :                                 page_num = chunk->free_tail;
     867             :                                 goto found;
     868             :                         }
     869             :                 } else if (0) {
     870             :                         /* First-Fit Search */
     871             :                         int free_tail = chunk->free_tail;
     872             :                         zend_mm_bitset *bitset = chunk->free_map;
     873             :                         zend_mm_bitset tmp = *(bitset++);
     874             :                         int i = 0;
     875             : 
     876             :                         while (1) {
     877             :                                 /* skip allocated blocks */
     878             :                                 while (tmp == (zend_mm_bitset)-1) {
     879             :                                         i += ZEND_MM_BITSET_LEN;
     880             :                                         if (i == ZEND_MM_PAGES) {
     881             :                                                 goto not_found;
     882             :                                         }
     883             :                                         tmp = *(bitset++);
     884             :                                 }
     885             :                                 /* find first 0 bit */
     886             :                                 page_num = i + zend_mm_bitset_nts(tmp);
     887             :                                 /* reset bits from 0 to "bit" */
     888             :                                 tmp &= tmp + 1;
     889             :                                 /* skip free blocks */
     890             :                                 while (tmp == 0) {
     891             :                                         i += ZEND_MM_BITSET_LEN;
     892             :                                         len = i - page_num;
     893             :                                         if (len >= pages_count) {
     894             :                                                 goto found;
     895             :                                         } else if (i >= free_tail) {
     896             :                                                 goto not_found;
     897             :                                         }
     898             :                                         tmp = *(bitset++);
     899             :                                 }
     900             :                                 /* find first 1 bit */
     901             :                                 len = (i + zend_mm_bitset_ntz(tmp)) - page_num;
     902             :                                 if (len >= pages_count) {
     903             :                                         goto found;
     904             :                                 }
     905             :                                 /* set bits from 0 to "bit" */
     906             :                                 tmp |= tmp - 1;
     907             :                         }
     908             : #endif
     909             :                 } else {
     910             :                         /* Best-Fit Search */
     911      910673 :                         int best = -1;
     912      910673 :                         int best_len = ZEND_MM_PAGES;
     913      910673 :                         int free_tail = chunk->free_tail;
     914      910673 :                         zend_mm_bitset *bitset = chunk->free_map;
     915      910673 :                         zend_mm_bitset tmp = *(bitset++);
     916      910673 :                         int i = 0;
     917             : 
     918             :                         while (1) {
     919             :                                 /* skip allocated blocks */
     920     8936373 :                                 while (tmp == (zend_mm_bitset)-1) {
     921     4899642 :                                         i += ZEND_MM_BITSET_LEN;
     922     4899642 :                                         if (i == ZEND_MM_PAGES) {
     923      111891 :                                                 if (best > 0) {
     924      111885 :                                                         page_num = best;
     925      111885 :                                                         goto found;
     926             :                                                 } else {
     927           6 :                                                         goto not_found;
     928             :                                                 }
     929             :                                         }
     930     4787751 :                                         tmp = *(bitset++);
     931             :                                 }
     932             :                                 /* find first 0 bit */
     933     1962420 :                                 page_num = i + zend_mm_bitset_nts(tmp);
     934             :                                 /* reset bits from 0 to "bit" */
     935     1962420 :                                 tmp &= tmp + 1;
     936             :                                 /* skip free blocks */
     937     5242304 :                                 while (tmp == 0) {
     938     1687863 :                                         i += ZEND_MM_BITSET_LEN;
     939     1687863 :                                         if (i >= free_tail) {
     940      370399 :                                                 len = ZEND_MM_PAGES - page_num;
     941      370399 :                                                 if (len >= pages_count && len < best_len) {
     942      304879 :                                                         chunk->free_tail = page_num + pages_count;
     943      304879 :                                                         goto found;
     944             :                                                 } else {
     945             :                                                         /* set accurate value */
     946       65520 :                                                         chunk->free_tail = page_num;
     947       65520 :                                                         if (best > 0) {
     948       65517 :                                                                 page_num = best;
     949       65517 :                                                                 goto found;
     950             :                                                         } else {
     951           3 :                                                                 goto not_found;
     952             :                                                         }
     953             :                                                 }
     954             :                                         }
     955     1317464 :                                         tmp = *(bitset++);
     956             :                                 }
     957             :                                 /* find first 1 bit */
     958     1592021 :                                 len = i + zend_mm_bitset_ntz(tmp) - page_num;
     959     1592021 :                                 if (len >= pages_count) {
     960     1421289 :                                         if (len == pages_count) {
     961      428383 :                                                 goto found;
     962      992906 :                                         } else if (len < best_len) {
     963      875448 :                                                 best_len = len;
     964      875448 :                                                 best = page_num;
     965             :                                         }
     966             :                                 }
     967             :                                 /* set bits from 0 to "bit" */
     968     1163638 :                                 tmp |= tmp - 1;
     969     1163638 :                         }
     970             :                 }
     971             : 
     972             : not_found:
     973     1458701 :                 if (chunk->next == heap->main_chunk) {
     974           7 :                         if (heap->cached_chunks) {
     975           0 :                                 heap->cached_chunks_count--;
     976           0 :                                 chunk = heap->cached_chunks;
     977           0 :                                 heap->cached_chunks = chunk->next;
     978             :                         } else {
     979             : #if ZEND_MM_LIMIT
     980           7 :                                 if (heap->real_size + ZEND_MM_CHUNK_SIZE > heap->limit) {
     981           0 :                                         if (heap->overflow == 0) {
     982             : #if ZEND_DEBUG
     983             :                                                 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);
     984             : #else
     985           0 :                                                 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);
     986             : #endif
     987             :                                                 return NULL;
     988             :                                         }
     989             :                                 }
     990             : #endif
     991           7 :                                 chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
     992           7 :                                 if (UNEXPECTED(chunk == NULL)) {
     993             :                                         /* insufficient memory */
     994             : #if !ZEND_MM_LIMIT
     995             :                                         zend_mm_safe_error(heap, "Out of memory");
     996             : #elif ZEND_DEBUG
     997             :                                         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);
     998             : #else
     999           0 :                                         zend_mm_safe_error(heap, "Out of memory (allocated %zu) (tried to allocate %zu bytes)", heap->real_size, ZEND_MM_PAGE_SIZE * pages_count);
    1000             : #endif
    1001             :                                         return NULL;
    1002             :                                 }
    1003             : #if ZEND_MM_STAT
    1004             :                                 do {
    1005           7 :                                         size_t size = heap->real_size + ZEND_MM_CHUNK_SIZE;
    1006           7 :                                         size_t peak = MAX(heap->real_peak, size);
    1007           7 :                                         heap->real_size = size;
    1008           7 :                                         heap->real_peak = peak;
    1009             :                                 } while (0);
    1010             : #elif ZEND_MM_LIMIT
    1011             :                                 heap->real_size += ZEND_MM_CHUNK_SIZE;
    1012             : 
    1013             : #endif
    1014             :                         }
    1015           7 :                         heap->chunks_count++;
    1016           7 :                         if (heap->chunks_count > heap->peak_chunks_count) {
    1017           5 :                                 heap->peak_chunks_count = heap->chunks_count;
    1018             :                         }
    1019             :                         zend_mm_chunk_init(heap, chunk);
    1020           7 :                         page_num = ZEND_MM_FIRST_PAGE;
    1021           7 :                         len = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    1022           7 :                         goto found;
    1023             :                 } else {
    1024     1458694 :                         chunk = chunk->next;
    1025             :                 }
    1026     1458694 :         }
    1027             : 
    1028             : found:
    1029             :         /* mark run as allocated */
    1030      910671 :         chunk->free_pages -= pages_count;
    1031      910671 :         zend_mm_bitset_set_range(chunk->free_map, page_num, pages_count);
    1032      910671 :         chunk->map[page_num] = ZEND_MM_LRUN(pages_count);
    1033      910671 :         if (page_num == chunk->free_tail) {
    1034           7 :                 chunk->free_tail = page_num + pages_count;
    1035             :         }
    1036      910671 :         return ZEND_MM_PAGE_ADDR(chunk, page_num);
    1037             : }
    1038             : 
    1039             : static zend_always_inline void *zend_mm_alloc_large(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1040             : {
    1041      909752 :         int pages_count = (int)ZEND_MM_SIZE_TO_NUM(size, ZEND_MM_PAGE_SIZE);
    1042             : #if ZEND_DEBUG
    1043             :         void *ptr = zend_mm_alloc_pages(heap, pages_count, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1044             : #else
    1045      909752 :         void *ptr = zend_mm_alloc_pages(heap, pages_count ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1046             : #endif
    1047             : #if ZEND_MM_STAT
    1048             :         do {
    1049      909752 :                 size_t size = heap->size + pages_count * ZEND_MM_PAGE_SIZE;
    1050      909752 :                 size_t peak = MAX(heap->peak, size);
    1051      909752 :                 heap->size = size;
    1052      909752 :                 heap->peak = peak;
    1053             :         } while (0);
    1054             : #endif
    1055      909752 :         return ptr;
    1056             : }
    1057             : 
    1058      909750 : static void zend_mm_free_pages(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count)
    1059             : {
    1060      909750 :         chunk->free_pages += pages_count;
    1061      909750 :         zend_mm_bitset_reset_range(chunk->free_map, page_num, pages_count);
    1062      909750 :         chunk->map[page_num] = 0;
    1063      909750 :         if (chunk->free_tail == page_num + pages_count) {
    1064             :                 /* this setting may be not accurate */
    1065      295031 :                 chunk->free_tail = page_num;
    1066             :         }
    1067      909750 :         if (chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE) {
    1068             :                 /* delete chunk */
    1069           5 :                 chunk->next->prev = chunk->prev;
    1070           5 :                 chunk->prev->next = chunk->next;
    1071           5 :                 heap->chunks_count--;
    1072           5 :                 if (heap->chunks_count + heap->cached_chunks_count < heap->avg_chunks_count + 0.1) {
    1073             :                         /* delay deletion */
    1074           0 :                         heap->cached_chunks_count++;
    1075           0 :                         chunk->next = heap->cached_chunks;
    1076           0 :                         heap->cached_chunks = chunk;
    1077             :                 } else {
    1078             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1079           5 :                         heap->real_size -= ZEND_MM_CHUNK_SIZE;
    1080             : #endif
    1081          10 :                         if (!heap->cached_chunks || chunk->num > heap->cached_chunks->num) {
    1082           5 :                                 zend_mm_chunk_free(heap, chunk, ZEND_MM_CHUNK_SIZE);
    1083             :                         } else {
    1084             : //TODO: select the best chunk to delete???
    1085           0 :                                 chunk->next = heap->cached_chunks->next;
    1086           0 :                                 zend_mm_chunk_free(heap, heap->cached_chunks, ZEND_MM_CHUNK_SIZE);
    1087           0 :                                 heap->cached_chunks = chunk;
    1088             :                         }
    1089             :                 }
    1090             :         }
    1091      909750 : }
    1092             : 
    1093             : static zend_always_inline void zend_mm_free_large(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count)
    1094             : {
    1095             : #if ZEND_MM_STAT
    1096      909750 :         heap->size -= pages_count * ZEND_MM_PAGE_SIZE;
    1097             : #endif
    1098      909750 :         zend_mm_free_pages(heap, chunk, page_num, pages_count);
    1099             : }
    1100             : 
    1101             : /**************/
    1102             : /* Small Runs */
    1103             : /**************/
    1104             : 
    1105             : /* higher set bit number (0->N/A, 1->1, 2->2, 4->3, 8->4, 127->7, 128->8 etc) */
    1106             : static zend_always_inline int zend_mm_small_size_to_bit(int size)
    1107             : {
    1108             : #if defined(__GNUC__)
    1109    19123356 :         return (__builtin_clz(size) ^ 0x1f) + 1;
    1110             : #elif defined(_WIN32)
    1111             :         unsigned long index;
    1112             : 
    1113             :         if (!BitScanReverse(&index, (unsigned long)size)) {
    1114             :                 /* undefined behavior */
    1115             :                 return 64;
    1116             :         }
    1117             : 
    1118             :         return (((31 - (int)index) ^ 0x1f) + 1);
    1119             : #else
    1120             :         int n = 16;
    1121             :         if (size <= 0x00ff) {n -= 8; size = size << 8;}
    1122             :         if (size <= 0x0fff) {n -= 4; size = size << 4;}
    1123             :         if (size <= 0x3fff) {n -= 2; size = size << 2;}
    1124             :         if (size <= 0x7fff) {n -= 1;}
    1125             :         return n;
    1126             : #endif
    1127             : }
    1128             : 
    1129             : #ifndef MAX
    1130             : # define MAX(a, b) (((a) > (b)) ? (a) : (b))
    1131             : #endif
    1132             : 
    1133             : #ifndef MIN
    1134             : # define MIN(a, b) (((a) < (b)) ? (a) : (b))
    1135             : #endif
    1136             : 
    1137             : static zend_always_inline int zend_mm_small_size_to_bin(size_t size)
    1138             : {
    1139             : #if 0
    1140             :         int n;
    1141             :                             /*0,  1,  2,  3,  4,  5,  6,  7,  8,  9  10, 11, 12*/
    1142             :         static const int f1[] = { 3,  3,  3,  3,  3,  3,  3,  4,  5,  6,  7,  8,  9};
    1143             :         static const int f2[] = { 0,  0,  0,  0,  0,  0,  0,  4,  8, 12, 16, 20, 24};
    1144             : 
    1145             :         if (UNEXPECTED(size <= 2)) return 0;
    1146             :         n = zend_mm_small_size_to_bit(size - 1);
    1147             :         return ((size-1) >> f1[n]) + f2[n];
    1148             : #else
    1149             :         int t1, t2, t3;
    1150             : 
    1151    19460952 :         if (UNEXPECTED(size <= 8)) return 0;
    1152    19123356 :         t1 = (int)(size - 1);
    1153    19123356 :         t2 = zend_mm_small_size_to_bit(t1);
    1154    19123356 :         t3 = t2 - 6;
    1155    19123356 :         t3 = (t3 < 0) ? 0 : t3;
    1156    19123356 :         t2 = t3 + 3;
    1157    19123356 :         t1 = t1 >> t2;
    1158    19123356 :         t3 = t3 << 2;
    1159    19123356 :         return t1 + t3;
    1160             : #endif
    1161             : }
    1162             : 
    1163             : #define ZEND_MM_SMALL_SIZE_TO_BIN(size)  zend_mm_small_size_to_bin(size)
    1164             : 
    1165         919 : static zend_never_inline void *zend_mm_alloc_small_slow(zend_mm_heap *heap, int bin_num ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1166             : {
    1167             :     zend_mm_chunk *chunk;
    1168             :     int page_num;
    1169             :         zend_mm_bin *bin;
    1170             :         zend_mm_free_slot *p, *end;
    1171             : 
    1172             : #if ZEND_DEBUG
    1173             :         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);
    1174             : #else
    1175         919 :         bin = (zend_mm_bin*)zend_mm_alloc_pages(heap, bin_pages[bin_num] ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1176             : #endif
    1177         919 :         if (UNEXPECTED(bin == NULL)) {
    1178             :                 /* insufficient memory */
    1179           0 :                 return NULL;
    1180             :         }
    1181             : 
    1182         919 :         chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(bin, ZEND_MM_CHUNK_SIZE);
    1183         919 :         page_num = ZEND_MM_ALIGNED_OFFSET(bin, ZEND_MM_CHUNK_SIZE) / ZEND_MM_PAGE_SIZE;
    1184         919 :         chunk->map[page_num] = ZEND_MM_SRUN(bin_num);
    1185         919 :         if (bin_pages[bin_num] > 1) {
    1186          73 :                 int i = 1;
    1187             :                 do {
    1188         233 :                         chunk->map[page_num+i] = ZEND_MM_SRUN(bin_num);
    1189         233 :                         i++;
    1190         233 :                 } while (i < bin_pages[bin_num]);
    1191             :         }
    1192             : 
    1193             :         /* create a linked list of elements from 1 to last */
    1194         919 :         end = (zend_mm_free_slot*)((char*)bin + (bin_data_size[bin_num] * (bin_elements[bin_num] - 1)));
    1195         919 :         heap->free_slot[bin_num] = p = (zend_mm_free_slot*)((char*)bin + bin_data_size[bin_num]);
    1196             :         do {
    1197       73062 :                 p->next_free_slot = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]);;
    1198             : #if ZEND_DEBUG
    1199             :                 do {
    1200             :                         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)));
    1201             :                         dbg->size = 0;
    1202             :                 } while (0);
    1203             : #endif
    1204       73062 :                 p = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]);
    1205       73062 :         } while (p != end);
    1206             : 
    1207             :         /* terminate list using NULL */
    1208         919 :         p->next_free_slot = NULL;
    1209             : #if ZEND_DEBUG
    1210             :                 do {
    1211             :                         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)));
    1212             :                         dbg->size = 0;
    1213             :                 } while (0);
    1214             : #endif
    1215             : 
    1216             :         /* return first element */
    1217         919 :         return (char*)bin;
    1218             : }
    1219             : 
    1220             : static zend_always_inline void *zend_mm_alloc_small(zend_mm_heap *heap, size_t size, int bin_num ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1221             : {
    1222             : #if ZEND_MM_STAT
    1223             :         do {
    1224    18988064 :                 size_t size = heap->size + bin_data_size[bin_num];
    1225    18988064 :                 size_t peak = MAX(heap->peak, size);
    1226    18988064 :                 heap->size = size;
    1227    18988064 :                 heap->peak = peak;
    1228             :         } while (0);
    1229             : #endif
    1230             : 
    1231    18988064 :         if (EXPECTED(heap->free_slot[bin_num] != NULL)) {
    1232    18987145 :                 zend_mm_free_slot *p = heap->free_slot[bin_num];
    1233    18987145 :                 heap->free_slot[bin_num] = p->next_free_slot;
    1234    18987145 :                 return (void*)p;
    1235             :         } else {
    1236         919 :                 return zend_mm_alloc_small_slow(heap, bin_num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1237             :         }
    1238             : }
    1239             : 
    1240             : static zend_always_inline void zend_mm_free_small(zend_mm_heap *heap, void *ptr, int bin_num)
    1241             : {
    1242             :         zend_mm_free_slot *p;
    1243             : 
    1244             : #if ZEND_MM_STAT
    1245    18987964 :         heap->size -= bin_data_size[bin_num];
    1246             : #endif
    1247             : 
    1248             : #if ZEND_DEBUG
    1249             :         do {
    1250             :                 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)));
    1251             :                 dbg->size = 0;
    1252             :         } while (0);
    1253             : #endif
    1254             : 
    1255    18987964 :     p = (zend_mm_free_slot*)ptr;
    1256    18987964 :     p->next_free_slot = heap->free_slot[bin_num];
    1257    18987964 :     heap->free_slot[bin_num] = p;
    1258             : }
    1259             : 
    1260             : /********/
    1261             : /* Heap */
    1262             : /********/
    1263             : 
    1264             : #if ZEND_DEBUG
    1265             : static zend_always_inline zend_mm_debug_info *zend_mm_get_debug_info(zend_mm_heap *heap, void *ptr)
    1266             : {
    1267             :         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1268             :         zend_mm_chunk *chunk;
    1269             :         int page_num;
    1270             :         zend_mm_page_info info;
    1271             : 
    1272             :         ZEND_MM_CHECK(page_offset != 0, "zend_mm_heap corrupted");
    1273             :         chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1274             :         page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1275             :         info = chunk->map[page_num];
    1276             :         ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1277             :         if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
    1278             :                 int bin_num = ZEND_MM_SRUN_BIN_NUM(info);
    1279             :                 return (zend_mm_debug_info*)((char*)ptr + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
    1280             :         } else /* if (info & ZEND_MM_IS_LRUN) */ {
    1281             :                 int pages_count = ZEND_MM_LRUN_PAGES(info);
    1282             : 
    1283             :                 return (zend_mm_debug_info*)((char*)ptr + ZEND_MM_PAGE_SIZE * pages_count - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
    1284             :         }
    1285             : }
    1286             : #endif
    1287             : 
    1288             : 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)
    1289             : {
    1290             :         void *ptr;
    1291             : #if ZEND_DEBUG
    1292             :         size_t real_size = size;
    1293             :         zend_mm_debug_info *dbg;
    1294             : 
    1295             :         /* special handling for zero-size allocation */
    1296             :         size = MAX(size, 1);
    1297             :         size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
    1298             : #endif
    1299    15079899 :         if (size <= ZEND_MM_MAX_SMALL_SIZE) {
    1300    14170146 :                 ptr = zend_mm_alloc_small(heap, size, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1301             : #if ZEND_DEBUG
    1302             :                 dbg = zend_mm_get_debug_info(heap, ptr);
    1303             :                 dbg->size = real_size;
    1304             :                 dbg->filename = __zend_filename;
    1305             :                 dbg->orig_filename = __zend_orig_filename;
    1306             :                 dbg->lineno = __zend_lineno;
    1307             :                 dbg->orig_lineno = __zend_orig_lineno;
    1308             : #endif
    1309    14170146 :                 return ptr;
    1310      909753 :         } else if (size <= ZEND_MM_MAX_LARGE_SIZE) {
    1311      909752 :                 ptr = zend_mm_alloc_large(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1312             : #if ZEND_DEBUG
    1313             :                 dbg = zend_mm_get_debug_info(heap, ptr);
    1314             :                 dbg->size = real_size;
    1315             :                 dbg->filename = __zend_filename;
    1316             :                 dbg->orig_filename = __zend_orig_filename;
    1317             :                 dbg->lineno = __zend_lineno;
    1318             :                 dbg->orig_lineno = __zend_orig_lineno;
    1319             : #endif
    1320      909752 :                 return ptr;
    1321             :         } else {
    1322             : #if ZEND_DEBUG
    1323             :                 size = real_size;
    1324             : #endif
    1325           1 :                 return zend_mm_alloc_huge(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1326             :         }
    1327             : }
    1328             : 
    1329             : static zend_always_inline void zend_mm_free_heap(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1330             : {
    1331    17017880 :         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1332             : 
    1333    17017880 :         if (UNEXPECTED(page_offset == 0)) {
    1334           1 :                 if (ptr != NULL) {
    1335           1 :                         zend_mm_free_huge(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1336             :                 }
    1337             :         } else {
    1338    17017879 :                 zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1339    17017879 :                 int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1340    17017879 :                 zend_mm_page_info info = chunk->map[page_num];
    1341             : 
    1342    17017879 :                 ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1343    17017879 :                 if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
    1344    16108129 :                         zend_mm_free_small(heap, ptr, ZEND_MM_SRUN_BIN_NUM(info));
    1345             :                 } else /* if (info & ZEND_MM_IS_LRUN) */ {
    1346      909750 :                         int pages_count = ZEND_MM_LRUN_PAGES(info);
    1347             : 
    1348      909750 :                         ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
    1349             :                         zend_mm_free_large(heap, chunk, page_num, pages_count);
    1350             :                 }
    1351             :         }
    1352             : }
    1353             : 
    1354     2288375 : static size_t zend_mm_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1355             : {
    1356     2288375 :         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1357             : 
    1358     2288375 :         if (UNEXPECTED(page_offset == 0)) {
    1359           0 :                 return zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1360             :         } else {
    1361             :                 zend_mm_chunk *chunk;
    1362             : #if 0 && ZEND_DEBUG
    1363             :                 zend_mm_debug_info *dbg = zend_mm_get_debug_info(heap, ptr);
    1364             :                 return dbg->size;
    1365             : #else
    1366             :                 int page_num;
    1367             :                 zend_mm_page_info info;
    1368             : 
    1369     2288375 :                 chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1370     2288375 :                 page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1371     2288375 :                 info = chunk->map[page_num];
    1372     2288375 :                 ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1373     2288375 :                 if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
    1374     2286161 :                         return bin_data_size[ZEND_MM_SRUN_BIN_NUM(info)];
    1375             :                 } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ {
    1376        2214 :                         return ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE;
    1377             :                 }
    1378             : #endif
    1379             :         }
    1380             : }
    1381             : 
    1382     8125481 : static void *zend_mm_realloc_heap(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1383             : {
    1384             :         size_t page_offset;
    1385             :         size_t old_size;
    1386             :         size_t new_size;
    1387             :         void *ret;
    1388             : #if ZEND_DEBUG
    1389             :         size_t real_size;
    1390             :         zend_mm_debug_info *dbg;
    1391             : #endif
    1392             : 
    1393     8125481 :         page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1394     8125481 :         if (UNEXPECTED(page_offset == 0)) {
    1395     2246115 :                 if (UNEXPECTED(ptr == NULL)) {
    1396     2246115 :                         return zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1397             :                 }
    1398           0 :                 old_size = zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1399             : #if ZEND_DEBUG
    1400             :                 real_size = size;
    1401             :                 size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
    1402             : #endif
    1403           0 :                 if (size > ZEND_MM_MAX_LARGE_SIZE) {
    1404             : #if ZEND_DEBUG
    1405             :                         size = real_size;
    1406             : #endif
    1407           0 :                         new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
    1408           0 :                         if (new_size == old_size) {
    1409             : #if ZEND_DEBUG
    1410             :                                 zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1411             : #else
    1412           0 :                                 zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1413             : #endif
    1414           0 :                                 return ptr;
    1415             : #ifndef _WIN32
    1416           0 :                         } else if (new_size < old_size) {
    1417             :                                 /* unmup tail */
    1418           0 :                                 zend_mm_chunk_truncate(heap, ptr, old_size, new_size);
    1419             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1420           0 :                                 heap->real_size -= old_size - new_size;
    1421             : #endif
    1422             : #if ZEND_MM_STAT
    1423           0 :                                 heap->size -= old_size - new_size;
    1424             : #endif
    1425             : #if ZEND_DEBUG
    1426             :                                 zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1427             : #else
    1428           0 :                                 zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1429             : #endif
    1430           0 :                                 return ptr;
    1431             :                         } else /* if (new_size > old_size) */ {
    1432             : #if ZEND_MM_LIMIT
    1433           0 :                                 if (heap->real_size + (new_size - old_size) > heap->limit) {
    1434           0 :                                         if (heap->overflow == 0) {
    1435             : #if ZEND_DEBUG
    1436             :                                                 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);
    1437             : #else
    1438           0 :                                                 zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, size);
    1439             : #endif
    1440             :                                                 return NULL;
    1441             :                                         }
    1442             :                                 }
    1443             : #endif
    1444             :                                 /* try to map tail right after this block */
    1445           0 :                                 if (zend_mm_mmap_fixed((char*)ptr + old_size, new_size - old_size)) {
    1446             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1447           0 :                                         heap->real_size += new_size - old_size;
    1448             : #endif
    1449             : #if ZEND_MM_STAT
    1450           0 :                                         heap->size += new_size - old_size;
    1451             : #endif
    1452             : #if ZEND_DEBUG
    1453             :                                         zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1454             : #else
    1455           0 :                                         zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1456             : #endif
    1457           0 :                                         return ptr;
    1458             :                                 }
    1459             : #endif
    1460             :                         }
    1461             :                 }
    1462             :         } else {
    1463     5879366 :                 zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1464     5879366 :                 int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1465     5879366 :                 zend_mm_page_info info = chunk->map[page_num];
    1466             : #if ZEND_DEBUG
    1467             :                 size_t real_size = size;
    1468             : 
    1469             :                 size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
    1470             : #endif
    1471             : 
    1472     5879366 :                 ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1473     5879366 :                 if (info & ZEND_MM_IS_SRUN) {
    1474             :                         int old_bin_num, bin_num;
    1475             : 
    1476     5290806 :                         old_bin_num = ZEND_MM_SRUN_BIN_NUM(info);
    1477     5290806 :                         old_size = bin_data_size[old_bin_num];
    1478     5290806 :                         bin_num = ZEND_MM_SMALL_SIZE_TO_BIN(size);
    1479     5290806 :                         if (old_bin_num == bin_num) {
    1480             : #if ZEND_DEBUG
    1481             :                                 dbg = zend_mm_get_debug_info(heap, ptr);
    1482             :                                 dbg->size = real_size;
    1483             :                                 dbg->filename = __zend_filename;
    1484             :                                 dbg->orig_filename = __zend_orig_filename;
    1485             :                                 dbg->lineno = __zend_lineno;
    1486             :                                 dbg->orig_lineno = __zend_orig_lineno;
    1487             : #endif
    1488     3004645 :                                 return ptr;
    1489             :                         }
    1490             :                 } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ {
    1491      588560 :                         ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
    1492      588560 :                         old_size = ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE;
    1493      588560 :                         if (size > ZEND_MM_MAX_SMALL_SIZE && size <= ZEND_MM_MAX_LARGE_SIZE) {
    1494      587054 :                                 new_size = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE);
    1495      587054 :                                 if (new_size == old_size) {
    1496             : #if ZEND_DEBUG
    1497             :                                         dbg = zend_mm_get_debug_info(heap, ptr);
    1498             :                                         dbg->size = real_size;
    1499             :                                         dbg->filename = __zend_filename;
    1500             :                                         dbg->orig_filename = __zend_orig_filename;
    1501             :                                         dbg->lineno = __zend_lineno;
    1502             :                                         dbg->orig_lineno = __zend_orig_lineno;
    1503             : #endif
    1504      583235 :                                         return ptr;
    1505        3819 :                                 } else if (new_size < old_size) {
    1506             :                                         /* free tail pages */
    1507         712 :                                         int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE);
    1508         712 :                                         int rest_pages_count = (int)((old_size - new_size) / ZEND_MM_PAGE_SIZE);
    1509             : 
    1510             : #if ZEND_MM_STAT
    1511         712 :                                         heap->size -= rest_pages_count * ZEND_MM_PAGE_SIZE;
    1512             : #endif
    1513         712 :                                         chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count);
    1514         712 :                                         chunk->free_pages += rest_pages_count;
    1515         712 :                                         zend_mm_bitset_reset_range(chunk->free_map, page_num + new_pages_count, rest_pages_count);
    1516             : #if ZEND_DEBUG
    1517             :                                         dbg = zend_mm_get_debug_info(heap, ptr);
    1518             :                                         dbg->size = real_size;
    1519             :                                         dbg->filename = __zend_filename;
    1520             :                                         dbg->orig_filename = __zend_orig_filename;
    1521             :                                         dbg->lineno = __zend_lineno;
    1522             :                                         dbg->orig_lineno = __zend_orig_lineno;
    1523             : #endif
    1524         712 :                                         return ptr;
    1525             :                                 } else /* if (new_size > old_size) */ {
    1526        3107 :                                         int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE);
    1527        3107 :                                         int old_pages_count = (int)(old_size / ZEND_MM_PAGE_SIZE);
    1528             : 
    1529             :                                         /* try to allocate tail pages after this block */
    1530        6205 :                                         if (page_num + new_pages_count <= ZEND_MM_PAGES &&
    1531        3098 :                                             zend_mm_bitset_is_free_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count)) {
    1532             : #if ZEND_MM_STAT
    1533             :                                                 do {
    1534        2399 :                                                         size_t size = heap->size + (new_size - old_size);
    1535        2399 :                                                         size_t peak = MAX(heap->peak, size);
    1536        2399 :                                                         heap->size = size;
    1537        2399 :                                                         heap->peak = peak;
    1538             :                                                 } while (0);
    1539             : #endif
    1540        2399 :                                                 chunk->free_pages -= new_pages_count - old_pages_count;
    1541        2399 :                                                 zend_mm_bitset_set_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count);
    1542        2399 :                                                 chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count);
    1543             : #if ZEND_DEBUG
    1544             :                                                 dbg = zend_mm_get_debug_info(heap, ptr);
    1545             :                                                 dbg->size = real_size;
    1546             :                                                 dbg->filename = __zend_filename;
    1547             :                                                 dbg->orig_filename = __zend_orig_filename;
    1548             :                                                 dbg->lineno = __zend_lineno;
    1549             :                                                 dbg->orig_lineno = __zend_orig_lineno;
    1550             : #endif
    1551        2399 :                                                 return ptr;
    1552             :                                         }
    1553             :                                 }
    1554             :                         }
    1555             :                 }
    1556             : #if ZEND_DEBUG
    1557             :                 size = real_size;
    1558             : #endif
    1559             :         }
    1560             : 
    1561             :         /* Naive reallocation */
    1562     2288375 :         old_size = zend_mm_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1563     2288375 :         ret = zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1564     2288375 :         memcpy(ret, ptr, MIN(old_size, size));
    1565             :         zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1566     2288375 :         return ret;
    1567             : }
    1568             : 
    1569             : /*********************/
    1570             : /* Huge Runs (again) */
    1571             : /*********************/
    1572             : 
    1573             : #if ZEND_DEBUG
    1574             : 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)
    1575             : #else
    1576           1 : 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)
    1577             : #endif
    1578             : {
    1579           1 :         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);
    1580           1 :         list->ptr = ptr;
    1581           1 :         list->size = size;
    1582           1 :         list->next = heap->huge_list;
    1583             : #if ZEND_DEBUG
    1584             :         list->dbg.size = dbg_size;
    1585             :         list->dbg.filename = __zend_filename;
    1586             :         list->dbg.orig_filename = __zend_orig_filename;
    1587             :         list->dbg.lineno = __zend_lineno;
    1588             :         list->dbg.orig_lineno = __zend_orig_lineno;
    1589             : #endif
    1590           1 :         heap->huge_list = list;
    1591           1 : }
    1592             : 
    1593           1 : static size_t zend_mm_del_huge_block(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1594             : {
    1595           1 :         zend_mm_huge_list *prev = NULL;
    1596           1 :         zend_mm_huge_list *list = heap->huge_list;
    1597           2 :         while (list != NULL) {
    1598           1 :                 if (list->ptr == ptr) {
    1599             :                         size_t size;
    1600             : 
    1601           1 :                         if (prev) {
    1602           0 :                                 prev->next = list->next;
    1603             :                         } else {
    1604           1 :                                 heap->huge_list = list->next;
    1605             :                         }
    1606           1 :                         size = list->size;
    1607             :                         zend_mm_free_heap(heap, list ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1608           1 :                         return size;
    1609             :                 }
    1610           0 :                 prev = list;
    1611           0 :                 list = list->next;
    1612             :         }
    1613           0 :         ZEND_MM_CHECK(0, "zend_mm_heap corrupted");
    1614             :         return 0;
    1615             : }
    1616             : 
    1617           0 : static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1618             : {
    1619           0 :         zend_mm_huge_list *list = heap->huge_list;
    1620           0 :         while (list != NULL) {
    1621           0 :                 if (list->ptr == ptr) {
    1622           0 :                         return list->size;
    1623             :                 }
    1624           0 :                 list = list->next;
    1625             :         }
    1626           0 :         ZEND_MM_CHECK(0, "zend_mm_heap corrupted");
    1627             :         return 0;
    1628             : }
    1629             : 
    1630             : #if ZEND_DEBUG
    1631             : 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)
    1632             : #else
    1633           0 : 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)
    1634             : #endif
    1635             : {
    1636           0 :         zend_mm_huge_list *list = heap->huge_list;
    1637           0 :         while (list != NULL) {
    1638           0 :                 if (list->ptr == ptr) {
    1639           0 :                         list->size = size;
    1640             : #if ZEND_DEBUG
    1641             :                         list->dbg.size = dbg_size;
    1642             :                         list->dbg.filename = __zend_filename;
    1643             :                         list->dbg.orig_filename = __zend_orig_filename;
    1644             :                         list->dbg.lineno = __zend_lineno;
    1645             :                         list->dbg.orig_lineno = __zend_orig_lineno;
    1646             : #endif
    1647           0 :                         return;
    1648             :                 }
    1649           0 :                 list = list->next;
    1650             :         }
    1651             : }
    1652             : 
    1653           1 : static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1654             : {
    1655           1 :         size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
    1656             :         void *ptr;
    1657             : 
    1658             : #if ZEND_MM_LIMIT
    1659           1 :         if (heap->real_size + new_size > heap->limit) {
    1660           0 :                 if (heap->overflow == 0) {
    1661             : #if ZEND_DEBUG
    1662             :                         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);
    1663             : #else
    1664           0 :                         zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, size);
    1665             : #endif
    1666             :                         return NULL;
    1667             :                 }
    1668             :         }
    1669             : #endif
    1670           1 :         ptr = zend_mm_chunk_alloc(heap, new_size, ZEND_MM_CHUNK_SIZE);
    1671           1 :         if (UNEXPECTED(ptr == NULL)) {
    1672             :                 /* insufficient memory */
    1673             : #if !ZEND_MM_LIMIT
    1674             :                 zend_mm_safe_error(heap, "Out of memory");
    1675             : #elif ZEND_DEBUG
    1676             :                 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);
    1677             : #else
    1678           0 :                 zend_mm_safe_error(heap, "Out of memory (allocated %zu) (tried to allocate %zu bytes)", heap->real_size, size);
    1679             : #endif
    1680             :                 return NULL;
    1681             :         }
    1682             : #if ZEND_DEBUG
    1683             :         zend_mm_add_huge_block(heap, ptr, new_size, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1684             : #else
    1685           1 :         zend_mm_add_huge_block(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1686             : #endif
    1687             : #if ZEND_MM_STAT
    1688             :         do {
    1689           1 :                 size_t size = heap->real_size + new_size;
    1690           1 :                 size_t peak = MAX(heap->real_peak, size);
    1691           1 :                 heap->real_size = size;
    1692           1 :                 heap->real_peak = peak;
    1693             :         } while (0);
    1694             :         do {
    1695           1 :                 size_t size = heap->size + new_size;
    1696           1 :                 size_t peak = MAX(heap->peak, size);
    1697           1 :                 heap->size = size;
    1698           1 :                 heap->peak = peak;
    1699             :         } while (0);
    1700             : #elif ZEND_MM_LIMIT
    1701             :         heap->real_size += new_size;
    1702             : #endif
    1703           1 :         return ptr;
    1704             : }
    1705             : 
    1706           1 : static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1707             : {
    1708             :         size_t size;
    1709             : 
    1710           1 :         ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE) == 0, "zend_mm_heap corrupted");
    1711           1 :         size = zend_mm_del_huge_block(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1712           1 :         zend_mm_chunk_free(heap, ptr, size);
    1713             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1714           1 :         heap->real_size -= size;
    1715             : #endif
    1716             : #if ZEND_MM_STAT
    1717           1 :         heap->size -= size;
    1718             : #endif
    1719           1 : }
    1720             : 
    1721             : /******************/
    1722             : /* Initialization */
    1723             : /******************/
    1724             : 
    1725          10 : static zend_mm_heap *zend_mm_init(void)
    1726             : {
    1727          10 :         zend_mm_chunk *chunk = (zend_mm_chunk*)zend_mm_chunk_alloc_int(ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
    1728             :         zend_mm_heap *heap;
    1729             : 
    1730          10 :         if (UNEXPECTED(chunk == NULL)) {
    1731             : #if ZEND_MM_ERROR
    1732             : #ifdef _WIN32
    1733             :                 stderr_last_error("Can't initialize heap");
    1734             : #else
    1735           0 :                 fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
    1736             : #endif
    1737             : #endif
    1738           0 :                 return NULL;
    1739             :         }
    1740          10 :         heap = &chunk->heap_slot;
    1741          10 :         chunk->heap = heap;
    1742          10 :         chunk->next = chunk;
    1743          10 :         chunk->prev = chunk;
    1744          10 :         chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    1745          10 :         chunk->free_tail = ZEND_MM_FIRST_PAGE;
    1746          10 :         chunk->num = 0;
    1747          10 :         chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1;
    1748          10 :         chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
    1749          10 :         heap->main_chunk = chunk;
    1750          10 :         heap->cached_chunks = NULL;
    1751          10 :         heap->chunks_count = 1;
    1752          10 :         heap->peak_chunks_count = 1;
    1753          10 :         heap->cached_chunks_count = 0;
    1754          10 :         heap->avg_chunks_count = 1.0;
    1755             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1756          10 :         heap->real_size = ZEND_MM_CHUNK_SIZE;
    1757             : #endif
    1758             : #if ZEND_MM_STAT
    1759          10 :         heap->real_peak = ZEND_MM_CHUNK_SIZE;
    1760          10 :         heap->size = 0;
    1761          10 :         heap->peak = 0;
    1762             : #endif
    1763             : #if ZEND_MM_LIMIT
    1764          10 :         heap->limit = (Z_L(-1) >> Z_L(1));
    1765          10 :         heap->overflow = 0;
    1766             : #endif
    1767             : #if ZEND_MM_CUSTOM
    1768          10 :         heap->use_custom_heap = 0;
    1769             : #endif
    1770             : #if ZEND_MM_STORAGE
    1771          10 :         heap->storage = NULL;
    1772             : #endif
    1773          10 :         heap->huge_list = NULL;
    1774          10 :         return heap;
    1775             : }
    1776             : 
    1777             : #if ZEND_DEBUG
    1778             : /******************/
    1779             : /* Leak detection */
    1780             : /******************/
    1781             : 
    1782             : static zend_long zend_mm_find_leaks_small(zend_mm_chunk *p, int i, int j, zend_leak_info *leak)
    1783             : {
    1784             :     int empty = 1;
    1785             :         zend_long count = 0;
    1786             :         int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]);
    1787             :         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)));
    1788             : 
    1789             :         while (j < bin_elements[bin_num]) {
    1790             :                 if (dbg->size != 0) {
    1791             :                         if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) {
    1792             :                                 count++;
    1793             :                                 dbg->size = 0;
    1794             :                                 dbg->filename = NULL;
    1795             :                                 dbg->lineno = 0;
    1796             :                         } else {
    1797             :                                 empty = 0;
    1798             :                         }
    1799             :                 }
    1800             :                 j++;
    1801             :                 dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]);
    1802             :         }
    1803             :         if (empty) {
    1804             :                 zend_mm_bitset_reset_range(p->free_map, i, bin_pages[bin_num]);
    1805             :         }
    1806             :         return count;
    1807             : }
    1808             : 
    1809             : static zend_long zend_mm_find_leaks(zend_mm_heap *heap, zend_mm_chunk *p, int i, zend_leak_info *leak)
    1810             : {
    1811             :         zend_long count = 0;
    1812             : 
    1813             :         do {
    1814             :                 while (i < p->free_tail) {
    1815             :                         if (zend_mm_bitset_is_set(p->free_map, i)) {
    1816             :                                 if (p->map[i] & ZEND_MM_IS_SRUN) {
    1817             :                                         int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]);
    1818             :                                         count += zend_mm_find_leaks_small(p, i, 0, leak);
    1819             :                                         i += bin_pages[bin_num];
    1820             :                                 } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ {
    1821             :                                         int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]);
    1822             :                                         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)));
    1823             : 
    1824             :                                         if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) {
    1825             :                                                 count++;
    1826             :                                         }
    1827             :                                         zend_mm_bitset_reset_range(p->free_map, i, pages_count);
    1828             :                                         i += pages_count;
    1829             :                                 }
    1830             :                         } else {
    1831             :                                 i++;
    1832             :                         }
    1833             :                 }
    1834             :                 p = p->next;
    1835             :         } while (p != heap->main_chunk);
    1836             :         return count;
    1837             : }
    1838             : 
    1839             : static void zend_mm_check_leaks(zend_mm_heap *heap TSRMLS_DC)
    1840             : {
    1841             :         zend_mm_huge_list *list;
    1842             :         zend_mm_chunk *p;
    1843             :         zend_leak_info leak;
    1844             :         zend_long repeated = 0;
    1845             :         uint32_t total = 0;
    1846             :         int i, j;
    1847             : 
    1848             :         /* find leaked huge blocks and free them */
    1849             :         list = heap->huge_list;
    1850             :         while (list) {
    1851             :                 zend_mm_huge_list *q = list;
    1852             : 
    1853             :                 heap->huge_list = list->next;
    1854             : 
    1855             :                 leak.addr = list->ptr;
    1856             :                 leak.size = list->dbg.size;
    1857             :                 leak.filename = list->dbg.filename;
    1858             :                 leak.orig_filename = list->dbg.orig_filename;
    1859             :                 leak.lineno = list->dbg.lineno;
    1860             :                 leak.orig_lineno = list->dbg.orig_lineno;
    1861             : 
    1862             :                 zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
    1863             :                 zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak TSRMLS_CC);
    1864             : //???           repeated = zend_mm_find_leaks_huge(segment, p);
    1865             :                 total += 1 + repeated;
    1866             :                 if (repeated) {
    1867             :                         zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated TSRMLS_CC);
    1868             :                 }
    1869             : 
    1870             :                 list = list->next;
    1871             :                 zend_mm_chunk_free(heap, q->ptr, q->size);
    1872             :                 zend_mm_free_heap(heap, q, NULL, 0, NULL, 0);
    1873             :         }
    1874             : 
    1875             :         /* for each chunk */
    1876             :         p = heap->main_chunk;
    1877             :         do {
    1878             :                 i = ZEND_MM_FIRST_PAGE;
    1879             :                 while (i < p->free_tail) {
    1880             :                         if (zend_mm_bitset_is_set(p->free_map, i)) {
    1881             :                                 if (p->map[i] & ZEND_MM_IS_SRUN) {
    1882             :                                         int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]);
    1883             :                                         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)));
    1884             : 
    1885             :                                         j = 0;
    1886             :                                         while (j < bin_elements[bin_num]) {
    1887             :                                                 if (dbg->size != 0) {
    1888             :                                                         leak.addr = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] * j);
    1889             :                                                         leak.size = dbg->size;
    1890             :                                                         leak.filename = dbg->filename;
    1891             :                                                         leak.orig_filename = dbg->orig_filename;
    1892             :                                                         leak.lineno = dbg->lineno;
    1893             :                                                         leak.orig_lineno = dbg->orig_lineno;
    1894             : 
    1895             :                                                         zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
    1896             :                                                         zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak TSRMLS_CC);
    1897             : 
    1898             :                                                         dbg->size = 0;
    1899             :                                                         dbg->filename = NULL;
    1900             :                                                         dbg->lineno = 0;
    1901             : 
    1902             :                                                         repeated = zend_mm_find_leaks_small(p, i, j + 1, &leak) +
    1903             :                                                                    zend_mm_find_leaks(heap, p, i + bin_pages[bin_num], &leak);
    1904             :                                                         total += 1 + repeated;
    1905             :                                                         if (repeated) {
    1906             :                                                                 zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated TSRMLS_CC);
    1907             :                                                         }
    1908             :                                                 }
    1909             :                                                 dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]);
    1910             :                                                 j++;
    1911             :                                         }
    1912             :                                         i += bin_pages[bin_num];
    1913             :                                 } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ {
    1914             :                                         int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]);
    1915             :                                         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)));
    1916             : 
    1917             :                                         leak.addr = (void*)((char*)p + ZEND_MM_PAGE_SIZE * i);
    1918             :                                         leak.size = dbg->size;
    1919             :                                         leak.filename = dbg->filename;
    1920             :                                         leak.orig_filename = dbg->orig_filename;
    1921             :                                         leak.lineno = dbg->lineno;
    1922             :                                         leak.orig_lineno = dbg->orig_lineno;
    1923             : 
    1924             :                                         zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
    1925             :                                         zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak TSRMLS_CC);
    1926             : 
    1927             :                                         zend_mm_bitset_reset_range(p->free_map, i, pages_count);
    1928             : 
    1929             :                                         repeated = zend_mm_find_leaks(heap, p, i + pages_count, &leak);
    1930             :                                         total += 1 + repeated;
    1931             :                                         if (repeated) {
    1932             :                                                 zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated TSRMLS_CC);
    1933             :                                         }
    1934             :                                         i += pages_count;
    1935             :                                 }
    1936             :                         } else {
    1937             :                                 i++;
    1938             :                         }
    1939             :                 }
    1940             :                 p = p->next;
    1941             :         } while (p != heap->main_chunk);
    1942             :         if (total) {
    1943             :                 zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &total TSRMLS_CC);
    1944             :         }
    1945             : }
    1946             : #endif
    1947             : 
    1948       61893 : void zend_mm_shutdown(zend_mm_heap *heap, int full, int silent TSRMLS_DC)
    1949             : {
    1950             :         zend_mm_chunk *p;
    1951             :         zend_mm_huge_list *list;
    1952             : 
    1953             : #if ZEND_MM_CUSTOM
    1954       61893 :         if (heap->use_custom_heap) {
    1955       61863 :                 return;
    1956             :         }
    1957             : #endif
    1958             : 
    1959             : #if ZEND_DEBUG
    1960             :         if (!silent) {
    1961             :                 zend_mm_check_leaks(heap TSRMLS_CC);
    1962             :         }
    1963             : #endif
    1964             : 
    1965             :         /* free huge blocks */
    1966          30 :         list = heap->huge_list;
    1967          60 :         while (list) {
    1968           0 :                 zend_mm_huge_list *q = list;
    1969           0 :                 list = list->next;
    1970           0 :                 zend_mm_chunk_free(heap, q->ptr, q->size);
    1971             :         }
    1972             : 
    1973             :         /* move all chunks except of the first one into the cache */
    1974          30 :         p = heap->main_chunk->next;
    1975          62 :         while (p != heap->main_chunk) {
    1976           2 :                 zend_mm_chunk *q = p->next;
    1977           2 :                 p->next = heap->cached_chunks;
    1978           2 :                 heap->cached_chunks = p;
    1979           2 :                 p = q;
    1980           2 :                 heap->chunks_count--;
    1981           2 :                 heap->cached_chunks_count++;
    1982             :         }
    1983             : 
    1984          30 :         if (full) {
    1985             :                 /* free all cached chunks */
    1986          22 :                 while (heap->cached_chunks) {
    1987           2 :                         p = heap->cached_chunks;
    1988           2 :                         heap->cached_chunks = p->next;
    1989           2 :                         zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE);
    1990             :                 }
    1991             :                 /* free the first chunk */
    1992             : #if ZEND_MM_STORAGE
    1993          10 :                 if (UNEXPECTED(heap->storage)) {
    1994           0 :                         zend_mm_storage *storage = heap->storage;
    1995           0 :                         zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE);
    1996           0 :                         storage->dtor(storage);
    1997             :                 } else {
    1998          10 :                         zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE);
    1999             :                 }
    2000             : #else
    2001             :                 zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE);
    2002             : #endif
    2003             :         } else {
    2004             :                 zend_mm_heap old_heap;
    2005             : 
    2006             :                 /* free some cached chunks to keep average count */
    2007          20 :                 heap->avg_chunks_count = (heap->avg_chunks_count + (double)heap->peak_chunks_count) / 2.0;
    2008          40 :                 while ((double)heap->cached_chunks_count + 0.9 > heap->avg_chunks_count &&
    2009           0 :                        heap->cached_chunks) {
    2010           0 :                         p = heap->cached_chunks;
    2011           0 :                         heap->cached_chunks = p->next;
    2012           0 :                         zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE);
    2013           0 :                         heap->cached_chunks_count--;
    2014             :                 }
    2015             :                 /* clear cached chunks */
    2016          20 :                 p = heap->cached_chunks;
    2017          42 :                 while (p != NULL) {
    2018           2 :                         zend_mm_chunk *q = p->next;
    2019           2 :                         memset(p, 0, sizeof(zend_mm_chunk));
    2020           2 :                         p->next = q;
    2021           2 :                         p = q;
    2022             :                 }
    2023             : 
    2024             :                 /* reinitialize the first chunk and heap */
    2025          20 :                 old_heap = *heap;
    2026          20 :                 p = heap->main_chunk;
    2027          20 :                 memset(p, 0, ZEND_MM_FIRST_PAGE * ZEND_MM_PAGE_SIZE);
    2028          20 :                 *heap = old_heap;
    2029          20 :                 memset(heap->free_slot, 0, sizeof(heap->free_slot));
    2030          20 :                 heap->main_chunk = p;
    2031          20 :                 p->heap = &p->heap_slot;
    2032          20 :                 p->next = p;
    2033          20 :                 p->prev = p;
    2034          20 :                 p->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    2035          20 :                 p->free_tail = ZEND_MM_FIRST_PAGE;
    2036          20 :                 p->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1;
    2037          20 :                 p->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
    2038          20 :                 heap->chunks_count = 1;
    2039          20 :                 heap->peak_chunks_count = 1;
    2040             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    2041          20 :                 heap->real_size = ZEND_MM_CHUNK_SIZE;
    2042             : #endif
    2043             : #if ZEND_MM_STAT
    2044          20 :                 heap->real_peak = ZEND_MM_CHUNK_SIZE;
    2045             : #endif
    2046             :         }
    2047             : }
    2048             : 
    2049             : /**************/
    2050             : /* PUBLIC API */
    2051             : /**************/
    2052             : 
    2053           0 : ZEND_API void* ZEND_FASTCALL _zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2054             : {
    2055           0 :         return zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2056             : }
    2057             : 
    2058           0 : ZEND_API void ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2059             : {
    2060             :         zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2061           0 : }
    2062             : 
    2063           0 : void* ZEND_FASTCALL _zend_mm_realloc(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2064             : {
    2065           0 :         return zend_mm_realloc_heap(heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2066             : }
    2067             : 
    2068           0 : 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)
    2069             : {
    2070           0 :         return zend_mm_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2071             : }
    2072             : 
    2073             : /**********************/
    2074             : /* Allocation Manager */
    2075             : /**********************/
    2076             : 
    2077             : typedef struct _zend_alloc_globals {
    2078             :         zend_mm_heap *mm_heap;
    2079             : } zend_alloc_globals;
    2080             : 
    2081             : #ifdef ZTS
    2082             : static int alloc_globals_id;
    2083             : # define AG(v) TSRMG(alloc_globals_id, zend_alloc_globals *, v)
    2084             : #else
    2085             : # define AG(v) (alloc_globals.v)
    2086             : static zend_alloc_globals alloc_globals;
    2087             : #endif
    2088             : 
    2089         139 : ZEND_API int is_zend_mm(TSRMLS_D)
    2090             : {
    2091             : #if ZEND_MM_CUSTOM
    2092         139 :         return !AG(mm_heap)->use_custom_heap;
    2093             : #else
    2094             :         return 1;
    2095             : #endif
    2096             : }
    2097             : 
    2098             : #if !ZEND_DEBUG && !defined(_WIN32)
    2099             : #undef _emalloc
    2100             : 
    2101             : #if ZEND_MM_CUSTOM
    2102             : # define ZEND_MM_CUSTOM_ALLOCATOR(size) do { \
    2103             :                 if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
    2104             :                         return AG(mm_heap)->_malloc(size); \
    2105             :                 } \
    2106             :         } while (0)
    2107             : # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) do { \
    2108             :                 if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
    2109             :                         AG(mm_heap)->_free(ptr); \
    2110             :                         return; \
    2111             :                 } \
    2112             :         } while (0)
    2113             : #else
    2114             : # define ZEND_MM_CUSTOM_ALLOCATOR(size)
    2115             : # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr)
    2116             : #endif
    2117             : 
    2118             : # define _ZEND_BIN_ALLOCATOR(_num, _size, _elements, _pages, x, y) \
    2119             :         ZEND_API void* ZEND_FASTCALL _emalloc_ ## _size(void) { \
    2120             :                 TSRMLS_FETCH(); \
    2121             :                 ZEND_MM_CUSTOM_ALLOCATOR(_size); \
    2122             :                 return zend_mm_alloc_small(AG(mm_heap), _size, _num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
    2123             :         }
    2124             : 
    2125    11957943 : ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR, x, y)
    2126             : 
    2127       67134 : ZEND_API void* ZEND_FASTCALL _emalloc_large(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2128             : {
    2129             :         TSRMLS_FETCH();
    2130             : 
    2131       67134 :         ZEND_MM_CUSTOM_ALLOCATOR(size);
    2132           0 :         return zend_mm_alloc_large(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2133             : }
    2134             : 
    2135           0 : ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size)
    2136             : {
    2137             :         TSRMLS_FETCH();
    2138             : 
    2139           0 :         ZEND_MM_CUSTOM_ALLOCATOR(size);
    2140           0 :         return zend_mm_alloc_huge(AG(mm_heap), size);
    2141             : }
    2142             : 
    2143             : #if ZEND_DEBUG
    2144             : # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \
    2145             :         ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \
    2146             :                 TSRMLS_FETCH(); \
    2147             :                 ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \
    2148             :                 { \
    2149             :                         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); \
    2150             :                         zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \
    2151             :                         int page_num = page_offset / ZEND_MM_PAGE_SIZE; \
    2152             :                         ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \
    2153             :                         ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_SRUN); \
    2154             :                         ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(chunk->map[page_num]) == _num); \
    2155             :                         zend_mm_free_small(AG(mm_heap), ptr, _num); \
    2156             :                 } \
    2157             :         }
    2158             : #else
    2159             : # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \
    2160             :         ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \
    2161             :                 TSRMLS_FETCH(); \
    2162             :                 ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \
    2163             :                 { \
    2164             :                         zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \
    2165             :                         ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \
    2166             :                         zend_mm_free_small(AG(mm_heap), ptr, _num); \
    2167             :                 } \
    2168             :         }
    2169             : #endif
    2170             : 
    2171     4339094 : ZEND_MM_BINS_INFO(_ZEND_BIN_FREE, x, y)
    2172             : 
    2173           0 : ZEND_API void ZEND_FASTCALL _efree_large(void *ptr, size_t size)
    2174             : {
    2175             :         TSRMLS_FETCH();
    2176             : 
    2177           0 :         ZEND_MM_CUSTOM_DEALLOCATOR(ptr);
    2178             :         {
    2179           0 :                 size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    2180           0 :                 zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    2181           0 :                 int page_num = page_offset / ZEND_MM_PAGE_SIZE;
    2182           0 :                 int pages_count = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE) / ZEND_MM_PAGE_SIZE;
    2183             : 
    2184           0 :                 ZEND_MM_CHECK(chunk->heap == AG(mm_heap) && ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
    2185             :                 ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_LRUN);
    2186             :                 ZEND_ASSERT(ZEND_MM_LRUN_PAGES(chunk->map[page_num]) == pages_count);
    2187           0 :                 zend_mm_free_large(AG(mm_heap), chunk, page_num, pages_count);
    2188             :         }
    2189             : }
    2190             : 
    2191           0 : ZEND_API void ZEND_FASTCALL _efree_huge(void *ptr, size_t size)
    2192             : {
    2193             :         TSRMLS_FETCH();
    2194             : 
    2195           0 :         ZEND_MM_CUSTOM_DEALLOCATOR(ptr);
    2196             :         // TODO: use size???
    2197           0 :         zend_mm_free_huge(AG(mm_heap), ptr);
    2198             : }
    2199             : #endif
    2200             : 
    2201    76760028 : ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2202             : {
    2203             :         TSRMLS_FETCH();
    2204             : 
    2205             : #if ZEND_MM_CUSTOM
    2206    76760028 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2207    66214620 :                 return AG(mm_heap)->_malloc(size);
    2208             :         }
    2209             : #endif
    2210    21090816 :         return zend_mm_alloc_heap(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2211             : }
    2212             : 
    2213    84217176 : ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2214             : {
    2215             :         TSRMLS_FETCH();
    2216             : 
    2217             : #if ZEND_MM_CUSTOM
    2218    84217176 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2219    69487672 :                 AG(mm_heap)->_free(ptr);
    2220    69487672 :                 return;
    2221             :         }
    2222             : #endif
    2223    14729504 :         zend_mm_free_heap(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2224             : }
    2225             : 
    2226    12290005 : ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2227             : {
    2228             :         TSRMLS_FETCH();
    2229             : 
    2230    12290005 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2231     4164524 :                 return AG(mm_heap)->_realloc(ptr, size);
    2232             :         }
    2233     8125481 :         return zend_mm_realloc_heap(AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2234             : }
    2235             : 
    2236           0 : ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2237             : {
    2238           0 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2239           0 :                 return 0;
    2240             :         }
    2241           0 :         return zend_mm_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2242             : }
    2243             : 
    2244             : static zend_always_inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
    2245             : {
    2246             :         int overflow;
    2247    18864508 :         size_t ret = zend_safe_address(nmemb, size, offset, &overflow);
    2248             : 
    2249    18864508 :         if (UNEXPECTED(overflow)) {
    2250           1 :                 zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
    2251             :                 return 0;
    2252             :         }
    2253    18864507 :         return ret;
    2254             : }
    2255             : 
    2256             : 
    2257     5589674 : 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)
    2258             : {
    2259     5589674 :         return emalloc_rel(safe_address(nmemb, size, offset));
    2260             : }
    2261             : 
    2262     6770852 : ZEND_API void* ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset)
    2263             : {
    2264     6770852 :         return pemalloc(safe_address(nmemb, size, offset), 1);
    2265             : }
    2266             : 
    2267      337352 : 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)
    2268             : {
    2269      337351 :         return erealloc_rel(ptr, safe_address(nmemb, size, offset));
    2270             : }
    2271             : 
    2272     6166630 : ZEND_API void* ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset)
    2273             : {
    2274     6166630 :         return perealloc(ptr, safe_address(nmemb, size, offset), 1);
    2275             : }
    2276             : 
    2277             : 
    2278     1443282 : ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2279             : {
    2280             :         void *p;
    2281             : #ifdef ZEND_SIGNALS
    2282             :         TSRMLS_FETCH();
    2283             : #endif
    2284     1443282 :         HANDLE_BLOCK_INTERRUPTIONS();
    2285             : 
    2286     1443282 :         p = _safe_emalloc(nmemb, size, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2287     1443282 :         if (UNEXPECTED(p == NULL)) {
    2288           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2289           0 :                 return p;
    2290             :         }
    2291     1443282 :         memset(p, 0, size * nmemb);
    2292     1443282 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2293     1443282 :         return p;
    2294             : }
    2295             : 
    2296      585796 : ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2297             : {
    2298             :         size_t length;
    2299             :         char *p;
    2300             : #ifdef ZEND_SIGNALS
    2301             :         TSRMLS_FETCH();
    2302             : #endif
    2303             : 
    2304      585796 :         HANDLE_BLOCK_INTERRUPTIONS();
    2305             : 
    2306      585796 :         length = strlen(s)+1;
    2307      585796 :         p = (char *) _emalloc(length ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2308      585796 :         if (UNEXPECTED(p == NULL)) {
    2309           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2310           0 :                 return p;
    2311             :         }
    2312      585796 :         memcpy(p, s, length);
    2313      585796 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2314      585796 :         return p;
    2315             : }
    2316             : 
    2317      536194 : ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2318             : {
    2319             :         char *p;
    2320             : #ifdef ZEND_SIGNALS
    2321             :         TSRMLS_FETCH();
    2322             : #endif
    2323             : 
    2324      536194 :         HANDLE_BLOCK_INTERRUPTIONS();
    2325             : 
    2326      536194 :         p = (char *) _emalloc(length+1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2327      536194 :         if (UNEXPECTED(p == NULL)) {
    2328           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2329           0 :                 return p;
    2330             :         }
    2331      536194 :         memcpy(p, s, length);
    2332      536194 :         p[length] = 0;
    2333      536194 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2334      536194 :         return p;
    2335             : }
    2336             : 
    2337             : 
    2338       60203 : ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
    2339             : {
    2340             :         char *p;
    2341             : #ifdef ZEND_SIGNALS
    2342             :         TSRMLS_FETCH();
    2343             : #endif
    2344             : 
    2345       60203 :         HANDLE_BLOCK_INTERRUPTIONS();
    2346             : 
    2347       60203 :         p = (char *) malloc(length+1);
    2348       60203 :         if (UNEXPECTED(p == NULL)) {
    2349           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2350           0 :                 return p;
    2351             :         }
    2352       60203 :         if (length) {
    2353       60203 :                 memcpy(p, s, length);
    2354             :         }
    2355       60203 :         p[length] = 0;
    2356       60203 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2357       60203 :         return p;
    2358             : }
    2359             : 
    2360             : 
    2361       21446 : ZEND_API int zend_set_memory_limit(size_t memory_limit TSRMLS_DC)
    2362             : {
    2363             : #if ZEND_MM_LIMIT
    2364       21446 :         AG(mm_heap)->limit = (memory_limit >= ZEND_MM_CHUNK_SIZE) ? memory_limit : ZEND_MM_CHUNK_SIZE;
    2365             : #endif
    2366       21446 :         return SUCCESS;
    2367             : }
    2368             : 
    2369        1571 : ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC)
    2370             : {
    2371             : #if ZEND_MM_STAT
    2372        1571 :         if (real_usage) {
    2373         441 :                 return AG(mm_heap)->real_size;
    2374             :         } else {
    2375        1130 :                 size_t usage = AG(mm_heap)->size;
    2376        1130 :                 return usage;
    2377             :         }
    2378             : #endif
    2379             :         return 0;
    2380             : }
    2381             : 
    2382           0 : ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC)
    2383             : {
    2384             : #if ZEND_MM_STAT
    2385           0 :         if (real_usage) {
    2386           0 :                 return AG(mm_heap)->real_peak;
    2387             :         } else {
    2388           0 :                 return AG(mm_heap)->peak;
    2389             :         }
    2390             : #endif
    2391             :         return 0;
    2392             : }
    2393             : 
    2394       61893 : ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC)
    2395             : {
    2396       61893 :         zend_mm_shutdown(AG(mm_heap), full_shutdown, silent TSRMLS_CC);
    2397       61893 : }
    2398             : 
    2399       20622 : static void alloc_globals_ctor(zend_alloc_globals *alloc_globals TSRMLS_DC)
    2400             : {
    2401             : #if ZEND_MM_CUSTOM
    2402       20622 :         char *tmp = getenv("USE_ZEND_ALLOC");
    2403             : 
    2404       20622 :         if (tmp && !zend_atoi(tmp, 0)) {
    2405       20612 :                 alloc_globals->mm_heap = malloc(sizeof(zend_mm_heap));
    2406       20612 :                 memset(alloc_globals->mm_heap, 0, sizeof(zend_mm_heap));
    2407       20612 :                 alloc_globals->mm_heap->use_custom_heap = 1;
    2408       20612 :                 alloc_globals->mm_heap->_malloc = malloc;
    2409       20612 :                 alloc_globals->mm_heap->_free = free;
    2410       20612 :                 alloc_globals->mm_heap->_realloc = realloc;
    2411       20612 :                 return;
    2412             :         }
    2413             : #endif
    2414          10 :         alloc_globals->mm_heap = zend_mm_init();
    2415             : }
    2416             : 
    2417             : #ifdef ZTS
    2418             : static void alloc_globals_dtor(zend_alloc_globals *alloc_globals TSRMLS_DC)
    2419             : {
    2420             :         shutdown_memory_manager(1, 1 TSRMLS_CC);
    2421             : }
    2422             : #endif
    2423             : 
    2424       20622 : ZEND_API void start_memory_manager(TSRMLS_D)
    2425             : {
    2426             : #ifdef ZTS
    2427             :         ts_allocate_id(&alloc_globals_id, sizeof(zend_alloc_globals), (ts_allocate_ctor) alloc_globals_ctor, (ts_allocate_dtor) alloc_globals_dtor);
    2428             : #else
    2429       20622 :         alloc_globals_ctor(&alloc_globals);
    2430             : #endif
    2431             : #ifndef _WIN32
    2432             : #  if defined(_SC_PAGESIZE)
    2433       20622 :         REAL_PAGE_SIZE = sysconf(_SC_PAGESIZE);
    2434             : #  elif defined(_SC_PAGE_SIZE)
    2435             :         REAL_PAGE_SIZE = sysconf(_SC_PAGE_SIZE);
    2436             : #  endif
    2437             : #endif
    2438       20622 : }
    2439             : 
    2440           0 : ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC)
    2441             : {
    2442             :         zend_mm_heap *old_heap;
    2443             : 
    2444           0 :         old_heap = AG(mm_heap);
    2445           0 :         AG(mm_heap) = (zend_mm_heap*)new_heap;
    2446           0 :         return (zend_mm_heap*)old_heap;
    2447             : }
    2448             : 
    2449           0 : ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
    2450             :                                           void* (*_malloc)(size_t),
    2451             :                                           void  (*_free)(void*),
    2452             :                                           void* (*_realloc)(void*, size_t))
    2453             : {
    2454             : #if ZEND_MM_CUSTOM
    2455           0 :         zend_mm_heap *_heap = (zend_mm_heap*)heap;
    2456             : 
    2457           0 :         _heap->use_custom_heap = 1;
    2458           0 :         _heap->_malloc = _malloc;
    2459           0 :         _heap->_free = _free;
    2460           0 :         _heap->_realloc = _realloc;
    2461             : #endif
    2462           0 : }
    2463             : 
    2464           0 : ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
    2465             :                                           void* (**_malloc)(size_t),
    2466             :                                           void  (**_free)(void*),
    2467             :                                           void* (**_realloc)(void*, size_t))
    2468             : {
    2469             : #if ZEND_MM_CUSTOM
    2470           0 :         zend_mm_heap *_heap = (zend_mm_heap*)heap;
    2471             : 
    2472           0 :         if (heap->use_custom_heap) {
    2473           0 :                 *_malloc = _heap->_malloc;
    2474           0 :                 *_free = _heap->_free;
    2475           0 :                 *_realloc = _heap->_realloc;
    2476             :         } else {
    2477           0 :                 *_malloc = NULL;
    2478           0 :                 *_free = NULL;
    2479           0 :                 *_realloc = NULL;
    2480             :         }
    2481             : #else
    2482             :         *_malloc = NULL;
    2483             :         *_free = NULL;
    2484             :         *_realloc = NULL;
    2485             : #endif
    2486           0 : }
    2487             : 
    2488           0 : ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap)
    2489             : {
    2490             : #if ZEND_MM_CUSTOM
    2491           0 :         return heap->storage;
    2492             : #else
    2493             :         return NULL
    2494             : #endif
    2495             : }
    2496             : 
    2497           0 : ZEND_API zend_mm_heap *zend_mm_startup(void)
    2498             : {
    2499           0 :         return zend_mm_init();
    2500             : }
    2501             : 
    2502           0 : ZEND_API zend_mm_heap *zend_mm_startup_ex(zend_mm_storage *storage)
    2503             : {
    2504             : #if ZEND_MM_STORAGE
    2505           0 :         zend_mm_chunk *chunk = (zend_mm_chunk*)storage->chunk_alloc(storage, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
    2506             :         zend_mm_heap *heap;
    2507             : 
    2508           0 :         if (UNEXPECTED(chunk == NULL)) {
    2509             : #if ZEND_MM_ERROR
    2510             : #ifdef _WIN32
    2511             :                 stderr_last_error("Can't initialize heap");
    2512             : #else
    2513           0 :                 fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
    2514             : #endif
    2515             : #endif
    2516           0 :                 return NULL;
    2517             :         }
    2518           0 :         heap = &chunk->heap_slot;
    2519           0 :         chunk->heap = heap;
    2520           0 :         chunk->next = chunk;
    2521           0 :         chunk->prev = chunk;
    2522           0 :         chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    2523           0 :         chunk->free_tail = ZEND_MM_FIRST_PAGE;
    2524           0 :         chunk->num = 0;
    2525           0 :         chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1;
    2526           0 :         chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
    2527           0 :         heap->main_chunk = chunk;
    2528           0 :         heap->cached_chunks = NULL;
    2529           0 :         heap->chunks_count = 1;
    2530           0 :         heap->peak_chunks_count = 1;
    2531           0 :         heap->cached_chunks_count = 0;
    2532           0 :         heap->avg_chunks_count = 1.0;
    2533             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    2534           0 :         heap->real_size = ZEND_MM_CHUNK_SIZE;
    2535             : #endif
    2536             : #if ZEND_MM_STAT
    2537           0 :         heap->real_peak = ZEND_MM_CHUNK_SIZE;
    2538           0 :         heap->size = 0;
    2539           0 :         heap->peak = 0;
    2540             : #endif
    2541             : #if ZEND_MM_LIMIT
    2542           0 :         heap->limit = (Z_L(-1) >> Z_L(1));
    2543           0 :         heap->overflow = 0;
    2544             : #endif
    2545             : #if ZEND_MM_CUSTOM
    2546           0 :         heap->use_custom_heap = 0;
    2547             : #endif
    2548           0 :         heap->storage = storage;
    2549           0 :         heap->huge_list = NULL;
    2550           0 :         return heap;
    2551             : #else
    2552             :         return NULL;
    2553             : #endif
    2554             : }
    2555             : 
    2556             : /*
    2557             :  * Local variables:
    2558             :  * tab-width: 4
    2559             :  * c-basic-offset: 4
    2560             :  * indent-tabs-mode: t
    2561             :  * End:
    2562             :  */

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:09 +0000 (6 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.