php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #63595
Patch gmp-memory.patch revision 2014-10-11 06:50 UTC by remi@php.net
revision 2014-10-11 06:16 UTC by remi@php.net

Patch gmp-memory.patch for GNU MP related Bug #63595

Patch version 2014-10-11 06:50 UTC

Return to Bug #63595 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: remi@php.net

--- gmp.c.old	2014-10-11 08:03:14.764951256 +0200
+++ gmp.c	2014-10-11 08:49:17.590456127 +0200
@@ -352,6 +352,26 @@ static inline void gmp_zval_unary_ui_op(
 #define gmp_unary_opl(op)         _gmp_unary_opl(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
 #define gmp_unary_ui_op(op)      _gmp_unary_ui_op(INTERNAL_FUNCTION_PARAM_PASSTHRU, op)
 
+static void *(*_save_malloc) (size_t);
+static void *(*_save_realloc) (void *, size_t, size_t);
+static void (*_save_free) (void *, size_t);
+static int _save_count = 0;
+
+#define GMP_SET_MEMORY do { \
+	if (_save_count == 0) { \
+		mp_get_memory_functions(&_save_malloc, &_save_realloc, &_save_free); \
+		mp_set_memory_functions(gmp_emalloc, gmp_erealloc, gmp_efree); \
+	} \
+	_save_count++; \
+} while (0)
+
+#define GMP_END_MEMORY do { \
+	_save_count--; \
+	if (_save_count == 0) { \
+		mp_set_memory_functions(_save_malloc, _save_realloc, _save_free); \
+	} \
+} while (0)
+
 /* {{{ gmp_emalloc
  */
 static void *gmp_emalloc(size_t size)
