/********************************************************************** Access : Public Filename : GSysAllocMalloc.h Content : System Allocator Interface Created : 2009 Authors : Maxim Shemanarev Notes : Interface to the system allocator. Copyright : (c) 1998-2009 Scaleform Corp. All Rights Reserved. Licensees may use this file in accordance with the valid Scaleform Commercial License Agreement provided with the software. This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR ANY PURPOSE. **********************************************************************/ #ifndef INC_GSysAllocMalloc_H #define INC_GSysAllocMalloc_H #include "GSysAlloc.h" #if defined(GFC_OS_WIN32) || defined(GFC_OS_WINCE) || defined(GFC_OS_XBOX) || defined(GFC_OS_XBOX360) #include #else #include #include #endif class GSysAllocMalloc : public GSysAllocBase_SingletonSupport { public: GSysAllocMalloc() {} virtual ~GSysAllocMalloc() {} #if defined(GFC_OS_WIN32) || defined(GFC_OS_WINCE) || defined(GFC_OS_XBOX) || defined(GFC_OS_XBOX360) virtual void* Alloc(UPInt size, UPInt align) { return _aligned_malloc(size, align); } virtual void Free(void* ptr, UPInt size, UPInt align) { GUNUSED2(size, align); _aligned_free(ptr); } virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align) { GUNUSED(oldSize); return _aligned_realloc(oldPtr, newSize, align); } #else virtual void* Alloc(UPInt size, UPInt align) { UPInt ptr = (UPInt)malloc(size+align); UPInt aligned = 0; if (ptr) { aligned = (UPInt(ptr) + align-1) & ~(align-1); if (aligned == ptr) aligned += align; *(((UPInt*)aligned)-1) = aligned-ptr; } return (void*)aligned; } virtual void Free(void* ptr, UPInt size, UPInt align) { UPInt src = UPInt(ptr) - *(((UPInt*)ptr)-1); free((void*)src); } virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align) { void* newPtr = Alloc(newSize, align); if (newPtr) { memcpy(newPtr, oldPtr, (newSize < oldSize) ? newSize : oldSize); Free(oldPtr, oldSize, align); } return newPtr; } #endif }; #endif