pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_Memory.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_MEMORY_H
00040 #define PLONK_MEMORY_H
00041 
00042 #include "../containers/plonk_Int24.h"
00043 
00044 class Memory
00045 {
00046 public:
00047     typedef PlankMemoryAllocateBytesFunction AllocateBytesFunction;
00048     typedef PlankMemoryFreeFunction FreeFunction;
00049     
00050     static Memory& global() throw()
00051     {
00052         static Memory memory (pl_MemoryGlobal());
00053         return memory;
00054     }
00055     
00056     Memory() throw()
00057     :   internal (pl_Memory_CreateAndInit())
00058     {
00059     }
00060     
00061     Memory (PlankMemory* const internalToUse) throw()
00062     :   internal (internalToUse)
00063     {
00064     }
00065     
00066     ~Memory()
00067     {        
00068         if (internal != pl_MemoryGlobal())
00069             pl_Memory_Destroy (internal);
00070     }
00071     
00072     inline void* allocateBytes (const UnsignedLong numBytes) throw()
00073     {
00074         void* const ptr = pl_Memory_AllocateBytes (internal, numBytes);
00075         plonk_assert (ptr != 0);
00076         return ptr;
00077     }
00078     
00079     inline void free (void* ptr) throw()
00080     {
00081         const ResultCode result = pl_Memory_Free (internal, ptr);
00082         plonk_assert (result == PlankResult_OK);
00083 #ifndef PLONK_DEBUG
00084         (void)result;
00085 #endif
00086     }
00087     
00088     template<class Type>
00089     void setUserData (Type* userData) throw()
00090     {
00091         const ResultCode result = pl_Memory_SetUserData (internal, userData);
00092         plonk_assert (result == PlankResult_OK);
00093 #ifndef PLONK_DEBUG
00094         (void)result;
00095 #endif
00096     }
00097     
00098     template<class Type>
00099     void getUserData (Type& userData) throw()
00100     {
00101         userData = *static_cast<Type*> (pl_Memory_GetUserData (internal));
00102     }
00103     
00104     inline void* getUserData() throw()
00105     {
00106         return pl_Memory_GetUserData (internal);
00107     }
00108     
00109     void resetUserData() throw()
00110     {
00111         setUserData (internal);
00112     }
00113 
00114     void setFunctions (AllocateBytesFunction allocateFunction, FreeFunction freeFunction) throw()
00115     {
00116         const ResultCode result = pl_Memory_SetFunctions (internal, allocateFunction, freeFunction);
00117         plonk_assert (result == PlankResult_OK);
00118 #ifndef PLONK_DEBUG
00119         (void)result;
00120 #endif        
00121     }
00122     
00123     void resetFunctions() throw()
00124     {
00125         setFunctions (0, 0);
00126     }
00127     
00128     static inline void zero (void* const ptr, const UnsignedLong numBytes) throw()
00129     {
00130         const ResultCode result = pl_MemoryZero (ptr, numBytes);
00131         plonk_assert (result == PlankResult_OK);
00132 #ifndef PLONK_DEBUG
00133         (void)result;
00134 #endif
00135     }
00136     
00137     template<class Type>
00138     static inline void zero (Type& object) throw()
00139     {
00140         Memory::zero (&object, sizeof (Type));
00141     }
00142     
00143     static inline void copy (void* const dst, const void* const src, const UnsignedLong numBytes) throw()
00144     {
00145         const ResultCode result = pl_MemoryCopy (dst, src, numBytes);
00146         plonk_assert (result == PlankResult_OK);
00147 #ifndef PLONK_DEBUG
00148         (void)result;
00149 #endif
00150     }
00151 
00152     static FreeFunction defaultFree;
00153     static AllocateBytesFunction defaultAllocateBytes;
00154     
00155 private:
00156     PlankMemory* const internal;
00157     
00158     Memory (Memory const&);
00159     Memory& operator= (Memory const&);
00160 };
00161 
00162 //Memory::FreeFunction Memory::defaultFree = pl_Memory_DefaultFree;
00163 //Memory::AllocateBytesFunction Memory::defaultAllocateBytes = pl_Memory_DefaultAllocateBytes;
00164 
00165 //------------------------------------------------------------------------------
00166 
00171 class ObjectMemoryBase
00172 {
00173 public:
00174     ObjectMemoryBase (Memory& m) throw() : memory (m) { }
00175     virtual ~ObjectMemoryBase() { }
00176     
00178     virtual void init() = 0; 
00179 
00180     inline Memory& getMemory() throw() { return memory; }
00181     
00182 private:
00183     Memory& memory;
00184     
00185     ObjectMemoryBase();
00186     ObjectMemoryBase& operator= (const ObjectMemoryBase&);
00187 };
00188 
00189 //template<class ObjectMemoryType>
00190 //class ObjectMemory
00191 //{
00192 //public:
00193 //    static ObjectMemoryBase* create (Memory& memory = Memory::global()) throw()
00194 //    {
00195 //        return new ObjectMemoryType (memory);
00196 //    }
00197 //};
00198 
00199 class ObjectMemoryDefault : public ObjectMemoryBase
00200 {
00201 public:     
00202     ObjectMemoryDefault (Memory& m) throw() 
00203     :   ObjectMemoryBase (m)
00204     { 
00205         getMemory().resetUserData(); 
00206         getMemory().resetFunctions(); 
00207     }
00208     
00209     void init() throw() { }
00210 };
00211 
00212 //------------------------------------------------------------------------------
00213 
00216 template<class Type>
00217 class ArrayAllocator
00218 {
00219 public:
00220     static Type* allocate (const int numItems) throw()  { return new Type[numItems]; }
00221     static void free (Type* const ptr) throw()          { delete [] ptr; }
00222 };
00223 
00225 template<class Type>
00226 class ArrayAllocator<Type*>
00227 {
00228 public:
00229     static Type** allocate (const int numItems) throw() { return static_cast<Type**> (Memory::global().allocateBytes (numItems * sizeof (Type*))); }    
00230     static void free (Type** const ptr) throw()         { Memory::global().free (static_cast<void*> (ptr)); }
00231 };
00232 
00234 template<class Type>
00235 class ArrayAllocatorBuiltIn
00236 {
00237 public:
00238     static Type* allocate (const int numItems) throw()  { return static_cast<Type*> (Memory::global().allocateBytes (numItems * sizeof (Type))); }    
00239     static void free (Type* const ptr) throw()          { Memory::global().free (static_cast<void*> (ptr)); }
00240 };
00241 
00242 // specialisations for the built-in numerical array types
00243 template<> class ArrayAllocator<Float>         : public ArrayAllocatorBuiltIn<Float>         { public: typedef Float Type; };
00244 template<> class ArrayAllocator<Double>        : public ArrayAllocatorBuiltIn<Double>        { public: typedef Double Type; };
00245 template<> class ArrayAllocator<Int>           : public ArrayAllocatorBuiltIn<Int>           { public: typedef Int Type; };
00246 template<> class ArrayAllocator<Int24>         : public ArrayAllocatorBuiltIn<Int24>         { public: typedef Int24 Type; };//??
00247 template<> class ArrayAllocator<UnsignedInt>   : public ArrayAllocatorBuiltIn<UnsignedInt>   { public: typedef UnsignedInt Type; };
00248 template<> class ArrayAllocator<Short>         : public ArrayAllocatorBuiltIn<Short>         { public: typedef Short Type; };
00249 template<> class ArrayAllocator<UnsignedShort> : public ArrayAllocatorBuiltIn<UnsignedShort> { public: typedef UnsignedShort Type; };
00250 template<> class ArrayAllocator<Long>          : public ArrayAllocatorBuiltIn<Long>          { public: typedef Long Type; };
00251 template<> class ArrayAllocator<UnsignedLong>  : public ArrayAllocatorBuiltIn<UnsignedLong>  { public: typedef UnsignedLong Type; };
00252 
00253 #if ! (PLANK_WIN && PLANK_64BIT)
00254 template<> class ArrayAllocator<LongLong>         : public ArrayAllocatorBuiltIn<LongLong>         { public: typedef LongLong Type; };
00255 template<> class ArrayAllocator<UnsignedLongLong> : public ArrayAllocatorBuiltIn<UnsignedLongLong> { public: typedef UnsignedLongLong Type; };
00256 #endif
00257 
00258 template<> class ArrayAllocator<Char>          : public ArrayAllocatorBuiltIn<Char>          { public: typedef Char Type; };
00259 template<> class ArrayAllocator<UnsignedChar>  : public ArrayAllocatorBuiltIn<UnsignedChar>  { public: typedef UnsignedChar Type; };
00260 template<> class ArrayAllocator<Bool>          : public ArrayAllocatorBuiltIn<Bool>          { public: typedef Bool Type; };
00261 
00262 
00263 
00264 
00265 
00266 
00267 #endif // PLONK_MEMORY_H
 All Classes Functions Typedefs Enumerations Enumerator Properties