TuttleOFX  1
TuttleOFX/libraries/tuttle/src/tuttle/plugin/memory/OfxAllocator.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLE_PLUGIN_OFXALLOCATOR_HPP_
00002 #define _TUTTLE_PLUGIN_OFXALLOCATOR_HPP_
00003 
00004 #include <tuttle/plugin/global.hpp>
00005 
00006 #include <ofxsMemory.h>
00007 
00008 #include <vector>
00009 #include <list>
00010 #include <string>
00011 #include <iostream>
00012 #include <sstream>
00013 #include <limits>
00014 namespace tuttle {
00015 namespace plugin {
00016 
00017 template <typename T>
00018 class OfxAllocator
00019 {
00020 static int size_all;
00021 
00022 public:
00023         typedef T Type;
00024         typedef size_t size_type;
00025         typedef ptrdiff_t difference_type;
00026         typedef T* pointer;
00027         typedef const T* const_pointer;
00028         typedef T& reference;
00029         typedef const T& const_reference;
00030         typedef T value_type;
00031 
00032 public:
00033         OfxAllocator() {}
00034         OfxAllocator( const OfxAllocator& ) {}
00035         template <class U>
00036         OfxAllocator( const OfxAllocator<U>& ) {}
00037 
00038 public:
00039         inline pointer allocate( const size_type n, const void* = 0 )
00040         {
00041                 ++size_all;
00042                 //TUTTLE_TLOG( TUTTLE_TRACE, "Use OfxAllocator to allocate" );
00043                 T* t = static_cast<T*>( OFX::memory::allocate( n * sizeof( T ) /*, ImageEffect* handle = 0*/ ) );
00044                 //              T* t = (T*) malloc( n * sizeof(T) );
00045                 //TUTTLE_TLOG( TUTTLE_TRACE, "allocate done (address:" << t << ") (+" << n << ") " << size_all );
00046                 return t;
00047         }
00048 
00049         inline void deallocate( void* ptr, size_type )
00050         {
00051                 --size_all;
00052                 //TUTTLE_TLOG( TUTTLE_TRACE, "Use OfxAllocator to deallocate (address:" << ptr << ") (-)" << size_all );
00053                 OFX::memory::free( ptr );
00054                 //free( ptr );
00055                 //TUTTLE_TLOG( TUTTLE_TRACE, "deallocate done." );
00056         }
00057 
00058         inline void construct( pointer p, const T& val ) { new ( (T*) p )T( val ); }
00059 
00060         inline void destroy( pointer p ) { p->~T(); }
00061 
00062         template <class U>
00063         struct rebind
00064         {
00065                 typedef OfxAllocator<U> other;
00066         };
00067 
00068         inline OfxAllocator<T>& operator=( const OfxAllocator<T>& ) { return *this; }
00069 
00070         template <class U>
00071         inline OfxAllocator<T>& operator=( const OfxAllocator<U>& ) { return *this; }
00072 
00073         inline pointer       address( reference x ) const       { return &x; }
00074         inline const_pointer address( const_reference x ) const { return &x; }
00075 
00076         inline size_type max_size() const { return std::numeric_limits<size_type>::max() / sizeof(T); }
00077 
00078         inline bool operator==( OfxAllocator<T> const& ) { return true; }
00079         inline bool operator!=( OfxAllocator<T> const& a ) { return !operator==(a); }
00080 
00081 };
00082 
00083 template <typename T>
00084 int OfxAllocator<T>::size_all = 0;
00085 
00086 }
00087 }
00088 
00089 #endif
00090