Back to index

php5  5.3.10
Classes | Defines | Typedefs | Functions | Variables
zend_alloc.c File Reference
#include "zend.h"
#include "zend_alloc.h"
#include "zend_globals.h"
#include "zend_operators.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

Go to the source code of this file.

Classes

struct  _zend_mm_block_info
struct  _zend_mm_block
struct  _zend_mm_small_free_block
struct  _zend_mm_free_block
struct  _zend_mm_heap
struct  _zend_alloc_globals

Defines

#define ZEND_MM_HEAP_PROTECTION   ZEND_DEBUG
#define ZEND_MM_SAFE_UNLINKING   1
#define ZEND_MM_COOKIES   ZEND_DEBUG
#define PTR_FMT   "0x%0.8lx"
#define HAVE_MEM_MALLOC   /* use malloc() to allocate segments */
#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}
#define ZEND_MM_STORAGE_DTOR()   heap->storage->handlers->dtor(heap->storage)
#define ZEND_MM_STORAGE_ALLOC(size)   heap->storage->handlers->_alloc(heap->storage, size)
#define ZEND_MM_STORAGE_REALLOC(ptr, size)   heap->storage->handlers->_realloc(heap->storage, ptr, size)
#define ZEND_MM_STORAGE_FREE(ptr)   heap->storage->handlers->_free(heap->storage, ptr)
#define MEM_BLOCK_VALID   0x7312F8DC
#define MEM_BLOCK_FREED   0x99954317
#define MEM_BLOCK_CACHED   0xFB8277DC
#define MEM_BLOCK_GUARD   0x2A8FCC84
#define MEM_BLOCK_LEAK   0x6C5E8F2D
#define ZEND_MM_NUM_BUCKETS   (sizeof(size_t) << 3)
#define ZEND_MM_CACHE   1
#define ZEND_MM_CACHE_SIZE   (ZEND_MM_NUM_BUCKETS * 4 * 1024)
#define ZEND_MM_CACHE_STAT   0
#define ZEND_MM_SMALL_FREE_BUCKET(heap, index)
#define ZEND_MM_REST_BUCKET(heap)
#define ZEND_MM_SET_COOKIE(block)
#define ZEND_MM_CHECK_COOKIE(block)
#define ZEND_MM_SEG_SIZE   (256 * 1024)
#define ZEND_MM_RESERVE_SIZE   (8*1024)
#define ZEND_MM_LONG_CONST(x)   (x##L)
#define ZEND_MM_TYPE_MASK   ZEND_MM_LONG_CONST(0x3)
#define ZEND_MM_FREE_BLOCK   ZEND_MM_LONG_CONST(0x0)
#define ZEND_MM_USED_BLOCK   ZEND_MM_LONG_CONST(0x1)
#define ZEND_MM_GUARD_BLOCK   ZEND_MM_LONG_CONST(0x3)
#define ZEND_MM_BLOCK(b, type, size)
#define ZEND_MM_LAST_BLOCK(b)
#define ZEND_MM_BLOCK_SIZE(b)   ((b)->info._size & ~ZEND_MM_TYPE_MASK)
#define ZEND_MM_IS_FREE_BLOCK(b)   (!((b)->info._size & ZEND_MM_USED_BLOCK))
#define ZEND_MM_IS_USED_BLOCK(b)   ((b)->info._size & ZEND_MM_USED_BLOCK)
#define ZEND_MM_IS_GUARD_BLOCK(b)   (((b)->info._size & ZEND_MM_TYPE_MASK) == ZEND_MM_GUARD_BLOCK)
#define ZEND_MM_NEXT_BLOCK(b)   ZEND_MM_BLOCK_AT(b, ZEND_MM_BLOCK_SIZE(b))
#define ZEND_MM_PREV_BLOCK(b)   ZEND_MM_BLOCK_AT(b, -(ssize_t)((b)->info._prev & ~ZEND_MM_TYPE_MASK))
#define ZEND_MM_PREV_BLOCK_IS_FREE(b)   (!((b)->info._prev & ZEND_MM_USED_BLOCK))
#define ZEND_MM_MARK_FIRST_BLOCK(b)   ((b)->info._prev = ZEND_MM_GUARD_BLOCK)
#define ZEND_MM_IS_FIRST_BLOCK(b)   ((b)->info._prev == ZEND_MM_GUARD_BLOCK)
#define ZEND_MM_FREE_BLOCK_SIZE(b)   (b)->info._size
#define ZEND_MM_ALIGNED_HEADER_SIZE   ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_block))
#define ZEND_MM_ALIGNED_FREE_HEADER_SIZE   ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_small_free_block))
#define ZEND_MM_MIN_ALLOC_BLOCK_SIZE   ZEND_MM_ALIGNED_SIZE(ZEND_MM_ALIGNED_HEADER_SIZE + END_MAGIC_SIZE)
#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)
#define ZEND_MM_ALIGNED_SEGMENT_SIZE   ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_segment))
#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)
#define ZEND_MM_MAX_SMALL_SIZE   ((ZEND_MM_NUM_BUCKETS<<ZEND_MM_ALIGNMENT_LOG2)+ZEND_MM_ALIGNED_MIN_HEADER_SIZE)
#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)))
#define ZEND_MM_BUCKET_INDEX(true_size)   ((true_size>>ZEND_MM_ALIGNMENT_LOG2)-(ZEND_MM_ALIGNED_MIN_HEADER_SIZE>>ZEND_MM_ALIGNMENT_LOG2))
#define ZEND_MM_SMALL_SIZE(true_size)   (true_size < ZEND_MM_MAX_SMALL_SIZE)
#define ZEND_MM_BLOCK_AT(blk, offset)   ((zend_mm_block *) (((char *) (blk))+(offset)))
#define ZEND_MM_DATA_OF(p)   ((void *) (((char *) (p))+ZEND_MM_ALIGNED_HEADER_SIZE))
#define ZEND_MM_HEADER_OF(blk)   ZEND_MM_BLOCK_AT(blk, -(int)ZEND_MM_ALIGNED_HEADER_SIZE)
#define ZEND_MM_VALID_PTR(ptr)   EXPECTED(ptr != NULL)
#define ZEND_MM_SET_MAGIC(block, val)
#define ZEND_MM_CHECK_MAGIC(block, val)
#define ZEND_MM_SET_DEBUG_INFO(block, __size, set_valid, set_thread)   ZEND_MM_SET_BLOCK_SIZE(block, __size)
#define ZEND_MM_SET_BLOCK_SIZE(block, _size)
#define ZEND_MM_CHECK_PROTECTION(block)
#define END_MAGIC_SIZE   0
#define ZEND_MM_CHECK_BLOCK_LINKAGE(block)
#define ZEND_MM_CHECK_TREE(block)
#define ZEND_MM_LARGE_BUCKET_INDEX(S)   zend_mm_high_bit(S)
#define AG(v)   (alloc_globals.v)

Typedefs

typedef struct _zend_mm_block_info zend_mm_block_info
typedef struct _zend_mm_block zend_mm_block
typedef struct
_zend_mm_small_free_block 
zend_mm_small_free_block
typedef struct _zend_mm_free_block zend_mm_free_block
typedef struct _zend_alloc_globals zend_alloc_globals

Functions

