Skip to content

Commit

Permalink
[xtrace] Implement NativeAllocator & Rework C++ Classes To Use Native…
Browse files Browse the repository at this point in the history
…Allocator
  • Loading branch information
CuriousTommy committed Jul 27, 2023
1 parent 3622830 commit b8eb5d2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/xtrace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(xtrace_sources
lock.c
posix_spawn_args.c
log.cpp
string.cpp
)

if (TARGET_x86_64)
Expand Down
14 changes: 7 additions & 7 deletions src/xtrace/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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);
Expand Down Expand Up @@ -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<char> c_string_buffer(size);
std::vector<char,xtrace::NativeAllocator<char>> c_string_buffer(size);

assert(vsnprintf_wrapper(c_string_buffer.data(), c_string_buffer.size(), format, args) == size);
if (type == XTRACE_LOG_OUTPUT_STDOUT) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
};

Expand All @@ -297,4 +297,4 @@ void xtrace_postfork_child_hook(void) {
}
set_xtrace_per_thread_logfile(-1);
}
};
};
14 changes: 8 additions & 6 deletions src/xtrace/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define XTRACE_LOGGING

#include "base.h"
#include "memory.h"
#include "string.h"

enum xtrace_logging_output {
XTRACE_LOG_OUTPUT_STDOUT,
Expand All @@ -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;
Expand Down Expand Up @@ -86,4 +88,4 @@ const char* xtrace_logging_get_filename_base();

XTRACE_DECLARATIONS_END

#endif // XTRACE_LOGGING
#endif // XTRACE_LOGGING
2 changes: 2 additions & 0 deletions src/xtrace/memory.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "memory.h"

#include <elfcalls.h>

extern struct elf_calls* _elfcalls;
Expand Down
32 changes: 31 additions & 1 deletion src/xtrace/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@ void* xtrace_realloc(void *ptr, size_t size);
XTRACE_DECLARATIONS_END

#ifdef __cplusplus
#include <memory>

namespace xtrace {
template <class T>
struct NativeAllocator : std::allocator<T> {
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 <typename ...Args>
void construct(T* p, Args&& ...args) {
new (p) T(std::forward<Args>(args)...);
}

void destroy(T* p) {
p->~T();
}

void deallocate(T* p, std::size_t) {
xtrace_free(p);
}
};
}

#endif

#endif // XTRACE_MEMORY
#endif // XTRACE_MEMORY
9 changes: 9 additions & 0 deletions src/xtrace/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "string.h"

#include <darling/emulation/simple.h>

xtrace::string xtrace::to_string(int value) {
char tmp[12] = "";
__simple_snprintf(tmp,sizeof(tmp),"%i",value);
return xtrace::string(tmp);
}
16 changes: 16 additions & 0 deletions src/xtrace/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef XTRACE_STRING
#define XTRACE_STRING

#include "memory.h"

#ifdef __cplusplus
#include <string>

namespace xtrace {
typedef std::basic_string<char,std::char_traits<char>,xtrace::NativeAllocator<char>> string;

string to_string(int value);
}
#endif

#endif // XTRACE_STRING

0 comments on commit b8eb5d2

Please sign in to comment.