Back to index

php5  5.3.10
zend_alloc.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_alloc.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifndef ZEND_ALLOC_H
00023 #define ZEND_ALLOC_H
00024 
00025 #include <stdio.h>
00026 
00027 #include "../TSRM/TSRM.h"
00028 #include "zend.h"
00029 
00030 #ifndef ZEND_MM_ALIGNMENT
00031 # define ZEND_MM_ALIGNMENT 8
00032 # define ZEND_MM_ALIGNMENT_LOG2 3
00033 #elif ZEND_MM_ALIGNMENT < 4
00034 # undef ZEND_MM_ALIGNMENT
00035 # undef ZEND_MM_ALIGNMENT_LOG2
00036 # define ZEND_MM_ALIGNMENT 4
00037 # define ZEND_MM_ALIGNMENT_LOG2 2
00038 #endif
00039 
00040 #define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1)
00041 
00042 #define ZEND_MM_ALIGNED_SIZE(size) (((size) + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
00043 
00044 typedef struct _zend_leak_info {
00045        void *addr;
00046        size_t size;
00047        char *filename;
00048        uint lineno;
00049        char *orig_filename;
00050        uint orig_lineno;
00051 } zend_leak_info;
00052 
00053 BEGIN_EXTERN_C()
00054 
00055 ZEND_API char *zend_strndup(const char *s, unsigned int length) ZEND_ATTRIBUTE_MALLOC;
00056 
00057 ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
00058 ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
00059 ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
00060 ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00061 ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
00062 ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00063 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);
00064 ZEND_API void *_safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
00065 ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
00066 ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
00067 ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00068 
00069 /* Standard wrapper macros */
00070 #define emalloc(size)                                          _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00071 #define safe_emalloc(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00072 #define efree(ptr)                                             _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00073 #define ecalloc(nmemb, size)                            _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00074 #define erealloc(ptr, size)                             _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00075 #define safe_erealloc(ptr, nmemb, size, offset)  _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00076 #define erealloc_recoverable(ptr, size)          _erealloc((ptr), (size), 1 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00077 #define estrdup(s)                                             _estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00078 #define estrndup(s, length)                             _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00079 #define zend_mem_block_size(ptr)                 _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00080 
00081 /* Relay wrapper macros */
00082 #define emalloc_rel(size)                                      _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00083 #define safe_emalloc_rel(nmemb, size, offset)    _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00084 #define efree_rel(ptr)                                                _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00085 #define ecalloc_rel(nmemb, size)                        _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00086 #define erealloc_rel(ptr, size)                                _erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00087 #define erealloc_recoverable_rel(ptr, size)             _erealloc((ptr), (size), 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00088 #define safe_erealloc_rel(ptr, nmemb, size, offset)     _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00089 #define estrdup_rel(s)                                                _estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00090 #define estrndup_rel(s, length)                                _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00091 #define zend_mem_block_size_rel(ptr)                    _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00092 
00093 inline static void * __zend_malloc(size_t len)
00094 {
00095        void *tmp = malloc(len);
00096        if (tmp) {
00097               return tmp;
00098        }
00099        fprintf(stderr, "Out of memory\n");
00100        exit(1);
00101 }
00102 
00103 inline static void * __zend_calloc(size_t nmemb, size_t len)
00104 {
00105        void *tmp = _safe_malloc(nmemb, len, 0);
00106        memset(tmp, 0, nmemb * len);
00107        return tmp;
00108 }
00109 
00110 inline static void * __zend_realloc(void *p, size_t len)
00111 {
00112        p = realloc(p, len);
00113        if (p) {
00114               return p;
00115        }
00116        fprintf(stderr, "Out of memory\n");
00117        exit(1);
00118 }
00119 
00120 
00121 /* Selective persistent/non persistent allocation macros */
00122 #define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
00123 #define safe_pemalloc(nmemb, size, offset, persistent)  ((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
00124 #define pefree(ptr, persistent)  ((persistent)?free(ptr):efree(ptr))
00125 #define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size)))
00126 #define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size)))
00127 #define safe_perealloc(ptr, nmemb, size, offset, persistent)   ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
00128 #define perealloc_recoverable(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
00129 #define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
00130 #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
00131 
00132 #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))
00133 #define pefree_rel(ptr, persistent)       ((persistent)?free(ptr):efree_rel(ptr))
00134 #define pecalloc_rel(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc_rel((nmemb), (size)))
00135 #define perealloc_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_rel((ptr), (size)))
00136 #define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
00137 #define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
00138 
00139 #define safe_estrdup(ptr)  ((ptr)?(estrdup(ptr)):STR_EMPTY_ALLOC())
00140 #define safe_estrndup(ptr, len) ((ptr)?(estrndup((ptr), (len))):STR_EMPTY_ALLOC())
00141 
00142 ZEND_API int zend_set_memory_limit(size_t memory_limit);
00143 
00144 ZEND_API void start_memory_manager(TSRMLS_D);
00145 ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC);
00146 ZEND_API int is_zend_mm(TSRMLS_D);
00147 
00148 #if ZEND_DEBUG
00149 ZEND_API int _mem_block_check(void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00150 ZEND_API void _full_mem_check(int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00151 void zend_debug_alloc_output(char *format, ...);
00152 #define mem_block_check(ptr, silent) _mem_block_check(ptr, silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00153 #define full_mem_check(silent) _full_mem_check(silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00154 #else
00155 #define mem_block_check(type, ptr, silent)
00156 #define full_mem_check(silent)
00157 #endif
00158 
00159 ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC);
00160 ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC);
00161 
00162 END_EXTERN_C()
00163 
00164 /* Macroses for zend_fast_cache.h compatibility */
00165 
00166 #define ZEND_FAST_ALLOC(p, type, fc_type) \
00167        (p) = (type *) emalloc(sizeof(type))
00168 
00169 #define ZEND_FAST_FREE(p, fc_type) \
00170        efree(p)
00171 
00172 #define ZEND_FAST_ALLOC_REL(p, type, fc_type)    \
00173        (p) = (type *) emalloc_rel(sizeof(type))
00174 
00175 #define ZEND_FAST_FREE_REL(p, fc_type)    \
00176        efree_rel(p)
00177 
00178 /* fast cache for zval's */
00179 #define ALLOC_ZVAL(z)       \
00180        ZEND_FAST_ALLOC(z, zval, ZVAL_CACHE_LIST)
00181 
00182 #define FREE_ZVAL(z) \
00183        ZEND_FAST_FREE(z, ZVAL_CACHE_LIST)
00184 
00185 #define ALLOC_ZVAL_REL(z)   \
00186        ZEND_FAST_ALLOC_REL(z, zval, ZVAL_CACHE_LIST)
00187 
00188 #define FREE_ZVAL_REL(z)    \
00189        ZEND_FAST_FREE_REL(z, ZVAL_CACHE_LIST)
00190 
00191 /* fast cache for HashTables */
00192 #define ALLOC_HASHTABLE(ht) \
00193        ZEND_FAST_ALLOC(ht, HashTable, HASHTABLE_CACHE_LIST)
00194 
00195 #define FREE_HASHTABLE(ht)  \
00196        ZEND_FAST_FREE(ht, HASHTABLE_CACHE_LIST)
00197 
00198 #define ALLOC_HASHTABLE_REL(ht)    \
00199        ZEND_FAST_ALLOC_REL(ht, HashTable, HASHTABLE_CACHE_LIST)
00200 
00201 #define FREE_HASHTABLE_REL(ht)     \
00202        ZEND_FAST_FREE_REL(ht, HASHTABLE_CACHE_LIST)
00203 
00204 /* Heap functions */
00205 typedef struct _zend_mm_heap zend_mm_heap;
00206 
00207 ZEND_API zend_mm_heap *zend_mm_startup(void);
00208 ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC);
00209 ZEND_API void *_zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
00210 ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00211 ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00212 ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
00213 
00214 #define zend_mm_alloc(heap, size)                _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00215 #define zend_mm_free(heap, p)                           _zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00216 #define zend_mm_realloc(heap, p, size)           _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00217 #define zend_mm_block_size(heap, p)                     _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00218 
00219 #define zend_mm_alloc_rel(heap, size)            _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00220 #define zend_mm_free_rel(heap, p)                _zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00221 #define zend_mm_realloc_rel(heap, p, size)       _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
00222 #define zend_mm_block_size_rel(heap, p)          _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00223 
00224 /* Heaps with user defined storage */
00225 typedef struct _zend_mm_storage zend_mm_storage;
00226 
00227 typedef struct _zend_mm_segment {
00228        size_t size;
00229        struct _zend_mm_segment *next_segment;
00230 } zend_mm_segment;
00231 
00232 typedef struct _zend_mm_mem_handlers {
00233        const char *name;
00234        zend_mm_storage* (*init)(void *params);
00235        void (*dtor)(zend_mm_storage *storage);
00236        void (*compact)(zend_mm_storage *storage);
00237        zend_mm_segment* (*_alloc)(zend_mm_storage *storage, size_t size);
00238        zend_mm_segment* (*_realloc)(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size);
00239        void (*_free)(zend_mm_storage *storage, zend_mm_segment *ptr);
00240 } zend_mm_mem_handlers;
00241 
00242 struct _zend_mm_storage {
00243        const zend_mm_mem_handlers *handlers;
00244        void *data;
00245 };
00246 
00247 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);
00248 ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC);
00249 ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
00250 
00251 ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
00252                                           void* (*_malloc)(size_t),
00253                                           void  (*_free)(void*),
00254                                           void* (*_realloc)(void*, size_t));
00255 
00256 #endif
00257 
00258 /*
00259  * Local variables:
00260  * tab-width: 4
00261  * c-basic-offset: 4
00262  * indent-tabs-mode: t
00263  * End:
00264  */