Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 29, 2010, 9:37:38 PM (14 years ago)
Author:
landauf
Message:

Added new utility SmallObjectAllocator.
Merged counter and destroyer in SharedPtr and allocate them with SmallObjectAllocator

Location:
code/branches/consolecommands3/src/libraries/util
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/consolecommands3/src/libraries/util/CMakeLists.txt

    r7207 r7264  
    3232  OutputHandler.cc
    3333  ScopedSingletonManager.cc
     34  SharedPtr.cc
    3435  SignalHandler.cc
    3536  Sleep.cc
     37  SmallObjectAllocator.cc
    3638  SubString.cc
    3739COMPILATION_END
  • code/branches/consolecommands3/src/libraries/util/SharedPtr.h

    r7212 r7264  
    3131
    3232#include "UtilPrereqs.h"
     33
    3334#include <algorithm>
    3435#include <cassert>
    3536
     37#include "SmallObjectAllocator.h"
     38
    3639namespace orxonox
    3740{
    38     class SharedPtrDestroyer
     41    class SharedCounter
    3942    {
    4043        public:
     44            SharedCounter() : count_(1) {}
    4145            virtual void destroy() = 0;
     46
     47            int count_;
    4248    };
    4349
    4450    template <class T>
    45     class SharedPtrDestroyerImpl : public SharedPtrDestroyer
     51    class SharedCounterImpl : public SharedCounter
    4652    {
    4753        public:
    48             SharedPtrDestroyerImpl(T* pointer) : pointer_(pointer) {}
     54            SharedCounterImpl(T* pointer) : pointer_(pointer) {}
    4955
    5056            void destroy()
     
    5763    };
    5864
     65    _UtilExport SmallObjectAllocator& createSharedCounterPool();
     66
     67    FORCEINLINE SmallObjectAllocator& getSharedCounterPool()
     68    {
     69        static SmallObjectAllocator& instance = createSharedCounterPool();
     70        return instance;
     71    }
     72
    5973    template <class T>
    6074    class SharedPtr
     
    6478
    6579        public:
    66             inline SharedPtr() : pointer_(0), counter_(0), destroyer_(0)
     80            inline SharedPtr() : pointer_(0), counter_(0)
    6781            {
    6882            }
    6983
    70             inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0), destroyer_(0)
     84            inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0)
    7185            {
    7286                if (this->pointer_)
    7387                {
    74                     this->counter_ = new int(1);
    75                     this->destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_);
     88                    void* chunk = getSharedCounterPool().alloc();
     89                    this->counter_ = new (chunk) SharedCounterImpl<T>(this->pointer_);
    7690                }
    7791            }
    7892
    79             inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
     93            inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_)
    8094            {
    8195                if (this->pointer_)
    82                     ++(*this->counter_);
     96                    ++this->counter_->count_;
    8397            }
    8498
    8599            template <class O>
    86             inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_)
     100            inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_)
    87101            {
    88102                if (this->pointer_)
    89                     ++(*this->counter_);
     103                    ++this->counter_->count_;
    90104            }
    91105
     
    94108                if (this->pointer_)
    95109                {
    96                     --(*this->counter_);
     110                    --this->counter_->count_;
    97111
    98                     if (*this->counter_ == 0)
     112                    if (this->counter_->count_ == 0)
    99113                    {
    100                         this->destroyer_->destroy();
    101                         delete this->destroyer_;
    102                         delete this->counter_;
     114                        this->counter_->destroy();
     115                        getSharedCounterPool().free(this->counter_);
    103116                    }
    104117                }
     
    122135            {
    123136                O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast
    124                 return SharedPtr<O>(temp, this->counter_, this->destroyer_);
     137                return SharedPtr<O>(temp, this->counter_);
    125138            }
    126139
     
    151164                std::swap(this->pointer_, other.pointer_);
    152165                std::swap(this->counter_, other.counter_);
    153                 std::swap(this->destroyer_, other.destroyer_);
    154166            }
    155167
    156168        private:
    157             inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer)
     169            inline SharedPtr(T* pointer, SharedCounter* counter) : pointer_(pointer), counter_(counter)
    158170            {
    159171                if (this->pointer_)
    160                     ++(*this->counter_);
     172                    ++this->counter_->count_;
    161173            }
    162174
    163175            T* pointer_;
    164             int* counter_;
    165             SharedPtrDestroyer* destroyer_;
     176            SharedCounter* counter_;
    166177    };
    167178
Note: See TracChangeset for help on using the changeset viewer.