自定义内存分配池

This commit is contained in:
2026-07-29 18:08:57 +08:00
parent cca030726d
commit 14a228d277
3 changed files with 35 additions and 2 deletions
+5 -2
View File
@@ -2,6 +2,7 @@
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <memory_resource>
#include <mutex>
#include <string>
#include <string_view>
@@ -32,7 +33,7 @@ template <typename MutexT, typename Size_T>
class Base_RingBuffer {
static_assert(std::is_unsigned_v<Size_T>, "Size_T must be unsigned");
public:
Base_RingBuffer() = default;
explicit Base_RingBuffer(std::pmr::memory_resource* resource = std::pmr::get_default_resource()) : buf_(resource) {}
void init(Size_T min_capacity) {
// 防御:min_capacity + 1 溢出
if (min_capacity >= std::numeric_limits<Size_T>::max()) {
@@ -95,13 +96,14 @@ protected:
Size_T capacity = 0;
Size_T head = 0;
Size_T tail = 0;
std::vector<uint8_t> buf_{};
std::pmr::vector<uint8_t> buf_;
Size_T mask_ = 0;
};
// -------------------- Packet mode (length-prefixed) --------------------
template <class MutexT, typename Size_T>
class RingBuffer : public Base_RingBuffer<MutexT, Size_T> {
public:
using Base_RingBuffer<MutexT, Size_T>::Base_RingBuffer;
static constexpr Size_T HEADER_SIZE = sizeof(Size_T);
// 包模式:空间不够则整体失败(不拆包)
bool write(const void* src, Size_T len) {
@@ -213,6 +215,7 @@ public:
template <class MutexT, typename Size_T>
class Stream_RingBuffer : public Base_RingBuffer<MutexT, Size_T> {
public:
using Base_RingBuffer<MutexT, Size_T>::Base_RingBuffer;
bool write(const void* src, Size_T len) {
if (len == 0)
return true;
+29
View File
@@ -19,6 +19,7 @@ std::string handle_fileName(std::string_view utf8_str) {
#endif
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
@@ -121,6 +122,34 @@ void pure_log(std::string_view pure_path, std::string_view info,
logger->flush();
}
}
void csv_log(std::string_view pure_path, std::string_view header, std::string_view line) {
auto path = Psc::get_abs_path(pure_path);
static std::map<std::string, std::shared_ptr<spdlog::logger>> file_logger_map;
static std::mutex mtx;
static std::once_flag pool_flag;
std::call_once(pool_flag, []() {
if (!spdlog::thread_pool())
spdlog::init_thread_pool(1024 * 1024, 1);
spdlog::flush_every(std::chrono::seconds(1));
});
std::shared_ptr<spdlog::logger> logger;
{
std::lock_guard<std::mutex> lock(mtx);
auto iter = file_logger_map.find(path);
if (iter == file_logger_map.end()) {
Psc::create_file_if_not_exists(path);
std::string platform_path = Psc::utf8_2_platform(path);
auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(platform_path, true);
auto created = std::make_shared<spdlog::async_logger>(platform_path, sink, spdlog::thread_pool(), spdlog::async_overflow_policy::overrun_oldest);
auto formatter = std::make_unique<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, "\n", spdlog::pattern_formatter::custom_flags());
created->set_formatter(std::move(formatter));
created->info(header);
iter = file_logger_map.insert({path, created}).first;
}
logger = iter->second;
}
logger->info(line);
}
Log_Type& Log_Type::append(std::string_view key) {
list.push_back(std::string(key));
return *this;
+1
View File
@@ -10,6 +10,7 @@
void rotating_log(std::string_view fileName, std::string_view info, int size = 10 * 1024 * 1024, int num = 10);
// 自带换行符
void pure_log(std::string_view path, std::string_view info, bool close_after_write = false);
void csv_log(std::string_view path, std::string_view header, std::string_view line);
struct Log_Type {
Log_Type() = default;
explicit Log_Type(std::vector<std::string> list);