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 -----------------------------------------------------------------------------
39 #include <xmmintrin.h> // for prefetch
43 #define pf(_x, _i) _mm_prefetch((void *)(_x + _i + pfval), 0)
45 #define pf2(_x, _i) _mm_prefetch((void *)(_x + _i + pfval2), 0)
53 ///Functions for manip packed arrays of numbers
54 #define GIM_COPY_ARRAYS(dest_array, source_array, element_count) \
56 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
58 dest_array[_i_] = source_array[_i_]; \
62 #define GIM_COPY_ARRAYS_1(dest_array, source_array, element_count, copy_macro) \
64 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
66 copy_macro(dest_array[_i_], source_array[_i_]); \
70 #define GIM_ZERO_ARRAY(array, element_count) \
72 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
78 #define GIM_CONSTANT_ARRAY(array, element_count, constant) \
80 for (GUINT _i_ = 0; _i_ < element_count; ++_i_) \
82 array[_i_] = constant; \
86 ///Function prototypes to allocate and free memory.
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);
92 ///Memory Function Handlers
93 ///set new memory management functions. if fn is 0, the default handlers are used.
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);
99 ///get current memory management functions.
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);
105 ///Standar Memory functions
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);
111 #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
112 #define GIM_SIMD_MEMORY 1
115 //! SIMD POINTER INTEGER
116 #define SIMD_T GUINT64
117 //! SIMD INTEGER SIZE
118 #define SIMD_T_SIZE sizeof(SIMD_T)
120 inline void gim_simd_memcpy(void *dst, const void *src, size_t copysize)
122 #ifdef GIM_SIMD_MEMORY
124 //'long long int' is incompatible with visual studio 6...
126 SIMD_T * ui_src_ptr = (SIMD_T *)src;
127 SIMD_T * ui_dst_ptr = (SIMD_T *)dst;
128 while(copysize>=SIMD_T_SIZE)
130 *(ui_dst_ptr++) = *(ui_src_ptr++);
131 copysize-=SIMD_T_SIZE;
133 if(copysize==0) return;
136 char *c_src_ptr = (char *)src;
137 char *c_dst_ptr = (char *)dst;
140 *(c_dst_ptr++) = *(c_src_ptr++);
145 memcpy(dst, src, copysize);
150 inline void gim_swap_elements(T *_array, size_t _i, size_t _j)
152 T _e_tmp_ = _array[_i];
153 _array[_i] = _array[_j];
154 _array[_j] = _e_tmp_;
158 inline void gim_swap_elements_memcpy(T *_array, size_t _i, size_t _j)
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));
167 inline void gim_swap_elements_ptr(char *_array, size_t _i, size_t _j)
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);
177 #endif // GIM_MEMORY_H_INCLUDED