static void zend_mm_panic (const char *message)
static zend_mm_storage * zend_mm_mem_dummy_init (void *params)
static void zend_mm_mem_dummy_dtor (zend_mm_storage *storage)
static void zend_mm_mem_dummy_compact (zend_mm_storage *storage)
static zend_mm_segmentzend_mm_mem_malloc_alloc (zend_mm_storage *storage, size_t size)
static zend_mm_segmentzend_mm_mem_malloc_realloc (zend_mm_storage *storage, zend_mm_segment *ptr, size_t size)
static void zend_mm_mem_malloc_free (zend_mm_storage *storage, zend_mm_segment *ptr)
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
static void _zend_mm_free_int (zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
static void * _zend_mm_realloc_int (zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
static unsigned int zend_mm_high_bit (size_t _size)
static unsigned int zend_mm_low_bit (size_t _size)
static void zend_mm_add_to_rest_list (zend_mm_heap *heap, zend_mm_free_block *mm_block)
static void zend_mm_add_to_free_list (zend_mm_heap *heap, zend_mm_free_block *mm_block)
static void zend_mm_remove_from_free_list (zend_mm_heap *heap, zend_mm_free_block *mm_block)
static void zend_mm_init (zend_mm_heap *heap)
static void zend_mm_del_segment (zend_mm_heap *heap, zend_mm_segment *segment)
static void zend_mm_free_cache (zend_mm_heap *heap)
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)
ZEND_API zend_mm_heap * zend_mm_startup (void)
ZEND_API void zend_mm_shutdown (zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC)
static void zend_mm_safe_error (zend_mm_heap *heap, const char *format, size_t limit, size_t size)
static zend_mm_free_blockzend_mm_search_large_block (zend_mm_heap *heap, size_t true_size)
ZEND_API void * _zend_mm_alloc (zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API void _zend_mm_free (zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API void * _zend_mm_realloc (zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API size_t _zend_mm_block_size (zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API int is_zend_mm (TSRMLS_D)
ZEND_API void * _emalloc (size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API void _efree (void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API void * _erealloc (void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API size_t _zend_mem_block_size (void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
static size_t safe_address (size_t nmemb, size_t size, size_t offset)
ZEND_API void * _safe_emalloc (size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API void * _safe_malloc (size_t nmemb, size_t size, size_t offset)
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)
ZEND_API void * _safe_realloc (void *ptr, size_t nmemb, size_t size, size_t offset)
ZEND_API void * _ecalloc (size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API char * _estrdup (const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API char * _estrndup (const char *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
ZEND_API char * zend_strndup (const char *s, uint length)
ZEND_API int zend_set_memory_limit (size_t memory_limit)
ZEND_API size_t zend_memory_usage (int real_usage TSRMLS_DC)
ZEND_API size_t zend_memory_peak_usage (int real_usage TSRMLS_DC)
ZEND_API void shutdown_memory_manager (int silent, int full_shutdown TSRMLS_DC)
static void alloc_globals_ctor (zend_alloc_globals *alloc_globals TSRMLS_DC)
ZEND_API void start_memory_manager (TSRMLS_D)
ZEND_API zend_mm_heap * zend_mm_set_heap (zend_mm_heap *new_heap TSRMLS_DC)
ZEND_API zend_mm_storage * zend_mm_get_storage (zend_mm_heap *heap)
ZEND_API void zend_mm_set_custom_handlers (zend_mm_heap *heap, void *(*_malloc)(size_t), void(*_free)(void *), void *(*_realloc)(void *, size_t))

Variables

static const zend_mm_mem_handlers mem_handlers []
static zend_alloc_globals alloc_globals

Class Documentation

struct _zend_mm_block_info

Definition at line 336 of file zend_alloc.c.

Class Members
size_t _prev
size_t _size
struct _zend_mm_block

Definition at line 366 of file zend_alloc.c.

Collaboration diagram for _zend_mm_block:
Class Members
zend_mm_block_info info
struct _zend_mm_small_free_block

Definition at line 379 of file zend_alloc.c.

Collaboration diagram for _zend_mm_small_free_block:
Class Members
zend_mm_block_info info
struct _zend_mm_free_block * next_free_block
struct _zend_mm_free_block * prev_free_block
struct _zend_mm_free_block

Definition at line 391 of file zend_alloc.c.

Collaboration diagram for _zend_mm_free_block:
Class Members
struct _zend_mm_free_block * child
zend_mm_block_info info
struct _zend_mm_free_block * next_free_block
struct _zend_mm_free_block ** parent
struct _zend_mm_free_block * prev_free_block
struct _zend_alloc_globals

Definition at line 2326 of file zend_alloc.c.

Class Members
zend_mm_heap * mm_heap

Define Documentation

#define AG (   v)    (alloc_globals.v)

Definition at line 2334 of file zend_alloc.c.

#define END_MAGIC_SIZE   0

Definition at line 634 of file zend_alloc.c.

#define HAVE_MEM_MALLOC   /* use malloc() to allocate segments */

Definition at line 104 of file zend_alloc.c.

#define MEM_BLOCK_CACHED   0xFB8277DC

Definition at line 331 of file zend_alloc.c.

#define MEM_BLOCK_FREED   0x99954317

Definition at line 330 of file zend_alloc.c.

#define MEM_BLOCK_GUARD   0x2A8FCC84

Definition at line 332 of file zend_alloc.c.

#define MEM_BLOCK_LEAK   0x6C5E8F2D

Definition at line 333 of file zend_alloc.c.

#define MEM_BLOCK_VALID   0x7312F8DC

Definition at line 329 of file zend_alloc.c.

#define PTR_FMT   "0x%0.8lx"

Definition at line 59 of file zend_alloc.c.

Definition at line 525 of file zend_alloc.c.

Definition at line 524 of file zend_alloc.c.

#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)

Definition at line 527 of file zend_alloc.c.

Definition at line 528 of file zend_alloc.c.

#define ZEND_MM_BLOCK (   b,
  type,
  size 
)
Value:
do { \
                                                                             size_t _size = (size); \
                                                                             (b)->info._size = (type) | _size; \
                                                                             ZEND_MM_BLOCK_AT(b, _size)->info._prev = (type) | _size; \
                                                                             ZEND_MM_SET_COOKIE(b); \
                                                                      } while (0);

Definition at line 497 of file zend_alloc.c.

#define ZEND_MM_BLOCK_AT (   blk,
  offset 
)    ((zend_mm_block *) (((char *) (blk))+(offset)))

Definition at line 541 of file zend_alloc.c.

#define ZEND_MM_BLOCK_SIZE (   b)    ((b)->info._size & ~ZEND_MM_TYPE_MASK)

Definition at line 507 of file zend_alloc.c.

#define ZEND_MM_BUCKET_INDEX (   true_size)    ((true_size>>ZEND_MM_ALIGNMENT_LOG2)-(ZEND_MM_ALIGNED_MIN_HEADER_SIZE>>ZEND_MM_ALIGNMENT_LOG2))

Definition at line 536 of file zend_alloc.c.

#define ZEND_MM_CACHE   1

Definition at line 408 of file zend_alloc.c.

#define ZEND_MM_CACHE_SIZE   (ZEND_MM_NUM_BUCKETS * 4 * 1024)

Definition at line 409 of file zend_alloc.c.

#define ZEND_MM_CACHE_STAT   0

Definition at line 412 of file zend_alloc.c.

#define ZEND_MM_CHECK_BLOCK_LINKAGE (   block)
Value:
if (UNEXPECTED((block)->info._size != ZEND_MM_BLOCK_AT(block, ZEND_MM_FREE_BLOCK_SIZE(block))->info._prev) || \
              UNEXPECTED(!UNEXPECTED(ZEND_MM_IS_FIRST_BLOCK(block)) && \
           UNEXPECTED(ZEND_MM_PREV_BLOCK(block)->info._size != (block)->info._prev))) { \
           zend_mm_panic("zend_mm_heap corrupted"); \
       }

Definition at line 639 of file zend_alloc.c.

#define ZEND_MM_CHECK_COOKIE (   block)

Definition at line 476 of file zend_alloc.c.

#define ZEND_MM_CHECK_MAGIC (   block,
  val 
)

Definition at line 590 of file zend_alloc.c.

#define ZEND_MM_CHECK_PROTECTION (   block)

Definition at line 632 of file zend_alloc.c.

#define ZEND_MM_CHECK_TREE (   block)
Value:
if (UNEXPECTED(*((block)->parent) != (block))) { \
              zend_mm_panic("zend_mm_heap corrupted"); \
       }

Definition at line 645 of file zend_alloc.c.

#define ZEND_MM_COOKIES   ZEND_DEBUG

Definition at line 49 of file zend_alloc.c.

#define ZEND_MM_DATA_OF (   p)    ((void *) (((char *) (p))+ZEND_MM_ALIGNED_HEADER_SIZE))

Definition at line 542 of file zend_alloc.c.

Definition at line 493 of file zend_alloc.c.

#define ZEND_MM_FREE_BLOCK_SIZE (   b)    (b)->info._size

Definition at line 521 of file zend_alloc.c.

Definition at line 495 of file zend_alloc.c.

Definition at line 543 of file zend_alloc.c.

#define ZEND_MM_HEAP_PROTECTION   ZEND_DEBUG

Definition at line 41 of file zend_alloc.c.

#define ZEND_MM_IS_FIRST_BLOCK (   b)    ((b)->info._prev == ZEND_MM_GUARD_BLOCK)

Definition at line 518 of file zend_alloc.c.

#define ZEND_MM_IS_FREE_BLOCK (   b)    (!((b)->info._size & ZEND_MM_USED_BLOCK))

Definition at line 508 of file zend_alloc.c.

#define ZEND_MM_IS_GUARD_BLOCK (   b)    (((b)->info._size & ZEND_MM_TYPE_MASK) == ZEND_MM_GUARD_BLOCK)

Definition at line 510 of file zend_alloc.c.

#define ZEND_MM_IS_USED_BLOCK (   b)    ((b)->info._size & ZEND_MM_USED_BLOCK)

Definition at line 509 of file zend_alloc.c.

Definition at line 654 of file zend_alloc.c.

#define ZEND_MM_LAST_BLOCK (   b)
Value:
do { \
              (b)->info._size = ZEND_MM_GUARD_BLOCK | ZEND_MM_ALIGNED_HEADER_SIZE; \
              ZEND_MM_SET_MAGIC(b, MEM_BLOCK_GUARD); \
       } while (0);

Definition at line 503 of file zend_alloc.c.

#define ZEND_MM_LONG_CONST (   x)    (x##L)

Definition at line 488 of file zend_alloc.c.

#define ZEND_MM_MARK_FIRST_BLOCK (   b)    ((b)->info._prev = ZEND_MM_GUARD_BLOCK)

Definition at line 517 of file zend_alloc.c.

Definition at line 532 of file zend_alloc.c.

Definition at line 300 of file zend_alloc.c.

Definition at line 526 of file zend_alloc.c.

Definition at line 530 of file zend_alloc.c.

Definition at line 512 of file zend_alloc.c.

#define ZEND_MM_NUM_BUCKETS   (sizeof(size_t) << 3)

Definition at line 406 of file zend_alloc.c.

#define ZEND_MM_PREV_BLOCK (   b)    ZEND_MM_BLOCK_AT(b, -(ssize_t)((b)->info._prev & ~ZEND_MM_TYPE_MASK))

Definition at line 513 of file zend_alloc.c.

#define ZEND_MM_PREV_BLOCK_IS_FREE (   b)    (!((b)->info._prev & ZEND_MM_USED_BLOCK))

Definition at line 515 of file zend_alloc.c.

#define ZEND_MM_RESERVE_SIZE   (8*1024)

Definition at line 483 of file zend_alloc.c.

#define ZEND_MM_REST_BUCKET (   heap)
Value:
(zend_mm_free_block*)((char*)&heap->rest_buckets[0] + \
              sizeof(zend_mm_free_block*) * 2 - \
              sizeof(zend_mm_small_free_block))

Definition at line 457 of file zend_alloc.c.

#define ZEND_MM_SAFE_UNLINKING   1

Definition at line 45 of file zend_alloc.c.

#define ZEND_MM_SEG_SIZE   (256 * 1024)

Definition at line 480 of file zend_alloc.c.

#define ZEND_MM_SET_BLOCK_SIZE (   block,
  _size 
)

Definition at line 629 of file zend_alloc.c.

#define ZEND_MM_SET_COOKIE (   block)

Definition at line 475 of file zend_alloc.c.

#define ZEND_MM_SET_DEBUG_INFO (   block,
  __size,
  set_valid,
  set_thread 
)    ZEND_MM_SET_BLOCK_SIZE(block, __size)

Definition at line 592 of file zend_alloc.c.

#define ZEND_MM_SET_MAGIC (   block,
  val 
)

Definition at line 588 of file zend_alloc.c.

#define ZEND_MM_SMALL_FREE_BUCKET (   heap,
  index 
)
Value:
(zend_mm_free_block*) ((char*)&heap->free_buckets[index * 2] + \
              sizeof(zend_mm_free_block*) * 2 - \
              sizeof(zend_mm_small_free_block))

Definition at line 452 of file zend_alloc.c.

#define ZEND_MM_SMALL_SIZE (   true_size)    (true_size < ZEND_MM_MAX_SMALL_SIZE)

Definition at line 538 of file zend_alloc.c.

#define ZEND_MM_STORAGE_ALLOC (   size)    heap->storage->handlers->_alloc(heap->storage, size)

Definition at line 321 of file zend_alloc.c.

#define ZEND_MM_STORAGE_DTOR ( )    heap->storage->handlers->dtor(heap->storage)

Definition at line 320 of file zend_alloc.c.

#define ZEND_MM_STORAGE_FREE (   ptr)    heap->storage->handlers->_free(heap->storage, ptr)

Definition at line 323 of file zend_alloc.c.

#define ZEND_MM_STORAGE_REALLOC (   ptr,
  size 
)    heap->storage->handlers->_realloc(heap->storage, ptr, size)

Definition at line 322 of file zend_alloc.c.

Definition at line 534 of file zend_alloc.c.

Definition at line 491 of file zend_alloc.c.

Definition at line 494 of file zend_alloc.c.

#define ZEND_MM_VALID_PTR (   ptr)    EXPECTED(ptr != NULL)

Definition at line 586 of file zend_alloc.c.


Typedef Documentation

typedef struct _zend_mm_block zend_mm_block

Function Documentation

ZEND_API void* _ecalloc ( size_t  nmemb,
size_t size ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2473 of file zend_alloc.c.

{
       void *p;

       p = _safe_emalloc(nmemb, size, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
       if (UNEXPECTED(p == NULL)) {
              return p;
       }
       memset(p, 0, size * nmemb);
       return p;
}

Here is the call graph for this function:

ZEND_API void _efree ( void *ptr ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC)

Definition at line 2353 of file zend_alloc.c.

{
       TSRMLS_FETCH();

       if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
              AG(mm_heap)->_free(ptr);
              return;
       }
       _zend_mm_free_int(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
}

Here is the call graph for this function:

ZEND_API void* _emalloc ( size_t size ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC)

Definition at line 2343 of file zend_alloc.c.

{
       TSRMLS_FETCH();

       if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
              return AG(mm_heap)->_malloc(size);
       }
       return _zend_mm_alloc_int(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API void* _erealloc ( void *  ptr,
size_t  size,
int allow_failure ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2364 of file zend_alloc.c.

{
       TSRMLS_FETCH();

       if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
              return AG(mm_heap)->_realloc(ptr, size);
       }
       return _zend_mm_realloc_int(AG(mm_heap), ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
}

Here is the call graph for this function:

ZEND_API char* _estrdup ( const char *s ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC)

Definition at line 2485 of file zend_alloc.c.

{
       int length;
       char *p;

       length = strlen(s)+1;
       p = (char *) _emalloc(length ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
       if (UNEXPECTED(p == NULL)) {
              return p;
       }
       memcpy(p, s, length);
       return p;
}

Here is the call graph for this function:

ZEND_API char* _estrndup ( const char *  s,
uint length ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2499 of file zend_alloc.c.

{
       char *p;

       p = (char *) _emalloc(length+1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
       if (UNEXPECTED(p == NULL)) {
              return p;
       }
       memcpy(p, s, length);
       p[length] = 0;
       return p;
}

Here is the call graph for this function:

ZEND_API void* _safe_emalloc ( size_t  nmemb,
size_t  size,
size_t offset ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2452 of file zend_alloc.c.

{
       return emalloc_rel(safe_address(nmemb, size, offset));
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 
)

Definition at line 2462 of file zend_alloc.c.

{
       return erealloc_rel(ptr, safe_address(nmemb, size, offset));
}

Here is the call graph for this function:

ZEND_API void* _safe_malloc ( size_t  nmemb,
size_t  size,
size_t  offset 
)

Definition at line 2457 of file zend_alloc.c.

{
       return pemalloc(safe_address(nmemb, size, offset), 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API void* _safe_realloc ( void *  ptr,
size_t  nmemb,
size_t  size,
size_t  offset 
)

Definition at line 2467 of file zend_alloc.c.

{
       return perealloc(ptr, safe_address(nmemb, size, offset), 1);
}

Here is the call graph for this function:

Definition at line 2374 of file zend_alloc.c.

{
       if (UNEXPECTED(!AG(mm_heap)->use_zend_alloc)) {
              return 0;
       }
       return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
}

Here is the call graph for this function:

ZEND_API void* _zend_mm_alloc ( zend_mm_heap *  heap,
size_t size ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2291 of file zend_alloc.c.

Here is the call graph for this function:

static void * _zend_mm_alloc_int ( zend_mm_heap *  heap,
size_t size ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
) [static]

Definition at line 1810 of file zend_alloc.c.

{
       zend_mm_free_block *best_fit;
       size_t true_size = ZEND_MM_TRUE_SIZE(size);
       size_t block_size;
       size_t remaining_size;
       size_t segment_size;
       zend_mm_segment *segment;
       int keep_rest = 0;

       if (EXPECTED(ZEND_MM_SMALL_SIZE(true_size))) {
              size_t index = ZEND_MM_BUCKET_INDEX(true_size);
              size_t bitmap;

              if (UNEXPECTED(true_size < size)) {
                     goto out_of_memory;
              }
#if ZEND_MM_CACHE
              if (EXPECTED(heap->cache[index] != NULL)) {
                     /* Get block from cache */
#if ZEND_MM_CACHE_STAT
                     heap->cache_stat[index].count--;
                     heap->cache_stat[index].hit++;
#endif
                     best_fit = heap->cache[index];
                     heap->cache[index] = best_fit->prev_free_block;
                     heap->cached -= true_size;
                     ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
                     ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
                     return ZEND_MM_DATA_OF(best_fit);
              }
#if ZEND_MM_CACHE_STAT
              heap->cache_stat[index].miss++;
#endif
#endif

              bitmap = heap->free_bitmap >> index;
              if (bitmap) {
                     /* Found some "small" free block that can be used */
                     index += zend_mm_low_bit(bitmap);
                     best_fit = heap->free_buckets[index*2];
#if ZEND_MM_CACHE_STAT
                     heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit++;
#endif
                     goto zend_mm_finished_searching_for_block;
              }
       }

#if ZEND_MM_CACHE_STAT
       heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss++;
#endif

       best_fit = zend_mm_search_large_block(heap, true_size);

       if (!best_fit && heap->real_size >= heap->limit - heap->block_size) {
              zend_mm_free_block *p = heap->rest_buckets[0];
              size_t best_size = -1;

              while (p != ZEND_MM_REST_BUCKET(heap)) {
                     if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
                            best_fit = p;
                            goto zend_mm_finished_searching_for_block;
                     } else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
                                ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
                            best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
                            best_fit = p;
                     }
                     p = p->prev_free_block;
              }
       }

       if (!best_fit) {
              if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
                     /* Make sure we add a memory block which is big enough,
                        segment must have header "size" and trailer "guard" block */
                     segment_size = true_size + ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE;
                     segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
                     keep_rest = 1;
              } else {
                     segment_size = heap->block_size;
              }

              HANDLE_BLOCK_INTERRUPTIONS();

              if (segment_size < true_size ||
                  heap->real_size + segment_size > heap->limit) {
                     /* Memory limit overflow */
#if ZEND_MM_CACHE
                     zend_mm_free_cache(heap);
#endif
                     HANDLE_UNBLOCK_INTERRUPTIONS();
#if ZEND_DEBUG
                     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);
#else
                     zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %lu bytes)", heap->limit, size);
#endif
              }

              segment = (zend_mm_segment *) ZEND_MM_STORAGE_ALLOC(segment_size);

              if (!segment) {
                     /* Storage manager cannot allocate memory */
#if ZEND_MM_CACHE
                     zend_mm_free_cache(heap);
#endif
                     HANDLE_UNBLOCK_INTERRUPTIONS();
out_of_memory:
#if ZEND_DEBUG
                     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);
#else
                     zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %lu bytes)", heap->real_size, size);
#endif
                     return NULL;
              }

              heap->real_size += segment_size;
              if (heap->real_size > heap->real_peak) {
                     heap->real_peak = heap->real_size;
              }

              segment->size = segment_size;
              segment->next_segment = heap->segments_list;
              heap->segments_list = segment;

              best_fit = (zend_mm_free_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
              ZEND_MM_MARK_FIRST_BLOCK(best_fit);

              block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;

              ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(best_fit, block_size));

       } else {
zend_mm_finished_searching_for_block:
              /* remove from free list */
              HANDLE_BLOCK_INTERRUPTIONS();
              ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_FREED);
              ZEND_MM_CHECK_COOKIE(best_fit);
              ZEND_MM_CHECK_BLOCK_LINKAGE(best_fit);
              zend_mm_remove_from_free_list(heap, best_fit);

              block_size = ZEND_MM_FREE_BLOCK_SIZE(best_fit);
       }

       remaining_size = block_size - true_size;

       if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
              true_size = block_size;
              ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
       } else {
              zend_mm_free_block *new_free_block;

              /* prepare new free block */
              ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
              new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(best_fit, true_size);
              ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);

              /* add the new free block to the free list */
              if (EXPECTED(!keep_rest)) {
                     zend_mm_add_to_free_list(heap, new_free_block);
              } else {
                     zend_mm_add_to_rest_list(heap, new_free_block);
              }
       }

       ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 1);

       heap->size += true_size;
       if (heap->peak < heap->size) {
              heap->peak = heap->size;
       }

       HANDLE_UNBLOCK_INTERRUPTIONS();

       return ZEND_MM_DATA_OF(best_fit);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API size_t _zend_mm_block_size ( zend_mm_heap *  heap,
void *p ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2306 of file zend_alloc.c.

{
       zend_mm_block *mm_block;

       if (!ZEND_MM_VALID_PTR(p)) {
              return 0;
       }
       mm_block = ZEND_MM_HEADER_OF(p);
       ZEND_MM_CHECK_PROTECTION(mm_block);
#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
       return mm_block->debug.size;
#else
       return ZEND_MM_BLOCK_SIZE(mm_block);
#endif
}

Here is the caller graph for this function:

ZEND_API void _zend_mm_free ( zend_mm_heap *  heap,
void *p ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2296 of file zend_alloc.c.

Here is the call graph for this function:

static void _zend_mm_free_int ( zend_mm_heap *  heap,
void *p ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
) [static]

Definition at line 1987 of file zend_alloc.c.

{
       zend_mm_block *mm_block;
       zend_mm_block *next_block;
       size_t size;

       if (!ZEND_MM_VALID_PTR(p)) {
              return;
       }

       mm_block = ZEND_MM_HEADER_OF(p);
       size = ZEND_MM_BLOCK_SIZE(mm_block);
       ZEND_MM_CHECK_PROTECTION(mm_block);

#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
       memset(ZEND_MM_DATA_OF(mm_block), 0x5a, mm_block->debug.size);
#endif

#if ZEND_MM_CACHE
       if (EXPECTED(ZEND_MM_SMALL_SIZE(size)) && EXPECTED(heap->cached < ZEND_MM_CACHE_SIZE)) {
              size_t index = ZEND_MM_BUCKET_INDEX(size);
              zend_mm_free_block **cache = &heap->cache[index];

              ((zend_mm_free_block*)mm_block)->prev_free_block = *cache;
              *cache = (zend_mm_free_block*)mm_block;
              heap->cached += size;
              ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
#if ZEND_MM_CACHE_STAT
              if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
                     heap->cache_stat[index].max_count = heap->cache_stat[index].count;
              }
#endif
              return;
       }
#endif

       HANDLE_BLOCK_INTERRUPTIONS();

       heap->size -= size;

       next_block = ZEND_MM_BLOCK_AT(mm_block, size);
       if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
              zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
              size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
       }
       if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
              mm_block = ZEND_MM_PREV_BLOCK(mm_block);
              zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) mm_block);
              size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
       }
       if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
           ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(mm_block, size))) {
              zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
       } else {
              ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);
              zend_mm_add_to_free_list(heap, (zend_mm_free_block *) mm_block);
       }
       HANDLE_UNBLOCK_INTERRUPTIONS();
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API void* _zend_mm_realloc ( zend_mm_heap *  heap,
void *  ptr,
size_t size ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
)

