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: 442 695 63.6 %
Date: 2015-03-01 Functions: 55 115 47.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 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             : 
     372           0 :         heap->overflow = 1;
     373           0 :         zend_try {
     374           0 :                 zend_error_noreturn(E_ERROR,
     375             :                         format,
     376             :                         limit,
     377             : #if ZEND_DEBUG
     378             :                         filename,
     379             :                         lineno,
     380             : #endif
     381             :                         size);
     382           0 :         } zend_catch {
     383           0 :         }  zend_end_try();
     384           0 :         heap->overflow = 0;
     385           0 :         zend_bailout();
     386           0 :         exit(1);
     387             : }
     388             : 
     389             : #ifdef _WIN32
     390             : void
     391             : stderr_last_error(char *msg)
     392             : {
     393             :         LPSTR buf = NULL;
     394             :         DWORD err = GetLastError();
     395             : 
     396             :         if (!FormatMessage(
     397             :                         FORMAT_MESSAGE_ALLOCATE_BUFFER |
     398             :                         FORMAT_MESSAGE_FROM_SYSTEM |
     399             :                         FORMAT_MESSAGE_IGNORE_INSERTS,
     400             :                         NULL,
     401             :                         err,
     402             :                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
     403             :                         (LPSTR)&buf,
     404             :                 0, NULL)) {
     405             :                 fprintf(stderr, "\n%s: [0x%08lx]\n", msg, err);
     406             :         }
     407             :         else {
     408             :                 fprintf(stderr, "\n%s: [0x%08lx] %s\n", msg, err, buf);
     409             :         }
     410             : }
     411             : #endif
     412             : 
     413             : /*****************/
     414             : /* OS Allocation */
     415             : /*****************/
     416             : 
     417           0 : static void *zend_mm_mmap_fixed(void *addr, size_t size)
     418             : {
     419             : #ifdef _WIN32
     420             :         return VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
     421             : #else
     422             :         /* MAP_FIXED leads to discarding of the old mapping, so it can't be used. */
     423           0 :         void *ptr = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON /*| MAP_POPULATE | MAP_HUGETLB*/, -1, 0);
     424             : 
     425           0 :         if (ptr == MAP_FAILED) {
     426             : #if ZEND_MM_ERROR
     427           0 :                 fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno));
     428             : #endif
     429           0 :                 return NULL;
     430           0 :         } else if (ptr != addr) {
     431           0 :                 if (munmap(ptr, size) != 0) {
     432             : #if ZEND_MM_ERROR
     433           0 :                         fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
     434             : #endif
     435             :                 }
     436           0 :                 return NULL;
     437             :         }
     438           0 :         return ptr;
     439             : #endif
     440             : }
     441             : 
     442          29 : static void *zend_mm_mmap(size_t size)
     443             : {
     444             : #ifdef _WIN32
     445             :         void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
     446             : 
     447             :         if (ptr == NULL) {
     448             : #if ZEND_MM_ERROR
     449             :                 stderr_last_error("VirtualAlloc() failed");
     450             : #endif
     451             :                 return NULL;
     452             :         }
     453             :         return ptr;
     454             : #else
     455          29 :         void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON /*| MAP_POPULATE | MAP_HUGETLB*/, -1, 0);
     456             : 
     457          29 :         if (ptr == MAP_FAILED) {
     458             : #if ZEND_MM_ERROR
     459           0 :                 fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno));
     460             : #endif
     461           0 :                 return NULL;
     462             :         }
     463          29 :         return ptr;
     464             : #endif
     465             : }
     466             : 
     467          56 : static void zend_mm_munmap(void *addr, size_t size)
     468             : {
     469             : #ifdef _WIN32
     470             :         if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
     471             : #if ZEND_MM_ERROR
     472             :                 stderr_last_error("VirtualFree() failed");
     473             : #endif
     474             :         }
     475             : #else
     476          56 :         if (munmap(addr, size) != 0) {
     477             : #if ZEND_MM_ERROR
     478           0 :                 fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
     479             : #endif
     480             :         }
     481             : #endif
     482          56 : }
     483             : 
     484             : /***********/
     485             : /* Bitmask */
     486             : /***********/
     487             : 
     488             : /* number of trailing set (1) bits */
     489             : static zend_always_inline int zend_mm_bitset_nts(zend_mm_bitset bitset)
     490             : {
     491             : #if defined(__GNUC__)
     492             : # if SIZEOF_ZEND_LONG == SIZEOF_LONG
     493     1591669 :         return __builtin_ctzl(~bitset);
     494             : # else
     495             :         return __builtin_ctzll(~bitset);
     496             : # endif
     497             : #elif defined(_WIN32)
     498             :         unsigned long index;
     499             : 
     500             : #if defined(_WIN64)
     501             :         if (!BitScanForward64(&index, ~bitset)) {
     502             : #else
     503             :         if (!BitScanForward(&index, ~bitset)) {
     504             : #endif
     505             :                 /* undefined behavior */
     506             :                 return 32;
     507             :         }
     508             : 
     509             :         return (int)index;
     510             : #else
     511             :         int n;
     512             : 
     513             :         if (bitset == (zend_mm_bitset)-1) return ZEND_MM_BITSET_LEN;
     514             : 
     515             :         n = 0;
     516             : #if SIZEOF_ZEND_LONG == 8
     517             :         if (sizeof(zend_mm_bitset) == 8) {
     518             :                 if ((bitset & 0xffffffff) == 0xffffffff) {n += 32; bitset = bitset >> Z_UL(32);}
     519             :         }
     520             : #endif
     521             :         if ((bitset & 0x0000ffff) == 0x0000ffff) {n += 16; bitset = bitset >> 16;}
     522             :         if ((bitset & 0x000000ff) == 0x000000ff) {n +=  8; bitset = bitset >>  8;}
     523             :         if ((bitset & 0x0000000f) == 0x0000000f) {n +=  4; bitset = bitset >>  4;}
     524             :         if ((bitset & 0x00000003) == 0x00000003) {n +=  2; bitset = bitset >>  2;}
     525             :         return n + (bitset & 1);
     526             : #endif
     527             : }
     528             : 
     529             : /* number of trailing zero bits (0x01 -> 1; 0x40 -> 6; 0x00 -> LEN) */
     530             : static zend_always_inline int zend_mm_bitset_ntz(zend_mm_bitset bitset)
     531             : {
     532             : #if defined(__GNUC__)
     533             : # if SIZEOF_ZEND_LONG == SIZEOF_LONG
     534     1265306 :         return __builtin_ctzl(bitset);
     535             : # else
     536             :         return __builtin_ctzll(bitset);
     537             : # endif
     538             : #elif defined(_WIN32)
     539             :         unsigned long index;
     540             : 
     541             : #if defined(_WIN64)
     542             :         if (!BitScanForward64(&index, bitset)) {
     543             : #else
     544             :         if (!BitScanForward(&index, bitset)) {
     545             : #endif
     546             :                 /* undefined behavior */
     547             :                 return 32;
     548             :         }
     549             : 
     550             :         return (int)index;
     551             : #else
     552             :         int n;
     553             : 
     554             :         if (bitset == (zend_mm_bitset)0) return ZEND_MM_BITSET_LEN;
     555             : 
     556             :         n = 1;
     557             : #if SIZEOF_ZEND_LONG == 8
     558             :         if (sizeof(zend_mm_bitset) == 8) {
     559             :                 if ((bitset & 0xffffffff) == 0) {n += 32; bitset = bitset >> Z_UL(32);}
     560             :         }
     561             : #endif
     562             :         if ((bitset & 0x0000ffff) == 0) {n += 16; bitset = bitset >> 16;}
     563             :         if ((bitset & 0x000000ff) == 0) {n +=  8; bitset = bitset >>  8;}
     564             :         if ((bitset & 0x0000000f) == 0) {n +=  4; bitset = bitset >>  4;}
     565             :         if ((bitset & 0x00000003) == 0) {n +=  2; bitset = bitset >>  2;}
     566             :         return n - (bitset & 1);
     567             : #endif
     568             : }
     569             : 
     570             : static zend_always_inline int zend_mm_bitset_find_zero(zend_mm_bitset *bitset, int size)
     571             : {
     572             :         int i = 0;
     573             : 
     574             :         do {
     575             :                 zend_mm_bitset tmp = bitset[i];
     576             :                 if (tmp != (zend_mm_bitset)-1) {
     577             :                         return i * ZEND_MM_BITSET_LEN + zend_mm_bitset_nts(tmp);
     578             :                 }
     579             :                 i++;
     580             :         } while (i < size);
     581             :         return -1;
     582             : }
     583             : 
     584             : static zend_always_inline int zend_mm_bitset_find_one(zend_mm_bitset *bitset, int size)
     585             : {
     586             :         int i = 0;
     587             : 
     588             :         do {
     589             :                 zend_mm_bitset tmp = bitset[i];
     590             :                 if (tmp != 0) {
     591             :                         return i * ZEND_MM_BITSET_LEN + zend_mm_bitset_ntz(tmp);
     592             :                 }
     593             :                 i++;
     594             :         } while (i < size);
     595             :         return -1;
     596             : }
     597             : 
     598             : static zend_always_inline int zend_mm_bitset_find_zero_and_set(zend_mm_bitset *bitset, int size)
     599             : {
     600             :         int i = 0;
     601             : 
     602             :         do {
     603             :                 zend_mm_bitset tmp = bitset[i];
     604             :                 if (tmp != (zend_mm_bitset)-1) {
     605             :                         int n = zend_mm_bitset_nts(tmp);
     606             :                         bitset[i] |= Z_UL(1) << n;
     607             :                         return i * ZEND_MM_BITSET_LEN + n;
     608             :                 }
     609             :                 i++;
     610             :         } while (i < size);
     611             :         return -1;
     612             : }
     613             : 
     614             : static zend_always_inline int zend_mm_bitset_is_set(zend_mm_bitset *bitset, int bit)
     615             : {
     616        2603 :         return (bitset[bit / ZEND_MM_BITSET_LEN] & (Z_L(1) << (bit & (ZEND_MM_BITSET_LEN-1)))) != 0;
     617             : }
     618             : 
     619             : static zend_always_inline void zend_mm_bitset_set_bit(zend_mm_bitset *bitset, int bit)
     620             : {
     621        6857 :         bitset[bit / ZEND_MM_BITSET_LEN] |= (Z_L(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
     622             : }
     623             : 
     624             : static zend_always_inline void zend_mm_bitset_reset_bit(zend_mm_bitset *bitset, int bit)
     625             : {
     626        3259 :         bitset[bit / ZEND_MM_BITSET_LEN] &= ~(Z_L(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
     627             : }
     628             : 
     629             : static zend_always_inline void zend_mm_bitset_set_range(zend_mm_bitset *bitset, int start, int len)
     630             : {
     631      896318 :         if (len == 1) {
     632             :                 zend_mm_bitset_set_bit(bitset, start);
     633             :         } else {
     634      889461 :                 int pos = start / ZEND_MM_BITSET_LEN;
     635      889461 :                 int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
     636      889461 :                 int bit = start & (ZEND_MM_BITSET_LEN - 1);
     637             :                 zend_mm_bitset tmp;
     638             : 
     639      889461 :                 if (pos != end) {
     640             :                         /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */
     641       37636 :                         tmp = (zend_mm_bitset)-1 << bit;
     642       37636 :                         bitset[pos++] |= tmp;
     643       37654 :                         while (pos != end) {
     644             :                                 /* set all bits */
     645          18 :                                 bitset[pos++] = (zend_mm_bitset)-1;
     646             :                         }
     647       37636 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     648             :                         /* set bits from "0" to "end" */
     649       37636 :                         tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     650       37636 :                         bitset[pos] |= tmp;
     651             :                 } else {
     652      851825 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     653             :                         /* set bits from "bit" to "end" */
     654      851825 :                         tmp = (zend_mm_bitset)-1 << bit;
     655      851825 :                         tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     656      851825 :                         bitset[pos] |= tmp;
     657             :                 }
     658             :         }
     659             : }
     660             : 
     661             : static zend_always_inline void zend_mm_bitset_reset_range(zend_mm_bitset *bitset, int start, int len)
     662             : {
     663      893801 :         if (len == 1) {
     664             :                 zend_mm_bitset_reset_bit(bitset, start);
     665             :         } else {
     666      890542 :                 int pos = start / ZEND_MM_BITSET_LEN;
     667      890542 :                 int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
     668      890542 :                 int bit = start & (ZEND_MM_BITSET_LEN - 1);
     669             :                 zend_mm_bitset tmp;
     670             : 
     671      890542 :                 if (pos != end) {
     672             :                         /* reset bits from "bit" to ZEND_MM_BITSET_LEN-1 */
     673       37663 :                         tmp = ~((Z_L(1) << bit) - 1);
     674       37663 :                         bitset[pos++] &= ~tmp;
     675       37687 :                         while (pos != end) {
     676             :                                 /* set all bits */
     677          24 :                                 bitset[pos++] = 0;
     678             :                         }
     679       37663 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     680             :                         /* reset bits from "0" to "end" */
     681       37663 :                         tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     682       37663 :                         bitset[pos] &= ~tmp;
     683             :                 } else {
     684      852879 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     685             :                         /* reset bits from "bit" to "end" */
     686      852879 :                         tmp = (zend_mm_bitset)-1 << bit;
     687      852879 :                         tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     688      852879 :                         bitset[pos] &= ~tmp;
     689             :                 }
     690             :         }
     691             : }
     692             : 
     693             : static zend_always_inline int zend_mm_bitset_is_free_range(zend_mm_bitset *bitset, int start, int len)
     694             : {
     695        2911 :         if (len == 1) {
     696        2603 :                 return !zend_mm_bitset_is_set(bitset, start);
     697             :         } else {
     698         308 :                 int pos = start / ZEND_MM_BITSET_LEN;
     699         308 :                 int end = (start + len - 1) / ZEND_MM_BITSET_LEN;
     700         308 :                 int bit = start & (ZEND_MM_BITSET_LEN - 1);
     701             :                 zend_mm_bitset tmp;
     702             : 
     703         308 :                 if (pos != end) {
     704             :                         /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */
     705           5 :                         tmp = (zend_mm_bitset)-1 << bit;
     706           5 :                         if ((bitset[pos++] & tmp) != 0) {
     707           4 :                                 return 0;
     708             :                         }
     709           1 :                         while (pos != end) {
     710             :                                 /* set all bits */
     711           0 :                                 if (bitset[pos++] != 0) {
     712           0 :                                         return 0;
     713             :                                 }
     714             :                         }
     715           1 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     716             :                         /* set bits from "0" to "end" */
     717           1 :                         tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     718           1 :                         return (bitset[pos] & tmp) == 0;
     719             :                 } else {
     720         303 :                         end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1);
     721             :                         /* set bits from "bit" to "end" */
     722         303 :                         tmp = (zend_mm_bitset)-1 << bit;
     723         303 :                         tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end);
     724         303 :                         return (bitset[pos] & tmp) == 0;
     725             :                 }
     726             :         }
     727             : }
     728             : 
     729             : /**********/
     730             : /* Chunks */
     731             : /**********/
     732             : 
     733          15 : static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment)
     734             : {
     735          15 :         void *ptr = zend_mm_mmap(size);
     736             : 
     737          15 :         if (ptr == NULL) {
     738           0 :                 return NULL;
     739          15 :         } else if (ZEND_MM_ALIGNED_OFFSET(ptr, alignment) == 0) {
     740             : #ifdef MADV_HUGEPAGE
     741             :             madvise(ptr, size, MADV_HUGEPAGE);
     742             : #endif
     743           1 :                 return ptr;
     744             :         } else {
     745             :                 size_t offset;
     746             : 
     747             :                 /* chunk has to be aligned */
     748          14 :                 zend_mm_munmap(ptr, size);
     749          14 :                 ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE);
     750             : #ifdef _WIN32
     751             :                 offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
     752             :                 zend_mm_munmap(ptr, size + alignment - REAL_PAGE_SIZE);
     753             :                 ptr = zend_mm_mmap_fixed((void*)((char*)ptr + (alignment - offset)), size);
     754             :                 offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
     755             :                 if (offset != 0) {
     756             :                         zend_mm_munmap(ptr, size);
     757             :                         return NULL;
     758             :                 }
     759             :                 return ptr;
     760             : #else
     761          14 :                 offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment);
     762          14 :                 if (offset != 0) {
     763          13 :                         offset = alignment - offset;
     764          13 :                         zend_mm_munmap(ptr, offset);
     765          13 :                         ptr = (char*)ptr + offset;
     766          13 :                         alignment -= offset;
     767             :                 }
     768          14 :                 if (alignment > REAL_PAGE_SIZE) {
     769          14 :                         zend_mm_munmap((char*)ptr + size, alignment - REAL_PAGE_SIZE);
     770             :                 }
     771             : # ifdef MADV_HUGEPAGE
     772             :             madvise(ptr, size, MADV_HUGEPAGE);
     773             : # endif
     774             : #endif
     775          14 :                 return ptr;
     776             :         }
     777             : }
     778             : 
     779           5 : static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignment)
     780             : {
     781             : #if ZEND_MM_STORAGE
     782           5 :         if (UNEXPECTED(heap->storage)) {
     783           0 :                 void *ptr = heap->storage->chunk_alloc(heap->storage, size, alignment);
     784             :                 ZEND_ASSERT(((zend_uintptr_t)((char*)ptr + (alignment-1)) & (alignment-1)) == (zend_uintptr_t)ptr);
     785           0 :                 return ptr;
     786             :         }
     787             : #endif
     788           5 :         return zend_mm_chunk_alloc_int(size, alignment);
     789             : }
     790             : 
     791          15 : static void zend_mm_chunk_free(zend_mm_heap *heap, void *addr, size_t size)
     792             : {
     793             : #if ZEND_MM_STORAGE
     794          15 :         if (UNEXPECTED(heap->storage)) {
     795           0 :                 heap->storage->chunk_free(heap->storage, addr, size);
     796           0 :                 return;
     797             :         }
     798             : #endif
     799          15 :         zend_mm_munmap(addr, size);
     800             : }
     801             : 
     802           0 : static void zend_mm_chunk_truncate(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size)
     803             : {
     804             : #if ZEND_MM_STORAGE
     805           0 :         if (UNEXPECTED(heap->storage)) {
     806           0 :                 heap->storage->chunk_truncate(heap->storage, addr, old_size, new_size);
     807           0 :                 return;
     808             :         }
     809             : #endif
     810           0 :         zend_mm_munmap((char*)addr + new_size, old_size - new_size);
     811             : }
     812             : 
     813             : static zend_always_inline void zend_mm_chunk_init(zend_mm_heap *heap, zend_mm_chunk *chunk)
     814             : {
     815           5 :         chunk->heap = heap;
     816           5 :         chunk->next = heap->main_chunk;
     817           5 :         chunk->prev = heap->main_chunk->prev;
     818           5 :         chunk->prev->next = chunk;
     819           5 :         chunk->next->prev = chunk;
     820             :         /* mark first pages as allocated */
     821           5 :         chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
     822           5 :         chunk->free_tail = ZEND_MM_FIRST_PAGE;
     823             :         /* the younger chunks have bigger number */
     824           5 :         chunk->num = chunk->prev->num + 1;
     825             :         /* mark first pages as allocated */
     826           5 :         chunk->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1;
     827           5 :         chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
     828             : }
     829             : 
     830             : /***********************/
     831             : /* Huge Runs (forward) */
     832             : /***********************/
     833             : 
     834             : static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     835             : static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     836             : static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     837             : 
     838             : #if ZEND_DEBUG
     839             : 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);
     840             : #else
     841             : 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);
     842             : #endif
     843             : 
     844             : /**************/
     845             : /* Large Runs */
     846             : /**************/
     847             : 
     848             : #if ZEND_DEBUG
     849             : 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)
     850             : #else
     851      893985 : static void *zend_mm_alloc_pages(zend_mm_heap *heap, int pages_count ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
     852             : #endif
     853             : {
     854      893985 :         zend_mm_chunk *chunk = heap->main_chunk;
     855             :         int page_num, len;
     856             : 
     857             :         while (1) {
     858     1787233 :                 if (UNEXPECTED(chunk->free_pages < pages_count)) {
     859      893249 :                         goto not_found;
     860             : #if 0
     861             :                 } else if (UNEXPECTED(chunk->free_pages + chunk->free_tail == ZEND_MM_PAGES)) {
     862             :                         if (UNEXPECTED(ZEND_MM_PAGES - chunk->free_tail < pages_count)) {
     863             :                                 goto not_found;
     864             :                         } else {
     865             :                                 page_num = chunk->free_tail;
     866             :                                 goto found;
     867             :                         }
     868             :                 } else if (0) {
     869             :                         /* First-Fit Search */
     870             :                         int free_tail = chunk->free_tail;
     871             :                         zend_mm_bitset *bitset = chunk->free_map;
     872             :                         zend_mm_bitset tmp = *(bitset++);
     873             :                         int i = 0;
     874             : 
     875             :                         while (1) {
     876             :                                 /* skip allocated blocks */
     877             :                                 while (tmp == (zend_mm_bitset)-1) {
     878             :                                         i += ZEND_MM_BITSET_LEN;
     879             :                                         if (i == ZEND_MM_PAGES) {
     880             :                                                 goto not_found;
     881             :                                         }
     882             :                                         tmp = *(bitset++);
     883             :                                 }
     884             :                                 /* find first 0 bit */
     885             :                                 page_num = i + zend_mm_bitset_nts(tmp);
     886             :                                 /* reset bits from 0 to "bit" */
     887             :                                 tmp &= tmp + 1;
     888             :                                 /* skip free blocks */
     889             :                                 while (tmp == 0) {
     890             :                                         i += ZEND_MM_BITSET_LEN;
     891             :                                         len = i - page_num;
     892             :                                         if (len >= pages_count) {
     893             :                                                 goto found;
     894             :                                         } else if (i >= free_tail) {
     895             :                                                 goto not_found;
     896             :                                         }
     897             :                                         tmp = *(bitset++);
     898             :                                 }
     899             :                                 /* find first 1 bit */
     900             :                                 len = (i + zend_mm_bitset_ntz(tmp)) - page_num;
     901             :                                 if (len >= pages_count) {
     902             :                                         goto found;
     903             :                                 }
     904             :                                 /* set bits from 0 to "bit" */
     905             :                                 tmp |= tmp - 1;
     906             :                         }
     907             : #endif
     908             :                 } else {
     909             :                         /* Best-Fit Search */
     910      893984 :                         int best = -1;
     911      893984 :                         int best_len = ZEND_MM_PAGES;
     912      893984 :                         int free_tail = chunk->free_tail;
     913      893984 :                         zend_mm_bitset *bitset = chunk->free_map;
     914      893984 :                         zend_mm_bitset tmp = *(bitset++);
     915      893984 :                         int i = 0;
     916             : 
     917             :                         while (1) {
     918             :                                 /* skip allocated blocks */
     919     6977605 :                                 while (tmp == (zend_mm_bitset)-1) {
     920     3635941 :                                         i += ZEND_MM_BITSET_LEN;
     921     3635941 :                                         if (i == ZEND_MM_PAGES) {
     922      158326 :                                                 if (best > 0) {
     923      158326 :                                                         page_num = best;
     924      158326 :                                                         goto found;
     925             :                                                 } else {
     926           0 :                                                         goto not_found;
     927             :                                                 }
     928             :                                         }
     929     3477615 :                                         tmp = *(bitset++);
     930             :                                 }
     931             :                                 /* find first 0 bit */
     932     1591669 :                                 page_num = i + zend_mm_bitset_nts(tmp);
     933             :                                 /* reset bits from 0 to "bit" */
     934     1591669 :                                 tmp &= tmp + 1;
     935             :                                 /* skip free blocks */
     936     4504347 :                                 while (tmp == 0) {
     937     1647372 :                                         i += ZEND_MM_BITSET_LEN;
     938     1647372 :                                         if (i >= free_tail) {
     939      326363 :                                                 len = ZEND_MM_PAGES - page_num;
     940      326363 :                                                 if (len >= pages_count && len < best_len) {
     941      142711 :                                                         chunk->free_tail = page_num + pages_count;
     942      142711 :                                                         goto found;
     943             :                                                 } else {
     944             :                                                         /* set accurate value */
     945      183652 :                                                         chunk->free_tail = page_num;
     946      183652 :                                                         if (best > 0) {
     947      183648 :                                                                 page_num = best;
     948      183648 :                                                                 goto found;
     949             :                                                         } else {
     950           4 :                                                                 goto not_found;
     951             :                                                         }
     952             :                                                 }
     953             :                                         }
     954     1321009 :                                         tmp = *(bitset++);
     955             :                                 }
     956             :                                 /* find first 1 bit */
     957     1265306 :                                 len = i + zend_mm_bitset_ntz(tmp) - page_num;
     958     1265306 :                                 if (len >= pages_count) {
     959     1150512 :                                         if (len == pages_count) {
     960      409295 :                                                 goto found;
     961      741217 :                                         } else if (len < best_len) {
     962      636307 :                                                 best_len = len;
     963      636307 :                                                 best = page_num;
     964             :                                         }
     965             :                                 }
     966             :                                 /* set bits from 0 to "bit" */
     967      856011 :                                 tmp |= tmp - 1;
     968      856011 :                         }
     969             :                 }
     970             : 
     971             : not_found:
     972      893253 :                 if (chunk->next == heap->main_chunk) {
     973           5 :                         if (heap->cached_chunks) {
     974           0 :                                 heap->cached_chunks_count--;
     975           0 :                                 chunk = heap->cached_chunks;
     976           0 :                                 heap->cached_chunks = chunk->next;
     977             :                         } else {
     978             : #if ZEND_MM_LIMIT
     979           5 :                                 if (heap->real_size + ZEND_MM_CHUNK_SIZE > heap->limit) {
     980           0 :                                         if (heap->overflow == 0) {
     981             : #if ZEND_DEBUG
     982             :                                                 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);
     983             : #else
     984           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);
     985             : #endif
     986             :                                                 return NULL;
     987             :                                         }
     988             :                                 }
     989             : #endif
     990           5 :                                 chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
     991           5 :                                 if (UNEXPECTED(chunk == NULL)) {
     992             :                                         /* insufficient memory */
     993             : #if !ZEND_MM_LIMIT
     994             :                                         zend_mm_safe_error(heap, "Out of memory");
     995             : #elif ZEND_DEBUG
     996             :                                         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);
     997             : #else
     998           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);
     999             : #endif
    1000             :                                         return NULL;
    1001             :                                 }
    1002             : #if ZEND_MM_STAT
    1003             :                                 do {
    1004           5 :                                         size_t size = heap->real_size + ZEND_MM_CHUNK_SIZE;
    1005           5 :                                         size_t peak = MAX(heap->real_peak, size);
    1006           5 :                                         heap->real_size = size;
    1007           5 :                                         heap->real_peak = peak;
    1008             :                                 } while (0);
    1009             : #elif ZEND_MM_LIMIT
    1010             :                                 heap->real_size += ZEND_MM_CHUNK_SIZE;
    1011             : 
    1012             : #endif
    1013             :                         }
    1014           5 :                         heap->chunks_count++;
    1015           5 :                         if (heap->chunks_count > heap->peak_chunks_count) {
    1016           5 :                                 heap->peak_chunks_count = heap->chunks_count;
    1017             :                         }
    1018             :                         zend_mm_chunk_init(heap, chunk);
    1019           5 :                         page_num = ZEND_MM_FIRST_PAGE;
    1020           5 :                         len = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    1021           5 :                         goto found;
    1022             :                 } else {
    1023      893248 :                         chunk = chunk->next;
    1024             :                 }
    1025      893248 :         }
    1026             : 
    1027             : found:
    1028             :         /* mark run as allocated */
    1029      893985 :         chunk->free_pages -= pages_count;
    1030      893985 :         zend_mm_bitset_set_range(chunk->free_map, page_num, pages_count);
    1031      893985 :         chunk->map[page_num] = ZEND_MM_LRUN(pages_count);
    1032      893985 :         if (page_num == chunk->free_tail) {
    1033           5 :                 chunk->free_tail = page_num + pages_count;
    1034             :         }
    1035      893985 :         return ZEND_MM_PAGE_ADDR(chunk, page_num);
    1036             : }
    1037             : 
    1038             : 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)
    1039             : {
    1040      893102 :         int pages_count = (int)ZEND_MM_SIZE_TO_NUM(size, ZEND_MM_PAGE_SIZE);
    1041             : #if ZEND_DEBUG
    1042             :         void *ptr = zend_mm_alloc_pages(heap, pages_count, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1043             : #else
    1044      893102 :         void *ptr = zend_mm_alloc_pages(heap, pages_count ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1045             : #endif
    1046             : #if ZEND_MM_STAT
    1047             :         do {
    1048      893102 :                 size_t size = heap->size + pages_count * ZEND_MM_PAGE_SIZE;
    1049      893102 :                 size_t peak = MAX(heap->peak, size);
    1050      893102 :                 heap->size = size;
    1051      893102 :                 heap->peak = peak;
    1052             :         } while (0);
    1053             : #endif
    1054      893102 :         return ptr;
    1055             : }
    1056             : 
    1057      893100 : static void zend_mm_free_pages(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count)
    1058             : {
    1059      893100 :         chunk->free_pages += pages_count;
    1060      893100 :         zend_mm_bitset_reset_range(chunk->free_map, page_num, pages_count);
    1061      893100 :         chunk->map[page_num] = 0;
    1062      893100 :         if (chunk->free_tail == page_num + pages_count) {
    1063             :                 /* this setting may be not accurate */
    1064      136183 :                 chunk->free_tail = page_num;
    1065             :         }
    1066      893100 :         if (chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE) {
    1067             :                 /* delete chunk */
    1068           4 :                 chunk->next->prev = chunk->prev;
    1069           4 :                 chunk->prev->next = chunk->next;
    1070           4 :                 heap->chunks_count--;
    1071           4 :                 if (heap->chunks_count + heap->cached_chunks_count < heap->avg_chunks_count + 0.1) {
    1072             :                         /* delay deletion */
    1073           0 :                         heap->cached_chunks_count++;
    1074           0 :                         chunk->next = heap->cached_chunks;
    1075           0 :                         heap->cached_chunks = chunk;
    1076             :                 } else {
    1077             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1078           4 :                         heap->real_size -= ZEND_MM_CHUNK_SIZE;
    1079             : #endif
    1080           8 :                         if (!heap->cached_chunks || chunk->num > heap->cached_chunks->num) {
    1081           4 :                                 zend_mm_chunk_free(heap, chunk, ZEND_MM_CHUNK_SIZE);
    1082             :                         } else {
    1083             : //TODO: select the best chunk to delete???
    1084           0 :                                 chunk->next = heap->cached_chunks->next;
    1085           0 :                                 zend_mm_chunk_free(heap, heap->cached_chunks, ZEND_MM_CHUNK_SIZE);
    1086           0 :                                 heap->cached_chunks = chunk;
    1087             :                         }
    1088             :                 }
    1089             :         }
    1090      893100 : }
    1091             : 
    1092             : static zend_always_inline void zend_mm_free_large(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count)
    1093             : {
    1094             : #if ZEND_MM_STAT
    1095      893100 :         heap->size -= pages_count * ZEND_MM_PAGE_SIZE;
    1096             : #endif
    1097      893100 :         zend_mm_free_pages(heap, chunk, page_num, pages_count);
    1098             : }
    1099             : 
    1100             : /**************/
    1101             : /* Small Runs */
    1102             : /**************/
    1103             : 
    1104             : /* higher set bit number (0->N/A, 1->1, 2->2, 4->3, 8->4, 127->7, 128->8 etc) */
    1105             : static zend_always_inline int zend_mm_small_size_to_bit(int size)
    1106             : {
    1107             : #if defined(__GNUC__)
    1108    16452569 :         return (__builtin_clz(size) ^ 0x1f) + 1;
    1109             : #elif defined(_WIN32)
    1110             :         unsigned long index;
    1111             : 
    1112             :         if (!BitScanReverse(&index, (unsigned long)size)) {
    1113             :                 /* undefined behavior */
    1114             :                 return 64;
    1115             :         }
    1116             : 
    1117             :         return (((31 - (int)index) ^ 0x1f) + 1);
    1118             : #else
    1119             :         int n = 16;
    1120             :         if (size <= 0x00ff) {n -= 8; size = size << 8;}
    1121             :         if (size <= 0x0fff) {n -= 4; size = size << 4;}
    1122             :         if (size <= 0x3fff) {n -= 2; size = size << 2;}
    1123             :         if (size <= 0x7fff) {n -= 1;}
    1124             :         return n;
    1125             : #endif
    1126             : }
    1127             : 
    1128             : #ifndef MAX
    1129             : # define MAX(a, b) (((a) > (b)) ? (a) : (b))
    1130             : #endif
    1131             : 
    1132             : #ifndef MIN
    1133             : # define MIN(a, b) (((a) < (b)) ? (a) : (b))
    1134             : #endif
    1135             : 
    1136             : static zend_always_inline int zend_mm_small_size_to_bin(size_t size)
    1137             : {
    1138             : #if 0
    1139             :         int n;
    1140             :                             /*0,  1,  2,  3,  4,  5,  6,  7,  8,  9  10, 11, 12*/
    1141             :         static const int f1[] = { 3,  3,  3,  3,  3,  3,  3,  4,  5,  6,  7,  8,  9};
    1142             :         static const int f2[] = { 0,  0,  0,  0,  0,  0,  0,  4,  8, 12, 16, 20, 24};
    1143             : 
    1144             :         if (UNEXPECTED(size <= 2)) return 0;
    1145             :         n = zend_mm_small_size_to_bit(size - 1);
    1146             :         return ((size-1) >> f1[n]) + f2[n];
    1147             : #else
    1148             :         int t1, t2, t3;
    1149             : 
    1150    16791267 :         if (UNEXPECTED(size <= 8)) return 0;
    1151    16452569 :         t1 = (int)(size - 1);
    1152    16452569 :         t2 = zend_mm_small_size_to_bit(t1);
    1153    16452569 :         t3 = t2 - 6;
    1154    16452569 :         t3 = (t3 < 0) ? 0 : t3;
    1155    16452569 :         t2 = t3 + 3;
    1156    16452569 :         t1 = t1 >> t2;
    1157    16452569 :         t3 = t3 << 2;
    1158    16452569 :         return t1 + t3;
    1159             : #endif
    1160             : }
    1161             : 
    1162             : #define ZEND_MM_SMALL_SIZE_TO_BIN(size)  zend_mm_small_size_to_bin(size)
    1163             : 
    1164         883 : 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)
    1165             : {
    1166             :     zend_mm_chunk *chunk;
    1167             :     int page_num;
    1168             :         zend_mm_bin *bin;
    1169             :         zend_mm_free_slot *p, *end;
    1170             : 
    1171             : #if ZEND_DEBUG
    1172             :         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);
    1173             : #else
    1174         883 :         bin = (zend_mm_bin*)zend_mm_alloc_pages(heap, bin_pages[bin_num] ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1175             : #endif
    1176         883 :         if (UNEXPECTED(bin == NULL)) {
    1177             :                 /* insufficient memory */
    1178           0 :                 return NULL;
    1179             :         }
    1180             : 
    1181         883 :         chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(bin, ZEND_MM_CHUNK_SIZE);
    1182         883 :         page_num = ZEND_MM_ALIGNED_OFFSET(bin, ZEND_MM_CHUNK_SIZE) / ZEND_MM_PAGE_SIZE;
    1183         883 :         chunk->map[page_num] = ZEND_MM_SRUN(bin_num);
    1184         883 :         if (bin_pages[bin_num] > 1) {
    1185          70 :                 int i = 1;
    1186             :                 do {
    1187         232 :                         chunk->map[page_num+i] = ZEND_MM_SRUN(bin_num);
    1188         232 :                         i++;
    1189         232 :                 } while (i < bin_pages[bin_num]);
    1190             :         }
    1191             : 
    1192             :         /* create a linked list of elements from 1 to last */
    1193         883 :         end = (zend_mm_free_slot*)((char*)bin + (bin_data_size[bin_num] * (bin_elements[bin_num] - 1)));
    1194         883 :         heap->free_slot[bin_num] = p = (zend_mm_free_slot*)((char*)bin + bin_data_size[bin_num]);
    1195             :         do {
    1196       74333 :                 p->next_free_slot = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]);;
    1197             : #if ZEND_DEBUG
    1198             :                 do {
    1199             :                         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)));
    1200             :                         dbg->size = 0;
    1201             :                 } while (0);
    1202             : #endif
    1203       74333 :                 p = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]);
    1204       74333 :         } while (p != end);
    1205             : 
    1206             :         /* terminate list using NULL */
    1207         883 :         p->next_free_slot = NULL;
    1208             : #if ZEND_DEBUG
    1209             :                 do {
    1210             :                         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)));
    1211             :                         dbg->size = 0;
    1212             :                 } while (0);
    1213             : #endif
    1214             : 
    1215             :         /* return first element */
    1216         883 :         return (char*)bin;
    1217             : }
    1218             : 
    1219             : 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)
    1220             : {
    1221             : #if ZEND_MM_STAT
    1222             :         do {
    1223    16817616 :                 size_t size = heap->size + bin_data_size[bin_num];
    1224    16817616 :                 size_t peak = MAX(heap->peak, size);
    1225    16817616 :                 heap->size = size;
    1226    16817616 :                 heap->peak = peak;
    1227             :         } while (0);
    1228             : #endif
    1229             : 
    1230    16817616 :         if (EXPECTED(heap->free_slot[bin_num] != NULL)) {
    1231    16816733 :                 zend_mm_free_slot *p = heap->free_slot[bin_num];
    1232    16816733 :                 heap->free_slot[bin_num] = p->next_free_slot;
    1233    16816733 :                 return (void*)p;
    1234             :         } else {
    1235         883 :                 return zend_mm_alloc_small_slow(heap, bin_num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1236             :         }
    1237             : }
    1238             : 
    1239             : static zend_always_inline void zend_mm_free_small(zend_mm_heap *heap, void *ptr, int bin_num)
    1240             : {
    1241             :         zend_mm_free_slot *p;
    1242             : 
    1243             : #if ZEND_MM_STAT
    1244    16817516 :         heap->size -= bin_data_size[bin_num];
    1245             : #endif
    1246             : 
    1247             : #if ZEND_DEBUG
    1248             :         do {
    1249             :                 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)));
    1250             :                 dbg->size = 0;
    1251             :         } while (0);
    1252             : #endif
    1253             : 
    1254    16817516 :     p = (zend_mm_free_slot*)ptr;
    1255    16817516 :     p->next_free_slot = heap->free_slot[bin_num];
    1256    16817516 :     heap->free_slot[bin_num] = p;
    1257             : }
    1258             : 
    1259             : /********/
    1260             : /* Heap */
    1261             : /********/
    1262             : 
    1263             : #if ZEND_DEBUG
    1264             : static zend_always_inline zend_mm_debug_info *zend_mm_get_debug_info(zend_mm_heap *heap, void *ptr)
    1265             : {
    1266             :         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1267             :         zend_mm_chunk *chunk;
    1268             :         int page_num;
    1269             :         zend_mm_page_info info;
    1270             : 
    1271             :         ZEND_MM_CHECK(page_offset != 0, "zend_mm_heap corrupted");
    1272             :         chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1273             :         page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1274             :         info = chunk->map[page_num];
    1275             :         ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1276             :         if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
    1277             :                 int bin_num = ZEND_MM_SRUN_BIN_NUM(info);
    1278             :                 return (zend_mm_debug_info*)((char*)ptr + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
    1279             :         } else /* if (info & ZEND_MM_IS_LRUN) */ {
    1280             :                 int pages_count = ZEND_MM_LRUN_PAGES(info);
    1281             : 
    1282             :                 return (zend_mm_debug_info*)((char*)ptr + ZEND_MM_PAGE_SIZE * pages_count - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)));
    1283             :         }
    1284             : }
    1285             : #endif
    1286             : 
    1287             : 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)
    1288             : {
    1289             :         void *ptr;
    1290             : #if ZEND_DEBUG
    1291             :         size_t real_size = size;
    1292             :         zend_mm_debug_info *dbg;
    1293             : 
    1294             :         /* special handling for zero-size allocation */
    1295             :         size = MAX(size, 1);
    1296             :         size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
    1297             : #endif
    1298    12922559 :         if (size <= ZEND_MM_MAX_SMALL_SIZE) {
    1299    12029457 :                 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);
    1300             : #if ZEND_DEBUG
    1301             :                 dbg = zend_mm_get_debug_info(heap, ptr);
    1302             :                 dbg->size = real_size;
    1303             :                 dbg->filename = __zend_filename;
    1304             :                 dbg->orig_filename = __zend_orig_filename;
    1305             :                 dbg->lineno = __zend_lineno;
    1306             :                 dbg->orig_lineno = __zend_orig_lineno;
    1307             : #endif
    1308    12029457 :                 return ptr;
    1309      893102 :         } else if (size <= ZEND_MM_MAX_LARGE_SIZE) {
    1310      893102 :                 ptr = zend_mm_alloc_large(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1311             : #if ZEND_DEBUG
    1312             :                 dbg = zend_mm_get_debug_info(heap, ptr);
    1313             :                 dbg->size = real_size;
    1314             :                 dbg->filename = __zend_filename;
    1315             :                 dbg->orig_filename = __zend_orig_filename;
    1316             :                 dbg->lineno = __zend_lineno;
    1317             :                 dbg->orig_lineno = __zend_orig_lineno;
    1318             : #endif
    1319      893102 :                 return ptr;
    1320             :         } else {
    1321             : #if ZEND_DEBUG
    1322             :                 size = real_size;
    1323             : #endif
    1324           0 :                 return zend_mm_alloc_huge(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1325             :         }
    1326             : }
    1327             : 
    1328             : static zend_always_inline void zend_mm_free_heap(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1329             : {
    1330    14862040 :         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1331             : 
    1332    14862040 :         if (UNEXPECTED(page_offset == 0)) {
    1333           0 :                 if (ptr != NULL) {
    1334           0 :                         zend_mm_free_huge(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1335             :                 }
    1336             :         } else {
    1337    14862040 :                 zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1338    14862040 :                 int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1339    14862040 :                 zend_mm_page_info info = chunk->map[page_num];
    1340             : 
    1341    14862040 :                 ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1342    14862040 :                 if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
    1343    13968940 :                         zend_mm_free_small(heap, ptr, ZEND_MM_SRUN_BIN_NUM(info));
    1344             :                 } else /* if (info & ZEND_MM_IS_LRUN) */ {
    1345      893100 :                         int pages_count = ZEND_MM_LRUN_PAGES(info);
    1346             : 
    1347      893100 :                         ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
    1348             :                         zend_mm_free_large(heap, chunk, page_num, pages_count);
    1349             :                 }
    1350             :         }
    1351             : }
    1352             : 
    1353           0 : static size_t zend_mm_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1354             : {
    1355           0 :         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1356             : 
    1357           0 :         if (UNEXPECTED(page_offset == 0)) {
    1358           0 :                 return zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1359             :         } else {
    1360             :                 zend_mm_chunk *chunk;
    1361             : #if 0 && ZEND_DEBUG
    1362             :                 zend_mm_debug_info *dbg = zend_mm_get_debug_info(heap, ptr);
    1363             :                 return dbg->size;
    1364             : #else
    1365             :                 int page_num;
    1366             :                 zend_mm_page_info info;
    1367             : 
    1368           0 :                 chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1369           0 :                 page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1370           0 :                 info = chunk->map[page_num];
    1371           0 :                 ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1372           0 :                 if (EXPECTED(info & ZEND_MM_IS_SRUN)) {
    1373           0 :                         return bin_data_size[ZEND_MM_SRUN_BIN_NUM(info)];
    1374             :                 } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ {
    1375           0 :                         return ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE;
    1376             :                 }
    1377             : #endif
    1378             :         }
    1379             : }
    1380             : 
    1381     7601286 : static void *zend_mm_realloc_heap(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1382             : {
    1383             :         size_t page_offset;
    1384             :         size_t old_size;
    1385             :         size_t new_size;
    1386             :         void *ret;
    1387             : #if ZEND_DEBUG
    1388             :         size_t real_size;
    1389             :         zend_mm_debug_info *dbg;
    1390             : #endif
    1391             : 
    1392     7601286 :         page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    1393     7601286 :         if (UNEXPECTED(page_offset == 0)) {
    1394     2258757 :                 if (UNEXPECTED(ptr == NULL)) {
    1395     2258757 :                         return zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1396             :                 }
    1397           0 :                 old_size = zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1398             : #if ZEND_DEBUG
    1399             :                 real_size = size;
    1400             :                 size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
    1401             : #endif
    1402           0 :                 if (size > ZEND_MM_MAX_LARGE_SIZE) {
    1403             : #if ZEND_DEBUG
    1404             :                         size = real_size;
    1405             : #endif
    1406           0 :                         new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
    1407           0 :                         if (new_size == old_size) {
    1408             : #if ZEND_DEBUG
    1409             :                                 zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1410             : #else
    1411           0 :                                 zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1412             : #endif
    1413           0 :                                 return ptr;
    1414             : #ifndef _WIN32
    1415           0 :                         } else if (new_size < old_size) {
    1416             :                                 /* unmup tail */
    1417           0 :                                 zend_mm_chunk_truncate(heap, ptr, old_size, new_size);
    1418             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1419           0 :                                 heap->real_size -= old_size - new_size;
    1420             : #endif
    1421             : #if ZEND_MM_STAT
    1422           0 :                                 heap->size -= old_size - new_size;
    1423             : #endif
    1424             : #if ZEND_DEBUG
    1425             :                                 zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1426             : #else
    1427           0 :                                 zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1428             : #endif
    1429           0 :                                 return ptr;
    1430             :                         } else /* if (new_size > old_size) */ {
    1431             : #if ZEND_MM_LIMIT
    1432           0 :                                 if (heap->real_size + (new_size - old_size) > heap->limit) {
    1433           0 :                                         if (heap->overflow == 0) {
    1434             : #if ZEND_DEBUG
    1435             :                                                 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);
    1436             : #else
    1437           0 :                                                 zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, size);
    1438             : #endif
    1439             :                                                 return NULL;
    1440             :                                         }
    1441             :                                 }
    1442             : #endif
    1443             :                                 /* try to map tail right after this block */
    1444           0 :                                 if (zend_mm_mmap_fixed((char*)ptr + old_size, new_size - old_size)) {
    1445             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1446           0 :                                         heap->real_size += new_size - old_size;
    1447             : #endif
    1448             : #if ZEND_MM_STAT
    1449           0 :                                         heap->size += new_size - old_size;
    1450             : #endif
    1451             : #if ZEND_DEBUG
    1452             :                                         zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1453             : #else
    1454           0 :                                         zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1455             : #endif
    1456           0 :                                         return ptr;
    1457             :                                 }
    1458             : #endif
    1459             :                         }
    1460             :                 }
    1461             :         } else {
    1462     5342529 :                 zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    1463     5342529 :                 int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
    1464     5342529 :                 zend_mm_page_info info = chunk->map[page_num];
    1465             : #if ZEND_DEBUG
    1466             :                 size_t real_size = size;
    1467             : 
    1468             :                 size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info));
    1469             : #endif
    1470             : 
    1471     5342529 :                 ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted");
    1472     5342529 :                 if (info & ZEND_MM_IS_SRUN) {
    1473             :                         int old_bin_num, bin_num;
    1474             : 
    1475     4761810 :                         old_bin_num = ZEND_MM_SRUN_BIN_NUM(info);
    1476     4761810 :                         old_size = bin_data_size[old_bin_num];
    1477     4761810 :                         bin_num = ZEND_MM_SMALL_SIZE_TO_BIN(size);
    1478     4761810 :                         if (old_bin_num == bin_num) {
    1479             : #if ZEND_DEBUG
    1480             :                                 dbg = zend_mm_get_debug_info(heap, ptr);
    1481             :                                 dbg->size = real_size;
    1482             :                                 dbg->filename = __zend_filename;
    1483             :                                 dbg->orig_filename = __zend_orig_filename;
    1484             :                                 dbg->lineno = __zend_lineno;
    1485             :                                 dbg->orig_lineno = __zend_orig_lineno;
    1486             : #endif
    1487     2638974 :                                 return ptr;
    1488             :                         }
    1489             :                 } else /* if (info & ZEND_MM_IS_LARGE_RUN) */ {
    1490      580719 :                         ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
    1491      580719 :                         old_size = ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE;
    1492      580719 :                         if (size > ZEND_MM_MAX_SMALL_SIZE && size <= ZEND_MM_MAX_LARGE_SIZE) {
    1493      579223 :                                 new_size = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE);
    1494      579223 :                                 if (new_size == old_size) {
    1495             : #if ZEND_DEBUG
    1496             :                                         dbg = zend_mm_get_debug_info(heap, ptr);
    1497             :                                         dbg->size = real_size;
    1498             :                                         dbg->filename = __zend_filename;
    1499             :                                         dbg->orig_filename = __zend_orig_filename;
    1500             :                                         dbg->lineno = __zend_lineno;
    1501             :                                         dbg->orig_lineno = __zend_orig_lineno;
    1502             : #endif
    1503      575448 :                                         return ptr;
    1504        3775 :                                 } else if (new_size < old_size) {
    1505             :                                         /* free tail pages */
    1506         701 :                                         int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE);
    1507         701 :                                         int rest_pages_count = (int)((old_size - new_size) / ZEND_MM_PAGE_SIZE);
    1508             : 
    1509             : #if ZEND_MM_STAT
    1510         701 :                                         heap->size -= rest_pages_count * ZEND_MM_PAGE_SIZE;
    1511             : #endif
    1512         701 :                                         chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count);
    1513         701 :                                         chunk->free_pages += rest_pages_count;
    1514         701 :                                         zend_mm_bitset_reset_range(chunk->free_map, page_num + new_pages_count, rest_pages_count);
    1515             : #if ZEND_DEBUG
    1516             :                                         dbg = zend_mm_get_debug_info(heap, ptr);
    1517             :                                         dbg->size = real_size;
    1518             :                                         dbg->filename = __zend_filename;
    1519             :                                         dbg->orig_filename = __zend_orig_filename;
    1520             :                                         dbg->lineno = __zend_lineno;
    1521             :                                         dbg->orig_lineno = __zend_orig_lineno;
    1522             : #endif
    1523         701 :                                         return ptr;
    1524             :                                 } else /* if (new_size > old_size) */ {
    1525        3074 :                                         int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE);
    1526        3074 :                                         int old_pages_count = (int)(old_size / ZEND_MM_PAGE_SIZE);
    1527             : 
    1528             :                                         /* try to allocate tail pages after this block */
    1529        5985 :                                         if (page_num + new_pages_count <= ZEND_MM_PAGES &&
    1530        2911 :                                             zend_mm_bitset_is_free_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count)) {
    1531             : #if ZEND_MM_STAT
    1532             :                                                 do {
    1533        2333 :                                                         size_t size = heap->size + (new_size - old_size);
    1534        2333 :                                                         size_t peak = MAX(heap->peak, size);
    1535        2333 :                                                         heap->size = size;
    1536        2333 :                                                         heap->peak = peak;
    1537             :                                                 } while (0);
    1538             : #endif
    1539        2333 :                                                 chunk->free_pages -= new_pages_count - old_pages_count;
    1540        2333 :                                                 zend_mm_bitset_set_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count);
    1541        2333 :                                                 chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count);
    1542             : #if ZEND_DEBUG
    1543             :                                                 dbg = zend_mm_get_debug_info(heap, ptr);
    1544             :                                                 dbg->size = real_size;
    1545             :                                                 dbg->filename = __zend_filename;
    1546             :                                                 dbg->orig_filename = __zend_orig_filename;
    1547             :                                                 dbg->lineno = __zend_lineno;
    1548             :                                                 dbg->orig_lineno = __zend_orig_lineno;
    1549             : #endif
    1550        2333 :                                                 return ptr;
    1551             :                                         }
    1552             :                                 }
    1553             :                         }
    1554             :                 }
    1555             : #if ZEND_DEBUG
    1556             :                 size = real_size;
    1557             : #endif
    1558             :         }
    1559             : 
    1560             :         /* Naive reallocation */
    1561     2125073 :         ret = zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1562     2125073 :         memcpy(ret, ptr, MIN(old_size, size));
    1563             :         zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1564     2125073 :         return ret;
    1565             : }
    1566             : 
    1567             : /*********************/
    1568             : /* Huge Runs (again) */
    1569             : /*********************/
    1570             : 
    1571             : #if ZEND_DEBUG
    1572             : 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)
    1573             : #else
    1574           0 : 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)
    1575             : #endif
    1576             : {
    1577           0 :         zend_mm_huge_list *list = (zend_mm_huge_list*)zend_mm_alloc_heap(heap, sizeof(zend_mm_huge_list) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1578           0 :         list->ptr = ptr;
    1579           0 :         list->size = size;
    1580           0 :         list->next = heap->huge_list;
    1581             : #if ZEND_DEBUG
    1582             :         list->dbg.size = dbg_size;
    1583             :         list->dbg.filename = __zend_filename;
    1584             :         list->dbg.orig_filename = __zend_orig_filename;
    1585             :         list->dbg.lineno = __zend_lineno;
    1586             :         list->dbg.orig_lineno = __zend_orig_lineno;
    1587             : #endif
    1588           0 :         heap->huge_list = list;
    1589           0 : }
    1590             : 
    1591           0 : static size_t zend_mm_del_huge_block(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1592             : {
    1593           0 :         zend_mm_huge_list *prev = NULL;
    1594           0 :         zend_mm_huge_list *list = heap->huge_list;
    1595           0 :         while (list != NULL) {
    1596           0 :                 if (list->ptr == ptr) {
    1597             :                         size_t size;
    1598             : 
    1599           0 :                         if (prev) {
    1600           0 :                                 prev->next = list->next;
    1601             :                         } else {
    1602           0 :                                 heap->huge_list = list->next;
    1603             :                         }
    1604           0 :                         size = list->size;
    1605             :                         zend_mm_free_heap(heap, list ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1606           0 :                         return size;
    1607             :                 }
    1608           0 :                 prev = list;
    1609           0 :                 list = list->next;
    1610             :         }
    1611           0 :         ZEND_MM_CHECK(0, "zend_mm_heap corrupted");
    1612             :         return 0;
    1613             : }
    1614             : 
    1615           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)
    1616             : {
    1617           0 :         zend_mm_huge_list *list = heap->huge_list;
    1618           0 :         while (list != NULL) {
    1619           0 :                 if (list->ptr == ptr) {
    1620           0 :                         return list->size;
    1621             :                 }
    1622           0 :                 list = list->next;
    1623             :         }
    1624           0 :         ZEND_MM_CHECK(0, "zend_mm_heap corrupted");
    1625             :         return 0;
    1626             : }
    1627             : 
    1628             : #if ZEND_DEBUG
    1629             : 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)
    1630             : #else
    1631           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)
    1632             : #endif
    1633             : {
    1634           0 :         zend_mm_huge_list *list = heap->huge_list;
    1635           0 :         while (list != NULL) {
    1636           0 :                 if (list->ptr == ptr) {
    1637           0 :                         list->size = size;
    1638             : #if ZEND_DEBUG
    1639             :                         list->dbg.size = dbg_size;
    1640             :                         list->dbg.filename = __zend_filename;
    1641             :                         list->dbg.orig_filename = __zend_orig_filename;
    1642             :                         list->dbg.lineno = __zend_lineno;
    1643             :                         list->dbg.orig_lineno = __zend_orig_lineno;
    1644             : #endif
    1645           0 :                         return;
    1646             :                 }
    1647           0 :                 list = list->next;
    1648             :         }
    1649             : }
    1650             : 
    1651           0 : static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1652             : {
    1653           0 :         size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE);
    1654             :         void *ptr;
    1655             : 
    1656             : #if ZEND_MM_LIMIT
    1657           0 :         if (heap->real_size + new_size > heap->limit) {
    1658           0 :                 if (heap->overflow == 0) {
    1659             : #if ZEND_DEBUG
    1660             :                         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);
    1661             : #else
    1662           0 :                         zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, size);
    1663             : #endif
    1664             :                         return NULL;
    1665             :                 }
    1666             :         }
    1667             : #endif
    1668           0 :         ptr = zend_mm_chunk_alloc(heap, new_size, ZEND_MM_CHUNK_SIZE);
    1669           0 :         if (UNEXPECTED(ptr == NULL)) {
    1670             :                 /* insufficient memory */
    1671             : #if !ZEND_MM_LIMIT
    1672             :                 zend_mm_safe_error(heap, "Out of memory");
    1673             : #elif ZEND_DEBUG
    1674             :                 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);
    1675             : #else
    1676           0 :                 zend_mm_safe_error(heap, "Out of memory (allocated %zu) (tried to allocate %zu bytes)", heap->real_size, size);
    1677             : #endif
    1678             :                 return NULL;
    1679             :         }
    1680             : #if ZEND_DEBUG
    1681             :         zend_mm_add_huge_block(heap, ptr, new_size, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1682             : #else
    1683           0 :         zend_mm_add_huge_block(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1684             : #endif
    1685             : #if ZEND_MM_STAT
    1686             :         do {
    1687           0 :                 size_t size = heap->real_size + new_size;
    1688           0 :                 size_t peak = MAX(heap->real_peak, size);
    1689           0 :                 heap->real_size = size;
    1690           0 :                 heap->real_peak = peak;
    1691             :         } while (0);
    1692             :         do {
    1693           0 :                 size_t size = heap->size + new_size;
    1694           0 :                 size_t peak = MAX(heap->peak, size);
    1695           0 :                 heap->size = size;
    1696           0 :                 heap->peak = peak;
    1697             :         } while (0);
    1698             : #elif ZEND_MM_LIMIT
    1699             :         heap->real_size += new_size;
    1700             : #endif
    1701           0 :         return ptr;
    1702             : }
    1703             : 
    1704           0 : static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    1705             : {
    1706             :         size_t size;
    1707             : 
    1708           0 :         ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE) == 0, "zend_mm_heap corrupted");
    1709           0 :         size = zend_mm_del_huge_block(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    1710           0 :         zend_mm_chunk_free(heap, ptr, size);
    1711             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1712           0 :         heap->real_size -= size;
    1713             : #endif
    1714             : #if ZEND_MM_STAT
    1715           0 :         heap->size -= size;
    1716             : #endif
    1717           0 : }
    1718             : 
    1719             : /******************/
    1720             : /* Initialization */
    1721             : /******************/
    1722             : 
    1723          10 : static zend_mm_heap *zend_mm_init(void)
    1724             : {
    1725          10 :         zend_mm_chunk *chunk = (zend_mm_chunk*)zend_mm_chunk_alloc_int(ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
    1726             :         zend_mm_heap *heap;
    1727             : 
    1728          10 :         if (UNEXPECTED(chunk == NULL)) {
    1729             : #if ZEND_MM_ERROR
    1730             : #ifdef _WIN32
    1731             :                 stderr_last_error("Can't initialize heap");
    1732             : #else
    1733           0 :                 fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
    1734             : #endif
    1735             : #endif
    1736           0 :                 return NULL;
    1737             :         }
    1738          10 :         heap = &chunk->heap_slot;
    1739          10 :         chunk->heap = heap;
    1740          10 :         chunk->next = chunk;
    1741          10 :         chunk->prev = chunk;
    1742          10 :         chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    1743          10 :         chunk->free_tail = ZEND_MM_FIRST_PAGE;
    1744          10 :         chunk->num = 0;
    1745          10 :         chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1;
    1746          10 :         chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
    1747          10 :         heap->main_chunk = chunk;
    1748          10 :         heap->cached_chunks = NULL;
    1749          10 :         heap->chunks_count = 1;
    1750          10 :         heap->peak_chunks_count = 1;
    1751          10 :         heap->cached_chunks_count = 0;
    1752          10 :         heap->avg_chunks_count = 1.0;
    1753             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    1754          10 :         heap->real_size = ZEND_MM_CHUNK_SIZE;
    1755             : #endif
    1756             : #if ZEND_MM_STAT
    1757          10 :         heap->real_peak = ZEND_MM_CHUNK_SIZE;
    1758          10 :         heap->size = 0;
    1759          10 :         heap->peak = 0;
    1760             : #endif
    1761             : #if ZEND_MM_LIMIT
    1762          10 :         heap->limit = (Z_L(-1) >> Z_L(1));
    1763          10 :         heap->overflow = 0;
    1764             : #endif
    1765             : #if ZEND_MM_CUSTOM
    1766          10 :         heap->use_custom_heap = 0;
    1767             : #endif
    1768             : #if ZEND_MM_STORAGE
    1769          10 :         heap->storage = NULL;
    1770             : #endif
    1771          10 :         heap->huge_list = NULL;
    1772          10 :         return heap;
    1773             : }
    1774             : 
    1775             : #if ZEND_DEBUG
    1776             : /******************/
    1777             : /* Leak detection */
    1778             : /******************/
    1779             : 
    1780             : static zend_long zend_mm_find_leaks_small(zend_mm_chunk *p, int i, int j, zend_leak_info *leak)
    1781             : {
    1782             :     int empty = 1;
    1783             :         zend_long count = 0;
    1784             :         int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]);
    1785             :         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)));
    1786             : 
    1787             :         while (j < bin_elements[bin_num]) {
    1788             :                 if (dbg->size != 0) {
    1789             :                         if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) {
    1790             :                                 count++;
    1791             :                                 dbg->size = 0;
    1792             :                                 dbg->filename = NULL;
    1793             :                                 dbg->lineno = 0;
    1794             :                         } else {
    1795             :                                 empty = 0;
    1796             :                         }
    1797             :                 }
    1798             :                 j++;
    1799             :                 dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]);
    1800             :         }
    1801             :         if (empty) {
    1802             :                 zend_mm_bitset_reset_range(p->free_map, i, bin_pages[bin_num]);
    1803             :         }
    1804             :         return count;
    1805             : }
    1806             : 
    1807             : static zend_long zend_mm_find_leaks(zend_mm_heap *heap, zend_mm_chunk *p, int i, zend_leak_info *leak)
    1808             : {
    1809             :         zend_long count = 0;
    1810             : 
    1811             :         do {
    1812             :                 while (i < p->free_tail) {
    1813             :                         if (zend_mm_bitset_is_set(p->free_map, i)) {
    1814             :                                 if (p->map[i] & ZEND_MM_IS_SRUN) {
    1815             :                                         int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]);
    1816             :                                         count += zend_mm_find_leaks_small(p, i, 0, leak);
    1817             :                                         i += bin_pages[bin_num];
    1818             :                                 } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ {
    1819             :                                         int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]);
    1820             :                                         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)));
    1821             : 
    1822             :                                         if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) {
    1823             :                                                 count++;
    1824             :                                         }
    1825             :                                         zend_mm_bitset_reset_range(p->free_map, i, pages_count);
    1826             :                                         i += pages_count;
    1827             :                                 }
    1828             :                         } else {
    1829             :                                 i++;
    1830             :                         }
    1831             :                 }
    1832             :                 p = p->next;
    1833             :         } while (p != heap->main_chunk);
    1834             :         return count;
    1835             : }
    1836             : 
    1837             : static void zend_mm_check_leaks(zend_mm_heap *heap)
    1838             : {
    1839             :         zend_mm_huge_list *list;
    1840             :         zend_mm_chunk *p;
    1841             :         zend_leak_info leak;
    1842             :         zend_long repeated = 0;
    1843             :         uint32_t total = 0;
    1844             :         int i, j;
    1845             : 
    1846             :         /* find leaked huge blocks and free them */
    1847             :         list = heap->huge_list;
    1848             :         while (list) {
    1849             :                 zend_mm_huge_list *q = list;
    1850             : 
    1851             :                 heap->huge_list = list->next;
    1852             : 
    1853             :                 leak.addr = list->ptr;
    1854             :                 leak.size = list->dbg.size;
    1855             :                 leak.filename = list->dbg.filename;
    1856             :                 leak.orig_filename = list->dbg.orig_filename;
    1857             :                 leak.lineno = list->dbg.lineno;
    1858             :                 leak.orig_lineno = list->dbg.orig_lineno;
    1859             : 
    1860             :                 zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL);
    1861             :                 zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak);
    1862             : //???           repeated = zend_mm_find_leaks_huge(segment, p);
    1863             :                 total += 1 + repeated;
    1864             :                 if (repeated) {
    1865             :                         zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated);
    1866             :                 }
    1867             : 
    1868             :                 list = list->next;
    1869             :                 zend_mm_chunk_free(heap, q->ptr, q->size);
    1870             :                 zend_mm_free_heap(heap, q, NULL, 0, NULL, 0);
    1871             :         }
    1872             : 
    1873             :         /* for each chunk */
    1874             :         p = heap->main_chunk;
    1875             :         do {
    1876             :                 i = ZEND_MM_FIRST_PAGE;
    1877             :                 while (i < p->free_tail) {
    1878             :                         if (zend_mm_bitset_is_set(p->free_map, i)) {
    1879             :                                 if (p->map[i] & ZEND_MM_IS_SRUN) {
    1880             :                                         int bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]);
    1881             :                                         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)));
    1882             : 
    1883             :                                         j = 0;
    1884             :                                         while (j < bin_elements[bin_num]) {
    1885             :                                                 if (dbg->size != 0) {
    1886             :                                                         leak.addr = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] * j);
    1887             :                                                         leak.size = dbg->size;
    1888             :                                                         leak.filename = dbg->filename;
    1889             :                                                         leak.orig_filename = dbg->orig_filename;
    1890             :                                                         leak.lineno = dbg->lineno;
    1891             :                                                         leak.orig_lineno = dbg->orig_lineno;
    1892             : 
    1893             :                                                         zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL);
    1894             :                                                         zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak);
    1895             : 
    1896             :                                                         dbg->size = 0;
    1897             :                                                         dbg->filename = NULL;
    1898             :                                                         dbg->lineno = 0;
    1899             : 
    1900             :                                                         repeated = zend_mm_find_leaks_small(p, i, j + 1, &leak) +
    1901             :                                                                    zend_mm_find_leaks(heap, p, i + bin_pages[bin_num], &leak);
    1902             :                                                         total += 1 + repeated;
    1903             :                                                         if (repeated) {
    1904             :                                                                 zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated);
    1905             :                                                         }
    1906             :                                                 }
    1907             :                                                 dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]);
    1908             :                                                 j++;
    1909             :                                         }
    1910             :                                         i += bin_pages[bin_num];
    1911             :                                 } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ {
    1912             :                                         int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]);
    1913             :                                         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)));
    1914             : 
    1915             :                                         leak.addr = (void*)((char*)p + ZEND_MM_PAGE_SIZE * i);
    1916             :                                         leak.size = dbg->size;
    1917             :                                         leak.filename = dbg->filename;
    1918             :                                         leak.orig_filename = dbg->orig_filename;
    1919             :                                         leak.lineno = dbg->lineno;
    1920             :                                         leak.orig_lineno = dbg->orig_lineno;
    1921             : 
    1922             :                                         zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL);
    1923             :                                         zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak);
    1924             : 
    1925             :                                         zend_mm_bitset_reset_range(p->free_map, i, pages_count);
    1926             : 
    1927             :                                         repeated = zend_mm_find_leaks(heap, p, i + pages_count, &leak);
    1928             :                                         total += 1 + repeated;
    1929             :                                         if (repeated) {
    1930             :                                                 zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated);
    1931             :                                         }
    1932             :                                         i += pages_count;
    1933             :                                 }
    1934             :                         } else {
    1935             :                                 i++;
    1936             :                         }
    1937             :                 }
    1938             :                 p = p->next;
    1939             :         } while (p != heap->main_chunk);
    1940             :         if (total) {
    1941             :                 zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &total);
    1942             :         }
    1943             : }
    1944             : #endif
    1945             : 
    1946       62640 : void zend_mm_shutdown(zend_mm_heap *heap, int full, int silent)
    1947             : {
    1948             :         zend_mm_chunk *p;
    1949             :         zend_mm_huge_list *list;
    1950             : 
    1951             : #if ZEND_MM_CUSTOM
    1952       62640 :         if (heap->use_custom_heap) {
    1953       62610 :                 return;
    1954             :         }
    1955             : #endif
    1956             : 
    1957             : #if ZEND_DEBUG
    1958             :         if (!silent) {
    1959             :                 zend_mm_check_leaks(heap);
    1960             :         }
    1961             : #endif
    1962             : 
    1963             :         /* free huge blocks */
    1964          30 :         list = heap->huge_list;
    1965          60 :         while (list) {
    1966           0 :                 zend_mm_huge_list *q = list;
    1967           0 :                 list = list->next;
    1968           0 :                 zend_mm_chunk_free(heap, q->ptr, q->size);
    1969             :         }
    1970             : 
    1971             :         /* move all chunks except of the first one into the cache */
    1972          30 :         p = heap->main_chunk->next;
    1973          61 :         while (p != heap->main_chunk) {
    1974           1 :                 zend_mm_chunk *q = p->next;
    1975           1 :                 p->next = heap->cached_chunks;
    1976           1 :                 heap->cached_chunks = p;
    1977           1 :                 p = q;
    1978           1 :                 heap->chunks_count--;
    1979           1 :                 heap->cached_chunks_count++;
    1980             :         }
    1981             : 
    1982          30 :         if (full) {
    1983             :                 /* free all cached chunks */
    1984          21 :                 while (heap->cached_chunks) {
    1985           1 :                         p = heap->cached_chunks;
    1986           1 :                         heap->cached_chunks = p->next;
    1987           1 :                         zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE);
    1988             :                 }
    1989             :                 /* free the first chunk */
    1990             : #if ZEND_MM_STORAGE
    1991          10 :                 if (UNEXPECTED(heap->storage)) {
    1992           0 :                         zend_mm_storage *storage = heap->storage;
    1993           0 :                         zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE);
    1994           0 :                         storage->dtor(storage);
    1995             :                 } else {
    1996          10 :                         zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE);
    1997             :                 }
    1998             : #else
    1999             :                 zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE);
    2000             : #endif
    2001             :         } else {
    2002             :                 zend_mm_heap old_heap;
    2003             : 
    2004             :                 /* free some cached chunks to keep average count */
    2005          20 :                 heap->avg_chunks_count = (heap->avg_chunks_count + (double)heap->peak_chunks_count) / 2.0;
    2006          40 :                 while ((double)heap->cached_chunks_count + 0.9 > heap->avg_chunks_count &&
    2007           0 :                        heap->cached_chunks) {
    2008           0 :                         p = heap->cached_chunks;
    2009           0 :                         heap->cached_chunks = p->next;
    2010           0 :                         zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE);
    2011           0 :                         heap->cached_chunks_count--;
    2012             :                 }
    2013             :                 /* clear cached chunks */
    2014          20 :                 p = heap->cached_chunks;
    2015          41 :                 while (p != NULL) {
    2016           1 :                         zend_mm_chunk *q = p->next;
    2017           1 :                         memset(p, 0, sizeof(zend_mm_chunk));
    2018           1 :                         p->next = q;
    2019           1 :                         p = q;
    2020             :                 }
    2021             : 
    2022             :                 /* reinitialize the first chunk and heap */
    2023          20 :                 old_heap = *heap;
    2024          20 :                 p = heap->main_chunk;
    2025          20 :                 memset(p, 0, ZEND_MM_FIRST_PAGE * ZEND_MM_PAGE_SIZE);
    2026          20 :                 *heap = old_heap;
    2027          20 :                 memset(heap->free_slot, 0, sizeof(heap->free_slot));
    2028          20 :                 heap->main_chunk = p;
    2029          20 :                 p->heap = &p->heap_slot;
    2030          20 :                 p->next = p;
    2031          20 :                 p->prev = p;
    2032          20 :                 p->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    2033          20 :                 p->free_tail = ZEND_MM_FIRST_PAGE;
    2034          20 :                 p->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1;
    2035          20 :                 p->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
    2036          20 :                 heap->chunks_count = 1;
    2037          20 :                 heap->peak_chunks_count = 1;
    2038             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    2039          20 :                 heap->real_size = ZEND_MM_CHUNK_SIZE;
    2040             : #endif
    2041             : #if ZEND_MM_STAT
    2042          20 :                 heap->real_peak = ZEND_MM_CHUNK_SIZE;
    2043             : #endif
    2044             :         }
    2045             : }
    2046             : 
    2047             : /**************/
    2048             : /* PUBLIC API */
    2049             : /**************/
    2050             : 
    2051           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)
    2052             : {
    2053           0 :         return zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2054             : }
    2055             : 
    2056           0 : ZEND_API void ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2057             : {
    2058             :         zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2059           0 : }
    2060             : 
    2061           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)
    2062             : {
    2063           0 :         return zend_mm_realloc_heap(heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2064             : }
    2065             : 
    2066           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)
    2067             : {
    2068           0 :         return zend_mm_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2069             : }
    2070             : 
    2071             : /**********************/
    2072             : /* Allocation Manager */
    2073             : /**********************/
    2074             : 
    2075             : typedef struct _zend_alloc_globals {
    2076             :         zend_mm_heap *mm_heap;
    2077             : } zend_alloc_globals;
    2078             : 
    2079             : #ifdef ZTS
    2080             : static int alloc_globals_id;
    2081             : # define AG(v) ZEND_TSRMG(alloc_globals_id, zend_alloc_globals *, v)
    2082             : #else
    2083             : # define AG(v) (alloc_globals.v)
    2084             : static zend_alloc_globals alloc_globals;
    2085             : #endif
    2086             : 
    2087         140 : ZEND_API int is_zend_mm(void)
    2088             : {
    2089             : #if ZEND_MM_CUSTOM
    2090         140 :         return !AG(mm_heap)->use_custom_heap;
    2091             : #else
    2092             :         return 1;
    2093             : #endif
    2094             : }
    2095             : 
    2096             : #if !ZEND_DEBUG && !defined(_WIN32)
    2097             : #undef _emalloc
    2098             : 
    2099             : #if ZEND_MM_CUSTOM
    2100             : # define ZEND_MM_CUSTOM_ALLOCATOR(size) do { \
    2101             :                 if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
    2102             :                         return AG(mm_heap)->_malloc(size); \
    2103             :                 } \
    2104             :         } while (0)
    2105             : # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) do { \
    2106             :                 if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
    2107             :                         AG(mm_heap)->_free(ptr); \
    2108             :                         return; \
    2109             :                 } \
    2110             :         } while (0)
    2111             : #else
    2112             : # define ZEND_MM_CUSTOM_ALLOCATOR(size)
    2113             : # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr)
    2114             : #endif
    2115             : 
    2116             : # define _ZEND_BIN_ALLOCATOR(_num, _size, _elements, _pages, x, y) \
    2117             :         ZEND_API void* ZEND_FASTCALL _emalloc_ ## _size(void) { \
    2118             :                 ZEND_MM_CUSTOM_ALLOCATOR(_size); \
    2119             :                 return zend_mm_alloc_small(AG(mm_heap), _size, _num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
    2120             :         }
    2121             : 
    2122    11909736 : ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR, x, y)
    2123             : 
    2124       67401 : ZEND_API void* ZEND_FASTCALL _emalloc_large(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2125             : {
    2126             : 
    2127       67401 :         ZEND_MM_CUSTOM_ALLOCATOR(size);
    2128           0 :         return zend_mm_alloc_large(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2129             : }
    2130             : 
    2131           0 : ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size)
    2132             : {
    2133             : 
    2134           0 :         ZEND_MM_CUSTOM_ALLOCATOR(size);
    2135           0 :         return zend_mm_alloc_huge(AG(mm_heap), size);
    2136             : }
    2137             : 
    2138             : #if ZEND_DEBUG
    2139             : # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \
    2140             :         ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \
    2141             :                 ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \
    2142             :                 { \
    2143             :                         size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); \
    2144             :                         zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \
    2145             :                         int page_num = page_offset / ZEND_MM_PAGE_SIZE; \
    2146             :                         ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \
    2147             :                         ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_SRUN); \
    2148             :                         ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(chunk->map[page_num]) == _num); \
    2149             :                         zend_mm_free_small(AG(mm_heap), ptr, _num); \
    2150             :                 } \
    2151             :         }
    2152             : #else
    2153             : # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \
    2154             :         ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \
    2155             :                 ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \
    2156             :                 { \
    2157             :                         zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \
    2158             :                         ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \
    2159             :                         zend_mm_free_small(AG(mm_heap), ptr, _num); \
    2160             :                 } \
    2161             :         }
    2162             : #endif
    2163             : 
    2164     4315856 : ZEND_MM_BINS_INFO(_ZEND_BIN_FREE, x, y)
    2165             : 
    2166           0 : ZEND_API void ZEND_FASTCALL _efree_large(void *ptr, size_t size)
    2167             : {
    2168             : 
    2169           0 :         ZEND_MM_CUSTOM_DEALLOCATOR(ptr);
    2170             :         {
    2171           0 :                 size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE);
    2172           0 :                 zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
    2173           0 :                 int page_num = page_offset / ZEND_MM_PAGE_SIZE;
    2174           0 :                 int pages_count = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE) / ZEND_MM_PAGE_SIZE;
    2175             : 
    2176           0 :                 ZEND_MM_CHECK(chunk->heap == AG(mm_heap) && ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted");
    2177             :                 ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_LRUN);
    2178             :                 ZEND_ASSERT(ZEND_MM_LRUN_PAGES(chunk->map[page_num]) == pages_count);
    2179           0 :                 zend_mm_free_large(AG(mm_heap), chunk, page_num, pages_count);
    2180             :         }
    2181             : }
    2182             : 
    2183           0 : ZEND_API void ZEND_FASTCALL _efree_huge(void *ptr, size_t size)
    2184             : {
    2185             : 
    2186           0 :         ZEND_MM_CUSTOM_DEALLOCATOR(ptr);
    2187             :         // TODO: use size???
    2188           0 :         zend_mm_free_huge(AG(mm_heap), ptr);
    2189             : }
    2190             : #endif
    2191             : 
    2192    72881552 : ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2193             : {
    2194             : 
    2195             : #if ZEND_MM_CUSTOM
    2196    72881552 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2197    64342823 :                 return AG(mm_heap)->_malloc(size);
    2198             :         }
    2199             : #endif
    2200    17077458 :         return zend_mm_alloc_heap(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2201             : }
    2202             : 
    2203    80376353 : ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2204             : {
    2205             : 
    2206             : #if ZEND_MM_CUSTOM
    2207    80376353 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2208    67639386 :                 AG(mm_heap)->_free(ptr);
    2209    67639386 :                 return;
    2210             :         }
    2211             : #endif
    2212    12736967 :         zend_mm_free_heap(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2213             : }
    2214             : 
    2215    11789573 : ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2216             : {
    2217             : 
    2218    11789573 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2219     4188287 :                 return AG(mm_heap)->_realloc(ptr, size);
    2220             :         }
    2221     7601286 :         return zend_mm_realloc_heap(AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2222             : }
    2223             : 
    2224           0 : ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2225             : {
    2226           0 :         if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
    2227           0 :                 return 0;
    2228             :         }
    2229           0 :         return zend_mm_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2230             : }
    2231             : 
    2232             : static zend_always_inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
    2233             : {
    2234             :         int overflow;
    2235     2511415 :         size_t ret = zend_safe_address(nmemb, size, offset, &overflow);
    2236             : 
    2237     2511415 :         if (UNEXPECTED(overflow)) {
    2238           1 :                 zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
    2239             :                 return 0;
    2240             :         }
    2241     2511414 :         return ret;
    2242             : }
    2243             : 
    2244             : 
    2245     2115174 : 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)
    2246             : {
    2247     2115174 :         return emalloc_rel(safe_address(nmemb, size, offset));
    2248             : }
    2249             : 
    2250      274389 : ZEND_API void* ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset)
    2251             : {
    2252      274389 :         return pemalloc(safe_address(nmemb, size, offset), 1);
    2253             : }
    2254             : 
    2255      121852 : 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)
    2256             : {
    2257      121851 :         return erealloc_rel(ptr, safe_address(nmemb, size, offset));
    2258             : }
    2259             : 
    2260           0 : ZEND_API void* ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset)
    2261             : {
    2262           0 :         return perealloc(ptr, safe_address(nmemb, size, offset), 1);
    2263             : }
    2264             : 
    2265             : 
    2266     1450819 : ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2267             : {
    2268             :         void *p;
    2269             : 
    2270     1450819 :         HANDLE_BLOCK_INTERRUPTIONS();
    2271             : 
    2272     1450819 :         p = _safe_emalloc(nmemb, size, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2273     1450819 :         if (UNEXPECTED(p == NULL)) {
    2274           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2275           0 :                 return p;
    2276             :         }
    2277     1450819 :         memset(p, 0, size * nmemb);
    2278     1450819 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2279     1450819 :         return p;
    2280             : }
    2281             : 
    2282      589508 : ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2283             : {
    2284             :         size_t length;
    2285             :         char *p;
    2286             : 
    2287      589508 :         HANDLE_BLOCK_INTERRUPTIONS();
    2288             : 
    2289      589508 :         length = strlen(s)+1;
    2290      589508 :         p = (char *) _emalloc(length ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2291      589508 :         if (UNEXPECTED(p == NULL)) {
    2292           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2293           0 :                 return p;
    2294             :         }
    2295      589508 :         memcpy(p, s, length);
    2296      589508 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2297      589508 :         return p;
    2298             : }
    2299             : 
    2300      535692 : ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
    2301             : {
    2302             :         char *p;
    2303             : 
    2304      535692 :         HANDLE_BLOCK_INTERRUPTIONS();
    2305             : 
    2306      535692 :         p = (char *) _emalloc(length+1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
    2307      535692 :         if (UNEXPECTED(p == NULL)) {
    2308           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2309           0 :                 return p;
    2310             :         }
    2311      535692 :         memcpy(p, s, length);
    2312      535692 :         p[length] = 0;
    2313      535692 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2314      535692 :         return p;
    2315             : }
    2316             : 
    2317             : 
    2318       60701 : ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
    2319             : {
    2320             :         char *p;
    2321             : 
    2322       60701 :         HANDLE_BLOCK_INTERRUPTIONS();
    2323             : 
    2324       60701 :         p = (char *) malloc(length+1);
    2325       60701 :         if (UNEXPECTED(p == NULL)) {
    2326           0 :                 HANDLE_UNBLOCK_INTERRUPTIONS();
    2327           0 :                 return p;
    2328             :         }
    2329       60701 :         if (length) {
    2330       60701 :                 memcpy(p, s, length);
    2331             :         }
    2332       60701 :         p[length] = 0;
    2333       60701 :         HANDLE_UNBLOCK_INTERRUPTIONS();
    2334       60701 :         return p;
    2335             : }
    2336             : 
    2337             : 
    2338       21718 : ZEND_API int zend_set_memory_limit(size_t memory_limit)
    2339             : {
    2340             : #if ZEND_MM_LIMIT
    2341       21718 :         AG(mm_heap)->limit = (memory_limit >= ZEND_MM_CHUNK_SIZE) ? memory_limit : ZEND_MM_CHUNK_SIZE;
    2342             : #endif
    2343       21718 :         return SUCCESS;
    2344             : }
    2345             : 
    2346        1572 : ZEND_API size_t zend_memory_usage(int real_usage)
    2347             : {
    2348             : #if ZEND_MM_STAT
    2349        1572 :         if (real_usage) {
    2350         442 :                 return AG(mm_heap)->real_size;
    2351             :         } else {
    2352        1130 :                 size_t usage = AG(mm_heap)->size;
    2353        1130 :                 return usage;
    2354             :         }
    2355             : #endif
    2356             :         return 0;
    2357             : }
    2358             : 
    2359           0 : ZEND_API size_t zend_memory_peak_usage(int real_usage)
    2360             : {
    2361             : #if ZEND_MM_STAT
    2362           0 :         if (real_usage) {
    2363           0 :                 return AG(mm_heap)->real_peak;
    2364             :         } else {
    2365           0 :                 return AG(mm_heap)->peak;
    2366             :         }
    2367             : #endif
    2368             :         return 0;
    2369             : }
    2370             : 
    2371       62640 : ZEND_API void shutdown_memory_manager(int silent, int full_shutdown)
    2372             : {
    2373       62640 :         zend_mm_shutdown(AG(mm_heap), full_shutdown, silent);
    2374       62640 : }
    2375             : 
    2376       20871 : static void alloc_globals_ctor(zend_alloc_globals *alloc_globals)
    2377             : {
    2378             : #if ZEND_MM_CUSTOM
    2379       20871 :         char *tmp = getenv("USE_ZEND_ALLOC");
    2380             : 
    2381       20871 :         if (tmp && !zend_atoi(tmp, 0)) {
    2382       20861 :                 alloc_globals->mm_heap = malloc(sizeof(zend_mm_heap));
    2383       20861 :                 memset(alloc_globals->mm_heap, 0, sizeof(zend_mm_heap));
    2384       20861 :                 alloc_globals->mm_heap->use_custom_heap = 1;
    2385       20861 :                 alloc_globals->mm_heap->_malloc = malloc;
    2386       20861 :                 alloc_globals->mm_heap->_free = free;
    2387       20861 :                 alloc_globals->mm_heap->_realloc = realloc;
    2388       20861 :                 return;
    2389             :         }
    2390             : #endif
    2391             :         ZEND_TSRMLS_CACHE_UPDATE;
    2392          10 :         alloc_globals->mm_heap = zend_mm_init();
    2393             : }
    2394             : 
    2395             : #ifdef ZTS
    2396             : static void alloc_globals_dtor(zend_alloc_globals *alloc_globals)
    2397             : {
    2398             :         zend_mm_shutdown(alloc_globals->mm_heap, 1, 1);
    2399             : }
    2400             : #endif
    2401             : 
    2402       20871 : ZEND_API void start_memory_manager(void)
    2403             : {
    2404             : #ifdef ZTS
    2405             :         ts_allocate_id(&alloc_globals_id, sizeof(zend_alloc_globals), (ts_allocate_ctor) alloc_globals_ctor, (ts_allocate_dtor) alloc_globals_dtor);
    2406             : #else
    2407       20871 :         alloc_globals_ctor(&alloc_globals);
    2408             : #endif
    2409             : #ifndef _WIN32
    2410             : #  if defined(_SC_PAGESIZE)
    2411       20871 :         REAL_PAGE_SIZE = sysconf(_SC_PAGESIZE);
    2412             : #  elif defined(_SC_PAGE_SIZE)
    2413             :         REAL_PAGE_SIZE = sysconf(_SC_PAGE_SIZE);
    2414             : #  endif
    2415             : #endif
    2416       20871 : }
    2417             : 
    2418           0 : ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap)
    2419             : {
    2420             :         zend_mm_heap *old_heap;
    2421             : 
    2422           0 :         old_heap = AG(mm_heap);
    2423           0 :         AG(mm_heap) = (zend_mm_heap*)new_heap;
    2424           0 :         return (zend_mm_heap*)old_heap;
    2425             : }
    2426             : 
    2427           0 : ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
    2428             :                                           void* (*_malloc)(size_t),
    2429             :                                           void  (*_free)(void*),
    2430             :                                           void* (*_realloc)(void*, size_t))
    2431             : {
    2432             : #if ZEND_MM_CUSTOM
    2433           0 :         zend_mm_heap *_heap = (zend_mm_heap*)heap;
    2434             : 
    2435           0 :         _heap->use_custom_heap = 1;
    2436           0 :         _heap->_malloc = _malloc;
    2437           0 :         _heap->_free = _free;
    2438           0 :         _heap->_realloc = _realloc;
    2439             : #endif
    2440           0 : }
    2441             : 
    2442           0 : ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
    2443             :                                           void* (**_malloc)(size_t),
    2444             :                                           void  (**_free)(void*),
    2445             :                                           void* (**_realloc)(void*, size_t))
    2446             : {
    2447             : #if ZEND_MM_CUSTOM
    2448           0 :         zend_mm_heap *_heap = (zend_mm_heap*)heap;
    2449             : 
    2450           0 :         if (heap->use_custom_heap) {
    2451           0 :                 *_malloc = _heap->_malloc;
    2452           0 :                 *_free = _heap->_free;
    2453           0 :                 *_realloc = _heap->_realloc;
    2454             :         } else {
    2455           0 :                 *_malloc = NULL;
    2456           0 :                 *_free = NULL;
    2457           0 :                 *_realloc = NULL;
    2458             :         }
    2459             : #else
    2460             :         *_malloc = NULL;
    2461             :         *_free = NULL;
    2462             :         *_realloc = NULL;
    2463             : #endif
    2464           0 : }
    2465             : 
    2466           0 : ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap)
    2467             : {
    2468             : #if ZEND_MM_CUSTOM
    2469           0 :         return heap->storage;
    2470             : #else
    2471             :         return NULL
    2472             : #endif
    2473             : }
    2474             : 
    2475           0 : ZEND_API zend_mm_heap *zend_mm_startup(void)
    2476             : {
    2477           0 :         return zend_mm_init();
    2478             : }
    2479             : 
    2480           0 : ZEND_API zend_mm_heap *zend_mm_startup_ex(zend_mm_storage *storage)
    2481             : {
    2482             : #if ZEND_MM_STORAGE
    2483           0 :         zend_mm_chunk *chunk = (zend_mm_chunk*)storage->chunk_alloc(storage, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
    2484             :         zend_mm_heap *heap;
    2485             : 
    2486           0 :         if (UNEXPECTED(chunk == NULL)) {
    2487             : #if ZEND_MM_ERROR
    2488             : #ifdef _WIN32
    2489             :                 stderr_last_error("Can't initialize heap");
    2490             : #else
    2491           0 :                 fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
    2492             : #endif
    2493             : #endif
    2494           0 :                 return NULL;
    2495             :         }
    2496           0 :         heap = &chunk->heap_slot;
    2497           0 :         chunk->heap = heap;
    2498           0 :         chunk->next = chunk;
    2499           0 :         chunk->prev = chunk;
    2500           0 :         chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE;
    2501           0 :         chunk->free_tail = ZEND_MM_FIRST_PAGE;
    2502           0 :         chunk->num = 0;
    2503           0 :         chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1;
    2504           0 :         chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
    2505           0 :         heap->main_chunk = chunk;
    2506           0 :         heap->cached_chunks = NULL;
    2507           0 :         heap->chunks_count = 1;
    2508           0 :         heap->peak_chunks_count = 1;
    2509           0 :         heap->cached_chunks_count = 0;
    2510           0 :         heap->avg_chunks_count = 1.0;
    2511             : #if ZEND_MM_STAT || ZEND_MM_LIMIT
    2512           0 :         heap->real_size = ZEND_MM_CHUNK_SIZE;
    2513             : #endif
    2514             : #if ZEND_MM_STAT
    2515           0 :         heap->real_peak = ZEND_MM_CHUNK_SIZE;
    2516           0 :         heap->size = 0;
    2517           0 :         heap->peak = 0;
    2518             : #endif
    2519             : #if ZEND_MM_LIMIT
    2520           0 :         heap->limit = (Z_L(-1) >> Z_L(1));
    2521           0 :         heap->overflow = 0;
    2522             : #endif
    2523             : #if ZEND_MM_CUSTOM
    2524           0 :         heap->use_custom_heap = 0;
    2525             : #endif
    2526           0 :         heap->storage = storage;
    2527           0 :         heap->huge_list = NULL;
    2528           0 :         return heap;
    2529             : #else
    2530             :         return NULL;
    2531             : #endif
    2532             : }
    2533             : 
    2534             : /*
    2535             :  * Local variables:
    2536             :  * tab-width: 4
    2537             :  * c-basic-offset: 4
    2538             :  * indent-tabs-mode: t
    2539             :  * End:
    2540             :  */

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:21 +0000 (3 days ago)

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