maint(string): update

This commit is contained in:
6arelyFuture 2023-03-29 17:47:57 +01:00
parent ad4ce13828
commit 2e95d9434a
No known key found for this signature in database
GPG Key ID: 22F9079C86CFAB31
7 changed files with 48 additions and 40 deletions

View File

@ -9,6 +9,9 @@
#include "console.hpp" #include "console.hpp"
#include "command.hpp" #include "command.hpp"
#include <corecrt_io.h>
#include <fcntl.h>
namespace console { namespace console {
namespace { namespace {
using message_queue = std::queue<std::string>; using message_queue = std::queue<std::string>;

View File

@ -51,6 +51,7 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call,
) { ) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) { if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
AddVectoredExceptionHandler(0, exception_handler); AddVectoredExceptionHandler(0, exception_handler);
SetProcessDEPPolicy(PROCESS_DEP_ENABLE);
std::srand(std::uint32_t(time(nullptr))); std::srand(std::uint32_t(time(nullptr)));

View File

@ -8,21 +8,20 @@
#include <WinSock2.h> #include <WinSock2.h>
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include <corecrt_io.h>
#include <fcntl.h>
#include <DbgHelp.h> #include <DbgHelp.h>
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <cctype>
#include <cstring>
#include <format>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <queue>
#include <source_location>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <source_location>
#include <queue>
#include <format>
#pragma comment(lib, "ntdll.lib") #pragma comment(lib, "ntdll.lib")
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")

View File

@ -30,7 +30,7 @@ void memory::allocator::free(const void* data) {
this->free(const_cast<void*>(data)); this->free(const_cast<void*>(data));
} }
void* memory::allocator::allocate(const size_t length) { void* memory::allocator::allocate(const std::size_t length) {
std::lock_guard _(this->mutex_); std::lock_guard _(this->mutex_);
const auto data = memory::allocate(length); const auto data = memory::allocate(length);
@ -48,7 +48,9 @@ char* memory::allocator::duplicate_string(const std::string& string) {
return data; return data;
} }
void* memory::allocate(const size_t length) { return std::calloc(length, 1); } void* memory::allocate(const std::size_t length) {
return std::calloc(1, length);
}
char* memory::duplicate_string(const std::string& string) { char* memory::duplicate_string(const std::string& string) {
const auto new_string = allocate_array<char>(string.size() + 1); const auto new_string = allocate_array<char>(string.size() + 1);
@ -60,10 +62,10 @@ void memory::free(void* data) { std::free(data); }
void memory::free(const void* data) { free(const_cast<void*>(data)); } void memory::free(const void* data) { free(const_cast<void*>(data)); }
bool memory::is_set(const void* mem, const char chr, const size_t length) { bool memory::is_set(const void* mem, const char chr, const std::size_t length) {
const auto mem_arr = static_cast<const char*>(mem); const auto mem_arr = static_cast<const char*>(mem);
for (size_t i = 0; i < length; ++i) { for (std::size_t i = 0; i < length; ++i) {
if (mem_arr[i] != chr) { if (mem_arr[i] != chr) {
return false; return false;
} }

View File

@ -16,15 +16,15 @@ public:
void free(const void* data); void free(const void* data);
void* allocate(size_t length); void* allocate(std::size_t length);
template <typename T> T* allocate() { return this->allocate_array<T>(1); } template <typename T> T* allocate() { return this->allocate_array<T>(1); }
template <typename T> T* allocate_array(const size_t count = 1) { template <typename T> T* allocate_array(const std::size_t count = 1) {
return static_cast<T*>(this->allocate(count * sizeof(T))); return static_cast<T*>(this->allocate(count * sizeof(T)));
} }
bool empty() const; [[nodiscard]] bool empty() const;
char* duplicate_string(const std::string& string); char* duplicate_string(const std::string& string);
@ -33,11 +33,11 @@ public:
std::vector<void*> pool_; std::vector<void*> pool_;
}; };
static void* allocate(size_t length); static void* allocate(std::size_t length);
template <typename T> static T* allocate() { return allocate_array<T>(1); } template <typename T> static T* allocate() { return allocate_array<T>(1); }
template <typename T> static T* allocate_array(const size_t count = 1) { template <typename T> static T* allocate_array(const std::size_t count = 1) {
return static_cast<T*>(allocate(count * sizeof(T))); return static_cast<T*>(allocate(count * sizeof(T)));
} }
@ -46,7 +46,7 @@ public:
static void free(void* data); static void free(void* data);
static void free(const void* data); static void free(const void* data);
static bool is_set(const void* mem, char chr, size_t length); static bool is_set(const void* mem, char chr, std::size_t length);
static bool is_bad_read_ptr(const void* ptr); static bool is_bad_read_ptr(const void* ptr);
static bool is_bad_code_ptr(const void* ptr); static bool is_bad_code_ptr(const void* ptr);

View File

@ -7,7 +7,7 @@
namespace utils::string { namespace utils::string {
const char* va(const char* fmt, ...) { const char* va(const char* fmt, ...) {
static thread_local va_provider<8, 256> provider; static thread_local va_provider<8, 1024> provider;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);

View File

@ -2,14 +2,17 @@
#include "memory.hpp" #include "memory.hpp"
#ifndef ARRAYSIZE #ifndef ARRAYSIZE
template <class Type, size_t n> size_t ARRAYSIZE(Type (&)[n]) { return n; } template <class Type, std::size_t n> std::size_t ARRAYSIZE(Type (&)[n]) {
return n;
}
#endif #endif
namespace utils::string { namespace utils::string {
template <size_t Buffers, size_t MinBufferSize> class va_provider final { template <std::size_t buffers, std::size_t min_buffer_size>
class va_provider final {
public: public:
static_assert(Buffers != 0 && MinBufferSize != 0, static_assert(buffers != 0 && min_buffer_size != 0,
"Buffers and MinBufferSize mustn't be 0"); "buffers and min_buffer_size mustn't be 0");
va_provider() : current_buffer_(0) {} va_provider() : current_buffer_(0) {}
@ -17,13 +20,13 @@ public:
++this->current_buffer_ %= ARRAYSIZE(this->string_pool_); ++this->current_buffer_ %= ARRAYSIZE(this->string_pool_);
auto entry = &this->string_pool_[this->current_buffer_]; auto entry = &this->string_pool_[this->current_buffer_];
if (!entry->size || !entry->buffer) { if (!entry->size_ || !entry->buffer_) {
throw std::runtime_error("String pool not initialized"); throw std::runtime_error("String pool not initialized");
} }
while (true) { while (true) {
const int res = const int res =
_vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap); vsnprintf_s(entry->buffer_, entry->size_, _TRUNCATE, format, ap);
if (res > 0) if (res > 0)
break; // Success break; // Success
if (res == 0) if (res == 0)
@ -32,44 +35,44 @@ public:
entry->double_size(); entry->double_size();
} }
return entry->buffer; return entry->buffer_;
} }
private: private:
class entry final { class entry final {
public: public:
explicit entry(const size_t _size = MinBufferSize) explicit entry(const std::size_t size = min_buffer_size)
: size(_size), buffer(nullptr) { : size_(size), buffer_(nullptr) {
if (this->size < MinBufferSize) if (this->size_ < min_buffer_size)
this->size = MinBufferSize; this->size_ = min_buffer_size;
this->allocate(); this->allocate();
} }
~entry() { ~entry() {
if (this->buffer) if (this->buffer_)
memory::get_allocator()->free(this->buffer); memory::get_allocator()->free(this->buffer_);
this->size = 0; this->size_ = 0;
this->buffer = nullptr; this->buffer_ = nullptr;
} }
void allocate() { void allocate() {
if (this->buffer) if (this->buffer_)
memory::get_allocator()->free(this->buffer); memory::get_allocator()->free(this->buffer_);
this->buffer = this->buffer_ =
memory::get_allocator()->allocate_array<char>(this->size + 1); memory::get_allocator()->allocate_array<char>(this->size_ + 1);
} }
void double_size() { void double_size() {
this->size *= 2; this->size_ *= 2;
this->allocate(); this->allocate();
} }
size_t size; size_t size_;
char* buffer; char* buffer_;
}; };
size_t current_buffer_; size_t current_buffer_;
entry string_pool_[Buffers]; entry string_pool_[buffers];
}; };
const char* va(const char* fmt, ...); const char* va(const char* fmt, ...);