Definition at line 2301 of file zend_alloc.c.

Here is the call graph for this function:

static void * _zend_mm_realloc_int ( zend_mm_heap *  heap,
void *  p,
size_t size ZEND_FILE_LINE_DC  ZEND_FILE_LINE_ORIG_DC 
) [static]

Definition at line 2047 of file zend_alloc.c.

{
       zend_mm_block *mm_block = ZEND_MM_HEADER_OF(p);
       zend_mm_block *next_block;
       size_t true_size;
       size_t orig_size;
       void *ptr;

       if (UNEXPECTED(!p) || !ZEND_MM_VALID_PTR(p)) {
              return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
       }
       mm_block = ZEND_MM_HEADER_OF(p);
       true_size = ZEND_MM_TRUE_SIZE(size);
       orig_size = ZEND_MM_BLOCK_SIZE(mm_block);
       ZEND_MM_CHECK_PROTECTION(mm_block);

       if (UNEXPECTED(true_size < size)) {
              goto out_of_memory;
       }

       if (true_size <= orig_size) {
              size_t remaining_size = orig_size - true_size;

              if (remaining_size >= ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
                     zend_mm_free_block *new_free_block;

                     HANDLE_BLOCK_INTERRUPTIONS();
                     next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);
                     if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
                            remaining_size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
                            zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
                     }

                     /* prepare new free block */
                     ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
                     new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(mm_block, true_size);

                     ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);

                     /* add the new free block to the free list */
                     zend_mm_add_to_free_list(heap, new_free_block);
                     heap->size += (true_size - orig_size);
                     HANDLE_UNBLOCK_INTERRUPTIONS();
              }
              ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
              return p;
       }

