about summary refs log tree commit diff stats
path: root/src/common/shared_memory.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/shared_memory.h')
-rw-r--r--src/common/shared_memory.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/common/shared_memory.h b/src/common/shared_memory.h
new file mode 100644
index 0000000..70554e4
--- /dev/null
+++ b/src/common/shared_memory.h
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <pthread.h>
+#include <sstream>
+
+#define SHM_NAME "/hashtable_queue"
+#define QUEUE_SIZE 10
+
+#define MAX_KEY_SIZE 64
+#define MAX_VALUE_SIZE 128
+
+enum Operations { INSERT, DELETE, GET, PRINT };
+
+struct Request {
+    Operations type;
+    char key[MAX_KEY_SIZE];
+    char value[MAX_VALUE_SIZE];
+    char response[MAX_VALUE_SIZE];
+};
+
+struct SharedMemory {
+    Request request[QUEUE_SIZE];
+    pthread_mutex_t mutex;
+    pthread_cond_t cond_var;
+
+    int tail;
+    int head;
+    bool full;
+};
+
+template <typename T>
+std::string serialize(const T& data)
+{
+    std::ostringstream oss;
+    oss << data;
+    return oss.str();
+}
+
+template <typename T>
+T deserialize(const std::string& str)
+{
+    std::istringstream iss(str);
+    T data;
+    iss >> data;
+    return data;
+}