From 2c312ff1f40b2c56115177df48ecc476fc8cf5ac Mon Sep 17 00:00:00 2001 From: anon Date: Mon, 1 Jul 2024 21:02:38 +0200 Subject: [PATCH] =?UTF-8?q?stacktor.hpp:40:13:=20note:=20the=20value=20of?= =?UTF-8?q?=20the=20stack=20pointer=20after=20an=20=E2=80=98asm=E2=80=99?= =?UTF-8?q?=20statement=20must=20be=20the=20same=20as=20it=20was=20before?= =?UTF-8?q?=20the=20statement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + main.cpp | 34 ++++++++++++++ stacktor.hpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 .gitignore create mode 100644 main.cpp create mode 100644 stacktor.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb71b6d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.out +.gdb_history diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..59d6dda --- /dev/null +++ b/main.cpp @@ -0,0 +1,34 @@ +// @BAKE g++ $@ -o stacktor.out -ggdb -O0 + +#include + +#include "stacktor.hpp" + +void print_stractor(stacktor &s) { + fputs("[ ", stdout); + /* + for (auto i : s) { + printf("%d ", i); + } + */ + for (int i = 0; i < s.size(); i++) { + printf("%d ", s.get(i)); + } + fputc(']', stdout); +} + +signed main() { + // It MUST be on the heap + stacktor * s = new stacktor(); + + puts(""); + s->push_back(10); + puts(""); + s->push_back(12); + puts(""); + s->push_back(6); + puts(""); + print_stractor(*s); + + return 0; +} diff --git a/stacktor.hpp b/stacktor.hpp new file mode 100644 index 0000000..cf46518 --- /dev/null +++ b/stacktor.hpp @@ -0,0 +1,130 @@ +#include +#include + +#ifndef DEBUG +# define DEBUG 0 +#endif + +#if DEBUG == 1 +# define DEBUG_PRINT(x) do { puts(x); fflush(stdout); } while (0) +#else +# define DEBUG_PRINT(x) do { ; } while (0) +#endif + +template +class stacktor { +private: + std::thread provider; + T data_swap; + T * data = nullptr; + size_t current_size = 0; + size_t request = -1; + size_t order = -1; + bool do_exit = false; + + int lock = 0; + + void bahaha() { + int data_size = sizeof(T); + + asm ( + "movq %%rsp, %0;" + : "=r" (data) + : + : + ); + + goto pass; + + start: + asm volatile ( + "sub %%rsp, %0;" + : + : "" (data_size) + ); + DEBUG_PRINT("Element allocated."); + --lock; + + pass: + DEBUG_PRINT("Locking."); + while (not lock) { + if (request != -1) { + data_swap = data[-request]; + request = -1; + DEBUG_PRINT("Data request satisfied."); + } + if (order != -1) { + data[-order] = data_swap; + order = -1; + DEBUG_PRINT("Data order satisfied."); + } + if (do_exit) { + return; + } + } + goto start; + } + +public: + stacktor() : provider(std::bind(&stacktor::bahaha, this)) { + DEBUG_PRINT("Waiting for data field initialization..."); + while (not data); + DEBUG_PRINT("Stacktor initialized."); + } + + //stacktor(const stacktor& other); + + //stacktor(stacktor&& other) noexcept; + + ~stacktor() { + do_exit = true; + provider.join(); + } + + //stacktor& operator=(const stacktor& other); + + //stacktor& operator=(stacktor&& other) noexcept; + + void push_back(const T& value) { + DEBUG_PRINT("Push initiated..."); + lock = 1; + while (lock); + this->set(this->current_size, value); + this->current_size += 1; + } + + void pop_back() { + --current_size; + } + + size_t size() const { + return this->current_size; + } + + bool empty() const { + return (this->size() == 0); + } + + void clear() { + current_size = 0; + } + + T& get(size_t index) { + DEBUG_PRINT("Request initiated..."); + + request = index; + while (request != -1); + DEBUG_PRINT("Response recieved."); + return data_swap; + } + + void set(size_t index, T value) { + DEBUG_PRINT("Data order initiated..."); + + data_swap = value; + order = index; + while (order != -1); + DEBUG_PRINT("Data order followed."); + return; + } +};