Bullet Collision Detection & Physics Library: gim_memory.h Source File

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
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 #include "gim_math.h"
36 #include <string.h>
37
38 #ifdef PREFETCH
39 #include <xmmintrin.h> // for prefetch
40 #define pfval 64
41 #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)
46 #else
48 #define pf(_x, _i)
50 #define pf2(_x, _i)
51 #endif
52
54 #define GIM_COPY_ARRAYS(dest_array, source_array, element_count) \
55 { \
56 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
57 { \
58 dest_array[_i_] = source_array[_i_]; \
59 } \
60 }
61
62 #define GIM_COPY_ARRAYS_1(dest_array, source_array, element_count, copy_macro) \
63 { \
64 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
65 { \
66 copy_macro(dest_array[_i_], source_array[_i_]); \
67 } \
68 }
69
70 #define GIM_ZERO_ARRAY(array, element_count) \
71 { \
72 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
73 { \
74 array[_i_] = 0; \
75 } \
76 }
77
78 #define GIM_CONSTANT_ARRAY(array, element_count, constant) \
79 { \
80 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
81 { \
82 array[_i_] = constant; \
83 } \
84 }
85
87 typedef void *gim_alloc_function(size_t size);
88 typedef void *gim_alloca_function(size_t size); //Allocs on the heap
89 typedef void *gim_realloc_function(void *ptr, size_t oldsize, size_t newsize);
90 typedef void gim_free_function(void *ptr);
91
94 void gim_set_alloc_handler(gim_alloc_function *fn);
95 void gim_set_alloca_handler(gim_alloca_function *fn);
96 void gim_set_realloc_handler(gim_realloc_function *fn);
97 void gim_set_free_handler(gim_free_function *fn);
98
100 gim_alloc_function *gim_get_alloc_handler(void);
101 gim_alloca_function *gim_get_alloca_handler(void);
102 gim_realloc_function *gim_get_realloc_handler(void);
103 gim_free_function *gim_get_free_handler(void);
104
106 void *gim_alloc(size_t size);
107 void *gim_alloca(size_t size);
108 void *gim_realloc(void *ptr, size_t oldsize, size_t newsize);
109 void gim_free(void *ptr);
110
111 #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
112 #define GIM_SIMD_MEMORY 1
113 #endif
114
116 #define SIMD_T GUINT64
118 #define SIMD_T_SIZE sizeof(SIMD_T)
119
120 inline void gim_simd_memcpy(void *dst, const void *src, size_t copysize)
121 {
122 #ifdef GIM_SIMD_MEMORY
123 /*
124 //'long long int' is incompatible with visual studio 6...
125 //copy words
126 SIMD_T * ui_src_ptr = (SIMD_T *)src;
127 SIMD_T * ui_dst_ptr = (SIMD_T *)dst;
128 while(copysize>=SIMD_T_SIZE)
129 {
130 *(ui_dst_ptr++) = *(ui_src_ptr++);
131 copysize-=SIMD_T_SIZE;
132 }
133 if(copysize==0) return;
134 */
135
136 char *c_src_ptr = (char *)src;
137 char *c_dst_ptr = (char *)dst;
138 while (copysize > 0)
139 {
140 *(c_dst_ptr++) = *(c_src_ptr++);
141 copysize--;
142 }
143 return;
144 #else
145 memcpy(dst, src, copysize);
146 #endif
147 }
148
149 template <class T>
150 inline void gim_swap_elements(T *_array, size_t _i, size_t _j)
151 {
152 T _e_tmp_ = _array[_i];
153 _array[_i] = _array[_j];
154 _array[_j] = _e_tmp_;
155 }
156
157 template <class T>
158 inline void gim_swap_elements_memcpy(T *_array, size_t _i, size_t _j)
159 {
160 char _e_tmp_[sizeof(T)];
161 gim_simd_memcpy(_e_tmp_, &_array[_i], sizeof(T));
162 gim_simd_memcpy(&_array[_i], &_array[_j], sizeof(T));
163 gim_simd_memcpy(&_array[_j], _e_tmp_, sizeof(T));
164 }
165
166 template <int SIZE>
167 inline void gim_swap_elements_ptr(char *_array, size_t _i, size_t _j)
168 {
169 char _e_tmp_[SIZE];
170 _i *= SIZE;
171 _j *= SIZE;
172 gim_simd_memcpy(_e_tmp_, _array + _i, SIZE);
173 gim_simd_memcpy(_array + _i, _array + _j, SIZE);
174 gim_simd_memcpy(_array + _j, _e_tmp_, SIZE);
175 }
176
177 #endif // GIM_MEMORY_H_INCLUDED
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
gim_math.h
gim_set_free_handler
void gim_set_free_handler(gim_free_function *fn)
Definition: gim_memory.cpp:57
gim_get_realloc_handler
gim_realloc_function * gim_get_realloc_handler(void)
Definition: gim_memory.cpp:72
gim_realloc_function
void * gim_realloc_function(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.h:89
gim_swap_elements_memcpy
void gim_swap_elements_memcpy(T *_array, size_t _i, size_t _j)
Definition: gim_memory.h:158
gim_simd_memcpy
void gim_simd_memcpy(void *dst, const void *src, size_t copysize)
Definition: gim_memory.h:120
gim_swap_elements
void gim_swap_elements(T *_array, size_t _i, size_t _j)
Definition: gim_memory.h:150
gim_alloca
void * gim_alloca(size_t size)
Definition: gim_memory.cpp:100
gim_alloc_function
void * gim_alloc_function(size_t size)
Function prototypes to allocate and free memory.
Definition: gim_memory.h:87
gim_set_alloca_handler
void gim_set_alloca_handler(gim_alloca_function *fn)
Definition: gim_memory.cpp:47
gim_free
void gim_free(void *ptr)
Definition: gim_memory.cpp:117
gim_get_free_handler
gim_free_function * gim_get_free_handler(void)
Definition: gim_memory.cpp:77
gim_realloc
void * gim_realloc(void *ptr, size_t oldsize, size_t newsize)
Definition: gim_memory.cpp:108
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_get_alloca_handler
gim_alloca_function * gim_get_alloca_handler(void)
Definition: gim_memory.cpp:67
gim_alloc
void * gim_alloc(size_t size)
Standar Memory functions.
Definition: gim_memory.cpp:82
gim_alloca_function
void * gim_alloca_function(size_t size)
Definition: gim_memory.h:88
gim_set_realloc_handler
void gim_set_realloc_handler(gim_realloc_function *fn)
Definition: gim_memory.cpp:52
gim_swap_elements_ptr
void gim_swap_elements_ptr(char *_array, size_t _i, size_t _j)
Definition: gim_memory.h:167
gim_free_function
void gim_free_function(void *ptr)
Definition: gim_memory.h:90
gim_get_alloc_handler
gim_alloc_function * gim_get_alloc_handler(void)
get current memory management functions.
Definition: gim_memory.cpp:62