diff --git a/Assignment/Task 2/fiber.cpp b/Assignment/Task 2/fiber.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c12b4e5d09a9adc7ca132145d9130fb60ba6236f --- /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_; +} +