Added custom memory manager. Actually added the darn files this time...

This commit is contained in:
Joshua Jones 2010-03-08 05:27:02 +00:00
parent befd5f5821
commit d5c5846d71
9 changed files with 277 additions and 4 deletions

View File

@ -30,12 +30,12 @@ namespace cAudio
{
//A bit hackish, but if the user doesn't want streaming, make this a memory source
int length = source->getSize();
char* tempbuf = new char[length];
char* tempbuf = (char*)CAUDIO_MALLOC(length);
if(tempbuf)
{
source->read(tempbuf, length);
IDataSource* memSource = CAUDIO_NEW cMemorySource(tempbuf, length, true);
delete[] tempbuf;
CAUDIO_FREE(tempbuf);
if(memSource && memSource->isValid())
{

77
Headers/cMemoryOverride.h Normal file
View File

@ -0,0 +1,77 @@
#ifndef CMEMORYOVERRIDE_H_INCLUDED
#define CMEMORYOVERRIDE_H_INCLUDED
#include <new>
#include "../include/cAudioMemory.h"
#ifdef CAUDIO_DEBUG
#define CAUDIO_NEW new (__FILE__, __LINE__, __FUNCTION__)
#define CAUDIO_DELETE delete
#define CAUDIO_MALLOC(size) cAudio::getMemoryProvider()->Allocate(size, __FILE__, __LINE__, __FUNCTION__)
#define CAUDIO_FREE(pointer) cAudio::getMemoryProvider()->Free((void*)pointer)
#else
#define CAUDIO_NEW new
#define CAUDIO_DELETE delete
#define CAUDIO_MALLOC(size) cAudio::getMemoryProvider()->Allocate(size, NULL, -1, NULL)
#define CAUDIO_FREE(pointer) cAudio::getMemoryProvider()->Free((void*)pointer)
#endif
namespace cAudio
{
class cMemoryOverride
{
public:
void* operator new(size_t size, const char* file, int line, const char* function)
{
return cAudio::getMemoryProvider()->Allocate(size, file, line, function);
}
void* operator new(size_t size)
{
return cAudio::getMemoryProvider()->Allocate(size, NULL, -1, NULL);
}
void* operator new(size_t size, void* pointer)
{
(void) size;
return pointer;
}
void* operator new[] ( size_t size, const char* file, int line, const char* function )
{
return cAudio::getMemoryProvider()->Allocate(size, file, line, function);
}
void* operator new[] ( size_t size )
{
return cAudio::getMemoryProvider()->Allocate(size, NULL, -1, NULL);
}
void operator delete( void* pointer )
{
cAudio::getMemoryProvider()->Free(pointer);
}
void operator delete( void* pointer, void* )
{
cAudio::getMemoryProvider()->Free(pointer);
}
void operator delete( void* pointer, const char* , int , const char* )
{
cAudio::getMemoryProvider()->Free(pointer);
}
void operator delete[] ( void* pointer )
{
cAudio::getMemoryProvider()->Free(pointer);
}
void operator delete[] ( void* pointer, const char* , int , const char* )
{
cAudio::getMemoryProvider()->Free(pointer);
}
};
};
#endif //! CMEMORYOVERRIDE_H_INCLUDED

110
Headers/cSTLAllocator.h Normal file
View File

@ -0,0 +1,110 @@
#ifndef CSTLALLOCATOR_H_INCLUDED
#define CSTLALLOCATOR_H_INCLUDED
#include "../include/cAudioDefines.h"
#include "../Headers/cMemoryOverride.h"
namespace cAudio
{
template <typename T> class cSTLAllocator;
// specialize for void:
template <> class cSTLAllocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
// reference to void members are impossible.
typedef void value_type;
template <class U>
struct rebind
{
typedef cSTLAllocator<U> other;
};
};
template <typename T> class cSTLAllocator
{
public:
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;
template<typename U>
struct rebind
{
typedef cSTLAllocator<U> other;
};
inline explicit cSTLAllocator()
{ }
virtual ~cSTLAllocator()
{ }
inline cSTLAllocator( cSTLAllocator const& )
{ }
template <typename U>
inline cSTLAllocator( cSTLAllocator<U> const& )
{ }
pointer address(reference x) const
{
return &x;
}
const_pointer address(const_reference x) const
{
return &x;
}
inline pointer allocate( size_type count, typename std::allocator<void>::const_pointer ptr = 0 )
{
(void)ptr;
register size_type size = count*sizeof( T );
pointer p = static_cast<pointer>(CAUDIO_MALLOC(size));
return p;
}
inline void deallocate( pointer p, size_type size )
{
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();
}
};
template <typename T1, typename T2>
bool operator==(const cSTLAllocator<T1>&, const cSTLAllocator<T2>&)
{
return true;
}
template <typename T1, typename T2>
bool operator!=(const cSTLAllocator<T1>&, const cSTLAllocator<T2>&)
{
return false;
}
};
#endif //! CSTLALLOCATOR_H_INCLUDED

View File

@ -0,0 +1,17 @@
#ifndef CSTANDARDMEMORYPROVIDER_H_INCLUDED
#define CSTANDARDMEMORYPROVIDER_H_INCLUDED
#include "../include/IMemoryProvider.h"
namespace cAudio
{
class cStandardMemoryProvider : public IMemoryProvider
{
public:
virtual void* Allocate(size_t size, const char* filename, int line, const char* function);
virtual void Free(void* pointer);
virtual size_t getMaxAllocationSize();
};
};
#endif //! CSTANDARDMEMORYPROVIDER_H_INCLUDED

15
Source/cAudioMemory.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "../include/cAudioMemory.h"
#include "../Headers/cStandardMemoryProvider.h"
namespace cAudio
{
CAUDIO_API IMemoryProvider* getMemoryProvider()
{
#ifdef CAUDIO_MEMORY_USE_STD
static cStandardMemoryProvider memoryProvider;
#endif
//To use your own memory provider, add it in here and set its name to memoryProvider
return &memoryProvider;
}
};

View File

@ -16,7 +16,7 @@ cMemorySource::cMemorySource(const void* data, int size, bool copy) : Data(NULL)
Size = size;
if(copy)
{
Data = new char[Size];
Data = (char*)CAUDIO_MALLOC(Size);
if(Data)
memcpy(Data, data, Size);
}
@ -31,7 +31,7 @@ cMemorySource::cMemorySource(const void* data, int size, bool copy) : Data(NULL)
cMemorySource::~cMemorySource()
{
delete[] Data;
CAUDIO_FREE(Data);
}
bool cMemorySource::isValid()

View File

@ -0,0 +1,24 @@
#include "../Headers/cStandardMemoryProvider.h"
#include <stdlib.h>
#include <limits>
namespace cAudio
{
void* cStandardMemoryProvider::Allocate(size_t size, const char* filename, int line, const char* function)
{
return malloc(size);
}
void cStandardMemoryProvider::Free(void* pointer)
{
if(pointer)
{
free(pointer);
}
}
size_t cStandardMemoryProvider::getMaxAllocationSize()
{
return std::numeric_limits<size_t>::max();
}
};

18
include/IMemoryProvider.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef IMEMORYPROVIDER_H_INCLUDED
#define IMEMORYPROVIDER_H_INCLUDED
#include "../include/cAudioDefines.h"
namespace cAudio
{
//! Interface for a class that allocates and frees memory used by cAudio.
class IMemoryProvider
{
public:
virtual void* Allocate(size_t size, const char* filename, int line, const char* function) = 0;
virtual void Free(void* pointer) = 0;
virtual size_t getMaxAllocationSize() = 0;
};
};
#endif //! IMEMORYPROVIDER_H_INCLUDED

12
include/cAudioMemory.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef CAUDIOMEMORY_H_INCLUDED
#define CAUDIOMEMORY_H_INCLUDED
#include "cAudioDefines.h"
#include "IMemoryProvider.h"
namespace cAudio
{
CAUDIO_API IMemoryProvider* getMemoryProvider();
};
#endif //! CAUDIOMEMORY_H_INCLUDED