[Orxonox-commit 2559] r7264 - code/branches/consolecommands3/src/libraries/util

landauf at orxonox.net landauf at orxonox.net
Sun Aug 29 21:37:38 CEST 2010


Author: landauf
Date: 2010-08-29 21:37:38 +0200 (Sun, 29 Aug 2010)
New Revision: 7264

Added:
   code/branches/consolecommands3/src/libraries/util/SharedPtr.cc
   code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.cc
   code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.h
Modified:
   code/branches/consolecommands3/src/libraries/util/CMakeLists.txt
   code/branches/consolecommands3/src/libraries/util/SharedPtr.h
Log:
Added new utility SmallObjectAllocator.
Merged counter and destroyer in SharedPtr and allocate them with SmallObjectAllocator

Modified: code/branches/consolecommands3/src/libraries/util/CMakeLists.txt
===================================================================
--- code/branches/consolecommands3/src/libraries/util/CMakeLists.txt	2010-08-29 17:11:36 UTC (rev 7263)
+++ code/branches/consolecommands3/src/libraries/util/CMakeLists.txt	2010-08-29 19:37:38 UTC (rev 7264)
@@ -31,8 +31,10 @@
   ExprParser.cc
   OutputHandler.cc
   ScopedSingletonManager.cc
+  SharedPtr.cc
   SignalHandler.cc
   Sleep.cc
+  SmallObjectAllocator.cc
   SubString.cc
 COMPILATION_END
 )

Added: code/branches/consolecommands3/src/libraries/util/SharedPtr.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/util/SharedPtr.cc	                        (rev 0)
+++ code/branches/consolecommands3/src/libraries/util/SharedPtr.cc	2010-08-29 19:37:38 UTC (rev 7264)
@@ -0,0 +1,38 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   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 2
+ *   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, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "SharedPtr.h"
+
+namespace orxonox
+{
+    SmallObjectAllocator& createSharedCounterPool()
+    {
+        static SmallObjectAllocator instance(sizeof(SharedCounterImpl<void>));
+        return instance;
+    }
+}


Property changes on: code/branches/consolecommands3/src/libraries/util/SharedPtr.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: code/branches/consolecommands3/src/libraries/util/SharedPtr.h
===================================================================
--- code/branches/consolecommands3/src/libraries/util/SharedPtr.h	2010-08-29 17:11:36 UTC (rev 7263)
+++ code/branches/consolecommands3/src/libraries/util/SharedPtr.h	2010-08-29 19:37:38 UTC (rev 7264)
@@ -30,22 +30,28 @@
 #define _SharedPtr_H__
 
 #include "UtilPrereqs.h"
+
 #include <algorithm>
 #include <cassert>
 
+#include "SmallObjectAllocator.h"
+
 namespace orxonox
 {
-    class SharedPtrDestroyer
+    class SharedCounter
     {
         public:
+            SharedCounter() : count_(1) {}
             virtual void destroy() = 0;
+
+            int count_;
     };
 
     template <class T>
-    class SharedPtrDestroyerImpl : public SharedPtrDestroyer
+    class SharedCounterImpl : public SharedCounter
     {
         public:
-            SharedPtrDestroyerImpl(T* pointer) : pointer_(pointer) {}
+            SharedCounterImpl(T* pointer) : pointer_(pointer) {}
 
             void destroy()
             {
@@ -56,6 +62,14 @@
             T* pointer_;
     };
 
+    _UtilExport SmallObjectAllocator& createSharedCounterPool();
+
+    FORCEINLINE SmallObjectAllocator& getSharedCounterPool()
+    {
+        static SmallObjectAllocator& instance = createSharedCounterPool();
+        return instance;
+    }
+
     template <class T>
     class SharedPtr
     {
@@ -63,43 +77,42 @@
         friend class SharedPtr;
 
         public:
-            inline SharedPtr() : pointer_(0), counter_(0), destroyer_(0)
+            inline SharedPtr() : pointer_(0), counter_(0)
             {
             }
 
-            inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0), destroyer_(0)
+            inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0)
             {
                 if (this->pointer_)
                 {
-                    this->counter_ = new int(1);
-                    this->destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_);
+                    void* chunk = getSharedCounterPool().alloc();
+                    this->counter_ = new (chunk) SharedCounterImpl<T>(this->pointer_);
                 }
             }
 
-            inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
+            inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_)
             {
                 if (this->pointer_)
-                    ++(*this->counter_);
+                    ++this->counter_->count_;
             }
 
             template <class O>
