From 91a1f28440194c7131e1fe0ee061b9054f3b69d7 Mon Sep 17 00:00:00 2001 From: Vadim Sadokhov <65451602+astrophysik@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:55:53 +0300 Subject: [PATCH] separate runtime to core and base parts (#1006) --- CMakeLists.txt | 1 + builtin-functions/_functions.txt | 2 +- common/stats/provider.h | 1 + .../contributing-to-kphp.md | 2 +- .../allocator/script-allocator-managed.h | 27 ++ .../class-instance/class-instance-decl.inl | 2 +- .../class-instance/class-instance.inl | 5 +- .../class-instance/refcountable-php-classes.h | 8 +- .../core-types}/comparison_operators.inl | 8 +- .../core-types}/conversions_types.inl | 2 +- .../core-types/decl}/array_decl.inl | 6 +- .../core-types/decl}/array_iterator.h | 2 +- .../core-types/decl}/declarations.h | 0 .../core-types/decl}/mixed_decl.inl | 2 +- .../core-types/decl}/optional.h | 4 +- .../core-types/decl}/shape.h | 0 .../core-types/decl}/string_buffer_decl.inl | 14 +- .../core-types/decl}/string_decl.inl | 2 +- .../core-types/definition}/array.inl | 18 +- .../core-types/definition}/mixed.cpp | 2 +- .../core-types/definition}/mixed.inl | 8 +- .../core-types/definition}/string.cpp | 2 +- .../core-types/definition}/string.inl | 16 +- .../core-types/definition/string_buffer.cpp | 15 + .../core-types/definition}/string_buffer.inl | 35 +-- .../core-types/definition}/string_cache.cpp | 2 +- .../core-types/definition}/string_cache.h | 2 +- .../core-types}/kphp_type_traits.h | 6 +- {runtime => runtime-core}/include.h | 8 +- .../details/memory_chunk_list.h | 2 +- .../details/memory_chunk_tree.cpp | 6 +- .../details/memory_chunk_tree.h | 2 +- .../details/memory_ordered_chunk_list.cpp | 2 +- .../details/memory_ordered_chunk_list.h | 4 +- .../details/universal_reallocate.h | 2 +- .../memory-resource}/extra-memory-pool.h | 0 .../memory-resource}/memory_resource.h | 4 - .../monotonic_buffer_resource.cpp | 19 ++ .../monotonic_buffer_resource.h | 9 +- .../memory-resource}/resource_allocator.h | 2 +- .../unsynchronized_pool_resource.cpp | 4 +- .../unsynchronized_pool_resource.h | 14 +- runtime-core/runtime-core-context.h | 41 +++ runtime-core/runtime-core.cmake | 25 ++ .../runtime-core.h | 113 ++------ runtime-core/utils/kphp-assert-core.h | 23 ++ .../utils/migration-php8.cpp | 9 +- .../utils/migration-php8.h | 2 - runtime/allocator.cpp | 2 +- runtime/allocator.h | 2 +- runtime/array_functions.h | 5 +- runtime/bcmath.cpp | 1 + runtime/bcmath.h | 2 +- runtime/common_template_instantiations.h | 2 +- runtime/confdata-functions.h | 2 +- runtime/confdata-global-manager.h | 6 +- runtime/confdata-keys.h | 3 +- runtime/context/runtime-context.cpp | 21 ++ runtime/context/runtime-context.h | 21 ++ runtime/context/runtime-core-allocator.cpp | 47 +++ runtime/context/runtime-core-context.cpp | 24 ++ runtime/ctype.h | 2 +- runtime/curl-async.h | 2 +- runtime/curl.cpp | 5 +- runtime/curl.h | 5 +- runtime/datetime/date_interval.h | 2 +- runtime/datetime/datetime.h | 6 +- runtime/datetime/datetime_functions.cpp | 3 +- runtime/datetime/datetime_functions.h | 2 +- runtime/datetime/datetime_immutable.h | 4 +- runtime/datetime/datetime_interface.h | 2 +- runtime/datetime/datetime_zone.h | 4 +- runtime/datetime/timelib_wrapper.cpp | 6 +- runtime/datetime/timelib_wrapper.h | 2 +- runtime/env.h | 2 +- runtime/exception.cpp | 9 +- runtime/exception.h | 6 +- runtime/exec.h | 2 +- runtime/ffi.h | 4 +- runtime/files.cpp | 5 +- runtime/files.h | 2 +- runtime/from-json-processor.h | 6 +- runtime/instance-cache.cpp | 4 +- runtime/instance-cache.h | 3 +- runtime/instance-copy-processor.h | 4 +- runtime/interface.cpp | 66 +++-- runtime/interface.h | 3 +- runtime/job-workers/client-functions.h | 2 +- runtime/job-workers/job-interface.h | 4 +- runtime/job-workers/processing-jobs.h | 2 +- runtime/job-workers/server-functions.h | 2 +- runtime/json-functions.cpp | 122 ++++---- runtime/json-functions.h | 33 +-- runtime/json-writer.cpp | 59 ++-- runtime/json-writer.h | 3 +- runtime/kphp-backtrace.h | 4 +- runtime/kphp_ml/kphp_ml.cpp | 2 +- runtime/kphp_ml/kphp_ml_catboost.cpp | 2 +- runtime/kphp_ml/kphp_ml_catboost.h | 2 +- runtime/kphp_ml/kphp_ml_init.h | 2 +- runtime/kphp_ml/kphp_ml_interface.h | 2 +- runtime/kphp_ml/kphp_ml_xgboost.cpp | 2 +- runtime/kphp_ml/kphp_ml_xgboost.h | 2 +- runtime/kphp_tracing.h | 4 +- runtime/kphp_tracing_binlog.cpp | 3 + runtime/kphp_tracing_binlog.h | 2 +- runtime/mail.h | 2 +- runtime/math_functions.h | 2 +- runtime/mbstring.h | 2 +- runtime/memcache.h | 6 +- runtime/memory_resource/memory_resource.cpp | 20 -- .../dealer.cpp | 2 +- .../dealer.h | 4 +- .../heap_resource.cpp | 2 +- .../heap_resource.h | 2 +- .../memory_resource_stats.cpp | 16 ++ .../memory_resource_stats.h | 10 + .../monotonic_runtime_buffer_resource.cpp} | 56 ++-- runtime/memory_usage.h | 46 ++- runtime/misc.cpp | 18 +- runtime/misc.h | 2 +- runtime/msgpack-serialization.h | 9 +- runtime/msgpack/adaptors.h | 2 +- runtime/msgpack/packer.cpp | 2 +- runtime/msgpack/unpacker.h | 2 +- runtime/mysql.h | 4 +- runtime/null_coalesce.h | 4 +- runtime/on_kphp_warning_callback.h | 2 +- runtime/openssl.cpp | 7 +- runtime/openssl.h | 2 +- runtime/pdo/abstract_pdo_driver.h | 2 +- runtime/pdo/abstract_pdo_statement.h | 2 +- runtime/pdo/mysql/mysql_pdo_driver.h | 2 +- .../pdo/mysql/mysql_pdo_emulated_statement.h | 2 +- runtime/pdo/pdo.h | 4 +- runtime/pdo/pdo_statement.h | 5 +- runtime/pdo/pgsql/pgsql_pdo_driver.h | 2 +- .../pgsql/pgsql_pdo_emulated_statement.cpp | 4 +- .../pdo/pgsql/pgsql_pdo_emulated_statement.h | 2 +- runtime/php-script-globals.h | 2 +- runtime/php_assert.cpp | 16 ++ runtime/php_assert.h | 24 +- runtime/profiler.h | 3 +- runtime/regexp.cpp | 35 ++- runtime/regexp.h | 14 +- runtime/resumable.h | 2 +- runtime/rpc.cpp | 9 +- runtime/rpc.h | 4 +- runtime/rpc_extra_info.h | 7 +- runtime/runtime.cmake | 21 +- runtime/serialize-functions.cpp | 50 ++-- runtime/serialize-functions.h | 16 +- runtime/spl/array_iterator.h | 4 +- runtime/storage.h | 2 +- runtime/streams.cpp | 1 + runtime/streams.h | 2 +- runtime/string-list.h | 3 +- runtime/string_buffer.cpp | 24 -- runtime/string_functions.cpp | 271 +++++++++--------- runtime/string_functions.h | 2 +- runtime/tcp.cpp | 1 + runtime/tcp.h | 2 +- runtime/tl/rpc_function.h | 4 +- runtime/tl/rpc_req_error.h | 2 +- runtime/tl/rpc_response.h | 4 +- runtime/tl/rpc_tl_query.h | 7 +- runtime/tl/tl_builtins.h | 2 +- runtime/tl/tl_func_base.h | 3 +- runtime/tl/tl_magics_decoding.h | 2 +- runtime/to-array-processor.h | 2 +- runtime/to-json-processor.h | 4 +- runtime/typed_rpc.h | 4 +- runtime/uber-h3.cpp | 2 + runtime/uber-h3.h | 2 +- runtime/udp.cpp | 1 + runtime/udp.h | 2 +- runtime/url.cpp | 44 +-- runtime/url.h | 8 +- runtime/vkext.cpp | 1 + runtime/vkext.h | 2 +- runtime/vkext_stats.h | 3 +- runtime/zlib.cpp | 13 +- runtime/zlib.h | 4 +- runtime/zstd.cpp | 2 +- runtime/zstd.h | 3 +- server/confdata-binlog-events.h | 2 +- server/confdata-binlog-replay.cpp | 1 + server/confdata-stats.cpp | 3 +- server/database-drivers/adaptor.h | 4 +- .../database-drivers/mysql/mysql-connector.h | 2 +- server/database-drivers/mysql/mysql-request.h | 2 +- server/database-drivers/mysql/mysql.h | 2 +- .../database-drivers/pgsql/pgsql-connector.h | 3 +- server/database-drivers/pgsql/pgsql-request.h | 2 +- server/database-drivers/pgsql/pgsql.h | 2 +- server/job-workers/job-message.h | 4 +- server/job-workers/job-stats.cpp | 2 +- server/job-workers/shared-memory-manager.h | 2 +- server/php-master.cpp | 3 +- server/php-runner.cpp | 1 + server/server-stats.h | 2 +- server/signal-handlers.cpp | 1 + server/statshouse/statshouse-manager.h | 2 +- tests/cpp/runtime/_runtime-tests-env.cpp | 1 + .../array-int-string-keys-collision-test.cpp | 2 +- tests/cpp/runtime/array-test.cpp | 2 +- tests/cpp/runtime/kphp-type-traits-test.cpp | 6 +- .../details/memory_chunk_list-test.cpp | 2 +- .../details/memory_chunk_tree-test.cpp | 4 +- .../memory_ordered_chunk_list-test.cpp | 4 +- .../memory_resource/details/test-helpers.h | 4 +- .../extra-memory-pool-test.cpp | 2 +- .../unsynchronized_pool_resource-test.cpp | 2 +- tests/cpp/runtime/msgpack-test.cpp | 2 +- .../cpp/runtime/number-string-comparison.cpp | 2 +- tests/cpp/runtime/string-test.cpp | 2 +- .../shared-memory-manager-test.cpp | 2 +- 217 files changed, 1117 insertions(+), 883 deletions(-) create mode 100644 runtime-core/allocator/script-allocator-managed.h rename runtime/class_instance_decl.inl => runtime-core/class-instance/class-instance-decl.inl (98%) rename runtime/class_instance.inl => runtime-core/class-instance/class-instance.inl (95%) rename runtime/refcountable_php_classes.h => runtime-core/class-instance/refcountable-php-classes.h (91%) rename {runtime => runtime-core/core-types}/comparison_operators.inl (98%) rename {runtime => runtime-core/core-types}/conversions_types.inl (98%) rename {runtime => runtime-core/core-types/decl}/array_decl.inl (99%) rename {runtime => runtime-core/core-types/decl}/array_iterator.h (99%) rename {runtime => runtime-core/core-types/decl}/declarations.h (100%) rename {runtime => runtime-core/core-types/decl}/mixed_decl.inl (99%) rename {runtime => runtime-core/core-types/decl}/optional.h (97%) rename {runtime => runtime-core/core-types/decl}/shape.h (100%) rename {runtime => runtime-core/core-types/decl}/string_buffer_decl.inl (86%) rename {runtime => runtime-core/core-types/decl}/string_decl.inl (99%) rename {runtime => runtime-core/core-types/definition}/array.inl (98%) rename {runtime => runtime-core/core-types/definition}/mixed.cpp (95%) rename {runtime => runtime-core/core-types/definition}/mixed.inl (99%) rename {runtime => runtime-core/core-types/definition}/string.cpp (90%) rename {runtime => runtime-core/core-types/definition}/string.inl (97%) create mode 100644 runtime-core/core-types/definition/string_buffer.cpp rename {runtime => runtime-core/core-types/definition}/string_buffer.inl (75%) rename {runtime => runtime-core/core-types/definition}/string_cache.cpp (95%) rename {runtime => runtime-core/core-types/definition}/string_cache.h (99%) rename {runtime => runtime-core/core-types}/kphp_type_traits.h (93%) rename {runtime => runtime-core}/include.h (84%) rename {runtime/memory_resource => runtime-core/memory-resource}/details/memory_chunk_list.h (94%) rename {runtime/memory_resource => runtime-core/memory-resource}/details/memory_chunk_tree.cpp (98%) rename {runtime/memory_resource => runtime-core/memory-resource}/details/memory_chunk_tree.h (96%) rename {runtime/memory_resource => runtime-core/memory-resource}/details/memory_ordered_chunk_list.cpp (97%) rename {runtime/memory_resource => runtime-core/memory-resource}/details/memory_ordered_chunk_list.h (94%) rename {runtime/memory_resource => runtime-core/memory-resource}/details/universal_reallocate.h (92%) rename {runtime/memory_resource => runtime-core/memory-resource}/extra-memory-pool.h (100%) rename {runtime/memory_resource => runtime-core/memory-resource}/memory_resource.h (93%) create mode 100644 runtime-core/memory-resource/monotonic_buffer_resource.cpp rename {runtime/memory_resource => runtime-core/memory-resource}/monotonic_buffer_resource.h (95%) rename {runtime/memory_resource => runtime-core/memory-resource}/resource_allocator.h (97%) rename {runtime/memory_resource => runtime-core/memory-resource}/unsynchronized_pool_resource.cpp (96%) rename {runtime/memory_resource => runtime-core/memory-resource}/unsynchronized_pool_resource.h (92%) create mode 100644 runtime-core/runtime-core-context.h create mode 100644 runtime-core/runtime-core.cmake rename runtime/kphp_core.h => runtime-core/runtime-core.h (91%) create mode 100644 runtime-core/utils/kphp-assert-core.h rename runtime/migration_php8.cpp => runtime-core/utils/migration-php8.cpp (59%) rename runtime/migration_php8.h => runtime-core/utils/migration-php8.h (89%) create mode 100644 runtime/context/runtime-context.cpp create mode 100644 runtime/context/runtime-context.h create mode 100644 runtime/context/runtime-core-allocator.cpp create mode 100644 runtime/context/runtime-core-context.cpp delete mode 100644 runtime/memory_resource/memory_resource.cpp rename runtime/{memory_resource => memory_resource_impl}/dealer.cpp (86%) rename runtime/{memory_resource => memory_resource_impl}/dealer.h (91%) rename runtime/{memory_resource => memory_resource_impl}/heap_resource.cpp (96%) rename runtime/{memory_resource => memory_resource_impl}/heap_resource.h (90%) create mode 100644 runtime/memory_resource_impl/memory_resource_stats.cpp create mode 100644 runtime/memory_resource_impl/memory_resource_stats.h rename runtime/{memory_resource/monotonic_buffer_resource.cpp => memory_resource_impl/monotonic_runtime_buffer_resource.cpp} (63%) delete mode 100644 runtime/string_buffer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 59e70955e5..280e882a48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ include(${COMMON_DIR}/common.cmake) include(${COMMON_DIR}/tl/tl.cmake) include(${COMMON_DIR}/unicode/unicode.cmake) +include(${BASE_DIR}/runtime-core/runtime-core.cmake) include(${BASE_DIR}/runtime/runtime.cmake) include(${BASE_DIR}/server/server.cmake) include(${BASE_DIR}/compiler/compiler.cmake) diff --git a/builtin-functions/_functions.txt b/builtin-functions/_functions.txt index 54444f053b..8c4f5e10e0 100644 --- a/builtin-functions/_functions.txt +++ b/builtin-functions/_functions.txt @@ -880,7 +880,7 @@ function vk_stats_hll_is_packed($hll ::: string) ::: bool; /** @kphp-extern-func-info cpp_template_call */ function vk_dot_product ($a ::: array, $b ::: array) ::: ^1[*] | ^2[*]; -/** defined in kphp_core.h **/ +/** defined in runtime-core.h **/ function likely ($x ::: bool) ::: bool; function unlikely ($x ::: bool) ::: bool; diff --git a/common/stats/provider.h b/common/stats/provider.h index d4b9e78ba5..d12ff3ec37 100644 --- a/common/stats/provider.h +++ b/common/stats/provider.h @@ -11,6 +11,7 @@ #include #include "common/stats/buffer.h" +#include "common/mixin/not_copyable.h" constexpr int am_get_memory_usage_self = 1; constexpr int am_get_memory_usage_overall = 2; diff --git a/docs/kphp-internals/developing-and-extending-kphp/contributing-to-kphp.md b/docs/kphp-internals/developing-and-extending-kphp/contributing-to-kphp.md index acd9b7bcf6..4f16175615 100644 --- a/docs/kphp-internals/developing-and-extending-kphp/contributing-to-kphp.md +++ b/docs/kphp-internals/developing-and-extending-kphp/contributing-to-kphp.md @@ -129,7 +129,7 @@ Then you think about type inferring. What type should using power operator lead On type inferring step, you introduce *recalc_power()*, call it as necessary, and implement given logic. Next, you need to tie codegeneration and C++ implementation together. -As you resulted in having 3 different inferrings, you need at least 3 C++ functions: say, you name them *int_power()*, *float_power()*, and *mixed_power()* and implement them somewhere in runtime — in *kphp_core.h* for example; the last one not only returns *mixed* but accepts *mixed* also, even though arguments could be inferred as clean types, they would be implicitly converted to *mixed* — it's easier to create a single function without lots overloads in this case. +As you resulted in having 3 different inferrings, you need at least 3 C++ functions: say, you name them *int_power()*, *float_power()*, and *mixed_power()* and implement them somewhere in runtime — in *runtime-core.h* for example; the last one not only returns *mixed* but accepts *mixed* also, even though arguments could be inferred as clean types, they would be implicitly converted to *mixed* — it's easier to create a single function without lots overloads in this case. On codegeneration of *op_pow*, you take the inferred result and output calling one of these functions. To support `**=`, you consider how `+=` and similar are made: "set operator" depends on "base operator". diff --git a/runtime-core/allocator/script-allocator-managed.h b/runtime-core/allocator/script-allocator-managed.h new file mode 100644 index 0000000000..eae0cb6125 --- /dev/null +++ b/runtime-core/allocator/script-allocator-managed.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "runtime-core/runtime-core.h" + +class ScriptAllocatorManaged { +public: + static void *operator new(size_t size) noexcept { + return RuntimeAllocator::current().alloc_script_memory(size); + } + + static void *operator new(size_t, void *ptr) noexcept { + return ptr; + } + + static void operator delete(void *ptr, size_t size) noexcept { + RuntimeAllocator::current().free_script_memory(ptr, size); + } + + static void *operator new[](size_t count) = delete; + static void operator delete[](void *ptr, size_t sz) = delete; + static void operator delete[](void *ptr) = delete; + +protected: + ~ScriptAllocatorManaged() = default; +}; diff --git a/runtime/class_instance_decl.inl b/runtime-core/class-instance/class-instance-decl.inl similarity index 98% rename from runtime/class_instance_decl.inl rename to runtime-core/class-instance/class-instance-decl.inl index 1f32c75f1f..be0d3ed53d 100644 --- a/runtime/class_instance_decl.inl +++ b/runtime-core/class-instance/class-instance-decl.inl @@ -3,7 +3,7 @@ #include "common/smart_ptrs/intrusive_ptr.h" #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif // PHP classes produce the C++ structures of the form: diff --git a/runtime/class_instance.inl b/runtime-core/class-instance/class-instance.inl similarity index 95% rename from runtime/class_instance.inl rename to runtime-core/class-instance/class-instance.inl index bed1014eb1..a7f572ed5a 100644 --- a/runtime/class_instance.inl +++ b/runtime-core/class-instance/class-instance.inl @@ -1,7 +1,7 @@ #pragma once #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif template @@ -43,8 +43,7 @@ class_instance class_instance::alloc(Args &&... args) { template inline class_instance class_instance::empty_alloc() { static_assert(std::is_empty{}, "class T must be empty"); - static uint32_t obj; - obj++; + uint32_t obj = ++KphpCoreContext::current().empty_obj_count; new (&o) vk::intrusive_ptr(reinterpret_cast(obj)); return *this; } diff --git a/runtime/refcountable_php_classes.h b/runtime-core/class-instance/refcountable-php-classes.h similarity index 91% rename from runtime/refcountable_php_classes.h rename to runtime-core/class-instance/refcountable-php-classes.h index 01ada7358e..09b37fafdc 100644 --- a/runtime/refcountable_php_classes.h +++ b/runtime-core/class-instance/refcountable-php-classes.h @@ -7,9 +7,9 @@ #include "common/php-functions.h" -#include "runtime/allocator.h" +#include "runtime-core/allocator/script-allocator-managed.h" -class abstract_refcountable_php_interface : public ManagedThroughDlAllocator { +class abstract_refcountable_php_interface : public ScriptAllocatorManaged { public: abstract_refcountable_php_interface() __attribute__((always_inline)) = default; virtual ~abstract_refcountable_php_interface() noexcept __attribute__((always_inline)) = default; @@ -98,7 +98,7 @@ class refcountable_polymorphic_php_classes_virt<> : public virtual abstract_refc }; template -class refcountable_php_classes : public ManagedThroughDlAllocator { +class refcountable_php_classes : public ScriptAllocatorManaged { public: void add_ref() noexcept { if (refcnt < ExtraRefCnt::for_global_const) { @@ -118,7 +118,7 @@ class refcountable_php_classes : public ManagedThroughDlAllocator { if (refcnt == 0) { static_assert(!std::is_polymorphic{}, "Derived may not be polymorphic"); /** - * because of inheritance from ManagedThroughDlAllocator, which override operators new/delete + * because of inheritance from ScriptAllocatorManaged, which override operators new/delete * we should have vptr for passing proper sizeof of Derived class, but we don't want to increase size of every class * therefore we use static_cast here */ diff --git a/runtime/comparison_operators.inl b/runtime-core/core-types/comparison_operators.inl similarity index 98% rename from runtime/comparison_operators.inl rename to runtime-core/core-types/comparison_operators.inl index fe926a73e4..12e62d797e 100644 --- a/runtime/comparison_operators.inl +++ b/runtime-core/core-types/comparison_operators.inl @@ -1,9 +1,9 @@ #pragma once -#include "runtime/migration_php8.h" +#include "runtime-core/utils/migration-php8.h" #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif namespace impl_ { @@ -92,7 +92,7 @@ inline bool eq2_number_string_as_php8(T lhs, const string &rhs) { inline bool eq2(int64_t lhs, const string &rhs) { const auto php7_result = eq2(lhs, rhs.to_float()); - if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { + if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { const auto php8_result = eq2_number_string_as_php8(lhs, rhs); if (php7_result == php8_result) { return php7_result; @@ -113,7 +113,7 @@ inline bool eq2(const string &lhs, int64_t rhs) { inline bool eq2(double lhs, const string &rhs) { const auto php7_result = lhs == rhs.to_float(); - if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { + if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { const auto php8_result = eq2_number_string_as_php8(lhs, rhs); if (php7_result == php8_result) { return php7_result; diff --git a/runtime/conversions_types.inl b/runtime-core/core-types/conversions_types.inl similarity index 98% rename from runtime/conversions_types.inl rename to runtime-core/core-types/conversions_types.inl index 02ca00c551..a97f3e7296 100644 --- a/runtime/conversions_types.inl +++ b/runtime-core/core-types/conversions_types.inl @@ -1,7 +1,7 @@ #pragma once #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif template> diff --git a/runtime/array_decl.inl b/runtime-core/core-types/decl/array_decl.inl similarity index 99% rename from runtime/array_decl.inl rename to runtime-core/core-types/decl/array_decl.inl index 7071751743..007b536113 100644 --- a/runtime/array_decl.inl +++ b/runtime-core/core-types/decl/array_decl.inl @@ -4,11 +4,11 @@ #pragma once -#include "runtime/array_iterator.h" -#include "runtime/include.h" +#include "runtime-core/core-types/decl/array_iterator.h" +#include "runtime-core/include.h" #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif struct array_size { diff --git a/runtime/array_iterator.h b/runtime-core/core-types/decl/array_iterator.h similarity index 99% rename from runtime/array_iterator.h rename to runtime-core/core-types/decl/array_iterator.h index bc29803850..b03b59af9c 100644 --- a/runtime/array_iterator.h +++ b/runtime-core/core-types/decl/array_iterator.h @@ -7,7 +7,7 @@ #include "common/type_traits/list_of_types.h" #include "common/sanitizer.h" -#include "runtime/declarations.h" +#include "runtime-core/core-types/decl/declarations.h" template class array_iterator { diff --git a/runtime/declarations.h b/runtime-core/core-types/decl/declarations.h similarity index 100% rename from runtime/declarations.h rename to runtime-core/core-types/decl/declarations.h diff --git a/runtime/mixed_decl.inl b/runtime-core/core-types/decl/mixed_decl.inl similarity index 99% rename from runtime/mixed_decl.inl rename to runtime-core/core-types/decl/mixed_decl.inl index a3ae474615..cad5f864fc 100644 --- a/runtime/mixed_decl.inl +++ b/runtime-core/core-types/decl/mixed_decl.inl @@ -5,7 +5,7 @@ #pragma once #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif template diff --git a/runtime/optional.h b/runtime-core/core-types/decl/optional.h similarity index 97% rename from runtime/optional.h rename to runtime-core/core-types/decl/optional.h index 60d2b8d112..69dbde2a31 100644 --- a/runtime/optional.h +++ b/runtime-core/core-types/decl/optional.h @@ -10,8 +10,8 @@ #include "common/type_traits/is_constructible.h" #include "common/type_traits/list_of_types.h" -#include "runtime/php_assert.h" -#include "runtime/declarations.h" +#include "runtime-core/utils/kphp-assert-core.h" +#include "runtime-core/core-types/decl/declarations.h" template class Optional; diff --git a/runtime/shape.h b/runtime-core/core-types/decl/shape.h similarity index 100% rename from runtime/shape.h rename to runtime-core/core-types/decl/shape.h diff --git a/runtime/string_buffer_decl.inl b/runtime-core/core-types/decl/string_buffer_decl.inl similarity index 86% rename from runtime/string_buffer_decl.inl rename to runtime-core/core-types/decl/string_buffer_decl.inl index 34c52f7495..17ad43919f 100644 --- a/runtime/string_buffer_decl.inl +++ b/runtime-core/core-types/decl/string_buffer_decl.inl @@ -1,7 +1,7 @@ #pragma once #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif #define STRING_BUFFER_ERROR_FLAG_ON -1 @@ -9,8 +9,6 @@ #define STRING_BUFFER_ERROR_FLAG_FAILED 1 class string_buffer { - static string::size_type MIN_BUFFER_LEN; - static string::size_type MAX_BUFFER_LEN; char *buffer_end; char *buffer_begin; string::size_type buffer_len; @@ -21,7 +19,6 @@ class string_buffer { string_buffer &operator=(const string_buffer &other) = delete; public: - static int string_buffer_error_flag; explicit string_buffer(string::size_type buffer_len = 4000) noexcept; inline string_buffer &clean() noexcept; @@ -56,7 +53,7 @@ public: ~string_buffer() noexcept; - friend void init_string_buffer_lib(int max_length); + friend void init_string_buffer_lib(string::size_type min_length, string::size_type max_length); inline void debug_print() const; @@ -66,5 +63,8 @@ public: friend inline bool operator!=(const string_buffer &lhs, const string_buffer &rhs); }; -extern string_buffer static_SB; -extern string_buffer static_SB_spare; +struct string_buffer_lib_context { + string::size_type MIN_BUFFER_LEN; + string::size_type MAX_BUFFER_LEN; + int error_flag = 0; +}; diff --git a/runtime/string_decl.inl b/runtime-core/core-types/decl/string_decl.inl similarity index 99% rename from runtime/string_decl.inl rename to runtime-core/core-types/decl/string_decl.inl index 1d5132ab3e..b3d1c0eaa3 100644 --- a/runtime/string_decl.inl +++ b/runtime-core/core-types/decl/string_decl.inl @@ -1,7 +1,7 @@ #pragma once #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif using string_size_type = uint32_t; diff --git a/runtime/array.inl b/runtime-core/core-types/definition/array.inl similarity index 98% rename from runtime/array.inl rename to runtime-core/core-types/definition/array.inl index 367e79cad4..7db12ae63f 100644 --- a/runtime/array.inl +++ b/runtime-core/core-types/definition/array.inl @@ -9,7 +9,7 @@ #include "common/algorithms/fastmod.h" #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif array_size::array_size(int64_t int_size, bool is_vector) noexcept @@ -237,7 +237,7 @@ template typename array::array_inner *array::array_inner::create(int64_t new_int_size, bool is_vector) { const size_t mem_size = estimate_size(new_int_size, is_vector); if (is_vector) { - auto p = reinterpret_cast(dl::allocate(mem_size)); + auto p = reinterpret_cast(RuntimeAllocator::current().alloc_script_memory(mem_size)); p->is_vector_internal = true; p->ref_cnt = 0; p->max_key = -1; @@ -250,7 +250,7 @@ typename array::array_inner *array::array_inner::create(int64_t new_int_si return reinterpret_cast(static_cast(mem) + sizeof(array_inner_fields_for_map)); }; - array_inner *p = shift_pointer_to_array_inner(dl::allocate0(mem_size)); + array_inner *p = shift_pointer_to_array_inner(RuntimeAllocator::current().alloc0_script_memory(mem_size)); p->is_vector_internal = false; p->ref_cnt = 0; p->max_key = -1; @@ -275,7 +275,7 @@ void array::array_inner::dispose() { ((T *)entries)[i].~T(); } - dl::deallocate((void *)this, sizeof_vector(buf_size)); + RuntimeAllocator::current().free_script_memory((void *)this, sizeof_vector(buf_size)); return; } @@ -288,7 +288,7 @@ void array::array_inner::dispose() { php_assert(this != empty_array()); auto shifted_this = reinterpret_cast(this) - sizeof(array_inner_fields_for_map); - dl::deallocate(shifted_this, sizeof_map(buf_size)); + RuntimeAllocator::current().free_script_memory(shifted_this, sizeof_map(buf_size)); } } } @@ -716,7 +716,7 @@ void array::mutate_to_size(int64_t int_size) { php_critical_error ("max array size exceeded: int_size = %" PRIi64, int_size); } const auto new_int_buff_size = static_cast(int_size); - p = static_cast(dl::reallocate(p, p->sizeof_vector(new_int_buff_size), p->sizeof_vector(p->buf_size))); + p = static_cast(RuntimeAllocator::current().realloc_script_memory(p, p->sizeof_vector(new_int_buff_size), p->sizeof_vector(p->buf_size))); p->buf_size = new_int_buff_size; } @@ -1650,7 +1650,7 @@ array &array::operator+=(const array &other) { p = new_array; } else if (p->buf_size < size + 2) { uint32_t new_size = max(size + 2, p->buf_size * 2); - p = (array_inner *)dl::reallocate((void *)p, p->sizeof_vector(new_size), p->sizeof_vector(p->buf_size)); + p = (array_inner *)RuntimeAllocator::current().realloc_script_memory((void *)p, p->sizeof_vector(new_size), p->sizeof_vector(p->buf_size)); p->buf_size = new_size; } @@ -1894,7 +1894,7 @@ void array::sort(const T1 &compare, bool renumber) { mutate_if_map_shared(); } - array_bucket **arTmp = (array_bucket **)dl::allocate(n * sizeof(array_bucket * )); + array_bucket **arTmp = (array_bucket **)RuntimeAllocator::current().alloc_script_memory(n * sizeof(array_bucket * )); uint32_t i = 0; for (array_bucket *it = p->begin(); it != p->end(); it = p->next(it)) { arTmp[i++] = it; @@ -1916,7 +1916,7 @@ void array::sort(const T1 &compare, bool renumber) { arTmp[n - 1]->next = p->get_pointer(p->end()); p->end()->prev = p->get_pointer(arTmp[n - 1]); - dl::deallocate(arTmp, n * sizeof(array_bucket * )); + RuntimeAllocator::current().free_script_memory(arTmp, n * sizeof(array_bucket * )); } diff --git a/runtime/mixed.cpp b/runtime-core/core-types/definition/mixed.cpp similarity index 95% rename from runtime/mixed.cpp rename to runtime-core/core-types/definition/mixed.cpp index 27e4b01f5c..2ead9df246 100644 --- a/runtime/mixed.cpp +++ b/runtime-core/core-types/definition/mixed.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2021 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" void mixed::destroy() noexcept { switch (get_type()) { diff --git a/runtime/mixed.inl b/runtime-core/core-types/definition/mixed.inl similarity index 99% rename from runtime/mixed.inl rename to runtime-core/core-types/definition/mixed.inl index c8f3742eac..9252a31fcf 100644 --- a/runtime/mixed.inl +++ b/runtime-core/core-types/definition/mixed.inl @@ -6,10 +6,10 @@ #include "common/algorithms/find.h" -#include "runtime/migration_php8.h" +#include "runtime-core/utils/migration-php8.h" #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif static_assert(vk::all_of_equal(sizeof(string), sizeof(double), sizeof(array)), "sizeof of array, string and double must be equal"); @@ -1760,7 +1760,7 @@ inline bool less_string_number_as_php8_impl(const string &lhs, T rhs) { template inline bool less_number_string_as_php8(bool php7_result, T lhs, const string &rhs) { - if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { + if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { const auto php8_result = less_number_string_as_php8_impl(lhs, rhs); if (php7_result == php8_result) { return php7_result; @@ -1778,7 +1778,7 @@ inline bool less_number_string_as_php8(bool php7_result, T lhs, const string &rh template inline bool less_string_number_as_php8(bool php7_result, const string &lhs, T rhs) { - if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { + if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) { const auto php8_result = less_string_number_as_php8_impl(lhs, rhs); if (php7_result == php8_result) { return php7_result; diff --git a/runtime/string.cpp b/runtime-core/core-types/definition/string.cpp similarity index 90% rename from runtime/string.cpp rename to runtime-core/core-types/definition/string.cpp index c405722055..8c95a60314 100644 --- a/runtime/string.cpp +++ b/runtime-core/core-types/definition/string.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2021 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" // Don't move this destructor to the headers, it spoils addr2line traces string::~string() noexcept { diff --git a/runtime/string.inl b/runtime-core/core-types/definition/string.inl similarity index 97% rename from runtime/string.inl rename to runtime-core/core-types/definition/string.inl index 0a577a0fd5..e8819e7865 100644 --- a/runtime/string.inl +++ b/runtime-core/core-types/definition/string.inl @@ -8,11 +8,11 @@ #include "common/algorithms/simd-int-to-string.h" -#include "runtime/string_cache.h" -#include "runtime/migration_php8.h" +#include "runtime-core/utils/migration-php8.h" +#include "runtime-core/core-types/definition/string_cache.h" #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif tmp_string::tmp_string(const char *data, string_size_type size) : data{data}, size{size} {} @@ -57,7 +57,7 @@ string::size_type string::string_inner::new_capacity(size_type requested_capacit string::string_inner *string::string_inner::create(size_type requested_capacity, size_type old_capacity) { size_type capacity = new_capacity(requested_capacity, old_capacity); size_type new_size = (size_type)(sizeof(string_inner) + (capacity + 1)); - string_inner *p = (string_inner *)dl::allocate(new_size); + string_inner *p = (string_inner *)RuntimeAllocator::current().alloc_script_memory(new_size); p->capacity = capacity; return p; } @@ -67,7 +67,7 @@ char *string::string_inner::reserve(size_type requested_capacity) { size_type old_size = (size_type)(sizeof(string_inner) + (capacity + 1)); size_type new_size = (size_type)(sizeof(string_inner) + (new_cap + 1)); - string_inner *p = (string_inner *)dl::reallocate((void *)this, new_size, old_size); + string_inner *p = (string_inner *)RuntimeAllocator::current().realloc_script_memory((void *)this, new_size, old_size); p->capacity = new_cap; return p->ref_data(); } @@ -83,7 +83,7 @@ void string::string_inner::dispose() { } void string::string_inner::destroy() { - dl::deallocate(this, get_memory_usage()); + RuntimeAllocator::current().free_script_memory(this, get_memory_usage()); } inline string::size_type string::string_inner::get_memory_usage() const { @@ -744,7 +744,7 @@ bool string::try_to_float_as_php7(double *val) const { bool string::try_to_float(double *val, bool php8_warning) const { const bool is_float_php7 = try_to_float_as_php7(val); - if ((show_migration_php8_warning & MIGRATION_PHP8_STRING_TO_FLOAT_FLAG) && php8_warning) { + if ((KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_TO_FLOAT_FLAG) && php8_warning) { const bool is_float_php8 = try_to_float_as_php8(val); if (is_float_php7 != is_float_php8) { @@ -904,7 +904,7 @@ bool string::is_numeric_as_php7() const { bool string::is_numeric() const { const auto php7_result = is_numeric_as_php7(); - if (show_migration_php8_warning & MIGRATION_PHP8_STRING_TO_FLOAT_FLAG) { + if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_TO_FLOAT_FLAG) { const bool php8_result = is_numeric_as_php8(); if (php7_result != php8_result) { diff --git a/runtime-core/core-types/definition/string_buffer.cpp b/runtime-core/core-types/definition/string_buffer.cpp new file mode 100644 index 0000000000..3d4c4c121a --- /dev/null +++ b/runtime-core/core-types/definition/string_buffer.cpp @@ -0,0 +1,15 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2020 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include "runtime-core/runtime-core.h" + +string_buffer::string_buffer(string::size_type buffer_len) noexcept: + buffer_end(static_cast(RuntimeAllocator::current().alloc_global_memory(buffer_len))), + buffer_begin(buffer_end), + buffer_len(buffer_len) { +} + +string_buffer::~string_buffer() noexcept { + RuntimeAllocator::current().free_global_memory(buffer_begin, buffer_len); +} diff --git a/runtime/string_buffer.inl b/runtime-core/core-types/definition/string_buffer.inl similarity index 75% rename from runtime/string_buffer.inl rename to runtime-core/core-types/definition/string_buffer.inl index 567a8aa69f..744015dd22 100644 --- a/runtime/string_buffer.inl +++ b/runtime-core/core-types/definition/string_buffer.inl @@ -3,20 +3,21 @@ #include "common/algorithms/simd-int-to-string.h" #ifndef INCLUDED_FROM_KPHP_CORE - #error "this file must be included only from kphp_core.h" + #error "this file must be included only from runtime-core.h" #endif inline void string_buffer::resize(string::size_type new_buffer_len) noexcept { - if (new_buffer_len < MIN_BUFFER_LEN) { - new_buffer_len = MIN_BUFFER_LEN; + string_buffer_lib_context &sb_context = KphpCoreContext::current().sb_lib_context; + if (new_buffer_len < sb_context.MIN_BUFFER_LEN) { + new_buffer_len = sb_context.MIN_BUFFER_LEN; } - if (new_buffer_len >= MAX_BUFFER_LEN) { - if (buffer_len + 1 < MAX_BUFFER_LEN) { - new_buffer_len = MAX_BUFFER_LEN - 1; + if (new_buffer_len >= sb_context.MAX_BUFFER_LEN) { + if (buffer_len + 1 < sb_context.MAX_BUFFER_LEN) { + new_buffer_len = sb_context.MAX_BUFFER_LEN - 1; } else { - if (string_buffer_error_flag != STRING_BUFFER_ERROR_FLAG_OFF) { + if (sb_context.error_flag != STRING_BUFFER_ERROR_FLAG_OFF) { clean(); - string_buffer_error_flag = STRING_BUFFER_ERROR_FLAG_FAILED; + sb_context.error_flag = STRING_BUFFER_ERROR_FLAG_FAILED; return; } else { php_critical_error ("maximum buffer size exceeded. buffer_len = %u, new_buffer_len = %u", buffer_len, new_buffer_len); @@ -25,7 +26,7 @@ inline void string_buffer::resize(string::size_type new_buffer_len) noexcept { } string::size_type current_len = size(); - if(void *new_mem = dl::heap_reallocate(buffer_begin, new_buffer_len, buffer_len)) { + if(void *new_mem = RuntimeAllocator::current().realloc_global_memory(buffer_begin, new_buffer_len, buffer_len)) { buffer_begin = static_cast(new_mem); buffer_len = new_buffer_len; buffer_end = buffer_begin + current_len; @@ -34,7 +35,7 @@ inline void string_buffer::resize(string::size_type new_buffer_len) noexcept { inline void string_buffer::reserve_at_least(string::size_type need) noexcept { string::size_type new_buffer_len = need + size(); - while (unlikely (buffer_len < new_buffer_len && string_buffer_error_flag != STRING_BUFFER_ERROR_FLAG_FAILED)) { + while (unlikely (buffer_len < new_buffer_len && KphpCoreContext::current().sb_lib_context.error_flag != STRING_BUFFER_ERROR_FLAG_FAILED)) { resize(((new_buffer_len * 2 + 1 + 64) | 4095) - 64); } } @@ -71,7 +72,7 @@ string_buffer &operator<<(string_buffer &sb, const string &s) { string::size_type l = s.size(); sb.reserve_at_least(l); - if (unlikely (sb.string_buffer_error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) { + if (unlikely (KphpCoreContext::current().sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) { return sb; } @@ -141,7 +142,7 @@ bool string_buffer::set_pos(int64_t pos) { string_buffer &string_buffer::append(const char *str, size_t len) noexcept { reserve_at_least(static_cast(len)); - if (unlikely (string_buffer_error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) { + if (unlikely (KphpCoreContext::current().sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) { return *this; } memcpy(buffer_end, str, len); @@ -168,11 +169,13 @@ void string_buffer::reserve(int len) { reserve_at_least(len + 1); } -inline void init_string_buffer_lib(int max_length) { - string_buffer::MIN_BUFFER_LEN = 266175; - string_buffer::MAX_BUFFER_LEN = (1 << 24); +inline void init_string_buffer_lib(string::size_type min_length, string::size_type max_length) { + string_buffer_lib_context &sb_context = KphpCoreContext::current().sb_lib_context; + if (min_length > 0) { + sb_context.MIN_BUFFER_LEN = min_length; + } if (max_length > 0) { - string_buffer::MAX_BUFFER_LEN = max_length; + sb_context.MAX_BUFFER_LEN = max_length; } } diff --git a/runtime/string_cache.cpp b/runtime-core/core-types/definition/string_cache.cpp similarity index 95% rename from runtime/string_cache.cpp rename to runtime-core/core-types/definition/string_cache.cpp index ec823df121..19e62b700f 100644 --- a/runtime/string_cache.cpp +++ b/runtime-core/core-types/definition/string_cache.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2020 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" constexpr auto string_cache::constexpr_make_large_ints() noexcept { return constexpr_make_ints(std::make_index_sequence{}); diff --git a/runtime/string_cache.h b/runtime-core/core-types/definition/string_cache.h similarity index 99% rename from runtime/string_cache.h rename to runtime-core/core-types/definition/string_cache.h index 645ca27833..66d5e289d9 100644 --- a/runtime/string_cache.h +++ b/runtime-core/core-types/definition/string_cache.h @@ -9,7 +9,7 @@ #include "common/php-functions.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" class string_cache { private: diff --git a/runtime/kphp_type_traits.h b/runtime-core/core-types/kphp_type_traits.h similarity index 93% rename from runtime/kphp_type_traits.h rename to runtime-core/core-types/kphp_type_traits.h index 93873c7e2f..0917cb0aea 100644 --- a/runtime/kphp_type_traits.h +++ b/runtime-core/core-types/kphp_type_traits.h @@ -8,9 +8,9 @@ #include "common/type_traits/list_of_types.h" -#include "runtime/declarations.h" -#include "runtime/optional.h" -#include "runtime/shape.h" +#include "runtime-core/core-types/decl/declarations.h" +#include "runtime-core/core-types/decl/optional.h" +#include "runtime-core/core-types/decl/shape.h" template struct is_array : std::false_type { diff --git a/runtime/include.h b/runtime-core/include.h similarity index 84% rename from runtime/include.h rename to runtime-core/include.h index 0195a50cc2..3c0070b6dd 100644 --- a/runtime/include.h +++ b/runtime-core/include.h @@ -11,10 +11,10 @@ #include "common/wrappers/likely.h" #include "common/php-functions.h" -#include "runtime/declarations.h" -#include "runtime/kphp_type_traits.h" -#include "runtime/optional.h" -#include "runtime/php_assert.h" +#include "runtime-core/core-types/decl/declarations.h" +#include "runtime-core/core-types/kphp_type_traits.h" +#include "runtime-core/core-types/decl/optional.h" +#include "runtime-core/utils/kphp-assert-core.h" #define COMMA , diff --git a/runtime/memory_resource/details/memory_chunk_list.h b/runtime-core/memory-resource/details/memory_chunk_list.h similarity index 94% rename from runtime/memory_resource/details/memory_chunk_list.h rename to runtime-core/memory-resource/details/memory_chunk_list.h index 2277318030..721499097a 100644 --- a/runtime/memory_resource/details/memory_chunk_list.h +++ b/runtime-core/memory-resource/details/memory_chunk_list.h @@ -6,7 +6,7 @@ #include -#include "runtime/memory_resource/memory_resource.h" +#include "runtime-core/memory-resource/memory_resource.h" namespace memory_resource { namespace details { diff --git a/runtime/memory_resource/details/memory_chunk_tree.cpp b/runtime-core/memory-resource/details/memory_chunk_tree.cpp similarity index 98% rename from runtime/memory_resource/details/memory_chunk_tree.cpp rename to runtime-core/memory-resource/details/memory_chunk_tree.cpp index b72d23bf77..e20383ee7a 100644 --- a/runtime/memory_resource/details/memory_chunk_tree.cpp +++ b/runtime-core/memory-resource/details/memory_chunk_tree.cpp @@ -2,13 +2,13 @@ // Copyright (c) 2020 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/memory_resource/details/memory_chunk_tree.h" +#include "memory_chunk_tree.h" #include #include -#include "runtime/memory_resource/details/memory_ordered_chunk_list.h" -#include "runtime/php_assert.h" +#include "runtime-core/memory-resource/details/memory_ordered_chunk_list.h" +#include "runtime-core/utils/kphp-assert-core.h" namespace memory_resource { namespace details { diff --git a/runtime/memory_resource/details/memory_chunk_tree.h b/runtime-core/memory-resource/details/memory_chunk_tree.h similarity index 96% rename from runtime/memory_resource/details/memory_chunk_tree.h rename to runtime-core/memory-resource/details/memory_chunk_tree.h index 53d60d3617..bc14e58108 100644 --- a/runtime/memory_resource/details/memory_chunk_tree.h +++ b/runtime-core/memory-resource/details/memory_chunk_tree.h @@ -6,7 +6,7 @@ #include "common/mixin/not_copyable.h" -#include "runtime/memory_resource/memory_resource.h" +#include "runtime-core/memory-resource/memory_resource.h" namespace memory_resource { namespace details { diff --git a/runtime/memory_resource/details/memory_ordered_chunk_list.cpp b/runtime-core/memory-resource/details/memory_ordered_chunk_list.cpp similarity index 97% rename from runtime/memory_resource/details/memory_ordered_chunk_list.cpp rename to runtime-core/memory-resource/details/memory_ordered_chunk_list.cpp index 96d9d3e60c..2458355120 100644 --- a/runtime/memory_resource/details/memory_ordered_chunk_list.cpp +++ b/runtime-core/memory-resource/details/memory_ordered_chunk_list.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2020 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/memory_resource/details/memory_ordered_chunk_list.h" +#include "runtime-core/memory-resource/details/memory_ordered_chunk_list.h" #include #include diff --git a/runtime/memory_resource/details/memory_ordered_chunk_list.h b/runtime-core/memory-resource/details/memory_ordered_chunk_list.h similarity index 94% rename from runtime/memory_resource/details/memory_ordered_chunk_list.h rename to runtime-core/memory-resource/details/memory_ordered_chunk_list.h index c046d9fc04..da75fe1980 100644 --- a/runtime/memory_resource/details/memory_ordered_chunk_list.h +++ b/runtime-core/memory-resource/details/memory_ordered_chunk_list.h @@ -10,8 +10,8 @@ #include "common/mixin/not_copyable.h" -#include "runtime/memory_resource/memory_resource.h" -#include "runtime/php_assert.h" +#include "runtime-core/memory-resource/memory_resource.h" +#include "runtime-core/utils/kphp-assert-core.h" namespace memory_resource { namespace details { diff --git a/runtime/memory_resource/details/universal_reallocate.h b/runtime-core/memory-resource/details/universal_reallocate.h similarity index 92% rename from runtime/memory_resource/details/universal_reallocate.h rename to runtime-core/memory-resource/details/universal_reallocate.h index 977f016416..96cb13639f 100644 --- a/runtime/memory_resource/details/universal_reallocate.h +++ b/runtime-core/memory-resource/details/universal_reallocate.h @@ -6,7 +6,7 @@ #include -#include "runtime/memory_resource/memory_resource.h" +#include "runtime-core/memory-resource/memory_resource.h" namespace memory_resource { namespace details { diff --git a/runtime/memory_resource/extra-memory-pool.h b/runtime-core/memory-resource/extra-memory-pool.h similarity index 100% rename from runtime/memory_resource/extra-memory-pool.h rename to runtime-core/memory-resource/extra-memory-pool.h diff --git a/runtime/memory_resource/memory_resource.h b/runtime-core/memory-resource/memory_resource.h similarity index 93% rename from runtime/memory_resource/memory_resource.h rename to runtime-core/memory-resource/memory_resource.h index fe75638938..f17d65e4c8 100644 --- a/runtime/memory_resource/memory_resource.h +++ b/runtime-core/memory-resource/memory_resource.h @@ -9,8 +9,6 @@ #include #include -#include "common/stats/provider.h" - // #define DEBUG_MEMORY inline void memory_debug(const char *format, ...) __attribute__ ((format (printf, 1, 2))); @@ -48,8 +46,6 @@ class MemoryStats { size_t total_allocations{0}; // the total number of allocations size_t total_memory_allocated{0}; // the total amount of the memory allocated (doesn't take the freed memory into the account) - - void write_stats_to(stats_t *stats, const char *prefix) const noexcept; }; } // namespace memory_resource diff --git a/runtime-core/memory-resource/monotonic_buffer_resource.cpp b/runtime-core/memory-resource/monotonic_buffer_resource.cpp new file mode 100644 index 0000000000..bbc5370659 --- /dev/null +++ b/runtime-core/memory-resource/monotonic_buffer_resource.cpp @@ -0,0 +1,19 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2020 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include "runtime-core/memory-resource/monotonic_buffer_resource.h" + +namespace memory_resource { + +void monotonic_buffer::init(void *buffer, size_t buffer_size) noexcept { + php_assert(buffer_size <= memory_buffer_limit()); + memory_begin_ = static_cast(buffer); + memory_current_ = memory_begin_; + memory_end_ = memory_begin_ + buffer_size; + + stats_ = MemoryStats{}; + stats_.memory_limit = buffer_size; +} + +} // namespace memory_resource diff --git a/runtime/memory_resource/monotonic_buffer_resource.h b/runtime-core/memory-resource/monotonic_buffer_resource.h similarity index 95% rename from runtime/memory_resource/monotonic_buffer_resource.h rename to runtime-core/memory-resource/monotonic_buffer_resource.h index 95679ec83d..7eb9cdbad3 100644 --- a/runtime/memory_resource/monotonic_buffer_resource.h +++ b/runtime-core/memory-resource/monotonic_buffer_resource.h @@ -10,9 +10,9 @@ #include "common/mixin/not_copyable.h" #include "common/wrappers/likely.h" -#include "runtime/memory_resource/memory_resource.h" -#include "runtime/memory_resource/details/universal_reallocate.h" -#include "runtime/php_assert.h" +#include "runtime-core/memory-resource/details/universal_reallocate.h" +#include "runtime-core/memory-resource/memory_resource.h" +#include "runtime-core/utils/kphp-assert-core.h" namespace memory_resource { @@ -46,8 +46,6 @@ class monotonic_buffer : vk::not_copyable { static_cast(mem) + size <= memory_current_; } - void critical_dump(void *mem, size_t size) const noexcept; - MemoryStats stats_; static_assert(sizeof(char) == 1, "sizeof char should be 1"); @@ -125,6 +123,7 @@ class monotonic_buffer_resource : protected monotonic_buffer { } protected: + void critical_dump(void *mem, size_t size) const noexcept; // this function should never be called from the nested/base context, // since all allocators have their own mem stats; // when signaling OOM, we want to see the root mem stats, not the diff --git a/runtime/memory_resource/resource_allocator.h b/runtime-core/memory-resource/resource_allocator.h similarity index 97% rename from runtime/memory_resource/resource_allocator.h rename to runtime-core/memory-resource/resource_allocator.h index aebc817cd4..d96cd1aee3 100644 --- a/runtime/memory_resource/resource_allocator.h +++ b/runtime-core/memory-resource/resource_allocator.h @@ -7,7 +7,7 @@ #include "common/wrappers/likely.h" -#include "runtime/php_assert.h" +#include "runtime-core/utils/kphp-assert-core.h" namespace memory_resource { diff --git a/runtime/memory_resource/unsynchronized_pool_resource.cpp b/runtime-core/memory-resource/unsynchronized_pool_resource.cpp similarity index 96% rename from runtime/memory_resource/unsynchronized_pool_resource.cpp rename to runtime-core/memory-resource/unsynchronized_pool_resource.cpp index cb98af6688..4c0a26481a 100644 --- a/runtime/memory_resource/unsynchronized_pool_resource.cpp +++ b/runtime-core/memory-resource/unsynchronized_pool_resource.cpp @@ -2,11 +2,11 @@ // Copyright (c) 2020 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/memory_resource/unsynchronized_pool_resource.h" +#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" #include "common/wrappers/likely.h" -#include "runtime/memory_resource/details/memory_ordered_chunk_list.h" +#include "runtime-core/memory-resource/details/memory_ordered_chunk_list.h" namespace memory_resource { diff --git a/runtime/memory_resource/unsynchronized_pool_resource.h b/runtime-core/memory-resource/unsynchronized_pool_resource.h similarity index 92% rename from runtime/memory_resource/unsynchronized_pool_resource.h rename to runtime-core/memory-resource/unsynchronized_pool_resource.h index 3c47ec3c66..cc4fb2db3e 100644 --- a/runtime/memory_resource/unsynchronized_pool_resource.h +++ b/runtime-core/memory-resource/unsynchronized_pool_resource.h @@ -6,13 +6,13 @@ #include -#include "runtime/memory_resource/details/memory_chunk_list.h" -#include "runtime/memory_resource/details/memory_chunk_tree.h" -#include "runtime/memory_resource/details/universal_reallocate.h" -#include "runtime/memory_resource/extra-memory-pool.h" -#include "runtime/memory_resource/monotonic_buffer_resource.h" -#include "runtime/memory_resource/resource_allocator.h" -#include "runtime/php_assert.h" +#include "runtime-core/memory-resource/extra-memory-pool.h" +#include "runtime-core/memory-resource/details/memory_chunk_list.h" +#include "runtime-core/memory-resource/details/memory_chunk_tree.h" +#include "runtime-core/memory-resource/details/universal_reallocate.h" +#include "runtime-core/memory-resource/monotonic_buffer_resource.h" +#include "runtime-core/memory-resource/resource_allocator.h" +#include "runtime-core/utils/kphp-assert-core.h" namespace memory_resource { diff --git a/runtime-core/runtime-core-context.h b/runtime-core/runtime-core-context.h new file mode 100644 index 0000000000..b8ef96c820 --- /dev/null +++ b/runtime-core/runtime-core-context.h @@ -0,0 +1,41 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once + +#include +#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" + +#ifndef INCLUDED_FROM_KPHP_CORE + #error "this file must be included only from runtime-core.h" +#endif + +struct RuntimeAllocator { + static RuntimeAllocator& current() noexcept; + + void init(void * buffer, size_t script_mem_size, size_t oom_handling_mem_size); + void free(); + + void * alloc_script_memory(size_t size) noexcept; + void * alloc0_script_memory(size_t size) noexcept; + void * realloc_script_memory(void *mem, size_t new_size, size_t old_size) noexcept; + void free_script_memory(void *mem, size_t size) noexcept; + + void * alloc_global_memory(size_t size) noexcept; + void * realloc_global_memory(void *mem, size_t new_size, size_t old_size) noexcept; + void free_global_memory(void *mem, size_t size) noexcept; + + memory_resource::unsynchronized_pool_resource memory_resource; +}; + +struct KphpCoreContext { + + static KphpCoreContext& current() noexcept; + void init(); + void free(); + + int show_migration_php8_warning = 0; + uint32_t empty_obj_count = 0; + string_buffer_lib_context sb_lib_context; +}; diff --git a/runtime-core/runtime-core.cmake b/runtime-core/runtime-core.cmake new file mode 100644 index 0000000000..be50be3f42 --- /dev/null +++ b/runtime-core/runtime-core.cmake @@ -0,0 +1,25 @@ +prepend(KPHP_CORE_RUNTIME_UTILS ${BASE_DIR}/runtime-core/utils/ + migration-php8.cpp +) + +prepend(KPHP_CORE_TYPES ${BASE_DIR}/runtime-core/core-types/definition/ + mixed.cpp + string.cpp + string_buffer.cpp + string_cache.cpp +) + +prepend(KPHP_CORE_MEMORY_RESOURCE ${BASE_DIR}/runtime-core/memory-resource/ + details/memory_chunk_tree.cpp + details/memory_ordered_chunk_list.cpp + monotonic_buffer_resource.cpp + unsynchronized_pool_resource.cpp +) + +set(KPHP_CORE_SRC + ${KPHP_CORE_RUNTIME_UTILS} + ${KPHP_CORE_TYPES} + ${KPHP_CORE_MEMORY_RESOURCE} +) + +vk_add_library(runtime-core OBJECT ${KPHP_CORE_SRC}) diff --git a/runtime/kphp_core.h b/runtime-core/runtime-core.h similarity index 91% rename from runtime/kphp_core.h rename to runtime-core/runtime-core.h index dff0300cce..cc262a2f09 100644 --- a/runtime/kphp_core.h +++ b/runtime-core/runtime-core.h @@ -4,35 +4,43 @@ #pragma once +#include +#include +#include +#include +#include +#include #include +#include #include #include "common/algorithms/find.h" #include "common/sanitizer.h" #include "common/type_traits/list_of_types.h" -#include "runtime/allocator.h" -#include "runtime/include.h" -#include "runtime/kphp_type_traits.h" -#include "runtime/shape.h" +#include "runtime-core/include.h" +#include "runtime-core/core-types/kphp_type_traits.h" +#include "runtime-core/core-types/decl/shape.h" // order of includes below matters, be careful #define INCLUDED_FROM_KPHP_CORE -#include "string_decl.inl" -#include "array_decl.inl" -#include "class_instance_decl.inl" -#include "mixed_decl.inl" -#include "string_buffer_decl.inl" +#include "runtime-core/core-types/decl/string_decl.inl" +#include "runtime-core/core-types/decl/array_decl.inl" +#include "runtime-core/class-instance/class-instance-decl.inl" +#include "runtime-core/core-types/decl/mixed_decl.inl" +#include "runtime-core/core-types/decl/string_buffer_decl.inl" -#include "string.inl" -#include "array.inl" -#include "class_instance.inl" -#include "mixed.inl" -#include "string_buffer.inl" -#include "conversions_types.inl" -#include "comparison_operators.inl" +#include "runtime-core/runtime-core-context.h" + +#include "runtime-core/core-types/definition/string.inl" +#include "runtime-core/core-types/definition/array.inl" +#include "runtime-core/class-instance/class-instance.inl" +#include "runtime-core/core-types/definition/mixed.inl" +#include "runtime-core/core-types/definition/string_buffer.inl" +#include "runtime-core/core-types/conversions_types.inl" +#include "runtime-core/core-types/comparison_operators.inl" #undef INCLUDED_FROM_KPHP_CORE @@ -460,27 +468,11 @@ constexpr int32_t E_ALL = 32767; inline mixed f$error_get_last(); -inline int64_t f$error_reporting(int64_t level); - -inline int64_t f$error_reporting(); - inline void f$warning(const string &message); #define f$critical_error(message) \ php_critical_error("%s", message.c_str()); -inline int64_t f$memory_get_static_usage(); - -inline int64_t f$memory_get_peak_usage(bool real_usage = false); - -inline int64_t f$memory_get_usage(bool real_usage = false); - -inline int64_t f$memory_get_total_usage(); - -inline array f$memory_get_detailed_stats(); - -inline std::tuple f$memory_get_allocations(); - template inline int64_t f$get_reference_counter(const array &v); @@ -1324,67 +1316,10 @@ mixed f$error_get_last() { return {}; } -int64_t f$error_reporting(int64_t level) { - int32_t prev = php_warning_level; - if ((level & E_ALL) == E_ALL) { - php_warning_level = 3; - } - if (0 <= level && level <= 3) { - php_warning_level = std::max(php_warning_minimum_level, static_cast(level)); - } - return prev; -} - -int64_t f$error_reporting() { - return php_warning_level; -} - void f$warning(const string &message) { php_warning("%s", message.c_str()); } -int64_t f$memory_get_static_usage() { - return static_cast(dl::get_heap_memory_used()); -} - -int64_t f$memory_get_peak_usage(bool real_usage) { - if (real_usage) { - return static_cast(dl::get_script_memory_stats().max_real_memory_used); - } else { - return static_cast(dl::get_script_memory_stats().max_memory_used); - } -} - -int64_t f$memory_get_usage(bool real_usage __attribute__((unused))) { - return static_cast(dl::get_script_memory_stats().memory_used); -} - -int64_t f$memory_get_total_usage() { - return static_cast(dl::get_script_memory_stats().real_memory_used); -} - -array f$memory_get_detailed_stats() { - const auto &stats = dl::get_script_memory_stats(); - return array( - { - std::make_pair(string{"memory_limit"}, static_cast(stats.memory_limit)), - std::make_pair(string{"real_memory_used"}, static_cast(stats.real_memory_used)), - std::make_pair(string{"memory_used"}, static_cast(stats.memory_used)), - std::make_pair(string{"max_real_memory_used"}, static_cast(stats.max_real_memory_used)), - std::make_pair(string{"max_memory_used"}, static_cast(stats.max_memory_used)), - std::make_pair(string{"defragmentation_calls"}, static_cast(stats.defragmentation_calls)), - std::make_pair(string{"huge_memory_pieces"}, static_cast(stats.huge_memory_pieces)), - std::make_pair(string{"small_memory_pieces"}, static_cast(stats.small_memory_pieces)), - std::make_pair(string{"heap_memory_used"}, static_cast(dl::get_heap_memory_used())) - }); -} - -std::tuple f$memory_get_allocations() { - const auto &stats = dl::get_script_memory_stats(); - return {stats.total_allocations, stats.total_memory_allocated}; -} - - template int64_t f$get_reference_counter(const array &v) { return v.get_reference_counter(); diff --git a/runtime-core/utils/kphp-assert-core.h b/runtime-core/utils/kphp-assert-core.h new file mode 100644 index 0000000000..44024d5508 --- /dev/null +++ b/runtime-core/utils/kphp-assert-core.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include "common/wrappers/likely.h" + +void php_notice(char const *message, ...) __attribute__ ((format (printf, 1, 2))); +void php_warning(char const *message, ...) __attribute__ ((format (printf, 1, 2))); +void php_error(char const *message, ...) __attribute__ ((format (printf, 1, 2))); + +[[noreturn]] void php_assert__(const char *msg, const char *file, int line); +[[noreturn]] void raise_php_assert_signal__(); + +#define php_assert(EX) do { \ + if (unlikely(!(EX))) { \ + php_assert__ (#EX, __FILE__, __LINE__); \ + } \ +} while(0) + +#define php_critical_error(format, ...) do { \ + php_error ("Critical error \"" format "\" in file %s on line %d", ##__VA_ARGS__, __FILE__, __LINE__); \ + raise_php_assert_signal__(); \ +} while(0) diff --git a/runtime/migration_php8.cpp b/runtime-core/utils/migration-php8.cpp similarity index 59% rename from runtime/migration_php8.cpp rename to runtime-core/utils/migration-php8.cpp index 40553c7e58..c003c519a9 100644 --- a/runtime/migration_php8.cpp +++ b/runtime-core/utils/migration-php8.cpp @@ -2,16 +2,15 @@ // Copyright (c) 2021 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "migration_php8.h" - -int show_migration_php8_warning = 0; +#include "runtime-core/utils/migration-php8.h" +#include "runtime-core/runtime-core.h" void f$set_migration_php8_warning(int mask) { - show_migration_php8_warning = mask; + KphpCoreContext::current().show_migration_php8_warning = mask; } static void reset_migration_php8_global_vars() { - show_migration_php8_warning = 0; + KphpCoreContext::current().show_migration_php8_warning = 0; } void free_migration_php8() { diff --git a/runtime/migration_php8.h b/runtime-core/utils/migration-php8.h similarity index 89% rename from runtime/migration_php8.h rename to runtime-core/utils/migration-php8.h index eddedc8caf..edcca7471f 100644 --- a/runtime/migration_php8.h +++ b/runtime-core/utils/migration-php8.h @@ -7,8 +7,6 @@ constexpr int MIGRATION_PHP8_STRING_COMPARISON_FLAG = 1 << 0; constexpr int MIGRATION_PHP8_STRING_TO_FLOAT_FLAG = 1 << 1; -extern int show_migration_php8_warning; - void f$set_migration_php8_warning(int mask); void free_migration_php8(); diff --git a/runtime/allocator.cpp b/runtime/allocator.cpp index e95744a8aa..f4682f862f 100644 --- a/runtime/allocator.cpp +++ b/runtime/allocator.cpp @@ -17,7 +17,7 @@ #include "runtime/critical_section.h" #include "runtime/kphp-backtrace.h" -#include "runtime/memory_resource/dealer.h" +#include "runtime/memory_resource_impl//dealer.h" #include "runtime/php_assert.h" #include "server/server-log.h" diff --git a/runtime/allocator.h b/runtime/allocator.h index 57345f7e9d..2ccf785f05 100644 --- a/runtime/allocator.h +++ b/runtime/allocator.h @@ -9,7 +9,7 @@ #include #include "common/containers/final_action.h" -#include "runtime/memory_resource/memory_resource.h" +#include "runtime-core/memory-resource/memory_resource.h" namespace memory_resource { class unsynchronized_pool_resource; diff --git a/runtime/array_functions.h b/runtime/array_functions.h index 40568b13a3..11b5b40aff 100644 --- a/runtime/array_functions.h +++ b/runtime/array_functions.h @@ -10,7 +10,8 @@ #include "common/type_traits/function_traits.h" #include "common/vector-product.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" +#include "runtime/context/runtime-context.h" #include "runtime/math_functions.h" #include "runtime/string_functions.h" @@ -323,7 +324,7 @@ string f$implode(const string &s, const array &a) { // fallback to the generic iterator + string_buffer solution - string_buffer &SB = static_SB; + string_buffer &SB = kphp_runtime_context.static_SB; SB.clean(); auto it = a.begin(), it_end = a.end(); diff --git a/runtime/bcmath.cpp b/runtime/bcmath.cpp index e807e46b3d..7560a60840 100644 --- a/runtime/bcmath.cpp +++ b/runtime/bcmath.cpp @@ -3,6 +3,7 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #include "runtime/bcmath.h" +#include "runtime/allocator.h" namespace { diff --git a/runtime/bcmath.h b/runtime/bcmath.h index d98da5bbb7..53836e8a4e 100644 --- a/runtime/bcmath.h +++ b/runtime/bcmath.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" void f$bcscale(int64_t scale); diff --git a/runtime/common_template_instantiations.h b/runtime/common_template_instantiations.h index 6c520084f4..94582cbf82 100644 --- a/runtime/common_template_instantiations.h +++ b/runtime/common_template_instantiations.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" // Use explicit template instantiation to make result binary smaller and force common instantiations to be compiled with -O3 // see https://en.cppreference.com/w/cpp/language/class_template diff --git a/runtime/confdata-functions.h b/runtime/confdata-functions.h index 402ad7e480..56b4e7104e 100644 --- a/runtime/confdata-functions.h +++ b/runtime/confdata-functions.h @@ -3,7 +3,7 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" void init_confdata_functions_lib(); void free_confdata_functions_lib(); diff --git a/runtime/confdata-global-manager.h b/runtime/confdata-global-manager.h index 57d223a275..12e8920eee 100644 --- a/runtime/confdata-global-manager.h +++ b/runtime/confdata-global-manager.h @@ -9,11 +9,11 @@ #include "common/mixin/not_copyable.h" #include "common/wrappers/string_view.h" +#include "runtime-core/runtime-core.h" +#include "runtime-core/memory-resource/resource_allocator.h" +#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" #include "runtime/confdata-keys.h" #include "runtime/inter-process-resource.h" -#include "runtime/kphp_core.h" -#include "runtime/memory_resource/resource_allocator.h" -#include "runtime/memory_resource/unsynchronized_pool_resource.h" using confdata_sample_storage = memory_resource::stl::map; diff --git a/runtime/confdata-keys.h b/runtime/confdata-keys.h index 10a5720b4c..8cbd9150e4 100644 --- a/runtime/confdata-keys.h +++ b/runtime/confdata-keys.h @@ -9,12 +9,13 @@ #include #include #include +#include #include "common/mixin/not_copyable.h" #include "common/wrappers/iterator_range.h" #include "common/wrappers/string_view.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" enum class ConfdataFirstKeyType { simple_key, diff --git a/runtime/context/runtime-context.cpp b/runtime/context/runtime-context.cpp new file mode 100644 index 0000000000..bc4bc48ffb --- /dev/null +++ b/runtime/context/runtime-context.cpp @@ -0,0 +1,21 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include "runtime-context.h" + +#include "common/kprintf.h" +#include "runtime/allocator.h" + +KphpRuntimeContext kphp_runtime_context; +RuntimeAllocator runtime_allocator; + +void KphpRuntimeContext::init(void *mem, size_t script_mem_size, size_t oom_handling_mem_size) { + runtime_allocator.init(mem, script_mem_size, oom_handling_mem_size); + KphpCoreContext::init(); +} + +void KphpRuntimeContext::free() { + KphpCoreContext::free(); + runtime_allocator.free(); +} diff --git a/runtime/context/runtime-context.h b/runtime/context/runtime-context.h new file mode 100644 index 0000000000..68e7f110c0 --- /dev/null +++ b/runtime/context/runtime-context.h @@ -0,0 +1,21 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once + +#include "runtime-core/runtime-core.h" + +#include "common/smart_ptrs/singleton.h" + +struct KphpRuntimeContext : KphpCoreContext { + + void init(void *mem, size_t script_mem_size, size_t oom_handling_mem_size); + void free(); + + string_buffer static_SB; + string_buffer static_SB_spare; +}; + +extern KphpRuntimeContext kphp_runtime_context; +extern RuntimeAllocator runtime_allocator; diff --git a/runtime/context/runtime-core-allocator.cpp b/runtime/context/runtime-core-allocator.cpp new file mode 100644 index 0000000000..143f02da17 --- /dev/null +++ b/runtime/context/runtime-core-allocator.cpp @@ -0,0 +1,47 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include "runtime-core/runtime-core.h" +#include "runtime/allocator.h" +#include "runtime/context/runtime-context.h" + +void RuntimeAllocator::init(void *buffer, size_t script_mem_size, size_t oom_handling_mem_size) { + dl::init_script_allocator(buffer, script_mem_size, oom_handling_mem_size); +} + +void RuntimeAllocator::free() { + dl::free_script_allocator(); +} + +RuntimeAllocator &RuntimeAllocator::current() noexcept { + return runtime_allocator; +} + +void *RuntimeAllocator::alloc_script_memory(size_t size) noexcept { + return dl::allocate(size); +} + +void *RuntimeAllocator::alloc0_script_memory(size_t size) noexcept { + return dl::allocate0(size); +} + +void *RuntimeAllocator::realloc_script_memory(void *mem, size_t new_size, size_t old_size) noexcept { + return dl::reallocate(mem, new_size, old_size); +} + +void RuntimeAllocator::free_script_memory(void *mem, size_t size) noexcept { + dl::deallocate(mem, size); +} + +void *RuntimeAllocator::alloc_global_memory(size_t size) noexcept { + return dl::heap_allocate(size); +} + +void *RuntimeAllocator::realloc_global_memory(void *mem, size_t new_size, size_t old_size) noexcept { + return dl::heap_reallocate(mem, new_size, old_size); +} + +void RuntimeAllocator::free_global_memory(void *mem, size_t size) noexcept { + dl::heap_deallocate(mem, size); +} diff --git a/runtime/context/runtime-core-context.cpp b/runtime/context/runtime-core-context.cpp new file mode 100644 index 0000000000..b14f1a0f4f --- /dev/null +++ b/runtime/context/runtime-core-context.cpp @@ -0,0 +1,24 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include "common/smart_ptrs/singleton.h" +#include "runtime/context/runtime-context.h" +#include "server/php-engine-vars.h" +#include "runtime/allocator.h" + +KphpCoreContext &KphpCoreContext::current() noexcept { + return kphp_runtime_context; +} + +void KphpCoreContext::init() { + if (static_buffer_length_limit < 0) { + init_string_buffer_lib(266175, (1 << 24)); + } else { + init_string_buffer_lib(266175, static_buffer_length_limit); + } +} + +void KphpCoreContext::free() { + free_migration_php8(); +} diff --git a/runtime/ctype.h b/runtime/ctype.h index 180a0a048b..a029767bfd 100644 --- a/runtime/ctype.h +++ b/runtime/ctype.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" bool f$ctype_alnum(const mixed &text) noexcept; bool f$ctype_alpha(const mixed &text) noexcept; diff --git a/runtime/curl-async.h b/runtime/curl-async.h index 4060ca37c3..af1fc37ef9 100644 --- a/runtime/curl-async.h +++ b/runtime/curl-async.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/curl.h" diff --git a/runtime/curl.cpp b/runtime/curl.cpp index 5d035e4a2e..c22ef194e6 100644 --- a/runtime/curl.cpp +++ b/runtime/curl.cpp @@ -10,6 +10,7 @@ #include #include +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/interface.h" #include "runtime/kphp_tracing.h" @@ -237,9 +238,9 @@ size_t curl_write(char *data, size_t size, size_t nmemb, void *userdata) { if (easy_context->return_transfer) { return easy_context->received_data.push_string(data, length) ? length : 0; } - string_buffer::string_buffer_error_flag = STRING_BUFFER_ERROR_FLAG_ON; + kphp_runtime_context.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_ON; print(data, length); - return std::exchange(string_buffer::string_buffer_error_flag, STRING_BUFFER_ERROR_FLAG_OFF) == STRING_BUFFER_ERROR_FLAG_FAILED ? 0 : length; + return std::exchange(kphp_runtime_context.sb_lib_context.error_flag, STRING_BUFFER_ERROR_FLAG_OFF) == STRING_BUFFER_ERROR_FLAG_FAILED ? 0 : length; } // this is a callback called from curl_easy_perform diff --git a/runtime/curl.h b/runtime/curl.h index 77c7ca26ac..516d8b8724 100644 --- a/runtime/curl.h +++ b/runtime/curl.h @@ -5,8 +5,9 @@ #pragma once #include "common/smart_ptrs/singleton.h" - -#include "runtime/kphp_core.h" +#include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" +#include "runtime/allocator.h" using curl_easy = int64_t; diff --git a/runtime/datetime/date_interval.h b/runtime/datetime/date_interval.h index 8116962db0..c6e753dbbc 100644 --- a/runtime/datetime/date_interval.h +++ b/runtime/datetime/date_interval.h @@ -4,9 +4,9 @@ #pragma once +#include "runtime-core/class-instance/refcountable-php-classes.h" #include "runtime/datetime/timelib_wrapper.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/refcountable_php_classes.h" struct C$DateInterval: public refcountable_php_classes, private DummyVisitorMethods { using DummyVisitorMethods::accept; diff --git a/runtime/datetime/datetime.h b/runtime/datetime/datetime.h index a83a138ac5..f5b8edbdde 100644 --- a/runtime/datetime/datetime.h +++ b/runtime/datetime/datetime.h @@ -4,11 +4,11 @@ #pragma once -#include "runtime/datetime/datetime_zone.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/datetime/datetime_interface.h" +#include "runtime/datetime/datetime_zone.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" struct C$DateInterval; struct C$DateTimeImmutable; diff --git a/runtime/datetime/datetime_functions.cpp b/runtime/datetime/datetime_functions.cpp index de744bae17..0bd9f0eede 100644 --- a/runtime/datetime/datetime_functions.cpp +++ b/runtime/datetime/datetime_functions.cpp @@ -9,6 +9,7 @@ #include #include +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/datetime/timelib_wrapper.h" #include "runtime/string_functions.h" @@ -116,7 +117,7 @@ void iso_week_number(int y, int doy, int weekday, int &iw, int &iy) { static string date(const string &format, const tm &t, int64_t timestamp, bool local) { - string_buffer &SB = static_SB_spare; + string_buffer &SB = kphp_runtime_context.static_SB_spare; int year = t.tm_year + 1900; int month = t.tm_mon + 1; diff --git a/runtime/datetime/datetime_functions.h b/runtime/datetime/datetime_functions.h index 8b307e8c22..637e5b4b85 100644 --- a/runtime/datetime/datetime_functions.h +++ b/runtime/datetime/datetime_functions.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" bool f$checkdate(int64_t month, int64_t day, int64_t year); diff --git a/runtime/datetime/datetime_immutable.h b/runtime/datetime/datetime_immutable.h index 2104dc146a..fb4c877a52 100644 --- a/runtime/datetime/datetime_immutable.h +++ b/runtime/datetime/datetime_immutable.h @@ -4,11 +4,11 @@ #pragma once +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/datetime/datetime_interface.h" #include "runtime/datetime/datetime_zone.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" struct C$DateInterval; struct C$DateTime; diff --git a/runtime/datetime/datetime_interface.h b/runtime/datetime/datetime_interface.h index 158ba9624c..5d9222631a 100644 --- a/runtime/datetime/datetime_interface.h +++ b/runtime/datetime/datetime_interface.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/refcountable_php_classes.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" #include "runtime/datetime/timelib_wrapper.h" diff --git a/runtime/datetime/datetime_zone.h b/runtime/datetime/datetime_zone.h index 48a18d6496..fdad631343 100644 --- a/runtime/datetime/datetime_zone.h +++ b/runtime/datetime/datetime_zone.h @@ -4,9 +4,9 @@ #pragma once +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" struct C$DateTimeZone : public refcountable_php_classes, private DummyVisitorMethods { using DummyVisitorMethods::accept; diff --git a/runtime/datetime/timelib_wrapper.cpp b/runtime/datetime/timelib_wrapper.cpp index a569966ab6..f7661a8051 100644 --- a/runtime/datetime/timelib_wrapper.cpp +++ b/runtime/datetime/timelib_wrapper.cpp @@ -8,6 +8,8 @@ #include "common/containers/final_action.h" #include "common/smart_ptrs/singleton.h" +#include "runtime/allocator.h" +#include "runtime/context/runtime-context.h" #include "server/php-engine-vars.h" #include "server/php-runner.h" @@ -431,7 +433,7 @@ string php_timelib_date_format(const string &format, timelib_time *t, bool local auto script_guard = make_malloc_replacement_with_script_allocator(); - string_buffer &SB = static_SB_spare; + string_buffer &SB = kphp_runtime_context.static_SB_spare; SB.clean(); timelib_time_offset *offset = localtime ? create_time_offset(t, script_guard) : nullptr; @@ -859,7 +861,7 @@ string php_timelib_date_interval_format(const string &format, timelib_rel_time * return {}; } - string_buffer &SB = static_SB_spare; + string_buffer &SB = kphp_runtime_context.static_SB_spare; SB.clean(); // php implementation has 33 bytes buffer capacity, we have 128 bytes as well as php_timelib_date_format() diff --git a/runtime/datetime/timelib_wrapper.h b/runtime/datetime/timelib_wrapper.h index 51cced348c..5c06638bf5 100644 --- a/runtime/datetime/timelib_wrapper.h +++ b/runtime/datetime/timelib_wrapper.h @@ -2,7 +2,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" // php_timelib wraps the https://github.com/derickr/timelib library // which is used in PHP to implement several datetime lib functions. diff --git a/runtime/env.h b/runtime/env.h index 89848bcc83..e2ded6ae20 100644 --- a/runtime/env.h +++ b/runtime/env.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" array f$getenv() noexcept; Optional f$getenv(const string &varname, bool local_only = false) noexcept; diff --git a/runtime/exception.cpp b/runtime/exception.cpp index 3cc90786e8..e6a3dfa238 100644 --- a/runtime/exception.cpp +++ b/runtime/exception.cpp @@ -6,6 +6,7 @@ #include "common/fast-backtrace.h" +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/string_functions.h" @@ -86,16 +87,16 @@ Exception new_Exception(const string &file, int64_t line, const string &message, } Exception f$err(const string &file, int64_t line, const string &code, const string &desc) { - return new_Exception(file, line, (static_SB.clean() << "ERR_" << code << ": " << desc).str(), 0); + return new_Exception(file, line, (kphp_runtime_context.static_SB.clean() << "ERR_" << code << ": " << desc).str(), 0); } string exception_trace_as_string(const Throwable &e) { - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); for (int64_t i = 0; i < e->trace.count(); i++) { array current = e->trace.get_value(i); - static_SB << '#' << i << ' ' << current.get_value(string("file", 4)) << ": " << current.get_value(string("function", 8)) << "\n"; + kphp_runtime_context.static_SB << '#' << i << ' ' << current.get_value(string("file", 4)) << ": " << current.get_value(string("function", 8)) << "\n"; } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } void exception_initialize(const Throwable &e, const string &message, int64_t code) { diff --git a/runtime/exception.h b/runtime/exception.h index 9fd7047f80..85b8ebaedd 100644 --- a/runtime/exception.h +++ b/runtime/exception.h @@ -8,12 +8,12 @@ #include "common/algorithms/hashes.h" #include "common/wrappers/string_view.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" #include "runtime/instance-copy-processor.h" -#include "runtime/to-array-processor.h" -#include "runtime/kphp_core.h" #include "runtime/memory_usage.h" -#include "runtime/refcountable_php_classes.h" +#include "runtime/to-array-processor.h" array> f$debug_backtrace(); diff --git a/runtime/exec.h b/runtime/exec.h index 089f1747c8..5539a777f0 100644 --- a/runtime/exec.h +++ b/runtime/exec.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" int64_t &get_dummy_result_code() noexcept; diff --git a/runtime/ffi.h b/runtime/ffi.h index 781865957c..53ac0fc837 100644 --- a/runtime/ffi.h +++ b/runtime/ffi.h @@ -4,10 +4,10 @@ #pragma once +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" #include "runtime/memory_usage.h" -#include "runtime/refcountable_php_classes.h" template struct C$FFI$CData: public refcountable_php_classes>, private DummyVisitorMethods { diff --git a/runtime/files.cpp b/runtime/files.cpp index 01f38ffa83..b33af43652 100644 --- a/runtime/files.cpp +++ b/runtime/files.cpp @@ -17,11 +17,12 @@ #include "common/macos-ports.h" #include "common/wrappers/mkdir_recursive.h" +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/interface.h" #include "runtime/kphp_tracing.h" #include "runtime/streams.h" -#include "runtime/string_functions.h"//php_buf, TODO +#include "runtime/string_functions.h" //php_buf, TODO static int32_t opened_fd{-1}; @@ -477,7 +478,7 @@ static Optional full_realpath(const string &path) { // realpath resolvin const char *basename_c_str = __xpg_basename(basename_path_copy.buffer()); dl::leave_critical_section(); - return result_cache = (static_SB.clean() << file_wrapper_name << real_path << '/' << basename_c_str).str(); + return result_cache = (kphp_runtime_context.static_SB.clean() << file_wrapper_name << real_path << '/' << basename_c_str).str(); } result_cache = LETTER_a; return false; diff --git a/runtime/files.h b/runtime/files.h index 5a4373bf5b..58654430ed 100644 --- a/runtime/files.h +++ b/runtime/files.h @@ -8,7 +8,7 @@ #include #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" extern const string LETTER_a; diff --git a/runtime/from-json-processor.h b/runtime/from-json-processor.h index 7e77cc8c8f..3c54975c3a 100644 --- a/runtime/from-json-processor.h +++ b/runtime/from-json-processor.h @@ -6,7 +6,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/json-functions.h" #include "runtime/json-processor-utils.h" @@ -91,13 +91,13 @@ class FromJsonVisitor { } void do_set(JsonRawString &value, const mixed &json) noexcept { - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); if (!impl_::JsonEncoder{0, false, get_json_obj_magic_key()}.encode(json)) { error_.append("failed to decode @kphp-json raw_string field "); error_.append(json_path_.to_string()); return; } - value.str = static_SB.str(); + value.str = kphp_runtime_context.static_SB.str(); } template diff --git a/runtime/instance-cache.cpp b/runtime/instance-cache.cpp index 04d034b67f..2bae5d31d9 100644 --- a/runtime/instance-cache.cpp +++ b/runtime/instance-cache.cpp @@ -13,12 +13,12 @@ #include "common/kprintf.h" #include "common/wrappers/memory-utils.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/memory-resource/resource_allocator.h" #include "runtime/allocator.h" #include "runtime/critical_section.h" #include "runtime/inter-process-mutex.h" #include "runtime/inter-process-resource.h" -#include "runtime/memory_resource/resource_allocator.h" -#include "runtime/refcountable_php_classes.h" namespace impl_ { diff --git a/runtime/instance-cache.h b/runtime/instance-cache.h index 1590ffaccf..b9b1706673 100644 --- a/runtime/instance-cache.h +++ b/runtime/instance-cache.h @@ -18,10 +18,9 @@ #include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" #include "runtime/instance-copy-processor.h" -#include "runtime/kphp_core.h" #include "runtime/memory_usage.h" -#include "runtime/shape.h" #include "server/statshouse/statshouse-manager.h" enum class InstanceCacheOpStatus; diff --git a/runtime/instance-copy-processor.h b/runtime/instance-copy-processor.h index 696e74687b..eac8457c3e 100644 --- a/runtime/instance-copy-processor.h +++ b/runtime/instance-copy-processor.h @@ -10,10 +10,10 @@ #include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" +#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" #include "runtime/allocator.h" #include "runtime/critical_section.h" -#include "runtime/kphp_core.h" -#include "runtime/memory_resource/unsynchronized_pool_resource.h" namespace impl_ { diff --git a/runtime/interface.cpp b/runtime/interface.cpp index be036860e7..76f75e837c 100644 --- a/runtime/interface.cpp +++ b/runtime/interface.cpp @@ -24,6 +24,7 @@ #include "runtime/array_functions.h" #include "runtime/bcmath.h" #include "runtime/confdata-functions.h" +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/curl.h" #include "runtime/datetime/datetime_functions.h" @@ -374,29 +375,29 @@ void f$send_http_103_early_hints(const array & headers) { void f$setrawcookie(const string &name, const string &value, int64_t expire, const string &path, const string &domain, bool secure, bool http_only) { string date = f$gmdate(HTTP_DATE, expire); - static_SB_spare.clean() << "Set-Cookie: " << name << '='; + kphp_runtime_context.static_SB_spare.clean() << "Set-Cookie: " << name << '='; if (value.empty()) { - static_SB_spare << "DELETED; expires=Thu, 01 Jan 1970 00:00:01 GMT"; + kphp_runtime_context.static_SB_spare << "DELETED; expires=Thu, 01 Jan 1970 00:00:01 GMT"; } else { - static_SB_spare << value; + kphp_runtime_context.static_SB_spare << value; if (expire != 0) { - static_SB_spare << "; expires=" << date; + kphp_runtime_context.static_SB_spare << "; expires=" << date; } } if (!path.empty()) { - static_SB_spare << "; path=" << path; + kphp_runtime_context.static_SB_spare << "; path=" << path; } if (!domain.empty()) { - static_SB_spare << "; domain=" << domain; + kphp_runtime_context.static_SB_spare << "; domain=" << domain; } if (secure) { - static_SB_spare << "; secure"; + kphp_runtime_context.static_SB_spare << "; secure"; } if (http_only) { - static_SB_spare << "; HttpOnly"; + kphp_runtime_context.static_SB_spare << "; HttpOnly"; } - header(static_SB_spare.c_str(), (int)static_SB_spare.size(), false); + header(kphp_runtime_context.static_SB_spare.c_str(), (int)kphp_runtime_context.static_SB_spare.size(), false); } void f$setcookie(const string &name, const string &value, int64_t expire, const string &path, const string &domain, bool secure, bool http_only) { @@ -486,32 +487,32 @@ static inline const char *http_get_error_msg_text(int *code) { } static void set_content_length_header(int content_length) { - static_SB_spare.clean() << "Content-Length: " << content_length; - header(static_SB_spare.c_str(), (int)static_SB_spare.size()); + kphp_runtime_context.static_SB_spare.clean() << "Content-Length: " << content_length; + header(kphp_runtime_context.static_SB_spare.c_str(), (int)kphp_runtime_context.static_SB_spare.size()); } -static const string_buffer *get_headers() {//can't use static_SB, returns pointer to static_SB_spare +static const string_buffer *get_headers() {//can't use static_SB, returns pointer to kphp_runtime_context.static_SB_spare string date = f$gmdate(HTTP_DATE); - static_SB_spare.clean() << "Date: " << date; - header(static_SB_spare.c_str(), (int)static_SB_spare.size()); + kphp_runtime_context.static_SB_spare.clean() << "Date: " << date; + header(kphp_runtime_context.static_SB_spare.c_str(), (int)kphp_runtime_context.static_SB_spare.size()); php_assert (dl::query_num == header_last_query_num); - static_SB_spare.clean(); + kphp_runtime_context.static_SB_spare.clean(); if (!http_status_line.empty()) { - static_SB_spare << http_status_line << "\r\n"; + kphp_runtime_context.static_SB_spare << http_status_line << "\r\n"; } else { const char *message = http_get_error_msg_text(&http_return_code); - static_SB_spare << "HTTP/1.1 " << http_return_code << " " << message << "\r\n"; + kphp_runtime_context.static_SB_spare << "HTTP/1.1 " << http_return_code << " " << message << "\r\n"; } const array *arr = headers; for (array::const_iterator p = arr->begin(); p != arr->end(); ++p) { - static_SB_spare << p.get_value(); + kphp_runtime_context.static_SB_spare << p.get_value(); } - static_SB_spare << "\r\n"; + kphp_runtime_context.static_SB_spare << "\r\n"; - return &static_SB_spare; + return &kphp_runtime_context.static_SB_spare; } constexpr uint32_t MAX_SHUTDOWN_FUNCTIONS = 256; @@ -571,7 +572,7 @@ void f$flush() { http_send_immediate_response(http_headers ? http_headers->buffer() : nullptr, http_headers ? http_headers->size() : 0, http_body->buffer(), http_body->size()); oub[ob_system_level].clean(); - static_SB_spare.clean(); + kphp_runtime_context.static_SB_spare.clean(); } void f$fastcgi_finish_request(int64_t exit_code) { @@ -766,14 +767,14 @@ double f$thread_pool_test_load(int64_t size, int64_t n, double a, double b) { } string f$long2ip(int64_t num) { - static_SB.clean().reserve(100); + kphp_runtime_context.static_SB.clean().reserve(100); for (int i = 3; i >= 0; i--) { - static_SB << ((num >> (i * 8)) & 255); + kphp_runtime_context.static_SB << ((num >> (i * 8)) & 255); if (i) { - static_SB.append_char('.'); + kphp_runtime_context.static_SB.append_char('.'); } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } Optional> f$gethostbynamel(const string &name) { @@ -1561,7 +1562,7 @@ static void init_superglobals_impl(const http_query_data &http_data, const rpc_q if (http_data.uri) { if (http_data.get_len) { - superglobals.v$_SERVER.set_value(string("REQUEST_URI"), (static_SB.clean() << uri_str << '?' << get_str).str()); + superglobals.v$_SERVER.set_value(string("REQUEST_URI"), (kphp_runtime_context.static_SB.clean() << uri_str << '?' << get_str).str()); } else { superglobals.v$_SERVER.set_value(string("REQUEST_URI"), uri_str); } @@ -1740,7 +1741,7 @@ static void init_superglobals_impl(const http_query_data &http_data, const rpc_q superglobals.v$_SERVER.set_value(string("REQUEST_TIME_FLOAT"), cur_time); superglobals.v$_SERVER.set_value(string("SERVER_PORT"), string("80")); superglobals.v$_SERVER.set_value(string("SERVER_PROTOCOL"), string("HTTP/1.1")); - superglobals.v$_SERVER.set_value(string("SERVER_SIGNATURE"), (static_SB.clean() << "Apache/2.2.9 (Debian) PHP/5.2.6-1<(static_buffer_length_limit)); - init_interface_lib(); } @@ -2386,7 +2385,6 @@ static void free_runtime_libs() { free_instance_cache_lib(); free_kphp_backtrace(); - free_migration_php8(); free_use_updated_gmmktime(); free_detect_incorrect_encoding_names(); @@ -2425,7 +2423,7 @@ void global_init_script_allocator() { } void init_runtime_environment(const php_query_data_t &data, PhpScriptBuiltInSuperGlobals &superglobals, void *mem, size_t script_mem_size, size_t oom_handling_mem_size) { - dl::init_script_allocator(mem, script_mem_size, oom_handling_mem_size); + kphp_runtime_context.init(mem, script_mem_size, oom_handling_mem_size); reset_global_interface_vars(superglobals); init_runtime_libs(); init_superglobals(data, superglobals); @@ -2435,7 +2433,7 @@ void free_runtime_environment(PhpScriptBuiltInSuperGlobals &superglobals) { reset_superglobals(superglobals); free_runtime_libs(); reset_global_interface_vars(superglobals); - dl::free_script_allocator(); + kphp_runtime_context.free(); } void worker_global_init(WorkerType worker_type) noexcept { diff --git a/runtime/interface.h b/runtime/interface.h index 7b21347b32..b1a05f8e55 100644 --- a/runtime/interface.h +++ b/runtime/interface.h @@ -8,9 +8,8 @@ #include "common/wrappers/string_view.h" +#include "runtime-core/runtime-core.h" #include "runtime/critical_section.h" -#include "runtime/kphp_core.h" -#include "runtime/optional.h" #include "runtime/php-script-globals.h" #include "server/php-query-data.h" #include "server/statshouse/statshouse-manager.h" diff --git a/runtime/job-workers/client-functions.h b/runtime/job-workers/client-functions.h index 95f03895d0..9c4730da30 100644 --- a/runtime/job-workers/client-functions.h +++ b/runtime/job-workers/client-functions.h @@ -4,8 +4,8 @@ #pragma once +#include "runtime-core/runtime-core.h" #include "runtime/job-workers/job-interface.h" -#include "runtime/kphp_core.h" void free_job_client_interface_lib() noexcept; diff --git a/runtime/job-workers/job-interface.h b/runtime/job-workers/job-interface.h index 8a429591dc..16ef3376b9 100644 --- a/runtime/job-workers/job-interface.h +++ b/runtime/job-workers/job-interface.h @@ -7,10 +7,10 @@ #include "common/algorithms/hashes.h" #include "common/wrappers/string_view.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/instance-copy-processor.h" #include "runtime/to-array-processor.h" -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" class InstanceReferencesCountingVisitor; diff --git a/runtime/job-workers/processing-jobs.h b/runtime/job-workers/processing-jobs.h index a68eb30d4e..bea2ad435b 100644 --- a/runtime/job-workers/processing-jobs.h +++ b/runtime/job-workers/processing-jobs.h @@ -7,7 +7,7 @@ #include "common/mixin/not_copyable.h" #include "common/smart_ptrs/singleton.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/net_events.h" #include "runtime/job-workers/job-interface.h" diff --git a/runtime/job-workers/server-functions.h b/runtime/job-workers/server-functions.h index c4380ebb65..868ec5c377 100644 --- a/runtime/job-workers/server-functions.h +++ b/runtime/job-workers/server-functions.h @@ -4,8 +4,8 @@ #pragma once +#include "runtime-core/runtime-core.h" #include "runtime/job-workers/job-interface.h" -#include "runtime/kphp_core.h" struct job_query_data; void init_job_server_interface_lib(job_query_data job_data) noexcept; diff --git a/runtime/json-functions.cpp b/runtime/json-functions.cpp index 79c958ca04..c066304e23 100644 --- a/runtime/json-functions.cpp +++ b/runtime/json-functions.cpp @@ -14,12 +14,12 @@ namespace { void json_append_one_char(unsigned int c) noexcept { - static_SB.append_char('\\'); - static_SB.append_char('u'); - static_SB.append_char("0123456789abcdef"[c >> 12]); - static_SB.append_char("0123456789abcdef"[(c >> 8) & 15]); - static_SB.append_char("0123456789abcdef"[(c >> 4) & 15]); - static_SB.append_char("0123456789abcdef"[c & 15]); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('u'); + kphp_runtime_context.static_SB.append_char("0123456789abcdef"[c >> 12]); + kphp_runtime_context.static_SB.append_char("0123456789abcdef"[(c >> 8) & 15]); + kphp_runtime_context.static_SB.append_char("0123456789abcdef"[(c >> 4) & 15]); + kphp_runtime_context.static_SB.append_char("0123456789abcdef"[c & 15]); } bool json_append_char(unsigned int c) noexcept { @@ -41,54 +41,54 @@ bool json_append_char(unsigned int c) noexcept { bool do_json_encode_string_php(const JsonPath &json_path, const char *s, int len, int64_t options) noexcept { - int begin_pos = static_SB.size(); + int begin_pos = kphp_runtime_context.static_SB.size(); if (options & JSON_UNESCAPED_UNICODE) { - static_SB.reserve(2 * len + 2); + kphp_runtime_context.static_SB.reserve(2 * len + 2); } else { - static_SB.reserve(6 * len + 2); + kphp_runtime_context.static_SB.reserve(6 * len + 2); } - static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_char('"'); auto fire_error = [json_path, begin_pos](int pos) { php_warning("%s: Not a valid utf-8 character at pos %d in function json_encode", json_path.to_string().c_str(), pos); - static_SB.set_pos(begin_pos); - static_SB.append("null", 4); + kphp_runtime_context.static_SB.set_pos(begin_pos); + kphp_runtime_context.static_SB.append("null", 4); return false; }; for (int pos = 0; pos < len; pos++) { switch (s[pos]) { case '"': - static_SB.append_char('\\'); - static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('"'); break; case '\\': - static_SB.append_char('\\'); - static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('\\'); break; case '/': - static_SB.append_char('\\'); - static_SB.append_char('/'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('/'); break; case '\b': - static_SB.append_char('\\'); - static_SB.append_char('b'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('b'); break; case '\f': - static_SB.append_char('\\'); - static_SB.append_char('f'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('f'); break; case '\n': - static_SB.append_char('\\'); - static_SB.append_char('n'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('n'); break; case '\r': - static_SB.append_char('\\'); - static_SB.append_char('r'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('r'); break; case '\t': - static_SB.append_char('\\'); - static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('t'); break; case 0 ... 7: case 11: @@ -110,8 +110,8 @@ bool do_json_encode_string_php(const JsonPath &json_path, const char *s, int len return fire_error(pos); } if (options & JSON_UNESCAPED_UNICODE) { - static_SB.append_char(static_cast(a)); - static_SB.append_char(static_cast(b)); + kphp_runtime_context.static_SB.append_char(static_cast(a)); + kphp_runtime_context.static_SB.append_char(static_cast(b)); } else if (!json_append_char(((a & 0x1f) << 6) | (b & 0x3f))) { return fire_error(pos); } @@ -127,9 +127,9 @@ bool do_json_encode_string_php(const JsonPath &json_path, const char *s, int len return fire_error(pos); } if (options & JSON_UNESCAPED_UNICODE) { - static_SB.append_char(static_cast(a)); - static_SB.append_char(static_cast(b)); - static_SB.append_char(static_cast(c)); + kphp_runtime_context.static_SB.append_char(static_cast(a)); + kphp_runtime_context.static_SB.append_char(static_cast(b)); + kphp_runtime_context.static_SB.append_char(static_cast(c)); } else if (!json_append_char(((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f))) { return fire_error(pos); } @@ -145,10 +145,10 @@ bool do_json_encode_string_php(const JsonPath &json_path, const char *s, int len return fire_error(pos); } if (options & JSON_UNESCAPED_UNICODE) { - static_SB.append_char(static_cast(a)); - static_SB.append_char(static_cast(b)); - static_SB.append_char(static_cast(c)); - static_SB.append_char(static_cast(d)); + kphp_runtime_context.static_SB.append_char(static_cast(a)); + kphp_runtime_context.static_SB.append_char(static_cast(b)); + kphp_runtime_context.static_SB.append_char(static_cast(c)); + kphp_runtime_context.static_SB.append_char(static_cast(d)); } else if (!json_append_char(((a & 0x07) << 18) | ((b & 0x3f) << 12) | ((c & 0x3f) << 6) | (d & 0x3f))) { return fire_error(pos); } @@ -158,57 +158,57 @@ bool do_json_encode_string_php(const JsonPath &json_path, const char *s, int len return fire_error(pos); } default: - static_SB.append_char(s[pos]); + kphp_runtime_context.static_SB.append_char(s[pos]); break; } } - static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_char('"'); return true; } bool do_json_encode_string_vkext(const char *s, int len) noexcept { - static_SB.reserve(2 * len + 2); - if (static_SB.string_buffer_error_flag == STRING_BUFFER_ERROR_FLAG_FAILED) { + kphp_runtime_context.static_SB.reserve(2 * len + 2); + if (kphp_runtime_context.sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED) { return false; } - static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_char('"'); for (int pos = 0; pos < len; pos++) { char c = s[pos]; if (unlikely (static_cast(c) < 32u)) { switch (c) { case '\b': - static_SB.append_char('\\'); - static_SB.append_char('b'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('b'); break; case '\f': - static_SB.append_char('\\'); - static_SB.append_char('f'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('f'); break; case '\n': - static_SB.append_char('\\'); - static_SB.append_char('n'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('n'); break; case '\r': - static_SB.append_char('\\'); - static_SB.append_char('r'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('r'); break; case '\t': - static_SB.append_char('\\'); - static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('t'); break; } } else { if (c == '"' || c == '\\' || c == '/') { - static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('\\'); } - static_SB.append_char(c); + kphp_runtime_context.static_SB.append_char(c); } } - static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_char('"'); return true; } @@ -254,20 +254,20 @@ JsonEncoder::JsonEncoder(int64_t options, bool simple_encode, const char *json_o bool JsonEncoder::encode(bool b) noexcept { if (b) { - static_SB.append("true", 4); + kphp_runtime_context.static_SB.append("true", 4); } else { - static_SB.append("false", 5); + kphp_runtime_context.static_SB.append("false", 5); } return true; } bool JsonEncoder::encode_null() const noexcept { - static_SB.append("null", 4); + kphp_runtime_context.static_SB.append("null", 4); return true; } bool JsonEncoder::encode(int64_t i) noexcept { - static_SB << i; + kphp_runtime_context.static_SB << i; return true; } @@ -275,12 +275,12 @@ bool JsonEncoder::encode(double d) noexcept { if (vk::any_of_equal(std::fpclassify(d), FP_INFINITE, FP_NAN)) { php_warning("%s: strange double %lf in function json_encode", json_path_.to_string().c_str(), d); if (options_ & JSON_PARTIAL_OUTPUT_ON_ERROR) { - static_SB.append("0", 1); + kphp_runtime_context.static_SB.append("0", 1); } else { return false; } } else { - static_SB << (simple_encode_ ? f$number_format(d, 6, string{"."}, string{}) : string{d}); + kphp_runtime_context.static_SB << (simple_encode_ ? f$number_format(d, 6, string{"."}, string{}) : string{d}); } return true; } diff --git a/runtime/json-functions.h b/runtime/json-functions.h index 8754d73dd5..8e46c9418d 100644 --- a/runtime/json-functions.h +++ b/runtime/json-functions.h @@ -4,8 +4,9 @@ #pragma once +#include "runtime-core/runtime-core.h" +#include "runtime/context/runtime-context.h" #include "runtime/exception.h" -#include "runtime/kphp_core.h" #include @@ -80,14 +81,14 @@ bool JsonEncoder::encode(const array &arr) noexcept { } is_vector &= !force_object; - static_SB << "{["[is_vector]; + kphp_runtime_context.static_SB << "{["[is_vector]; if (is_vector) { int i = 0; json_path_.enter(nullptr); // similar key for all entries for (auto p : arr) { if (i != 0) { - static_SB << ','; + kphp_runtime_context.static_SB << ','; } if (!encode(p.get_value())) { if (!(options_ & JSON_PARTIAL_OUTPUT_ON_ERROR)) { @@ -101,7 +102,7 @@ bool JsonEncoder::encode(const array &arr) noexcept { bool is_first = true; for (auto p : arr) { if (!is_first) { - static_SB << ','; + kphp_runtime_context.static_SB << ','; } is_first = false; const char *next_key = nullptr; @@ -109,7 +110,7 @@ bool JsonEncoder::encode(const array &arr) noexcept { if (array::is_int_key(key)) { auto int_key = key.to_int(); next_key = nullptr; - static_SB << '"' << int_key << '"'; + kphp_runtime_context.static_SB << '"' << int_key << '"'; } else { const string &str_key = key.as_string(); // skip service key intended only for distinguish empty json object with empty json array @@ -123,7 +124,7 @@ bool JsonEncoder::encode(const array &arr) noexcept { } } } - static_SB << ':'; + kphp_runtime_context.static_SB << ':'; json_path_.enter(next_key); if (!encode(p.get_value())) { if (!(options_ & JSON_PARTIAL_OUTPUT_ON_ERROR)) { @@ -134,7 +135,7 @@ bool JsonEncoder::encode(const array &arr) noexcept { } } - static_SB << "}]"[is_vector]; + kphp_runtime_context.static_SB << "}]"[is_vector]; return true; } @@ -161,26 +162,26 @@ Optional f$json_encode(const T &v, int64_t options = 0, bool simple_enco return false; } - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); if (unlikely(!impl_::JsonEncoder(options, simple_encode).encode(v))) { return false; } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } template string f$vk_json_encode_safe(const T &v, bool simple_encode = true) noexcept { - static_SB.clean(); - string_buffer::string_buffer_error_flag = STRING_BUFFER_ERROR_FLAG_ON; + kphp_runtime_context.static_SB.clean(); + kphp_runtime_context.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_ON; impl_::JsonEncoder(0, simple_encode).encode(v); - if (unlikely(string_buffer::string_buffer_error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) { - static_SB.clean(); - string_buffer::string_buffer_error_flag = STRING_BUFFER_ERROR_FLAG_OFF; + if (unlikely(kphp_runtime_context.sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) { + kphp_runtime_context.static_SB.clean(); + kphp_runtime_context.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF; THROW_EXCEPTION (new_Exception(string(__FILE__), __LINE__, string("json_encode buffer overflow", 27))); return {}; } - string_buffer::string_buffer_error_flag = STRING_BUFFER_ERROR_FLAG_OFF; - return static_SB.str(); + kphp_runtime_context.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF; + return kphp_runtime_context.static_SB.str(); } template diff --git a/runtime/json-writer.cpp b/runtime/json-writer.cpp index 065477f377..2e9ef7cdbc 100644 --- a/runtime/json-writer.cpp +++ b/runtime/json-writer.cpp @@ -5,6 +5,7 @@ #include "runtime/json-writer.h" #include "runtime/array_functions.h" +#include "runtime/context/runtime-context.h" #include "runtime/math_functions.h" // note: json-writer.cpp is used for classes, e.g. `JsonEncoder::encode(new A)` (also see from/to visitors) @@ -56,18 +57,18 @@ static void escape_json_string(string_buffer &buffer, std::string_view s) noexce JsonWriter::JsonWriter(bool pretty_print, bool preserve_zero_fraction) noexcept : pretty_print_(pretty_print) , preserve_zero_fraction_(preserve_zero_fraction) { - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); } JsonWriter::~JsonWriter() noexcept { - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); } bool JsonWriter::write_bool(bool b) noexcept { if (!register_value()) { return false; } - b ? static_SB.append("true", 4) : static_SB.append("false", 5); + b ? kphp_runtime_context.static_SB.append("true", 4) : kphp_runtime_context.static_SB.append("false", 5); return true; } @@ -75,7 +76,7 @@ bool JsonWriter::write_int(int64_t i) noexcept { if (!register_value()) { return false; } - static_SB << i; + kphp_runtime_context.static_SB << i; return true; } @@ -87,13 +88,13 @@ bool JsonWriter::write_double(double d) noexcept { d = 0.0; } if (double_precision_) { - static_SB << f$round(d, double_precision_); + kphp_runtime_context.static_SB << f$round(d, double_precision_); } else { - static_SB << d; + kphp_runtime_context.static_SB << d; } if (preserve_zero_fraction_) { if (double dummy = 0.0; std::modf(d, &dummy) == 0.0) { - static_SB << ".0"; + kphp_runtime_context.static_SB << ".0"; } } return true; @@ -103,10 +104,10 @@ bool JsonWriter::write_string(const string &s) noexcept { if (!register_value()) { return false; } - static_SB.reserve(2 * s.size() + 2); - static_SB.append_char('"'); - escape_json_string(static_SB, {s.c_str(), s.size()}); - static_SB.append_char('"'); + kphp_runtime_context.static_SB.reserve(2 * s.size() + 2); + kphp_runtime_context.static_SB.append_char('"'); + escape_json_string(kphp_runtime_context.static_SB, {s.c_str(), s.size()}); + kphp_runtime_context.static_SB.append_char('"'); return true; } @@ -114,7 +115,7 @@ bool JsonWriter::write_raw_string(const string &s) noexcept { if (!register_value()) { return false; } - static_SB << s; + kphp_runtime_context.static_SB << s; return true; } @@ -122,7 +123,7 @@ bool JsonWriter::write_null() noexcept { if (!register_value()) { return false; } - static_SB.append("null", 4); + kphp_runtime_context.static_SB.append("null", 4); return true; } @@ -132,22 +133,22 @@ bool JsonWriter::write_key(std::string_view key, bool escape) noexcept { return false; } if (stack_.back().values_count) { - static_SB << ','; + kphp_runtime_context.static_SB << ','; } if (pretty_print_) { - static_SB << '\n'; + kphp_runtime_context.static_SB << '\n'; write_indent(); } - static_SB << '"'; + kphp_runtime_context.static_SB << '"'; if (escape) { - escape_json_string(static_SB, key); + escape_json_string(kphp_runtime_context.static_SB, key); } else { - static_SB.append(key.data(), key.size()); + kphp_runtime_context.static_SB.append(key.data(), key.size()); } - static_SB << '"'; - static_SB << ':'; + kphp_runtime_context.static_SB << '"'; + kphp_runtime_context.static_SB << ':'; if (pretty_print_) { - static_SB << ' '; + kphp_runtime_context.static_SB << ' '; } return true; } @@ -177,7 +178,7 @@ string JsonWriter::get_error() const noexcept { } string JsonWriter::get_final_json() const noexcept { - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } bool JsonWriter::new_level(bool is_array) noexcept { @@ -186,7 +187,7 @@ bool JsonWriter::new_level(bool is_array) noexcept { } stack_.emplace_back(NestedLevel{.in_array = is_array}); - static_SB << (is_array ? '[' : '{'); + kphp_runtime_context.static_SB << (is_array ? '[' : '{'); indent_ += 4; return true; } @@ -208,11 +209,11 @@ bool JsonWriter::exit_level(bool is_array) noexcept { indent_ -= 4; if (pretty_print_ && cur_level.values_count) { - static_SB << '\n'; + kphp_runtime_context.static_SB << '\n'; write_indent(); } - static_SB << (is_array ? ']' : '}'); + kphp_runtime_context.static_SB << (is_array ? ']' : '}'); return true; } @@ -228,10 +229,10 @@ bool JsonWriter::register_value() noexcept { auto &top = stack_.back(); if (top.in_array) { if (top.values_count) { - static_SB << ','; + kphp_runtime_context.static_SB << ','; } if (pretty_print_) { - static_SB << '\n'; + kphp_runtime_context.static_SB << '\n'; write_indent(); } } @@ -242,9 +243,9 @@ bool JsonWriter::register_value() noexcept { void JsonWriter::write_indent() const noexcept { if (indent_) { - static_SB.reserve(indent_); + kphp_runtime_context.static_SB.reserve(indent_); for (std::size_t i = 0; i < indent_; ++i) { - static_SB.append_char(' '); + kphp_runtime_context.static_SB.append_char(' '); } } } diff --git a/runtime/json-writer.h b/runtime/json-writer.h index 76bfd8601b..507716dbf9 100644 --- a/runtime/json-writer.h +++ b/runtime/json-writer.h @@ -4,7 +4,8 @@ #pragma once -#include "runtime/kphp_core.h" +#include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" #include diff --git a/runtime/kphp-backtrace.h b/runtime/kphp-backtrace.h index 9207ca5e32..9c02b3eacc 100644 --- a/runtime/kphp-backtrace.h +++ b/runtime/kphp-backtrace.h @@ -8,8 +8,8 @@ #include #include "common/wrappers/iterator_range.h" - -#include "runtime/kphp_core.h" +#include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" class KphpBacktrace : vk::not_copyable { public: diff --git a/runtime/kphp_ml/kphp_ml.cpp b/runtime/kphp_ml/kphp_ml.cpp index b73c775969..622083d72a 100644 --- a/runtime/kphp_ml/kphp_ml.cpp +++ b/runtime/kphp_ml/kphp_ml.cpp @@ -6,8 +6,8 @@ // This file exists both in KPHP and in a private vkcom repo "ml_experiments". // They are almost identical, besides include paths and input types (`array` vs `unordered_map`). -#include "runtime/kphp_core.h" #include "runtime/kphp_ml/kphp_ml.h" +#include "runtime-core/runtime-core.h" // for detailed comments about KML, see kphp_ml.h diff --git a/runtime/kphp_ml/kphp_ml_catboost.cpp b/runtime/kphp_ml/kphp_ml_catboost.cpp index 24ccaa712e..dae506e7de 100644 --- a/runtime/kphp_ml/kphp_ml_catboost.cpp +++ b/runtime/kphp_ml/kphp_ml_catboost.cpp @@ -8,7 +8,7 @@ #include "runtime/kphp_ml/kphp_ml_catboost.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/kphp_ml/kphp_ml.h" /* diff --git a/runtime/kphp_ml/kphp_ml_catboost.h b/runtime/kphp_ml/kphp_ml_catboost.h index 2214225587..94ad2ee35b 100644 --- a/runtime/kphp_ml/kphp_ml_catboost.h +++ b/runtime/kphp_ml/kphp_ml_catboost.h @@ -12,7 +12,7 @@ #include #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" namespace kphp_ml { struct MLModel; } diff --git a/runtime/kphp_ml/kphp_ml_init.h b/runtime/kphp_ml/kphp_ml_init.h index 4843f785c8..c1648299da 100644 --- a/runtime/kphp_ml/kphp_ml_init.h +++ b/runtime/kphp_ml/kphp_ml_init.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" namespace kphp_ml { struct MLModel; } diff --git a/runtime/kphp_ml/kphp_ml_interface.h b/runtime/kphp_ml/kphp_ml_interface.h index bb785d2173..483732a3e3 100644 --- a/runtime/kphp_ml/kphp_ml_interface.h +++ b/runtime/kphp_ml/kphp_ml_interface.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" Optional> f$kml_xgboost_predict_matrix(const string &model_name, const array> &features_map_matrix); diff --git a/runtime/kphp_ml/kphp_ml_xgboost.cpp b/runtime/kphp_ml/kphp_ml_xgboost.cpp index 82764ba0ad..aa87d6f0a4 100644 --- a/runtime/kphp_ml/kphp_ml_xgboost.cpp +++ b/runtime/kphp_ml/kphp_ml_xgboost.cpp @@ -10,7 +10,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/kphp_ml/kphp_ml.h" /* diff --git a/runtime/kphp_ml/kphp_ml_xgboost.h b/runtime/kphp_ml/kphp_ml_xgboost.h index bd124ce77d..bdad91a1ba 100644 --- a/runtime/kphp_ml/kphp_ml_xgboost.h +++ b/runtime/kphp_ml/kphp_ml_xgboost.h @@ -11,7 +11,7 @@ #include #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" /* * For detailed comments about KML, see kphp_ml.h. diff --git a/runtime/kphp_tracing.h b/runtime/kphp_tracing.h index 3517d6288c..420fcb33fd 100644 --- a/runtime/kphp_tracing.h +++ b/runtime/kphp_tracing.h @@ -6,9 +6,9 @@ #include +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/critical_section.h" -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" #include "runtime/dummy-visitor-methods.h" // for detailed comments about tracing in general, see kphp_tracing.cpp diff --git a/runtime/kphp_tracing_binlog.cpp b/runtime/kphp_tracing_binlog.cpp index ed09c0b7bc..03c65672ea 100644 --- a/runtime/kphp_tracing_binlog.cpp +++ b/runtime/kphp_tracing_binlog.cpp @@ -6,8 +6,11 @@ #include #include +#include +#include #include "runtime/critical_section.h" +#include "runtime/allocator.h" #include "server/json-logger.h" diff --git a/runtime/kphp_tracing_binlog.h b/runtime/kphp_tracing_binlog.h index 05f1a65177..b1e334ff80 100644 --- a/runtime/kphp_tracing_binlog.h +++ b/runtime/kphp_tracing_binlog.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" namespace kphp_tracing { diff --git a/runtime/mail.h b/runtime/mail.h index 0ec58241e9..eb1cabf778 100644 --- a/runtime/mail.h +++ b/runtime/mail.h @@ -4,6 +4,6 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" bool f$mail(const string &to, const string &subject, const string &message, string additional_headers = string()); diff --git a/runtime/math_functions.h b/runtime/math_functions.h index 9d71585763..13c02959a4 100644 --- a/runtime/math_functions.h +++ b/runtime/math_functions.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" int64_t f$bindec(const string &number) noexcept; diff --git a/runtime/mbstring.h b/runtime/mbstring.h index 9685f4be76..a6abe3638a 100644 --- a/runtime/mbstring.h +++ b/runtime/mbstring.h @@ -6,7 +6,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/string_functions.h" bool mb_UTF8_check(const char *s); diff --git a/runtime/memcache.h b/runtime/memcache.h index fc0c030dfb..048d826241 100644 --- a/runtime/memcache.h +++ b/runtime/memcache.h @@ -6,15 +6,15 @@ #include +#include "common/algorithms/hashes.h" +#include "common/wrappers/string_view.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" #include "runtime/exception.h" -#include "runtime/kphp_core.h" #include "runtime/memory_usage.h" #include "runtime/net_events.h" #include "runtime/resumable.h" #include "runtime/rpc.h" -#include "common/algorithms/hashes.h" -#include "common/wrappers/string_view.h" void init_memcache_lib(); void free_memcache_lib(); diff --git a/runtime/memory_resource/memory_resource.cpp b/runtime/memory_resource/memory_resource.cpp deleted file mode 100644 index a96314e746..0000000000 --- a/runtime/memory_resource/memory_resource.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2020 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime/memory_resource/memory_resource.h" - -namespace memory_resource { - -void MemoryStats::write_stats_to(stats_t *stats, const char *prefix) const noexcept { - stats->add_gauge_stat(memory_limit, prefix, ".memory.limit"); - stats->add_gauge_stat(memory_used, prefix, ".memory.used"); - stats->add_gauge_stat(real_memory_used, prefix, ".memory.real_used"); - stats->add_gauge_stat(max_memory_used, prefix, ".memory.used_max"); - stats->add_gauge_stat(max_real_memory_used, prefix, ".memory.real_used_max"); - stats->add_gauge_stat(defragmentation_calls, prefix, ".memory.defragmentation_calls"); - stats->add_gauge_stat(huge_memory_pieces, prefix, ".memory.huge_memory_pieces"); - stats->add_gauge_stat(small_memory_pieces, prefix, ".memory.small_memory_pieces"); -} - -} // namespace memory_resource diff --git a/runtime/memory_resource/dealer.cpp b/runtime/memory_resource_impl/dealer.cpp similarity index 86% rename from runtime/memory_resource/dealer.cpp rename to runtime/memory_resource_impl/dealer.cpp index f9886cbb69..44bcbbb84f 100644 --- a/runtime/memory_resource/dealer.cpp +++ b/runtime/memory_resource_impl/dealer.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2020 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/memory_resource/dealer.h" +#include "runtime/memory_resource_impl//dealer.h" namespace memory_resource { diff --git a/runtime/memory_resource/dealer.h b/runtime/memory_resource_impl/dealer.h similarity index 91% rename from runtime/memory_resource/dealer.h rename to runtime/memory_resource_impl/dealer.h index 958fed3101..e08c5b5ab6 100644 --- a/runtime/memory_resource/dealer.h +++ b/runtime/memory_resource_impl/dealer.h @@ -3,8 +3,8 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #pragma once -#include "runtime/memory_resource/heap_resource.h" -#include "runtime/memory_resource/unsynchronized_pool_resource.h" +#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" +#include "runtime/memory_resource_impl/heap_resource.h" namespace memory_resource { diff --git a/runtime/memory_resource/heap_resource.cpp b/runtime/memory_resource_impl/heap_resource.cpp similarity index 96% rename from runtime/memory_resource/heap_resource.cpp rename to runtime/memory_resource_impl/heap_resource.cpp index 6fadacd66b..3d395bf1f4 100644 --- a/runtime/memory_resource/heap_resource.cpp +++ b/runtime/memory_resource_impl/heap_resource.cpp @@ -2,7 +2,7 @@ // Copyright (c) 2020 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/memory_resource/heap_resource.h" +#include "runtime/memory_resource_impl//heap_resource.h" #include #include diff --git a/runtime/memory_resource/heap_resource.h b/runtime/memory_resource_impl/heap_resource.h similarity index 90% rename from runtime/memory_resource/heap_resource.h rename to runtime/memory_resource_impl/heap_resource.h index fef4237def..740b043622 100644 --- a/runtime/memory_resource/heap_resource.h +++ b/runtime/memory_resource_impl/heap_resource.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/memory_resource/memory_resource.h" +#include "runtime-core/memory-resource/memory_resource.h" namespace memory_resource { diff --git a/runtime/memory_resource_impl/memory_resource_stats.cpp b/runtime/memory_resource_impl/memory_resource_stats.cpp new file mode 100644 index 0000000000..92db1cce56 --- /dev/null +++ b/runtime/memory_resource_impl/memory_resource_stats.cpp @@ -0,0 +1,16 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#include "runtime/memory_resource_impl/memory_resource_stats.h" + +void write_memory_stats_to(const memory_resource::MemoryStats & memoryStats, stats_t *stats, const char *prefix) noexcept { + stats->add_gauge_stat(memoryStats.memory_limit, prefix, ".memory.limit"); + stats->add_gauge_stat(memoryStats.memory_used, prefix, ".memory.used"); + stats->add_gauge_stat(memoryStats.real_memory_used, prefix, ".memory.real_used"); + stats->add_gauge_stat(memoryStats.max_memory_used, prefix, ".memory.used_max"); + stats->add_gauge_stat(memoryStats.max_real_memory_used, prefix, ".memory.real_used_max"); + stats->add_gauge_stat(memoryStats.defragmentation_calls, prefix, ".memory.defragmentation_calls"); + stats->add_gauge_stat(memoryStats.huge_memory_pieces, prefix, ".memory.huge_memory_pieces"); + stats->add_gauge_stat(memoryStats.small_memory_pieces, prefix, ".memory.small_memory_pieces"); +} \ No newline at end of file diff --git a/runtime/memory_resource_impl/memory_resource_stats.h b/runtime/memory_resource_impl/memory_resource_stats.h new file mode 100644 index 0000000000..dd41b25e12 --- /dev/null +++ b/runtime/memory_resource_impl/memory_resource_stats.h @@ -0,0 +1,10 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once + +#include "common/stats/provider.h" +#include "runtime-core/memory-resource/memory_resource.h" + +void write_memory_stats_to(const memory_resource::MemoryStats & memoryStats, stats_t *stats, const char *prefix) noexcept; diff --git a/runtime/memory_resource/monotonic_buffer_resource.cpp b/runtime/memory_resource_impl/monotonic_runtime_buffer_resource.cpp similarity index 63% rename from runtime/memory_resource/monotonic_buffer_resource.cpp rename to runtime/memory_resource_impl/monotonic_runtime_buffer_resource.cpp index 2fdb5a8d82..7051dcc892 100644 --- a/runtime/memory_resource/monotonic_buffer_resource.cpp +++ b/runtime/memory_resource_impl/monotonic_runtime_buffer_resource.cpp @@ -1,26 +1,17 @@ // Compiler for PHP (aka KPHP) -// Copyright (c) 2020 LLC «V Kontakte» +// Copyright (c) 2024 LLC «V Kontakte» // Distributed under the GPL v3 License, see LICENSE.notice.txt -#include "runtime/memory_resource/monotonic_buffer_resource.h" +#include "runtime-core/memory-resource/monotonic_buffer_resource.h" #include + #include "runtime/allocator.h" +#include "runtime/php_assert.h" #include "runtime/oom_handler.h" namespace memory_resource { - -void monotonic_buffer::init(void *buffer, size_t buffer_size) noexcept { - php_assert(buffer_size <= memory_buffer_limit()); - memory_begin_ = static_cast(buffer); - memory_current_ = memory_begin_; - memory_end_ = memory_begin_ + buffer_size; - - stats_ = MemoryStats{}; - stats_.memory_limit = buffer_size; -} - -void monotonic_buffer::critical_dump(void *mem, size_t size) const noexcept { +void monotonic_buffer_resource::critical_dump(void *mem, size_t size) const noexcept { std::array malloc_replacement_stacktrace_buf = {'\0'}; if (dl::is_malloc_replaced()) { const char *descr = "last_malloc_replacement_stacktrace:\n"; @@ -28,25 +19,21 @@ void monotonic_buffer::critical_dump(void *mem, size_t size) const noexcept { dl::write_last_malloc_replacement_stacktrace(malloc_replacement_stacktrace_buf.data() + strlen(descr), malloc_replacement_stacktrace_buf.size() - strlen(descr)); } - php_critical_error( - "Found unexpected memory piece:\n" - "ptr: %p\n" - "size: %zu\n" - "memory_begin: %p\n" - "memory_current: %p\n" - "memory_end: %p\n" - "memory_limit: %zu\n" - "memory_used: %zu\n" - "max_memory_used: %zu\n" - "real_memory_used: %zu\n" - "max_real_memory_used: %zu\n" - "is_malloc_replaced: %d\n" - "%s", - mem, size, memory_begin_, memory_current_, memory_end_, - stats_.memory_limit, - stats_.memory_used, stats_.max_memory_used, - stats_.real_memory_used, stats_.max_real_memory_used, - dl::is_malloc_replaced(), malloc_replacement_stacktrace_buf.data()); + php_critical_error("Found unexpected memory piece:\n" + "ptr: %p\n" + "size: %zu\n" + "memory_begin: %p\n" + "memory_current: %p\n" + "memory_end: %p\n" + "memory_limit: %zu\n" + "memory_used: %zu\n" + "max_memory_used: %zu\n" + "real_memory_used: %zu\n" + "max_real_memory_used: %zu\n" + "is_malloc_replaced: %d\n" + "%s", + mem, size, memory_begin_, memory_current_, memory_end_, stats_.memory_limit, stats_.memory_used, stats_.max_memory_used, + stats_.real_memory_used, stats_.max_real_memory_used, dl::is_malloc_replaced(), malloc_replacement_stacktrace_buf.data()); } void monotonic_buffer_resource::raise_oom(size_t size) const noexcept { @@ -78,5 +65,4 @@ void monotonic_buffer_resource::raise_oom(size_t size) const noexcept { raise(SIGUSR2); } - -} // namespace memory_resource +} \ No newline at end of file diff --git a/runtime/memory_usage.h b/runtime/memory_usage.h index c024b3ab6f..abfbf783ee 100644 --- a/runtime/memory_usage.h +++ b/runtime/memory_usage.h @@ -13,9 +13,8 @@ #include "common/mixin/not_copyable.h" #include "common/type_traits/list_of_types.h" -#include "runtime/declarations.h" -#include "runtime/kphp_core.h" -#include "runtime/shape.h" +#include "runtime-core/runtime-core.h" +#include "runtime/allocator.h" template struct CDataPtr; @@ -172,3 +171,44 @@ int64_t f$estimate_memory_usage(const T &value) { } array f$get_global_vars_memory_stats(int64_t lower_bound = 0); + +inline int64_t f$memory_get_static_usage() { + return static_cast(dl::get_heap_memory_used()); +} + +inline int64_t f$memory_get_peak_usage(bool real_usage) { + if (real_usage) { + return static_cast(dl::get_script_memory_stats().max_real_memory_used); + } else { + return static_cast(dl::get_script_memory_stats().max_memory_used); + } +} + +inline int64_t f$memory_get_usage(bool real_usage __attribute__((unused))) { + return static_cast(dl::get_script_memory_stats().memory_used); +} + +inline int64_t f$memory_get_total_usage() { + return static_cast(dl::get_script_memory_stats().real_memory_used); +} + +inline array f$memory_get_detailed_stats() { + const auto &stats = dl::get_script_memory_stats(); + return array( + { + std::make_pair(string{"memory_limit"}, static_cast(stats.memory_limit)), + std::make_pair(string{"real_memory_used"}, static_cast(stats.real_memory_used)), + std::make_pair(string{"memory_used"}, static_cast(stats.memory_used)), + std::make_pair(string{"max_real_memory_used"}, static_cast(stats.max_real_memory_used)), + std::make_pair(string{"max_memory_used"}, static_cast(stats.max_memory_used)), + std::make_pair(string{"defragmentation_calls"}, static_cast(stats.defragmentation_calls)), + std::make_pair(string{"huge_memory_pieces"}, static_cast(stats.huge_memory_pieces)), + std::make_pair(string{"small_memory_pieces"}, static_cast(stats.small_memory_pieces)), + std::make_pair(string{"heap_memory_used"}, static_cast(dl::get_heap_memory_used())) + }); +} + +inline std::tuple f$memory_get_allocations() { + const auto &stats = dl::get_script_memory_stats(); + return {stats.total_allocations, stats.total_memory_allocated}; +} diff --git a/runtime/misc.cpp b/runtime/misc.cpp index 62dfb24ead..ad82d4f385 100644 --- a/runtime/misc.cpp +++ b/runtime/misc.cpp @@ -37,18 +37,18 @@ string f$uniqid(const string &prefix, bool more_entropy) { size_t buf_size = 30; char buf[buf_size]; - static_SB.clean() << prefix; + kphp_runtime_context.static_SB.clean() << prefix; if (more_entropy) { snprintf(buf, buf_size, "%08x%05x%.8f", sec, usec, f$lcg_value() * 10); - static_SB.append(buf, 23); + kphp_runtime_context.static_SB.append(buf, 23); } else { snprintf(buf, buf_size, "%08x%05x", sec, usec); - static_SB.append(buf, 13); + kphp_runtime_context.static_SB.append(buf, 13); } dl::leave_critical_section(); - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } @@ -710,10 +710,10 @@ string f$cp1251(const string &utf8_string) { void f$kphp_set_context_on_error(const array &tags, const array &extra_info, const string& env) { auto &json_logger = vk::singleton::get(); - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); - const auto get_json_string_from_SB_without_brackets = [] { - vk::string_view json_str{static_SB.c_str(), static_SB.size()}; + const auto get_json_string_from_SB_without_brackets = [&] { + vk::string_view json_str{kphp_runtime_context.static_SB.c_str(), kphp_runtime_context.static_SB.size()}; php_assert(json_str.size() >= 2 && json_str.front() == '{' && json_str.back() == '}'); json_str.remove_prefix(1); json_str.remove_suffix(1); @@ -725,14 +725,14 @@ void f$kphp_set_context_on_error(const array &tags, const array &e dl::CriticalSectionGuard critical_section; json_logger.set_tags(tags_json); } - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); if (impl_::JsonEncoder(JSON_FORCE_OBJECT, false).encode(extra_info)) { auto extra_info_json = get_json_string_from_SB_without_brackets(); dl::CriticalSectionGuard critical_section; json_logger.set_extra_info(extra_info_json); } - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); dl::CriticalSectionGuard critical_section; json_logger.set_env({env.c_str(), env.size()}); diff --git a/runtime/misc.h b/runtime/misc.h index 50cd617e56..cb7e969bd8 100644 --- a/runtime/misc.h +++ b/runtime/misc.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" string f$uniqid(const string &prefix = string(), bool more_entropy = false); diff --git a/runtime/msgpack-serialization.h b/runtime/msgpack-serialization.h index e0b594d0a0..37a1f2fee2 100644 --- a/runtime/msgpack-serialization.h +++ b/runtime/msgpack-serialization.h @@ -11,10 +11,11 @@ #include "common/containers/final_action.h" +#include "runtime-core/runtime-core.h" +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/exception.h" #include "runtime/interface.h" -#include "runtime/kphp_core.h" #include "runtime/string_functions.h" template @@ -22,15 +23,15 @@ inline Optional f$msgpack_serialize(const T &value, string *out_err_msg f$ob_start(); php_assert(f$ob_get_length().has_value() && f$ob_get_length().val() == 0); - string_buffer::string_buffer_error_flag = STRING_BUFFER_ERROR_FLAG_ON; + kphp_runtime_context.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_ON; auto clean_buffer = vk::finally([] { f$ob_end_clean(); - string_buffer::string_buffer_error_flag = STRING_BUFFER_ERROR_FLAG_OFF; + kphp_runtime_context.sb_lib_context.error_flag = STRING_BUFFER_ERROR_FLAG_OFF; }); vk::msgpack::packer{*coub}.pack(value); - if (string_buffer::string_buffer_error_flag == STRING_BUFFER_ERROR_FLAG_FAILED) { + if (kphp_runtime_context.sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED) { string err_msg{"msgpacke_serialize buffer overflow"}; if (out_err_msg) { *out_err_msg = std::move(err_msg); diff --git a/runtime/msgpack/adaptors.h b/runtime/msgpack/adaptors.h index 7de7e26cf6..fa2070635b 100644 --- a/runtime/msgpack/adaptors.h +++ b/runtime/msgpack/adaptors.h @@ -7,7 +7,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/msgpack/adaptor_base.h" #include "runtime/msgpack/check_instance_depth.h" #include "runtime/msgpack/object.h" diff --git a/runtime/msgpack/packer.cpp b/runtime/msgpack/packer.cpp index 28eb12c03b..914bd10556 100644 --- a/runtime/msgpack/packer.cpp +++ b/runtime/msgpack/packer.cpp @@ -5,7 +5,7 @@ #include "runtime/msgpack/packer.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/msgpack/sysdep.h" namespace vk::msgpack { diff --git a/runtime/msgpack/unpacker.h b/runtime/msgpack/unpacker.h index 7bd5471db2..6784ae215e 100644 --- a/runtime/msgpack/unpacker.h +++ b/runtime/msgpack/unpacker.h @@ -6,7 +6,7 @@ #pragma once #include "common/mixin/not_copyable.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/msgpack/object.h" #include "runtime/msgpack/zone.h" diff --git a/runtime/mysql.h b/runtime/mysql.h index 9c001fd53b..4d812ced0f 100644 --- a/runtime/mysql.h +++ b/runtime/mysql.h @@ -4,10 +4,10 @@ #pragma once +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" #include "runtime/memory_usage.h" -#include "runtime/refcountable_php_classes.h" class C$mysqli : public refcountable_php_classes, private DummyVisitorMethods { public: diff --git a/runtime/null_coalesce.h b/runtime/null_coalesce.h index 159eeb35d9..146c4e0ff3 100644 --- a/runtime/null_coalesce.h +++ b/runtime/null_coalesce.h @@ -5,8 +5,8 @@ #pragma once #include -#include "runtime/include.h" -#include "runtime/kphp_core.h" +#include "runtime-core/include.h" +#include "runtime-core/runtime-core.h" namespace impl_ { diff --git a/runtime/on_kphp_warning_callback.h b/runtime/on_kphp_warning_callback.h index 7b48bcf81b..6896c8f816 100644 --- a/runtime/on_kphp_warning_callback.h +++ b/runtime/on_kphp_warning_callback.h @@ -5,8 +5,8 @@ #pragma once #include +#include "runtime-core/runtime-core.h" #include "runtime/critical_section.h" -#include "runtime/kphp_core.h" using on_kphp_warning_callback_type = std::function &)>; diff --git a/runtime/openssl.cpp b/runtime/openssl.cpp index cf7c91433b..ef6635596b 100644 --- a/runtime/openssl.cpp +++ b/runtime/openssl.cpp @@ -29,6 +29,7 @@ #include "common/wrappers/to_array.h" #include "runtime/array_functions.h" +#include "runtime/allocator.h" #include "runtime/critical_section.h" #include "runtime/datetime/datetime_functions.h" #include "runtime/files.h" @@ -515,11 +516,11 @@ static const EVP_MD *openssl_algo_to_evp_md(openssl_algo algo) { } static const char *ssl_get_error_string() { - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); while (unsigned long error_code = ERR_get_error()) { - static_SB << "Error " << (int)error_code << ": [" << ERR_error_string(error_code, nullptr) << "]\n"; + kphp_runtime_context.static_SB << "Error " << (int)error_code << ": [" << ERR_error_string(error_code, nullptr) << "]\n"; } - return static_SB.c_str(); + return kphp_runtime_context.static_SB.c_str(); } bool f$openssl_sign(const string &data, string &signature, const string &priv_key_id, int64_t algo) { diff --git a/runtime/openssl.h b/runtime/openssl.h index 06ba6d70be..21ffb44d79 100644 --- a/runtime/openssl.h +++ b/runtime/openssl.h @@ -6,7 +6,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" enum openssl_algo { OPENSSL_ALGO_SHA1 = 1, diff --git a/runtime/pdo/abstract_pdo_driver.h b/runtime/pdo/abstract_pdo_driver.h index 74e318e148..7c574559da 100644 --- a/runtime/pdo/abstract_pdo_driver.h +++ b/runtime/pdo/abstract_pdo_driver.h @@ -6,8 +6,8 @@ #include +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" -#include "runtime/kphp_core.h" struct C$PDO; struct C$PDOStatement; diff --git a/runtime/pdo/abstract_pdo_statement.h b/runtime/pdo/abstract_pdo_statement.h index e503e63011..26737846dc 100644 --- a/runtime/pdo/abstract_pdo_statement.h +++ b/runtime/pdo/abstract_pdo_statement.h @@ -4,8 +4,8 @@ #pragma once +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" -#include "runtime/kphp_core.h" struct C$PDO; struct C$PDOStatement; diff --git a/runtime/pdo/mysql/mysql_pdo_driver.h b/runtime/pdo/mysql/mysql_pdo_driver.h index 231fb978af..f362a69b9e 100644 --- a/runtime/pdo/mysql/mysql_pdo_driver.h +++ b/runtime/pdo/mysql/mysql_pdo_driver.h @@ -4,8 +4,8 @@ #pragma once +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" -#include "runtime/kphp_core.h" #include "runtime/pdo/abstract_pdo_driver.h" namespace pdo::mysql { diff --git a/runtime/pdo/mysql/mysql_pdo_emulated_statement.h b/runtime/pdo/mysql/mysql_pdo_emulated_statement.h index 66c3f78629..b15121d80f 100644 --- a/runtime/pdo/mysql/mysql_pdo_emulated_statement.h +++ b/runtime/pdo/mysql/mysql_pdo_emulated_statement.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/pdo/abstract_pdo_statement.h" namespace database_drivers { diff --git a/runtime/pdo/pdo.h b/runtime/pdo/pdo.h index f22a3a7f5e..46b9f0c022 100644 --- a/runtime/pdo/pdo.h +++ b/runtime/pdo/pdo.h @@ -9,11 +9,11 @@ #include "common/algorithms/hashes.h" #include "common/wrappers/string_view.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" #include "runtime/memory_usage.h" #include "runtime/pdo/abstract_pdo_driver.h" -#include "runtime/refcountable_php_classes.h" struct C$PDO : public refcountable_polymorphic_php_classes, private DummyVisitorMethods { static constexpr int ATTR_TIMEOUT = 2; diff --git a/runtime/pdo/pdo_statement.h b/runtime/pdo/pdo_statement.h index 17aa965d3a..3243117deb 100644 --- a/runtime/pdo/pdo_statement.h +++ b/runtime/pdo/pdo_statement.h @@ -9,13 +9,12 @@ #include "common/algorithms/hashes.h" #include "common/wrappers/string_view.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" #include "runtime/memory_usage.h" -#include "runtime/refcountable_php_classes.h" #include "runtime/pdo/abstract_pdo_statement.h" - struct C$PDOStatement : public refcountable_polymorphic_php_classes, private DummyVisitorMethods { std::unique_ptr statement; int64_t timeout_sec{-1}; diff --git a/runtime/pdo/pgsql/pgsql_pdo_driver.h b/runtime/pdo/pgsql/pgsql_pdo_driver.h index bcc4d577b1..8b7d22b0b1 100644 --- a/runtime/pdo/pgsql/pgsql_pdo_driver.h +++ b/runtime/pdo/pgsql/pgsql_pdo_driver.h @@ -1,7 +1,7 @@ #pragma once +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" -#include "runtime/kphp_core.h" #include "runtime/pdo/abstract_pdo_driver.h" namespace pdo::pgsql { diff --git a/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.cpp b/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.cpp index 5ef7e6f47c..46e0a32e7d 100644 --- a/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.cpp +++ b/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.cpp @@ -1,8 +1,8 @@ #include -#include "runtime/pdo/pgsql/pgsql_pdo_emulated_statement.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/pdo/pdo_statement.h" +#include "runtime/pdo/pgsql/pgsql_pdo_emulated_statement.h" #include "runtime/resumable.h" #include "server/database-drivers/adaptor.h" #include "server/database-drivers/pgsql/pgsql-request.h" diff --git a/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.h b/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.h index 9f578751dc..ed130ae27b 100644 --- a/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.h +++ b/runtime/pdo/pgsql/pgsql_pdo_emulated_statement.h @@ -1,6 +1,6 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/pdo/abstract_pdo_statement.h" namespace database_drivers { diff --git a/runtime/php-script-globals.h b/runtime/php-script-globals.h index 028a165c56..a81bd03d64 100644 --- a/runtime/php-script-globals.h +++ b/runtime/php-script-globals.h @@ -6,7 +6,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" struct PhpScriptBuiltInSuperGlobals { // variables below are PHP language superglobals diff --git a/runtime/php_assert.cpp b/runtime/php_assert.cpp index c8f11fc95b..5b3b8a5d57 100644 --- a/runtime/php_assert.cpp +++ b/runtime/php_assert.cpp @@ -236,4 +236,20 @@ void php_assert__(const char *msg, const char *file, int line) { void raise_php_assert_signal__() { raise(SIGPHPASSERT); vk::singleton::get().fsync_log_file(); + _exit(1); +} + +int64_t f$error_reporting(int64_t level) { + int32_t prev = php_warning_level; + if ((level & E_ALL) == E_ALL) { + php_warning_level = 3; + } + if (0 <= level && level <= 3) { + php_warning_level = std::max(php_warning_minimum_level, static_cast(level)); + } + return prev; +} + +int64_t f$error_reporting() { + return php_warning_level; } diff --git a/runtime/php_assert.h b/runtime/php_assert.h index c4514f9b2f..0532199bac 100644 --- a/runtime/php_assert.h +++ b/runtime/php_assert.h @@ -6,10 +6,13 @@ #include #include +#include #include "common/wrappers/likely.h" #include "common/mixin/not_copyable.h" +#include "runtime-core/utils/kphp-assert-core.h" + extern int die_on_fail; extern const char *engine_tag; @@ -19,9 +22,6 @@ extern int php_disable_warnings; extern int php_warning_level; extern int php_warning_minimum_level; -void php_notice(char const *message, ...) __attribute__ ((format (printf, 1, 2))); -void php_warning(char const *message, ...) __attribute__ ((format (printf, 1, 2))); -void php_error(char const *message, ...) __attribute__ ((format (printf, 1, 2))); void php_out_of_memory_warning(char const *message, ...) __attribute__ ((format (printf, 1, 2))); template @@ -29,18 +29,6 @@ class class_instance; struct C$Throwable; const char *php_uncaught_exception_error(const class_instance &ex) noexcept; -void php_assert__(const char *msg, const char *file, int line) __attribute__((noreturn)); -void raise_php_assert_signal__(); - -#define php_assert(EX) do { \ - if (unlikely(!(EX))) { \ - php_assert__ (#EX, __FILE__, __LINE__); \ - } \ -} while(0) - -#define php_critical_error(format, ...) do { \ - php_error ("Critical error \"" format "\" in file %s on line %d", ##__VA_ARGS__, __FILE__, __LINE__); \ - raise_php_assert_signal__(); \ - fprintf (stderr, "_exiting in php_critical_error\n"); \ - _exit (1); \ -} while(0) +int64_t f$error_reporting(int64_t level); + +int64_t f$error_reporting(); diff --git a/runtime/profiler.h b/runtime/profiler.h index 742ab2706c..cb5ae96270 100644 --- a/runtime/profiler.h +++ b/runtime/profiler.h @@ -12,7 +12,8 @@ #include "common/mixin/not_copyable.h" #include "common/wrappers/string_view.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" +#include "runtime/allocator.h" #include "server/php-queries-stats.h" template diff --git a/runtime/regexp.cpp b/runtime/regexp.cpp index 16669bf205..3fb1a67db7 100644 --- a/runtime/regexp.cpp +++ b/runtime/regexp.cpp @@ -412,7 +412,7 @@ void regexp::init(const char *regexp_string, int64_t regexp_len, const char *fun return; } - static_SB.clean().append(regexp_string + 1, static_cast(regexp_end - 1)); + kphp_runtime_context.static_SB.clean().append(regexp_string + 1, static_cast(regexp_end - 1)); use_heap_memory = !(php_script.has_value() && php_script->is_running()); @@ -478,23 +478,22 @@ void regexp::init(const char *regexp_string, int64_t regexp_len, const char *fun } } - can_use_RE2 = can_use_RE2 && is_valid_RE2_regexp(static_SB.c_str(), static_SB.size(), is_utf8, function, file); + can_use_RE2 = can_use_RE2 && is_valid_RE2_regexp(kphp_runtime_context.static_SB.c_str(), kphp_runtime_context.static_SB.size(), is_utf8, function, file); - if (is_utf8 && !mb_UTF8_check(static_SB.c_str())) { - pattern_compilation_warning(function, file, "Regexp \"%s\" contains not UTF-8 symbols", static_SB.c_str()); + if (is_utf8 && !mb_UTF8_check(kphp_runtime_context.static_SB.c_str())) { + pattern_compilation_warning(function, file, "Regexp \"%s\" contains not UTF-8 symbols", kphp_runtime_context.static_SB.c_str()); clean(); return; } bool need_pcre = false; if (can_use_RE2) { - RE2_regexp = new RE2(re2::StringPiece(static_SB.c_str(), static_SB.size()), RE2_options); + RE2_regexp = new RE2(re2::StringPiece(kphp_runtime_context.static_SB.c_str(), kphp_runtime_context.static_SB.size()), RE2_options); #if ASAN_ENABLED __lsan_ignore_object(RE2_regexp); #endif if (!RE2_regexp->ok()) { - pattern_compilation_warning(function, file, "RE2 compilation of regexp \"%s\" failed. Error %d at %s", - static_SB.c_str(), RE2_regexp->error_code(), RE2_regexp->error().c_str()); + pattern_compilation_warning(function, file, "RE2 compilation of regexp \"%s\" failed. Error %d at %s", kphp_runtime_context.static_SB.c_str(), RE2_regexp->error_code(), RE2_regexp->error().c_str()); delete RE2_regexp; RE2_regexp = nullptr; @@ -514,7 +513,7 @@ void regexp::init(const char *regexp_string, int64_t regexp_len, const char *fun if (RE2_regexp == nullptr || need_pcre) { const char *error; int32_t erroffset = 0; - pcre_regexp = pcre_compile(static_SB.c_str(), pcre_options, &error, &erroffset, nullptr); + pcre_regexp = pcre_compile(kphp_runtime_context.static_SB.c_str(), pcre_options, &error, &erroffset, nullptr); #if ASAN_ENABLED __lsan_ignore_object(pcre_regexp); #endif @@ -1078,7 +1077,7 @@ int64_t regexp::last_error() { string f$preg_quote(const string &str, const string &delimiter) { const string::size_type len = str.size(); - static_SB.clean().reserve(4 * len); + kphp_runtime_context.static_SB.clean().reserve(4 * len); for (string::size_type i = 0; i < len; i++) { switch (str[i]) { @@ -1103,25 +1102,25 @@ string f$preg_quote(const string &str, const string &delimiter) { case ':': case '-': case '#': - static_SB.append_char('\\'); - static_SB.append_char(str[i]); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char(str[i]); break; case '\0': - static_SB.append_char('\\'); - static_SB.append_char('0'); - static_SB.append_char('0'); - static_SB.append_char('0'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('0'); + kphp_runtime_context.static_SB.append_char('0'); + kphp_runtime_context.static_SB.append_char('0'); break; default: if (!delimiter.empty() && str[i] == delimiter[0]) { - static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('\\'); } - static_SB.append_char(str[i]); + kphp_runtime_context.static_SB.append_char(str[i]); break; } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } void regexp::global_init() { diff --git a/runtime/regexp.h b/runtime/regexp.h index 40fbde3308..1de0260cee 100644 --- a/runtime/regexp.h +++ b/runtime/regexp.h @@ -8,9 +8,10 @@ #include "common/mixin/not_copyable.h" -#include "runtime/kphp_core.h" -#include "runtime/mbstring.h" +#include "runtime-core/runtime-core.h" +#include "runtime/context/runtime-context.h" #include "runtime/kphp_tracing.h" +#include "runtime/mbstring.h" namespace re2 { class RE2; @@ -210,8 +211,7 @@ inline int64_t f$preg_last_error(); template<> inline string regexp::get_replacement(const string &replace_val, const string &subject, int64_t count) const { const string::size_type len = replace_val.size(); - - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); for (string::size_type i = 0; i < len; i++) { int64_t backref = -1; if (replace_val[i] == '\\' && (replace_val[i + 1] == '\\' || replace_val[i + 1] == '$')) { @@ -239,16 +239,16 @@ inline string regexp::get_replacement(const string &replace_val, const string &s } if (backref == -1) { - static_SB << replace_val[i]; + kphp_runtime_context.static_SB << replace_val[i]; } else { if (backref < count) { int64_t index = backref + backref; - static_SB.append(subject.c_str() + submatch[index], + kphp_runtime_context.static_SB.append(subject.c_str() + submatch[index], static_cast(submatch[index + 1] - submatch[index])); } } } - return static_SB.str();//TODO optimize + return kphp_runtime_context.static_SB.str();//TODO optimize } template diff --git a/runtime/resumable.h b/runtime/resumable.h index 56df39a775..649e40aaf0 100644 --- a/runtime/resumable.h +++ b/runtime/resumable.h @@ -4,9 +4,9 @@ #pragma once +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" #include "runtime/exception.h" -#include "runtime/kphp_core.h" #include "runtime/storage.h" extern bool resumable_finished; diff --git a/runtime/rpc.cpp b/runtime/rpc.cpp index d476cc54e0..0568326b81 100644 --- a/runtime/rpc.cpp +++ b/runtime/rpc.cpp @@ -12,6 +12,7 @@ #include "common/rpc-headers.h" #include "common/tl/constants/common.h" +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/exception.h" #include "runtime/kphp_tracing.h" @@ -1395,9 +1396,9 @@ class rpc_tl_query_result_resumable : public Resumable { int64_t query_id = it.get_value(); if (!tl_objects_unsorted.isset(query_id)) { if (query_id <= 0) { - tl_objects[it.get_key()] = tl_fetch_error((static_SB.clean() << "Very wrong query_id " << query_id).str(), TL_ERROR_WRONG_QUERY_ID); + tl_objects[it.get_key()] = tl_fetch_error((kphp_runtime_context.static_SB.clean() << "Very wrong query_id " << query_id).str(), TL_ERROR_WRONG_QUERY_ID); } else { - tl_objects[it.get_key()] = tl_fetch_error((static_SB.clean() << "No answer received or duplicate/wrong query_id " + tl_objects[it.get_key()] = tl_fetch_error((kphp_runtime_context.static_SB.clean() << "No answer received or duplicate/wrong query_id " << query_id).str(), TL_ERROR_WRONG_QUERY_ID); } } else { @@ -1448,9 +1449,9 @@ array> f$rpc_tl_query_result_synchronously(const array &qu int64_t query_id = it.get_value(); if (!tl_objects_unsorted.isset(query_id)) { if (query_id <= 0) { - tl_objects[it.get_key()] = tl_fetch_error((static_SB.clean() << "Very wrong query_id " << query_id).str(), TL_ERROR_WRONG_QUERY_ID); + tl_objects[it.get_key()] = tl_fetch_error((kphp_runtime_context.static_SB.clean() << "Very wrong query_id " << query_id).str(), TL_ERROR_WRONG_QUERY_ID); } else { - tl_objects[it.get_key()] = tl_fetch_error((static_SB.clean() << "No answer received or duplicate/wrong query_id " + tl_objects[it.get_key()] = tl_fetch_error((kphp_runtime_context.static_SB.clean() << "No answer received or duplicate/wrong query_id " << query_id).str(), TL_ERROR_WRONG_QUERY_ID); } } else { diff --git a/runtime/rpc.h b/runtime/rpc.h index 7cf9844758..696ae41dc1 100644 --- a/runtime/rpc.h +++ b/runtime/rpc.h @@ -8,12 +8,12 @@ #include "common/algorithms/hashes.h" #include "common/kprintf.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" #include "runtime/net_events.h" #include "runtime/resumable.h" -#include "runtime/to-array-processor.h" #include "runtime/rpc_extra_info.h" +#include "runtime/to-array-processor.h" DECLARE_VERBOSITY(rpc); diff --git a/runtime/rpc_extra_info.h b/runtime/rpc_extra_info.h index db55ca3bad..7de8d95af8 100644 --- a/runtime/rpc_extra_info.h +++ b/runtime/rpc_extra_info.h @@ -7,12 +7,11 @@ #include #include -#include "runtime/kphp_core.h" -#include "runtime/dummy-visitor-methods.h" -#include "runtime/refcountable_php_classes.h" #include "common/algorithms/hashes.h" #include "common/wrappers/string_view.h" - +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" +#include "runtime/dummy-visitor-methods.h" using rpc_request_extra_info_t = std::tuple; // tuple(request_size) using rpc_response_extra_info_t = std::tuple; // tuple(response_size, response_time) diff --git a/runtime/runtime.cmake b/runtime/runtime.cmake index 62215b7066..0ca8ebacf1 100644 --- a/runtime/runtime.cmake +++ b/runtime/runtime.cmake @@ -6,14 +6,11 @@ prepend(KPHP_RUNTIME_DATETIME_SOURCES datetime/ datetime_zone.cpp timelib_wrapper.cpp) -prepend(KPHP_RUNTIME_MEMORY_RESOURCE_SOURCES memory_resource/ +prepend(KPHP_RUNTIME_MEMORY_IMPL_RESOURCE_SOURCES memory_resource_impl/ dealer.cpp - details/memory_chunk_tree.cpp - details/memory_ordered_chunk_list.cpp heap_resource.cpp - memory_resource.cpp - monotonic_buffer_resource.cpp - unsynchronized_pool_resource.cpp) + memory_resource_stats.cpp + monotonic_runtime_buffer_resource.cpp) prepend(KPHP_RUNTIME_MSGPACK_SOURCES msgpack/ check_instance_depth.cpp @@ -59,7 +56,7 @@ endif() prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/ ${KPHP_RUNTIME_DATETIME_SOURCES} - ${KPHP_RUNTIME_MEMORY_RESOURCE_SOURCES} + ${KPHP_RUNTIME_MEMORY_IMPL_RESOURCE_SOURCES} ${KPHP_RUNTIME_MSGPACK_SOURCES} ${KPHP_RUNTIME_JOB_WORKERS_SOURCES} ${KPHP_RUNTIME_SPL_SOURCES} @@ -68,6 +65,9 @@ prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/ ${KPHP_RUNTIME_PDO_MYSQL_SOURCES} ${KPHP_RUNTIME_PDO_PGSQL_SOURCES} allocator.cpp + context/runtime-core-allocator.cpp + context/runtime-core-context.cpp + context/runtime-context.cpp array_functions.cpp bcmath.cpp common_template_instantiations.cpp @@ -97,9 +97,7 @@ prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/ mbstring.cpp memcache.cpp memory_usage.cpp - migration_php8.cpp misc.cpp - mixed.cpp mysql.cpp net_events.cpp on_kphp_warning_callback.cpp @@ -115,9 +113,6 @@ prepend(KPHP_RUNTIME_SOURCES ${BASE_DIR}/runtime/ serialize-functions.cpp storage.cpp streams.cpp - string.cpp - string_buffer.cpp - string_cache.cpp string_functions.cpp tl/rpc_req_error.cpp tl/rpc_tl_query.cpp @@ -159,7 +154,7 @@ target_include_directories(kphp_runtime PUBLIC ${BASE_DIR} /opt/curl7600/include add_dependencies(kphp_runtime kphp-timelib) prepare_cross_platform_libs(RUNTIME_LIBS yaml-cpp re2 zstd h3) # todo: linking between static libs is no-op, is this redundant? do we need to add mysqlclient here? -set(RUNTIME_LIBS vk::kphp_runtime vk::kphp_server vk::popular_common vk::unicode vk::common_src vk::binlog_src vk::net_src ${RUNTIME_LIBS} OpenSSL::Crypto m z pthread) +set(RUNTIME_LIBS vk::kphp_runtime vk::kphp_server vk::runtime-core vk::popular_common vk::unicode vk::common_src vk::binlog_src vk::net_src ${RUNTIME_LIBS} OpenSSL::Crypto m z pthread) vk_add_library(kphp-full-runtime STATIC) target_link_libraries(kphp-full-runtime PUBLIC ${RUNTIME_LIBS}) set_target_properties(kphp-full-runtime PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${OBJS_DIR}) diff --git a/runtime/serialize-functions.cpp b/runtime/serialize-functions.cpp index 736a6c4580..ba7cadd15b 100644 --- a/runtime/serialize-functions.cpp +++ b/runtime/serialize-functions.cpp @@ -4,44 +4,46 @@ #include "runtime/serialize-functions.h" +#include "runtime/context/runtime-context.h" + void impl_::PhpSerializer::serialize(bool b) noexcept { - static_SB.reserve(4); - static_SB.append_char('b'); - static_SB.append_char(':'); - static_SB.append_char(static_cast(b + '0')); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.reserve(4); + kphp_runtime_context.static_SB.append_char('b'); + kphp_runtime_context.static_SB.append_char(':'); + kphp_runtime_context.static_SB.append_char(static_cast(b + '0')); + kphp_runtime_context.static_SB.append_char(';'); } void impl_::PhpSerializer::serialize(int64_t i) noexcept { - static_SB.reserve(24); - static_SB.append_char('i'); - static_SB.append_char(':'); - static_SB << i; - static_SB.append_char(';'); + kphp_runtime_context.static_SB.reserve(24); + kphp_runtime_context.static_SB.append_char('i'); + kphp_runtime_context.static_SB.append_char(':'); + kphp_runtime_context.static_SB << i; + kphp_runtime_context.static_SB.append_char(';'); } void impl_::PhpSerializer::serialize(double f) noexcept { - static_SB.append("d:", 2); - static_SB << f << ';'; + kphp_runtime_context.static_SB.append("d:", 2); + kphp_runtime_context.static_SB << f << ';'; } void impl_::PhpSerializer::serialize(const string &s) noexcept { string::size_type len = s.size(); - static_SB.reserve(25 + len); - static_SB.append_char('s'); - static_SB.append_char(':'); - static_SB << len; - static_SB.append_char(':'); - static_SB.append_char('"'); - static_SB.append_unsafe(s.c_str(), len); - static_SB.append_char('"'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.reserve(25 + len); + kphp_runtime_context.static_SB.append_char('s'); + kphp_runtime_context.static_SB.append_char(':'); + kphp_runtime_context.static_SB << len; + kphp_runtime_context.static_SB.append_char(':'); + kphp_runtime_context.static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_unsafe(s.c_str(), len); + kphp_runtime_context.static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_char(';'); } void impl_::PhpSerializer::serialize_null() noexcept { - static_SB.reserve(2); - static_SB.append_char('N'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.reserve(2); + kphp_runtime_context.static_SB.append_char('N'); + kphp_runtime_context.static_SB.append_char(';'); } void impl_::PhpSerializer::serialize(const mixed &v) noexcept { diff --git a/runtime/serialize-functions.h b/runtime/serialize-functions.h index c3bdd15f84..9ebb60a5ce 100644 --- a/runtime/serialize-functions.h +++ b/runtime/serialize-functions.h @@ -4,7 +4,9 @@ #pragma once -#include "runtime/kphp_core.h" +#include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" +#include "runtime/context/runtime-context.h" namespace impl_ { @@ -28,9 +30,9 @@ class PhpSerializer : vk::not_copyable { template void PhpSerializer::serialize(const array &arr) noexcept { - static_SB.append("a:", 2); - static_SB << arr.count(); - static_SB.append(":{", 2); + kphp_runtime_context.static_SB.append("a:", 2); + kphp_runtime_context.static_SB << arr.count(); + kphp_runtime_context.static_SB.append(":{", 2); for (auto p : arr) { auto key = p.get_key(); if (array::is_int_key(key)) { @@ -40,7 +42,7 @@ void PhpSerializer::serialize(const array &arr) noexcept { } serialize(p.get_value()); } - static_SB << '}'; + kphp_runtime_context.static_SB << '}'; } template @@ -59,9 +61,9 @@ void PhpSerializer::serialize(const Optional &opt) noexcept { template string f$serialize(const T &v) noexcept { - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); impl_::PhpSerializer::serialize(v); - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } mixed f$unserialize(const string &v) noexcept; diff --git a/runtime/spl/array_iterator.h b/runtime/spl/array_iterator.h index 76ad9aabf7..07d023a680 100644 --- a/runtime/spl/array_iterator.h +++ b/runtime/spl/array_iterator.h @@ -7,9 +7,9 @@ #include "common/algorithms/hashes.h" #include "common/wrappers/string_view.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" // C$ArrayIterator implements SPL ArrayIterator class. struct C$ArrayIterator : public refcountable_php_classes, private DummyVisitorMethods { diff --git a/runtime/storage.h b/runtime/storage.h index 870fa81d80..b778b8b75b 100644 --- a/runtime/storage.h +++ b/runtime/storage.h @@ -6,8 +6,8 @@ #include +#include "runtime-core/runtime-core.h" #include "runtime/exception.h" -#include "runtime/kphp_core.h" extern const char *last_wait_error; diff --git a/runtime/streams.cpp b/runtime/streams.cpp index 77f6901850..f6a5a2e7e5 100644 --- a/runtime/streams.cpp +++ b/runtime/streams.cpp @@ -9,6 +9,7 @@ #include #include "runtime/array_functions.h" +#include "runtime/allocator.h" #include "runtime/critical_section.h" constexpr int PHP_CSV_NO_ESCAPE = EOF; diff --git a/runtime/streams.h b/runtime/streams.h index b95321e5b5..1f1ccc6b6f 100644 --- a/runtime/streams.h +++ b/runtime/streams.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" using Stream =mixed; diff --git a/runtime/string-list.h b/runtime/string-list.h index 8adc349638..f7af13ce1a 100644 --- a/runtime/string-list.h +++ b/runtime/string-list.h @@ -4,8 +4,9 @@ #pragma once +#include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" -#include "runtime/kphp_core.h" class string_list : vk::not_copyable { public: diff --git a/runtime/string_buffer.cpp b/runtime/string_buffer.cpp deleted file mode 100644 index ff62489b97..0000000000 --- a/runtime/string_buffer.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Compiler for PHP (aka KPHP) -// Copyright (c) 2020 LLC «V Kontakte» -// Distributed under the GPL v3 License, see LICENSE.notice.txt - -#include "runtime/allocator.h" -#include "runtime/kphp_core.h" - -string_buffer static_SB; -string_buffer static_SB_spare; - -string::size_type string_buffer::MIN_BUFFER_LEN = 266175; -string::size_type string_buffer::MAX_BUFFER_LEN = (1 << 24); - -int string_buffer::string_buffer_error_flag = 0; - -string_buffer::string_buffer(string::size_type buffer_len) noexcept: - buffer_end(static_cast(dl::heap_allocate(buffer_len))), - buffer_begin(buffer_end), - buffer_len(buffer_len) { -} - -string_buffer::~string_buffer() noexcept { - dl::heap_deallocate(buffer_begin, buffer_len); -} diff --git a/runtime/string_functions.cpp b/runtime/string_functions.cpp index a467e36d29..d61931bec0 100644 --- a/runtime/string_functions.cpp +++ b/runtime/string_functions.cpp @@ -11,6 +11,7 @@ #include "common/macos-ports.h" #include "common/unicode/unicode-utils.h" +#include "runtime/context/runtime-context.h" #include "runtime/interface.h" const string COLON(",", 1); @@ -54,70 +55,70 @@ string f$addcslashes(const string &str, const string &what) { const char *mask = get_mask(what); int len = str.size(); - static_SB.clean().reserve(4 * len); + kphp_runtime_context.static_SB.clean().reserve(4 * len); for (int i = 0; i < len; i++) { unsigned char c = str[i]; if (mask[c]) { - static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('\\'); if (c < 32 || c > 126) { switch (c) { case '\n': - static_SB.append_char('n'); + kphp_runtime_context.static_SB.append_char('n'); break; case '\t': - static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char('t'); break; case '\r': - static_SB.append_char('r'); + kphp_runtime_context.static_SB.append_char('r'); break; case '\a': - static_SB.append_char('a'); + kphp_runtime_context.static_SB.append_char('a'); break; case '\v': - static_SB.append_char('v'); + kphp_runtime_context.static_SB.append_char('v'); break; case '\b': - static_SB.append_char('b'); + kphp_runtime_context.static_SB.append_char('b'); break; case '\f': - static_SB.append_char('f'); + kphp_runtime_context.static_SB.append_char('f'); break; default: - static_SB.append_char(static_cast((c >> 6) + '0')); - static_SB.append_char(static_cast(((c >> 3) & 7) + '0')); - static_SB.append_char(static_cast((c & 7) + '0')); + kphp_runtime_context.static_SB.append_char(static_cast((c >> 6) + '0')); + kphp_runtime_context.static_SB.append_char(static_cast(((c >> 3) & 7) + '0')); + kphp_runtime_context.static_SB.append_char(static_cast((c & 7) + '0')); } } else { - static_SB.append_char(c); + kphp_runtime_context.static_SB.append_char(c); } } else { - static_SB.append_char(c); + kphp_runtime_context.static_SB.append_char(c); } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$addslashes(const string &str) { int len = str.size(); - static_SB.clean().reserve(2 * len); + kphp_runtime_context.static_SB.clean().reserve(2 * len); for (int i = 0; i < len; i++) { switch (str[i]) { case '\0': - static_SB.append_char('\\'); - static_SB.append_char('0'); + kphp_runtime_context.static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('0'); break; case '\'': case '\"': case '\\': - static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('\\'); /* fallthrough */ default: - static_SB.append_char(str[i]); + kphp_runtime_context.static_SB.append_char(str[i]); } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$bin2hex(const string &str) { @@ -329,48 +330,48 @@ static const char *cp1251_to_utf8_str[128] = { string f$htmlentities(const string &str) { int len = (int)str.size(); - static_SB.clean().reserve(8 * len); + kphp_runtime_context.static_SB.clean().reserve(8 * len); for (int i = 0; i < len; i++) { switch (str[i]) { case '&': - static_SB.append_char('&'); - static_SB.append_char('a'); - static_SB.append_char('m'); - static_SB.append_char('p'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('a'); + kphp_runtime_context.static_SB.append_char('m'); + kphp_runtime_context.static_SB.append_char('p'); + kphp_runtime_context.static_SB.append_char(';'); break; case '"': - static_SB.append_char('&'); - static_SB.append_char('q'); - static_SB.append_char('u'); - static_SB.append_char('o'); - static_SB.append_char('t'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('q'); + kphp_runtime_context.static_SB.append_char('u'); + kphp_runtime_context.static_SB.append_char('o'); + kphp_runtime_context.static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char(';'); break; case '<': - static_SB.append_char('&'); - static_SB.append_char('l'); - static_SB.append_char('t'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('l'); + kphp_runtime_context.static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char(';'); break; case '>': - static_SB.append_char('&'); - static_SB.append_char('g'); - static_SB.append_char('t'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('g'); + kphp_runtime_context.static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char(';'); break; default: if (str[i] < 0) { const char *utf8_str = cp1251_to_utf8_str[128 + str[i]]; - static_SB.append_unsafe(utf8_str, static_cast(strlen(utf8_str))); + kphp_runtime_context.static_SB.append_unsafe(utf8_str, static_cast(strlen(utf8_str))); } else { - static_SB.append_char(str[i]); + kphp_runtime_context.static_SB.append_char(str[i]); } } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$html_entity_decode(const string &str, int64_t flags, const string &encoding) { @@ -465,59 +466,59 @@ string f$htmlspecialchars(const string &str, int64_t flags) { } const string::size_type len = str.size(); - static_SB.clean().reserve(6 * len); + kphp_runtime_context.static_SB.clean().reserve(6 * len); for (string::size_type i = 0; i < len; i++) { switch (str[i]) { case '&': - static_SB.append_char('&'); - static_SB.append_char('a'); - static_SB.append_char('m'); - static_SB.append_char('p'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('a'); + kphp_runtime_context.static_SB.append_char('m'); + kphp_runtime_context.static_SB.append_char('p'); + kphp_runtime_context.static_SB.append_char(';'); break; case '"': if (!(flags & ENT_NOQUOTES)) { - static_SB.append_char('&'); - static_SB.append_char('q'); - static_SB.append_char('u'); - static_SB.append_char('o'); - static_SB.append_char('t'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('q'); + kphp_runtime_context.static_SB.append_char('u'); + kphp_runtime_context.static_SB.append_char('o'); + kphp_runtime_context.static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char(';'); } else { - static_SB.append_char('"'); + kphp_runtime_context.static_SB.append_char('"'); } break; case '\'': if (flags & ENT_QUOTES) { - static_SB.append_char('&'); - static_SB.append_char('#'); - static_SB.append_char('0'); - static_SB.append_char('3'); - static_SB.append_char('9'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('#'); + kphp_runtime_context.static_SB.append_char('0'); + kphp_runtime_context.static_SB.append_char('3'); + kphp_runtime_context.static_SB.append_char('9'); + kphp_runtime_context.static_SB.append_char(';'); } else { - static_SB.append_char('\''); + kphp_runtime_context.static_SB.append_char('\''); } break; case '<': - static_SB.append_char('&'); - static_SB.append_char('l'); - static_SB.append_char('t'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('l'); + kphp_runtime_context.static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char(';'); break; case '>': - static_SB.append_char('&'); - static_SB.append_char('g'); - static_SB.append_char('t'); - static_SB.append_char(';'); + kphp_runtime_context.static_SB.append_char('&'); + kphp_runtime_context.static_SB.append_char('g'); + kphp_runtime_context.static_SB.append_char('t'); + kphp_runtime_context.static_SB.append_char(';'); break; default: - static_SB.append_char(str[i]); + kphp_runtime_context.static_SB.append_char(str[i]); } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$htmlspecialchars_decode(const string &str, int64_t flags) { @@ -630,7 +631,7 @@ string f$ltrim(const string &s, const string &what) { string f$mysql_escape_string(const string &str) { int len = str.size(); - static_SB.clean().reserve(2 * len); + kphp_runtime_context.static_SB.clean().reserve(2 * len); for (int i = 0; i < len; i++) { switch (str[i]) { case '\0': @@ -640,13 +641,13 @@ string f$mysql_escape_string(const string &str) { case '\'': case '\"': case '\\': - static_SB.append_char('\\'); + kphp_runtime_context.static_SB.append_char('\\'); /* fallthrough */ default: - static_SB.append_char(str[i]); + kphp_runtime_context.static_SB.append_char(str[i]); } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$nl2br(const string &str, bool is_xhtml) { @@ -654,20 +655,19 @@ string f$nl2br(const string &str, bool is_xhtml) { int br_len = (int)strlen(br); int len = str.size(); - - static_SB.clean().reserve((br_len + 1) * len); + kphp_runtime_context.static_SB.clean().reserve((br_len + 1) * len); for (int i = 0; i < len;) { if (str[i] == '\n' || str[i] == '\r') { - static_SB.append_unsafe(br, br_len); + kphp_runtime_context.static_SB.append_unsafe(br, br_len); if (str[i] + str[i + 1] == '\n' + '\r') { - static_SB.append_char(str[i++]); + kphp_runtime_context.static_SB.append_char(str[i++]); } } - static_SB.append_char(str[i++]); + kphp_runtime_context.static_SB.append_char(str[i++]); } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$number_format(double number, int64_t decimals, const string &dec_point, const string &thousands_sep) { @@ -760,7 +760,7 @@ static double float64_from_bits(uint64_t bits) { } string f$pack(const string &pattern, const array &a) { - static_SB.clean(); + kphp_runtime_context.static_SB.clean(); int cur_arg = 0; for (int i = 0; i < (int)pattern.size();) { if (pattern[i] == '*') { @@ -817,9 +817,9 @@ string f$pack(const string &pattern, const array &a) { cnt = len; i++; } - static_SB.append(arg_str.c_str(), static_cast(min(cnt, len))); + kphp_runtime_context.static_SB.append(arg_str.c_str(), static_cast(min(cnt, len))); while (cnt > len) { - static_SB << filler; + kphp_runtime_context.static_SB << filler; cnt--; } break; @@ -841,9 +841,9 @@ string f$pack(const string &pattern, const array &a) { return {}; } if (format == 'H') { - static_SB << (char)((num_high << 4) + num_low); + kphp_runtime_context.static_SB << (char)((num_high << 4) + num_low); } else { - static_SB << (char)((num_low << 4) + num_high); + kphp_runtime_context.static_SB << (char)((num_low << 4) + num_high); } } if (cnt > 0) { @@ -857,18 +857,18 @@ string f$pack(const string &pattern, const array &a) { switch (format) { case 'c': case 'C': - static_SB << (char)(arg.to_int()); + kphp_runtime_context.static_SB << (char)(arg.to_int()); break; case 's': case 'S': case 'v': { unsigned short value = (short)arg.to_int(); - static_SB.append((const char *)&value, 2); + kphp_runtime_context.static_SB.append((const char *)&value, 2); break; } case 'n': { unsigned short value = (short)arg.to_int(); - static_SB + kphp_runtime_context.static_SB << (char)(value >> 8) << (char)(value & 255); break; @@ -879,12 +879,12 @@ string f$pack(const string &pattern, const array &a) { case 'L': case 'V': { auto value = static_cast(arg.to_int()); - static_SB.append((const char *)&value, 4); + kphp_runtime_context.static_SB.append((const char *)&value, 4); break; } case 'N': { auto value = static_cast(arg.to_int()); - static_SB + kphp_runtime_context.static_SB << (char)(value >> 24) << (char)((value >> 16) & 255) << (char)((value >> 8) & 255) @@ -893,7 +893,7 @@ string f$pack(const string &pattern, const array &a) { } case 'f': { float value = (float)arg.to_float(); - static_SB.append((const char *)&value, sizeof(float)); + kphp_runtime_context.static_SB.append((const char *)&value, sizeof(float)); break; } case 'e': @@ -906,7 +906,7 @@ string f$pack(const string &pattern, const array &a) { } else if (format == 'E') { value_byteordered = htobe64(value_byteordered); } - static_SB.append((const char *)&value_byteordered, sizeof(uint64_t)); + kphp_runtime_context.static_SB.append((const char *)&value_byteordered, sizeof(uint64_t)); break; } case 'J': @@ -922,12 +922,12 @@ string f$pack(const string &pattern, const array &a) { value_byteordered = htobe64(value_byteordered); } - static_SB.append((const char *)&value_byteordered, sizeof(unsigned long long)); + kphp_runtime_context.static_SB.append((const char *)&value_byteordered, sizeof(unsigned long long)); break; } case 'q': { int64_t value = arg.to_string().to_int(); - static_SB.append((const char *)&value, sizeof(long long)); + kphp_runtime_context.static_SB.append((const char *)&value, sizeof(long long)); break; } default: @@ -958,7 +958,7 @@ string f$pack(const string &pattern, const array &a) { php_warning("Too much arguments to call pack with format \"%s\"", pattern.c_str()); } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$prepare_search_query(const string &query) { @@ -1114,7 +1114,7 @@ string f$sprintf(const string &format, const array &a) { case 'd': { int64_t arg_int = arg.to_int(); if (sign == '+' && arg_int >= 0) { - piece = (static_SB.clean() << "+" << arg_int).str(); + piece = (kphp_runtime_context.static_SB.clean() << "+" << arg_int).str(); } else { piece = string(arg_int); } @@ -1138,16 +1138,16 @@ string f$sprintf(const string &format, const array &a) { case 'G': { double arg_float = arg.to_float(); - static_SB.clean() << '%'; + kphp_runtime_context.static_SB.clean() << '%'; if (sign) { - static_SB << sign; + kphp_runtime_context.static_SB << sign; } if (precision >= 0) { - static_SB << '.' << precision; + kphp_runtime_context.static_SB << '.' << precision; } - static_SB << format[i]; + kphp_runtime_context.static_SB << format[i]; - int len = snprintf(php_buf, PHP_BUF_LEN, static_SB.c_str(), arg_float); + int len = snprintf(php_buf, PHP_BUF_LEN, kphp_runtime_context.static_SB.c_str(), arg_float); if (len >= PHP_BUF_LEN) { error_too_big = true; break; @@ -1169,13 +1169,13 @@ string f$sprintf(const string &format, const array &a) { case 's': { string arg_string = arg.to_string(); - static_SB.clean() << '%'; + kphp_runtime_context.static_SB.clean() << '%'; if (precision >= 0) { - static_SB << '.' << precision; + kphp_runtime_context.static_SB << '.' << precision; } - static_SB << 's'; + kphp_runtime_context.static_SB << 's'; - int len = snprintf(php_buf, PHP_BUF_LEN, static_SB.c_str(), arg_string.c_str()); + int len = snprintf(php_buf, PHP_BUF_LEN, kphp_runtime_context.static_SB.c_str(), arg_string.c_str()); if (len >= PHP_BUF_LEN) { error_too_big = true; break; @@ -1415,9 +1415,8 @@ string f$strip_tags(const string &str, const string &allow) { int state = 0; const string allow_low = f$strtolower(allow); - - static_SB.clean(); - static_SB_spare.clean(); + kphp_runtime_context.static_SB.clean(); + kphp_runtime_context.static_SB_spare.clean(); char lc = 0; int len = str.size(); for (int i = 0; i < len; i++) { @@ -1429,14 +1428,14 @@ string f$strip_tags(const string &str, const string &allow) { if (!in_q) { if (isspace(str[i + 1])) { if (state == 0) { - static_SB << c; + kphp_runtime_context.static_SB << c; } else if (state == 1) { - static_SB_spare << c; + kphp_runtime_context.static_SB_spare << c; } } else if (state == 0) { lc = '<'; state = 1; - static_SB_spare << '<'; + kphp_runtime_context.static_SB_spare << '<'; } else if (state == 1) { depth++; } @@ -1449,9 +1448,9 @@ string f$strip_tags(const string &str, const string &allow) { br++; } } else if (state == 1) { - static_SB_spare << c; + kphp_runtime_context.static_SB_spare << c; } else if (state == 0) { - static_SB << c; + kphp_runtime_context.static_SB << c; } break; case ')': @@ -1461,9 +1460,9 @@ string f$strip_tags(const string &str, const string &allow) { br--; } } else if (state == 1) { - static_SB_spare << c; + kphp_runtime_context.static_SB_spare << c; } else if (state == 0) { - static_SB << c; + kphp_runtime_context.static_SB << c; } break; case '>': @@ -1480,30 +1479,30 @@ string f$strip_tags(const string &str, const string &allow) { case 1: /* HTML/XML */ lc = '>'; in_q = state = 0; - static_SB_spare << '>'; - if (php_tag_find(static_SB_spare.str(), allow_low)) { - static_SB << static_SB_spare.c_str(); + kphp_runtime_context.static_SB_spare << '>'; + if (php_tag_find(kphp_runtime_context.static_SB_spare.str(), allow_low)) { + kphp_runtime_context.static_SB << kphp_runtime_context.static_SB_spare.c_str(); } - static_SB_spare.clean(); + kphp_runtime_context.static_SB_spare.clean(); break; case 2: /* PHP */ if (!br && lc != '\"' && str[i - 1] == '?') { in_q = state = 0; - static_SB_spare.clean(); + kphp_runtime_context.static_SB_spare.clean(); } break; case 3: in_q = state = 0; - static_SB_spare.clean(); + kphp_runtime_context.static_SB_spare.clean(); break; case 4: /* JavaScript/CSS/etc... */ if (i >= 2 && str[i - 1] == '-' && str[i - 2] == '-') { in_q = state = 0; - static_SB_spare.clean(); + kphp_runtime_context.static_SB_spare.clean(); } break; default: - static_SB << c; + kphp_runtime_context.static_SB << c; break; } break; @@ -1520,9 +1519,9 @@ string f$strip_tags(const string &str, const string &allow) { lc = c; } } else if (state == 0) { - static_SB << c; + kphp_runtime_context.static_SB << c; } else if (state == 1) { - static_SB_spare << c; + kphp_runtime_context.static_SB_spare << c; } if (state && i > 0 && (state == 1 || str[i - 1] != '\\') && (!in_q || c == in_q)) { if (in_q) { @@ -1539,9 +1538,9 @@ string f$strip_tags(const string &str, const string &allow) { lc = c; } else { if (state == 0) { - static_SB << c; + kphp_runtime_context.static_SB << c; } else if (state == 1) { - static_SB_spare << c; + kphp_runtime_context.static_SB_spare << c; } } break; @@ -1550,9 +1549,9 @@ string f$strip_tags(const string &str, const string &allow) { state = 4; } else { if (state == 0) { - static_SB << c; + kphp_runtime_context.static_SB << c; } else if (state == 1) { - static_SB_spare << c; + kphp_runtime_context.static_SB_spare << c; } } break; @@ -1591,15 +1590,15 @@ string f$strip_tags(const string &str, const string &allow) { /* fall-through */ default: if (state == 0) { - static_SB << c; + kphp_runtime_context.static_SB << c; } else if (state == 1) { - static_SB_spare << c; + kphp_runtime_context.static_SB_spare << c; } break; } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } template diff --git a/runtime/string_functions.h b/runtime/string_functions.h index a7c159f4ca..a0f13df801 100644 --- a/runtime/string_functions.h +++ b/runtime/string_functions.h @@ -4,8 +4,8 @@ #pragma once +#include "runtime-core/runtime-core.h" #include -#include "runtime/kphp_core.h" extern const string COLON; extern const string CP1251; diff --git a/runtime/tcp.cpp b/runtime/tcp.cpp index aa31a24c79..0176df3650 100644 --- a/runtime/tcp.cpp +++ b/runtime/tcp.cpp @@ -7,6 +7,7 @@ #include #include +#include "runtime/allocator.h" #include "runtime/critical_section.h" #include "runtime/datetime/datetime_functions.h" #include "runtime/net_events.h" diff --git a/runtime/tcp.h b/runtime/tcp.h index 6579831e7e..1772ca7a7b 100644 --- a/runtime/tcp.h +++ b/runtime/tcp.h @@ -1,6 +1,6 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" void global_init_tcp_lib(); diff --git a/runtime/tl/rpc_function.h b/runtime/tl/rpc_function.h index 65abbd832d..a2df8b19e8 100644 --- a/runtime/tl/rpc_function.h +++ b/runtime/tl/rpc_function.h @@ -4,9 +4,11 @@ #pragma once -#include "runtime/refcountable_php_classes.h" +#include + #include "common/algorithms/hashes.h" #include "common/wrappers/string_view.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" struct tl_func_base; diff --git a/runtime/tl/rpc_req_error.h b/runtime/tl/rpc_req_error.h index 4a65290e2b..e41918a864 100644 --- a/runtime/tl/rpc_req_error.h +++ b/runtime/tl/rpc_req_error.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" struct RpcError { int error_code = 0; diff --git a/runtime/tl/rpc_response.h b/runtime/tl/rpc_response.h index 4b1ae316e4..c3fb435e4c 100644 --- a/runtime/tl/rpc_response.h +++ b/runtime/tl/rpc_response.h @@ -4,8 +4,8 @@ #pragma once -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/tl/rpc_function.h" class RpcErrorFactory { diff --git a/runtime/tl/rpc_tl_query.h b/runtime/tl/rpc_tl_query.h index 47c4485dbd..39d3b5fa7f 100644 --- a/runtime/tl/rpc_tl_query.h +++ b/runtime/tl/rpc_tl_query.h @@ -3,8 +3,11 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #pragma once -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" + +#include + +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" class RpcRequestResult; diff --git a/runtime/tl/tl_builtins.h b/runtime/tl/tl_builtins.h index 115153a08a..d134c58213 100644 --- a/runtime/tl/tl_builtins.h +++ b/runtime/tl/tl_builtins.h @@ -8,7 +8,7 @@ #include "common/tl/constants/common.h" -#include "runtime/include.h" +#include "runtime-core/include.h" #include "runtime/interface.h" #include "runtime/rpc.h" #include "runtime/tl/rpc_function.h" diff --git a/runtime/tl/tl_func_base.h b/runtime/tl/tl_func_base.h index 2dffce59bb..04b73612a8 100644 --- a/runtime/tl/tl_func_base.h +++ b/runtime/tl/tl_func_base.h @@ -3,8 +3,9 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/tl/rpc_function.h" +#include "runtime/allocator.h" struct tl_func_base : ManagedThroughDlAllocator { virtual mixed fetch() = 0; diff --git a/runtime/tl/tl_magics_decoding.h b/runtime/tl/tl_magics_decoding.h index 168a4e215c..dfdcd1073e 100644 --- a/runtime/tl/tl_magics_decoding.h +++ b/runtime/tl/tl_magics_decoding.h @@ -6,7 +6,7 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" const char *tl_magic_convert_to_name(uint32_t magic) noexcept; array tl_magic_get_all_functions() noexcept; diff --git a/runtime/to-array-processor.h b/runtime/to-array-processor.h index 9b6cf77555..19ac809395 100644 --- a/runtime/to-array-processor.h +++ b/runtime/to-array-processor.h @@ -8,7 +8,7 @@ #include "common/mixin/not_copyable.h" #include "common/smart_ptrs/singleton.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" class ShapeKeyDemangle : vk::not_copyable { public: diff --git a/runtime/to-json-processor.h b/runtime/to-json-processor.h index d576fef63a..aa3856362f 100644 --- a/runtime/to-json-processor.h +++ b/runtime/to-json-processor.h @@ -4,9 +4,9 @@ #pragma once -#include "runtime/kphp_core.h" -#include "runtime/json-writer.h" +#include "runtime-core/runtime-core.h" #include "runtime/json-processor-utils.h" +#include "runtime/json-writer.h" template class ToJsonVisitor { diff --git a/runtime/typed_rpc.h b/runtime/typed_rpc.h index 6b57fdf5a1..88d5beaa59 100644 --- a/runtime/typed_rpc.h +++ b/runtime/typed_rpc.h @@ -3,12 +3,12 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/rpc_extra_info.h" #include "runtime/tl/rpc_function.h" -#include "runtime/tl/rpc_tl_query.h" #include "runtime/tl/rpc_request.h" #include "runtime/tl/rpc_response.h" +#include "runtime/tl/rpc_tl_query.h" struct C$RpcConnection; diff --git a/runtime/uber-h3.cpp b/runtime/uber-h3.cpp index d39194af52..b5a32f8284 100644 --- a/runtime/uber-h3.cpp +++ b/runtime/uber-h3.cpp @@ -6,6 +6,8 @@ #include

+#include "runtime/allocator.h" + namespace { inline std::tuple coord2deg(GeoCoord geo_coord) noexcept { diff --git a/runtime/uber-h3.h b/runtime/uber-h3.h index fd67bedf91..80145ace52 100644 --- a/runtime/uber-h3.h +++ b/runtime/uber-h3.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" int64_t f$UberH3$$geoToH3(double latitude, double longitude, int64_t resolution) noexcept; std::tuple f$UberH3$$h3ToGeo(int64_t h3_index) noexcept; diff --git a/runtime/udp.cpp b/runtime/udp.cpp index d00524b3cb..ede9928675 100644 --- a/runtime/udp.cpp +++ b/runtime/udp.cpp @@ -10,6 +10,7 @@ #include "common/resolver.h" +#include "runtime/allocator.h" #include "runtime/critical_section.h" #include "runtime/datetime/datetime_functions.h" #include "runtime/net_events.h" diff --git a/runtime/udp.h b/runtime/udp.h index 11d195d4ac..f64087b1b2 100644 --- a/runtime/udp.h +++ b/runtime/udp.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" void global_init_udp_lib(); diff --git a/runtime/url.cpp b/runtime/url.cpp index 89c869354d..2ed7e6fc5d 100644 --- a/runtime/url.cpp +++ b/runtime/url.cpp @@ -454,22 +454,22 @@ mixed f$parse_url(const string &s, int64_t component) { } string f$rawurldecode(const string &s) { - static_SB.clean().reserve(s.size()); + kphp_runtime_context.static_SB.clean().reserve(s.size()); for (int i = 0; i < (int)s.size(); i++) { if (s[i] == '%') { int num_high = hex_to_int(s[i + 1]); if (num_high < 16) { int num_low = hex_to_int(s[i + 2]); if (num_low < 16) { - static_SB.append_char(static_cast((num_high << 4) + num_low)); + kphp_runtime_context.static_SB.append_char(static_cast((num_high << 4) + num_low)); i += 2; continue; } } } - static_SB.append_char(s[i]); + kphp_runtime_context.static_SB.append_char(s[i]); } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } @@ -484,53 +484,53 @@ static const char *good_url_symbols = "00000000000000000000000000000000";//[0-9a-zA-Z-_.] string f$rawurlencode(const string &s) { - static_SB.clean().reserve(3 * s.size()); + kphp_runtime_context.static_SB.clean().reserve(3 * s.size()); for (int i = 0; i < (int)s.size(); i++) { if (good_url_symbols[(unsigned char)s[i]] == '1') { - static_SB.append_char(s[i]); + kphp_runtime_context.static_SB.append_char(s[i]); } else { - static_SB.append_char('%'); - static_SB.append_char(uhex_digits[(s[i] >> 4) & 15]); - static_SB.append_char(uhex_digits[s[i] & 15]); + kphp_runtime_context.static_SB.append_char('%'); + kphp_runtime_context.static_SB.append_char(uhex_digits[(s[i] >> 4) & 15]); + kphp_runtime_context.static_SB.append_char(uhex_digits[s[i] & 15]); } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$urldecode(const string &s) { - static_SB.clean().reserve(s.size()); + kphp_runtime_context.static_SB.clean().reserve(s.size()); for (int i = 0; i < (int)s.size(); i++) { if (s[i] == '%') { int num_high = hex_to_int(s[i + 1]); if (num_high < 16) { int num_low = hex_to_int(s[i + 2]); if (num_low < 16) { - static_SB.append_char(static_cast((num_high << 4) + num_low)); + kphp_runtime_context.static_SB.append_char(static_cast((num_high << 4) + num_low)); i += 2; continue; } } } else if (s[i] == '+') { - static_SB.append_char(' '); + kphp_runtime_context.static_SB.append_char(' '); continue; } - static_SB.append_char(s[i]); + kphp_runtime_context.static_SB.append_char(s[i]); } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } string f$urlencode(const string &s) { - static_SB.clean().reserve(3 * s.size()); + kphp_runtime_context.static_SB.clean().reserve(3 * s.size()); for (int i = 0; i < (int)s.size(); i++) { if (good_url_symbols[(unsigned char)s[i]] == '1') { - static_SB.append_char(s[i]); + kphp_runtime_context.static_SB.append_char(s[i]); } else if (s[i] == ' ') { - static_SB.append_char('+'); + kphp_runtime_context.static_SB.append_char('+'); } else { - static_SB.append_char('%'); - static_SB.append_char(uhex_digits[(s[i] >> 4) & 15]); - static_SB.append_char(uhex_digits[s[i] & 15]); + kphp_runtime_context.static_SB.append_char('%'); + kphp_runtime_context.static_SB.append_char(uhex_digits[(s[i] >> 4) & 15]); + kphp_runtime_context.static_SB.append_char(uhex_digits[s[i] & 15]); } } - return static_SB.str(); + return kphp_runtime_context.static_SB.str(); } diff --git a/runtime/url.h b/runtime/url.h index 4d1ab1dff9..bd4232aaed 100644 --- a/runtime/url.h +++ b/runtime/url.h @@ -4,7 +4,9 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" + +#include "runtime/context/runtime-context.h" Optional f$base64_decode(const string &s, bool strict = false); @@ -44,7 +46,7 @@ string http_build_query_get_param_array(const string &key, const array &a, string result; bool first = true; for (typename array::const_iterator p = a.begin(); p != a.end(); ++p) { - const string &key_value_param = http_build_query_get_param((static_SB.clean() << key << '[' << p.get_key() << ']').str(), p.get_value(), arg_separator, enc_type); + const string &key_value_param = http_build_query_get_param((kphp_runtime_context.static_SB.clean() << key << '[' << p.get_key() << ']').str(), p.get_value(), arg_separator, enc_type); if (!key_value_param.empty()) { if (!first) { result.append(arg_separator); @@ -75,7 +77,7 @@ string http_build_query_get_param(const string &key, const T &a, }; string key_encoded = encode(key); string value_encoded = encode(f$strval(a)); - return (static_SB.clean() << key_encoded << '=' << value_encoded).str(); + return (kphp_runtime_context.static_SB.clean() << key_encoded << '=' << value_encoded).str(); } } diff --git a/runtime/vkext.cpp b/runtime/vkext.cpp index e9b41fb0b8..5ed5feac53 100644 --- a/runtime/vkext.cpp +++ b/runtime/vkext.cpp @@ -10,6 +10,7 @@ #include "flex/flex.h" #include "runtime/misc.h" +#include "runtime/allocator.h" static int utf8_to_win_convert_0x400[256] = {-1, 0xa8, 0x80, 0x81, 0xaa, 0xbd, 0xb2, 0xaf, 0xa3, 0x8a, 0x8c, 0x8e, 0x8d, -1, 0xa1, 0x8f, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, diff --git a/runtime/vkext.h b/runtime/vkext.h index b4c1f779a5..b41e87d71f 100644 --- a/runtime/vkext.h +++ b/runtime/vkext.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" string f$vk_utf8_to_win(const string &text, int64_t max_len = 0, bool exit_on_error = false); diff --git a/runtime/vkext_stats.h b/runtime/vkext_stats.h index 135912d01b..03c993305b 100644 --- a/runtime/vkext_stats.h +++ b/runtime/vkext_stats.h @@ -4,8 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" - +#include "runtime-core/runtime-core.h" Optional f$vk_stats_hll_merge(const array &a); Optional f$vk_stats_hll_count(const string &hll); diff --git a/runtime/zlib.cpp b/runtime/zlib.cpp index a621a9a718..3d9d48c234 100644 --- a/runtime/zlib.cpp +++ b/runtime/zlib.cpp @@ -4,6 +4,7 @@ #include "runtime/zlib.h" +#include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/string_functions.h" @@ -37,7 +38,7 @@ const string_buffer *zlib_encode(const char *s, int32_t s_len, int32_t level, in strm.opaque = &buf_pos; unsigned int res_len = (unsigned int)compressBound(s_len) + 30; - static_SB.clean().reserve(res_len); + kphp_runtime_context.static_SB.clean().reserve(res_len); dl::enter_critical_section();//OK int ret = deflateInit2 (&strm, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); @@ -45,7 +46,7 @@ const string_buffer *zlib_encode(const char *s, int32_t s_len, int32_t level, in strm.avail_in = (unsigned int)s_len; strm.next_in = reinterpret_cast (const_cast (s)); strm.avail_out = res_len; - strm.next_out = reinterpret_cast (static_SB.buffer()); + strm.next_out = reinterpret_cast (kphp_runtime_context.static_SB.buffer()); ret = deflate(&strm, Z_FINISH); deflateEnd(&strm); @@ -53,16 +54,16 @@ const string_buffer *zlib_encode(const char *s, int32_t s_len, int32_t level, in if (ret == Z_STREAM_END) { dl::leave_critical_section(); - static_SB.set_pos(static_cast(strm.total_out)); - return &static_SB; + kphp_runtime_context.static_SB.set_pos(static_cast(strm.total_out)); + return &kphp_runtime_context.static_SB; } } dl::leave_critical_section(); php_warning("Error during pack of string with length %d", s_len); - static_SB.clean(); - return &static_SB; + kphp_runtime_context.static_SB.clean(); + return &kphp_runtime_context.static_SB; } class_instance f$deflate_init(int64_t encoding, const array &options) { diff --git a/runtime/zlib.h b/runtime/zlib.h index ac6a05f7be..7da2742314 100644 --- a/runtime/zlib.h +++ b/runtime/zlib.h @@ -8,8 +8,8 @@ #include "common/wrappers/string_view.h" -#include "runtime/kphp_core.h" -#include "runtime/refcountable_php_classes.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" #include "runtime/dummy-visitor-methods.h" constexpr int64_t ZLIB_ENCODING_RAW = -0x0f; diff --git a/runtime/zstd.cpp b/runtime/zstd.cpp index b3a52a5b2f..683265411d 100644 --- a/runtime/zstd.cpp +++ b/runtime/zstd.cpp @@ -9,7 +9,7 @@ #include "common/smart_ptrs/unique_ptr_with_delete_function.h" #include "runtime/string_functions.h" - +#include "runtime/allocator.h" #include "runtime/zstd.h" namespace { diff --git a/runtime/zstd.h b/runtime/zstd.h index 3e1ada4935..752eab7410 100644 --- a/runtime/zstd.h +++ b/runtime/zstd.h @@ -4,8 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" -#include "runtime/optional.h" +#include "runtime-core/runtime-core.h" constexpr int DEFAULT_COMPRESS_LEVEL = 3; diff --git a/server/confdata-binlog-events.h b/server/confdata-binlog-events.h index bfbe8c2dc2..8aef180608 100644 --- a/server/confdata-binlog-events.h +++ b/server/confdata-binlog-events.h @@ -7,7 +7,7 @@ #include "common/type_traits/list_of_types.h" #include "server/pmemcached-binlog-interface.h" -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/memcache.h" namespace impl_ { diff --git a/server/confdata-binlog-replay.cpp b/server/confdata-binlog-replay.cpp index 7f4c6b58ce..8ea8e8f396 100644 --- a/server/confdata-binlog-replay.cpp +++ b/server/confdata-binlog-replay.cpp @@ -26,6 +26,7 @@ #include "common/tl/methods/string.h" #include "common/wrappers/string_view.h" #include "runtime/confdata-global-manager.h" +#include "runtime-core/runtime-core.h" #include "server/confdata-binlog-events.h" #include "server/confdata-stats.h" #include "server/server-log.h" diff --git a/server/confdata-stats.cpp b/server/confdata-stats.cpp index c1c4c7d360..8f9b4f335b 100644 --- a/server/confdata-stats.cpp +++ b/server/confdata-stats.cpp @@ -5,6 +5,7 @@ #include "server/confdata-stats.h" #include "common/algorithms/contains.h" +#include "runtime/memory_resource_impl/memory_resource_stats.h" namespace { @@ -84,7 +85,7 @@ const memory_resource::MemoryStats &ConfdataStats::get_memory_stats() const noex void ConfdataStats::write_stats_to(stats_t *stats) const noexcept { const auto &memory_stats = get_memory_stats(); - memory_stats.write_stats_to(stats, "confdata"); + write_memory_stats_to(memory_stats, stats, "confdata"); stats->add_gauge_stat("confdata.initial_loading_duration", to_seconds(initial_loading_time)); stats->add_gauge_stat("confdata.total_updating_time", to_seconds(total_updating_time)); diff --git a/server/database-drivers/adaptor.h b/server/database-drivers/adaptor.h index fd85754481..3dafb82e79 100644 --- a/server/database-drivers/adaptor.h +++ b/server/database-drivers/adaptor.h @@ -7,12 +7,12 @@ #include #include -#include "common/smart_ptrs/singleton.h" #include "common/mixin/not_copyable.h" +#include "common/smart_ptrs/singleton.h" +#include "runtime-core/runtime-core.h" #include "net/net-events.h" #include "runtime/critical_section.h" #include "runtime/signal_safe_hashtable.h" -#include "runtime/kphp_core.h" #include "server/database-drivers/connector.h" #include "server/database-drivers/request.h" diff --git a/server/database-drivers/mysql/mysql-connector.h b/server/database-drivers/mysql/mysql-connector.h index ec0366be24..47f82a9bf8 100644 --- a/server/database-drivers/mysql/mysql-connector.h +++ b/server/database-drivers/mysql/mysql-connector.h @@ -7,7 +7,7 @@ #include #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "server/database-drivers/connector.h" namespace database_drivers { diff --git a/server/database-drivers/mysql/mysql-request.h b/server/database-drivers/mysql/mysql-request.h index f27cde1ba7..deb12a240b 100644 --- a/server/database-drivers/mysql/mysql-request.h +++ b/server/database-drivers/mysql/mysql-request.h @@ -4,7 +4,7 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "server/database-drivers/request.h" namespace database_drivers { diff --git a/server/database-drivers/mysql/mysql.h b/server/database-drivers/mysql/mysql.h index c54907ba73..318603d22e 100644 --- a/server/database-drivers/mysql/mysql.h +++ b/server/database-drivers/mysql/mysql.h @@ -5,9 +5,9 @@ #pragma once #include "common/kprintf.h" +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" #include "runtime/critical_section.h" -#include "runtime/kphp_core.h" DECLARE_VERBOSITY(mysql); diff --git a/server/database-drivers/pgsql/pgsql-connector.h b/server/database-drivers/pgsql/pgsql-connector.h index 1adf1910a8..9d8f288a08 100644 --- a/server/database-drivers/pgsql/pgsql-connector.h +++ b/server/database-drivers/pgsql/pgsql-connector.h @@ -3,12 +3,11 @@ #include #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "server/database-drivers/connector.h" #include "server/database-drivers/pgsql/pgsql.h" #include "server/database-drivers/pgsql/pgsql-storage.h" - namespace database_drivers { class Request; diff --git a/server/database-drivers/pgsql/pgsql-request.h b/server/database-drivers/pgsql/pgsql-request.h index f7dcc3d6e2..547e1384a2 100644 --- a/server/database-drivers/pgsql/pgsql-request.h +++ b/server/database-drivers/pgsql/pgsql-request.h @@ -1,6 +1,6 @@ #pragma once -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "server/database-drivers/request.h" namespace database_drivers { diff --git a/server/database-drivers/pgsql/pgsql.h b/server/database-drivers/pgsql/pgsql.h index 7e158ff46c..fae78aecfa 100644 --- a/server/database-drivers/pgsql/pgsql.h +++ b/server/database-drivers/pgsql/pgsql.h @@ -1,9 +1,9 @@ #pragma once #include "common/kprintf.h" +#include "runtime-core/runtime-core.h" #include "runtime/allocator.h" #include "runtime/critical_section.h" -#include "runtime/kphp_core.h" DECLARE_VERBOSITY(pgsql); diff --git a/server/job-workers/job-message.h b/server/job-workers/job-message.h index fa6af2c11b..8c60491579 100644 --- a/server/job-workers/job-message.h +++ b/server/job-workers/job-message.h @@ -6,9 +6,9 @@ #include "common/mixin/not_copyable.h" +#include "runtime-core/runtime-core.h" +#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" #include "runtime/job-workers/job-interface.h" -#include "runtime/memory_resource/unsynchronized_pool_resource.h" -#include "runtime/kphp_core.h" namespace job_workers { diff --git a/server/job-workers/job-stats.cpp b/server/job-workers/job-stats.cpp index 45885faa7a..cc777bf38e 100644 --- a/server/job-workers/job-stats.cpp +++ b/server/job-workers/job-stats.cpp @@ -5,7 +5,7 @@ #include #include -#include "runtime/memory_resource/extra-memory-pool.h" +#include "runtime-core/memory-resource/extra-memory-pool.h" #include "server/job-workers/job-stats.h" diff --git a/server/job-workers/shared-memory-manager.h b/server/job-workers/shared-memory-manager.h index 03603be804..3d80564baf 100644 --- a/server/job-workers/shared-memory-manager.h +++ b/server/job-workers/shared-memory-manager.h @@ -8,8 +8,8 @@ #include "common/mixin/not_copyable.h" #include "common/smart_ptrs/singleton.h" +#include "runtime-core/memory-resource/extra-memory-pool.h" #include "runtime/critical_section.h" -#include "runtime/memory_resource/extra-memory-pool.h" #include "server/job-workers/job-stats.h" #include "server/job-workers/job-workers-context.h" #include "server/php-engine-vars.h" diff --git a/server/php-master.cpp b/server/php-master.cpp index a7434a409c..80d5d851c0 100644 --- a/server/php-master.cpp +++ b/server/php-master.cpp @@ -50,6 +50,7 @@ #include "net/net-tcp-rpc-client.h" #include "net/net-tcp-rpc-server.h" +#include "runtime/memory_resource_impl/memory_resource_stats.h" #include "runtime/confdata-global-manager.h" #include "runtime/instance-cache.h" #include "runtime/thread-pool.h" @@ -1148,7 +1149,7 @@ STATS_PROVIDER_TAGGED(kphp_stats, 100, stats_tag_kphp_server) { stats->add_gauge_stat("server.total_json_logs_count", std::get<0>(total_workers_json_count) + master_json_logs_count); stats->add_gauge_stat("server.total_json_traces_count", std::get<1>(total_workers_json_count)); - instance_cache_get_memory_stats().write_stats_to(stats, "instance_cache"); + write_memory_stats_to(instance_cache_get_memory_stats(), stats, "instance_cache"); stats->add_gauge_stat("instance_cache.memory.buffer_swaps_ok", instance_cache_memory_swaps_ok); stats->add_gauge_stat("instance_cache.memory.buffer_swaps_fail", instance_cache_memory_swaps_fail); diff --git a/server/php-runner.cpp b/server/php-runner.cpp index 24276fd132..a6d82ede40 100644 --- a/server/php-runner.cpp +++ b/server/php-runner.cpp @@ -29,6 +29,7 @@ #include "runtime/kphp_tracing.h" #include "runtime/oom_handler.h" #include "runtime/profiler.h" +#include "runtime/php_assert.h" #include "server/json-logger.h" #include "server/php-engine-vars.h" #include "server/php-queries.h" diff --git a/server/server-stats.h b/server/server-stats.h index b637f6b5b5..81a016c15e 100644 --- a/server/server-stats.h +++ b/server/server-stats.h @@ -12,7 +12,7 @@ #include "common/smart_ptrs/singleton.h" #include "common/stats/provider.h" -#include "runtime/memory_resource/memory_resource.h" +#include "runtime-core/memory-resource/memory_resource.h" #include "server/php-runner.h" #include "server/workers-control.h" diff --git a/server/signal-handlers.cpp b/server/signal-handlers.cpp index 701cd567ca..79a07fdd80 100644 --- a/server/signal-handlers.cpp +++ b/server/signal-handlers.cpp @@ -12,6 +12,7 @@ #include "common/server/signals.h" #include "runtime/critical_section.h" #include "runtime/interface.h" +#include "runtime/php_assert.h" #include "server/json-logger.h" #include "server/php-engine-vars.h" #include "server/server-log.h" diff --git a/server/statshouse/statshouse-manager.h b/server/statshouse/statshouse-manager.h index f848f631e3..141de2e885 100644 --- a/server/statshouse/statshouse-manager.h +++ b/server/statshouse/statshouse-manager.h @@ -9,7 +9,7 @@ #include "common/dl-utils-lite.h" #include "common/mixin/not_copyable.h" -#include "runtime/memory_resource/memory_resource.h" +#include "runtime-core/memory-resource/memory_resource.h" #include "server/job-workers/job-stats.h" #include "server/statshouse/statshouse-client.h" #include "server/workers-control.h" diff --git a/tests/cpp/runtime/_runtime-tests-env.cpp b/tests/cpp/runtime/_runtime-tests-env.cpp index 2bfbc6d37c..ee911ea7b3 100644 --- a/tests/cpp/runtime/_runtime-tests-env.cpp +++ b/tests/cpp/runtime/_runtime-tests-env.cpp @@ -7,6 +7,7 @@ #include "runtime/interface.h" #include "runtime/job-workers/job-interface.h" #include "runtime/pdo/pdo_statement.h" +#include "runtime/php_assert.h" #include "runtime/tl/rpc_response.h" #include "server/php-engine-vars.h" #include "server/workers-control.h" diff --git a/tests/cpp/runtime/array-int-string-keys-collision-test.cpp b/tests/cpp/runtime/array-int-string-keys-collision-test.cpp index 09004e5baa..f0085a9f71 100644 --- a/tests/cpp/runtime/array-int-string-keys-collision-test.cpp +++ b/tests/cpp/runtime/array-int-string-keys-collision-test.cpp @@ -1,7 +1,7 @@ #include +#include "runtime-core/runtime-core.h" #include "runtime/array_functions.h" -#include "runtime/kphp_core.h" class ArrayIntStringKeysCollision : public testing::Test { protected: diff --git a/tests/cpp/runtime/array-test.cpp b/tests/cpp/runtime/array-test.cpp index c7b34c5cf3..f2ac66c4e9 100644 --- a/tests/cpp/runtime/array-test.cpp +++ b/tests/cpp/runtime/array-test.cpp @@ -1,6 +1,6 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" TEST(array_test, find_no_mutate_in_empy_array) { array arr; diff --git a/tests/cpp/runtime/kphp-type-traits-test.cpp b/tests/cpp/runtime/kphp-type-traits-test.cpp index ccdb62d3d5..ccec5ea19e 100644 --- a/tests/cpp/runtime/kphp-type-traits-test.cpp +++ b/tests/cpp/runtime/kphp-type-traits-test.cpp @@ -1,9 +1,7 @@ #include - -#include "runtime/kphp_core.h" -#include "runtime/kphp_type_traits.h" -#include "runtime/refcountable_php_classes.h" +#include "runtime-core/class-instance/refcountable-php-classes.h" +#include "runtime-core/runtime-core.h" struct Stub : refcountable_php_classes { }; diff --git a/tests/cpp/runtime/memory_resource/details/memory_chunk_list-test.cpp b/tests/cpp/runtime/memory_resource/details/memory_chunk_list-test.cpp index f9ed196a2a..0921607e8f 100644 --- a/tests/cpp/runtime/memory_resource/details/memory_chunk_list-test.cpp +++ b/tests/cpp/runtime/memory_resource/details/memory_chunk_list-test.cpp @@ -1,6 +1,6 @@ #include -#include "runtime/memory_resource/details/memory_chunk_list.h" +#include "runtime-core/memory-resource/details/memory_chunk_list.h" TEST(unsynchronized_memory_chunk_list_test, empty) { memory_resource::details::memory_chunk_list mem_chunk_list; diff --git a/tests/cpp/runtime/memory_resource/details/memory_chunk_tree-test.cpp b/tests/cpp/runtime/memory_resource/details/memory_chunk_tree-test.cpp index e898abbae1..d283440fd3 100644 --- a/tests/cpp/runtime/memory_resource/details/memory_chunk_tree-test.cpp +++ b/tests/cpp/runtime/memory_resource/details/memory_chunk_tree-test.cpp @@ -2,8 +2,8 @@ #include #include -#include "runtime/memory_resource/details/memory_chunk_tree.h" -#include "runtime/memory_resource/details/memory_ordered_chunk_list.h" +#include "runtime-core/memory-resource/details/memory_chunk_tree.h" +#include "runtime-core/memory-resource/details/memory_ordered_chunk_list.h" #include "tests/cpp/runtime/memory_resource/details/test-helpers.h" TEST(memory_chunk_tree_test, empty) { diff --git a/tests/cpp/runtime/memory_resource/details/memory_ordered_chunk_list-test.cpp b/tests/cpp/runtime/memory_resource/details/memory_ordered_chunk_list-test.cpp index d98af5253f..9dc1d58687 100644 --- a/tests/cpp/runtime/memory_resource/details/memory_ordered_chunk_list-test.cpp +++ b/tests/cpp/runtime/memory_resource/details/memory_ordered_chunk_list-test.cpp @@ -3,8 +3,8 @@ #include -#include "runtime/memory_resource/details/memory_ordered_chunk_list.h" -#include "runtime/memory_resource/monotonic_buffer_resource.h" +#include "runtime-core/memory-resource/details/memory_ordered_chunk_list.h" +#include "runtime-core/memory-resource/monotonic_buffer_resource.h" #include "tests/cpp/runtime/memory_resource/details/test-helpers.h" TEST(memory_ordered_chunk_list_test, empty) { diff --git a/tests/cpp/runtime/memory_resource/details/test-helpers.h b/tests/cpp/runtime/memory_resource/details/test-helpers.h index ee96824a15..cfc69ca9fe 100644 --- a/tests/cpp/runtime/memory_resource/details/test-helpers.h +++ b/tests/cpp/runtime/memory_resource/details/test-helpers.h @@ -2,8 +2,8 @@ #include "common/wrappers/to_array.h" -#include "runtime/memory_resource/memory_resource.h" -#include "runtime/memory_resource/details/memory_chunk_list.h" +#include "runtime-core/memory-resource/details/memory_chunk_list.h" +#include "runtime-core/memory-resource/memory_resource.h" template inline auto make_offsets(const std::array &sizes) { diff --git a/tests/cpp/runtime/memory_resource/extra-memory-pool-test.cpp b/tests/cpp/runtime/memory_resource/extra-memory-pool-test.cpp index 06b57b5b99..92bae2ccb8 100644 --- a/tests/cpp/runtime/memory_resource/extra-memory-pool-test.cpp +++ b/tests/cpp/runtime/memory_resource/extra-memory-pool-test.cpp @@ -5,7 +5,7 @@ #include #include -#include "runtime/memory_resource/extra-memory-pool.h" +#include "runtime-core/memory-resource/extra-memory-pool.h" namespace mr = memory_resource; diff --git a/tests/cpp/runtime/memory_resource/unsynchronized_pool_resource-test.cpp b/tests/cpp/runtime/memory_resource/unsynchronized_pool_resource-test.cpp index cb3852699c..81dc799f2e 100644 --- a/tests/cpp/runtime/memory_resource/unsynchronized_pool_resource-test.cpp +++ b/tests/cpp/runtime/memory_resource/unsynchronized_pool_resource-test.cpp @@ -1,7 +1,7 @@ #include #include -#include "runtime/memory_resource/unsynchronized_pool_resource.h" +#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" TEST(unsynchronized_pool_resource_test, uninited_state) { memory_resource::unsynchronized_pool_resource resource; diff --git a/tests/cpp/runtime/msgpack-test.cpp b/tests/cpp/runtime/msgpack-test.cpp index b8fe90032d..d55072723d 100644 --- a/tests/cpp/runtime/msgpack-test.cpp +++ b/tests/cpp/runtime/msgpack-test.cpp @@ -1,4 +1,4 @@ -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/msgpack-serialization.h" #include #include diff --git a/tests/cpp/runtime/number-string-comparison.cpp b/tests/cpp/runtime/number-string-comparison.cpp index e9c3f252b3..995a0dbe68 100644 --- a/tests/cpp/runtime/number-string-comparison.cpp +++ b/tests/cpp/runtime/number-string-comparison.cpp @@ -1,6 +1,6 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" template struct TestCaseComparison { diff --git a/tests/cpp/runtime/string-test.cpp b/tests/cpp/runtime/string-test.cpp index 6353645304..90dc4ffda9 100644 --- a/tests/cpp/runtime/string-test.cpp +++ b/tests/cpp/runtime/string-test.cpp @@ -1,6 +1,6 @@ #include -#include "runtime/kphp_core.h" +#include "runtime-core/runtime-core.h" #include "runtime/string_functions.h" TEST(string_test, test_empty) { diff --git a/tests/cpp/server/job-workers/shared-memory-manager-test.cpp b/tests/cpp/server/job-workers/shared-memory-manager-test.cpp index 0696dc50a2..a79c3005b4 100644 --- a/tests/cpp/server/job-workers/shared-memory-manager-test.cpp +++ b/tests/cpp/server/job-workers/shared-memory-manager-test.cpp @@ -6,7 +6,7 @@ #include "common/macos-ports.h" -#include "runtime/memory_resource/extra-memory-pool.h" +#include "runtime-core/memory-resource/extra-memory-pool.h" #include "server/job-workers/job-message.h" #include "server/job-workers/job-stats.h" #include "server/job-workers/job-workers-context.h"