Changeset View
Changeset View
Standalone View
Standalone View
extern/bullet2/src/BulletCollision/Gimpact/gim_memory.cpp
| Show All 21 Lines | |||||
| This library is distributed in the hope that it will be useful, | This library is distributed in the hope that it will be useful, | ||||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files | ||||
| GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details. | GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details. | ||||
| ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ||||
| */ | */ | ||||
| #include "gim_memory.h" | #include "gim_memory.h" | ||||
| #include "stdlib.h" | #include "stdlib.h" | ||||
| #ifdef GIM_SIMD_MEMORY | #ifdef GIM_SIMD_MEMORY | ||||
| #include "LinearMath/btAlignedAllocator.h" | #include "LinearMath/btAlignedAllocator.h" | ||||
| #endif | #endif | ||||
| static gim_alloc_function *g_allocfn = 0; | static gim_alloc_function *g_allocfn = 0; | ||||
| static gim_alloca_function *g_allocafn = 0; | static gim_alloca_function *g_allocafn = 0; | ||||
| static gim_realloc_function *g_reallocfn = 0; | static gim_realloc_function *g_reallocfn = 0; | ||||
| static gim_free_function *g_freefn = 0; | static gim_free_function *g_freefn = 0; | ||||
| void gim_set_alloc_handler (gim_alloc_function *fn) | void gim_set_alloc_handler(gim_alloc_function *fn) | ||||
| { | { | ||||
| g_allocfn = fn; | g_allocfn = fn; | ||||
| } | } | ||||
| void gim_set_alloca_handler (gim_alloca_function *fn) | void gim_set_alloca_handler(gim_alloca_function *fn) | ||||
| { | { | ||||
| g_allocafn = fn; | g_allocafn = fn; | ||||
| } | } | ||||
| void gim_set_realloc_handler (gim_realloc_function *fn) | void gim_set_realloc_handler(gim_realloc_function *fn) | ||||
| { | { | ||||
| g_reallocfn = fn; | g_reallocfn = fn; | ||||
| } | } | ||||
| void gim_set_free_handler (gim_free_function *fn) | void gim_set_free_handler(gim_free_function *fn) | ||||
| { | { | ||||
| g_freefn = fn; | g_freefn = fn; | ||||
| } | } | ||||
| gim_alloc_function *gim_get_alloc_handler() | gim_alloc_function *gim_get_alloc_handler() | ||||
| { | { | ||||
| return g_allocfn; | return g_allocfn; | ||||
| } | } | ||||
| gim_alloca_function *gim_get_alloca_handler() | gim_alloca_function *gim_get_alloca_handler() | ||||
| { | { | ||||
| return g_allocafn; | return g_allocafn; | ||||
| } | } | ||||
| gim_realloc_function *gim_get_realloc_handler () | gim_realloc_function *gim_get_realloc_handler() | ||||
| { | { | ||||
| return g_reallocfn; | return g_reallocfn; | ||||
| } | } | ||||
| gim_free_function *gim_get_free_handler () | gim_free_function *gim_get_free_handler() | ||||
| { | { | ||||
| return g_freefn; | return g_freefn; | ||||
| } | } | ||||
| void * gim_alloc(size_t size) | void *gim_alloc(size_t size) | ||||
| { | { | ||||
| void * ptr; | void *ptr; | ||||
| if (g_allocfn) | if (g_allocfn) | ||||
| { | { | ||||
| ptr = g_allocfn(size); | ptr = g_allocfn(size); | ||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| #ifdef GIM_SIMD_MEMORY | #ifdef GIM_SIMD_MEMORY | ||||
| ptr = btAlignedAlloc(size,16); | ptr = btAlignedAlloc(size, 16); | ||||
| #else | #else | ||||
| ptr = malloc(size); | ptr = malloc(size); | ||||
| #endif | #endif | ||||
| } | } | ||||
| return ptr; | return ptr; | ||||
| } | } | ||||
| void * gim_alloca(size_t size) | void *gim_alloca(size_t size) | ||||
| { | { | ||||
| if (g_allocafn) return g_allocafn(size); else return gim_alloc(size); | if (g_allocafn) | ||||
| return g_allocafn(size); | |||||
| else | |||||
| return gim_alloc(size); | |||||
| } | } | ||||
| void * gim_realloc(void *ptr, size_t oldsize, size_t newsize) | void *gim_realloc(void *ptr, size_t oldsize, size_t newsize) | ||||
| { | { | ||||
| void * newptr = gim_alloc(newsize); | void *newptr = gim_alloc(newsize); | ||||
| size_t copysize = oldsize<newsize?oldsize:newsize; | size_t copysize = oldsize < newsize ? oldsize : newsize; | ||||
| gim_simd_memcpy(newptr,ptr,copysize); | gim_simd_memcpy(newptr, ptr, copysize); | ||||
| gim_free(ptr); | gim_free(ptr); | ||||
| return newptr; | return newptr; | ||||
| } | } | ||||
| void gim_free(void *ptr) | void gim_free(void *ptr) | ||||
| { | { | ||||
| if (!ptr) return; | if (!ptr) return; | ||||
| if (g_freefn) | if (g_freefn) | ||||
| { | { | ||||
| g_freefn(ptr); | g_freefn(ptr); | ||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| #ifdef GIM_SIMD_MEMORY | #ifdef GIM_SIMD_MEMORY | ||||
| btAlignedFree(ptr); | btAlignedFree(ptr); | ||||
| #else | #else | ||||
| free(ptr); | free(ptr); | ||||
| #endif | #endif | ||||
| } | } | ||||
| } | } | ||||