From c01870aef40f075349b08fe5f1838feb894a7bcf Mon Sep 17 00:00:00 2001
From: Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Date: Mon, 16 Nov 2015 11:13:56 +0100
Subject: [PATCH] started with implementation of flexible message pool

---
 server/include/pool.h | 65 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 server/include/pool.h

diff --git a/server/include/pool.h b/server/include/pool.h
new file mode 100644
index 000000000..19d4016f4
--- /dev/null
+++ b/server/include/pool.h
@@ -0,0 +1,65 @@
+/** Circular buffer
+ *
+ * Every path uses a circular buffer to save past messages.
+ *
+ * @file
+ * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
+ * @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
+ *   This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
+ *   Unauthorized copying of this file, via any medium is strictly prohibited. 
+ */
+
+#ifndef _POOL_H_
+#define _POOL_H_
+
+/* Forward declaration */
+struct msg;
+
+struct pool {
+	void *buffer;
+	
+	int last;
+	
+	size_t len;
+	size_t blocklen;
+};
+
+
+int pool_init(struct pool *p, size_t len, size_t blocklen)
+{
+	p->buffer = alloc(len * blocklen);
+	
+	p->first = NULL;
+	p->last = NULL;
+	
+	p->len = len;
+	p->blocklen = len;
+}
+
+void pool_destroy(struct pool *p)
+{
+	free(p->buffer);
+
+	p->len = 0;
+	p->blocklen = 0;
+}
+
+void * pool_get(struct pool *p, int index);
+{
+	/* Negative indizes are relative to p->last */
+	if (index < 0)
+		index = p->last - index;
+
+	/* Check boundaries */
+	if (index > p->last || p->last - index > p->len)
+		return NULL;
+	
+	return (char *) p->buffer + p->blocklen * (index % p->len);
+}
+
+void pool_put(struct pool *p, void *src)
+{
+	memcpy(p->buffer + p->blocklen * (p->last++ % p->len), src, p->blocklen);
+}
+
+#endif /* _POOL_H_ */
\ No newline at end of file