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 
31 #include "gim_memory.h"
32 #include "stdlib.h"
33 
34 #ifdef GIM_SIMD_MEMORY
36 #endif
37 
42 
44 {
45  g_allocfn = fn;
46 }
47 
49 {
50  g_allocafn = fn;
51 }
52 
54 {
55  g_reallocfn = fn;
56 }
57 
59 {
60  g_freefn = fn;
61 }
62 
64 {
65  return g_allocfn;
66 }
67 
69 {
70  return g_allocafn;
71 }
72 
73 
75 {
76  return g_reallocfn;
77 }
78 
79 
81 {
82  return g_freefn;
83 }
84 
85 
86 void * gim_alloc(size_t size)
87 {
88  void * ptr;
89  if (g_allocfn)
90  {
91  ptr = g_allocfn(size);
92  }
93  else
94  {
95 #ifdef GIM_SIMD_MEMORY
96  ptr = btAlignedAlloc(size,16);
97 #else
98  ptr = malloc(size);
99 #endif
100  }
101  return ptr;
102 }
103 
104 void * gim_alloca(size_t size)
105 {
106  if (g_allocafn) return g_allocafn(size); else return gim_alloc(size);
107 }
108 
109 
110 void * gim_realloc(void *ptr, size_t oldsize, size_t newsize)
111 {
112  void * newptr = gim_alloc(newsize);
113  size_t copysize = oldsize<newsize?oldsize:newsize;
114  gim_simd_memcpy(newptr,ptr,copysize);
115  gim_free(ptr);
116  return newptr;
117 }
118 
119 void gim_free(void *ptr)
120 {
121  if (!ptr) return;
122  if (g_freefn)
123  {
124  g_freefn(ptr);
125  }
126  else
127  {
128  #ifdef GIM_SIMD_MEMORY
129  btAlignedFree(ptr);
130  #else
131  free(ptr);
132  #endif
133  }
134 }
135 
void gim_simd_memcpy(void *dst, const void *src, size_t copysize)
Definition: gim_memory.h:130
static gim_realloc_function * g_reallocfn
Definition: gim_memory.cpp:40
void gim_set_free_handler(gim_free_function *fn)
Definition: gim_memory.cpp:58
void * gim_realloc_function(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.h:93
gim_realloc_function * gim_get_realloc_handler()
Definition: gim_memory.cpp:74
static gim_alloc_function * g_allocfn
Definition: gim_memory.cpp:38
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
gim_free_function * gim_get_free_handler()
Definition: gim_memory.cpp:80
void gim_set_realloc_handler(gim_realloc_function *fn)
Definition: gim_memory.cpp:53
static gim_alloca_function * g_allocafn
Definition: gim_memory.cpp:39
void gim_free(void *ptr)
Definition: gim_memory.cpp:119
void gim_set_alloca_handler(gim_alloca_function *fn)
Definition: gim_memory.cpp:48
void gim_free_function(void *ptr)
Definition: gim_memory.h:94
void * gim_alloca_function(size_t size)
Definition: gim_memory.h:92
#define btAlignedFree(ptr)
gim_alloc_function * gim_get_alloc_handler()
get current memory management functions.
Definition: gim_memory.cpp:63
void * gim_alloca(size_t size)
Definition: gim_memory.cpp:104
void * gim_realloc(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.cpp:110
void gim_set_alloc_handler(gim_alloc_function *fn)
Memory Function Handlers set new memory management functions.
Definition: gim_memory.cpp:43
void * gim_alloc_function(size_t size)
Function prototypes to allocate and free memory.
Definition: gim_memory.h:91
void * gim_alloc(size_t size)
Standar Memory functions.
Definition: gim_memory.cpp:86
#define btAlignedAlloc(size, alignment)
gim_alloca_function * gim_get_alloca_handler()
Definition: gim_memory.cpp:68
static gim_free_function * g_freefn
Definition: gim_memory.cpp:41