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

LCOV - code coverage report
Current view: top level - Zend - zend_alloc.h (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 9 13 69.2 %
Date: 2014-10-30 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 2.00 of the Zend license,     |
       8             :    | that is bundled with this package in the file LICENSE, and is        | 
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.zend.com/license/2_00.txt.                                |
      11             :    | If you did not receive a copy of the Zend license and are unable to  |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@zend.com so we can mail you a copy immediately.              |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #ifndef ZEND_ALLOC_H
      23             : #define ZEND_ALLOC_H
      24             : 
      25             : #include <stdio.h>
      26             : 
      27             : #include "../TSRM/TSRM.h"
      28             : #include "zend.h"
      29             : 
      30             : #ifndef ZEND_MM_ALIGNMENT
      31             : # define ZEND_MM_ALIGNMENT Z_L(8)
      32             : # define ZEND_MM_ALIGNMENT_LOG2 Z_L(3)
      33             : #elif ZEND_MM_ALIGNMENT < 4
      34             : # undef ZEND_MM_ALIGNMENT
      35             : # undef ZEND_MM_ALIGNMENT_LOG2
      36             : # define ZEND_MM_ALIGNMENT Z_L(4)
      37             : # define ZEND_MM_ALIGNMENT_LOG2 Z_L(2)
      38             : #endif
      39             : 
      40             : #define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT - Z_L(1))
      41             : 
      42             : #define ZEND_MM_ALIGNED_SIZE(size)      (((size) + ZEND_MM_ALIGNMENT - Z_L(1)) & ZEND_MM_ALIGNMENT_MASK)
      43             : 
      44             : typedef struct _zend_leak_info {
      45             :         void *addr;
      46             :         size_t size;
      47             :         const char *filename;
      48             :         const char *orig_filename;
      49             :         uint lineno;
      50             :         uint orig_lineno;
      51             : } zend_leak_info;
      52             : 
      53             : BEGIN_EXTERN_C()
      54             : 
      55             : ZEND_API char*  ZEND_FASTCALL zend_strndup(const char *s, size_t length) ZEND_ATTRIBUTE_MALLOC;
      56             : 
      57             : ZEND_API void*  ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
      58             : ZEND_API void*  ZEND_FASTCALL _safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
      59             : ZEND_API void*  ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
      60             : ZEND_API void   ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
      61             : ZEND_API void*  ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
      62             : ZEND_API void*  ZEND_FASTCALL _erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
      63             : ZEND_API void*  ZEND_FASTCALL _safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
      64             : ZEND_API void*  ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
      65             : ZEND_API char*  ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
      66             : ZEND_API char*  ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
      67             : ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
      68             : 
      69             : #include "zend_alloc_sizes.h"
      70             : 
      71             : /* _emalloc() & _efree() specialization */
      72             : #if !ZEND_DEBUG && !defined(_WIN32)
      73             : 
      74             : # define _ZEND_BIN_ALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
      75             :         ZEND_API void* ZEND_FASTCALL _emalloc_  ## _size(void) ZEND_ATTRIBUTE_MALLOC;
      76             : 
      77             : ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_DEF, x, y)
      78             : 
      79             : ZEND_API void* ZEND_FASTCALL _emalloc_large(size_t size) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
      80             : ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
      81             : 
      82             : # define _ZEND_BIN_ALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, size, y) \
      83             :         ((size <= _size) ? _emalloc_ ## _size() :
      84             : # define _ZEND_BIN_ALLOCATOR_SELECTOR_END(_num, _size, _elements, _pages, size, y) \
      85             :         )
      86             : 
      87             : # define ZEND_ALLOCATOR(size) \
      88             :         ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_SELECTOR_START, size, y) \
      89             :         ((size <= ZEND_MM_MAX_LARGE_SIZE) ? _emalloc_large(size) : _emalloc_huge(size)) \
      90             :         ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_SELECTOR_END, size, y)
      91             : 
      92             : # define _emalloc(size) \
      93             :         (__builtin_constant_p(size) ? \
      94             :                 ZEND_ALLOCATOR(size) \
      95             :         : \
      96             :                 _emalloc(size) \
      97             :         )                               
      98             : 
      99             : # define _ZEND_BIN_DEALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
     100             :         ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *);
     101             : 
     102             : ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_DEF, x, y)
     103             : 
     104             : ZEND_API void ZEND_FASTCALL _efree_large(void *, size_t size);
     105             : ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size);
     106             : 
     107             : # define _ZEND_BIN_DEALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, ptr, size) \
     108             :         if (size <= _size) { _efree_ ## _size(ptr); } else
     109             : 
     110             : # define ZEND_DEALLOCATOR(ptr, size) \
     111             :         ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_SELECTOR_START, ptr, size) \
     112             :         if (size <= ZEND_MM_MAX_LARGE_SIZE) { _efree_large(ptr, size); } \
     113             :         else { _efree_huge(ptr, size); }
     114             : 
     115             : # define efree_size(ptr, size) do { \
     116             :                 if (__builtin_constant_p(size)) { \
     117             :                         ZEND_DEALLOCATOR(ptr, size) \
     118             :                 } else { \
     119             :                         _efree(ptr); \
     120             :                 } \
     121             :         } while (0)
     122             : # define efree_size_rel(ptr, size) \
     123             :         efree_size(ptr, size)
     124             : 
     125             : #else
     126             : 
     127             : # define efree_size(ptr, size) \
     128             :         efree(ptr)
     129             : # define efree_size_rel(ptr, size) \
     130             :         efree_rel(ptr)
     131             : 
     132             : #define _emalloc_large _emalloc
     133             : #define _emalloc_huge  _emalloc
     134             : #define _efree_large   _efree
     135             : #define _efree_huge    _efree
     136             : 
     137             : #endif
     138             : 
     139             : /* Standard wrapper macros */
     140             : #define emalloc(size)                                           _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     141             : #define emalloc_large(size)                                     _emalloc_large((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     142             : #define emalloc_huge(size)                                      _emalloc_huge((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     143             : #define safe_emalloc(nmemb, size, offset)       _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     144             : #define efree(ptr)                                                      _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     145             : #define efree_large(ptr)                                        _efree_large((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     146             : #define efree_huge(ptr)                                         _efree_huge((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     147             : #define ecalloc(nmemb, size)                            _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     148             : #define erealloc(ptr, size)                                     _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     149             : #define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     150             : #define erealloc_recoverable(ptr, size)         _erealloc((ptr), (size), 1 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     151             : #define estrdup(s)                                                      _estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     152             : #define estrndup(s, length)                                     _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     153             : #define zend_mem_block_size(ptr)                        _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     154             : 
     155             : /* Relay wrapper macros */
     156             : #define emalloc_rel(size)                                               _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     157             : #define safe_emalloc_rel(nmemb, size, offset)   _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     158             : #define efree_rel(ptr)                                                  _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     159             : #define ecalloc_rel(nmemb, size)                                _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     160             : #define erealloc_rel(ptr, size)                                 _erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     161             : #define erealloc_recoverable_rel(ptr, size)             _erealloc((ptr), (size), 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     162             : #define safe_erealloc_rel(ptr, nmemb, size, offset)     _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     163             : #define estrdup_rel(s)                                                  _estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     164             : #define estrndup_rel(s, length)                                 _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     165             : #define zend_mem_block_size_rel(ptr)                    _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     166             : 
     167             : zend_always_inline static void * __zend_malloc(size_t len)
     168             : {
     169   401256947 :         void *tmp = malloc(len);
     170   401256947 :         if (tmp) {
     171   401256947 :                 return tmp;
     172             :         }
     173           0 :         fprintf(stderr, "Out of memory\n");
     174           0 :         exit(1);
     175             : }
     176             : 
     177             : zend_always_inline static void * __zend_calloc(size_t nmemb, size_t len)
     178             : {
     179      128541 :         void *tmp = _safe_malloc(nmemb, len, 0);
     180      128541 :         memset(tmp, 0, nmemb * len);
     181      128541 :         return tmp;
     182             : }
     183             : 
     184             : zend_always_inline static void * __zend_realloc(void *p, size_t len)
     185             : {
     186     8234318 :         p = realloc(p, len);
     187     8234318 :         if (p) {
     188     8234318 :                 return p;
     189             :         }
     190           0 :         fprintf(stderr, "Out of memory\n");
     191           0 :         exit(1);
     192             : }
     193             : 
     194             : 
     195             : /* Selective persistent/non persistent allocation macros */
     196             : #define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
     197             : #define safe_pemalloc(nmemb, size, offset, persistent)  ((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
     198             : #define pefree(ptr, persistent)  ((persistent)?free(ptr):efree(ptr))
     199             : #define pefree_size(ptr, size, persistent)  ((persistent)?free(ptr):efree_size(ptr, size))
     200             : #define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size)))
     201             : #define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size)))
     202             : #define safe_perealloc(ptr, nmemb, size, offset, persistent)    ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
     203             : #define perealloc_recoverable(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
     204             : #define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
     205             : #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
     206             : 
     207             : #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))
     208             : #define pefree_rel(ptr, persistent)     ((persistent)?free(ptr):efree_rel(ptr))
     209             : #define pecalloc_rel(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc_rel((nmemb), (size)))
     210             : #define perealloc_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_rel((ptr), (size)))
     211             : #define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
     212             : #define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
     213             : 
     214             : ZEND_API int zend_set_memory_limit(size_t memory_limit TSRMLS_DC);
     215             : 
     216             : ZEND_API void start_memory_manager(TSRMLS_D);
     217             : ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC);
     218             : ZEND_API int is_zend_mm(TSRMLS_D);
     219             : 
     220             : ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC);
     221             : ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC);
     222             : 
     223             : /* fast cache for HashTables */
     224             : #define ALLOC_HASHTABLE(ht)     \
     225             :         (ht) = (HashTable *) emalloc(sizeof(HashTable))
     226             : 
     227             : #define FREE_HASHTABLE(ht)      \
     228             :         efree_size(ht, sizeof(HashTable))
     229             : 
     230             : #define ALLOC_HASHTABLE_REL(ht) \
     231             :         (ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
     232             : 
     233             : #define FREE_HASHTABLE_REL(ht)  \
     234             :         efree_size_rel(ht, sizeof(HashTable))
     235             : 
     236             : /* Heap functions */
     237             : typedef struct _zend_mm_heap zend_mm_heap;
     238             : 
     239             : ZEND_API zend_mm_heap *zend_mm_startup(void);
     240             : ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC);
     241             : ZEND_API void*  ZEND_FASTCALL _zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
     242             : ZEND_API void   ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     243             : ZEND_API void*  ZEND_FASTCALL _zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     244             : ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
     245             : 
     246             : #define zend_mm_alloc(heap, size)                       _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     247             : #define zend_mm_free(heap, p)                           _zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     248             : #define zend_mm_realloc(heap, p, size)          _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     249             : #define zend_mm_block_size(heap, p)                     _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     250             : 
     251             : #define zend_mm_alloc_rel(heap, size)           _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     252             : #define zend_mm_free_rel(heap, p)                       _zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     253             : #define zend_mm_realloc_rel(heap, p, size)      _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
     254             : #define zend_mm_block_size_rel(heap, p)         _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
     255             : 
     256             : ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC);
     257             : 
     258             : ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
     259             :                                           void* (*_malloc)(size_t),
     260             :                                           void  (*_free)(void*),
     261             :                                           void* (*_realloc)(void*, size_t));
     262             : ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
     263             :                                           void* (**_malloc)(size_t),
     264             :                                           void  (**_free)(void*),
     265             :                                           void* (**_realloc)(void*, size_t));
     266             : 
     267             : typedef struct _zend_mm_storage zend_mm_storage;
     268             : 
     269             : typedef void* (*zend_mm_chunk_alloc_t)(zend_mm_storage *storage, size_t size, size_t alignment);
     270             : typedef void  (*zend_mm_chunk_free_t)(zend_mm_storage *storage, void *chunk, size_t size);
     271             : typedef void  (*zend_mm_chunk_truncate_t)(zend_mm_storage *storage, void *chunk, size_t old_size, size_t new_size);
     272             : typedef void  (*zend_mm_storage_dtor_t)(zend_mm_storage *storage);
     273             : 
     274             : struct _zend_mm_storage {
     275             :         zend_mm_chunk_alloc_t       chunk_alloc;
     276             :         zend_mm_chunk_free_t        chunk_free;
     277             :         zend_mm_chunk_truncate_t    chunk_truncate;
     278             :         zend_mm_storage_dtor_t      dtor;
     279             : };
     280             : 
     281             : ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
     282             : ZEND_API zend_mm_heap *zend_mm_startup_ex(zend_mm_storage *storage);
     283             : 
     284             : END_EXTERN_C()
     285             : 
     286             : #endif
     287             : 
     288             : /*
     289             :  * Local variables:
     290             :  * tab-width: 4
     291             :  * c-basic-offset: 4
     292             :  * indent-tabs-mode: t
     293             :  * End:
     294             :  */

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:26 +0000 (6 hours ago)

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