Back to index

php5  5.3.10
zend_alloc.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    |          Dmitry Stogov <dmitry@zend.com>                             |
00018    +----------------------------------------------------------------------+
00019 */
00020 
00021 /* $Id: zend_alloc.c 321634 2012-01-01 13:15:04Z felipe $ */
00022 
00023 #include "zend.h"
00024 #include "zend_alloc.h"
00025 #include "zend_globals.h"
00026 #include "zend_operators.h"
00027 
00028 #ifdef HAVE_SIGNAL_H
00029 # include <signal.h>
00030 #endif
00031 #ifdef HAVE_UNISTD_H
00032 # include <unistd.h>
00033 #endif
00034 
00035 #ifdef ZEND_WIN32
00036 # include <wincrypt.h>
00037 # include <process.h>
00038 #endif
00039 
00040 #ifndef ZEND_MM_HEAP_PROTECTION
00041 # define ZEND_MM_HEAP_PROTECTION ZEND_DEBUG
00042 #endif
00043 
00044 #ifndef ZEND_MM_SAFE_UNLINKING
00045 # define ZEND_MM_SAFE_UNLINKING 1
00046 #endif
00047 
00048 #ifndef ZEND_MM_COOKIES
00049 # define ZEND_MM_COOKIES ZEND_DEBUG
00050 #endif
00051 
00052 #ifdef _WIN64
00053 # define PTR_FMT "0x%0.16I64x"
00054 /*
00055 #elif sizeof(long) == 8
00056 # define PTR_FMT "0x%0.16lx"
00057 */
00058 #else
00059 # define PTR_FMT "0x%0.8lx"
00060 #endif
00061 
00062 #if ZEND_DEBUG
00063 void zend_debug_alloc_output(char *format, ...)
00064 {
00065        char output_buf[256];
00066        va_list args;
00067 
00068        va_start(args, format);
00069        vsprintf(output_buf, format, args);
00070        va_end(args);
00071 
00072 #ifdef ZEND_WIN32
00073        OutputDebugString(output_buf);
00074 #else
00075        fprintf(stderr, "%s", output_buf);
00076 #endif
00077 }
00078 #endif
00079 
00080 #if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
00081 static void zend_mm_panic(const char *message) __attribute__ ((noreturn));
00082 #endif
00083 
00084 static void zend_mm_panic(const char *message)
00085 {
00086        fprintf(stderr, "%s\n", message);
00087 /* See http://support.microsoft.com/kb/190351 */
00088 #ifdef PHP_WIN32
00089        fflush(stderr);
00090 #endif
00091 #if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
00092        kill(getpid(), SIGSEGV);
00093 #endif
00094        exit(1);
00095 }
00096 
00097 /*******************/
00098 /* Storage Manager */
00099 /*******************/
00100 
00101 #ifdef ZEND_WIN32
00102 #  define HAVE_MEM_WIN32    /* use VirtualAlloc() to allocate memory     */
00103 #endif
00104 #define HAVE_MEM_MALLOC     /* use malloc() to allocate segments         */
00105 
00106 #include <sys/types.h>
00107 #include <sys/stat.h>
00108 #if HAVE_LIMITS_H
00109 #include <limits.h>
00110 #endif
00111 #include <fcntl.h>
00112 #include <errno.h>
00113 
00114 #if defined(HAVE_MEM_MMAP_ANON) || defined(HAVE_MEM_MMAP_ZERO)
00115 # ifdef HAVE_MREMAP
00116 #  ifndef _GNU_SOURCE
00117 #   define _GNU_SOURCE
00118 #  endif
00119 #  ifndef __USE_GNU
00120 #   define __USE_GNU
00121 #  endif
00122 # endif
00123 # include <sys/mman.h>
00124 # ifndef MAP_ANON
00125 #  ifdef MAP_ANONYMOUS
00126 #   define MAP_ANON MAP_ANONYMOUS
00127 #  endif
00128 # endif
00129 # ifndef MREMAP_MAYMOVE
00130 #  define MREMAP_MAYMOVE 0
00131 # endif
00132 # ifndef MAP_FAILED
00133 #  define MAP_FAILED ((void*)-1)
00134 # endif
00135 #endif
00136 
00137 static zend_mm_storage* zend_mm_mem_dummy_init(void *params)
00138 {
00139        return malloc(sizeof(zend_mm_storage));
00140 }
00141 
00142 static void zend_mm_mem_dummy_dtor(zend_mm_storage *storage)
00143 {
00144        free(storage);
00145 }
00146 
00147 static void zend_mm_mem_dummy_compact(zend_mm_storage *storage)
00148 {
00149 }
00150 
00151 #if defined(HAVE_MEM_MMAP_ANON) || defined(HAVE_MEM_MMAP_ZERO)
00152 
00153 static zend_mm_segment* zend_mm_mem_mmap_realloc(zend_mm_storage *storage, zend_mm_segment* segment, size_t size)
00154 {
00155        zend_mm_segment *ret;
00156 #ifdef HAVE_MREMAP
00157 #if defined(__NetBSD__)
00158        /* NetBSD 5 supports mremap but takes an extra newp argument */
00159        ret = (zend_mm_segment*)mremap(segment, segment->size, segment, size, MREMAP_MAYMOVE);
00160 #else
00161        ret = (zend_mm_segment*)mremap(segment, segment->size, size, MREMAP_MAYMOVE);
00162 #endif
00163        if (ret == MAP_FAILED) {
00164 #endif
00165               ret = storage->handlers->_alloc(storage, size);
00166               if (ret) {
00167                      memcpy(ret, segment, size > segment->size ? segment->size : size);
00168                      storage->handlers->_free(storage, segment);
00169               }
00170 #ifdef HAVE_MREMAP
00171        }
00172 #endif
00173        return ret;
00174 }
00175 
00176 static void zend_mm_mem_mmap_free(zend_mm_storage *storage, zend_mm_segment* segment)
00177 {
00178        munmap((void*)segment, segment->size);
00179 }
00180 
00181 #endif
00182 
00183 #ifdef HAVE_MEM_MMAP_ANON
00184 
00185 static zend_mm_segment* zend_mm_mem_mmap_anon_alloc(zend_mm_storage *storage, size_t size)
00186 {
00187        zend_mm_segment *ret = (zend_mm_segment*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
00188        if (ret == MAP_FAILED) {
00189               ret = NULL;
00190        }
00191        return ret;
00192 }
00193 
00194 # define ZEND_MM_MEM_MMAP_ANON_DSC {"mmap_anon", zend_mm_mem_dummy_init, zend_mm_mem_dummy_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_mmap_anon_alloc, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
00195 
00196 #endif
00197 
00198 #ifdef HAVE_MEM_MMAP_ZERO
00199 
00200 static int zend_mm_dev_zero_fd = -1;
00201 
00202 static zend_mm_storage* zend_mm_mem_mmap_zero_init(void *params)
00203 {
00204        if (zend_mm_dev_zero_fd != -1) {
00205               zend_mm_dev_zero_fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR);
00206        }
00207        if (zend_mm_dev_zero_fd >= 0) {
00208               return malloc(sizeof(zend_mm_storage));
00209        } else {
00210               return NULL;
00211        }
00212 }
00213 
00214 static void zend_mm_mem_mmap_zero_dtor(zend_mm_storage *storage)
00215 {
00216        close(zend_mm_dev_zero_fd);
00217        free(storage);
00218 }
00219 
00220 static zend_mm_segment* zend_mm_mem_mmap_zero_alloc(zend_mm_storage *storage, size_t size)
00221 {
00222        zend_mm_segment *ret = (zend_mm_segment*)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zend_mm_dev_zero_fd, 0);
00223        if (ret == MAP_FAILED) {
00224               ret = NULL;
00225        }
00226        return ret;
00227 }
00228 
00229 # define ZEND_MM_MEM_MMAP_ZERO_DSC {"mmap_zero", zend_mm_mem_mmap_zero_init, zend_mm_mem_mmap_zero_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_mmap_zero_alloc, zend_mm_mem_mmap_realloc, zend_mm_mem_mmap_free}
00230 
00231 #endif
00232 
00233 #ifdef HAVE_MEM_WIN32
00234 
00235 static zend_mm_storage* zend_mm_mem_win32_init(void *params)
00236 {
00237        HANDLE heap = HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
00238        zend_mm_storage* storage;
00239 
00240        if (heap == NULL) {
00241               return NULL;
00242        }
00243        storage = (zend_mm_storage*)malloc(sizeof(zend_mm_storage));
00244        if (storage == NULL) {
00245               HeapDestroy(heap);
00246               return NULL;
00247        }
00248        storage->data = (void*) heap;
00249        return storage;
00250 }
00251 
00252 static void zend_mm_mem_win32_dtor(zend_mm_storage *storage)
00253 {
00254        HeapDestroy((HANDLE)storage->data);
00255        free(storage);
00256 }
00257 
00258 static void zend_mm_mem_win32_compact(zend_mm_storage *storage)
00259 {
00260     HeapDestroy((HANDLE)storage->data);
00261     storage->data = (void*)HeapCreate(HEAP_NO_SERIALIZE, 0, 0);
00262 }
00263 
00264 static zend_mm_segment* zend_mm_mem_win32_alloc(zend_mm_storage *storage, size_t size)
00265 {
00266        return (zend_mm_segment*) HeapAlloc((HANDLE)storage->data, HEAP_NO_SERIALIZE, size);
00267 }
00268 
00269 static void zend_mm_mem_win32_free(zend_mm_storage *storage, zend_mm_segment* segment)
00270 {
00271        HeapFree((HANDLE)storage->data, HEAP_NO_SERIALIZE, segment);
00272 }
00273 
00274 static zend_mm_segment* zend_mm_mem_win32_realloc(zend_mm_storage *storage, zend_mm_segment* segment, size_t size)
00275 {
00276        return (zend_mm_segment*) HeapReAlloc((HANDLE)storage->data, HEAP_NO_SERIALIZE, segment, size);
00277 }
00278 
00279 # define ZEND_MM_MEM_WIN32_DSC {"win32", zend_mm_mem_win32_init, zend_mm_mem_win32_dtor, zend_mm_mem_win32_compact, zend_mm_mem_win32_alloc, zend_mm_mem_win32_realloc, zend_mm_mem_win32_free}
00280 
00281 #endif
00282 
00283 #ifdef HAVE_MEM_MALLOC
00284 
00285 static zend_mm_segment* zend_mm_mem_malloc_alloc(zend_mm_storage *storage, size_t size)
00286 {
00287        return (zend_mm_segment*)malloc(size);
00288 }
00289 
00290 static zend_mm_segment* zend_mm_mem_malloc_realloc(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size)
00291 {
00292        return (zend_mm_segment*)realloc(ptr, size);
00293 }
00294 
00295 static void zend_mm_mem_malloc_free(zend_mm_storage *storage, zend_mm_segment *ptr)
00296 {
00297        free(ptr);
00298 }
00299 
00300 # define ZEND_MM_MEM_MALLOC_DSC {"malloc", zend_mm_mem_dummy_init, zend_mm_mem_dummy_dtor, zend_mm_mem_dummy_compact, zend_mm_mem_malloc_alloc, zend_mm_mem_malloc_realloc, zend_mm_mem_malloc_free}
00301 
00302 #endif
00303 
00304 static const zend_mm_mem_handlers mem_handlers[] = {
00305 #ifdef HAVE_MEM_WIN32
00306        ZEND_MM_MEM_WIN32_DSC,
00307 #endif
00308 #ifdef HAVE_MEM_MALLOC
00309        ZEND_MM_MEM_MALLOC_DSC,
00310 #endif
00311 #ifdef HAVE_MEM_MMAP_ANON
00312        ZEND_MM_MEM_MMAP_ANON_DSC,
00313 #endif
00314 #ifdef HAVE_MEM_MMAP_ZERO
00315        ZEND_MM_MEM_MMAP_ZERO_DSC,
00316 #endif
00317        {NULL, NULL, NULL, NULL, NULL, NULL}
00318 };
00319 
00320 # define ZEND_MM_STORAGE_DTOR()                                       heap->storage->handlers->dtor(heap->storage)
00321 # define ZEND_MM_STORAGE_ALLOC(size)                           heap->storage->handlers->_alloc(heap->storage, size)
00322 # define ZEND_MM_STORAGE_REALLOC(ptr, size)                    heap->storage->handlers->_realloc(heap->storage, ptr, size)
00323 # define ZEND_MM_STORAGE_FREE(ptr)                             heap->storage->handlers->_free(heap->storage, ptr)
00324 
00325 /****************/
00326 /* Heap Manager */
00327 /****************/
00328 
00329 #define MEM_BLOCK_VALID  0x7312F8DC
00330 #define       MEM_BLOCK_FREED  0x99954317
00331 #define       MEM_BLOCK_CACHED 0xFB8277DC
00332 #define       MEM_BLOCK_GUARD  0x2A8FCC84
00333 #define       MEM_BLOCK_LEAK   0x6C5E8F2D
00334 
00335 /* mm block type */
00336 typedef struct _zend_mm_block_info {
00337 #if ZEND_MM_COOKIES
00338        size_t _cookie;
00339 #endif
00340        size_t _size;
00341        size_t _prev;
00342 } zend_mm_block_info;
00343 
00344 #if ZEND_DEBUG
00345 
00346 typedef struct _zend_mm_debug_info {
00347        char *filename;
00348        uint lineno;
00349        char *orig_filename;
00350        uint orig_lineno;
00351        size_t size;
00352 #if ZEND_MM_HEAP_PROTECTION
00353        unsigned int start_magic;
00354 #endif
00355 } zend_mm_debug_info;
00356 
00357 #elif ZEND_MM_HEAP_PROTECTION
00358 
00359 typedef struct _zend_mm_debug_info {
00360        size_t size;
00361        unsigned int start_magic;
00362 } zend_mm_debug_info;
00363 
00364 #endif
00365 
00366 typedef struct _zend_mm_block {
00367        zend_mm_block_info info;
00368 #if ZEND_DEBUG
00369        unsigned int magic;
00370 # ifdef ZTS
00371        THREAD_T thread_id;
00372 # endif
00373        zend_mm_debug_info debug;
00374 #elif ZEND_MM_HEAP_PROTECTION
00375        zend_mm_debug_info debug;
00376 #endif
00377 } zend_mm_block;
00378 
00379 typedef struct _zend_mm_small_free_block {
00380        zend_mm_block_info info;
00381 #if ZEND_DEBUG
00382        unsigned int magic;
00383 # ifdef ZTS
00384        THREAD_T thread_id;
00385 # endif
00386 #endif
00387        struct _zend_mm_free_block *prev_free_block;
00388        struct _zend_mm_free_block *next_free_block;
00389 } zend_mm_small_free_block;
00390 
00391 typedef struct _zend_mm_free_block {
00392        zend_mm_block_info info;
00393 #if ZEND_DEBUG
00394        unsigned int magic;
00395 # ifdef ZTS
00396        THREAD_T thread_id;
00397 # endif
00398 #endif
00399        struct _zend_mm_free_block *prev_free_block;
00400        struct _zend_mm_free_block *next_free_block;
00401 
00402        struct _zend_mm_free_block **parent;
00403        struct _zend_mm_free_block *child[2];
00404 } zend_mm_free_block;
00405 
00406 #define ZEND_MM_NUM_BUCKETS (sizeof(size_t) << 3)
00407 
00408 #define ZEND_MM_CACHE 1
00409 #define ZEND_MM_CACHE_SIZE (ZEND_MM_NUM_BUCKETS * 4 * 1024)
00410 
00411 #ifndef ZEND_MM_CACHE_STAT
00412 # define ZEND_MM_CACHE_STAT 0
00413 #endif
00414 
00415 struct _zend_mm_heap {
00416        int                 use_zend_alloc;
00417        void               *(*_malloc)(size_t);
00418        void                (*_free)(void*);
00419        void               *(*_realloc)(void*, size_t);
00420        size_t              free_bitmap;
00421        size_t              large_free_bitmap;
00422        size_t              block_size;
00423        size_t              compact_size;
00424        zend_mm_segment    *segments_list;
00425        zend_mm_storage    *storage;
00426        size_t              real_size;
00427        size_t              real_peak;
00428        size_t              limit;
00429        size_t              size;
00430        size_t              peak;
00431        size_t              reserve_size;
00432        void               *reserve;
00433        int                 overflow;
00434        int                 internal;
00435 #if ZEND_MM_CACHE
00436        unsigned int        cached;
00437        zend_mm_free_block *cache[ZEND_MM_NUM_BUCKETS];
00438 #endif
00439        zend_mm_free_block *free_buckets[ZEND_MM_NUM_BUCKETS*2];
00440        zend_mm_free_block *large_free_buckets[ZEND_MM_NUM_BUCKETS];
00441        zend_mm_free_block *rest_buckets[2];
00442 #if ZEND_MM_CACHE_STAT
00443        struct {
00444               int count;
00445               int max_count;
00446               int hit;
00447               int miss;
00448        } cache_stat[ZEND_MM_NUM_BUCKETS+1];
00449 #endif
00450 };
00451 
00452 #define ZEND_MM_SMALL_FREE_BUCKET(heap, index) \
00453        (zend_mm_free_block*) ((char*)&heap->free_buckets[index * 2] + \
00454               sizeof(zend_mm_free_block*) * 2 - \
00455               sizeof(zend_mm_small_free_block))
00456 
00457 #define ZEND_MM_REST_BUCKET(heap) \
00458        (zend_mm_free_block*)((char*)&heap->rest_buckets[0] + \
00459               sizeof(zend_mm_free_block*) * 2 - \
00460               sizeof(zend_mm_small_free_block))
00461 
00462 #if ZEND_MM_COOKIES
00463 
00464 static unsigned int _zend_mm_cookie = 0;
00465 
00466 # define ZEND_MM_COOKIE(block) \
00467        (((size_t)(block)) ^ _zend_mm_cookie)
00468 # define ZEND_MM_SET_COOKIE(block) \
00469        (block)->info._cookie = ZEND_MM_COOKIE(block)
00470 # define ZEND_MM_CHECK_COOKIE(block) \
00471        if (UNEXPECTED((block)->info._cookie != ZEND_MM_COOKIE(block))) { \
00472               zend_mm_panic("zend_mm_heap corrupted"); \
00473        }
00474 #else
00475 # define ZEND_MM_SET_COOKIE(block)
00476 # define ZEND_MM_CHECK_COOKIE(block)
00477 #endif
00478 
00479 /* Default memory segment size */
00480 #define ZEND_MM_SEG_SIZE   (256 * 1024)
00481 
00482 /* Reserved space for error reporting in case of memory overflow */
00483 #define ZEND_MM_RESERVE_SIZE            (8*1024)
00484 
00485 #ifdef _WIN64
00486 # define ZEND_MM_LONG_CONST(x)     (x##i64)
00487 #else
00488 # define ZEND_MM_LONG_CONST(x)     (x##L)
00489 #endif
00490 
00491 #define ZEND_MM_TYPE_MASK          ZEND_MM_LONG_CONST(0x3)
00492 
00493 #define ZEND_MM_FREE_BLOCK         ZEND_MM_LONG_CONST(0x0)
00494 #define ZEND_MM_USED_BLOCK         ZEND_MM_LONG_CONST(0x1)
00495 #define ZEND_MM_GUARD_BLOCK        ZEND_MM_LONG_CONST(0x3)
00496 
00497 #define ZEND_MM_BLOCK(b, type, size)      do { \
00498                                                                              size_t _size = (size); \
00499                                                                              (b)->info._size = (type) | _size; \
00500                                                                              ZEND_MM_BLOCK_AT(b, _size)->info._prev = (type) | _size; \
00501                                                                              ZEND_MM_SET_COOKIE(b); \
00502                                                                       } while (0);
00503 #define ZEND_MM_LAST_BLOCK(b)                    do { \
00504               (b)->info._size = ZEND_MM_GUARD_BLOCK | ZEND_MM_ALIGNED_HEADER_SIZE; \
00505               ZEND_MM_SET_MAGIC(b, MEM_BLOCK_GUARD); \
00506        } while (0);
00507 #define ZEND_MM_BLOCK_SIZE(b)                    ((b)->info._size & ~ZEND_MM_TYPE_MASK)
00508 #define ZEND_MM_IS_FREE_BLOCK(b)          (!((b)->info._size & ZEND_MM_USED_BLOCK))
00509 #define ZEND_MM_IS_USED_BLOCK(b)          ((b)->info._size & ZEND_MM_USED_BLOCK)
00510 #define ZEND_MM_IS_GUARD_BLOCK(b)         (((b)->info._size & ZEND_MM_TYPE_MASK) == ZEND_MM_GUARD_BLOCK)
00511 
00512 #define ZEND_MM_NEXT_BLOCK(b)                    ZEND_MM_BLOCK_AT(b, ZEND_MM_BLOCK_SIZE(b))
00513 #define ZEND_MM_PREV_BLOCK(b)                    ZEND_MM_BLOCK_AT(b, -(ssize_t)((b)->info._prev & ~ZEND_MM_TYPE_MASK))
00514 
00515 #define ZEND_MM_PREV_BLOCK_IS_FREE(b)     (!((b)->info._prev & ZEND_MM_USED_BLOCK))
00516 
00517 #define ZEND_MM_MARK_FIRST_BLOCK(b)              ((b)->info._prev = ZEND_MM_GUARD_BLOCK)
00518 #define ZEND_MM_IS_FIRST_BLOCK(b)         ((b)->info._prev == ZEND_MM_GUARD_BLOCK)
00519 
00520 /* optimized access */
00521 #define ZEND_MM_FREE_BLOCK_SIZE(b)        (b)->info._size
00522 
00523 /* Aligned header size */
00524 #define ZEND_MM_ALIGNED_HEADER_SIZE                     ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_block))
00525 #define ZEND_MM_ALIGNED_FREE_HEADER_SIZE  ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_small_free_block))
00526 #define ZEND_MM_MIN_ALLOC_BLOCK_SIZE             ZEND_MM_ALIGNED_SIZE(ZEND_MM_ALIGNED_HEADER_SIZE + END_MAGIC_SIZE)
00527 #define ZEND_MM_ALIGNED_MIN_HEADER_SIZE          (ZEND_MM_MIN_ALLOC_BLOCK_SIZE>ZEND_MM_ALIGNED_FREE_HEADER_SIZE?ZEND_MM_MIN_ALLOC_BLOCK_SIZE:ZEND_MM_ALIGNED_FREE_HEADER_SIZE)
00528 #define ZEND_MM_ALIGNED_SEGMENT_SIZE             ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_segment))
00529 
00530 #define ZEND_MM_MIN_SIZE                                ((ZEND_MM_ALIGNED_MIN_HEADER_SIZE>(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE))?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE-(ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE)):0)
00531 
00532 #define ZEND_MM_MAX_SMALL_SIZE                          ((ZEND_MM_NUM_BUCKETS<<ZEND_MM_ALIGNMENT_LOG2)+ZEND_MM_ALIGNED_MIN_HEADER_SIZE)
00533 
00534 #define ZEND_MM_TRUE_SIZE(size)                         ((size<ZEND_MM_MIN_SIZE)?(ZEND_MM_ALIGNED_MIN_HEADER_SIZE):(ZEND_MM_ALIGNED_SIZE(size+ZEND_MM_ALIGNED_HEADER_SIZE+END_MAGIC_SIZE)))
00535 
00536 #define ZEND_MM_BUCKET_INDEX(true_size)          ((true_size>>ZEND_MM_ALIGNMENT_LOG2)-(ZEND_MM_ALIGNED_MIN_HEADER_SIZE>>ZEND_MM_ALIGNMENT_LOG2))
00537 
00538 #define ZEND_MM_SMALL_SIZE(true_size)            (true_size < ZEND_MM_MAX_SMALL_SIZE)
00539 
00540 /* Memory calculations */
00541 #define ZEND_MM_BLOCK_AT(blk, offset)     ((zend_mm_block *) (((char *) (blk))+(offset)))
00542 #define ZEND_MM_DATA_OF(p)                       ((void *) (((char *) (p))+ZEND_MM_ALIGNED_HEADER_SIZE))
00543 #define ZEND_MM_HEADER_OF(blk)                   ZEND_MM_BLOCK_AT(blk, -(int)ZEND_MM_ALIGNED_HEADER_SIZE)
00544 
00545 /* Debug output */
00546 #if ZEND_DEBUG
00547 
00548 # ifdef ZTS
00549 #  define ZEND_MM_SET_THREAD_ID(block) \
00550        ((zend_mm_block*)(block))->thread_id = tsrm_thread_id()
00551 #  define ZEND_MM_BAD_THREAD_ID(block) ((block)->thread_id != tsrm_thread_id())
00552 # else
00553 #  define ZEND_MM_SET_THREAD_ID(block)
00554 #  define ZEND_MM_BAD_THREAD_ID(block) 0
00555 # endif
00556 
00557 # define ZEND_MM_VALID_PTR(block) \
00558        zend_mm_check_ptr(heap, block, 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)
00559 
00560 # define ZEND_MM_SET_MAGIC(block, val) do { \
00561               (block)->magic = (val); \
00562        } while (0)
00563 
00564 # define ZEND_MM_CHECK_MAGIC(block, val) do { \
00565               if ((block)->magic != (val)) { \
00566                      zend_mm_panic("zend_mm_heap corrupted"); \
00567               } \
00568        } while (0)
00569 
00570 # define ZEND_MM_SET_DEBUG_INFO(block, __size, set_valid, set_thread) do { \
00571               ((zend_mm_block*)(block))->debug.filename = __zend_filename; \
00572               ((zend_mm_block*)(block))->debug.lineno = __zend_lineno; \
00573               ((zend_mm_block*)(block))->debug.orig_filename = __zend_orig_filename; \
00574               ((zend_mm_block*)(block))->debug.orig_lineno = __zend_orig_lineno; \
00575               ZEND_MM_SET_BLOCK_SIZE(block, __size); \
00576               if (set_valid) { \
00577                      ZEND_MM_SET_MAGIC(block, MEM_BLOCK_VALID); \
00578               } \
00579               if (set_thread) { \
00580                      ZEND_MM_SET_THREAD_ID(block); \
00581               } \
00582        } while (0)
00583 
00584 #else
00585 
00586 # define ZEND_MM_VALID_PTR(ptr) EXPECTED(ptr != NULL)
00587 
00588 # define ZEND_MM_SET_MAGIC(block, val)
00589 
00590 # define ZEND_MM_CHECK_MAGIC(block, val)
00591 
00592 # define ZEND_MM_SET_DEBUG_INFO(block, __size, set_valid, set_thread) ZEND_MM_SET_BLOCK_SIZE(block, __size)
00593 
00594 #endif
00595 
00596 
00597 #if ZEND_MM_HEAP_PROTECTION
00598 
00599 # define ZEND_MM_CHECK_PROTECTION(block) \
00600        do { \
00601               if ((block)->debug.start_magic != _mem_block_start_magic || \
00602                   memcmp(ZEND_MM_END_MAGIC_PTR(block), &_mem_block_end_magic, END_MAGIC_SIZE) != 0) { \
00603                   zend_mm_panic("zend_mm_heap corrupted"); \
00604               } \
00605        } while (0)
00606 
00607 # define ZEND_MM_END_MAGIC_PTR(block) \
00608        (((char*)(ZEND_MM_DATA_OF(block))) + ((zend_mm_block*)(block))->debug.size)
00609 
00610 # define END_MAGIC_SIZE sizeof(unsigned int)
00611 
00612 # define ZEND_MM_SET_BLOCK_SIZE(block, __size) do { \
00613               char *p; \
00614               ((zend_mm_block*)(block))->debug.size = (__size); \
00615               p = ZEND_MM_END_MAGIC_PTR(block); \
00616               ((zend_mm_block*)(block))->debug.start_magic = _mem_block_start_magic; \
00617               memcpy(p, &_mem_block_end_magic, END_MAGIC_SIZE); \
00618        } while (0)
00619 
00620 static unsigned int _mem_block_start_magic = 0;
00621 static unsigned int _mem_block_end_magic   = 0;
00622 
00623 #else
00624 
00625 # if ZEND_DEBUG
00626 #  define ZEND_MM_SET_BLOCK_SIZE(block, _size) \
00627        ((zend_mm_block*)(block))->debug.size = (_size)
00628 # else
00629 #  define ZEND_MM_SET_BLOCK_SIZE(block, _size)
00630 # endif
00631 
00632 # define ZEND_MM_CHECK_PROTECTION(block)
00633 
00634 # define END_MAGIC_SIZE 0
00635 
00636 #endif
00637 
00638 #if ZEND_MM_SAFE_UNLINKING
00639 # define ZEND_MM_CHECK_BLOCK_LINKAGE(block) \
00640        if (UNEXPECTED((block)->info._size != ZEND_MM_BLOCK_AT(block, ZEND_MM_FREE_BLOCK_SIZE(block))->info._prev) || \
00641               UNEXPECTED(!UNEXPECTED(ZEND_MM_IS_FIRST_BLOCK(block)) && \
00642            UNEXPECTED(ZEND_MM_PREV_BLOCK(block)->info._size != (block)->info._prev))) { \
00643            zend_mm_panic("zend_mm_heap corrupted"); \
00644        }
00645 #define ZEND_MM_CHECK_TREE(block) \
00646        if (UNEXPECTED(*((block)->parent) != (block))) { \
00647               zend_mm_panic("zend_mm_heap corrupted"); \
00648        }
00649 #else
00650 # define ZEND_MM_CHECK_BLOCK_LINKAGE(block)
00651 # define ZEND_MM_CHECK_TREE(block)
00652 #endif
00653 
00654 #define ZEND_MM_LARGE_BUCKET_INDEX(S) zend_mm_high_bit(S)
00655 
00656 static void *_zend_mm_alloc_int(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
00657 static void _zend_mm_free_int(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00658 static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00659 
00660 static inline unsigned int zend_mm_high_bit(size_t _size)
00661 {
00662 #if defined(__GNUC__) && defined(i386)
00663        unsigned int n;
00664 
00665        __asm__("bsrl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
00666        return n;
00667 #elif defined(__GNUC__) && defined(__x86_64__)
00668        unsigned long n;
00669 
00670         __asm__("bsrq %1,%0\n\t" : "=r" (n) : "rm"  (_size));
00671         return (unsigned int)n;
00672 #elif defined(_MSC_VER) && defined(_M_IX86)
00673        __asm {
00674               bsr eax, _size
00675        }
00676 #else
00677        unsigned int n = 0;
00678        while (_size != 0) {
00679               _size = _size >> 1;
00680               n++;
00681        }
00682        return n-1;
00683 #endif
00684 }
00685 
00686 static inline unsigned int zend_mm_low_bit(size_t _size)
00687 {
00688 #if defined(__GNUC__) && defined(i386)
00689        unsigned int n;
00690 
00691        __asm__("bsfl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
00692        return n;
00693 #elif defined(__GNUC__) && defined(__x86_64__)
00694         unsigned long n;
00695 
00696         __asm__("bsfq %1,%0\n\t" : "=r" (n) : "rm"  (_size));
00697         return (unsigned int)n;
00698 #elif defined(_MSC_VER) && defined(_M_IX86)
00699        __asm {
00700               bsf eax, _size
00701    }
00702 #else
00703        static const int offset[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
00704        unsigned int n;
00705        unsigned int index = 0;
00706 
00707        n = offset[_size & 15];
00708        while (n == 4) {
00709               _size >>= 4;
00710               index += n;
00711               n = offset[_size & 15];
00712        }
00713 
00714        return index + n;
00715 #endif
00716 }
00717 
00718 static inline void zend_mm_add_to_rest_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
00719 {
00720        zend_mm_free_block *prev, *next;
00721 
00722        ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);
00723 
00724        if (!ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block))) {
00725               mm_block->parent = NULL;
00726        }
00727 
00728        prev = heap->rest_buckets[0];
00729        next = prev->next_free_block;
00730        mm_block->prev_free_block = prev;
00731        mm_block->next_free_block = next;
00732        prev->next_free_block = next->prev_free_block = mm_block;
00733 }
00734 
00735 static inline void zend_mm_add_to_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
00736 {
00737        size_t size;
00738        size_t index;
00739 
00740        ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);
00741 
00742        size = ZEND_MM_FREE_BLOCK_SIZE(mm_block);
00743        if (EXPECTED(!ZEND_MM_SMALL_SIZE(size))) {
00744               zend_mm_free_block **p;
00745 
00746               index = ZEND_MM_LARGE_BUCKET_INDEX(size);
00747               p = &heap->large_free_buckets[index];
00748               mm_block->child[0] = mm_block->child[1] = NULL;
00749               if (!*p) {
00750                      *p = mm_block;
00751                      mm_block->parent = p;
00752                      mm_block->prev_free_block = mm_block->next_free_block = mm_block;
00753                      heap->large_free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
00754               } else {
00755                      size_t m;
00756 
00757                      for (m = size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
00758                             zend_mm_free_block *prev = *p;
00759 
00760                             if (ZEND_MM_FREE_BLOCK_SIZE(prev) != size) {
00761                                    p = &prev->child[(m >> (ZEND_MM_NUM_BUCKETS-1)) & 1];
00762                                    if (!*p) {
00763                                           *p = mm_block;
00764                                           mm_block->parent = p;
00765                                           mm_block->prev_free_block = mm_block->next_free_block = mm_block;
00766                                           break;
00767                                    }
00768                             } else {
00769                                    zend_mm_free_block *next = prev->next_free_block;
00770 
00771                                    prev->next_free_block = next->prev_free_block = mm_block;
00772                                    mm_block->next_free_block = next;
00773                                    mm_block->prev_free_block = prev;
00774                                    mm_block->parent = NULL;
00775                                    break;
00776                             }
00777                      }
00778               }
00779        } else {
00780               zend_mm_free_block *prev, *next;
00781 
00782               index = ZEND_MM_BUCKET_INDEX(size);
00783 
00784               prev = ZEND_MM_SMALL_FREE_BUCKET(heap, index);
00785               if (prev->prev_free_block == prev) {
00786                      heap->free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
00787               }
00788               next = prev->next_free_block;
00789 
00790               mm_block->prev_free_block = prev;
00791               mm_block->next_free_block = next;
00792               prev->next_free_block = next->prev_free_block = mm_block;
00793        }
00794 }
00795 
00796 static inline void zend_mm_remove_from_free_list(zend_mm_heap *heap, zend_mm_free_block *mm_block)
00797 {
00798        zend_mm_free_block *prev = mm_block->prev_free_block;
00799        zend_mm_free_block *next = mm_block->next_free_block;
00800 
00801        ZEND_MM_CHECK_MAGIC(mm_block, MEM_BLOCK_FREED);
00802 
00803        if (EXPECTED(prev == mm_block)) {
00804               zend_mm_free_block **rp, **cp;
00805 
00806 #if ZEND_MM_SAFE_UNLINKING
00807               if (UNEXPECTED(next != mm_block)) {
00808                      zend_mm_panic("zend_mm_heap corrupted");
00809               }
00810 #endif
00811 
00812               rp = &mm_block->child[mm_block->child[1] != NULL];
00813               prev = *rp;
00814               if (EXPECTED(prev == NULL)) {
00815                      size_t index = ZEND_MM_LARGE_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));
00816 
00817                      ZEND_MM_CHECK_TREE(mm_block);
00818                      *mm_block->parent = NULL;
00819                      if (mm_block->parent == &heap->large_free_buckets[index]) {
00820                             heap->large_free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
00821                   }
00822               } else {
00823                      while (*(cp = &(prev->child[prev->child[1] != NULL])) != NULL) {
00824                             prev = *cp;
00825                             rp = cp;
00826                      }
00827                      *rp = NULL;
00828 
00829 subst_block:
00830                      ZEND_MM_CHECK_TREE(mm_block);
00831                      *mm_block->parent = prev;
00832                      prev->parent = mm_block->parent;
00833                      if ((prev->child[0] = mm_block->child[0])) {
00834                             ZEND_MM_CHECK_TREE(prev->child[0]);
00835                             prev->child[0]->parent = &prev->child[0];
00836                      }
00837                      if ((prev->child[1] = mm_block->child[1])) {
00838                             ZEND_MM_CHECK_TREE(prev->child[1]);
00839                             prev->child[1]->parent = &prev->child[1];
00840                      }
00841               }
00842        } else {
00843 
00844 #if ZEND_MM_SAFE_UNLINKING
00845               if (UNEXPECTED(prev->next_free_block != mm_block) || UNEXPECTED(next->prev_free_block != mm_block)) {
00846                      zend_mm_panic("zend_mm_heap corrupted");
00847               }
00848 #endif
00849 
00850               prev->next_free_block = next;
00851               next->prev_free_block = prev;
00852 
00853               if (EXPECTED(ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block)))) {
00854                      if (EXPECTED(prev == next)) {
00855                             size_t index = ZEND_MM_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));
00856 
00857                             if (EXPECTED(heap->free_buckets[index*2] == heap->free_buckets[index*2+1])) {
00858                                    heap->free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
00859                             }
00860                      }
00861               } else if (UNEXPECTED(mm_block->parent != NULL)) {
00862                      goto subst_block;
00863               }
00864        }
00865 }
00866 
00867 static inline void zend_mm_init(zend_mm_heap *heap)
00868 {
00869        zend_mm_free_block* p;
00870        int i;
00871 
00872        heap->free_bitmap = 0;
00873        heap->large_free_bitmap = 0;
00874 #if ZEND_MM_CACHE
00875        heap->cached = 0;
00876        memset(heap->cache, 0, sizeof(heap->cache));
00877 #endif
00878 #if ZEND_MM_CACHE_STAT
00879        for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
00880               heap->cache_stat[i].count = 0;
00881        }
00882 #endif
00883        p = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
00884        for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
00885               p->next_free_block = p;
00886               p->prev_free_block = p;
00887               p = (zend_mm_free_block*)((char*)p + sizeof(zend_mm_free_block*) * 2);
00888               heap->large_free_buckets[i] = NULL;
00889        }
00890        heap->rest_buckets[0] = heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(heap);
00891 }
00892 
00893 static void zend_mm_del_segment(zend_mm_heap *heap, zend_mm_segment *segment)
00894 {
00895        zend_mm_segment **p = &heap->segments_list;
00896 
00897        while (*p != segment) {
00898               p = &(*p)->next_segment;
00899        }
00900        *p = segment->next_segment;
00901        heap->real_size -= segment->size;
00902        ZEND_MM_STORAGE_FREE(segment);
00903 }
00904 
00905 #if ZEND_MM_CACHE
00906 static void zend_mm_free_cache(zend_mm_heap *heap)
00907 {
00908        int i;
00909 
00910        for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
00911               if (heap->cache[i]) {
00912                      zend_mm_free_block *mm_block = heap->cache[i];
00913 
00914                      while (mm_block) {
00915                             size_t size = ZEND_MM_BLOCK_SIZE(mm_block);
00916                             zend_mm_free_block *q = mm_block->prev_free_block;
00917                             zend_mm_block *next_block = ZEND_MM_NEXT_BLOCK(mm_block);
00918 
00919                             heap->cached -= size;
00920 
00921                             if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
00922                                    mm_block = (zend_mm_free_block*)ZEND_MM_PREV_BLOCK(mm_block);
00923                                    size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
00924                                    zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) mm_block);
00925                             }
00926                             if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
00927                                    size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
00928                                    zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
00929                             }
00930                             ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);
00931 
00932                             if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
00933                                 ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_NEXT_BLOCK(mm_block))) {
00934                                    zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
00935                             } else {
00936                                    zend_mm_add_to_free_list(heap, (zend_mm_free_block *) mm_block);
00937                             }
00938 
00939                             mm_block = q;
00940                      }
00941                      heap->cache[i] = NULL;
00942 #if ZEND_MM_CACHE_STAT
00943                      heap->cache_stat[i].count = 0;
00944 #endif
00945               }
00946        }
00947 }
00948 #endif
00949 
00950 #if ZEND_MM_HEAP_PROTECTION || ZEND_MM_COOKIES
00951 static void zend_mm_random(unsigned char *buf, size_t size) /* {{{ */
00952 {
00953        size_t i = 0;
00954        unsigned char t;
00955 
00956 #ifdef ZEND_WIN32
00957        HCRYPTPROV   hCryptProv;
00958        int has_context = 0;
00959 
00960        if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
00961               /* Could mean that the key container does not exist, let try 
00962                  again by asking for a new one */
00963               if (GetLastError() == NTE_BAD_KEYSET) {
00964                      if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
00965                             has_context = 1;
00966                      }
00967               }
00968        } else {
00969               has_context = 1;
00970        }
00971        if (has_context) {
00972               do {
00973                      BOOL ret = CryptGenRandom(hCryptProv, size, buf);
00974                      CryptReleaseContext(hCryptProv, 0);
00975                      if (ret) {
00976                             while (i < size && buf[i] != 0) {
00977                                    i++;
00978                             }
00979                             if (i == size) {
00980                                    return;
00981                             }
00982                  }
00983               } while (0);
00984        }
00985 #elif defined(HAVE_DEV_URANDOM)
00986        int fd = open("/dev/urandom", 0);
00987 
00988        if (fd >= 0) {
00989               if (read(fd, buf, size) == size) {
00990                      while (i < size && buf[i] != 0) {
00991                             i++;
00992                      }
00993                      if (i == size) {
00994                             close(fd);
00995                          return;
00996                      }
00997               }
00998               close(fd);
00999        }
01000 #endif
01001        t = (unsigned char)getpid();
01002        while (i < size) {
01003               do {
01004                      buf[i] = ((unsigned char)rand()) ^ t;
01005               } while (buf[i] == 0);
01006               t = buf[i++] << 1;
01007     }
01008 }
01009 /* }}} */
01010 #endif
01011 
01012 /* Notes:
01013  * - This function may alter the block_sizes values to match platform alignment
01014  * - This function does *not* perform sanity checks on the arguments
01015  */
01016 ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params)
01017 {
01018        zend_mm_storage *storage;
01019        zend_mm_heap    *heap;
01020 
01021 #if 0
01022        int i;
01023 
01024        printf("ZEND_MM_ALIGNMENT=%d\n", ZEND_MM_ALIGNMENT);
01025        printf("ZEND_MM_ALIGNMENT_LOG2=%d\n", ZEND_MM_ALIGNMENT_LOG2);
01026        printf("ZEND_MM_MIN_SIZE=%d\n", ZEND_MM_MIN_SIZE);
01027        printf("ZEND_MM_MAX_SMALL_SIZE=%d\n", ZEND_MM_MAX_SMALL_SIZE);
01028        printf("ZEND_MM_ALIGNED_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_HEADER_SIZE);
01029        printf("ZEND_MM_ALIGNED_FREE_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_FREE_HEADER_SIZE);
01030        printf("ZEND_MM_MIN_ALLOC_BLOCK_SIZE=%d\n", ZEND_MM_MIN_ALLOC_BLOCK_SIZE);
01031        printf("ZEND_MM_ALIGNED_MIN_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_MIN_HEADER_SIZE);
01032        printf("ZEND_MM_ALIGNED_SEGMENT_SIZE=%d\n", ZEND_MM_ALIGNED_SEGMENT_SIZE);
01033        for (i = 0; i < ZEND_MM_MAX_SMALL_SIZE; i++) {
01034               printf("%3d%c: %3ld %d %2ld\n", i, (i == ZEND_MM_MIN_SIZE?'*':' '), (long)ZEND_MM_TRUE_SIZE(i), ZEND_MM_SMALL_SIZE(ZEND_MM_TRUE_SIZE(i)), (long)ZEND_MM_BUCKET_INDEX(ZEND_MM_TRUE_SIZE(i)));
01035        }
01036        exit(0);
01037 #endif
01038 
01039 #if ZEND_MM_HEAP_PROTECTION
01040        if (_mem_block_start_magic == 0) {
01041               zend_mm_random((unsigned char*)&_mem_block_start_magic, sizeof(_mem_block_start_magic));
01042        }
01043        if (_mem_block_end_magic == 0) {
01044               zend_mm_random((unsigned char*)&_mem_block_end_magic, sizeof(_mem_block_end_magic));
01045        }
01046 #endif
01047 #if ZEND_MM_COOKIES
01048        if (_zend_mm_cookie == 0) {
01049               zend_mm_random((unsigned char*)&_zend_mm_cookie, sizeof(_zend_mm_cookie));
01050        }
01051 #endif
01052 
01053        if (zend_mm_low_bit(block_size) != zend_mm_high_bit(block_size)) {
01054               fprintf(stderr, "'block_size' must be a power of two\n");
01055 /* See http://support.microsoft.com/kb/190351 */
01056 #ifdef PHP_WIN32
01057               fflush(stderr);
01058 #endif
01059               exit(255);
01060        }
01061        storage = handlers->init(params);
01062        if (!storage) {
01063               fprintf(stderr, "Cannot initialize zend_mm storage [%s]\n", handlers->name);
01064 /* See http://support.microsoft.com/kb/190351 */
01065 #ifdef PHP_WIN32
01066               fflush(stderr);
01067 #endif
01068               exit(255);
01069        }
01070        storage->handlers = handlers;
01071 
01072        heap = malloc(sizeof(struct _zend_mm_heap));
01073        if (heap == NULL) {
01074               fprintf(stderr, "Cannot allocate heap for zend_mm storage [%s]\n", handlers->name);
01075 #ifdef PHP_WIN32
01076               fflush(stderr);
01077 #endif
01078               exit(255);
01079        }
01080        heap->storage = storage;
01081        heap->block_size = block_size;
01082        heap->compact_size = 0;
01083        heap->segments_list = NULL;
01084        zend_mm_init(heap);
01085 # if ZEND_MM_CACHE_STAT
01086        memset(heap->cache_stat, 0, sizeof(heap->cache_stat));
01087 # endif
01088 
01089        heap->use_zend_alloc = 1;
01090        heap->real_size = 0;
01091        heap->overflow = 0;
01092        heap->real_peak = 0;
01093        heap->limit = ZEND_MM_LONG_CONST(1)<<(ZEND_MM_NUM_BUCKETS-2);
01094        heap->size = 0;
01095        heap->peak = 0;
01096        heap->internal = internal;
01097        heap->reserve = NULL;
01098        heap->reserve_size = reserve_size;
01099        if (reserve_size > 0) {
01100               heap->reserve = _zend_mm_alloc_int(heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
01101        }
01102        if (internal) {
01103               int i;
01104               zend_mm_free_block *p, *q, *orig;
01105               zend_mm_heap *mm_heap = _zend_mm_alloc_int(heap, sizeof(zend_mm_heap)  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
01106 
01107               *mm_heap = *heap;
01108 
01109               p = ZEND_MM_SMALL_FREE_BUCKET(mm_heap, 0);
01110               orig = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
01111               for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
01112                      q = p;
01113                      while (q->prev_free_block != orig) {
01114                             q = q->prev_free_block;
01115                      }
01116                      q->prev_free_block = p;
01117                      q = p;
01118                      while (q->next_free_block != orig) {
01119                             q = q->next_free_block;
01120                      }
01121                      q->next_free_block = p;
01122                      p = (zend_mm_free_block*)((char*)p + sizeof(zend_mm_free_block*) * 2);
01123                      orig = (zend_mm_free_block*)((char*)orig + sizeof(zend_mm_free_block*) * 2);
01124                      if (mm_heap->large_free_buckets[i]) {
01125                             mm_heap->large_free_buckets[i]->parent = &mm_heap->large_free_buckets[i];
01126                      }
01127               }
01128               mm_heap->rest_buckets[0] = mm_heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(mm_heap);
01129 
01130               free(heap);
01131               heap = mm_heap;
01132        }
01133        return heap;
01134 }
01135 
01136 ZEND_API zend_mm_heap *zend_mm_startup(void)
01137 {
01138        int i;
01139        size_t seg_size;
01140        char *mem_type = getenv("ZEND_MM_MEM_TYPE");
01141        char *tmp;
01142        const zend_mm_mem_handlers *handlers;
01143        zend_mm_heap *heap;
01144 
01145        if (mem_type == NULL) {
01146               i = 0;
01147        } else {
01148               for (i = 0; mem_handlers[i].name; i++) {
01149                      if (strcmp(mem_handlers[i].name, mem_type) == 0) {
01150                             break;
01151                      }
01152               }
01153               if (!mem_handlers[i].name) {
01154                      fprintf(stderr, "Wrong or unsupported zend_mm storage type '%s'\n", mem_type);
01155                      fprintf(stderr, "  supported types:\n");
01156 /* See http://support.microsoft.com/kb/190351 */
01157 #ifdef PHP_WIN32
01158                      fflush(stderr);
01159 #endif
01160                      for (i = 0; mem_handlers[i].name; i++) {
01161                             fprintf(stderr, "    '%s'\n", mem_handlers[i].name);
01162                      }
01163 /* See http://support.microsoft.com/kb/190351 */
01164 #ifdef PHP_WIN32
01165                      fflush(stderr);
01166 #endif
01167                      exit(255);
01168               }
01169        }
01170        handlers = &mem_handlers[i];
01171 
01172        tmp = getenv("ZEND_MM_SEG_SIZE");
01173        if (tmp) {
01174               seg_size = zend_atoi(tmp, 0);
01175               if (zend_mm_low_bit(seg_size) != zend_mm_high_bit(seg_size)) {
01176                      fprintf(stderr, "ZEND_MM_SEG_SIZE must be a power of two\n");
01177 /* See http://support.microsoft.com/kb/190351 */
01178 #ifdef PHP_WIN32
01179                      fflush(stderr);
01180 #endif
01181                      exit(255);
01182               } else if (seg_size < ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE) {
01183                      fprintf(stderr, "ZEND_MM_SEG_SIZE is too small\n");
01184 /* See http://support.microsoft.com/kb/190351 */
01185 #ifdef PHP_WIN32
01186                      fflush(stderr);
01187 #endif
01188                      exit(255);
01189               }
01190        } else {
01191               seg_size = ZEND_MM_SEG_SIZE;
01192        }
01193 
01194        heap = zend_mm_startup_ex(handlers, seg_size, ZEND_MM_RESERVE_SIZE, 0, NULL);
01195        if (heap) {
01196               tmp = getenv("ZEND_MM_COMPACT");
01197               if (tmp) {
01198                      heap->compact_size = zend_atoi(tmp, 0);
01199               } else {
01200                      heap->compact_size = 2 * 1024 * 1024;
01201               }
01202        }
01203        return heap;
01204 }
01205 
01206 #if ZEND_DEBUG
01207 static long zend_mm_find_leaks(zend_mm_segment *segment, zend_mm_block *b)
01208 {
01209        long leaks = 0;
01210        zend_mm_block *p, *q;
01211 
01212        p = ZEND_MM_NEXT_BLOCK(b);
01213        while (1) {
01214               if (ZEND_MM_IS_GUARD_BLOCK(p)) {
01215                      ZEND_MM_CHECK_MAGIC(p, MEM_BLOCK_GUARD);
01216                      segment = segment->next_segment;
01217                      if (!segment) {
01218                             break;
01219                      }
01220                      p = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
01221                      continue;
01222               }
01223               q = ZEND_MM_NEXT_BLOCK(p);
01224               if (q <= p ||
01225                   (char*)q > (char*)segment + segment->size ||
01226                   p->info._size != q->info._prev) {
01227                   zend_mm_panic("zend_mm_heap corrupted");
01228               }
01229               if (!ZEND_MM_IS_FREE_BLOCK(p)) {
01230                      if (p->magic == MEM_BLOCK_VALID) {
01231                             if (p->debug.filename==b->debug.filename && p->debug.lineno==b->debug.lineno) {
01232                                    ZEND_MM_SET_MAGIC(p, MEM_BLOCK_LEAK);
01233                                    leaks++;
01234                             }
01235 #if ZEND_MM_CACHE
01236                      } else if (p->magic == MEM_BLOCK_CACHED) {
01237                             /* skip it */
01238 #endif
01239                      } else if (p->magic != MEM_BLOCK_LEAK) {
01240                          zend_mm_panic("zend_mm_heap corrupted");
01241                      }
01242               }
01243               p = q;
01244        }
01245        return leaks;
01246 }
01247 
01248 static void zend_mm_check_leaks(zend_mm_heap *heap TSRMLS_DC)
01249 {
01250        zend_mm_segment *segment = heap->segments_list;
01251        zend_mm_block *p, *q;
01252        zend_uint total = 0;
01253 
01254        if (!segment) {
01255               return;
01256        }
01257        p = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
01258        while (1) {
01259               q = ZEND_MM_NEXT_BLOCK(p);
01260               if (q <= p ||
01261                   (char*)q > (char*)segment + segment->size ||
01262                   p->info._size != q->info._prev) {
01263                      zend_mm_panic("zend_mm_heap corrupted");
01264               }
01265               if (!ZEND_MM_IS_FREE_BLOCK(p)) {
01266                      if (p->magic == MEM_BLOCK_VALID) {
01267                             long repeated;
01268                             zend_leak_info leak;
01269 
01270                             ZEND_MM_SET_MAGIC(p, MEM_BLOCK_LEAK);
01271 
01272                             leak.addr = ZEND_MM_DATA_OF(p);
01273                             leak.size = p->debug.size;
01274                             leak.filename = p->debug.filename;
01275                             leak.lineno = p->debug.lineno;
01276                             leak.orig_filename = p->debug.orig_filename;
01277                             leak.orig_lineno = p->debug.orig_lineno;
01278 
01279                             zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
01280                             zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak TSRMLS_CC);
01281                             repeated = zend_mm_find_leaks(segment, p);
01282                             total += 1 + repeated;
01283                             if (repeated) {
01284                                    zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(zend_uintptr_t)repeated TSRMLS_CC);
01285                             }
01286 #if ZEND_MM_CACHE
01287                      } else if (p->magic == MEM_BLOCK_CACHED) {
01288                             /* skip it */
01289 #endif
01290                      } else if (p->magic != MEM_BLOCK_LEAK) {
01291                             zend_mm_panic("zend_mm_heap corrupted");
01292                      }
01293               }
01294               if (ZEND_MM_IS_GUARD_BLOCK(q)) {
01295                      segment = segment->next_segment;
01296                      if (!segment) {
01297                             break;
01298                      }
01299                      q = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
01300               }
01301               p = q;
01302        }
01303        if (total) {
01304               zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &total TSRMLS_CC);
01305        }
01306 }
01307 
01308 static int zend_mm_check_ptr(zend_mm_heap *heap, void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
01309 {
01310        zend_mm_block *p;
01311        int no_cache_notice = 0;
01312        int had_problems = 0;
01313        int valid_beginning = 1;
01314 
01315        if (silent==2) {
01316               silent = 1;
01317               no_cache_notice = 1;
01318        } else if (silent==3) {
01319               silent = 0;
01320               no_cache_notice = 1;
01321        }
01322        if (!silent) {
01323               TSRMLS_FETCH();
01324               
01325               zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL TSRMLS_CC);
01326               zend_debug_alloc_output("---------------------------------------\n");
01327               zend_debug_alloc_output("%s(%d) : Block "PTR_FMT" status:\n" ZEND_FILE_LINE_RELAY_CC, ptr);
01328               if (__zend_orig_filename) {
01329                      zend_debug_alloc_output("%s(%d) : Actual location (location was relayed)\n" ZEND_FILE_LINE_ORIG_RELAY_CC);
01330               }
01331               if (!ptr) {
01332                      zend_debug_alloc_output("NULL\n");
01333                      zend_debug_alloc_output("---------------------------------------\n");
01334                      return 0;
01335               }
01336        }
01337 
01338        if (!ptr) {
01339               if (silent) {
01340                      return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01341               }
01342        }
01343 
01344        p = ZEND_MM_HEADER_OF(ptr);
01345 
01346 #ifdef ZTS
01347        if (ZEND_MM_BAD_THREAD_ID(p)) {
01348               if (!silent) {
01349                      zend_debug_alloc_output("Invalid pointer: ((thread_id=0x%0.8X) != (expected=0x%0.8X))\n", (long)p->thread_id, (long)tsrm_thread_id());
01350                      had_problems = 1;
01351               } else {
01352                      return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01353               }
01354        }
01355 #endif
01356 
01357        if (p->info._size != ZEND_MM_NEXT_BLOCK(p)->info._prev) {
01358               if (!silent) {
01359                      zend_debug_alloc_output("Invalid pointer: ((size="PTR_FMT") != (next.prev="PTR_FMT"))\n", p->info._size, ZEND_MM_NEXT_BLOCK(p)->info._prev);
01360                      had_problems = 1;
01361               } else {
01362                      return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01363               }
01364        }
01365        if (p->info._prev != ZEND_MM_GUARD_BLOCK &&
01366            ZEND_MM_PREV_BLOCK(p)->info._size != p->info._prev) {
01367               if (!silent) {
01368                      zend_debug_alloc_output("Invalid pointer: ((prev="PTR_FMT") != (prev.size="PTR_FMT"))\n", p->info._prev, ZEND_MM_PREV_BLOCK(p)->info._size);
01369                      had_problems = 1;
01370               } else {
01371                      return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01372               }
01373        }
01374 
01375        if (had_problems) {
01376               zend_debug_alloc_output("---------------------------------------\n");
01377               return 0;
01378        }
01379 
01380        if (!silent) {
01381               zend_debug_alloc_output("%10s\t","Beginning:  ");
01382        }
01383 
01384        if (!ZEND_MM_IS_USED_BLOCK(p)) {
01385               if (!silent) {
01386                      if (p->magic != MEM_BLOCK_FREED) {
01387                             zend_debug_alloc_output("Freed (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_FREED);
01388                      } else {
01389                             zend_debug_alloc_output("Freed\n");
01390                      }
01391                      had_problems = 1;
01392               } else {
01393                      return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01394               }
01395        } else if (ZEND_MM_IS_GUARD_BLOCK(p)) {
01396               if (!silent) {
01397                      if (p->magic != MEM_BLOCK_FREED) {
01398                             zend_debug_alloc_output("Guard (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_FREED);
01399                      } else {
01400                             zend_debug_alloc_output("Guard\n");
01401                      }
01402                      had_problems = 1;
01403               } else {
01404                      return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01405               }
01406        } else {
01407               switch (p->magic) {
01408                      case MEM_BLOCK_VALID:
01409                      case MEM_BLOCK_LEAK:
01410                             if (!silent) {
01411                                    zend_debug_alloc_output("OK (allocated on %s:%d, %d bytes)\n", p->debug.filename, p->debug.lineno, (int)p->debug.size);
01412                             }
01413                             break; /* ok */
01414                      case MEM_BLOCK_CACHED:
01415                             if (!no_cache_notice) {
01416                                    if (!silent) {
01417                                           zend_debug_alloc_output("Cached\n");
01418                                           had_problems = 1;
01419                                    } else {
01420                                           return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01421                                    }
01422                             }
01423                      case MEM_BLOCK_FREED:
01424                             if (!silent) {
01425                                    zend_debug_alloc_output("Freed (invalid)\n");
01426                                    had_problems = 1;
01427                             } else {
01428                                    return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01429                             }
01430                             break;
01431                      case MEM_BLOCK_GUARD:
01432                             if (!silent) {
01433                                    zend_debug_alloc_output("Guard (invalid)\n");
01434                                    had_problems = 1;
01435                             } else {
01436                                    return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01437                             }
01438                             break;
01439                      default:
01440                             if (!silent) {
01441                                    zend_debug_alloc_output("Unknown (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic, MEM_BLOCK_VALID);
01442                                    had_problems = 1;
01443                                    valid_beginning = 0;
01444                             } else {
01445                                    return zend_mm_check_ptr(heap, ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01446                             }
01447                             break;
01448               }
01449        }
01450 
01451 #if ZEND_MM_HEAP_PROTECTION
01452        if (!valid_beginning) {
01453               if (!silent) {
01454                      zend_debug_alloc_output("%10s\t", "Start:");
01455                      zend_debug_alloc_output("Unknown\n");
01456                      zend_debug_alloc_output("%10s\t", "End:");
01457                      zend_debug_alloc_output("Unknown\n");
01458               }
01459        } else {
01460               char *end_magic = ZEND_MM_END_MAGIC_PTR(p);
01461 
01462               if (p->debug.start_magic == _mem_block_start_magic) {
01463                      if (!silent) {
01464                             zend_debug_alloc_output("%10s\t", "Start:");
01465                             zend_debug_alloc_output("OK\n");
01466                      }
01467               } else {
01468                      char *overflow_ptr, *magic_ptr=(char *) &_mem_block_start_magic;
01469                      int overflows=0;
01470                      int i;
01471 
01472                      if (silent) {
01473                             return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01474                      }
01475                      had_problems = 1;
01476                      overflow_ptr = (char *) &p->debug.start_magic;
01477                      i = END_MAGIC_SIZE;
01478                      while (--i >= 0) {
01479                             if (overflow_ptr[i]!=magic_ptr[i]) {
01480                                    overflows++;
01481                             }
01482                      }
01483                      zend_debug_alloc_output("%10s\t", "Start:");
01484                      zend_debug_alloc_output("Overflown (magic=0x%0.8X instead of 0x%0.8X)\n", p->debug.start_magic, _mem_block_start_magic);
01485                      zend_debug_alloc_output("%10s\t","");
01486                      if (overflows >= END_MAGIC_SIZE) {
01487                             zend_debug_alloc_output("At least %d bytes overflown\n", END_MAGIC_SIZE);
01488                      } else {
01489                             zend_debug_alloc_output("%d byte(s) overflown\n", overflows);
01490                      }
01491               }
01492               if (memcmp(end_magic, &_mem_block_end_magic, END_MAGIC_SIZE)==0) {
01493                      if (!silent) {
01494                             zend_debug_alloc_output("%10s\t", "End:");
01495                             zend_debug_alloc_output("OK\n");
01496                      }
01497               } else {
01498                      char *overflow_ptr, *magic_ptr=(char *) &_mem_block_end_magic;
01499                      int overflows=0;
01500                      int i;
01501 
01502                      if (silent) {
01503                             return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
01504                      }
01505                      had_problems = 1;
01506                      overflow_ptr = (char *) end_magic;
01507 
01508                      for (i=0; i < END_MAGIC_SIZE; i++) {
01509                             if (overflow_ptr[i]!=magic_ptr[i]) {
01510                                    overflows++;
01511                             }
01512                      }
01513 
01514                      zend_debug_alloc_output("%10s\t", "End:");
01515                      zend_debug_alloc_output("Overflown (magic=0x%0.8X instead of 0x%0.8X)\n", *end_magic, _mem_block_end_magic);
01516                      zend_debug_alloc_output("%10s\t","");
01517                      if (overflows >= END_MAGIC_SIZE) {
01518                             zend_debug_alloc_output("At least %d bytes overflown\n", END_MAGIC_SIZE);
01519                      } else {
01520                             zend_debug_alloc_output("%d byte(s) overflown\n", overflows);
01521                      }
01522               }
01523        }
01524 #endif
01525 
01526        if (!silent) {
01527               zend_debug_alloc_output("---------------------------------------\n");
01528        }
01529        return ((!had_problems) ? 1 : 0);
01530 }
01531 
01532 static int zend_mm_check_heap(zend_mm_heap *heap, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
01533 {
01534        zend_mm_segment *segment = heap->segments_list;
01535        zend_mm_block *p, *q;
01536        int errors = 0;
01537 
01538        if (!segment) {
01539               return 0;
01540        }
01541        p = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
01542        while (1) {
01543               q = ZEND_MM_NEXT_BLOCK(p);
01544               if (q <= p ||
01545                   (char*)q > (char*)segment + segment->size ||
01546                   p->info._size != q->info._prev) {
01547                      zend_mm_panic("zend_mm_heap corrupted");
01548               }
01549               if (!ZEND_MM_IS_FREE_BLOCK(p)) {
01550                      if (p->magic == MEM_BLOCK_VALID || p->magic == MEM_BLOCK_LEAK) {
01551                             if (!zend_mm_check_ptr(heap, ZEND_MM_DATA_OF(p), (silent?2:3) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)) {
01552                                    errors++;
01553                             }
01554 #if ZEND_MM_CACHE
01555                      } else if (p->magic == MEM_BLOCK_CACHED) {
01556                             /* skip it */
01557 #endif
01558                      } else if (p->magic != MEM_BLOCK_LEAK) {
01559                             zend_mm_panic("zend_mm_heap corrupted");
01560                      }
01561               }
01562               if (ZEND_MM_IS_GUARD_BLOCK(q)) {
01563                      segment = segment->next_segment;
01564                      if (!segment) {
01565                             return errors;
01566                      }
01567                      q = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
01568               }
01569               p = q;
01570        }
01571 }
01572 #endif
01573 
01574 ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
01575 {
01576        zend_mm_storage *storage;
01577        zend_mm_segment *segment;
01578        zend_mm_segment *prev;
01579        int internal;
01580 
01581        if (heap->reserve) {
01582 #if ZEND_DEBUG
01583               if (!silent) {
01584                      _zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
01585               }
01586 #endif
01587               heap->reserve = NULL;
01588        }
01589 
01590 #if ZEND_MM_CACHE_STAT
01591        if (full_shutdown) {
01592               FILE *f;
01593 
01594               f = fopen("zend_mm.log", "w");
01595               if (f) {
01596                      int i,j;
01597                      size_t size, true_size, min_size, max_size;
01598                      int hit = 0, miss = 0;
01599 
01600                      fprintf(f, "\nidx min_size max_size true_size  max_len     hits   misses\n");
01601                      size = 0;
01602                      while (1) {
01603                             true_size = ZEND_MM_TRUE_SIZE(size);
01604                             if (ZEND_MM_SMALL_SIZE(true_size)) {
01605                                    min_size = size;
01606                                    i = ZEND_MM_BUCKET_INDEX(true_size);
01607                                    size++;
01608                                    while (1) {
01609                                           true_size = ZEND_MM_TRUE_SIZE(size);
01610                                           if (ZEND_MM_SMALL_SIZE(true_size)) {
01611                                                  j = ZEND_MM_BUCKET_INDEX(true_size);
01612                                                  if (j > i) {
01613                                                         max_size = size-1;
01614                                                         break;
01615                                                  }
01616                                           } else {
01617                                                  max_size = size-1;
01618                                                  break;
01619                                           }
01620                                           size++;
01621                                    }
01622                                    hit += heap->cache_stat[i].hit;
01623                                    miss += heap->cache_stat[i].miss;
01624                                    fprintf(f, "%2d %8d %8d %9d %8d %8d %8d\n", i, (int)min_size, (int)max_size, ZEND_MM_TRUE_SIZE(max_size), heap->cache_stat[i].max_count, heap->cache_stat[i].hit, heap->cache_stat[i].miss);
01625                             } else {
01626                                    break;
01627                             }
01628                      }
01629                      fprintf(f, "                                        %8d %8d\n", hit, miss);
01630                      fprintf(f, "                                        %8d %8d\n", heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit, heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss);
01631                      fclose(f);
01632               }
01633        }
01634 #endif
01635 
01636 #if ZEND_DEBUG
01637        if (!silent) {
01638               zend_mm_check_leaks(heap TSRMLS_CC);
01639        }
01640 #endif
01641 
01642        internal = heap->internal;
01643        storage = heap->storage;
01644        segment = heap->segments_list;
01645        while (segment) {
01646               prev = segment;
01647               segment = segment->next_segment;
01648               ZEND_MM_STORAGE_FREE(prev);
01649        }
01650        if (full_shutdown) {
01651               storage->handlers->dtor(storage);
01652               if (!internal) {
01653                      free(heap);
01654               }
01655        } else {
01656               if (heap->compact_size &&
01657                   heap->real_peak > heap->compact_size) {
01658                      storage->handlers->compact(storage);
01659               }
01660               heap->segments_list = NULL;
01661               zend_mm_init(heap);
01662               heap->real_size = 0;
01663               heap->real_peak = 0;
01664               heap->size = 0;
01665               heap->peak = 0;
01666               if (heap->reserve_size) {
01667                      heap->reserve = _zend_mm_alloc_int(heap, heap->reserve_size  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
01668               }
01669               heap->overflow = 0;
01670        }
01671 }
01672 
01673 static void zend_mm_safe_error(zend_mm_heap *heap,
01674        const char *format,
01675        size_t limit,
01676 #if ZEND_DEBUG
01677        const char *filename,
01678        uint lineno,
01679 #endif
01680        size_t size)
01681 {
01682        if (heap->reserve) {
01683               _zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
01684               heap->reserve = NULL;
01685        }
01686        if (heap->overflow == 0) {
01687               char *error_filename;
01688               uint error_lineno;
01689               TSRMLS_FETCH();
01690               if (zend_is_compiling(TSRMLS_C)) {
01691                      error_filename = zend_get_compiled_filename(TSRMLS_C);
01692                      error_lineno = zend_get_compiled_lineno(TSRMLS_C);
01693               } else if (EG(in_execution)) {
01694                      error_filename = EG(active_op_array)?EG(active_op_array)->filename:NULL;
01695                      error_lineno = EG(opline_ptr)?(*EG(opline_ptr))->lineno:0;
01696               } else {
01697                      error_filename = NULL;
01698                      error_lineno = 0;
01699               }
01700               if (!error_filename) {
01701                      error_filename = "Unknown";
01702               }
01703               heap->overflow = 1;
01704               zend_try {
01705                      zend_error_noreturn(E_ERROR,
01706                             format,
01707                             limit,
01708 #if ZEND_DEBUG
01709                             filename,
01710                             lineno,
01711 #endif
01712                             size);
01713               } zend_catch {
01714                      if (heap->overflow == 2) {
01715                             fprintf(stderr, "\nFatal error: ");
01716                             fprintf(stderr,
01717                                    format,
01718                                    limit,
01719 #if ZEND_DEBUG
01720                                    filename,
01721                                    lineno,
01722 #endif
01723                                    size);
01724                             fprintf(stderr, " in %s on line %d\n", error_filename, error_lineno);
01725                      }
01726 /* See http://support.microsoft.com/kb/190351 */
01727 #ifdef PHP_WIN32
01728                      fflush(stderr);
01729 #endif
01730               } zend_end_try();
01731        } else {
01732               heap->overflow = 2;
01733        }
01734        zend_bailout();
01735 }
01736 
01737 static zend_mm_free_block *zend_mm_search_large_block(zend_mm_heap *heap, size_t true_size)
01738 {
01739        zend_mm_free_block *best_fit;
01740        size_t index = ZEND_MM_LARGE_BUCKET_INDEX(true_size);
01741        size_t bitmap = heap->large_free_bitmap >> index;
01742        zend_mm_free_block *p;
01743 
01744        if (bitmap == 0) {
01745               return NULL;
01746        }
01747 
01748        if (UNEXPECTED((bitmap & 1) != 0)) {
01749               /* Search for best "large" free block */
01750               zend_mm_free_block *rst = NULL;
01751               size_t m;
01752               size_t best_size = -1;
01753 
01754               best_fit = NULL;
01755               p = heap->large_free_buckets[index];
01756               for (m = true_size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
01757                      if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
01758                             return p->next_free_block;
01759                      } else if (ZEND_MM_FREE_BLOCK_SIZE(p) >= true_size &&
01760                                 ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
01761                             best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
01762                             best_fit = p;
01763                      }
01764                      if ((m & (ZEND_MM_LONG_CONST(1) << (ZEND_MM_NUM_BUCKETS-1))) == 0) {
01765                             if (p->child[1]) {
01766                                    rst = p->child[1];
01767                             }
01768                             if (p->child[0]) {
01769                                    p = p->child[0];
01770                             } else {
01771                                    break;
01772                             }
01773                      } else if (p->child[1]) {
01774                             p = p->child[1];
01775                      } else {
01776                             break;
01777                      }
01778               }
01779 
01780               for (p = rst; p; p = p->child[p->child[0] != NULL]) {
01781                      if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
01782                             return p->next_free_block;
01783                      } else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
01784                                 ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
01785                             best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
01786                             best_fit = p;
01787                      }
01788               }
01789 
01790               if (best_fit) {
01791                      return best_fit->next_free_block;
01792               }
01793               bitmap = bitmap >> 1;
01794               if (!bitmap) {
01795                      return NULL;
01796               }
01797               index++;
01798        }
01799 
01800        /* Search for smallest "large" free block */
01801        best_fit = p = heap->large_free_buckets[index + zend_mm_low_bit(bitmap)];
01802        while ((p = p->child[p->child[0] != NULL])) {
01803               if (ZEND_MM_FREE_BLOCK_SIZE(p) < ZEND_MM_FREE_BLOCK_SIZE(best_fit)) {
01804                      best_fit = p;
01805               }
01806        }
01807        return best_fit->next_free_block;
01808 }
01809 
01810 static void *_zend_mm_alloc_int(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
01811 {
01812        zend_mm_free_block *best_fit;
01813        size_t true_size = ZEND_MM_TRUE_SIZE(size);
01814        size_t block_size;
01815        size_t remaining_size;
01816        size_t segment_size;
01817        zend_mm_segment *segment;
01818        int keep_rest = 0;
01819 
01820        if (EXPECTED(ZEND_MM_SMALL_SIZE(true_size))) {
01821               size_t index = ZEND_MM_BUCKET_INDEX(true_size);
01822               size_t bitmap;
01823 
01824               if (UNEXPECTED(true_size < size)) {
01825                      goto out_of_memory;
01826               }
01827 #if ZEND_MM_CACHE
01828               if (EXPECTED(heap->cache[index] != NULL)) {
01829                      /* Get block from cache */
01830 #if ZEND_MM_CACHE_STAT
01831                      heap->cache_stat[index].count--;
01832                      heap->cache_stat[index].hit++;
01833 #endif
01834                      best_fit = heap->cache[index];
01835                      heap->cache[index] = best_fit->prev_free_block;
01836                      heap->cached -= true_size;
01837                      ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
01838                      ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
01839                      return ZEND_MM_DATA_OF(best_fit);
01840               }
01841 #if ZEND_MM_CACHE_STAT
01842               heap->cache_stat[index].miss++;
01843 #endif
01844 #endif
01845 
01846               bitmap = heap->free_bitmap >> index;
01847               if (bitmap) {
01848                      /* Found some "small" free block that can be used */
01849                      index += zend_mm_low_bit(bitmap);
01850                      best_fit = heap->free_buckets[index*2];
01851 #if ZEND_MM_CACHE_STAT
01852                      heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit++;
01853 #endif
01854                      goto zend_mm_finished_searching_for_block;
01855               }
01856        }
01857 
01858 #if ZEND_MM_CACHE_STAT
01859        heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss++;
01860 #endif
01861 
01862        best_fit = zend_mm_search_large_block(heap, true_size);
01863 
01864        if (!best_fit && heap->real_size >= heap->limit - heap->block_size) {
01865               zend_mm_free_block *p = heap->rest_buckets[0];
01866               size_t best_size = -1;
01867 
01868               while (p != ZEND_MM_REST_BUCKET(heap)) {
01869                      if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
01870                             best_fit = p;
01871                             goto zend_mm_finished_searching_for_block;
01872                      } else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
01873                                 ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
01874                             best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
01875                             best_fit = p;
01876                      }
01877                      p = p->prev_free_block;
01878               }
01879        }
01880 
01881        if (!best_fit) {
01882               if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
01883                      /* Make sure we add a memory block which is big enough,
01884                         segment must have header "size" and trailer "guard" block */
01885                      segment_size = true_size + ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE;
01886                      segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
01887                      keep_rest = 1;
01888               } else {
01889                      segment_size = heap->block_size;
01890               }
01891 
01892               HANDLE_BLOCK_INTERRUPTIONS();
01893 
01894               if (segment_size < true_size ||
01895                   heap->real_size + segment_size > heap->limit) {
01896                      /* Memory limit overflow */
01897 #if ZEND_MM_CACHE
01898                      zend_mm_free_cache(heap);
01899 #endif
01900                      HANDLE_UNBLOCK_INTERRUPTIONS();
01901 #if ZEND_DEBUG
01902                      zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %lu bytes)", heap->limit, __zend_filename, __zend_lineno, size);
01903 #else
01904                      zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %lu bytes)", heap->limit, size);
01905 #endif
01906               }
01907 
01908               segment = (zend_mm_segment *) ZEND_MM_STORAGE_ALLOC(segment_size);
01909 
01910               if (!segment) {
01911                      /* Storage manager cannot allocate memory */
01912 #if ZEND_MM_CACHE
01913                      zend_mm_free_cache(heap);
01914 #endif
01915                      HANDLE_UNBLOCK_INTERRUPTIONS();
01916 out_of_memory:
01917 #if ZEND_DEBUG
01918                      zend_mm_safe_error(heap, "Out of memory (allocated %ld) at %s:%d (tried to allocate %lu bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
01919 #else
01920                      zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %lu bytes)", heap->real_size, size);
01921 #endif
01922                      return NULL;
01923               }
01924 
01925               heap->real_size += segment_size;
01926               if (heap->real_size > heap->real_peak) {
01927                      heap->real_peak = heap->real_size;
01928               }
01929 
01930               segment->size = segment_size;
01931               segment->next_segment = heap->segments_list;
01932               heap->segments_list = segment;
01933 
01934               best_fit = (zend_mm_free_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
01935               ZEND_MM_MARK_FIRST_BLOCK(best_fit);
01936 
01937               block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;
01938 
01939               ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(best_fit, block_size));
01940 
01941        } else {
01942 zend_mm_finished_searching_for_block:
01943               /* remove from free list */
01944               HANDLE_BLOCK_INTERRUPTIONS();
01945               ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_FREED);
01946               ZEND_MM_CHECK_COOKIE(best_fit);
01947               ZEND_MM_CHECK_BLOCK_LINKAGE(best_fit);
01948               zend_mm_remove_from_free_list(heap, best_fit);
01949 
01950               block_size = ZEND_MM_FREE_BLOCK_SIZE(best_fit);
01951        }
01952 
01953        remaining_size = block_size - true_size;
01954 
01955        if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
01956               true_size = block_size;
01957               ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
01958        } else {
01959               zend_mm_free_block *new_free_block;
01960 
01961               /* prepare new free block */
01962               ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
01963               new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(best_fit, true_size);
01964               ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
01965 
01966               /* add the new free block to the free list */
01967               if (EXPECTED(!keep_rest)) {
01968                      zend_mm_add_to_free_list(heap, new_free_block);
01969               } else {
01970                      zend_mm_add_to_rest_list(heap, new_free_block);
01971               }
01972        }
01973 
01974        ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 1);
01975 
01976        heap->size += true_size;
01977        if (heap->peak < heap->size) {
01978               heap->peak = heap->size;
01979        }
01980 
01981        HANDLE_UNBLOCK_INTERRUPTIONS();
01982 
01983        return ZEND_MM_DATA_OF(best_fit);
01984 }
01985 
01986 
01987 static void _zend_mm_free_int(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
01988 {
01989        zend_mm_block *mm_block;
01990        zend_mm_block *next_block;
01991        size_t size;
01992 
01993        if (!ZEND_MM_VALID_PTR(p)) {
01994               return;
01995        }
01996 
01997        mm_block = ZEND_MM_HEADER_OF(p);
01998        size = ZEND_MM_BLOCK_SIZE(mm_block);
01999        ZEND_MM_CHECK_PROTECTION(mm_block);
02000 
02001 #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
02002        memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->debug.size);
02003 #endif
02004 
02005 #if ZEND_MM_CACHE
02006        if (EXPECTED(ZEND_MM_SMALL_SIZE(size)) && EXPECTED(heap->cached < ZEND_MM_CACHE_SIZE)) {
02007               size_t index = ZEND_MM_BUCKET_INDEX(size);
02008               zend_mm_free_block **cache = &heap->cache[index];
02009 
02010               ((zend_mm_free_block*)mm_block)->prev_free_block = *cache;
02011               *cache = (zend_mm_free_block*)mm_block;
02012               heap->cached += size;
02013               ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
02014 #if ZEND_MM_CACHE_STAT
02015               if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
02016                      heap->cache_stat[index].max_count = heap->cache_stat[index].count;
02017               }
02018 #endif
02019               return;
02020        }
02021 #endif
02022 
02023        HANDLE_BLOCK_INTERRUPTIONS();
02024 
02025        heap->size -= size;
02026 
02027        next_block = ZEND_MM_BLOCK_AT(mm_block, size);
02028        if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
02029               zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
02030               size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
02031        }
02032        if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
02033               mm_block = ZEND_MM_PREV_BLOCK(mm_block);
02034               zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) mm_block);
02035               size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
02036        }
02037        if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
02038            ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(mm_block, size))) {
02039               zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
02040        } else {
02041               ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);
02042               zend_mm_add_to_free_list(heap, (zend_mm_free_block *) mm_block);
02043        }
02044        HANDLE_UNBLOCK_INTERRUPTIONS();
02045 }
02046 
02047 static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02048 {
02049        zend_mm_block *mm_block = ZEND_MM_HEADER_OF(p);
02050        zend_mm_block *next_block;
02051        size_t true_size;
02052        size_t orig_size;
02053        void *ptr;
02054 
02055        if (UNEXPECTED(!p) || !ZEND_MM_VALID_PTR(p)) {
02056               return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02057        }
02058        mm_block = ZEND_MM_HEADER_OF(p);
02059        true_size = ZEND_MM_TRUE_SIZE(size);
02060        orig_size = ZEND_MM_BLOCK_SIZE(mm_block);
02061        ZEND_MM_CHECK_PROTECTION(mm_block);
02062 
02063        if (UNEXPECTED(true_size < size)) {
02064               goto out_of_memory;
02065        }
02066 
02067        if (true_size <= orig_size) {
02068               size_t remaining_size = orig_size - true_size;
02069 
02070               if (remaining_size >= ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
02071                      zend_mm_free_block *new_free_block;
02072 
02073                      HANDLE_BLOCK_INTERRUPTIONS();
02074                      next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);
02075                      if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
02076                             remaining_size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
02077                             zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
02078                      }
02079 
02080                      /* prepare new free block */
02081                      ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
02082                      new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(mm_block, true_size);
02083 
02084                      ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
02085 
02086                      /* add the new free block to the free list */
02087                      zend_mm_add_to_free_list(heap, new_free_block);
02088                      heap->size += (true_size - orig_size);
02089                      HANDLE_UNBLOCK_INTERRUPTIONS();
02090               }
02091               ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
02092               return p;
02093        }
02094 
02095 #if ZEND_MM_CACHE
02096        if (ZEND_MM_SMALL_SIZE(true_size)) {
02097               size_t index = ZEND_MM_BUCKET_INDEX(true_size);
02098               
02099               if (heap->cache[index] != NULL) {
02100                      zend_mm_free_block *best_fit;
02101                      zend_mm_free_block **cache;
02102 
02103 #if ZEND_MM_CACHE_STAT
02104                      heap->cache_stat[index].count--;
02105                      heap->cache_stat[index].hit++;
02106 #endif
02107                      best_fit = heap->cache[index];
02108                      heap->cache[index] = best_fit->prev_free_block;
02109                      ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
02110                      ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
02111        
02112                      ptr = ZEND_MM_DATA_OF(best_fit);
02113 
02114 #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
02115                      memcpy(ptr, p, mm_block->debug.size);
02116 #else
02117                      memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
02118 #endif
02119 
02120                      heap->cached -= true_size - orig_size;
02121 
02122                      index = ZEND_MM_BUCKET_INDEX(orig_size);
02123                      cache = &heap->cache[index];
02124 
02125                      ((zend_mm_free_block*)mm_block)->prev_free_block = *cache;
02126                      *cache = (zend_mm_free_block*)mm_block;
02127                      ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
02128 #if ZEND_MM_CACHE_STAT
02129                      if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
02130                             heap->cache_stat[index].max_count = heap->cache_stat[index].count;
02131                      }
02132 #endif
02133 
02134                      return ptr;
02135               }
02136        }
02137 #endif
02138 
02139        next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);
02140 
02141        if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
02142               ZEND_MM_CHECK_COOKIE(next_block);
02143               ZEND_MM_CHECK_BLOCK_LINKAGE(next_block);
02144               if (orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block) >= true_size) {
02145                      size_t block_size = orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block);
02146                      size_t remaining_size = block_size - true_size;
02147 
02148                      HANDLE_BLOCK_INTERRUPTIONS();
02149                      zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
02150 
02151                      if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
02152                             true_size = block_size;
02153                             ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
02154                      } else {
02155                             zend_mm_free_block *new_free_block;
02156 
02157                             /* prepare new free block */
02158                             ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
02159                             new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(mm_block, true_size);
02160                             ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
02161 
02162                             /* add the new free block to the free list */
02163                             if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
02164                                 ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(new_free_block, remaining_size))) {
02165                                    zend_mm_add_to_rest_list(heap, new_free_block);
02166                             } else {
02167                                    zend_mm_add_to_free_list(heap, new_free_block);
02168                             }
02169                      }
02170                      ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
02171                      heap->size = heap->size + true_size - orig_size;
02172                      if (heap->peak < heap->size) {
02173                             heap->peak = heap->size;
02174                      }
02175                      HANDLE_UNBLOCK_INTERRUPTIONS();
02176                      return p;
02177               } else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
02178                                ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(next_block, ZEND_MM_FREE_BLOCK_SIZE(next_block)))) {
02179                      HANDLE_BLOCK_INTERRUPTIONS();
02180                      zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
02181                      goto realloc_segment;
02182               }
02183        } else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) && ZEND_MM_IS_GUARD_BLOCK(next_block)) {
02184               zend_mm_segment *segment;
02185               zend_mm_segment *segment_copy;
02186               size_t segment_size;
02187               size_t block_size;
02188               size_t remaining_size;
02189 
02190               HANDLE_BLOCK_INTERRUPTIONS();
02191 realloc_segment:
02192               /* segment size, size of block and size of guard block */
02193               if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
02194                      segment_size = true_size+ZEND_MM_ALIGNED_SEGMENT_SIZE+ZEND_MM_ALIGNED_HEADER_SIZE;
02195                      segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
02196               } else {
02197                      segment_size = heap->block_size;
02198               }
02199 
02200               segment_copy = (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE);
02201               if (segment_size < true_size ||
02202                   heap->real_size + segment_size - segment_copy->size > heap->limit) {
02203                      if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
02204                             zend_mm_add_to_free_list(heap, (zend_mm_free_block *) next_block);
02205                      }
02206 #if ZEND_MM_CACHE
02207                      zend_mm_free_cache(heap);
02208 #endif
02209                      HANDLE_UNBLOCK_INTERRUPTIONS();
02210 #if ZEND_DEBUG
02211                      zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %ld bytes)", heap->limit, __zend_filename, __zend_lineno, size);
02212 #else
02213                      zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %ld bytes)", heap->limit, size);
02214 #endif
02215                      return NULL;
02216               }
02217 
02218               segment = ZEND_MM_STORAGE_REALLOC(segment_copy, segment_size);
02219               if (!segment) {
02220 #if ZEND_MM_CACHE
02221                      zend_mm_free_cache(heap);
02222 #endif
02223                      HANDLE_UNBLOCK_INTERRUPTIONS();
02224 out_of_memory:
02225 #if ZEND_DEBUG
02226                      zend_mm_safe_error(heap, "Out of memory (allocated %ld) at %s:%d (tried to allocate %ld bytes)", heap->real_size, __zend_filename, __zend_lineno, size);
02227 #else
02228                      zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %ld bytes)", heap->real_size, size);
02229 #endif
02230                      return NULL;
02231               }
02232               heap->real_size += segment_size - segment->size;
02233               if (heap->real_size > heap->real_peak) {
02234                      heap->real_peak = heap->real_size;
02235               }
02236 
02237               segment->size = segment_size;
02238 
02239               if (segment != segment_copy) {
02240                      zend_mm_segment **seg = &heap->segments_list;
02241                      while (*seg != segment_copy) {
02242                             seg = &(*seg)->next_segment;
02243                      }
02244                      *seg = segment;
02245                      mm_block = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
02246                      ZEND_MM_MARK_FIRST_BLOCK(mm_block);
02247               }
02248 
02249               block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;
02250               remaining_size = block_size - true_size;
02251 
02252               /* setup guard block */
02253               ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(mm_block, block_size));
02254 
02255               if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
02256                      true_size = block_size;
02257                      ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
02258               } else {
02259                      zend_mm_free_block *new_free_block;
02260 
02261                      /* prepare new free block */
02262                      ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
02263                      new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(mm_block, true_size);
02264                      ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);
02265 
02266                      /* add the new free block to the free list */
02267                      zend_mm_add_to_rest_list(heap, new_free_block);
02268               }
02269 
02270               ZEND_MM_SET_DEBUG_INFO(mm_block, size, 1, 1);
02271 
02272               heap->size = heap->size + true_size - orig_size;
02273               if (heap->peak < heap->size) {
02274                      heap->peak = heap->size;
02275               }
02276 
02277               HANDLE_UNBLOCK_INTERRUPTIONS();
02278               return ZEND_MM_DATA_OF(mm_block);
02279        }
02280 
02281        ptr = _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02282 #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
02283        memcpy(ptr, p, mm_block->debug.size);
02284 #else
02285        memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
02286 #endif
02287        _zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02288        return ptr;
02289 }
02290 
02291 ZEND_API void *_zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02292 {
02293        return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02294 }
02295 
02296 ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02297 {
02298        _zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02299 }
02300 
02301 ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02302 {
02303        return _zend_mm_realloc_int(heap, ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02304 }
02305 
02306 ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02307 {
02308        zend_mm_block *mm_block;
02309 
02310        if (!ZEND_MM_VALID_PTR(p)) {
02311               return 0;
02312        }
02313        mm_block = ZEND_MM_HEADER_OF(p);
02314        ZEND_MM_CHECK_PROTECTION(mm_block);
02315 #if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
02316        return mm_block->debug.size;
02317 #else
02318        return ZEND_MM_BLOCK_SIZE(mm_block);
02319 #endif
02320 }
02321 
02322 /**********************/
02323 /* Allocation Manager */
02324 /**********************/
02325 
02326 typedef struct _zend_alloc_globals {
02327        zend_mm_heap *mm_heap;
02328 } zend_alloc_globals;
02329 
02330 #ifdef ZTS
02331 static int alloc_globals_id;
02332 # define AG(v) TSRMG(alloc_globals_id, zend_alloc_globals *, v)
02333 #else
02334 # define AG(v) (alloc_globals.v)
02335 static zend_alloc_globals alloc_globals;
02336 #endif
02337 
02338 ZEND_API int is_zend_mm(TSRMLS_D)
02339 {
02340        return AG(mm_heap)->use_zend_alloc;
02341 }
02342 
02343 ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02344 {
02345        TSRMLS_FETCH();
02346 
02347        if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
02348               return AG(mm_heap)->_malloc(size);
02349        }
02350        return _zend_mm_alloc_int(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02351 }
02352 
02353 ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02354 {
02355        TSRMLS_FETCH();
02356 
02357        if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
02358               AG(mm_heap)->_free(ptr);
02359               return;
02360        }
02361        _zend_mm_free_int(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02362 }
02363 
02364 ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02365 {
02366        TSRMLS_FETCH();
02367 
02368        if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
02369               return AG(mm_heap)->_realloc(ptr, size);
02370        }
02371        return _zend_mm_realloc_int(AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02372 }
02373 
02374 ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02375 {
02376        if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
02377               return 0;
02378        }
02379        return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02380 }
02381 
02382 #if defined(__GNUC__) && defined(i386)
02383 
02384 static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
02385 {
02386        size_t res = nmemb;
02387        unsigned long overflow = 0;
02388 
02389        __asm__ ("mull %3\n\taddl %4,%0\n\tadcl %1,%1"
02390             : "=&a"(res), "=&d" (overflow)
02391             : "%0"(res),
02392               "rm"(size),
02393               "rm"(offset));
02394        
02395        if (UNEXPECTED(overflow)) {
02396               zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
02397               return 0;
02398        }
02399        return res;
02400 }
02401 
02402 #elif defined(__GNUC__) && defined(__x86_64__)
02403 
02404 static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
02405 {
02406         size_t res = nmemb;
02407         unsigned long overflow = 0;
02408 
02409         __asm__ ("mulq %3\n\taddq %4,%0\n\tadcq %1,%1"
02410              : "=&a"(res), "=&d" (overflow)
02411              : "%0"(res),
02412                "rm"(size),
02413                "rm"(offset));
02414 
02415         if (UNEXPECTED(overflow)) {
02416                 zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
02417                 return 0;
02418         }
02419         return res;
02420 }
02421 
02422 #elif SIZEOF_SIZE_T == 4 && defined(HAVE_ZEND_LONG64)
02423 
02424 static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
02425 {
02426        zend_ulong64 res = (zend_ulong64)nmemb * (zend_ulong64)size + (zend_ulong64)offset;
02427 
02428        if (UNEXPECTED(res > (zend_ulong64)0xFFFFFFFFL)) {
02429               zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
02430               return 0;
02431        }
02432        return (size_t) res;
02433 }
02434 
02435 #else
02436 
02437 static inline size_t safe_address(size_t nmemb, size_t size, size_t offset)
02438 {
02439        size_t res = nmemb * size + offset;
02440        double _d  = (double)nmemb * (double)size + (double)offset;
02441        double _delta = (double)res - _d;
02442 
02443        if (UNEXPECTED((_d + _delta ) != _d)) {
02444               zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
02445               return 0;
02446        }
02447        return res;
02448 }
02449 #endif
02450 
02451 
02452 ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02453 {
02454        return emalloc_rel(safe_address(nmemb, size, offset));
02455 }
02456 
02457 ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset)
02458 {
02459        return pemalloc(safe_address(nmemb, size, offset), 1);
02460 }
02461 
02462 ZEND_API void *_safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02463 {
02464        return erealloc_rel(ptr, safe_address(nmemb, size, offset));
02465 }
02466 
02467 ZEND_API void *_safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset)
02468 {
02469        return perealloc(ptr, safe_address(nmemb, size, offset), 1);
02470 }
02471 
02472 
02473 ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02474 {
02475        void *p;
02476 
02477        p = _safe_emalloc(nmemb, size, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02478        if (UNEXPECTED(p == NULL)) {
02479               return p;
02480        }
02481        memset(p, 0, size * nmemb);
02482        return p;
02483 }
02484 
02485 ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02486 {
02487        int length;
02488        char *p;
02489 
02490        length = strlen(s)+1;
02491        p = (char *) _emalloc(length ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02492        if (UNEXPECTED(p == NULL)) {
02493               return p;
02494        }
02495        memcpy(p, s, length);
02496        return p;
02497 }
02498 
02499 ZEND_API char *_estrndup(const char *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02500 {
02501        char *p;
02502 
02503        p = (char *) _emalloc(length+1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02504        if (UNEXPECTED(p == NULL)) {
02505               return p;
02506        }
02507        memcpy(p, s, length);
02508        p[length] = 0;
02509        return p;
02510 }
02511 
02512 
02513 ZEND_API char *zend_strndup(const char *s, uint length)
02514 {
02515        char *p;
02516 
02517        p = (char *) malloc(length+1);
02518        if (UNEXPECTED(p == NULL)) {
02519               return p;
02520        }
02521        if (length) {
02522               memcpy(p, s, length);
02523        }
02524        p[length] = 0;
02525        return p;
02526 }
02527 
02528 
02529 ZEND_API int zend_set_memory_limit(size_t memory_limit)
02530 {
02531        TSRMLS_FETCH();
02532 
02533        AG(mm_heap)->limit = (memory_limit >= AG(mm_heap)->block_size) ? memory_limit : AG(mm_heap)->block_size;
02534 
02535        return SUCCESS;
02536 }
02537 
02538 ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC)
02539 {
02540        if (real_usage) {
02541               return AG(mm_heap)->real_size;
02542        } else {
02543               size_t usage = AG(mm_heap)->size;
02544 #if ZEND_MM_CACHE
02545               usage -= AG(mm_heap)->cached;
02546 #endif
02547               return usage;
02548        }
02549 }
02550 
02551 ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC)
02552 {
02553        if (real_usage) {
02554               return AG(mm_heap)->real_peak;
02555        } else {
02556               return AG(mm_heap)->peak;
02557        }
02558 }
02559 
02560 ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC)
02561 {
02562        zend_mm_shutdown(AG(mm_heap), full_shutdown, silent TSRMLS_CC);
02563 }
02564 
02565 static void alloc_globals_ctor(zend_alloc_globals *alloc_globals TSRMLS_DC)
02566 {
02567        char *tmp;
02568        alloc_globals->mm_heap = zend_mm_startup();
02569 
02570        tmp = getenv("USE_ZEND_ALLOC");
02571        if (tmp) {
02572               alloc_globals->mm_heap->use_zend_alloc = zend_atoi(tmp, 0);
02573               if (!alloc_globals->mm_heap->use_zend_alloc) {
02574                      alloc_globals->mm_heap->_malloc = malloc;
02575                      alloc_globals->mm_heap->_free = free;
02576                      alloc_globals->mm_heap->_realloc = realloc;
02577               }
02578        }
02579 }
02580 
02581 #ifdef ZTS
02582 static void alloc_globals_dtor(zend_alloc_globals *alloc_globals TSRMLS_DC)
02583 {
02584        shutdown_memory_manager(1, 1 TSRMLS_CC);
02585 }
02586 #endif
02587 
02588 ZEND_API void start_memory_manager(TSRMLS_D)
02589 {
02590 #ifdef ZTS
02591        ts_allocate_id(&alloc_globals_id, sizeof(zend_alloc_globals), (ts_allocate_ctor) alloc_globals_ctor, (ts_allocate_dtor) alloc_globals_dtor);
02592 #else
02593        alloc_globals_ctor(&alloc_globals);
02594 #endif
02595 }
02596 
02597 ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC)
02598 {
02599        zend_mm_heap *old_heap;
02600 
02601        old_heap = AG(mm_heap);
02602        AG(mm_heap) = new_heap;
02603        return old_heap;
02604 }
02605 
02606 ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap)
02607 {
02608        return heap->storage;
02609 }
02610 
02611 ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
02612                                           void* (*_malloc)(size_t),
02613                                           void  (*_free)(void*),
02614                                           void* (*_realloc)(void*, size_t))
02615 {
02616        heap->use_zend_alloc = 0;
02617        heap->_malloc = _malloc;
02618        heap->_free = _free;
02619        heap->_realloc = _realloc;
02620 }
02621 
02622 #if ZEND_DEBUG
02623 ZEND_API int _mem_block_check(void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02624 {
02625        TSRMLS_FETCH();
02626 
02627        if (!AG(mm_heap)->use_zend_alloc) {
02628               return 1;
02629        }
02630        return zend_mm_check_ptr(AG(mm_heap), ptr, silent ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02631 }
02632 
02633 
02634 ZEND_API void _full_mem_check(int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
02635 {
02636        int errors;
02637        TSRMLS_FETCH();
02638 
02639        if (!AG(mm_heap)->use_zend_alloc) {
02640               return;
02641        }
02642 
02643        zend_debug_alloc_output("------------------------------------------------\n");
02644        zend_debug_alloc_output("Full Memory Check at %s:%d\n" ZEND_FILE_LINE_RELAY_CC);
02645 
02646        errors = zend_mm_check_heap(AG(mm_heap), silent ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
02647 
02648        zend_debug_alloc_output("End of full memory check %s:%d (%d errors)\n" ZEND_FILE_LINE_RELAY_CC, errors);
02649        zend_debug_alloc_output("------------------------------------------------\n");
02650 }
02651 #endif
02652 
02653 /*
02654  * Local variables:
02655  * tab-width: 4
02656  * c-basic-offset: 4
02657  * indent-tabs-mode: t
02658  * End:
02659  */