Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move most of string functions into runtime-common/stdlib #1130

Merged
merged 14 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions runtime-common/core/allocator/runtime-allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
#pragma once

#include <cstddef>
#include "runtime-common/core/memory-resource/unsynchronized_pool_resource.h"

#include "common/mixin/not_copyable.h"
#include "runtime-common/core/memory-resource/unsynchronized_pool_resource.h"

struct RuntimeAllocator {
static RuntimeAllocator& current() noexcept;
struct RuntimeAllocator final : vk::not_copyable {
static RuntimeAllocator &get() noexcept;

RuntimeAllocator() = default;
RuntimeAllocator(size_t script_mem_size, size_t oom_handling_mem_size);

void init(void * buffer, size_t script_mem_size, size_t oom_handling_mem_size);
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 *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 * alloc0_global_memory(size_t size) noexcept;
void * realloc_global_memory(void *mem, size_t new_size, size_t old_size) noexcept;
void *alloc_global_memory(size_t size) noexcept;
void *alloc0_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;
Expand Down
4 changes: 2 additions & 2 deletions runtime-common/core/allocator/script-allocator-managed.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
class ScriptAllocatorManaged {
public:
static void *operator new(size_t size) noexcept {
return RuntimeAllocator::current().alloc_script_memory(size);
return RuntimeAllocator::get().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);
RuntimeAllocator::get().free_script_memory(ptr, size);
}

static void *operator new[](size_t count) = delete;
Expand Down
2 changes: 1 addition & 1 deletion runtime-common/core/class-instance/class-instance.inl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class_instance<T> class_instance<T>::alloc(Args &&... args) {
template<class T>
inline class_instance<T> class_instance<T>::empty_alloc() {
static_assert(std::is_empty<T>{}, "class T must be empty");
uint32_t obj = ++KphpCoreContext::current().empty_obj_count;
uint32_t obj = ++RuntimeContext::get().empty_obj_count;
new (&o) vk::intrusive_ptr<T>(reinterpret_cast<T*>(obj));
return *this;
}
Expand Down
29 changes: 29 additions & 0 deletions runtime-common/core/core-context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2024 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstddef>
#include <cstdint>

#include "common/mixin/not_copyable.h"

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from runtime-core.h"
#endif

struct RuntimeContext final : vk::not_copyable {
int32_t show_migration_php8_warning{};
int32_t php_disable_warnings{};
uint32_t empty_obj_count{};

string_buffer_lib_context sb_lib_context{};
string_buffer static_SB{};
string_buffer static_SB_spare{};

void init() noexcept;
void free() noexcept;

static RuntimeContext &get() noexcept;
};
4 changes: 2 additions & 2 deletions runtime-common/core/core-types/comparison_operators.inl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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 (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (RuntimeContext::get().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;
Expand All @@ -117,7 +117,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 (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (RuntimeContext::get().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;
Expand Down
16 changes: 8 additions & 8 deletions runtime-common/core/core-types/definition/array.inl
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ template<class T>
typename array<T>::array_inner *array<T>::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<array_inner *>(RuntimeAllocator::current().alloc_script_memory(mem_size));
auto p = reinterpret_cast<array_inner *>(RuntimeAllocator::get().alloc_script_memory(mem_size));
p->is_vector_internal = true;
p->ref_cnt = 0;
p->max_key = -1;
Expand All @@ -266,7 +266,7 @@ typename array<T>::array_inner *array<T>::array_inner::create(int64_t new_int_si
return reinterpret_cast<array_inner *>(static_cast<char *>(mem) + sizeof(array_inner_fields_for_map));
};

array_inner *p = shift_pointer_to_array_inner(RuntimeAllocator::current().alloc0_script_memory(mem_size));
array_inner *p = shift_pointer_to_array_inner(RuntimeAllocator::get().alloc0_script_memory(mem_size));
p->is_vector_internal = false;
p->ref_cnt = 0;
p->max_key = -1;
Expand All @@ -291,7 +291,7 @@ void array<T>::array_inner::dispose() {
((T *)entries())[i].~T();
}

RuntimeAllocator::current().free_script_memory((void *)this, sizeof_vector(buf_size));
RuntimeAllocator::get().free_script_memory((void *)this, sizeof_vector(buf_size));
return;
}

Expand All @@ -304,7 +304,7 @@ void array<T>::array_inner::dispose() {

php_assert(this != empty_array());
auto shifted_this = std::launder(reinterpret_cast<char *>(this)) - sizeof(array_inner_fields_for_map);
RuntimeAllocator::current().free_script_memory(shifted_this, sizeof_map(buf_size));
RuntimeAllocator::get().free_script_memory(shifted_this, sizeof_map(buf_size));
}
}
}
Expand Down Expand Up @@ -732,7 +732,7 @@ void array<T>::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<uint32_t>(int_size);
p = static_cast<array_inner *>(RuntimeAllocator::current().realloc_script_memory(p, p->sizeof_vector(new_int_buff_size), p->sizeof_vector(p->buf_size)));
p = static_cast<array_inner *>(RuntimeAllocator::get().realloc_script_memory(p, p->sizeof_vector(new_int_buff_size), p->sizeof_vector(p->buf_size)));
p->buf_size = new_int_buff_size;
}

Expand Down Expand Up @@ -1681,7 +1681,7 @@ array<T> &array<T>::operator+=(const array<T> &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 *)RuntimeAllocator::current().realloc_script_memory((void *)p, p->sizeof_vector(new_size), p->sizeof_vector(p->buf_size));
p = (array_inner *)RuntimeAllocator::get().realloc_script_memory((void *)p, p->sizeof_vector(new_size), p->sizeof_vector(p->buf_size));
p->buf_size = new_size;
}

Expand Down Expand Up @@ -1925,7 +1925,7 @@ void array<T>::sort(const T1 &compare, bool renumber) {
mutate_if_map_shared();
}

array_bucket **arTmp = (array_bucket **)RuntimeAllocator::current().alloc_script_memory(n * sizeof(array_bucket * ));
array_bucket **arTmp = (array_bucket **)RuntimeAllocator::get().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;
Expand All @@ -1947,7 +1947,7 @@ void array<T>::sort(const T1 &compare, bool renumber) {
arTmp[n - 1]->next = p->get_pointer(p->end());
p->end()->prev = p->get_pointer(arTmp[n - 1]);

RuntimeAllocator::current().free_script_memory(arTmp, n * sizeof(array_bucket * ));
RuntimeAllocator::get().free_script_memory(arTmp, n * sizeof(array_bucket * ));
}


Expand Down
6 changes: 3 additions & 3 deletions runtime-common/core/core-types/definition/mixed.inl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ bool less_string_number_as_php8_impl(const string &lhs, T rhs) {

template <typename T>
bool less_number_string_as_php8(bool php7_result, T lhs, const string &rhs) {
if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (RuntimeContext::get().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;
Expand All @@ -296,7 +296,7 @@ bool less_number_string_as_php8(bool php7_result, T lhs, const string &rhs) {

template <typename T>
bool less_string_number_as_php8(bool php7_result, const string &lhs, T rhs) {
if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (RuntimeContext::get().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;
Expand Down Expand Up @@ -327,4 +327,4 @@ ResultClass from_mixed(const mixed &m, const string &) noexcept {
} else {
return ResultClass::create_from_base_raw_ptr(dynamic_cast<abstract_refcountable_php_interface *>(m.as_object_ptr<ResultClass>()));
}
}
}
10 changes: 5 additions & 5 deletions runtime-common/core/core-types/definition/string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)RuntimeAllocator::current().alloc_script_memory(new_size);
string_inner *p = (string_inner *)RuntimeAllocator::get().alloc_script_memory(new_size);
p->capacity = capacity;
return p;
}
Expand All @@ -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 *)RuntimeAllocator::current().realloc_script_memory((void *)this, new_size, old_size);
string_inner *p = (string_inner *)RuntimeAllocator::get().realloc_script_memory((void *)this, new_size, old_size);
p->capacity = new_cap;
return p->ref_data();
}
Expand All @@ -83,7 +83,7 @@ void string::string_inner::dispose() {
}

void string::string_inner::destroy() {
RuntimeAllocator::current().free_script_memory(this, get_memory_usage());
RuntimeAllocator::get().free_script_memory(this, get_memory_usage());
}

inline string::size_type string::string_inner::get_memory_usage() const {
Expand Down Expand Up @@ -701,7 +701,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 ((KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_TO_FLOAT_FLAG) && php8_warning) {
if ((RuntimeContext::get().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) {
Expand Down Expand Up @@ -861,7 +861,7 @@ bool string::is_numeric_as_php7() const {
bool string::is_numeric() const {
const auto php7_result = is_numeric_as_php7();

if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_TO_FLOAT_FLAG) {
if (RuntimeContext::get().show_migration_php8_warning & MIGRATION_PHP8_STRING_TO_FLOAT_FLAG) {
const bool php8_result = is_numeric_as_php8();

if (php7_result != php8_result) {
Expand Down
4 changes: 2 additions & 2 deletions runtime-common/core/core-types/definition/string_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#include "runtime-common/core/runtime-core.h"

string_buffer::string_buffer(string::size_type buffer_len) noexcept:
buffer_end(static_cast<char *>(RuntimeAllocator::current().alloc_global_memory(buffer_len))),
buffer_end(static_cast<char *>(RuntimeAllocator::get().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);
RuntimeAllocator::get().free_global_memory(buffer_begin, buffer_len);
}
12 changes: 6 additions & 6 deletions runtime-common/core/core-types/definition/string_buffer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#endif

inline void string_buffer::resize(string::size_type new_buffer_len) noexcept {
string_buffer_lib_context &sb_context = KphpCoreContext::current().sb_lib_context;
string_buffer_lib_context &sb_context = RuntimeContext::get().sb_lib_context;
if (new_buffer_len < sb_context.MIN_BUFFER_LEN) {
new_buffer_len = sb_context.MIN_BUFFER_LEN;
}
Expand All @@ -26,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 = RuntimeAllocator::current().realloc_global_memory(buffer_begin, new_buffer_len, buffer_len)) {
if(void *new_mem = RuntimeAllocator::get().realloc_global_memory(buffer_begin, new_buffer_len, buffer_len)) {
buffer_begin = static_cast<char *>(new_mem);
buffer_len = new_buffer_len;
buffer_end = buffer_begin + current_len;
Expand All @@ -35,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 && KphpCoreContext::current().sb_lib_context.error_flag != STRING_BUFFER_ERROR_FLAG_FAILED)) {
while (unlikely (buffer_len < new_buffer_len && RuntimeContext::get().sb_lib_context.error_flag != STRING_BUFFER_ERROR_FLAG_FAILED)) {
resize(((new_buffer_len * 2 + 1 + 64) | 4095) - 64);
}
}
Expand Down Expand Up @@ -72,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 (KphpCoreContext::current().sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) {
if (unlikely (RuntimeContext::get().sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) {
return sb;
}

Expand Down Expand Up @@ -142,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<string::size_type>(len));

if (unlikely (KphpCoreContext::current().sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) {
if (unlikely (RuntimeContext::get().sb_lib_context.error_flag == STRING_BUFFER_ERROR_FLAG_FAILED)) {
return *this;
}
memcpy(buffer_end, str, len);
Expand Down Expand Up @@ -170,7 +170,7 @@ void string_buffer::reserve(int len) {
}

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;
string_buffer_lib_context &sb_context = RuntimeContext::get().sb_lib_context;
if (min_length > 0) {
sb_context.MIN_BUFFER_LEN = min_length;
}
Expand Down
31 changes: 0 additions & 31 deletions runtime-common/core/runtime-core-context.h

This file was deleted.

6 changes: 3 additions & 3 deletions runtime-common/core/runtime-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "runtime-common/core/core-types/decl/string_buffer_decl.inl"

#include "runtime-common/core/allocator/runtime-allocator.h"
#include "runtime-common/core/runtime-core-context.h"
#include "runtime-common/core/core-context.h"

#include "runtime-common/core/core-types/definition/string.inl"
#include "runtime-common/core/core-types/definition/array.inl"
Expand All @@ -54,8 +54,8 @@
#define SAFE_SET_VALUE(a, b, b_type, c, c_type) ({b_type b_tmp___ = b; c_type c_tmp___ = c; (a).set_value (b_tmp___, c_tmp___);})
#define SAFE_PUSH_BACK(a, b, b_type) ({b_type b_tmp___ = b; a.push_back (b_tmp___);})
#define SAFE_PUSH_BACK_RETURN(a, b, b_type) ({b_type b_tmp___ = b; a.push_back_return (b_tmp___);})
#define NOERR(a, a_type) ({KphpCoreContext::current().php_disable_warnings++; a_type a_tmp___ = a; KphpCoreContext::current().php_disable_warnings--; a_tmp___;})
#define NOERR_VOID(a) ({KphpCoreContext::current().php_disable_warnings++; a; KphpCoreContext::current().php_disable_warnings--;})
#define NOERR(a, a_type) ({RuntimeContext::get().php_disable_warnings++; a_type a_tmp___ = a; RuntimeContext::get().php_disable_warnings--; a_tmp___;})
#define NOERR_VOID(a) ({RuntimeContext::get().php_disable_warnings++; a; RuntimeContext::get().php_disable_warnings--;})

#define f$likely likely
#define f$unlikely unlikely
Expand Down
2 changes: 2 additions & 0 deletions runtime-common/core/utils/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <cstddef>
#include <functional>

#pragma once

// from boost
// see https://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine
template<typename T>
Expand Down
Loading
Loading