#if ZEND_MM_CACHE
       if (ZEND_MM_SMALL_SIZE(true_size)) {
              size_t index = ZEND_MM_BUCKET_INDEX(true_size);
              
              if (heap->cache[index] != NULL) {
                     zend_mm_free_block *best_fit;
                     zend_mm_free_block **cache;

#if ZEND_MM_CACHE_STAT
                     heap->cache_stat[index].count--;
                     heap->cache_stat[index].hit++;
#endif
                     best_fit = heap->cache[index];
                     heap->cache[index] = best_fit->prev_free_block;
                     ZEND_MM_CHECK_MAGIC(best_fit, MEM_BLOCK_CACHED);
                     ZEND_MM_SET_DEBUG_INFO(best_fit, size, 1, 0);
       
                     ptr = ZEND_MM_DATA_OF(best_fit);

#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
                     memcpy(ptr, p, mm_block->debug.size);
#else
                     memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
#endif

                     heap->cached -= true_size - orig_size;

                     index = ZEND_MM_BUCKET_INDEX(orig_size);
                     cache = &heap->cache[index];

                     ((zend_mm_free_block*)mm_block)->prev_free_block = *cache;
                     *cache = (zend_mm_free_block*)mm_block;
                     ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_CACHED);
#if ZEND_MM_CACHE_STAT
                     if (++heap->cache_stat[index].count > heap->cache_stat[index].max_count) {
                            heap->cache_stat[index].max_count = heap->cache_stat[index].count;
                     }
#endif

                     return ptr;
              }
       }
