Skip to content

Commit e4fbec0

Browse files
committed
libxml: use php_stream_context* for stream_context global
Rather than a zval, saving 8 bytes
1 parent 6741f32 commit e4fbec0

4 files changed

Lines changed: 34 additions & 37 deletions

File tree

‎UPGRADING.INTERNALS‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ PHP 8.7 INTERNALS UPGRADE NOTES
125125
. Added php_date_time_duration_create() to create Time\Duration objects
126126
without going through the userland API.
127127

128+
- libxml:
129+
. The signature of php_libxml_switch_context() changed from
130+
void php_libxml_switch_context(const zval *context, zval *oldcontext) to
131+
php_stream_context *php_libxml_switch_context(php_stream_context *context)
132+
128133
- MySQLi:
129134
. Removed the reverse API. The mysqlnd_reverse_api.h header, the
130135
MYSQLND_REVERSE_API struct and the functions zval_to_mysqlnd(),

‎ext/libxml/libxml.c‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,19 @@ static PHP_GINIT_FUNCTION(libxml)
409409
#if defined(COMPILE_DL_LIBXML) && defined(ZTS)
410410
ZEND_TSRMLS_CACHE_UPDATE();
411411
#endif
412-
ZVAL_UNDEF(&libxml_globals->stream_context);
412+
libxml_globals->stream_context = NULL;
413413
libxml_globals->error_buffer.s = NULL;
414414
libxml_globals->error_list = NULL;
415415
libxml_globals->entity_loader_callback = empty_fcall_info_cache;
416416
}
417417

418418
PHP_LIBXML_API php_stream_context *php_libxml_get_stream_context(void)
419419
{
420-
return php_stream_context_from_zval(Z_ISUNDEF(LIBXML(stream_context)) ? NULL : &LIBXML(stream_context), false);
420+
if (LIBXML(stream_context)) {
421+
return LIBXML(stream_context);
422+
} else {
423+
return php_stream_context_get_default(false);
424+
}
421425
}
422426

423427
/* Channel libxml file io layer through the PHP streams subsystem.
@@ -973,14 +977,11 @@ PHP_LIBXML_API void php_libxml_shutdown(void)
973977
}
974978
}
975979

976-
PHP_LIBXML_API void php_libxml_switch_context(const zval *context, zval *oldcontext)
980+
PHP_LIBXML_API php_stream_context *php_libxml_switch_context(php_stream_context *context)
977981
{
978-
if (oldcontext) {
979-
ZVAL_COPY_VALUE(oldcontext, &LIBXML(stream_context));
980-
}
981-
if (context) {
982-
ZVAL_COPY_VALUE(&LIBXML(stream_context), context);
983-
}
982+
php_stream_context *old_context = LIBXML(stream_context);
983+
LIBXML(stream_context) = context;
984+
return old_context;
984985
}
985986

986987
static PHP_MINIT_FUNCTION(libxml)
@@ -1072,7 +1073,7 @@ static zend_result php_libxml_post_deactivate(void)
10721073
xmlSetStructuredErrorFunc(NULL, NULL);
10731074

10741075
/* the steam_context resource will be released by resource list destructor */
1075-
ZVAL_UNDEF(&LIBXML(stream_context));
1076+
LIBXML(stream_context) = NULL;
10761077
smart_str_free(&LIBXML(error_buffer));
10771078
if (LIBXML(error_list)) {
10781079
zend_llist_destroy(LIBXML(error_list));
@@ -1105,11 +1106,14 @@ PHP_FUNCTION(libxml_set_streams_context)
11051106
Z_PARAM_RESOURCE(arg)
11061107
ZEND_PARSE_PARAMETERS_END();
11071108

1108-
if (php_stream_context_from_zval(arg, true) != NULL) {
1109-
if (!Z_ISUNDEF(LIBXML(stream_context))) {
1110-
zval_ptr_dtor(&LIBXML(stream_context));
1109+
php_stream_context *context = php_stream_context_from_zval(arg, true);
1110+
if (context != NULL) {
1111+
if (LIBXML(stream_context)) {
1112+
/* Close resource associated to stream context, which will take
1113+
* care of freeing the rest of the stream context. */
1114+
zend_list_close(LIBXML(stream_context)->res);
11111115
}
1112-
ZVAL_COPY(&LIBXML(stream_context), arg);
1116+
LIBXML(stream_context) = context;
11131117
}
11141118
}
11151119
/* }}} */

‎ext/libxml/php_libxml.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern zend_module_entry libxml_module_entry;
4040
#define LIBXML_NS_TAG_HOOK 1
4141

4242
ZEND_BEGIN_MODULE_GLOBALS(libxml)
43-
zval stream_context;
43+
php_stream_context *stream_context;
4444
smart_str error_buffer;
4545
zend_llist *error_list;
4646
zend_fcall_info_cache entity_loader_callback;
@@ -205,7 +205,7 @@ PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...);
205205
PHP_LIBXML_API void php_libxml_pretend_ctx_error_ex(const char *file, int line, int column, const char *msg,...);
206206
PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...);
207207
PHP_LIBXML_API void php_libxml_error_handler_va(php_libxml_error_level error_type, void *ctx, const char *msg, va_list args);
208-
PHP_LIBXML_API void php_libxml_switch_context(const zval *context, zval *oldcontext);
208+
PHP_LIBXML_API php_stream_context *php_libxml_switch_context(php_stream_context *context);
209209
PHP_LIBXML_API void php_libxml_issue_error(int level, const char *msg);
210210
PHP_LIBXML_API bool php_libxml_disable_entity_loader(bool disable);
211211
PHP_LIBXML_API void php_libxml_set_old_ns(xmlDocPtr doc, xmlNsPtr ns);

