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:16 UTC

Return to Bug #63595 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: remi@php.net

--- gmp.c.old	2014-10-11 08:03:14.764951256 +0200
+++ gmp.c	2014-10-11 08:13:30.892303049 +0200
@@ -352,6 +352,16 @@ 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);
+
+#define GMP_SET_MEMORY do { \
+  mp_get_memory_functions(&_save_malloc, &_save_realloc, &_save_free); \
+  mp_set_memory_functions(gmp_emalloc, gmp_erealloc, gmp_efree); \
+  } while (0)
+#define GMP_END_MEMORY mp_set_memory_functions(_save_malloc, _save_realloc, _save_free)
+
 /* {{{ gmp_emalloc
  */
 static void *gmp_emalloc(size_t size)
@@ -391,7 +401,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 +418,9 @@ 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(
@@ -723,7 +737,7 @@ 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);
 
 	return SUCCESS;
 }
@@ -1085,12 +1099,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 +1167,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 +1202,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 +1223,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 +1241,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 +1264,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 +1283,7 @@ ZEND_FUNCTION(gmp_strval)
 	gmp_strval(return_value, gmpnum, base);
 
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1261,7 +1291,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 +1301,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 +1311,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 +1327,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 +1341,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 +1358,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 +1372,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 +1389,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 +1403,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 +1414,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 +1424,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 +1434,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 +1444,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 +1459,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 +1498,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 +1508,7 @@ ZEND_FUNCTION(gmp_pow)
 		mpz_pow_ui(gmpnum_result, gmpnum_base, exp);
 		FREE_GMP_TEMP(temp_base);
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1466,6 +1524,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 +1537,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 +1548,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 +1562,7 @@ ZEND_FUNCTION(gmp_powm)
 
 	FREE_GMP_TEMP(temp_base);
 	FREE_GMP_TEMP(temp_mod);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1515,18 +1577,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 +1606,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 +1623,7 @@ ZEND_FUNCTION(gmp_sqrtrem)
 
 	mpz_sqrtrem(gmpnum_result1, gmpnum_result2, gmpnum_a);
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1576,18 +1644,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 +1679,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 +1704,7 @@ ZEND_FUNCTION(gmp_rootrem)
 #endif
 
 	FREE_GMP_TEMP(temp_a);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1645,11 +1719,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 +1741,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 +1755,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 +1772,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 +1785,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 +1801,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 +1812,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 +1823,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 +1833,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 +1849,9 @@ ZEND_FUNCTION(gmp_cmp)
 		return;
 	}
 
+	GMP_SET_MEMORY;
 	gmp_cmp(return_value, a_arg, b_arg TSRMLS_CC);
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1777,10 +1868,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 +1887,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 +1905,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 +1913,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 +1923,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 +1933,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 +1943,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 +1953,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 +1976,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 +1985,7 @@ ZEND_FUNCTION(gmp_setbit)
 	} else {
 		mpz_clrbit(gmpnum_a, index);
 	}
+	GMP_END_MEMORY;
 }
 /* }}} */
 
@@ -1898,9 +2005,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 +2030,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 +2043,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 +2053,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 +2076,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 +2103,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-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 19:01:29 2024 UTC