|
| 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 | + |
0 commit comments