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#include "zend_types.h"
30
31#ifndef ZEND_MM_ALIGNMENT
32# define ZEND_MM_ALIGNMENT Z_I(8)
33# define ZEND_MM_ALIGNMENT_LOG2 Z_I(3)
34#elif ZEND_MM_ALIGNMENT < Z_I(4)
35# undef ZEND_MM_ALIGNMENT
36# undef ZEND_MM_ALIGNMENT_LOG2
37# define ZEND_MM_ALIGNMENT Z_I(4)
38# define ZEND_MM_ALIGNMENT_LOG2 Z_I(2)
39#endif
40
41#define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-Z_I(1))
42
43#define ZEND_MM_ALIGNED_SIZE(size)  (((size) + ZEND_MM_ALIGNMENT - Z_I(1)) & ZEND_MM_ALIGNMENT_MASK)
44
45typedef struct _zend_leak_info {
46    void *addr;
47    size_t size;
48    const char *filename;
49    uint lineno;
50    const char *orig_filename;
51    uint orig_lineno;
52} zend_leak_info;
53
54BEGIN_EXTERN_C()
55
56ZEND_API char *zend_strndup(const char *s, zend_size_t length) ZEND_ATTRIBUTE_MALLOC;
57
58ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
59ZEND_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;
60ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
61ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
62ZEND_API void *_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);
63ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
64ZEND_API void *_safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
65ZEND_API void *_safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
66ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
67ZEND_API char *_estrndup(const char *s, zend_size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
68ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
69
70/* Standard wrapper macros */
71#define emalloc(size)                       _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
72#define safe_emalloc(nmemb, size, offset)   _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
73#define efree(ptr)                          _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
74#define ecalloc(nmemb, size)                _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
75#define erealloc(ptr, size)                 _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
76#define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
77#define erealloc_recoverable(ptr, size)     _erealloc((ptr), (size), 1 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
78#define estrdup(s)                          _estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
79#define estrndup(s, length)                 _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
80#define zend_mem_block_size(ptr)            _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
81
82/* Relay wrapper macros */
83#define emalloc_rel(size)                       _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
84#define safe_emalloc_rel(nmemb, size, offset)   _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
85#define efree_rel(ptr)                          _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
86#define ecalloc_rel(nmemb, size)                _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
87#define erealloc_rel(ptr, size)                 _erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
88#define erealloc_recoverable_rel(ptr, size)     _erealloc((ptr), (size), 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
89#define safe_erealloc_rel(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
90#define estrdup_rel(s)                          _estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
91#define estrndup_rel(s, length)                 _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
92#define zend_mem_block_size_rel(ptr)            _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
93
94inline static void * __zend_malloc(size_t len)
95{
96    void *tmp = malloc(len);
97    if (tmp) {
98        return tmp;
99    }
100    fprintf(stderr, "Out of memory\n");
101    exit(1);
102}
103
104inline static void * __zend_calloc(size_t nmemb, size_t len)
105{
106    void *tmp = _safe_malloc(nmemb, len, 0);
107    memset(tmp, 0, nmemb * len);
108    return tmp;
109}
110
111inline static void * __zend_realloc(void *p, size_t len)
112{
113    p = realloc(p, len);
114    if (p) {
115        return p;
116    }
117    fprintf(stderr, "Out of memory\n");
118    exit(1);
119}
120
121
122/* Selective persistent/non persistent allocation macros */
123#define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
124#define safe_pemalloc(nmemb, size, offset, persistent)  ((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
125#define pefree(ptr, persistent)  ((persistent)?free(ptr):efree(ptr))
126#define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size)))
127#define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size)))
128#define safe_perealloc(ptr, nmemb, size, offset, persistent)    ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
129#define perealloc_recoverable(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
130#define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
131#define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
132
133#define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))
134#define pefree_rel(ptr, persistent) ((persistent)?free(ptr):efree_rel(ptr))
135#define pecalloc_rel(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc_rel((nmemb), (size)))
136#define perealloc_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_rel((ptr), (size)))
137#define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
138#define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
139
140ZEND_API int zend_set_memory_limit(size_t memory_limit TSRMLS_DC);
141
142ZEND_API void start_memory_manager(TSRMLS_D);
143ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC);
144ZEND_API int is_zend_mm(TSRMLS_D);
145
146#if ZEND_DEBUG
147ZEND_API int _mem_block_check(void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
148ZEND_API void _full_mem_check(int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
149void zend_debug_alloc_output(char *format, ...);
150#define mem_block_check(ptr, silent) _mem_block_check(ptr, silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
151#define full_mem_check(silent) _full_mem_check(silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
152#else
153#define mem_block_check(type, ptr, silent)
154#define full_mem_check(silent)
155#endif
156
157ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC);
158ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC);
159
160END_EXTERN_C()
161
162/* fast cache for HashTables */
163#define ALLOC_HASHTABLE(ht) \
164    (ht) = (HashTable *) emalloc(sizeof(HashTable))
165
166#define FREE_HASHTABLE(ht)  \
167    efree(ht)
168
169#define ALLOC_HASHTABLE_REL(ht) \
170    (ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
171
172#define FREE_HASHTABLE_REL(ht)  \
173    efree_rel(ht)
174
175/* Heap functions */
176typedef struct _zend_mm_heap zend_mm_heap;
177
178ZEND_API zend_mm_heap *zend_mm_startup(void);
179ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC);
180ZEND_API void *_zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
181ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
182ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
183ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
184
185#define zend_mm_alloc(heap, size)           _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
186#define zend_mm_free(heap, p)               _zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
187#define zend_mm_realloc(heap, p, size)      _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
188#define zend_mm_block_size(heap, p)         _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
189
190#define zend_mm_alloc_rel(heap, size)       _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
191#define zend_mm_free_rel(heap, p)           _zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
192#define zend_mm_realloc_rel(heap, p, size)  _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
193#define zend_mm_block_size_rel(heap, p)     _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
194
195/* Heaps with user defined storage */
196typedef struct _zend_mm_storage zend_mm_storage;
197
198typedef struct _zend_mm_segment {
199    size_t  size;
200    struct _zend_mm_segment *next_segment;
201} zend_mm_segment;
202
203typedef struct _zend_mm_mem_handlers {
204    const char *name;
205    zend_mm_storage* (*init)(void *params);
206    void (*dtor)(zend_mm_storage *storage);
207    void (*compact)(zend_mm_storage *storage);
208    zend_mm_segment* (*_alloc)(zend_mm_storage *storage, size_t size);
209    zend_mm_segment* (*_realloc)(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size);
210    void (*_free)(zend_mm_storage *storage, zend_mm_segment *ptr);
211} zend_mm_mem_handlers;
212
213struct _zend_mm_storage {
214    const zend_mm_mem_handlers *handlers;
215    void *data;
216};
217
218ZEND_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);
219ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC);
220ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
221
222ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
223                                          void* (*_malloc)(size_t),
224                                          void  (*_free)(void*),
225                                          void* (*_realloc)(void*, size_t));
226
227#endif
228
229/*
230 * Local variables:
231 * tab-width: 4
232 * c-basic-offset: 4
233 * indent-tabs-mode: t
234 * End:
235 */
236