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: 480 822 58.4 %
Date: 2016-08-28 Functions: 63 125 50.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 28 Aug 2016 17:09:57 +0000 (35 hours ago)

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