Skip to content

Commit 3efcd0c

Browse files
committed
PoC using the boehm-gc
1 parent a4bb5b9 commit 3efcd0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1220
-396
lines changed

Zend/zend.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
119119
}
120120
/* }}} */
121121

122+
#ifndef USE_LIBGC
122123
static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
123124
{
124125
bool val;
@@ -139,7 +140,7 @@ static ZEND_INI_DISP(zend_gc_enabled_displayer_cb) /* {{{ */
139140
}
140141
}
141142
/* }}} */
142-
143+
#endif
143144

144145
static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
145146
{
@@ -262,7 +263,9 @@ ZEND_INI_BEGIN()
262263
ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting)
263264
STD_ZEND_INI_BOOLEAN("fatal_error_backtraces", "1", ZEND_INI_ALL, OnUpdateBool, fatal_error_backtrace_on, zend_executor_globals, executor_globals)
264265
STD_ZEND_INI_ENTRY("zend.assertions", "1", ZEND_INI_ALL, OnUpdateAssertions, assertions, zend_executor_globals, executor_globals)
266+
#ifndef USE_LIBGC
265267
ZEND_INI_ENTRY3_EX("zend.enable_gc", "1", ZEND_INI_ALL, OnUpdateGCEnabled, NULL, NULL, NULL, zend_gc_enabled_displayer_cb)
268+
#endif
266269
STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte, zend_compiler_globals, compiler_globals)
267270
ZEND_INI_ENTRY("zend.script_encoding", NULL, ZEND_INI_ALL, OnUpdateScriptEncoding)
268271
STD_ZEND_INI_BOOLEAN("zend.detect_unicode", "1", ZEND_INI_ALL, OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)

