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
44typedef 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
53BEGIN_EXTERN_C()
54
55ZEND_API char*  ZEND_FASTCALL zend_strndup(const char *s, size_t length) ZEND_ATTRIBUTE_MALLOC;
56
57ZEND_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);
58ZEND_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;
59ZEND_API void*  ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
60ZEND_API void   ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
61ZEND_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);
62ZEND_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);
63ZEND_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);
64ZEND_API void*  ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
65ZEND_API char*  ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
66ZEND_API char*  ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
67ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr 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
77ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_DEF, x, y)
78
79ZEND_API void* ZEND_FASTCALL _emalloc_large(size_t size) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
80ZEND_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
102ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_DEF, x, y)
103
104ZEND_API void ZEND_FASTCALL _efree_large(void *, size_t size);
105ZEND_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) 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) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
166
167zend_always_inline static void * __zend_malloc(size_t len)
168{
169    void *tmp = malloc(len);
170    if (tmp) {
171        return tmp;
172    }
173    fprintf(stderr, "Out of memory\n");
174    exit(1);
175}
176
177zend_always_inline static void * __zend_calloc(size_t nmemb, size_t len)
178{
179    void *tmp = _safe_malloc(nmemb, len, 0);
180    memset(tmp, 0, nmemb * len);
181    return tmp;
182}
183
184zend_always_inline static void * __zend_realloc(void *p, size_t len)
185{
186    p = realloc(p, len);
187    if (p) {
188        return p;
189    }
190    fprintf(stderr, "Out of memory\n");
191    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
214ZEND_API int zend_set_memory_limit(size_t memory_limit);
215
216ZEND_API void start_memory_manager(void);
217ZEND_API void shutdown_memory_manager(int silent, int full_shutdown);
218ZEND_API int is_zend_mm(void);
219
220ZEND_API size_t zend_memory_usage(int real_usage);
221ZEND_API size_t zend_memory_peak_usage(int real_usage);
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 */
237typedef struct _zend_mm_heap zend_mm_heap;
238
239ZEND_API zend_mm_heap *zend_mm_startup(void);
240ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent);
241ZEND_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;
242ZEND_API void   ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
243ZEND_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);
244ZEND_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
256ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap);
257
258ZEND_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));
262ZEND_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
267typedef struct _zend_mm_storage zend_mm_storage;
268
269typedef void* (*zend_mm_chunk_alloc_t)(zend_mm_storage *storage, size_t size, size_t alignment);
270typedef void  (*zend_mm_chunk_free_t)(zend_mm_storage *storage, void *chunk, size_t size);
271typedef void  (*zend_mm_chunk_truncate_t)(zend_mm_storage *storage, void *chunk, size_t old_size, size_t new_size);
272typedef void  (*zend_mm_storage_dtor_t)(zend_mm_storage *storage);
273
274struct _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
281ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
282ZEND_API zend_mm_heap *zend_mm_startup_ex(zend_mm_storage *storage);
283
284END_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 */
295