‎ext/soap/php_sdl.c‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ void sdl_set_uri_credentials(sdlCtx *ctx, char *uri)
271271
{
272272
char *s;
273273
size_t l1, l2;
274-
zval context;
275274
zval *header = NULL;
276275

277276
/* check if we load xsd from the same server */
@@ -320,18 +319,13 @@ void sdl_set_uri_credentials(sdlCtx *ctx, char *uri)
320319
if (l1 == l2 && memcmp(ctx->sdl->source, uri, l1) == 0) {
321320
return;
322321
}
323-
/* another server. clear authentication credentals */
324-
php_libxml_switch_context(NULL, &context);
325-
php_libxml_switch_context(&context, NULL);
326-
if (Z_TYPE(context) == IS_UNDEF) {
327-
return;
328-
}
329-
zval *context_ptr = &context;
330-
ctx->context = php_stream_context_from_zval(context_ptr, 1);
331-
332-
if (!ctx->context) {
322+
/* another server. clear authentication credentials */
323+
php_stream_context *context = php_libxml_switch_context(NULL);
324+
php_libxml_switch_context(context);
325+
if (context == NULL) {
333326
return;
334327
}
328+
ctx->context = context;
335329

336330
header = php_stream_context_get_option(ctx->context, "http", "header");
337331
if (header == NULL) {
@@ -3316,15 +3310,13 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
33163310
char* old_error_code = SOAP_GLOBAL(error_code);
33173311
size_t uri_len = 0;
33183312
php_stream_context *context=NULL;
3319-
zval *tmp, orig_context, new_context;
3313+
zval *tmp;
33203314
smart_str headers = {0};
33213315
char* key = NULL;
33223316
time_t t = time(0);
33233317
bool has_proxy_authorization = false;
33243318
bool has_authorization = false;
33253319

3326-
ZVAL_UNDEF(&orig_context);
3327-
ZVAL_UNDEF(&new_context);
33283320
if (strchr(uri,':') != NULL || IS_ABSOLUTE_PATH(uri, uri_len)) {
33293321
uri_len = strlen(uri);
33303322
} else if (VCWD_REALPATH(uri, fn) == NULL) {
@@ -3397,9 +3389,6 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
33973389
tmp = Z_CLIENT_STREAM_CONTEXT_P(this_ptr);
33983390
if (Z_TYPE_P(tmp) == IS_RESOURCE) {
33993391
context = php_stream_context_from_zval(tmp, 0);
3400-
/* Share a reference with new_context down below.
3401-
* For new contexts, the reference is only in new_context so that doesn't need extra refcounting. */
3402-
GC_ADDREF(context->res);
34033392
}
34043393

34053394
tmp = Z_CLIENT_USER_AGENT_P(this_ptr);
@@ -3465,9 +3454,9 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
34653454
zval_ptr_dtor(&str_headers);
34663455
}
34673456

3457+
php_stream_context *orig_context = NULL;
34683458
if (context) {
3469-
ZVAL_RES(&new_context, context->res);
3470-
php_libxml_switch_context(&new_context, &orig_context);
3459+
orig_context = php_libxml_switch_context(context);
34713460
}
34723461

34733462
SOAP_GLOBAL(error_code) = "WSDL";
@@ -3480,8 +3469,7 @@ sdlPtr get_sdl(zval *this_ptr, char *uri, zend_long cache_wsdl)
34803469
SOAP_GLOBAL(error_code) = old_error_code;
34813470

34823471
if (context) {
3483-
php_libxml_switch_context(&orig_context, NULL);
3484-
zval_ptr_dtor(&new_context);
3472+
php_libxml_switch_context(orig_context);
34853473
}
34863474

34873475
if ((cache_wsdl & WSDL_CACHE_DISK) && key) {

0 commit comments

Comments
 (0)