Zend/zend_alloc.h

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,170 @@ typedef struct _zend_mm_debug_info {
6262

6363
BEGIN_EXTERN_C()
6464

65+
#ifdef USE_LIBGC
66+
67+
#if defined(ZEND_DEBUG) && !defined(GC_DEBUG)
68+
# define GC_DEBUG
69+
#endif
70+
#ifdef GC_H
71+
# error gc/gh.h included before zend_alloc.h
72+
#endif
73+
#include <gc/gc.h>
74+
#include <gc/gc_backptr.h>
75+
76+
typedef struct _zend_mm_heap zend_mm_heap;
77+
78+
ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
79+
ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
80+
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);
81+
ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
82+
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);
83+
ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
84+
ZEND_API char* _estrndup(const char *s, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
85+
ZEND_API void* ZEND_FASTCALL _safe_emalloc_atomic(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
86+
87+
ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length);
88+
89+
ZEND_API zend_result zend_set_memory_limit(size_t memory_limit);
90+
ZEND_API size_t zend_memory_usage(bool real_usage);
91+
ZEND_API size_t zend_memory_peak_usage(bool real_usage);
92+
93+
ZEND_API size_t zend_mm_gc(zend_mm_heap *heap);
94+
ZEND_API zend_mm_heap *zend_mm_get_heap(void);
95+
ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap);
96+
97+
ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown);
98+
ZEND_API void init_memory_manager(void);
99+
ZEND_API void start_memory_manager(void);
100+
101+
ZEND_API bool is_zend_ptr(const void *ptr);
102+
103+
#define is_zend_mm() false
104+
#define zend_memory_reset_peak_usage() do { } while (0)
105+
#define zend_alloc_in_memory_limit_error_reporting() false
106+
#define refresh_memory_manager() do { } while (0)
107+
108+
#define emalloc(size) GC_MALLOC(size)
109+
#define emalloc_large(size) GC_MALLOC(size)
110+
#define emalloc_huge(size) GC_MALLOC(size)
111+
#define safe_emalloc(nmemb, size, offset) _safe_emalloc(nmemb, size, offset ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
112+
#define efree(ptr) ((void)(ptr))
113+
#define efree_large(ptr) ((void)(ptr))
114+
#define efree_huge(ptr) ((void)(ptr))
115+
#define ecalloc(nmemb, size) _ecalloc(nmemb, size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
116+
#define erealloc(ptr, size) GC_REALLOC(ptr, size)
117+
#define erealloc2(ptr, size, copy_size) GC_REALLOC(ptr, size)
118+
#define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc(ptr, nmemb, size, offset ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
119+
#define erealloc_recoverable(ptr, size) GC_REALLOC(ptr, size)
120+
#define erealloc2_recoverable(ptr, size, copy_size) GC_REALLOC(ptr, size)
121+
#define estrdup(s) _estrdup(s ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
122+
#define estrndup(s, length) _estrndup(s, length ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
123+
#define emalloc_atomic(size) GC_MALLOC_ATOMIC(size)
124+
#define safe_emalloc_atomic(nmemb, size, offset) _safe_emalloc_atomic(nmemb, size, offset ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
125+
126+
#define emalloc_rel(size) emalloc(size)
127+
#define safe_emalloc_rel(nmemb, size, offset) safe_emalloc(nmemb, size, offset)
128+
#define efree_rel(ptr) efree(ptr)
129+
#define ecalloc_rel(nmemb, size) ecalloc(nmemb, size)
130+
#define erealloc_rel(ptr, size) erealloc(ptr, size)
131+
#define erealloc2_rel(ptr, size, copy_size) erealloc2(ptr, size, copy_size)
132+
#define erealloc_recoverable_rel(ptr, size) erealloc_recoverable(ptr, size)
133+
#define erealloc2_recoverable_rel(ptr, size, copy_size) erealloc2_recoverable(ptr, size, copy_size)
134+
#define safe_erealloc_rel(ptr, nmemb, size, offset) safe_erealloc(ptr, nmemb, size, offset)
135+
#define estrdup_rel(s) estrdup(s)
136+
#define estrndup_rel(s, length) estrndup(s, length)
137+
138+
#define efree_size(ptr, size)
139+
#define efree_size_rel(ptr, size)
140+
141+
#define pemalloc(size, persistent) emalloc(size)
142+
#define safe_pemalloc(nmemb, size, offset, persistent) safe_emalloc(nmemb, size, offset)
143+
#define pefree(ptr, persistent) efree(ptr)
144+
#define pefree_size(ptr, size, persistent) efree_size(ptr, size)
145+
#define pecalloc(nmemb, size, persistent) ecalloc(nmemb, size)
146+
#define perealloc(ptr, size, persistent) erealloc(ptr, size)
147+
#define perealloc2(ptr, size, copy_size, persistent) erealloc2(ptr, size, copy_size)
148+
#define safe_perealloc(ptr, nmemb, size, offset, persistent) safe_erealloc(ptr, nmemb, size, offset)
149+
#define perealloc_recoverable(ptr, size, persistent) erealloc_recoverable(ptr, size)
150+
#define perealloc2_recoverable(ptr, size, persistent) erealloc2_recoverable(ptr, size)
151+
#define pestrdup(s, persistent) estrdup(s)
152+
#define pestrndup(s, length, persistent) estrndup(s, length)
153+
#define pemalloc_atomic(size, persistent) emalloc_atomic(size)
154+
#define safe_pemalloc_atomic(nmemb, size, offset, persistent) safe_emalloc_atomic(nmemb, size, offset)
155+
156+
#define pemalloc_rel(size, persistent) pemalloc(size, persistent)
157+
#define pefree_rel(ptr, persistent) pefree(ptr, persistent)
158+
#define pecalloc_rel(nmemb, size, persistent) pecalloc(nmemb, size, persistent)
159+
#define perealloc_rel(ptr, size, persistent) perealloc(ptr, size, persistent)
160+
#define perealloc2_rel(ptr, size, copy_size, persistent) perealloc2(ptr, size, copy_size, persistent)
161+
#define perealloc_recoverable_rel(ptr, size, persistent) perealloc_recoverable(ptr, size, persistent)
162+
#define perealloc2_recoverable_rel(ptr, size, copy_size, persistent) perealloc2_recoverable(ptr, size, copy_size, persistent)
163+
#define pestrdup_rel(s, persistent) pestrdup(s, persistent)
164+
165+
/* fast cache for HashTables */
166+
#define ALLOC_HASHTABLE(ht) \
167+
(ht) = (HashTable *) emalloc(sizeof(HashTable))
168+
169+
#define FREE_HASHTABLE(ht) \
170+
efree_size(ht, sizeof(HashTable))
171+
172+
#define ALLOC_HASHTABLE_REL(ht) \
173+
(ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
174+
175+
#define FREE_HASHTABLE_REL(ht) \
176+
efree_size_rel(ht, sizeof(HashTable))
177+
178+
// TODO: should fix call site instead
179+
#define malloc(size) emalloc(size)
180+
#define free(size) efree(size)
181+
#define realloc(ptr, size) erealloc(ptr, size)
182+
#define calloc(nmemb, size) ecalloc(nmemb, size)
183+
#define strdup(s) estrdup(s)
184+
#define strndup(s) estrndup(s)
185+
186+
/* Hide ptr from libgc */
187+
#define MASK_PTR(ptr) ((uintptr_t)(ptr) | (1ULL<<63))
188+
#define UNMASK_PTR(ptr) ((uintptr_t)(ptr) & ~(1ULL<<63))
189+
190+
#include "zend_alloc_sizes.h"
191+
192+
/* _emalloc() & _efree() specialization */
193+
# if !ZEND_DEBUG
194+
# define _ZEND_BIN_ALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
195+
ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc_ ## _size(void);
196+
197+
ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_DEF, x, y)
198+
199+
# define _ZEND_BIN_DEALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
200+
ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *);
201+
202+
ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_DEF, x, y)
203+
# endif
204+
205+
ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap);
206+
ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
207+
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
208+
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
209+
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC));
210+
ZEND_API void zend_mm_set_custom_handlers_ex(zend_mm_heap *heap,
211+
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
212+
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
213+
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
214+
size_t (*_gc)(void),
215+
void (*_shutdown)(bool, bool));
216+
ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
217+
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
218+
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
219+
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC));
220+
ZEND_API void zend_mm_get_custom_handlers_ex(zend_mm_heap *heap,
221+
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
222+
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
223+
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
224+
size_t (**_gc)(void),
225+
void (**_shutdown)(bool, bool));
226+
227+
#else /* USE_LIBGC */
228+
65229
ZEND_API ZEND_ATTRIBUTE_MALLOC char* ZEND_FASTCALL zend_strndup(const char *s, size_t length);
66230

67231
ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
@@ -408,6 +572,8 @@ static void apc_init_heap(void)
408572
size_t zend_mm_globals_size(void);
409573
#endif
410574

575+
#endif /* LIBGC */
576+
411577
END_EXTERN_C()
412578

413579
#endif

0 commit comments

Comments
 (0)