From eee9ba0caeed52ba380ba8d9931db57714725455 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Fri, 13 Jun 2014 19:12:31 +0200 Subject: [PATCH] SSE2: Add aligned allocators --HG-- branch : sse2 --- code/nel/include/nel/misc/types_nl.h | 34 ++++++++++++++++++++++++++++ code/nel/src/misc/common.cpp | 29 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/code/nel/include/nel/misc/types_nl.h b/code/nel/include/nel/misc/types_nl.h index 5c3b80475..f4001fd94 100644 --- a/code/nel/include/nel/misc/types_nl.h +++ b/code/nel/include/nel/misc/types_nl.h @@ -328,6 +328,40 @@ typedef unsigned int uint; // at least 32bits (depend of processor) #endif // NL_OS_UNIX + +#ifdef NL_COMP_VC +#define NL_ALIGN(nb) __declspec(align(nb)) +#else +#define NL_ALIGN(nb) __attribute__((aligned(nb))) +#endif + +#ifdef NL_COMP_VC +inline void *aligned_malloc(size_t size, size_t alignment) { return _aligned_malloc(size, alignment); } +inline void aligned_free(void *ptr) { _aligned_free(ptr); } +#else +inline void *aligned_malloc(size_t size, size_t alignment) { return memalign(alignment, size); } +inline void aligned_free(void *ptr) { free(ptr); } +#endif /* NL_COMP_ */ + + +#ifdef NL_HAS_SSE2 + +#define NL_DEFAULT_MEMORY_ALIGNMENT 16 +#define NL_ALIGN_SSE2 NL_ALIGN(NL_DEFAULT_MEMORY_ALIGNMENT) + +extern void *operator new(size_t size) throw(std::bad_alloc); +extern void *operator new[](size_t size) throw(std::bad_alloc); +extern void operator delete(void *p) throw(); +extern void operator delete[](void *p) throw(); + +#else /* NL_HAS_SSE2 */ + +#define NL_DEFAULT_MEMORY_ALIGNMENT 4 +#define NL_ALIGN_SSE2(nb) + +#endif /* NL_HAS_SSE2 */ + + // CHashMap, CHashSet and CHashMultiMap definitions #if defined(_STLPORT_VERSION) // STLport detected # include diff --git a/code/nel/src/misc/common.cpp b/code/nel/src/misc/common.cpp index 36e167260..3c72b6ca4 100644 --- a/code/nel/src/misc/common.cpp +++ b/code/nel/src/misc/common.cpp @@ -71,6 +71,35 @@ extern "C" long _ftol2( double dblSource ) { return _ftol( dblSource ); } #endif // NL_OS_WINDOWS +#ifdef NL_HAS_SSE2 + +void *operator new(size_t size) throw(std::bad_alloc) +{ + void *p = aligned_malloc(size, NL_DEFAULT_MEMORY_ALIGNMENT); + if (p == NULL) throw std::bad_alloc(); + return p; +} + +void *operator new[](size_t size) throw(std::bad_alloc) +{ + void *p = aligned_malloc(size, NL_DEFAULT_MEMORY_ALIGNMENT); + if (p == NULL) throw std::bad_alloc(); + return p; +} + +void operator delete(void *p) throw() +{ + aligned_free(p); +} + +void operator delete[](void *p) throw() +{ + aligned_free(p); +} + +#endif /* NL_HAS_SSE2 */ + + #ifdef DEBUG_NEW #define new DEBUG_NEW #endif