Skip to content

Commit 17248bc

Browse files
committed
PoC using the boehm-gc
1 parent a4bb5b9 commit 17248bc

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

+1195
-397
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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,147 @@ typedef struct _zend_mm_debug_info {
6262

6363
BEGIN_EXTERN_C()
6464

65+
#ifdef USE_LIBGC
66+
67+
#if 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+
95+
ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown);
96+
ZEND_API void init_memory_manager(void);
97+
ZEND_API void start_memory_manager(void);
98+
ZEND_API void prepare_memory_manager(void);
99+
100+
ZEND_API bool is_zend_ptr(const void *ptr);
101+
102+
#define is_zend_mm() false
103+
#define zend_memory_reset_peak_usage() do { } while (0)
104+
#define zend_alloc_in_memory_limit_error_reporting() false
105+
#define refresh_memory_manager() do { } while (0)
106+
107+
#define emalloc(size) GC_MALLOC(size)
108+
#define emalloc_large(size) GC_MALLOC(size)
109+
#define emalloc_huge(size) GC_MALLOC(size)
110+
#define safe_emalloc(nmemb, size, offset) _safe_emalloc(nmemb, size, offset ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
111+
#define efree(ptr) ((void)(ptr))
112+
#define efree_large(ptr) ((void)(ptr))
113+
#define efree_huge(ptr) ((void)(ptr))
114+
#define ecalloc(nmemb, size) _ecalloc(nmemb, size ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
115+
#define erealloc(ptr, size) GC_REALLOC(ptr, size)
116+
#define erealloc2(ptr, size, copy_size) GC_REALLOC(ptr, size)
117+
#define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc(ptr, nmemb, size, offset ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
118+
#define erealloc_recoverable(ptr, size) GC_REALLOC(ptr, size)
119+
#define erealloc2_recoverable(ptr, size, copy_size) GC_REALLOC(ptr, size)
120+
#define estrdup(s) _estrdup(s ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
121+
#define estrndup(s, length) _estrndup(s, length ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
122+
#define emalloc_atomic(size) GC_MALLOC_ATOMIC(size)
123+
#define safe_emalloc_atomic(nmemb, size, offset) _safe_emalloc_atomic(nmemb, size, offset ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
124+
125+
#define emalloc_rel(size) emalloc(size)
126+
#define safe_emalloc_rel(nmemb, size, offset) safe_emalloc(nmemb, size, offset)
127+
#define efree_rel(ptr) efree(ptr)
128+
#define ecalloc_rel(nmemb, size) ecalloc(nmemb, size)
129+
#define erealloc_rel(ptr, size) erealloc(ptr, size)
130+
#define erealloc2_rel(ptr, size, copy_size) erealloc2(ptr, size, copy_size)
131+
#define erealloc_recoverable_rel(ptr, size) erealloc_recoverable(ptr, size)
132+
#define erealloc2_recoverable_rel(ptr, size, copy_size) erealloc2_recoverable(ptr, size, copy_size)
133+
#define safe_erealloc_rel(ptr, nmemb, size, offset) safe_erealloc(ptr, nmemb, size, offset)
134+
#define estrdup_rel(s) estrdup(s)
135+
#define estrndup_rel(s, length) estrndup(s, length)
136+
137+
#define efree_size(ptr, size)
138+
#define efree_size_rel(ptr, size)
139+
140+
#define pemalloc(size, persistent) emalloc(size)
141+
#define safe_pemalloc(nmemb, size, offset, persistent) safe_emalloc(nmemb, size, offset)
142+
#define pefree(ptr, persistent) efree(ptr)
143+
#define pefree_size(ptr, size, persistent) efree_size(ptr, size)
144+
#define pecalloc(nmemb, size, persistent) ecalloc(nmemb, size)
145+
#define perealloc(ptr, size, persistent) erealloc(ptr, size)
146+
#define perealloc2(ptr, size, copy_size, persistent) erealloc2(ptr, size, copy_size)
147+
#define safe_perealloc(ptr, nmemb, size, offset, persistent) safe_erealloc(ptr, nmemb, size, offset)
148+
#define perealloc_recoverable(ptr, size, persistent) erealloc_recoverable(ptr, size)
149+
#define perealloc2_recoverable(ptr, size, persistent) erealloc2_recoverable(ptr, size)
150+
#define pestrdup(s, persistent) estrdup(s)
151+
#define pestrndup(s, length, persistent) estrndup(s, length)
152+
#define pemalloc_atomic(size, persistent) emalloc_atomic(size)
153+
#define safe_pemalloc_atomic(nmemb, size, offset, persistent) safe_emalloc_atomic(nmemb, size, offset)
154+
155+
#define pemalloc_rel(size, persistent) pemalloc(size, persistent)
156+
#define pefree_rel(ptr, persistent) pefree(ptr, persistent)
157+
#define pecalloc_rel(nmemb, size, persistent) pecalloc(nmemb, size, persistent)
158+
#define perealloc_rel(ptr, size, persistent) perealloc(ptr, size, persistent)
159+
#define perealloc2_rel(ptr, size, copy_size, persistent) perealloc2(ptr, size, copy_size, persistent)
160+
#define perealloc_recoverable_rel(ptr, size, persistent) perealloc_recoverable(ptr, size, persistent)
161+
#define perealloc2_recoverable_rel(ptr, size, copy_size, persistent) perealloc2_recoverable(ptr, size, copy_size, persistent)
162+
#define pestrdup_rel(s, persistent) pestrdup(s, persistent)
163+
164+
/* fast cache for HashTables */
165+
#define ALLOC_HASHTABLE(ht) \
166+
(ht) = (HashTable *) emalloc(sizeof(HashTable))
167+
168+
#define FREE_HASHTABLE(ht) \
169+
efree_size(ht, sizeof(HashTable))
170+
171+
#define ALLOC_HASHTABLE_REL(ht) \
172+
(ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
173+
174+
#define FREE_HASHTABLE_REL(ht) \
175+
efree_size_rel(ht, sizeof(HashTable))
176+
177+
// TODO: should fix call site instead
178+
#define malloc(size) emalloc(size)
179+
#define free(size) efree(size)
180+
#define realloc(ptr, size) erealloc(ptr, size)
181+
#define calloc(nmemb, size) ecalloc(nmemb, size)
182+
#define strdup(s) estrdup(s)
183+
#define strndup(s) estrndup(s)
184+
185+
/* Hide ptr from libgc */
186+
#define MASK_PTR(ptr) ((uintptr_t)(ptr) | (1ULL<<63))
187+
#define UNMASK_PTR(ptr) ((uintptr_t)(ptr) & ~(1ULL<<63))
188+
189+
#include "zend_alloc_sizes.h"
190+
191+
/* _emalloc() & _efree() specialization */
192+
# if !ZEND_DEBUG
193+
# define _ZEND_BIN_ALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
194+
ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc_ ## _size(void);
195+
196+
ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR_DEF, x, y)
197+
198+
# define _ZEND_BIN_DEALLOCATOR_DEF(_num, _size, _elements, _pages, x, y) \
199+
ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *);
200+
201+
ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_DEF, x, y)
202+
# endif
203+
204+
#else /* USE_LIBGC */
205+
65206
ZEND_API ZEND_ATTRIBUTE_MALLOC char* ZEND_FASTCALL zend_strndup(const char *s, size_t length);
66207

67208
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 +549,8 @@ static void apc_init_heap(void)
408549
size_t zend_mm_globals_size(void);
409550
#endif
410551

552+
#endif /* LIBGC */
553+
411554
END_EXTERN_C()
412555

413556
#endif

0 commit comments

Comments
 (0)