@@ -391,7 +411,9 @@ static inline long gmp_get_long(zval *zv
 
 static void gmp_free_object_storage(gmp_object *intern TSRMLS_DC) /* {{{ */
 {
+	GMP_SET_MEMORY;
 	mpz_clear(intern->num);
+	GMP_END_MEMORY;
 
 	zend_object_std_dtor(&intern->std TSRMLS_CC);
 	efree(intern);
@@ -406,7 +428,10 @@ static inline zend_object_value gmp_crea
 	zend_object_std_init(&intern->std, ce TSRMLS_CC);
 	object_properties_init(&intern->std, ce);
 
+	GMP_SET_MEMORY;
 	mpz_init(intern->num);
+	GMP_END_MEMORY;
+
 	*gmpnum_target = intern->num;
 
 	retval.handle = zend_objects_store_put(
@@ -545,6 +570,7 @@ static void shift_operator_helper(gmp_bi
 
 static int gmp_do_operation_ex(zend_uchar opcode, zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
 {
+	GMP_SET_MEMORY;
 	switch (opcode) {
 	case ZEND_ADD:
 		DO_BINARY_UI_OP(mpz_add);
@@ -575,8 +601,10 @@ static int gmp_do_operation_ex(zend_ucha
 		DO_UNARY_OP(mpz_com);
 
 	default:
+		GMP_END_MEMORY;
 		return FAILURE;
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -602,7 +630,10 @@ static int gmp_do_operation(zend_uchar o
 
 static int gmp_compare(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
 {
+	GMP_SET_MEMORY;
 	gmp_cmp(result, op1, op2 TSRMLS_CC);
+	GMP_END_MEMORY;
+
 	if (Z_TYPE_P(result) == IS_BOOL) {
 		ZVAL_LONG(result, 1);
 	}
@@ -616,6 +647,7 @@ static int gmp_serialize(zval *object, u
 	smart_str buf = {0};
 	zval zv, *zv_ptr = &zv;
 	php_serialize_data_t serialize_data = (php_serialize_data_t) data;
+	GMP_SET_MEMORY;
 
 	PHP_VAR_SERIALIZE_INIT(serialize_data);
 	INIT_PZVAL(zv_ptr);
@@ -632,6 +664,7 @@ static int gmp_serialize(zval *object, u
 	*buffer = (unsigned char *) buf.c;
 	*buf_len = buf.len;
 
+	GMP_END_MEMORY;
 	return SUCCESS;
 }
 /* }}} */
@@ -643,6 +676,7 @@ static int gmp_unserialize(zval **object
 	zval zv, *zv_ptr = &zv;
 	int retval = FAILURE;
 	php_unserialize_data_t unserialize_data = (php_unserialize_data_t) data;
+	GMP_SET_MEMORY;
 
 	PHP_VAR_UNSERIALIZE_INIT(unserialize_data);
 	gmp_create_ex(*object, &gmpnum TSRMLS_CC);
@@ -679,6 +713,7 @@ static int gmp_unserialize(zval **object
 exit:
 	zval_dtor(&zv);
 	PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
+	GMP_END_MEMORY;
 	return retval;
 }
 /* }}} */
@@ -723,7 +758,8 @@ ZEND_MINIT_FUNCTION(gmp)
 	REGISTER_LONG_CONSTANT("GMP_BIG_ENDIAN", GMP_BIG_ENDIAN, CONST_CS | CONST_PERSISTENT);
 	REGISTER_LONG_CONSTANT("GMP_NATIVE_ENDIAN", GMP_NATIVE_ENDIAN, CONST_CS | CONST_PERSISTENT);
 
-	mp_set_memory_functions(gmp_emalloc, gmp_erealloc, gmp_efree);
+	// mp_set_memory_functions(gmp_emalloc, gmp_erealloc, gmp_efree);
+	_save_malloc = NULL;
 
 	return SUCCESS;
 }
@@ -734,7 +770,9 @@ ZEND_MINIT_FUNCTION(gmp)
 ZEND_MODULE_DEACTIVATE_D(gmp)
 {
 	if (GMPG(rand_initialized)) {
+		GMP_SET_MEMORY;
 		gmp_randclear(GMPG(rand_state));
+		GMP_END_MEMORY;
 		GMPG(rand_initialized) = 0;
 	}
 
@@ -1085,12 +1123,15 @@ ZEND_FUNCTION(gmp_init)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad base for conversion: %ld (should be between 2 and %d)", base, MAX_BASE);
 		RETURN_FALSE;
 	}
+	GMP_SET_MEMORY;
 
 	INIT_GMP_RETVAL(gmpnumber);
 	if (convert_to_gmp(gmpnumber, number_arg, base TSRMLS_CC) == FAILURE) {
 		zval_dtor(return_value);
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1150,20 +1191,24 @@ ZEND_FUNCTION(gmp_import)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &data, &data_len, &size, &options) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	if (gmp_import_export_validate(size, options, &order, &endian TSRMLS_CC) == FAILURE) {
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
 	if ((data_len % size) != 0) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING,
 			"Input length must be a multiple of word size");
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
 	INIT_GMP_RETVAL(gmpnumber);
 
 	mpz_import(gmpnumber, data_len / size, order, size, endian, 0, data);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1181,14 +1226,17 @@ ZEND_FUNCTION(gmp_export)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &gmpnumber_arg, &size, &options) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	if (gmp_import_export_validate(size, options, &order, &endian TSRMLS_CC) == FAILURE) {
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
 	FETCH_GMP_ZVAL(gmpnumber, gmpnumber_arg, temp_a);
 
 	if (mpz_sgn(gmpnumber) == 0) {
+		GMP_END_MEMORY;
 		RETURN_EMPTY_STRING();
 	} else {
 		size_t bits_per_word = size * 8;
@@ -1199,10 +1247,12 @@ ZEND_FUNCTION(gmp_export)
 		mpz_export(out_string, NULL, order, size, endian, 0, gmpnumber);
 		out_string[out_len] = '\0';
 
+		GMP_END_MEMORY;
 		RETURN_STRINGL(out_string, out_len, 0);
 	}
 
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1215,12 +1265,14 @@ ZEND_FUNCTION(gmp_intval)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &gmpnumber_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	if (IS_GMP(gmpnumber_arg)) {
 		RETVAL_LONG(mpz_get_si(GET_GMP_FROM_ZVAL(gmpnumber_arg)));
 	} else {
 		RETVAL_LONG(gmp_get_long(gmpnumber_arg));
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1236,6 +1288,7 @@ ZEND_FUNCTION(gmp_strval)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &gmpnumber_arg, &base) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 #if MAX_BASE == 62
 	/* Although the maximum base in general in GMP >= 4.2 is 62, mpz_get_str()
@@ -1254,6 +1307,7 @@ ZEND_FUNCTION(gmp_strval)
 	gmp_strval(return_value, gmpnum, base);
 
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1261,7 +1315,9 @@ ZEND_FUNCTION(gmp_strval)
    Add a and b */
 ZEND_FUNCTION(gmp_add)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_ui_op(mpz_add, mpz_add_ui);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1269,7 +1325,9 @@ ZEND_FUNCTION(gmp_add)
    Subtract b from a */
 ZEND_FUNCTION(gmp_sub)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_ui_op(mpz_sub, mpz_sub_ui);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1277,7 +1335,9 @@ ZEND_FUNCTION(gmp_sub)
    Multiply a and b */
 ZEND_FUNCTION(gmp_mul)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_ui_op(mpz_mul, mpz_mul_ui);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1291,6 +1351,7 @@ ZEND_FUNCTION(gmp_div_qr)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	switch (round) {
 	case GMP_ROUND_ZERO:
@@ -1304,8 +1365,10 @@ ZEND_FUNCTION(gmp_div_qr)
 		break;
 	default:
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode");
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1319,6 +1382,7 @@ ZEND_FUNCTION(gmp_div_r)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	switch (round) {
 	case GMP_ROUND_ZERO:
@@ -1332,8 +1396,10 @@ ZEND_FUNCTION(gmp_div_r)
 		break;
 	default:
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode");
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1347,6 +1413,7 @@ ZEND_FUNCTION(gmp_div_q)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &a_arg, &b_arg, &round) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	switch (round) {
 	case GMP_ROUND_ZERO:
@@ -1360,9 +1427,10 @@ ZEND_FUNCTION(gmp_div_q)
 		break;
 	default:
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid rounding mode");
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
-
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1370,7 +1438,9 @@ ZEND_FUNCTION(gmp_div_q)
    Computes a modulo b */
 ZEND_FUNCTION(gmp_mod)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_ui_op_no_zero(mpz_mod, (gmp_binary_ui_op_t) mpz_mod_ui);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1378,7 +1448,9 @@ ZEND_FUNCTION(gmp_mod)
    Divide a by b using exact division algorithm */
 ZEND_FUNCTION(gmp_divexact)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_ui_op_no_zero(mpz_divexact, NULL);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1386,7 +1458,9 @@ ZEND_FUNCTION(gmp_divexact)
    Negates a number */
 ZEND_FUNCTION(gmp_neg)
 {
+	GMP_SET_MEMORY;
 	gmp_unary_op(mpz_neg);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1394,7 +1468,9 @@ ZEND_FUNCTION(gmp_neg)
    Calculates absolute value */
 ZEND_FUNCTION(gmp_abs)
 {
+	GMP_SET_MEMORY;
 	gmp_unary_op(mpz_abs);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1407,21 +1483,25 @@ ZEND_FUNCTION(gmp_fact)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	if (IS_GMP(a_arg)) {
 		mpz_ptr gmpnum_tmp = GET_GMP_FROM_ZVAL(a_arg);
 		if (mpz_sgn(gmpnum_tmp) < 0) {
 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
+			GMP_END_MEMORY;
 			RETURN_FALSE;
 		}
 	} else {
 		if (gmp_get_long(a_arg) < 0) {
 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
+			GMP_END_MEMORY;
 			RETURN_FALSE;
 		}
 	}
 
 	gmp_zval_unary_ui_op(return_value, a_arg, mpz_fac_ui TSRMLS_CC);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1442,6 +1522,7 @@ ZEND_FUNCTION(gmp_pow)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Negative exponent not supported");
 		RETURN_FALSE;
 	}
+	GMP_SET_MEMORY;
 
 	INIT_GMP_RETVAL(gmpnum_result);
 	if (Z_TYPE_P(base_arg) == IS_LONG && Z_LVAL_P(base_arg) >= 0) {
@@ -1451,6 +1532,7 @@ ZEND_FUNCTION(gmp_pow)
 		mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
 		FREE_GMP_TEMP(temp_base);
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1466,6 +1548,7 @@ ZEND_FUNCTION(gmp_powm)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &base_arg, &exp_arg, &mod_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_base, base_arg, temp_base);
 
@@ -1478,6 +1561,7 @@ ZEND_FUNCTION(gmp_powm)
 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second parameter cannot be less than 0");
 			FREE_GMP_TEMP(temp_base);
 			FREE_GMP_TEMP(temp_exp);
+			GMP_END_MEMORY;
 			RETURN_FALSE;
 		}
 	}
@@ -1488,6 +1572,7 @@ ZEND_FUNCTION(gmp_powm)
 		FREE_GMP_TEMP(temp_base);
 		FREE_GMP_TEMP(temp_exp);
 		FREE_GMP_TEMP(temp_mod);
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
@@ -1501,6 +1586,7 @@ ZEND_FUNCTION(gmp_powm)
 
 	FREE_GMP_TEMP(temp_base);
 	FREE_GMP_TEMP(temp_mod);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1515,18 +1601,21 @@ ZEND_FUNCTION(gmp_sqrt)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	if (mpz_sgn(gmpnum_a) < 0) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
 		FREE_GMP_TEMP(temp_a);
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
 	INIT_GMP_RETVAL(gmpnum_result);
 	mpz_sqrt(gmpnum_result, gmpnum_a);
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1541,12 +1630,14 @@ ZEND_FUNCTION(gmp_sqrtrem)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	if (mpz_sgn(gmpnum_a) < 0) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number has to be greater than or equal to 0");
 		FREE_GMP_TEMP(temp_a);
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
@@ -1556,6 +1647,7 @@ ZEND_FUNCTION(gmp_sqrtrem)
 
 	mpz_sqrtrem(gmpnum_result1, gmpnum_result2, gmpnum_a);
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1576,18 +1668,21 @@ ZEND_FUNCTION(gmp_root)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "The root must be positive");
 		RETURN_FALSE;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't take even root of negative number");
 		FREE_GMP_TEMP(temp_a);
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
 	INIT_GMP_RETVAL(gmpnum_result);
 	mpz_root(gmpnum_result, gmpnum_a, (unsigned long) nth);
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1608,12 +1703,14 @@ ZEND_FUNCTION(gmp_rootrem)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "The root must be positive");
 		RETURN_FALSE;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	if (nth % 2 == 0 && mpz_sgn(gmpnum_a) < 0) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't take even root of negative number");
 		FREE_GMP_TEMP(temp_a);
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
 
@@ -1631,6 +1728,7 @@ ZEND_FUNCTION(gmp_rootrem)
 #endif
 
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1645,11 +1743,13 @@ ZEND_FUNCTION(gmp_perfect_square)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &a_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	RETVAL_BOOL((mpz_perfect_square_p(gmpnum_a) != 0));
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1665,11 +1765,13 @@ ZEND_FUNCTION(gmp_prob_prime)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &gmpnumber_arg, &reps) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, gmpnumber_arg, temp_a);
 
 	RETVAL_LONG(mpz_probab_prime_p(gmpnum_a, reps));
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1677,7 +1779,9 @@ ZEND_FUNCTION(gmp_prob_prime)
    Computes greatest common denominator (gcd) of a and b */
 ZEND_FUNCTION(gmp_gcd)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_ui_op(mpz_gcd, (gmp_binary_ui_op_t) mpz_gcd_ui);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1692,6 +1796,7 @@ ZEND_FUNCTION(gmp_gcdext)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 	FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
@@ -1704,6 +1809,7 @@ ZEND_FUNCTION(gmp_gcdext)
 	mpz_gcdext(gmpnum_g, gmpnum_s, gmpnum_t, gmpnum_a, gmpnum_b);
 	FREE_GMP_TEMP(temp_a);
 	FREE_GMP_TEMP(temp_b);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1719,6 +1825,7 @@ ZEND_FUNCTION(gmp_invert)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &a_arg, &b_arg) == FAILURE){
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 	FETCH_GMP_ZVAL_DEP(gmpnum_b, b_arg, temp_b, temp_a);
@@ -1729,8 +1836,10 @@ ZEND_FUNCTION(gmp_invert)
 	FREE_GMP_TEMP(temp_b);
 	if (!res) {
 		zval_dtor(return_value);
+		GMP_END_MEMORY;
 		RETURN_FALSE;
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1738,7 +1847,9 @@ ZEND_FUNCTION(gmp_invert)
    Computes Jacobi symbol */
 ZEND_FUNCTION(gmp_jacobi)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_opl(mpz_jacobi);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1746,7 +1857,9 @@ ZEND_FUNCTION(gmp_jacobi)
    Computes Legendre symbol */
 ZEND_FUNCTION(gmp_legendre)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_opl(mpz_legendre);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1760,7 +1873,9 @@ ZEND_FUNCTION(gmp_cmp)
 		return;
 	}
 
+	GMP_SET_MEMORY;
 	gmp_cmp(return_value, a_arg, b_arg TSRMLS_CC);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1777,10 +1892,12 @@ ZEND_FUNCTION(gmp_sign)
 		return;
 	}
 
+	GMP_SET_MEMORY;
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	RETVAL_LONG(mpz_sgn(gmpnum_a));
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1794,6 +1911,7 @@ ZEND_FUNCTION(gmp_random)
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &limiter) == FAILURE) {
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	INIT_GMP_RETVAL(gmpnum_result);
 
@@ -1811,6 +1929,7 @@ ZEND_FUNCTION(gmp_random)
 #else
 	mpz_urandomb(gmpnum_result, GMPG(rand_state), GMP_ABS (limiter) * __GMP_BITS_PER_MP_LIMB);
 #endif
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1818,7 +1937,9 @@ ZEND_FUNCTION(gmp_random)
    Calculates logical AND of a and b */
 ZEND_FUNCTION(gmp_and)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_op(mpz_and);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1826,7 +1947,9 @@ ZEND_FUNCTION(gmp_and)
    Calculates logical OR of a and b */
 ZEND_FUNCTION(gmp_or)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_op(mpz_ior);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1834,7 +1957,9 @@ ZEND_FUNCTION(gmp_or)
    Calculates one's complement of a */
 ZEND_FUNCTION(gmp_com)
 {
+	GMP_SET_MEMORY;
 	gmp_unary_op(mpz_com);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1842,7 +1967,9 @@ ZEND_FUNCTION(gmp_com)
    Finds next prime of a */
 ZEND_FUNCTION(gmp_nextprime)
 {
+	GMP_SET_MEMORY;
    gmp_unary_op(mpz_nextprime);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1850,7 +1977,9 @@ ZEND_FUNCTION(gmp_nextprime)
    Calculates logical exclusive OR of a and b */
 ZEND_FUNCTION(gmp_xor)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_op(mpz_xor);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1871,6 +2000,7 @@ ZEND_FUNCTION(gmp_setbit)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero");
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	gmpnum_a = GET_GMP_FROM_ZVAL(a_arg);
 
@@ -1879,6 +2009,7 @@ ZEND_FUNCTION(gmp_setbit)
 	} else {
 		mpz_clrbit(gmpnum_a, index);
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1898,9 +2029,11 @@ ZEND_FUNCTION(gmp_clrbit)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero");
 		return;
 	}
+	GMP_SET_MEMORY;
 
 	gmpnum_a = GET_GMP_FROM_ZVAL(a_arg);
 	mpz_clrbit(gmpnum_a, index);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1921,10 +2054,12 @@ ZEND_FUNCTION(gmp_testbit)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Index must be greater than or equal to zero");
 		RETURN_FALSE;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 	RETVAL_BOOL(mpz_tstbit(gmpnum_a, index));
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1932,7 +2067,9 @@ ZEND_FUNCTION(gmp_testbit)
    Calculates the population count of a */
 ZEND_FUNCTION(gmp_popcount)
 {
+	GMP_SET_MEMORY;
 	gmp_unary_opl((gmp_unary_opl_t) mpz_popcount);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1940,7 +2077,9 @@ ZEND_FUNCTION(gmp_popcount)
    Calculates hamming distance between a and b */
 ZEND_FUNCTION(gmp_hamdist)
 {
+	GMP_SET_MEMORY;
 	gmp_binary_opl((gmp_binary_opl_t) mpz_hamdist);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1961,11 +2100,13 @@ ZEND_FUNCTION(gmp_scan0)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Starting index must be greater than or equal to zero");
 		RETURN_FALSE;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	RETVAL_LONG(mpz_scan0(gmpnum_a, start));
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1986,11 +2127,13 @@ ZEND_FUNCTION(gmp_scan1)
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Starting index must be greater than or equal to zero");
 		RETURN_FALSE;
 	}
+	GMP_SET_MEMORY;
 
 	FETCH_GMP_ZVAL(gmpnum_a, a_arg, temp_a);
 
 	RETVAL_LONG(mpz_scan1(gmpnum_a, start));
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 20 23:03:35 2021 UTC