#endif

       next_block = ZEND_MM_BLOCK_AT(mm_block, orig_size);

       if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
              ZEND_MM_CHECK_COOKIE(next_block);
              ZEND_MM_CHECK_BLOCK_LINKAGE(next_block);
              if (orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block) >= true_size) {
                     size_t block_size = orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block);
                     size_t remaining_size = block_size - true_size;

                     HANDLE_BLOCK_INTERRUPTIONS();
                     zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);

                     if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
                            true_size = block_size;
                            ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
                     } else {
                            zend_mm_free_block *new_free_block;

                            /* prepare new free block */
                            ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
                            new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(mm_block, true_size);
                            ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);

                            /* add the new free block to the free list */
                            if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
                                ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(new_free_block, remaining_size))) {
                                   zend_mm_add_to_rest_list(heap, new_free_block);
                            } else {
                                   zend_mm_add_to_free_list(heap, new_free_block);
                            }
                     }
                     ZEND_MM_SET_DEBUG_INFO(mm_block, size, 0, 0);
                     heap->size = heap->size + true_size - orig_size;
                     if (heap->peak < heap->size) {
                            heap->peak = heap->size;
                     }
                     HANDLE_UNBLOCK_INTERRUPTIONS();
                     return p;
              } else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
                               ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_BLOCK_AT(next_block, ZEND_MM_FREE_BLOCK_SIZE(next_block)))) {
                     HANDLE_BLOCK_INTERRUPTIONS();
                     zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
                     goto realloc_segment;
              }
       } else if (ZEND_MM_IS_FIRST_BLOCK(mm_block) && ZEND_MM_IS_GUARD_BLOCK(next_block)) {
              zend_mm_segment *segment;
              zend_mm_segment *segment_copy;
              size_t segment_size;
              size_t block_size;
              size_t remaining_size;

              HANDLE_BLOCK_INTERRUPTIONS();
realloc_segment:
              /* segment size, size of block and size of guard block */
              if (true_size > heap->block_size - (ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE)) {
                     segment_size = true_size+ZEND_MM_ALIGNED_SEGMENT_SIZE+ZEND_MM_ALIGNED_HEADER_SIZE;
                     segment_size = (segment_size + (heap->block_size-1)) & ~(heap->block_size-1);
              } else {
                     segment_size = heap->block_size;
              }

              segment_copy = (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE);
              if (segment_size < true_size ||
                  heap->real_size + segment_size - segment_copy->size > heap->limit) {
                     if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
                            zend_mm_add_to_free_list(heap, (zend_mm_free_block *) next_block);
                     }
#if ZEND_MM_CACHE
                     zend_mm_free_cache(heap);
#endif
                     HANDLE_UNBLOCK_INTERRUPTIONS();
#if ZEND_DEBUG
                     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);
#else
                     zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %ld bytes)", heap->limit, size);
#endif
                     return NULL;
              }

              segment = ZEND_MM_STORAGE_REALLOC(segment_copy, segment_size);
              if (!segment) {
#if ZEND_MM_CACHE
                     zend_mm_free_cache(heap);
#endif
                     HANDLE_UNBLOCK_INTERRUPTIONS();
out_of_memory:
#if ZEND_DEBUG
                     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);
#else
                     zend_mm_safe_error(heap, "Out of memory (allocated %ld) (tried to allocate %ld bytes)", heap->real_size, size);
#endif
                     return NULL;
              }
              heap->real_size += segment_size - segment->size;
              if (heap->real_size > heap->real_peak) {
                     heap->real_peak = heap->real_size;
              }

              segment->size = segment_size;

              if (segment != segment_copy) {
                     zend_mm_segment **seg = &heap->segments_list;
                     while (*seg != segment_copy) {
                            seg = &(*seg)->next_segment;
                     }
                     *seg = segment;
                     mm_block = (zend_mm_block *) ((char *) segment + ZEND_MM_ALIGNED_SEGMENT_SIZE);
                     ZEND_MM_MARK_FIRST_BLOCK(mm_block);
              }

              block_size = segment_size - ZEND_MM_ALIGNED_SEGMENT_SIZE - ZEND_MM_ALIGNED_HEADER_SIZE;
              remaining_size = block_size - true_size;

              /* setup guard block */
              ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(mm_block, block_size));

              if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
                     true_size = block_size;
                     ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
              } else {
                     zend_mm_free_block *new_free_block;

                     /* prepare new free block */
                     ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
                     new_free_block = (zend_mm_free_block *) ZEND_MM_BLOCK_AT(mm_block, true_size);
                     ZEND_MM_BLOCK(new_free_block, ZEND_MM_FREE_BLOCK, remaining_size);

                     /* add the new free block to the free list */
                     zend_mm_add_to_rest_list(heap, new_free_block);
              }

              ZEND_MM_SET_DEBUG_INFO(mm_block, size, 1, 1);

              heap->size = heap->size + true_size - orig_size;
              if (heap->peak < heap->size) {
                     heap->peak = heap->size;
              }

              HANDLE_UNBLOCK_INTERRUPTIONS();
              return ZEND_MM_DATA_OF(mm_block);
       }

       ptr = _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
#if ZEND_DEBUG || ZEND_MM_HEAP_PROTECTION
       memcpy(ptr, p, mm_block->debug.size);
#else
       memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
#endif
       _zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
       return ptr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void alloc_globals_ctor ( zend_alloc_globals *alloc_globals  TSRMLS_DC) [static]

Definition at line 2565 of file zend_alloc.c.

