2010-03-08 06:27:02 +01:00
|
|
|
#ifndef CSTLALLOCATOR_H_INCLUDED
|
|
|
|
#define CSTLALLOCATOR_H_INCLUDED
|
|
|
|
|
|
|
|
#include "../include/cAudioDefines.h"
|
2010-03-14 23:58:03 +01:00
|
|
|
#include "../include/cAudioMemory.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2010-03-08 06:27:02 +01:00
|
|
|
|
|
|
|
namespace cAudio
|
|
|
|
{
|
2010-03-14 23:58:03 +01:00
|
|
|
#ifdef CAUDIO_REROUTE_STL_ALLOCATIONS
|
2010-03-16 02:11:08 +01:00
|
|
|
//! Reroutes allocations from STL containers into cAudio's memory system
|
2010-03-14 23:58:03 +01:00
|
|
|
template <typename T> class cSTLAllocator
|
2010-03-08 06:27:02 +01:00
|
|
|
{
|
|
|
|
public:
|
2010-03-14 23:58:03 +01:00
|
|
|
typedef T value_type;
|
|
|
|
typedef value_type* pointer;
|
|
|
|
typedef const value_type* const_pointer;
|
|
|
|
typedef value_type& reference;
|
|
|
|
typedef const value_type& const_reference;
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
2010-03-08 06:27:02 +01:00
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
struct rebind
|
|
|
|
{
|
|
|
|
typedef cSTLAllocator<U> other;
|
|
|
|
};
|
|
|
|
|
2010-03-14 01:01:23 +01:00
|
|
|
cSTLAllocator()
|
2010-03-08 06:27:02 +01:00
|
|
|
{ }
|
|
|
|
|
2010-03-14 01:01:23 +01:00
|
|
|
~cSTLAllocator() throw()
|
2010-03-08 06:27:02 +01:00
|
|
|
{ }
|
|
|
|
|
2010-03-14 01:01:23 +01:00
|
|
|
cSTLAllocator( const cSTLAllocator& ) throw()
|
2010-03-08 06:27:02 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
template <typename U>
|
2010-03-14 01:01:23 +01:00
|
|
|
cSTLAllocator( const cSTLAllocator<U>& ) throw()
|
2010-03-08 06:27:02 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
pointer address(reference x) const
|
|
|
|
{
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
|
|
|
const_pointer address(const_reference x) const
|
|
|
|
{
|
|
|
|
return &x;
|
|
|
|
}
|
|
|
|
|
2010-03-14 23:58:03 +01:00
|
|
|
pointer allocate( size_type count, typename std::allocator<void>::const_pointer ptr = 0 )
|
2010-03-08 06:27:02 +01:00
|
|
|
{
|
|
|
|
(void)ptr;
|
|
|
|
register size_type size = count*sizeof( T );
|
|
|
|
pointer p = static_cast<pointer>(CAUDIO_MALLOC(size));
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-03-14 23:58:03 +01:00
|
|
|
void deallocate( pointer p, size_type size )
|
2010-03-08 06:27:02 +01:00
|
|
|
{
|
|
|
|
CAUDIO_FREE(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type max_size() const throw()
|
|
|
|
{
|
|
|
|
return cAudio::getMemoryProvider()->getMaxAllocationSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void construct(pointer p, const T& val)
|
|
|
|
{
|
|
|
|
// call placement new
|
|
|
|
new(static_cast<void*>(p)) T(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy(pointer p)
|
|
|
|
{
|
|
|
|
p->~T();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-14 01:01:23 +01:00
|
|
|
template<> class cSTLAllocator<void>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef size_t size_type;
|
|
|
|
typedef ptrdiff_t difference_type;
|
|
|
|
typedef void* pointer;
|
|
|
|
typedef const void* const_pointer;
|
|
|
|
typedef void value_type;
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
struct rebind
|
|
|
|
{
|
|
|
|
typedef cSTLAllocator<U> other;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline bool operator==(const cSTLAllocator<T>&, const cSTLAllocator<T>&)
|
2010-03-08 06:27:02 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-14 01:01:23 +01:00
|
|
|
template <typename T>
|
|
|
|
inline bool operator!=(const cSTLAllocator<T>&, const cSTLAllocator<T>&)
|
2010-03-08 06:27:02 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-14 23:58:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CAUDIO_REROUTE_STL_ALLOCATIONS
|
|
|
|
typedef std::basic_string< char, std::char_traits<char>, cSTLAllocator<char> > cAudioString;
|
|
|
|
template<typename T1, typename T2> struct cAudioMap { typedef std::map< T1, T2, std::less< T1 >, cSTLAllocator< std::pair< T1, T2 > > > Type; };
|
|
|
|
template<typename T> struct cAudioSet { typedef std::set< T, std::less< T >, cSTLAllocator< T > > Type; };
|
|
|
|
template<typename T> struct cAudioList { typedef std::list< T, cSTLAllocator< T > > Type; };
|
|
|
|
template<typename T> struct cAudioVector { typedef std::vector< T, cSTLAllocator< T > > Type; };
|
|
|
|
#else
|
|
|
|
typedef std::string cAudioString;
|
|
|
|
template<typename T1, typename T2> struct cAudioMap { typedef std::map< T1, T2> Type; };
|
|
|
|
template<typename T> struct cAudioSet { typedef std::set< T > Type; };
|
|
|
|
template<typename T> struct cAudioList { typedef std::list< T > Type; };
|
|
|
|
template<typename T> struct cAudioVector { typedef std::vector< T > Type; };
|
|
|
|
#endif
|
2010-03-08 06:27:02 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //! CSTLALLOCATOR_H_INCLUDED
|