Skip to content

Commit e05cba0

Browse files
committed
Make GMP more usable by third-party extensions.
Export a PHPAPI function to return gmp_ce (and make the actual storage static). Provide gmp_object struct in header w/ inline accessor. Install php_gmp_int.h header. Remove unnecessary `#ifdef HAVE_GMP` checks.
1 parent 39ded1d commit e05cba0

File tree

6 files changed

+42
-27
lines changed

6 files changed

+42
-27
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ PHP NEWS
3232
- GD:
3333
. Added support for WebP in imagecreatefromstring() (Andreas Treichel, cmb).
3434

35+
- GMP:
36+
. Export internal structures and accessor helpers for GMP object. (Sara)
37+
3538
- LDAP:
3639
. Added ldap_exop_refresh helper for EXOP REFRESH operation with dds overlay.
3740
(Come)

ext/gmp/config.m4

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if test "$PHP_GMP" != "no"; then
2323

2424
PHP_ADD_LIBRARY_WITH_PATH(gmp, $GMP_DIR/$PHP_LIBDIR, GMP_SHARED_LIBADD)
2525
PHP_ADD_INCLUDE($GMP_DIR/include)
26+
PHP_INSTALL_HEADERS([ext/gmp/php_gmp_int.h])
2627

2728
PHP_NEW_EXTENSION(gmp, gmp.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
2829
PHP_SUBST(GMP_SHARED_LIBADD)

ext/gmp/config.w32

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (PHP_GMP != "no") {
77
if (CHECK_LIB("mpir_a.lib", "gmp", PHP_GMP) &&
88
CHECK_HEADER_ADD_INCLUDE("gmp.h", "CFLAGS_GMP", PHP_GMP + ";" + PHP_PHP_BUILD + "\\include\\mpir")) {
99
EXTENSION("gmp", "gmp.c", null, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
10+
PHP_INSTALL_HEADERS("ext/gmp", "php_gmp_int.h");
1011
AC_DEFINE('HAVE_GMP', 1, 'GMP support');
1112
AC_DEFINE('HAVE_MPIR', 1, 'MPIR support');
1213
} else {

ext/gmp/gmp.c

+6-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323
#include "php.h"
2424
#include "php_ini.h"
2525
#include "php_gmp.h"
26+
#include "php_gmp_int.h"
2627
#include "ext/standard/info.h"
2728
#include "ext/standard/php_var.h"
2829
#include "zend_smart_str_public.h"
2930
#include "zend_exceptions.h"
3031

31-
#if HAVE_GMP
32-
3332
#include <gmp.h>
3433

3534
/* Needed for gmp_random() */
@@ -220,13 +219,12 @@ ZEND_TSRMLS_CACHE_DEFINE()
220219
ZEND_GET_MODULE(gmp)
221220
#endif
222221

223-
zend_class_entry *gmp_ce;
222+
static zend_class_entry *gmp_ce;
224223
static zend_object_handlers gmp_object_handlers;
225224

226-
typedef struct _gmp_object {
227-
mpz_t num;
228-
zend_object std;
229-
} gmp_object;
225+
zend_class_entry *php_gmp_class_entry() {
226+
return gmp_ce;
227+
}
230228

231229
typedef struct _gmp_temp {
232230
mpz_t num;
@@ -252,7 +250,7 @@ typedef struct _gmp_temp {
252250
(Z_TYPE_P(zval) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zval), gmp_ce))
253251

254252
#define GET_GMP_OBJECT_FROM_OBJ(obj) \
255-
((gmp_object *) ((char *) (obj) - XtOffsetOf(gmp_object, std)))
253+
php_gmp_object_from_zend_object(obj)
256254
#define GET_GMP_OBJECT_FROM_ZVAL(zv) \
257255
GET_GMP_OBJECT_FROM_OBJ(Z_OBJ_P(zv))
258256

@@ -2088,8 +2086,6 @@ ZEND_FUNCTION(gmp_scan1)
20882086
}
20892087
/* }}} */
20902088

2091-
#endif /* HAVE_GMP */
2092-
20932089
/*
20942090
* Local variables:
20952091
* tab-width: 4

ext/gmp/php_gmp.h

-17
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#ifndef PHP_GMP_H
2020
#define PHP_GMP_H
2121

22-
#if HAVE_GMP
23-
2422
#include <gmp.h>
2523

2624
extern zend_module_entry gmp_module_entry;
@@ -81,15 +79,6 @@ ZEND_FUNCTION(gmp_popcount);
8179
ZEND_FUNCTION(gmp_hamdist);
8280
ZEND_FUNCTION(gmp_nextprime);
8381

84-
/* GMP and MPIR use different datatypes on different platforms */
85-
#ifdef PHP_WIN32
86-
typedef zend_long gmp_long;
87-
typedef zend_ulong gmp_ulong;
88-
#else
89-
typedef long gmp_long;
90-
typedef unsigned long gmp_ulong;
91-
#endif
92-
9382
ZEND_BEGIN_MODULE_GLOBALS(gmp)
9483
zend_bool rand_initialized;
9584
gmp_randstate_t rand_state;
@@ -101,12 +90,6 @@ ZEND_END_MODULE_GLOBALS(gmp)
10190
ZEND_TSRMLS_CACHE_EXTERN()
10291
#endif
10392

104-
#else
105-
106-
#define phpext_gmp_ptr NULL
107-
108-
#endif
109-
11093
#endif /* PHP_GMP_H */
11194

11295

ext/gmp/php_gmp_int.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef incl_PHP_GMP_INT_H
2+
#define incl_PHP_GMP_INT_H
3+
4+
#ifdef HAVE_CONFIG_H
5+
#include "config.h"
6+
#endif
7+
8+
#include "php.h"
9+
#include <gmp.h>
10+
11+
typedef struct _gmp_object {
12+
mpz_t num;
13+
zend_object std;
14+
} gmp_object;
15+
16+
static inline gmp_object *php_gmp_object_from_zend_object(zend_object *zobj) {
17+
return (gmp_object *)( ((char *)zobj) - XtOffsetOf(gmp_object, std) );
18+
}
19+
20+
PHPAPI zend_class_entry *php_gmp_class_entry();
21+
22+
/* GMP and MPIR use different datatypes on different platforms */
23+
#ifdef PHP_WIN32
24+
typedef zend_long gmp_long;
25+
typedef zend_ulong gmp_ulong;
26+
#else
27+
typedef long gmp_long;
28+
typedef unsigned long gmp_ulong;
29+
#endif
30+
31+
#endif

0 commit comments

Comments
 (0)