Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 59845a9

Browse files
add tests for ZendMM Observer
1 parent fb65a9c commit 59845a9

File tree

10 files changed

+217
-2
lines changed

10 files changed

+217
-2
lines changed

‎ext/zend_test/config.m4‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ PHP_ARG_ENABLE([zend-test],
44
[Enable zend_test extension])])
55

66
if test "$PHP_ZEND_TEST" != "no"; then
7-
PHP_NEW_EXTENSION(zend_test, test.c observer.c fiber.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
7+
PHP_NEW_EXTENSION(zend_test, test.c observer.c fiber.c zendmm_observer.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
88
fi

‎ext/zend_test/config.w32‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
ARG_ENABLE("zend-test", "enable zend_test extension", "no");
44

55
if (PHP_ZEND_TEST != "no") {
6-
EXTENSION("zend_test", "test.c observer.c fiber.c", PHP_ZEND_TEST_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
6+
EXTENSION("zend_test", "test.c observer.c fiber.c zendmm_observer.c", PHP_ZEND_TEST_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
77
ADD_FLAG("CFLAGS_ZEND_TEST", "/D PHP_ZEND_TEST_EXPORTS ");
88
}

‎ext/zend_test/php_test.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define PHP_TEST_H
1919

2020
#include "fiber.h"
21+
#include "Zend/zend_alloc.h"
2122

2223
extern zend_module_entry zend_test_module_entry;
2324
#define phpext_zend_test_ptr &zend_test_module_entry
@@ -59,6 +60,8 @@ ZEND_BEGIN_MODULE_GLOBALS(zend_test)
5960
zend_long quantity_value;
6061
zend_string *str_test;
6162
zend_string *not_empty_str_test;
63+
int zendmm_observer_enabled;
64+
zend_mm_observer *observer;
6265
ZEND_END_MODULE_GLOBALS(zend_test)
6366

6467
extern ZEND_DECLARE_MODULE_GLOBALS(zend_test)

‎ext/zend_test/test.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ext/standard/info.h"
2424
#include "php_test.h"
2525
#include "observer.h"
26+
#include "zendmm_observer.h"
2627
#include "fiber.h"
2728
#include "zend_attributes.h"
2829
#include "zend_enum.h"
@@ -1031,6 +1032,7 @@ PHP_MINIT_FUNCTION(zend_test)
10311032
}
10321033

10331034
zend_test_observer_init(INIT_FUNC_ARGS_PASSTHRU);
1035+
zend_test_mm_observer_minit(INIT_FUNC_ARGS_PASSTHRU);
10341036
zend_test_fiber_init();
10351037

10361038
le_throwing_resource = zend_register_list_destructors_ex(le_throwing_resource_dtor, NULL, "throwing resource", module_number);
@@ -1057,6 +1059,7 @@ PHP_RINIT_FUNCTION(zend_test)
10571059
{
10581060
zend_hash_init(&ZT_G(global_weakmap), 8, NULL, ZVAL_PTR_DTOR, 0);
10591061
ZT_G(observer_nesting_depth) = 0;
1062+
zend_test_mm_observer_rinit();
10601063
return SUCCESS;
10611064
}
10621065

@@ -1067,6 +1070,7 @@ PHP_RSHUTDOWN_FUNCTION(zend_test)
10671070
zend_weakrefs_hash_del(&ZT_G(global_weakmap), zend_weakref_key_to_object(obj_key));
10681071
} ZEND_HASH_FOREACH_END();
10691072
zend_hash_destroy(&ZT_G(global_weakmap));
1073+
zend_test_mm_observer_rshutdown();
10701074
return SUCCESS;
10711075
}
10721076

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
ZendMM Observer: Observe all
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
zend_test.zendmm_observer.enabled=1
7+
opcache.enable=0
8+
--FILE--
9+
<?php
10+
?>
11+
--EXPECTREGEX--
12+
.*
13+
ZendMM Observer enabled
14+
.*
15+
.*
16+
ZendMM Observer disabled
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
ZendMM Observer: Observe all
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
zend_test.zendmm_observer.enabled=0
7+
opcache.enable=0
8+
--FILE--
9+
<?php
10+
ini_set('zend_test.zendmm_observer.enabled', 'true');
11+
$str0 = str_repeat("ZendMM Observer", 100);
12+
ini_set('zend_test.zendmm_observer.enabled', 'false');
13+
14+
$str1 = str_repeat("ZendMM Observer", 100);
15+
16+
ini_set('zend_test.zendmm_observer.enabled', 'true');
17+
$str2 = str_repeat("ZendMM Observer", 100);
18+
unset($str2);
19+
ini_set('zend_test.zendmm_observer.enabled', 'false');
20+
?>
21+
--EXPECTF--
22+
malloc 0x%s of size %d (block: %d)
23+
malloc 0x%s of size %d (block: %d)
24+
freed 0x%s of size %d

‎ext/zend_test/zendmm_observer.c‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "php.h"
18+
#include "php_test.h"
19+
#include "Zend/zend_alloc.h"
20+
#include "ext/standard/info.h"
21+
#include "ext/standard/php_var.h"
22+
#include "zendmm_observer.h"
23+
24+
void observer_malloc(size_t len, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
25+
{
26+
size_t block_len = zend_mm_block_size(zend_mm_get_heap(), ptr);
27+
printf("malloc %p of size %zu (block: %zu)\n", ptr, len, block_len);
28+
}
29+
30+
void observer_free(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
31+
{
32+
size_t block_len = zend_mm_block_size(zend_mm_get_heap(), ptr);
33+
printf("freed %p of size %zu\n", ptr, block_len);
34+
}
35+
36+
void observer_realloc(void *ptr, size_t len, void *newptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
37+
{
38+
size_t block_len = zend_mm_block_size(zend_mm_get_heap(), newptr);
39+
printf("realloc %p of size %zu (block: %zu, former %p)\n", newptr, len, block_len, ptr);
40+
}
41+
42+
PHP_FUNCTION(memprof_enable)
43+
{
44+
ZEND_PARSE_PARAMETERS_NONE();
45+
if (ZT_G(observer)) {
46+
RETURN_FALSE;
47+
}
48+
ZT_G(observer) = zend_mm_observer_register(zend_mm_get_heap(), observer_malloc, observer_free, observer_realloc);
49+
RETURN_TRUE;
50+
}
51+
52+
PHP_FUNCTION(memprof_disable)
53+
{
54+
ZEND_PARSE_PARAMETERS_NONE();
55+
zend_mm_observer_unregister(zend_mm_get_heap(), ZT_G(observer));
56+
ZT_G(observer) = NULL;
57+
RETURN_TRUE;
58+
}
59+
60+
static PHP_INI_MH(OnUpdate_zend_test_zendmm_observer_enabled)
61+
{
62+
int int_value;
63+
if (new_value == NULL) {
64+
return FAILURE;
65+
}
66+
67+
if (zend_string_equals_literal_ci(new_value, "true")) {
68+
int_value = 1;
69+
} else if (zend_string_equals_literal_ci(new_value, "false")) {
70+
int_value = 0;
71+
} else {
72+
int_value = (int) zend_ini_parse_quantity_warn(new_value, entry->name);
73+
}
74+
75+
if (int_value == 1) {
76+
if (ZT_G(observer) == NULL) {
77+
ZT_G(observer) = zend_mm_observer_register(zend_mm_get_heap(), observer_malloc, observer_free, observer_realloc);
78+
}
79+
} else {
80+
if (ZT_G(observer) != NULL) {
81+
zend_mm_observer_unregister(zend_mm_get_heap(), ZT_G(observer));
82+
ZT_G(observer) = NULL;
83+
}
84+
}
85+
return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
86+
}
87+
88+
PHP_INI_BEGIN()
89+
STD_PHP_INI_BOOLEAN("zend_test.zendmm_observer.enabled", "0", PHP_INI_ALL, OnUpdate_zend_test_zendmm_observer_enabled, zendmm_observer_enabled, zend_zend_test_globals, zend_test_globals)
90+
PHP_INI_END()
91+
92+
void zend_test_mm_observer_minit(INIT_FUNC_ARGS)
93+
{
94+
if (type != MODULE_TEMPORARY) {
95+
REGISTER_INI_ENTRIES();
96+
} else {
97+
(void)ini_entries;
98+
}
99+
}
100+
101+
void zend_test_mm_observer_rinit(void)
102+
{
103+
if (ZT_G(zendmm_observer_enabled)) {
104+
printf("ZendMM Observer enabled\n");
105+
ZT_G(observer) = zend_mm_observer_register(zend_mm_get_heap(), observer_malloc, observer_free, observer_realloc);
106+
}
107+
}
108+
109+
void zend_test_mm_observer_rshutdown(void)
110+
{
111+
if (ZT_G(observer)) {
112+
printf("ZendMM Observer disabled\n");
113+
zend_mm_observer_unregister(zend_mm_get_heap(), ZT_G(observer));
114+
}
115+
ZT_G(observer) = NULL;
116+
}
117+

‎ext/zend_test/zendmm_observer.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_TEST_MM_OBSERVER_H
18+
#define ZEND_TEST_MM_OBSERVER_H
19+
20+
void zend_test_mm_observer_minit(INIT_FUNC_ARGS);
21+
void zend_test_mm_observer_rinit(void);
22+
void zend_test_mm_observer_rshutdown(void);
23+
24+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
/**
4+
* @generate-class-entries static
5+
* @undocumentable
6+
*/
7+
8+
function memprof_enable(): bool {}
9+
function memprof_disable(): bool {}

‎ext/zend_test/zendmm_observer_arginfo.h‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /