-
Notifications
You must be signed in to change notification settings - Fork 19
/
php_decimal.c
185 lines (163 loc) · 4.74 KB
/
php_decimal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* The MIT License (MIT)
* Copyright (c) 2018 Rudi Theunissen
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include <ext/standard/info.h>
#include "php_decimal.h"
#include "src/context.h"
#include "src/decimal.h"
#include "src/errors.h"
#include "src/number.h"
#include "src/number.h"
#include "src/rational.h"
/**
* Set custom allocators.
*/
static void *php_decimal_mpd_malloc(size_t size)
{
return emalloc(size);
}
static void *php_decimal_mpd_calloc(size_t nmemb, size_t size)
{
return ecalloc(nmemb, size);
}
static void *php_decimal_mpd_realloc(void *ptr, size_t size)
{
return erealloc(ptr, size);
}
static void php_decimal_mpd_free(void *ptr)
{
efree(ptr);
}
/**
* The second Opcache pass will convert numeric string constants to float,
* so statements like `$decimal * "0.75"` will throw because floats are not
* supported. Otherwise, this conversion will be transparent which means you
* are using float internally when your code uses a string.
*
* Disabling it gives us guaranteed consistency at a small performance cost.
*/
static void php_decimal_disable_opcache_pass2()
{
zend_long level = INI_INT("opcache.optimization_level");
if (level & 2) {
zend_string *key = zend_string_init(ZEND_STRL("opcache.optimization_level"), 1);
zend_string *val = strpprintf(0, "0x%08X", (unsigned int) (level & ~2));
zend_alter_ini_entry(key, val, ZEND_INI_SYSTEM, ZEND_INI_STAGE_ACTIVATE);
zend_string_release(key);
zend_string_release(val);
}
}
/**
* Module information displayed by phpinfo()
*/
PHP_MINFO_FUNCTION(decimal)
{
php_info_print_table_start();
php_info_print_table_row(2, "decimal support", "enabled");
php_info_print_table_row(2, "decimal version", PHP_DECIMAL_VERSION);
php_info_print_table_row(2, "libmpdec version", MPD_VERSION);
php_info_print_table_end();
}
/**
* Module startup
*/
PHP_MINIT_FUNCTION(decimal)
{
/* Set guaranteed minimum number of coefficient words based on default prec. */
mpd_setminalloc(2 * ((PHP_DECIMAL_DEFAULT_PREC + MPD_RDIGITS - 1) / MPD_RDIGITS));
/* Set custom memory allocation and trap handler functions. */
mpd_callocfunc = php_decimal_mpd_calloc;
mpd_mallocfunc = php_decimal_mpd_malloc;
mpd_reallocfunc = php_decimal_mpd_realloc;
mpd_free = php_decimal_mpd_free;
mpd_traphandler = php_decimal_mpd_traphandler;
php_decimal_register_number_class();
php_decimal_register_decimal_class();
php_decimal_register_rational_class();
return SUCCESS;
}
/**
* Module shutdown
*/
PHP_MSHUTDOWN_FUNCTION(decimal)
{
return SUCCESS;
}
/**
* Request startup
*/
PHP_RINIT_FUNCTION(decimal)
{
#if defined(COMPILE_DL_DECIMAL) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
php_decimal_disable_opcache_pass2();
php_decimal_context_init();
return SUCCESS;
}
/**
* Request shutdown
*/
PHP_RSHUTDOWN_FUNCTION(decimal)
{
php_decimal_context_dtor();
return SUCCESS;
}
/**
* globals init
*/
static PHP_GINIT_FUNCTION(decimal)
{
#if defined(COMPILE_DL_DECIMAL) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
php_decimal_init_globals(decimal_globals);
}
/**
* Module entry
*/
zend_module_entry decimal_module_entry = {
STANDARD_MODULE_HEADER,
PHP_DECIMAL_EXTNAME,
NULL, /* no functions */
PHP_MINIT(decimal),
PHP_MSHUTDOWN(decimal),
PHP_RINIT(decimal),
PHP_RSHUTDOWN(decimal),
PHP_MINFO(decimal),
PHP_DECIMAL_VERSION,
PHP_MODULE_GLOBALS(decimal),
PHP_GINIT(decimal),
NULL, // GSHUTDOWN
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_DECIMAL
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(decimal)
#endif