From b8eb5d2e29c11b1b66a2e015b92d91b738516dc8 Mon Sep 17 00:00:00 2001 From: Thomas A Date: Mon, 24 Jul 2023 14:36:19 -0700 Subject: [PATCH] [xtrace] Implement NativeAllocator & Rework C++ Classes To Use NativeAllocator --- src/xtrace/CMakeLists.txt | 1 + src/xtrace/log.cpp | 14 +++++++------- src/xtrace/log.h | 14 ++++++++------ src/xtrace/memory.cpp | 2 ++ src/xtrace/memory.h | 32 +++++++++++++++++++++++++++++++- src/xtrace/string.cpp | 9 +++++++++ src/xtrace/string.h | 16 ++++++++++++++++ 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 src/xtrace/string.cpp create mode 100644 src/xtrace/string.h diff --git a/src/xtrace/CMakeLists.txt b/src/xtrace/CMakeLists.txt index fbfaa7cd7..365e00a55 100644 --- a/src/xtrace/CMakeLists.txt +++ b/src/xtrace/CMakeLists.txt @@ -19,6 +19,7 @@ set(xtrace_sources lock.c posix_spawn_args.c log.cpp + string.cpp ) if (TARGET_x86_64) diff --git a/src/xtrace/log.cpp b/src/xtrace/log.cpp index 02170f862..66de2d498 100644 --- a/src/xtrace/log.cpp +++ b/src/xtrace/log.cpp @@ -45,7 +45,7 @@ xtrace_logging_file::xtrace_logging_file() { per_thread = false; } -void xtrace_logging_file::initalize(xtrace_logging_location *state, std::string filename_base) { +void xtrace_logging_file::initalize(xtrace_logging_location *state, xtrace::string filename_base) { this->state = state; this->filename_base = filename_base; this->per_thread = string_is_truthy(getenv("XTRACE_LOG_FILE_PER_THREAD")); @@ -64,7 +64,7 @@ void xtrace_logging_file::ensure_logfile(void) { } if (per_thread) { - std::string filename = filename_base + "." + std::to_string(sys_thread_selfid()); + xtrace::string filename = filename_base + "." + xtrace::to_string(sys_thread_selfid()); fd = _open_for_xtrace(filename.c_str(), O_WRONLY | O_APPEND | O_CLOEXEC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); } else { xtrace_once(&xtrace_common_logfile_once, xtrace_common_logfile_init); @@ -97,14 +97,14 @@ void xtrace_logging::initalize() { state = XTRACE_LOG_LOCATION_KERNEL; } else if (xtrace_log_file != NULL && xtrace_log_file[0] != '\0') { state = XTRACE_LOG_LOCATION_FILE; - logfile_setting.initalize(&state, std::string(xtrace_log_file)); + logfile_setting.initalize(&state, xtrace::string(xtrace_log_file)); } } void xtrace_logging::append(xtrace_logging_output type, const char* format, va_list args) { // Calculate size int size = vsnprintf_wrapper(nullptr, 0, format, args); - std::vector c_string_buffer(size); + std::vector> c_string_buffer(size); assert(vsnprintf_wrapper(c_string_buffer.data(), c_string_buffer.size(), format, args) == size); if (type == XTRACE_LOG_OUTPUT_STDOUT) { @@ -117,7 +117,7 @@ void xtrace_logging::append(xtrace_logging_output type, const char* format, va_l void xtrace_logging::flush(xtrace_logging_output type, const char* format, va_list args) { append(type, format, args); - std::string *buffer = nullptr; + xtrace::string *buffer = nullptr; int fd = -1; if (type == XTRACE_LOG_OUTPUT_STDOUT) { buffer = &buffer_stdout; @@ -281,7 +281,7 @@ void xtrace_per_thread_logfile_destroy(int* ptr) { extern "C" void xtrace_common_logfile_init(void) { - std::string& filename_base = internal_logging->get_logfile_settings().get_filename_base(); + xtrace::string& filename_base = internal_logging->get_logfile_settings().get_filename_base(); xtrace_common_logfile = _open_for_xtrace(filename_base.c_str(), O_WRONLY | O_APPEND | O_CLOEXEC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); }; @@ -297,4 +297,4 @@ void xtrace_postfork_child_hook(void) { } set_xtrace_per_thread_logfile(-1); } -}; \ No newline at end of file +}; diff --git a/src/xtrace/log.h b/src/xtrace/log.h index 1ff36f094..f5a35ac35 100644 --- a/src/xtrace/log.h +++ b/src/xtrace/log.h @@ -2,6 +2,8 @@ #define XTRACE_LOGGING #include "base.h" +#include "memory.h" +#include "string.h" enum xtrace_logging_output { XTRACE_LOG_OUTPUT_STDOUT, @@ -25,24 +27,24 @@ enum xtrace_logging_location { class xtrace_logging_file { private: xtrace_logging_location *state; - std::string filename_base; + xtrace::string filename_base; bool per_thread; public: xtrace_logging_file(); public: - void initalize(xtrace_logging_location *state, std::string filename_base); + void initalize(xtrace_logging_location *state, xtrace::string filename_base); void ensure_logfile(void); bool is_per_thread(void) { return per_thread; } - std::string& get_filename_base() { return filename_base; } + xtrace::string& get_filename_base() { return filename_base; } }; class xtrace_logging { private: - std::string buffer_stdout; - std::string buffer_error; + xtrace::string buffer_stdout; + xtrace::string buffer_error; xtrace_logging_location state; xtrace_logging_file logfile_setting; bool disable_color; @@ -86,4 +88,4 @@ const char* xtrace_logging_get_filename_base(); XTRACE_DECLARATIONS_END -#endif // XTRACE_LOGGING \ No newline at end of file +#endif // XTRACE_LOGGING diff --git a/src/xtrace/memory.cpp b/src/xtrace/memory.cpp index 361dbc10e..886ee8d2f 100644 --- a/src/xtrace/memory.cpp +++ b/src/xtrace/memory.cpp @@ -1,3 +1,5 @@ +#include "memory.h" + #include extern struct elf_calls* _elfcalls; diff --git a/src/xtrace/memory.h b/src/xtrace/memory.h index 23d1f670d..7ffac4320 100644 --- a/src/xtrace/memory.h +++ b/src/xtrace/memory.h @@ -13,6 +13,36 @@ void* xtrace_realloc(void *ptr, size_t size); XTRACE_DECLARATIONS_END #ifdef __cplusplus +#include + +namespace xtrace { + template + struct NativeAllocator : std::allocator { + using value_type = T; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using propagate_on_container_move_assignment = std::true_type; + using is_always_equal = std::true_type; + + T* allocate(std::size_t n) { + return (T*)xtrace_malloc(n*sizeof(T)); + } + + template + void construct(T* p, Args&& ...args) { + new (p) T(std::forward(args)...); + } + + void destroy(T* p) { + p->~T(); + } + + void deallocate(T* p, std::size_t) { + xtrace_free(p); + } + }; +} + #endif -#endif // XTRACE_MEMORY \ No newline at end of file +#endif // XTRACE_MEMORY diff --git a/src/xtrace/string.cpp b/src/xtrace/string.cpp new file mode 100644 index 000000000..8aa6568ae --- /dev/null +++ b/src/xtrace/string.cpp @@ -0,0 +1,9 @@ +#include "string.h" + +#include + +xtrace::string xtrace::to_string(int value) { + char tmp[12] = ""; + __simple_snprintf(tmp,sizeof(tmp),"%i",value); + return xtrace::string(tmp); +} diff --git a/src/xtrace/string.h b/src/xtrace/string.h new file mode 100644 index 000000000..f41b04734 --- /dev/null +++ b/src/xtrace/string.h @@ -0,0 +1,16 @@ +#ifndef XTRACE_STRING +#define XTRACE_STRING + +#include "memory.h" + +#ifdef __cplusplus +#include + +namespace xtrace { + typedef std::basic_string,xtrace::NativeAllocator> string; + + string to_string(int value); +} +#endif + +#endif // XTRACE_STRING