{
       char *tmp;
       alloc_globals->mm_heap = zend_mm_startup();

       tmp = getenv("USE_ZEND_ALLOC");
       if (tmp) {
              alloc_globals->mm_heap->use_zend_alloc = zend_atoi(tmp, 0);
              if (!alloc_globals->mm_heap->use_zend_alloc) {
                     alloc_globals->mm_heap->_malloc = malloc;
                     alloc_globals->mm_heap->_free = free;
                     alloc_globals->mm_heap->_realloc = realloc;
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2338 of file zend_alloc.c.

{
       return AG(mm_heap)->use_zend_alloc;
}

Here is the caller graph for this function:

static size_t safe_address ( size_t  nmemb,
size_t  size,
size_t  offset 
) [inline, static]

Definition at line 2437 of file zend_alloc.c.

{
       size_t res = nmemb * size + offset;
       double _d  = (double)nmemb * (double)size + (double)offset;
       double _delta = (double)res - _d;

       if (UNEXPECTED((_d + _delta ) != _d)) {
              zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", nmemb, size, offset);
              return 0;
       }
       return res;
}

Here is the caller graph for this function:

ZEND_API void shutdown_memory_manager ( int  silent,
int full_shutdown  TSRMLS_DC 
)

Definition at line 2560 of file zend_alloc.c.

{
       zend_mm_shutdown(AG(mm_heap), full_shutdown, silent TSRMLS_CC);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2588 of file zend_alloc.c.

{
#ifdef ZTS
       ts_allocate_id(&alloc_globals_id, sizeof(zend_alloc_globals), (ts_allocate_ctor) alloc_globals_ctor, (ts_allocate_dtor) alloc_globals_dtor);
#else
       alloc_globals_ctor(&alloc_globals);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API size_t zend_memory_peak_usage ( int real_usage  TSRMLS_DC)

Definition at line 2551 of file zend_alloc.c.

{
       if (real_usage) {
              return AG(mm_heap)->real_peak;
       } else {
              return AG(mm_heap)->peak;
       }
}

Here is the caller graph for this function:

ZEND_API size_t zend_memory_usage ( int real_usage  TSRMLS_DC)

Definition at line 2538 of file zend_alloc.c.

{
       if (real_usage) {
              return AG(mm_heap)->real_size;
       } else {
              size_t usage = AG(mm_heap)->size;
#if ZEND_MM_CACHE
              usage -= AG(mm_heap)->cached;
#endif
              return usage;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void zend_mm_add_to_free_list ( zend_mm_heap *  heap,
zend_mm_free_block mm_block 
) [inline, static]

Definition at line 735 of file zend_alloc.c.

{
       size_t size;
       size_t index;

       ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);

       size = ZEND_MM_FREE_BLOCK_SIZE(mm_block);
       if (EXPECTED(!ZEND_MM_SMALL_SIZE(size))) {
              zend_mm_free_block **p;

              index = ZEND_MM_LARGE_BUCKET_INDEX(size);
              p = &heap->large_free_buckets[index];
              mm_block->child[0] = mm_block->child[1] = NULL;
              if (!*p) {
                     *p = mm_block;
                     mm_block->parent = p;
                     mm_block->prev_free_block = mm_block->next_free_block = mm_block;
                     heap->large_free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
              } else {
                     size_t m;

                     for (m = size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
                            zend_mm_free_block *prev = *p;

                            if (ZEND_MM_FREE_BLOCK_SIZE(prev) != size) {
                                   p = &prev->child[(m >> (ZEND_MM_NUM_BUCKETS-1)) & 1];
                                   if (!*p) {
                                          *p = mm_block;
                                          mm_block->parent = p;
                                          mm_block->prev_free_block = mm_block->next_free_block = mm_block;
                                          break;
                                   }
                            } else {
                                   zend_mm_free_block *next = prev->next_free_block;

                                   prev->next_free_block = next->prev_free_block = mm_block;
                                   mm_block->next_free_block = next;
                                   mm_block->prev_free_block = prev;
                                   mm_block->parent = NULL;
                                   break;
                            }
                     }
              }
       } else {
              zend_mm_free_block *prev, *next;

              index = ZEND_MM_BUCKET_INDEX(size);

              prev = ZEND_MM_SMALL_FREE_BUCKET(heap, index);
              if (prev->prev_free_block == prev) {
                     heap->free_bitmap |= (ZEND_MM_LONG_CONST(1) << index);
              }
              next = prev->next_free_block;

              mm_block->prev_free_block = prev;
              mm_block->next_free_block = next;
              prev->next_free_block = next->prev_free_block = mm_block;
       }
}

Here is the caller graph for this function:

static void zend_mm_add_to_rest_list ( zend_mm_heap *  heap,
zend_mm_free_block mm_block 
) [inline, static]

Definition at line 718 of file zend_alloc.c.

{
       zend_mm_free_block *prev, *next;

       ZEND_MM_SET_MAGIC(mm_block, MEM_BLOCK_FREED);

       if (!ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block))) {
              mm_block->parent = NULL;
       }

       prev = heap->rest_buckets[0];
       next = prev->next_free_block;
       mm_block->prev_free_block = prev;
       mm_block->next_free_block = next;
       prev->next_free_block = next->prev_free_block = mm_block;
}

Here is the caller graph for this function:

static void zend_mm_del_segment ( zend_mm_heap *  heap,
zend_mm_segment segment 
) [static]

Definition at line 893 of file zend_alloc.c.

{
       zend_mm_segment **p = &heap->segments_list;

       while (*p != segment) {
              p = &(*p)->next_segment;
       }
       *p = segment->next_segment;
       heap->real_size -= segment->size;
       ZEND_MM_STORAGE_FREE(segment);
}

Here is the caller graph for this function:

static void zend_mm_free_cache ( zend_mm_heap *  heap) [static]

Definition at line 906 of file zend_alloc.c.

{
       int i;

       for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
              if (heap->cache[i]) {
                     zend_mm_free_block *mm_block = heap->cache[i];

                     while (mm_block) {
                            size_t size = ZEND_MM_BLOCK_SIZE(mm_block);
                            zend_mm_free_block *q = mm_block->prev_free_block;
                            zend_mm_block *next_block = ZEND_MM_NEXT_BLOCK(mm_block);

                            heap->cached -= size;

                            if (ZEND_MM_PREV_BLOCK_IS_FREE(mm_block)) {
                                   mm_block = (zend_mm_free_block*)ZEND_MM_PREV_BLOCK(mm_block);
                                   size += ZEND_MM_FREE_BLOCK_SIZE(mm_block);
                                   zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) mm_block);
                            }
                            if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
                                   size += ZEND_MM_FREE_BLOCK_SIZE(next_block);
                                   zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
                            }
                            ZEND_MM_BLOCK(mm_block, ZEND_MM_FREE_BLOCK, size);

                            if (ZEND_MM_IS_FIRST_BLOCK(mm_block) &&
                                ZEND_MM_IS_GUARD_BLOCK(ZEND_MM_NEXT_BLOCK(mm_block))) {
                                   zend_mm_del_segment(heap, (zend_mm_segment *) ((char *)mm_block - ZEND_MM_ALIGNED_SEGMENT_SIZE));
                            } else {
                                   zend_mm_add_to_free_list(heap, (zend_mm_free_block *) mm_block);
                            }

                            mm_block = q;
                     }
                     heap->cache[i] = NULL;
#if ZEND_MM_CACHE_STAT
                     heap->cache_stat[i].count = 0;
#endif
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API zend_mm_storage* zend_mm_get_storage ( zend_mm_heap *  heap)

Definition at line 2606 of file zend_alloc.c.

{
       return heap->storage;
}
static unsigned int zend_mm_high_bit ( size_t  _size) [inline, static]

Definition at line 660 of file zend_alloc.c.

{
#if defined(__GNUC__) && defined(i386)
       unsigned int n;

       __asm__("bsrl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
       return n;
#elif defined(__GNUC__) && defined(__x86_64__)
       unsigned long n;

        __asm__("bsrq %1,%0\n\t" : "=r" (n) : "rm"  (_size));
        return (unsigned int)n;
#elif defined(_MSC_VER) && defined(_M_IX86)
       __asm {
              bsr eax, _size
       }
#else
       unsigned int n = 0;
       while (_size != 0) {
              _size = _size >> 1;
              n++;
       }
       return n-1;
#endif
}

Here is the caller graph for this function:

static void zend_mm_init ( zend_mm_heap *  heap) [inline, static]

Definition at line 867 of file zend_alloc.c.

{
       zend_mm_free_block* p;
       int i;

       heap->free_bitmap = 0;
       heap->large_free_bitmap = 0;
#if ZEND_MM_CACHE
       heap->cached = 0;
       memset(heap->cache, 0, sizeof(heap->cache));
#endif
#if ZEND_MM_CACHE_STAT
       for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
              heap->cache_stat[i].count = 0;
       }
#endif
       p = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
       for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
              p->next_free_block = p;
              p->prev_free_block = p;
              p = (zend_mm_free_block*)((char*)p + sizeof(zend_mm_free_block*) * 2);
              heap->large_free_buckets[i] = NULL;
       }
       heap->rest_buckets[0] = heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(heap);
}

Here is the caller graph for this function:

static unsigned int zend_mm_low_bit ( size_t  _size) [inline, static]

Definition at line 686 of file zend_alloc.c.

{
#if defined(__GNUC__) && defined(i386)
       unsigned int n;

       __asm__("bsfl %1,%0\n\t" : "=r" (n) : "rm"  (_size));
       return n;
#elif defined(__GNUC__) && defined(__x86_64__)
        unsigned long n;

        __asm__("bsfq %1,%0\n\t" : "=r" (n) : "rm"  (_size));
        return (unsigned int)n;
#elif defined(_MSC_VER) && defined(_M_IX86)
       __asm {
              bsf eax, _size
   }
#else
       static const int offset[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
       unsigned int n;
       unsigned int index = 0;

       n = offset[_size & 15];
       while (n == 4) {
              _size >>= 4;
              index += n;
              n = offset[_size & 15];
       }

       return index + n;
#endif
}

Here is the caller graph for this function:

static void zend_mm_mem_dummy_compact ( zend_mm_storage *  storage) [static]

Definition at line 147 of file zend_alloc.c.

{
}
static void zend_mm_mem_dummy_dtor ( zend_mm_storage *  storage) [static]

Definition at line 142 of file zend_alloc.c.

{
       free(storage);
}
static zend_mm_storage* zend_mm_mem_dummy_init ( void *  params) [static]

Definition at line 137 of file zend_alloc.c.

{
       return malloc(sizeof(zend_mm_storage));
}
static zend_mm_segment* zend_mm_mem_malloc_alloc ( zend_mm_storage *  storage,
size_t  size 
) [static]

Definition at line 285 of file zend_alloc.c.

{
       return (zend_mm_segment*)malloc(size);
}
static void zend_mm_mem_malloc_free ( zend_mm_storage *  storage,
zend_mm_segment ptr 
) [static]

Definition at line 295 of file zend_alloc.c.

{
       free(ptr);
}
static zend_mm_segment* zend_mm_mem_malloc_realloc ( zend_mm_storage *  storage,
zend_mm_segment ptr,
size_t  size 
) [static]

Definition at line 290 of file zend_alloc.c.

{
       return (zend_mm_segment*)realloc(ptr, size);
}
static void zend_mm_panic ( const char *  message) [static]

Definition at line 84 of file zend_alloc.c.

{
       fprintf(stderr, "%s\n", message);
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
       fflush(stderr);
#endif
#if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
       kill(getpid(), SIGSEGV);
#endif
       exit(1);
}

Here is the caller graph for this function:

static void zend_mm_remove_from_free_list ( zend_mm_heap *  heap,
zend_mm_free_block mm_block 
) [inline, static]

Definition at line 796 of file zend_alloc.c.

{
       zend_mm_free_block *prev = mm_block->prev_free_block;
       zend_mm_free_block *next = mm_block->next_free_block;

       ZEND_MM_CHECK_MAGIC(mm_block, MEM_BLOCK_FREED);

       if (EXPECTED(prev == mm_block)) {
              zend_mm_free_block **rp, **cp;

#if ZEND_MM_SAFE_UNLINKING
              if (UNEXPECTED(next != mm_block)) {
                     zend_mm_panic("zend_mm_heap corrupted");
              }
#endif

              rp = &mm_block->child[mm_block->child[1] != NULL];
              prev = *rp;
              if (EXPECTED(prev == NULL)) {
                     size_t index = ZEND_MM_LARGE_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));

                     ZEND_MM_CHECK_TREE(mm_block);
                     *mm_block->parent = NULL;
                     if (mm_block->parent == &heap->large_free_buckets[index]) {
                            heap->large_free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
                  }
              } else {
                     while (*(cp = &(prev->child[prev->child[1] != NULL])) != NULL) {
                            prev = *cp;
                            rp = cp;
                     }
                     *rp = NULL;

subst_block:
                     ZEND_MM_CHECK_TREE(mm_block);
                     *mm_block->parent = prev;
                     prev->parent = mm_block->parent;
                     if ((prev->child[0] = mm_block->child[0])) {
                            ZEND_MM_CHECK_TREE(prev->child[0]);
                            prev->child[0]->parent = &prev->child[0];
                     }
                     if ((prev->child[1] = mm_block->child[1])) {
                            ZEND_MM_CHECK_TREE(prev->child[1]);
                            prev->child[1]->parent = &prev->child[1];
                     }
              }
       } else {

#if ZEND_MM_SAFE_UNLINKING
              if (UNEXPECTED(prev->next_free_block != mm_block) || UNEXPECTED(next->prev_free_block != mm_block)) {
                     zend_mm_panic("zend_mm_heap corrupted");
              }
#endif

              prev->next_free_block = next;
              next->prev_free_block = prev;

              if (EXPECTED(ZEND_MM_SMALL_SIZE(ZEND_MM_FREE_BLOCK_SIZE(mm_block)))) {
                     if (EXPECTED(prev == next)) {
                            size_t index = ZEND_MM_BUCKET_INDEX(ZEND_MM_FREE_BLOCK_SIZE(mm_block));

                            if (EXPECTED(heap->free_buckets[index*2] == heap->free_buckets[index*2+1])) {
                                   heap->free_bitmap &= ~(ZEND_MM_LONG_CONST(1) << index);
                            }
                     }
              } else if (UNEXPECTED(mm_block->parent != NULL)) {
                     goto subst_block;
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void zend_mm_safe_error ( zend_mm_heap *  heap,
const char *  format,
size_t  limit,
size_t  size 
) [static]

Definition at line 1673 of file zend_alloc.c.

{
       if (heap->reserve) {
              _zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
              heap->reserve = NULL;
       }
       if (heap->overflow == 0) {
              char *error_filename;
              uint error_lineno;
              TSRMLS_FETCH();
              if (zend_is_compiling(TSRMLS_C)) {
                     error_filename = zend_get_compiled_filename(TSRMLS_C);
                     error_lineno = zend_get_compiled_lineno(TSRMLS_C);
              } else if (EG(in_execution)) {
                     error_filename = EG(active_op_array)?EG(active_op_array)->filename:NULL;
                     error_lineno = EG(opline_ptr)?(*EG(opline_ptr))->lineno:0;
              } else {
                     error_filename = NULL;
                     error_lineno = 0;
              }
              if (!error_filename) {
                     error_filename = "Unknown";
              }
              heap->overflow = 1;
              zend_try {
                     zend_error_noreturn(E_ERROR,
                            format,
                            limit,
#if ZEND_DEBUG
                            filename,
                            lineno,
#endif
                            size);
              } zend_catch {
                     if (heap->overflow == 2) {
                            fprintf(stderr, "\nFatal error: ");
                            fprintf(stderr,
                                   format,
                                   limit,
#if ZEND_DEBUG
                                   filename,
                                   lineno,
#endif
                                   size);
                            fprintf(stderr, " in %s on line %d\n", error_filename, error_lineno);
                     }
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
                     fflush(stderr);
#endif
              } zend_end_try();
       } else {
              heap->overflow = 2;
       }
       zend_bailout();
}

Here is the call graph for this function:

Here is the caller graph for this function:

static zend_mm_free_block* zend_mm_search_large_block ( zend_mm_heap *  heap,
size_t  true_size 
) [static]

Definition at line 1737 of file zend_alloc.c.

{
       zend_mm_free_block *best_fit;
       size_t index = ZEND_MM_LARGE_BUCKET_INDEX(true_size);
       size_t bitmap = heap->large_free_bitmap >> index;
       zend_mm_free_block *p;

       if (bitmap == 0) {
              return NULL;
       }

       if (UNEXPECTED((bitmap & 1) != 0)) {
              /* Search for best "large" free block */
              zend_mm_free_block *rst = NULL;
              size_t m;
              size_t best_size = -1;

              best_fit = NULL;
              p = heap->large_free_buckets[index];
              for (m = true_size << (ZEND_MM_NUM_BUCKETS - index); ; m <<= 1) {
                     if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
                            return p->next_free_block;
                     } else if (ZEND_MM_FREE_BLOCK_SIZE(p) >= true_size &&
                                ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
                            best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
                            best_fit = p;
                     }
                     if ((m & (ZEND_MM_LONG_CONST(1) << (ZEND_MM_NUM_BUCKETS-1))) == 0) {
                            if (p->child[1]) {
                                   rst = p->child[1];
                            }
                            if (p->child[0]) {
                                   p = p->child[0];
                            } else {
                                   break;
                            }
                     } else if (p->child[1]) {
                            p = p->child[1];
                     } else {
                            break;
                     }
              }

              for (p = rst; p; p = p->child[p->child[0] != NULL]) {
                     if (UNEXPECTED(ZEND_MM_FREE_BLOCK_SIZE(p) == true_size)) {
                            return p->next_free_block;
                     } else if (ZEND_MM_FREE_BLOCK_SIZE(p) > true_size &&
                                ZEND_MM_FREE_BLOCK_SIZE(p) < best_size) {
                            best_size = ZEND_MM_FREE_BLOCK_SIZE(p);
                            best_fit = p;
                     }
              }

              if (best_fit) {
                     return best_fit->next_free_block;
              }
              bitmap = bitmap >> 1;
              if (!bitmap) {
                     return NULL;
              }
              index++;
       }

       /* Search for smallest "large" free block */
       best_fit = p = heap->large_free_buckets[index + zend_mm_low_bit(bitmap)];
       while ((p = p->child[p->child[0] != NULL])) {
              if (ZEND_MM_FREE_BLOCK_SIZE(p) < ZEND_MM_FREE_BLOCK_SIZE(best_fit)) {
                     best_fit = p;
              }
       }
       return best_fit->next_free_block;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API void zend_mm_set_custom_handlers ( zend_mm_heap *  heap,
void *(*)(size_t _malloc,
void(*)(void *)  _free,
void *(*)(void *, size_t _realloc 
)

Definition at line 2611 of file zend_alloc.c.

{
       heap->use_zend_alloc = 0;
       heap->_malloc = _malloc;
       heap->_free = _free;
       heap->_realloc = _realloc;
}
ZEND_API zend_mm_heap* zend_mm_set_heap ( zend_mm_heap *new_heap  TSRMLS_DC)

Definition at line 2597 of file zend_alloc.c.

{
       zend_mm_heap *old_heap;

       old_heap = AG(mm_heap);
       AG(mm_heap) = new_heap;
       return old_heap;
}
ZEND_API void zend_mm_shutdown ( zend_mm_heap *  heap,
int  full_shutdown,
int silent  TSRMLS_DC 
)

Definition at line 1574 of file zend_alloc.c.

{
       zend_mm_storage *storage;
       zend_mm_segment *segment;
       zend_mm_segment *prev;
       int internal;

       if (heap->reserve) {
#if ZEND_DEBUG
              if (!silent) {
                     _zend_mm_free_int(heap, heap->reserve ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
              }
#endif
              heap->reserve = NULL;
       }

#if ZEND_MM_CACHE_STAT
       if (full_shutdown) {
              FILE *f;

              f = fopen("zend_mm.log", "w");
              if (f) {
                     int i,j;
                     size_t size, true_size, min_size, max_size;
                     int hit = 0, miss = 0;

                     fprintf(f, "\nidx min_size max_size true_size  max_len     hits   misses\n");
                     size = 0;
                     while (1) {
                            true_size = ZEND_MM_TRUE_SIZE(size);
                            if (ZEND_MM_SMALL_SIZE(true_size)) {
                                   min_size = size;
                                   i = ZEND_MM_BUCKET_INDEX(true_size);
                                   size++;
                                   while (1) {
                                          true_size = ZEND_MM_TRUE_SIZE(size);
                                          if (ZEND_MM_SMALL_SIZE(true_size)) {
                                                 j = ZEND_MM_BUCKET_INDEX(true_size);
                                                 if (j > i) {
                                                        max_size = size-1;
                                                        break;
                                                 }
                                          } else {
                                                 max_size = size-1;
                                                 break;
                                          }
                                          size++;
                                   }
                                   hit += heap->cache_stat[i].hit;
                                   miss += heap->cache_stat[i].miss;
                                   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);
                            } else {
                                   break;
                            }
                     }
                     fprintf(f, "                                        %8d %8d\n", hit, miss);
                     fprintf(f, "                                        %8d %8d\n", heap->cache_stat[ZEND_MM_NUM_BUCKETS].hit, heap->cache_stat[ZEND_MM_NUM_BUCKETS].miss);
                     fclose(f);
              }
       }
#endif

#if ZEND_DEBUG
       if (!silent) {
              zend_mm_check_leaks(heap TSRMLS_CC);
       }
#endif

       internal = heap->internal;
       storage = heap->storage;
       segment = heap->segments_list;
       while (segment) {
              prev = segment;
              segment = segment->next_segment;
              ZEND_MM_STORAGE_FREE(prev);
       }
       if (full_shutdown) {
              storage->handlers->dtor(storage);
              if (!internal) {
                     free(heap);
              }
       } else {
              if (heap->compact_size &&
                  heap->real_peak > heap->compact_size) {
                     storage->handlers->compact(storage);
              }
              heap->segments_list = NULL;
              zend_mm_init(heap);
              heap->real_size = 0;
              heap->real_peak = 0;
              heap->size = 0;
              heap->peak = 0;
              if (heap->reserve_size) {
                     heap->reserve = _zend_mm_alloc_int(heap, heap->reserve_size  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
              }
              heap->overflow = 0;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

ZEND_API zend_mm_heap* zend_mm_startup ( void  )

Definition at line 1136 of file zend_alloc.c.

{
       int i;
       size_t seg_size;
       char *mem_type = getenv("ZEND_MM_MEM_TYPE");
       char *tmp;
       const zend_mm_mem_handlers *handlers;
       zend_mm_heap *heap;

       if (mem_type == NULL) {
              i = 0;
       } else {
              for (i = 0; mem_handlers[i].name; i++) {
                     if (strcmp(mem_handlers[i].name, mem_type) == 0) {
                            break;
                     }
              }
              if (!mem_handlers[i].name) {
                     fprintf(stderr, "Wrong or unsupported zend_mm storage type '%s'\n", mem_type);
                     fprintf(stderr, "  supported types:\n");
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
                     fflush(stderr);
#endif
                     for (i = 0; mem_handlers[i].name; i++) {
                            fprintf(stderr, "    '%s'\n", mem_handlers[i].name);
                     }
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
                     fflush(stderr);
#endif
                     exit(255);
              }
       }
       handlers = &mem_handlers[i];

       tmp = getenv("ZEND_MM_SEG_SIZE");
       if (tmp) {
              seg_size = zend_atoi(tmp, 0);
              if (zend_mm_low_bit(seg_size) != zend_mm_high_bit(seg_size)) {
                     fprintf(stderr, "ZEND_MM_SEG_SIZE must be a power of two\n");
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
                     fflush(stderr);
#endif
                     exit(255);
              } else if (seg_size < ZEND_MM_ALIGNED_SEGMENT_SIZE + ZEND_MM_ALIGNED_HEADER_SIZE) {
                     fprintf(stderr, "ZEND_MM_SEG_SIZE is too small\n");
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
                     fflush(stderr);
#endif
                     exit(255);
              }
       } else {
              seg_size = ZEND_MM_SEG_SIZE;
       }

       heap = zend_mm_startup_ex(handlers, seg_size, ZEND_MM_RESERVE_SIZE, 0, NULL);
       if (heap) {
              tmp = getenv("ZEND_MM_COMPACT");
              if (tmp) {
                     heap->compact_size = zend_atoi(tmp, 0);
              } else {
                     heap->compact_size = 2 * 1024 * 1024;
              }
       }
       return heap;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 
)

Definition at line 1016 of file zend_alloc.c.

{
       zend_mm_storage *storage;
       zend_mm_heap    *heap;

#if 0
       int i;

       printf("ZEND_MM_ALIGNMENT=%d\n", ZEND_MM_ALIGNMENT);
       printf("ZEND_MM_ALIGNMENT_LOG2=%d\n", ZEND_MM_ALIGNMENT_LOG2);
       printf("ZEND_MM_MIN_SIZE=%d\n", ZEND_MM_MIN_SIZE);
       printf("ZEND_MM_MAX_SMALL_SIZE=%d\n", ZEND_MM_MAX_SMALL_SIZE);
       printf("ZEND_MM_ALIGNED_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_HEADER_SIZE);
       printf("ZEND_MM_ALIGNED_FREE_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_FREE_HEADER_SIZE);
       printf("ZEND_MM_MIN_ALLOC_BLOCK_SIZE=%d\n", ZEND_MM_MIN_ALLOC_BLOCK_SIZE);
       printf("ZEND_MM_ALIGNED_MIN_HEADER_SIZE=%d\n", ZEND_MM_ALIGNED_MIN_HEADER_SIZE);
       printf("ZEND_MM_ALIGNED_SEGMENT_SIZE=%d\n", ZEND_MM_ALIGNED_SEGMENT_SIZE);
       for (i = 0; i < ZEND_MM_MAX_SMALL_SIZE; i++) {
              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)));
       }
       exit(0);
#endif

#if ZEND_MM_HEAP_PROTECTION
       if (_mem_block_start_magic == 0) {
              zend_mm_random((unsigned char*)&_mem_block_start_magic, sizeof(_mem_block_start_magic));
       }
       if (_mem_block_end_magic == 0) {
              zend_mm_random((unsigned char*)&_mem_block_end_magic, sizeof(_mem_block_end_magic));
       }
#endif
#if ZEND_MM_COOKIES
       if (_zend_mm_cookie == 0) {
              zend_mm_random((unsigned char*)&_zend_mm_cookie, sizeof(_zend_mm_cookie));
       }
#endif

       if (zend_mm_low_bit(block_size) != zend_mm_high_bit(block_size)) {
              fprintf(stderr, "'block_size' must be a power of two\n");
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
              fflush(stderr);
#endif
              exit(255);
       }
       storage = handlers->init(params);
       if (!storage) {
              fprintf(stderr, "Cannot initialize zend_mm storage [%s]\n", handlers->name);
/* See http://support.microsoft.com/kb/190351 */
#ifdef PHP_WIN32
              fflush(stderr);
#endif
              exit(255);
       }
       storage->handlers = handlers;

       heap = malloc(sizeof(struct _zend_mm_heap));
       if (heap == NULL) {
              fprintf(stderr, "Cannot allocate heap for zend_mm storage [%s]\n", handlers->name);
#ifdef PHP_WIN32
              fflush(stderr);
#endif
              exit(255);
       }
       heap->storage = storage;
       heap->block_size = block_size;
       heap->compact_size = 0;
       heap->segments_list = NULL;
       zend_mm_init(heap);
# if ZEND_MM_CACHE_STAT
       memset(heap->cache_stat, 0, sizeof(heap->cache_stat));
# endif

       heap->use_zend_alloc = 1;
       heap->real_size = 0;
       heap->overflow = 0;
       heap->real_peak = 0;
       heap->limit = ZEND_MM_LONG_CONST(1)<<(ZEND_MM_NUM_BUCKETS-2);
       heap->size = 0;
       heap->peak = 0;
       heap->internal = internal;
       heap->reserve = NULL;
       heap->reserve_size = reserve_size;
       if (reserve_size > 0) {
              heap->reserve = _zend_mm_alloc_int(heap, reserve_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
       }
       if (internal) {
              int i;
              zend_mm_free_block *p, *q, *orig;
              zend_mm_heap *mm_heap = _zend_mm_alloc_int(heap, sizeof(zend_mm_heap)  ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);

              *mm_heap = *heap;

              p = ZEND_MM_SMALL_FREE_BUCKET(mm_heap, 0);
              orig = ZEND_MM_SMALL_FREE_BUCKET(heap, 0);
              for (i = 0; i < ZEND_MM_NUM_BUCKETS; i++) {
                     q = p;
                     while (q->prev_free_block != orig) {
                            q = q->prev_free_block;
                     }
                     q->prev_free_block = p;
                     q = p;
                     while (q->next_free_block != orig) {
                            q = q->next_free_block;
                     }
                     q->next_free_block = p;
                     p = (zend_mm_free_block*)((char*)p + sizeof(zend_mm_free_block*) * 2);
                     orig = (zend_mm_free_block*)((char*)orig + sizeof(zend_mm_free_block*) * 2);
                     if (mm_heap->large_free_buckets[i]) {
                            mm_heap->large_free_buckets[i]->parent = &mm_heap->large_free_buckets[i];
                     }
              }
              mm_heap->rest_buckets[0] = mm_heap->rest_buckets[1] = ZEND_MM_REST_BUCKET(mm_heap);

              free(heap);
              heap = mm_heap;
       }
       return heap;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 2529 of file zend_alloc.c.

{
       TSRMLS_FETCH();

       AG(mm_heap)->limit = (memory_limit >= AG(mm_heap)->block_size) ? memory_limit : AG(mm_heap)->block_size;

       return SUCCESS;
}

Here is the caller graph for this function:

ZEND_API char* zend_strndup ( const char *  s,
uint  length 
)

Definition at line 2513 of file zend_alloc.c.

{
       char *p;

       p = (char *) malloc(length+1);
       if (UNEXPECTED(p == NULL)) {
              return p;
       }
       if (length) {
              memcpy(p, s, length);
       }
       p[length] = 0;
       return p;
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 2335 of file zend_alloc.c.

Initial value:

Definition at line 304 of file zend_alloc.c.