-            inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
+            inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_)
             {
                 if (this->pointer_)
-                    ++(*this->counter_);
+                    ++this->counter_->count_;
             }
 
             inline ~SharedPtr()
             {
                 if (this->pointer_)
                 {
-                    --(*this->counter_);
+                    --this->counter_->count_;
 
-                    if (*this->counter_ == 0)
+                    if (this->counter_->count_ == 0)
                     {
-                        this->destroyer_->destroy();
-                        delete this->destroyer_;
-                        delete this->counter_;
+                        this->counter_->destroy();
+                        getSharedCounterPool().free(this->counter_);
                     }
                 }
             }
@@ -121,7 +134,7 @@
             inline SharedPtr<O> cast() const
             {
                 O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast
-                return SharedPtr<O>(temp, this->counter_, this->destroyer_);
+                return SharedPtr<O>(temp, this->counter_);
             }
 
             inline T* operator->() const
@@ -150,19 +163,17 @@
             {
                 std::swap(this->pointer_, other.pointer_);
                 std::swap(this->counter_, other.counter_);
-                std::swap(this->destroyer_, other.destroyer_);
             }
 
         private:
-            inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer)
+            inline SharedPtr(T* pointer, SharedCounter* counter) : pointer_(pointer), counter_(counter)
             {
                 if (this->pointer_)
-                    ++(*this->counter_);
+                    ++this->counter_->count_;
             }
 
             T* pointer_;
-            int* counter_;
-            SharedPtrDestroyer* destroyer_;
+            SharedCounter* counter_;
     };
 
     template <class T, class Parent>

Added: code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.cc
===================================================================
--- code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.cc	                        (rev 0)
+++ code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.cc	2010-08-29 19:37:38 UTC (rev 7264)
@@ -0,0 +1,87 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   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 2
+ *   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, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "SmallObjectAllocator.h"
+
+namespace orxonox
+{
+    SmallObjectAllocator::SmallObjectAllocator(size_t objectSize, size_t numObjects)
+    {
+        this->objectSize_ = std::max(objectSize, sizeof(Chunk));
+        this->numObjects_ = numObjects;
+        this->first_ = 0;
+    }
+
+    SmallObjectAllocator::~SmallObjectAllocator()
+    {
+        for (std::vector<char*>::iterator it = this->blocks_.begin(); it != this->blocks_.end(); ++it)
+            delete[] *it;
+    }
+
+    /* static */ void SmallObjectAllocator::setNext(void* chunk, void* next)
+    {
+        static_cast<Chunk*>(chunk)->next_ = static_cast<Chunk*>(next);
+    }
+
+    /* static */ void* SmallObjectAllocator::getNext(void* chunk)
+    {
+        return static_cast<Chunk*>(chunk)->next_;
+    }
+
+    void* SmallObjectAllocator::alloc()
+    {
+        void* chunk = this->first_;
+
+        if (chunk)
+        {
+            this->first_ = getNext(chunk);
+        }
+        else
+        {
+            char* block = new char[this->objectSize_ * this->numObjects_];
+            this->blocks_.push_back(block);
+
+            for (size_t i = 1; i < this->numObjects_ - 1; ++i)
+                setNext(block + i * this->objectSize_, block + (i + 1) * this->objectSize_);
+
+            setNext(block + (this->numObjects_ - 1) * this->objectSize_, 0);
+
+            this->first_ = block + this->objectSize_;
+
+            chunk = block;
+        }
+
+        return chunk;
+    }
+
+    void SmallObjectAllocator::free(void* chunk)
+    {
+        setNext(chunk, this->first_);
+        this->first_ = chunk;
+    }
+}


Property changes on: code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.h
===================================================================
--- code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.h	                        (rev 0)
+++ code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.h	2010-08-29 19:37:38 UTC (rev 7264)
@@ -0,0 +1,63 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   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 2
+ *   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, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _SmallObjectAllocator_H__
+#define _SmallObjectAllocator_H__
+
+#include "UtilPrereqs.h"
+#include <vector>
+
+namespace orxonox
+{
+    class _UtilExport SmallObjectAllocator
+    {
+        struct Chunk
+        {
+            Chunk* next_;
+        };
+
+        public:
+            SmallObjectAllocator(size_t objectSize, size_t numObjects = 64);
+            ~SmallObjectAllocator();
+
+            void* alloc();
+            void free(void* chunk);
+
+        private:
+            static void setNext(void* chunk, void* next);
+            static void* getNext(void* chunk);
+
+            void* first_;
+            size_t objectSize_;
+            size_t numObjects_;
+
+            std::vector<char*> blocks_;
+    };
+}
+
+#endif /* _SmallObjectAllocator_H__ */


Property changes on: code/branches/consolecommands3/src/libraries/util/SmallObjectAllocator.h
___________________________________________________________________
Added: svn:eol-style
   + native




More information about the Orxonox-commit mailing list