1 #ifndef GIM_MEMORY_H_INCLUDED
2 #define GIM_MEMORY_H_INCLUDED
4 \author Francisco Leon Najera
7 -----------------------------------------------------------------------------
8 This source file is part of GIMPACT Library.
10 For the latest info, see http://gimpact.sourceforge.net/
12 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
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.
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.
32 -----------------------------------------------------------------------------
40 #include <xmmintrin.h> // for prefetch
44 #define pf(_x,_i) _mm_prefetch((void *)(_x + _i + pfval), 0)
46 #define pf2(_x,_i) _mm_prefetch((void *)(_x + _i + pfval2), 0)
55 ///Functions for manip packed arrays of numbers
56 #define GIM_COPY_ARRAYS(dest_array,source_array,element_count)\
58 for (GUINT _i_=0;_i_<element_count ;++_i_)\
60 dest_array[_i_] = source_array[_i_];\
64 #define GIM_COPY_ARRAYS_1(dest_array,source_array,element_count,copy_macro)\
66 for (GUINT _i_=0;_i_<element_count ;++_i_)\
68 copy_macro(dest_array[_i_],source_array[_i_]);\
73 #define GIM_ZERO_ARRAY(array,element_count)\
75 for (GUINT _i_=0;_i_<element_count ;++_i_)\
81 #define GIM_CONSTANT_ARRAY(array,element_count,constant)\
83 for (GUINT _i_=0;_i_<element_count ;++_i_)\
85 array[_i_] = constant;\
90 ///Function prototypes to allocate and free memory.
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);
97 ///Memory Function Handlers
98 ///set new memory management functions. if fn is 0, the default handlers are used.
99 void gim_set_alloc_handler (gim_alloc_function *fn);
100 void gim_set_alloca_handler (gim_alloca_function *fn);
101 void gim_set_realloc_handler (gim_realloc_function *fn);
102 void gim_set_free_handler (gim_free_function *fn);
105 ///get current memory management functions.
106 gim_alloc_function *gim_get_alloc_handler (void);
107 gim_alloca_function *gim_get_alloca_handler(void);
108 gim_realloc_function *gim_get_realloc_handler (void);
109 gim_free_function *gim_get_free_handler (void);
112 ///Standar Memory functions
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);
120 #if defined (_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
121 #define GIM_SIMD_MEMORY 1
124 //! SIMD POINTER INTEGER
125 #define SIMD_T GUINT64
126 //! SIMD INTEGER SIZE
127 #define SIMD_T_SIZE sizeof(SIMD_T)
130 inline void gim_simd_memcpy(void * dst, const void * src, size_t copysize)
132 #ifdef GIM_SIMD_MEMORY
134 //'long long int' is incompatible with visual studio 6...
136 SIMD_T * ui_src_ptr = (SIMD_T *)src;
137 SIMD_T * ui_dst_ptr = (SIMD_T *)dst;
138 while(copysize>=SIMD_T_SIZE)
140 *(ui_dst_ptr++) = *(ui_src_ptr++);
141 copysize-=SIMD_T_SIZE;
143 if(copysize==0) return;
146 char * c_src_ptr = (char *)src;
147 char * c_dst_ptr = (char *)dst;
150 *(c_dst_ptr++) = *(c_src_ptr++);
155 memcpy(dst,src,copysize);
162 inline void gim_swap_elements(T* _array,size_t _i,size_t _j)
164 T _e_tmp_ = _array[_i];
165 _array[_i] = _array[_j];
166 _array[_j] = _e_tmp_;
171 inline void gim_swap_elements_memcpy(T* _array,size_t _i,size_t _j)
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));
180 inline void gim_swap_elements_ptr(char * _array,size_t _i,size_t _j)
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);
190 #endif // GIM_MEMORY_H_INCLUDED