[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pingus-CVS] r3924 - trunk/pingus/src
From: |
grumbel at BerliOS |
Subject: |
[Pingus-CVS] r3924 - trunk/pingus/src |
Date: |
Mon, 28 Jul 2008 03:50:46 +0200 |
Author: grumbel
Date: 2008-07-28 03:50:41 +0200 (Mon, 28 Jul 2008)
New Revision: 3924
Added:
trunk/pingus/src/memory_pool.hpp
Log:
Added simple MemoryPool class
Added: trunk/pingus/src/memory_pool.hpp
===================================================================
--- trunk/pingus/src/memory_pool.hpp 2008-07-27 20:50:24 UTC (rev 3923)
+++ trunk/pingus/src/memory_pool.hpp 2008-07-28 01:50:41 UTC (rev 3924)
@@ -0,0 +1,100 @@
+// Pingus - A free Lemmings clone
+// Copyright (C) 1999 Ingo Ruhnke <address@hidden>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_MEMORY_POOL_HPP
+#define HEADER_MEMORY_POOL_HPP
+
+#include <assert.h>
+#include <vector>
+
+template<class T>
+class MemoryPool
+{
+private:
+ typedef std::vector<T*> Objects;
+ Objects objects;
+
+ typedef std::vector<char*> Chunks;
+ Chunks chunks;
+
+ size_t chunk_size;
+ int next_free;
+
+ char* allocate(size_t size)
+ {
+ assert(size <= chunk_size);
+
+ if (chunks.empty() ||
+ (next_free + size) > chunk_size)
+ {
+ char* chunk = new char[chunk_size];
+ chunks.push_back(chunk);
+ next_free = 0;
+ }
+
+ char* ptr = chunks.back() + next_free;
+ next_free += size;
+ return ptr;
+ }
+
+ T* keep(T* t)
+ {
+ objects.push_back(t);
+ return t;
+ }
+
+public:
+ MemoryPool(size_t chunk_size_ = 16384)
+ : chunk_size(chunk_size_),
+ next_free(0)
+ {
+
+ }
+
+ ~MemoryPool()
+ {
+ clear();
+ }
+
+ void clear()
+ {
+ for(typename Objects::reverse_iterator i = objects.rbegin(); i !=
objects.rend(); ++i)
+ (*i)->~T();
+
+ for(typename Chunks::reverse_iterator i = chunks.rbegin(); i !=
chunks.rend(); ++i)
+ delete[] *i;
+ }
+
+ template<class C>
+ C* create() { return keep(new (allocate(sizeof(C))) C()); }
+ template<class C, class Arg1>
+ C* create(const Arg1& arg1) { return keep(new (allocate(sizeof(C)))
C(arg1)); }
+ template<class C, class Arg1, class Arg2>
+ C* create(const Arg1& arg1, const Arg2& arg2) { return keep(new
(allocate(sizeof(C))) C(arg1, arg2)); }
+ template<class C, class Arg1, class Arg2, class Arg3>
+ C* create(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) { return
keep(new (allocate(sizeof(C))) C(arg1, arg2, arg3)); }
+ template<class C, class Arg1, class Arg2, class Arg3, class Arg4>
+ C* create(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4&
arg4) { return keep(new (allocate(sizeof(C))) C(arg1, arg2, arg3, arg4)); }
+
+private:
+ MemoryPool (const MemoryPool&);
+ MemoryPool& operator= (const MemoryPool&);
+};
+
+
+#endif
+
+/* EOF */
Property changes on: trunk/pingus/src/memory_pool.hpp
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pingus-CVS] r3924 - trunk/pingus/src,
grumbel at BerliOS <=