2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
This source file is part of GIMPACT Library.
|
|
|
|
|
|
|
|
For the latest info, see http://gimpact.sourceforge.net/
|
|
|
|
|
|
|
|
Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
|
|
|
|
email: projectileman@yahoo.com
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of EITHER:
|
|
|
|
(1) The GNU Lesser General Public License as published by the Free
|
|
|
|
Software Foundation; either version 2.1 of the License, or (at
|
|
|
|
your option) any later version. The text of the GNU Lesser
|
|
|
|
General Public License is included with this library in the
|
|
|
|
file GIMPACT-LICENSE-LGPL.TXT.
|
|
|
|
(2) The BSD-style license that is included with this library in
|
|
|
|
the file GIMPACT-LICENSE-BSD.TXT.
|
|
|
|
(3) The zlib/libpng license that is included with this library in
|
|
|
|
the file GIMPACT-LICENSE-ZLIB.TXT.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
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.
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gim_memory.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
|
|
|
#ifdef GIM_SIMD_MEMORY
|
|
|
|
#include "LinearMath/btAlignedAllocator.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static gim_alloc_function *g_allocfn = 0;
|
|
|
|
static gim_alloca_function *g_allocafn = 0;
|
|
|
|
static gim_realloc_function *g_reallocfn = 0;
|
|
|
|
static gim_free_function *g_freefn = 0;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void gim_set_alloc_handler(gim_alloc_function *fn)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
g_allocfn = fn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void gim_set_alloca_handler(gim_alloca_function *fn)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
g_allocafn = fn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void gim_set_realloc_handler(gim_realloc_function *fn)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
g_reallocfn = fn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void gim_set_free_handler(gim_free_function *fn)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
g_freefn = fn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gim_alloc_function *gim_get_alloc_handler()
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return g_allocfn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gim_alloca_function *gim_get_alloca_handler()
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return g_allocafn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_realloc_function *gim_get_realloc_handler()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return g_reallocfn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_free_function *gim_get_free_handler()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return g_freefn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void *gim_alloc(size_t size)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
void *ptr;
|
2017-08-01 12:30:58 +00:00
|
|
|
if (g_allocfn)
|
|
|
|
{
|
|
|
|
ptr = g_allocfn(size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef GIM_SIMD_MEMORY
|
2019-01-03 13:26:51 +00:00
|
|
|
ptr = btAlignedAlloc(size, 16);
|
2017-08-01 12:30:58 +00:00
|
|
|
#else
|
|
|
|
ptr = malloc(size);
|
|
|
|
#endif
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
return ptr;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void *gim_alloca(size_t size)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (g_allocafn)
|
|
|
|
return g_allocafn(size);
|
|
|
|
else
|
|
|
|
return gim_alloc(size);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void *gim_realloc(void *ptr, size_t oldsize, size_t newsize)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
void *newptr = gim_alloc(newsize);
|
|
|
|
size_t copysize = oldsize < newsize ? oldsize : newsize;
|
|
|
|
gim_simd_memcpy(newptr, ptr, copysize);
|
|
|
|
gim_free(ptr);
|
|
|
|
return newptr;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gim_free(void *ptr)
|
|
|
|
{
|
|
|
|
if (!ptr) return;
|
|
|
|
if (g_freefn)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
g_freefn(ptr);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
#ifdef GIM_SIMD_MEMORY
|
2017-08-01 12:30:58 +00:00
|
|
|
btAlignedFree(ptr);
|
2019-01-03 13:26:51 +00:00
|
|
|
#else
|
2017-08-01 12:30:58 +00:00
|
|
|
free(ptr);
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|