Bullet Collision Detection & Physics Library
gim_memory.cpp
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of GIMPACT Library.
4 
5 For the latest info, see http://gimpact.sourceforge.net/
6 
7 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
8 email: projectileman@yahoo.com
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of EITHER:
12  (1) The GNU Lesser General Public License as published by the Free
13  Software Foundation; either version 2.1 of the License, or (at
14  your option) any later version. The text of the GNU Lesser
15  General Public License is included with this library in the
16  file GIMPACT-LICENSE-LGPL.TXT.
17  (2) The BSD-style license that is included with this library in
18  the file GIMPACT-LICENSE-BSD.TXT.
19  (3) The zlib/libpng license that is included with this library in
20  the file GIMPACT-LICENSE-ZLIB.TXT.
21 
22  This library is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
25  GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
26 
27 -----------------------------------------------------------------------------
28 */
29 
30 #include "gim_memory.h"
31 #include "stdlib.h"
32 
33 #ifdef GIM_SIMD_MEMORY
35 #endif
36 
41 
43 {
44  g_allocfn = fn;
45 }
46 
48 {
49  g_allocafn = fn;
50 }
51 
53 {
54  g_reallocfn = fn;
55 }
56 
58 {
59  g_freefn = fn;
60 }
61 
63 {
64  return g_allocfn;
65 }
66 
68 {
69  return g_allocafn;
70 }
71 
73 {
74  return g_reallocfn;
75 }
76 
78 {
79  return g_freefn;
80 }
81 
82 void *gim_alloc(size_t size)
83 {
84  void *ptr;
85  if (g_allocfn)
86  {
87  ptr = g_allocfn(size);
88  }
89  else
90  {
91 #ifdef GIM_SIMD_MEMORY
92  ptr = btAlignedAlloc(size, 16);
93 #else
94  ptr = malloc(size);
95 #endif
96  }
97  return ptr;
98 }
99 
100 void *gim_alloca(size_t size)
101 {
102  if (g_allocafn)
103  return g_allocafn(size);
104  else
105  return gim_alloc(size);
106 }
107 
108 void *gim_realloc(void *ptr, size_t oldsize, size_t newsize)
109 {
110  void *newptr = gim_alloc(newsize);
111  size_t copysize = oldsize < newsize ? oldsize : newsize;
112  gim_simd_memcpy(newptr, ptr, copysize);
113  gim_free(ptr);
114  return newptr;
115 }
116 
117 void gim_free(void *ptr)
118 {
119  if (!ptr) return;
120  if (g_freefn)
121  {
122  g_freefn(ptr);
123  }
124  else
125  {
126 #ifdef GIM_SIMD_MEMORY
127  btAlignedFree(ptr);
128 #else
129  free(ptr);
130 #endif
131  }
132 }
g_allocfn
static gim_alloc_function * g_allocfn
Definition: gim_memory.cpp:37
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:47
g_reallocfn
static gim_realloc_function * g_reallocfn
Definition: gim_memory.cpp:39
gim_set_free_handler
void gim_set_free_handler(gim_free_function *fn)
Definition: gim_memory.cpp:57
gim_get_free_handler
gim_free_function * gim_get_free_handler()
Definition: gim_memory.cpp:77
gim_get_realloc_handler
gim_realloc_function * gim_get_realloc_handler()
Definition: gim_memory.cpp:72
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:46
g_allocafn
static gim_alloca_function * g_allocafn
Definition: gim_memory.cpp:38
gim_set_realloc_handler
void gim_set_realloc_handler(gim_realloc_function *fn)
Definition: gim_memory.cpp:52
gim_free
void gim_free(void *ptr)
Definition: gim_memory.cpp:117
gim_alloca_function
void * gim_alloca_function(size_t size)
Definition: gim_memory.h:88
gim_set_alloca_handler
void gim_set_alloca_handler(gim_alloca_function *fn)
Definition: gim_memory.cpp:47
btAlignedAllocator.h
gim_memory.h
gim_realloc
void * gim_realloc(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.cpp:108
gim_get_alloc_handler
gim_alloc_function * gim_get_alloc_handler()
get current memory management functions.
Definition: gim_memory.cpp:62
gim_alloc_function
void * gim_alloc_function(size_t size)
Function prototypes to allocate and free memory.
Definition: gim_memory.h:87
gim_alloca
void * gim_alloca(size_t size)
Definition: gim_memory.cpp:100
gim_set_alloc_handler
void gim_set_alloc_handler(gim_alloc_function *fn)
Memory Function Handlers set new memory management functions.
Definition: gim_memory.cpp:42
gim_simd_memcpy
void gim_simd_memcpy(void *dst, const void *src, size_t copysize)
Definition: gim_memory.h:120
gim_free_function
void gim_free_function(void *ptr)
Definition: gim_memory.h:90
gim_alloc
void * gim_alloc(size_t size)
Standar Memory functions.
Definition: gim_memory.cpp:82
gim_get_alloca_handler
gim_alloca_function * gim_get_alloca_handler()
Definition: gim_memory.cpp:67
g_freefn
static gim_free_function * g_freefn
Definition: gim_memory.cpp:40
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
gim_realloc_function
void * gim_realloc_function(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.h:89