Bullet Collision Detection & Physics Library
gim_memory.h
Go to the documentation of this file.
1 #ifndef GIM_MEMORY_H_INCLUDED
2 #define GIM_MEMORY_H_INCLUDED
3 
6 /*
7 -----------------------------------------------------------------------------
8 This source file is part of GIMPACT Library.
9 
10 For the latest info, see http://gimpact.sourceforge.net/
11 
12 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
14 
15  This library is free software; you can redistribute it and/or
16  modify it under the terms of EITHER:
17  (1) The GNU Lesser General Public License as published by the Free
18  Software Foundation; either version 2.1 of the License, or (at
19  your option) any later version. The text of the GNU Lesser
20  General Public License is included with this library in the
21  file GIMPACT-LICENSE-LGPL.TXT.
22  (2) The BSD-style license that is included with this library in
23  the file GIMPACT-LICENSE-BSD.TXT.
24  (3) The zlib/libpng license that is included with this library in
25  the file GIMPACT-LICENSE-ZLIB.TXT.
26 
27  This library is distributed in the hope that it will be useful,
28  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
30  GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
31 
32 -----------------------------------------------------------------------------
33 */
34 
35 
36 #include "gim_math.h"
37 #include <string.h>
38 
39 #ifdef PREFETCH
40 #include <xmmintrin.h> // for prefetch
41 #define pfval 64
42 #define pfval2 128
43 #define pf(_x,_i) _mm_prefetch((void *)(_x + _i + pfval), 0)
45 #define pf2(_x,_i) _mm_prefetch((void *)(_x + _i + pfval2), 0)
47 #else
48 #define pf(_x,_i)
50 #define pf2(_x,_i)
52 #endif
53 
54 
56 #define GIM_COPY_ARRAYS(dest_array,source_array,element_count)\
57 {\
58  for (GUINT _i_=0;_i_<element_count ;++_i_)\
59  {\
60  dest_array[_i_] = source_array[_i_];\
61  }\
62 }\
63 
64 #define GIM_COPY_ARRAYS_1(dest_array,source_array,element_count,copy_macro)\
65 {\
66  for (GUINT _i_=0;_i_<element_count ;++_i_)\
67  {\
68  copy_macro(dest_array[_i_],source_array[_i_]);\
69  }\
70 }\
71 
72 
73 #define GIM_ZERO_ARRAY(array,element_count)\
74 {\
75  for (GUINT _i_=0;_i_<element_count ;++_i_)\
76  {\
77  array[_i_] = 0;\
78  }\
79 }\
80 
81 #define GIM_CONSTANT_ARRAY(array,element_count,constant)\
82 {\
83  for (GUINT _i_=0;_i_<element_count ;++_i_)\
84  {\
85  array[_i_] = constant;\
86  }\
87 }\
88 
89 
91 typedef void * gim_alloc_function (size_t size);
92 typedef void * gim_alloca_function (size_t size);//Allocs on the heap
93 typedef void * gim_realloc_function (void *ptr, size_t oldsize, size_t newsize);
94 typedef void gim_free_function (void *ptr);
95 
96 
103 
104 
110 
111 
113 void * gim_alloc(size_t size);
114 void * gim_alloca(size_t size);
115 void * gim_realloc(void *ptr, size_t oldsize, size_t newsize);
116 void gim_free(void *ptr);
117 
118 
119 
120 #if defined (_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
121  #define GIM_SIMD_MEMORY 1
122 #endif
123 
125 #define SIMD_T GUINT64
126 #define SIMD_T_SIZE sizeof(SIMD_T)
128 
129 
130 inline void gim_simd_memcpy(void * dst, const void * src, size_t copysize)
131 {
132 #ifdef GIM_SIMD_MEMORY
133 /*
134 //'long long int' is incompatible with visual studio 6...
135  //copy words
136  SIMD_T * ui_src_ptr = (SIMD_T *)src;
137  SIMD_T * ui_dst_ptr = (SIMD_T *)dst;
138  while(copysize>=SIMD_T_SIZE)
139  {
140  *(ui_dst_ptr++) = *(ui_src_ptr++);
141  copysize-=SIMD_T_SIZE;
142  }
143  if(copysize==0) return;
144 */
145 
146  char * c_src_ptr = (char *)src;
147  char * c_dst_ptr = (char *)dst;
148  while(copysize>0)
149  {
150  *(c_dst_ptr++) = *(c_src_ptr++);
151  copysize--;
152  }
153  return;
154 #else
155  memcpy(dst,src,copysize);
156 #endif
157 }
158 
159 
160 
161 template<class T>
162 inline void gim_swap_elements(T* _array,size_t _i,size_t _j)
163 {
164  T _e_tmp_ = _array[_i];
165  _array[_i] = _array[_j];
166  _array[_j] = _e_tmp_;
167 }
168 
169 
170 template<class T>
171 inline void gim_swap_elements_memcpy(T* _array,size_t _i,size_t _j)
172 {
173  char _e_tmp_[sizeof(T)];
174  gim_simd_memcpy(_e_tmp_,&_array[_i],sizeof(T));
175  gim_simd_memcpy(&_array[_i],&_array[_j],sizeof(T));
176  gim_simd_memcpy(&_array[_j],_e_tmp_,sizeof(T));
177 }
178 
179 template <int SIZE>
180 inline void gim_swap_elements_ptr(char * _array,size_t _i,size_t _j)
181 {
182  char _e_tmp_[SIZE];
183  _i*=SIZE;
184  _j*=SIZE;
185  gim_simd_memcpy(_e_tmp_,_array+_i,SIZE);
186  gim_simd_memcpy(_array+_i,_array+_j,SIZE);
187  gim_simd_memcpy(_array+_j,_e_tmp_,SIZE);
188 }
189 
190 #endif // GIM_MEMORY_H_INCLUDED
void gim_simd_memcpy(void *dst, const void *src, size_t copysize)
Definition: gim_memory.h:130
void * gim_alloc(size_t size)
Standar Memory functions.
Definition: gim_memory.cpp:86
gim_alloca_function * gim_get_alloca_handler(void)
Definition: gim_memory.cpp:68
void * gim_realloc_function(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.h:93
void * gim_realloc(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.cpp:110
void gim_set_alloca_handler(gim_alloca_function *fn)
Definition: gim_memory.cpp:48
gim_free_function * gim_get_free_handler(void)
Definition: gim_memory.cpp:80
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void gim_free_function(void *ptr)
Definition: gim_memory.h:94
gim_realloc_function * gim_get_realloc_handler(void)
Definition: gim_memory.cpp:74
void * gim_alloca_function(size_t size)
Definition: gim_memory.h:92
void * gim_alloca(size_t size)
Definition: gim_memory.cpp:104
void gim_set_alloc_handler(gim_alloc_function *fn)
Memory Function Handlers set new memory management functions.
Definition: gim_memory.cpp:43
void gim_free(void *ptr)
Definition: gim_memory.cpp:119
void gim_swap_elements_memcpy(T *_array, size_t _i, size_t _j)
Definition: gim_memory.h:171
gim_alloc_function * gim_get_alloc_handler(void)
get current memory management functions.
Definition: gim_memory.cpp:63
void gim_set_free_handler(gim_free_function *fn)
Definition: gim_memory.cpp:58
void gim_swap_elements_ptr(char *_array, size_t _i, size_t _j)
Definition: gim_memory.h:180
void gim_set_realloc_handler(gim_realloc_function *fn)
Definition: gim_memory.cpp:53
void gim_swap_elements(T *_array, size_t _i, size_t _j)
Definition: gim_memory.h:162
void * gim_alloc_function(size_t size)
Function prototypes to allocate and free memory.
Definition: gim_memory.h:91