From 09e89c867c8dc14adee298176a0a4c72d8f32245 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Tue, 7 Aug 2012 16:54:36 +0200 Subject: Split out reusable parts into the laks project. --- os/pool.h | 146 -------------------------------------------------------------- 1 file changed, 146 deletions(-) delete mode 100644 os/pool.h (limited to 'os/pool.h') diff --git a/os/pool.h b/os/pool.h deleted file mode 100644 index 1cb0a71..0000000 --- a/os/pool.h +++ /dev/null @@ -1,146 +0,0 @@ -#ifndef POOL_H -#define POOL_H - -#include "stdint.h" - -template -class BasePool { - public: - struct Element { - unsigned int use_count; - BasePool* pool; - - char data[sizeof(T)]; - }; - - virtual void free(Element* e) = 0; -}; - -template -class P { - private: - typedef typename BasePool::Element Element; - - Element* e; - - void inc() { - e->use_count++; - } - - void dec() { - e->use_count--; - if(!e->use_count) { - T* p = (T*)e->data; - p->~T(); - e->pool->free(e); - } - } - - public: - P() : e(0) {} - - explicit P(Element* ep) : e(ep) { - inc(); - } - - P(const P& p) : e(p.e) { - inc(); - } - - ~P() { - if(e) { - dec(); - } - } - - void operator=(const P& p) { - if(e) { - dec(); - } - - e = p.e; - - if(e) { - inc(); - } - } - - void reset() { - if(e) { - dec(); - } - - e = 0; - } - - T* operator->() { - return (T*)e->data; - } - - T* operator*() { - return (T*)e->data; - } - - operator bool() { - return bool(e); - } -}; - -template -class Pool : public BasePool { - private: - typedef typename BasePool::Element Element; - - union Node { - Element e; - Node* next; - }; - - Node elements[size]; - - Node* next_free; - - void free(Element* e) { - Node* n = (Node*)e; - - n->next = next_free; - next_free = n; - } - - Element* alloc() { - if(!next_free) { - return 0; - } - - Element* e = &next_free->e; - next_free = next_free->next; - - e->use_count = 0; - e->pool = this; - - return e; - } - - public: - Pool() : next_free(0) { - for(unsigned int i = 0; i < size; i++) { - free(&elements[i].e); - } - } - - P create() { - Element* e = alloc(); - - if(!e) { - return P(); - } - - new (e->data) T; - - return P(e); - } -}; - -void* operator new(unsigned int, char* buf); - -#endif -- cgit v1.2.3