Add copying utility methods to Arena
This commit is contained in:
@@ -8,24 +8,14 @@
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
// Helper to copy a string into arena memory
|
||||
static std::string_view arena_copy_string(std::string_view str, Arena &arena) {
|
||||
if (str.empty()) {
|
||||
return std::string_view{};
|
||||
}
|
||||
char *copied = arena.allocate<char>(str.size());
|
||||
std::memcpy(copied, str.data(), str.size());
|
||||
return std::string_view(copied, str.size());
|
||||
}
|
||||
|
||||
struct EchoHandler : ConnectionHandler {
|
||||
Arena arena;
|
||||
std::span<std::string_view> reply;
|
||||
WeakRef<MessageSender> wconn;
|
||||
std::latch done{1};
|
||||
void on_data_arrived(std::string_view data, Connection &conn) override {
|
||||
reply = std::span{arena.allocate<std::string_view>(1), 1};
|
||||
reply[0] = arena_copy_string(data, arena);
|
||||
reply = arena.allocate_span<std::string_view>(1);
|
||||
reply[0] = arena.copy_string(data);
|
||||
wconn = conn.get_weak_ref();
|
||||
CHECK(wconn.lock());
|
||||
done.count_down();
|
||||
|
||||
Reference in New Issue
Block a user