Skip to content

Commit 5ae3083

Browse files
committed
Use ZendMM for libgmp
As is, ext/gmp uses the default memory allocator of the underlying libgmp; this allocator is fallible, but contrary to ZendMM, it `abort()`s without giving us an opportunity to do a cleaner shutdown. Furthermore, the libgmp allocator obviously doesn't heed memory_limit. Thus we install wrappers of the ZendMM API as libgmp allocator.
1 parent a00c734 commit 5ae3083

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

ext/gmp/gmp.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,21 @@ static int gmp_unserialize(zval *object, zend_class_entry *ce, const unsigned ch
521521
}
522522
/* }}} */
523523

524+
static void *gmp_alloc(size_t size)
525+
{
526+
return emalloc(size);
527+
}
528+
529+
static void *gmp_realloc(void *ptr, size_t old_size, size_t new_size)
530+
{
531+
return erealloc(ptr, new_size);
532+
}
533+
534+
static void gmp_free(void *ptr, size_t size)
535+
{
536+
efree(ptr);
537+
}
538+
524539
/* {{{ ZEND_GINIT_FUNCTION */
525540
static ZEND_GINIT_FUNCTION(gmp)
526541
{
@@ -551,6 +566,8 @@ ZEND_MINIT_FUNCTION(gmp)
551566

552567
register_gmp_symbols(module_number);
553568

569+
mp_set_memory_functions(gmp_alloc, gmp_realloc, gmp_free);
570+
554571
return SUCCESS;
555572
}
556573
/* }}} */

0 commit comments

Comments
 (0)