diff --git a/Makefile b/Makefile
index b3c84ec9b..cb358aec8 100644
--- a/Makefile
+++ b/Makefile
@@ -1,15 +1,15 @@
ifeq ($(DEBUGBUILD), yes)
- DEBUGFLAGS = -g -DDEBUG -DEE_DEBUG
+ DEBUGFLAGS = -g -DDEBUG -DEE_DEBUG -DEE_MEMORY_MANAGER
else
DEBUGFLAGS = -O2 -s -DNDEBUG
endif
-ifeq ($(STATIC), yes)
- BUILDFLAGS =
- LINKFLAGS =
-else
+ifeq ($(DYNAMIC), yes)
BUILDFLAGS = -fPIC
LINKFLAGS = -shared
+else
+ BUILDFLAGS =
+ LINKFLAGS =
endif
export CC = gcc
@@ -44,6 +44,7 @@ SRCZLIB = $(wildcard ./src/helper/zlib/*.c)
SRCLIBZIP = $(wildcard ./src/helper/libzip/*.c)
SRCHAIKUTTF = $(wildcard ./src/helper/haikuttf/*.cpp)
+SRCBASE = $(wildcard ./src/base/*.cpp)
SRCAUDIO = $(wildcard ./src/audio/*.cpp)
SRCGAMING = $(wildcard ./src/gaming/*.cpp)
SRCGRAPHICS = $(wildcard ./src/graphics/*.cpp)
@@ -63,6 +64,7 @@ OBJZLIB = $(SRCZLIB:.c=.o)
OBJLIBZIP = $(SRCLIBZIP:.c=.o)
OBJHAIKUTTF = $(SRCHAIKUTTF:.cpp=.o)
+OBJBASE = $(SRCBASE:.cpp=.o)
OBJAUDIO = $(SRCAUDIO:.cpp=.o)
OBJGAMING = $(SRCGAMING:.cpp=.o)
OBJGRAPHICS = $(SRCGRAPHICS:.cpp=.o)
@@ -73,19 +75,19 @@ OBJUTILS = $(SRCUTILS:.cpp=.o)
OBJWINDOW = $(SRCWINDOW:.cpp=.o)
OBJHELPERS = $(OBJGLEW) $(OBJSOIL) $(OBJSTBVORBIS) $(OBJZLIB) $(OBJLIBZIP)
-OBJMODULES = $(OBJHAIKUTTF) $(OBJUTILS) $(OBJMATH) $(OBJSYSTEM) $(OBJAUDIO) $(OBJWINDOW) $(OBJGRAPHICS) $(OBJGAMING) $(OBJUI)
+OBJMODULES = $(OBJHAIKUTTF) $(OBJBASE) $(OBJUTILS) $(OBJMATH) $(OBJSYSTEM) $(OBJAUDIO) $(OBJWINDOW) $(OBJGRAPHICS) $(OBJGAMING) $(OBJUI)
OBJTEST = $(SRCTEST:.cpp=.o)
OBJEEIV = $(SRCEEIV:.cpp=.o)
-ifeq ($(STATIC), yes)
- LIB = libeepp-s.a
- LIBNAME = $(LIBPATH)/$(LIB)
- INSTALL =
-else
+ifeq ($(DYNAMIC), yes)
LIB = libeepp.so
LIBNAME = $(LIBPATH)/$(LIB).$(VERSION)
INSTALL = && $(LN) $(LNFLAGS) $(DESTLIBDIR)/$(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
+else
+ LIB = libeepp-s.a
+ LIBNAME = $(LIBPATH)/$(LIB)
+ INSTALL =
endif
all: $(LIB)
diff --git a/Makefile.macosx b/Makefile.macosx
index e57112422..cfc239cb3 100644
--- a/Makefile.macosx
+++ b/Makefile.macosx
@@ -1,15 +1,15 @@
ifeq ($(DEBUGBUILD), yes)
- DEBUGFLAGS = -g -DDEBUG -DEE_DEBUG
+ DEBUGFLAGS = -g -DDEBUG -DEE_DEBUG -DEE_MEMORY_MANAGER
else
DEBUGFLAGS = -O2 -s -DNDEBUG
endif
-ifeq ($(STATIC), yes)
- BUILDFLAGS =
- LINKFLAGS =
-else
+ifeq ($(DYNAMIC), yes)
BUILDFLAGS = -fPIC
LINKFLAGS = -shared
+else
+ BUILDFLAGS =
+ LINKFLAGS =
endif
export CC = gcc
@@ -44,6 +44,7 @@ SRCZLIB = $(wildcard ./src/helper/zlib/*.c)
SRCLIBZIP = $(wildcard ./src/helper/libzip/*.c)
SRCHAIKUTTF = $(wildcard ./src/helper/haikuttf/*.cpp)
+SRCBASE = $(wildcard ./src/base/*.cpp)
SRCAUDIO = $(wildcard ./src/audio/*.cpp)
SRCGAMING = $(wildcard ./src/gaming/*.cpp)
SRCGRAPHICS = $(wildcard ./src/graphics/*.cpp)
@@ -63,6 +64,7 @@ OBJZLIB = $(SRCZLIB:.c=.o)
OBJLIBZIP = $(SRCLIBZIP:.c=.o)
OBJHAIKUTTF = $(SRCHAIKUTTF:.cpp=.o)
+OBJBASE = $(SRCBASE:.cpp=.o)
OBJAUDIO = $(SRCAUDIO:.cpp=.o)
OBJGAMING = $(SRCGAMING:.cpp=.o)
OBJGRAPHICS = $(SRCGRAPHICS:.cpp=.o)
@@ -73,19 +75,19 @@ OBJUTILS = $(SRCUTILS:.cpp=.o)
OBJWINDOW = $(SRCWINDOW:.cpp=.o)
OBJHELPERS = $(OBJGLEW) $(OBJSOIL) $(OBJSTBVORBIS) $(OBJZLIB) $(OBJLIBZIP)
-OBJMODULES = $(OBJHAIKUTTF) $(OBJUTILS) $(OBJMATH) $(OBJSYSTEM) $(OBJAUDIO) $(OBJWINDOW) $(OBJGRAPHICS) $(OBJGAMING) $(OBJUI)
+OBJMODULES = $(OBJHAIKUTTF) $(OBJBASE) $(OBJUTILS) $(OBJMATH) $(OBJSYSTEM) $(OBJAUDIO) $(OBJWINDOW) $(OBJGRAPHICS) $(OBJGAMING) $(OBJUI)
OBJTEST = $(SRCTEST:.cpp=.o)
OBJEEIV = $(SRCEEIV:.cpp=.o)
-ifeq ($(STATIC), yes)
- LIB = libeepp-s.a
- LIBNAME = $(LIBPATH)/$(LIB)
- INSTALL =
-else
+ifeq ($(DYNAMIC), yes)
LIB = libeepp.so
LIBNAME = $(LIBPATH)/$(LIB).$(VERSION)
INSTALL = && $(LN) $(LNFLAGS) $(DESTLIBDIR)/$(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
+else
+ LIB = libeepp-s.a
+ LIBNAME = $(LIBPATH)/$(LIB)
+ INSTALL =
endif
all: $(LIB)
diff --git a/ee.linux.cbp b/ee.linux.cbp
index a17748165..f4109e91a 100644
--- a/ee.linux.cbp
+++ b/ee.linux.cbp
@@ -8,7 +8,7 @@
-
+
@@ -18,11 +18,10 @@
-
+
-
@@ -32,6 +31,7 @@
+
@@ -67,6 +67,11 @@
+
+
+
+
+
@@ -140,11 +145,18 @@
+
+
+
+
+
+
+
diff --git a/src/audio/caudiodevice.cpp b/src/audio/caudiodevice.cpp
index 92fb9fc4c..2fd81ec2b 100755
--- a/src/audio/caudiodevice.cpp
+++ b/src/audio/caudiodevice.cpp
@@ -48,7 +48,7 @@ bool cAudioDevice::isCreated() {
cAudioDevice * cAudioDevice::instance() {
// Create the audio device if it doesn't exist
if ( NULL == mInstance )
- mInstance = new cAudioDevice;
+ mInstance = eeNew( cAudioDevice, () );
return mInstance;
}
@@ -66,7 +66,7 @@ void cAudioDevice::RemoveReference() {
// Destroy the audio device if the references count reaches 0
if (mInstance->mRefCount == 0) {
- delete mInstance;
+ eeDelete( mInstance );
mInstance = NULL;
}
}
diff --git a/src/audio/caudiodevice.hpp b/src/audio/caudiodevice.hpp
index 849a1fc28..8bf428daf 100755
--- a/src/audio/caudiodevice.hpp
+++ b/src/audio/caudiodevice.hpp
@@ -20,10 +20,10 @@ class EE_API cAudioDevice {
bool IsExtensionSupported( const std::string& extension );
bool isCreated();
- private :
- cAudioDevice();
~cAudioDevice();
+ private :
+ cAudioDevice();
static cAudioDevice * mInstance;
diff --git a/src/audio/csoundbuffer.cpp b/src/audio/csoundbuffer.cpp
index 10502ec77..b8646545d 100755
--- a/src/audio/csoundbuffer.cpp
+++ b/src/audio/csoundbuffer.cpp
@@ -64,10 +64,10 @@ bool cSoundBuffer::LoadFromPack( cPack* Pack, const std::string& FilePackPath )
bool cSoundBuffer::LoadFromMemory( const char* Data, std::size_t SizeInBytes ) {
// Create the sound file
- std::auto_ptr File( cSoundFile::CreateRead( Data, SizeInBytes ) );
+ cSoundFile * File = cSoundFile::CreateRead( Data, SizeInBytes );
// Open the sound file
- if ( File.get() ) {
+ if ( NULL != File ) {
// Get the sound parameters
std::size_t NbSamples = File->GetSamplesCount();
unsigned int ChannelsCount = File->GetChannelsCount();
@@ -80,9 +80,14 @@ bool cSoundBuffer::LoadFromMemory( const char* Data, std::size_t SizeInBytes ) {
cLog::instance()->Write( "Sound file loaded from memory." );
// Update the internal buffer with the new samples
+ eeDelete( File );
+
return Update( ChannelsCount, SampleRate );
} else {
cLog::instance()->Write( "Failed to read audio data from file in memory" );
+
+ eeDelete( File );
+
return false;
}
} else {
diff --git a/src/audio/csoundfile.cpp b/src/audio/csoundfile.cpp
index 42460f9d4..b23d6c581 100755
--- a/src/audio/csoundfile.cpp
+++ b/src/audio/csoundfile.cpp
@@ -21,9 +21,9 @@ cSoundFile * cSoundFile::CreateRead( const std::string& Filename ) {
// Create the file according to its type
cSoundFile * File = NULL;
- if ( cSoundFileOgg::IsFileSupported(Filename, true) ) File = new cSoundFileOgg;
+ if ( cSoundFileOgg::IsFileSupported(Filename, true) ) File = eeNew( cSoundFileOgg, () );
#ifndef EE_NO_SNDFILE
- else if ( cSoundFileDefault::IsFileSupported(Filename, true) ) File = new cSoundFileDefault;
+ else if ( cSoundFileDefault::IsFileSupported(Filename, true) ) File = eeNew( cSoundFileDefault, () );
#endif
// Open it for reading
@@ -40,7 +40,7 @@ cSoundFile * cSoundFile::CreateRead( const std::string& Filename ) {
File->mChannelsCount = ChannelsCount;
File->mSampleRate = SampleRate;
} else {
- delete File;
+ eeDelete( File );
File = NULL;
}
}
@@ -52,9 +52,9 @@ cSoundFile * cSoundFile::CreateRead( const char* Data, std::size_t SizeInMemory
// Create the file according to its type
cSoundFile * File = NULL;
- if ( cSoundFileOgg::IsFileSupported(Data, SizeInMemory)) File = new cSoundFileOgg;
+ if ( cSoundFileOgg::IsFileSupported(Data, SizeInMemory)) File = eeNew( cSoundFileOgg, () );
#ifndef EE_NO_SNDFILE
- else if ( cSoundFileDefault::IsFileSupported(Data, SizeInMemory) ) File = new cSoundFileDefault;
+ else if ( cSoundFileDefault::IsFileSupported(Data, SizeInMemory) ) File = eeNew( cSoundFileDefault, () );
#endif
// Open it for reading
@@ -71,7 +71,7 @@ cSoundFile * cSoundFile::CreateRead( const char* Data, std::size_t SizeInMemory
File->mChannelsCount = ChannelsCount;
File->mSampleRate = SampleRate;
} else {
- delete File;
+ eeDelete( File );
File = NULL;
}
}
@@ -83,9 +83,9 @@ cSoundFile * cSoundFile::CreateWrite( const std::string& Filename, unsigned int
// Create the file according to its type
cSoundFile * File = NULL;
- if ( cSoundFileOgg::IsFileSupported(Filename, false) ) File = new cSoundFileOgg;
+ if ( cSoundFileOgg::IsFileSupported(Filename, false) ) File = eeNew( cSoundFileOgg, () );
#ifndef EE_NO_SNDFILE
- else if ( cSoundFileDefault::IsFileSupported(Filename, false) ) File = new cSoundFileDefault;
+ else if ( cSoundFileDefault::IsFileSupported(Filename, false) ) File = eeNew( cSoundFileDefault, () );
#endif
// Open it for writing
@@ -98,7 +98,7 @@ cSoundFile * cSoundFile::CreateWrite( const std::string& Filename, unsigned int
File->mChannelsCount = ChannelsCount;
File->mSampleRate = SampleRate;
} else {
- delete File;
+ eeDelete( File );
File = NULL;
}
}
diff --git a/src/base.hpp b/src/base.hpp
index 17a90fb6d..802d1244a 100644
--- a/src/base.hpp
+++ b/src/base.hpp
@@ -7,6 +7,14 @@
#include
#include
#include
+#include
+
+#include
+#include
+
+#include
+#include
+#include
#include
#include
@@ -14,21 +22,14 @@
#include
#include
#include
-#include
-#include
-#include
+#include
-#include
-#include
-#include
-
-#include
-
-// Templates needed by the library
#include
#include
#include
+#include
+
#define Int8 Sint8
#define Int16 Sint16
#define Int32 Sint32
@@ -84,6 +85,12 @@
#define EE_CALL
#endif
+#if ( __GNUC__ >= 4 ) && defined( EE_DYNAMIC ) && defined( EE_EXPORTS )
+#define EE_API __attribute__ ((visibility("default")))
+#else
+#define EE_API
+#endif
+
#include "helper/glew/glew.h"
#if EE_PLATFORM == EE_PLATFORM_MACOSX
#include
@@ -91,14 +98,16 @@
#include
#endif
+#include "base/base.hpp"
+
namespace EE {
#define eeARRAY_SIZE(__array) ( sizeof(__array) / sizeof(__array[0]) )
- #define eeSAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
- #define eeSAFE_FREE(p) { if(p) { free ( (void*)p );(p)=NULL; } }
- #define eeSAFE_DELETE_ARRAY(p) { if(p) { delete[](p); (p)=NULL; } }
+ #define eeSAFE_DELETE(p) { if(p) { eeDelete (p); (p)=NULL; } }
+ #define eeSAFE_FREE(p) { if(p) { eeFree ( (void*)p ); (p)=NULL; } }
+ #define eeSAFE_DELETE_ARRAY(p) { if(p) { eeDeleteArray(p); (p)=NULL; } }
typedef float eeFloat; //! The internal floating point used on EE++. \n This can help to improve compatibility with some platforms. \n And helps for an easy change from single precision to double precision.
- typedef double eeDouble; //! The internal double floating point. It's only used when the engine needs some very high precision floating point ( for example the timer )
+ typedef double eeDouble; //! The internal double floating point. It's only used when the engine needs some very high precision floating point ( for example the timer )
typedef unsigned int eeUint;
typedef signed int eeInt;
diff --git a/src/base/allocator.hpp b/src/base/allocator.hpp
new file mode 100644
index 000000000..ca2af2768
--- /dev/null
+++ b/src/base/allocator.hpp
@@ -0,0 +1,75 @@
+#ifndef EE_ALLOCATOR_HPP
+#define EE_ALLOCATOR_HPP
+
+#include "memorymanager.hpp"
+
+namespace EE {
+
+template
+class eeAllocator {
+ public:
+ typedef T value_type;
+ typedef T * pointer;
+ typedef const T * const_pointer;
+ typedef T& reference;
+ typedef const T& const_reference;
+ typedef ptrdiff_t difference_type;
+ typedef size_t size_type;
+
+ eeAllocator() {
+ }
+
+ eeAllocator( const eeAllocator& ) {
+ }
+
+ virtual ~eeAllocator() {
+ }
+
+ T * allocate( size_t cnt, typename std::allocator::const_pointer ptr = 0 ) {
+ (void)ptr;
+ return ( T * ) eeMalloc( cnt * sizeof( T ) );
+ }
+
+ void deallocate( T * ptr, size_type ) {
+ eeFree( ptr );
+ }
+
+ void construct( T * ptr, const T&e ) {
+ new ( (void*)ptr ) T( e );
+ }
+
+ void destroy( T * ptr ) {
+ ptr->~T();
+ }
+
+ size_t max_size() const {
+ return size_t( 0xFFFFFFFF );
+ }
+
+ pointer address( reference x ) const {
+ return &x;
+ }
+
+ const_pointer address( const_reference x ) const {
+ return &x;
+ }
+
+ eeAllocator& operator=(const eeAllocator&) { return *this; }
+
+ template
+ struct rebind {
+ typedef eeAllocator other;
+ };
+
+ template
+ eeAllocator( const eeAllocator& ) {}
+
+ template
+ eeAllocator& operator=(const eeAllocator&) { return *this; }
+ protected:
+
+};
+
+}
+
+#endif
diff --git a/src/base/base.hpp b/src/base/base.hpp
new file mode 100644
index 000000000..a81df9a0a
--- /dev/null
+++ b/src/base/base.hpp
@@ -0,0 +1,9 @@
+#ifndef EE_BASE_BASE_HPP
+#define EE_BASE_BASE_HPP
+
+#include "memorymanager.hpp"
+#include "allocator.hpp"
+#include "stlcontainers.hpp"
+
+#endif
+
diff --git a/src/base/memorymanager.cpp b/src/base/memorymanager.cpp
new file mode 100644
index 000000000..5b84a45ac
--- /dev/null
+++ b/src/base/memorymanager.cpp
@@ -0,0 +1,138 @@
+#include "memorymanager.hpp"
+#include
+#include
+
+namespace EE {
+
+static std::string SizeToString( const unsigned int& MemSize ) {
+ std::string size = " bytes";
+ double mem = static_cast( MemSize );
+ unsigned int c = 0;
+
+ while ( mem > 1024 ) {
+ c++;
+ mem = mem / 1024;
+ }
+
+ switch (c) {
+ case 0: size = " bytes"; break;
+ case 1: size = " KB"; break;
+ case 2: size = " MB"; break;
+ case 3: size = " GB"; break;
+ case 4: size = " TB"; break;
+ default: size = " WTF";
+ }
+
+ std::ostringstream ss;
+ ss << mem;
+
+ return std::string( ss.str() + size );
+}
+
+tAllocatedPointerMap MemoryManager::mMapPointers;
+size_t MemoryManager::mTotalMemoryUsage = 0;
+size_t MemoryManager::mPeakMemoryUsage = 0;
+
+cAllocatedPointer::cAllocatedPointer( void * Data,const std::string& File, int Line, size_t Memory ) {
+ mData = Data;
+ mFile = File;
+ mLine = Line;
+ mMemory = Memory;
+}
+
+void * MemoryManager::AddPointer( const cAllocatedPointer& aAllocatedPointer ) {
+ mMapPointers.insert( tAllocatedPointerMap::value_type( aAllocatedPointer.mData, aAllocatedPointer ) );
+
+ mTotalMemoryUsage += aAllocatedPointer.mMemory;
+
+ if ( mPeakMemoryUsage < mTotalMemoryUsage )
+ mPeakMemoryUsage = mTotalMemoryUsage;
+
+ return aAllocatedPointer.mData;
+}
+
+bool MemoryManager::RemovePointer( void * Data ) {
+ bool Found = false;
+
+ tAllocatedPointerMapIt it = mMapPointers.upper_bound( Data );
+
+ it--;
+
+ if( it != mMapPointers.end() ) {
+ char * Test = (char*)it->second.mData;
+
+ size_t testSize = it->second.mMemory;
+
+ if( Data >= Test && Data < Test + testSize )
+ Found = true;
+ }
+
+
+ if ( !Found ) {
+ printf( "Trying to delete pointer %p created that does not exist!\n", Data );
+
+ return false;
+ }
+
+ mTotalMemoryUsage -= it->second.mMemory;
+
+ mMapPointers.erase( it );
+
+ return true;
+}
+
+void MemoryManager::LogResults() {
+ #ifdef EE_MEMORY_MANAGER
+ printf("\n|--Memory Manager Report-------------------------------|\n");
+ printf("|\n");
+
+ if( mMapPointers.empty() ) {
+ printf( "| No memory leaks detected.\n" );
+ } else {
+ printf( "| Memory leaks detected: \n" );
+ printf( "|\n");
+
+ printf( "| address\t file" );
+
+ //Get max length of file name
+ int lMax =0;
+ tAllocatedPointerMapIt it = mMapPointers.begin();
+
+ for( ; it != mMapPointers.end(); ++it ){
+ cAllocatedPointer &ap = it->second;
+
+ if( (int)ap.mFile.length() > lMax )
+ lMax = (int)ap.mFile.length();
+ }
+
+ lMax += 5;
+
+ for( int i = 0; i < lMax - 4; ++i )
+ printf(" ");
+
+ printf( "line\t\t memory usage\t \n" );
+
+ printf( "|------------------------------------------------------------\n" );
+
+ it = mMapPointers.begin();
+
+ for( ; it != mMapPointers.end(); ++it ) {
+ cAllocatedPointer &ap = it->second;
+
+ printf( "| %p\t %s", ap.mData, ap.mFile.c_str() );
+
+ for ( int i=0; i < lMax - (int)ap.mFile.length(); ++i )
+ printf(" ");
+
+ printf( "%d\t\t %d\t\n", ap.mLine, ap.mMemory );
+ }
+ }
+
+ printf( "|\n" );
+ printf( "| Memory left: %s\n", SizeToString( mTotalMemoryUsage ).c_str() );
+ printf( "| Peak Memory Usage: %s\n", SizeToString( mPeakMemoryUsage ).c_str() );
+ printf( "|------------------------------------------------------|\n\n" );
+ #endif
+}
+
+}
diff --git a/src/base/memorymanager.hpp b/src/base/memorymanager.hpp
new file mode 100644
index 000000000..e6849c107
--- /dev/null
+++ b/src/base/memorymanager.hpp
@@ -0,0 +1,101 @@
+#ifndef EE_MEMORY_MANAGER_HPP
+#define EE_MEMORY_MANAGER_HPP
+
+#include
+#include