Blender V2.61 - r43446

gim_memory.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of GIMPACT Library.
00004 
00005 For the latest info, see http://gimpact.sourceforge.net/
00006 
00007 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
00008 email: projectileman@yahoo.com
00009 
00010  This library is free software; you can redistribute it and/or
00011  modify it under the terms of EITHER:
00012    (1) The GNU Lesser General Public License as published by the Free
00013        Software Foundation; either version 2.1 of the License, or (at
00014        your option) any later version. The text of the GNU Lesser
00015        General Public License is included with this library in the
00016        file GIMPACT-LICENSE-LGPL.TXT.
00017    (2) The BSD-style license that is included with this library in
00018        the file GIMPACT-LICENSE-BSD.TXT.
00019    (3) The zlib/libpng license that is included with this library in
00020        the file GIMPACT-LICENSE-ZLIB.TXT.
00021 
00022  This library is distributed in the hope that it will be useful,
00023  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
00025  GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
00026 
00027 -----------------------------------------------------------------------------
00028 */
00029 
00030 
00031 #include "gim_memory.h"
00032 #include "stdlib.h"
00033 
00034 #ifdef GIM_SIMD_MEMORY
00035 #include "LinearMath/btAlignedAllocator.h"
00036 #endif
00037 
00038 static gim_alloc_function *g_allocfn = 0;
00039 static gim_alloca_function *g_allocafn = 0;
00040 static gim_realloc_function *g_reallocfn = 0;
00041 static gim_free_function *g_freefn = 0;
00042 
00043 void gim_set_alloc_handler (gim_alloc_function *fn)
00044 {
00045   g_allocfn = fn;
00046 }
00047 
00048 void gim_set_alloca_handler (gim_alloca_function *fn)
00049 {
00050   g_allocafn = fn;
00051 }
00052 
00053 void gim_set_realloc_handler (gim_realloc_function *fn)
00054 {
00055   g_reallocfn = fn;
00056 }
00057 
00058 void gim_set_free_handler (gim_free_function *fn)
00059 {
00060   g_freefn = fn;
00061 }
00062 
00063 gim_alloc_function *gim_get_alloc_handler()
00064 {
00065   return g_allocfn;
00066 }
00067 
00068 gim_alloca_function *gim_get_alloca_handler()
00069 {
00070   return g_allocafn;
00071 }
00072 
00073 
00074 gim_realloc_function *gim_get_realloc_handler ()
00075 {
00076   return g_reallocfn;
00077 }
00078 
00079 
00080 gim_free_function  *gim_get_free_handler ()
00081 {
00082   return g_freefn;
00083 }
00084 
00085 
00086 void * gim_alloc(size_t size)
00087 {
00088     void * ptr;
00089     if (g_allocfn)
00090     {
00091         ptr = g_allocfn(size);
00092     }
00093     else
00094     {
00095 #ifdef GIM_SIMD_MEMORY
00096         ptr = btAlignedAlloc(size,16);
00097 #else
00098         ptr = malloc(size);
00099 #endif
00100     }
00101     return ptr;
00102 }
00103 
00104 void * gim_alloca(size_t size)
00105 {
00106   if (g_allocafn) return g_allocafn(size); else return gim_alloc(size);
00107 }
00108 
00109 
00110 void * gim_realloc(void *ptr, size_t oldsize, size_t newsize)
00111 {
00112     void * newptr = gim_alloc(newsize);
00113     size_t copysize = oldsize<newsize?oldsize:newsize;
00114     gim_simd_memcpy(newptr,ptr,copysize);
00115     gim_free(ptr);
00116     return newptr;
00117 }
00118 
00119 void gim_free(void *ptr)
00120 {
00121     if (!ptr) return;
00122     if (g_freefn)
00123     {
00124        g_freefn(ptr);
00125     }
00126     else
00127     {
00128     #ifdef GIM_SIMD_MEMORY
00129         btAlignedFree(ptr);
00130     #else
00131         free(ptr);
00132     #endif
00133     }
00134 }
00135