From dba699c7596f583da16b6c7e9e937bdf60a5380e Mon Sep 17 00:00:00 2001 From: j2-tulloch <james2.tulloch@live.uwe.ac.uk> Date: Fri, 5 Jan 2024 14:43:33 +0000 Subject: [PATCH] Upload New File --- Assignment/Task 2/fiber.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Assignment/Task 2/fiber.cpp diff --git a/Assignment/Task 2/fiber.cpp b/Assignment/Task 2/fiber.cpp new file mode 100644 index 0000000..c12b4e5 --- /dev/null +++ b/Assignment/Task 2/fiber.cpp @@ -0,0 +1,31 @@ +#include "fiber.h" +#include <cstring> + +fiber::fiber(void (*function)(void*), void* data) : data_(data) { + // Align the stack, set RIP, RSP, etc. + // Allocate the stack + std::memset(stack_.data(), 0, stack_.size()); + stack_bottom_ = stack_.data(); + stack_top_ = stack_bottom_ + stack_.size(); + + // Align the stack top to 16 bytes as per System V ABI requirements + stack_top_ = reinterpret_cast<char*>((reinterpret_cast<uintptr_t>(stack_top_) & -16L) - 128); + + // Set the context + context_.rip = reinterpret_cast<void*>(function); + context_.rsp = reinterpret_cast<void*>(stack_top_); + // Other registers (rbx, rbp, r12-r15) will be set when saving context +} + +fiber::~fiber() { + // Clean up, if needed +} + +Context* fiber::get_context() { + return &context_; +} + +void* fiber::get_data() const { + return data_; +} + -- GitLab