remove \r
[extl.git] / extl / memory / help_functions.h
blob9da3bd0fbb34bc5ef4842c79f7abe4fdfd1ed6c7
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: help_functions.h
4 * Created: 08.03.10
5 * Updated: 08.04.14
7 * Brief: Memory helpful functions
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_MEMORY_HELP_FUNCTIONS_H
14 #define EXTL_MEMORY_HELP_FUNCTIONS_H
16 /*!\file help_functions.h
17 * \brief Memory helpful functions
19 #ifndef __cplusplus
20 # error help_functions.h need be supported by c++.
21 #endif
23 /* ///////////////////////////////////////////////////////////////////////
24 * Includes
26 # include "../prefix.h"
27 # include "../type/traits/is_void.h"
29 #ifdef EXTL_TYPE_TRAITS_IS_POD_SUPPORT
30 # include "../type/traits/is_pod.h"
31 #endif
33 /* ///////////////////////////////////////////////////////////////////////
34 * ::extl namespace
36 EXTL_BEGIN_NAMESPACE
38 /* ///////////////////////////////////////////////////////////////////////
39 * Memory block operations
42 /// mem_copy
43 EXTL_INLINE void const* mem_copy(void* dest, void const* src, e_size_t size)
45 EXTL_ASSERT(NULL != dest);
46 EXTL_ASSERT(NULL != src);
47 return ::memcpy(dest, src, size);
50 /// mem_cmp
51 EXTL_INLINE e_int_t mem_cmp(void const* lhs, void const* rhs, e_size_t size)
53 EXTL_ASSERT(NULL != lhs);
54 EXTL_ASSERT(NULL != rhs);
56 return ::memcmp(lhs, rhs, size);
59 /// mem_move
60 EXTL_INLINE void const* mem_move(void* dest, void const* src, e_size_t size)
62 EXTL_ASSERT(NULL != dest);
63 EXTL_ASSERT(NULL != src);
65 return ::memmove(dest, src, size);
68 /// mem_fill
69 EXTL_INLINE void const* mem_fill(void* dest, e_int_t const& value, e_size_t size)
71 EXTL_ASSERT(NULL != dest);
72 return ::memset(dest, value, size);
75 /// mem_fill_0
76 EXTL_INLINE void const* mem_fill_0(void* dest, e_size_t size)
78 EXTL_ASSERT(NULL != dest);
79 return ::memset(dest, 0, size);
81 /* ///////////////////////////////////////////////////////////////////////
82 * POD memory block operations
84 /// pod_copy_n
85 template < typename_param_k D, typename_param_k S >
86 EXTL_INLINE D const* pod_copy_n(D* dest, S const* src, e_size_t n)
88 #ifdef EXTL_TYPE_TRAITS_IS_POD_SUPPORT
89 EXTL_MUST_BE_POD(D);
90 EXTL_MUST_BE_POD(S);
91 #endif
92 EXTL_MUST_NOT_BE_VOID(D);
93 EXTL_MUST_NOT_BE_VOID(S);
95 return static_cast<D const*>(mem_copy(dest, src, n * sizeof(D)));
98 /// non_pod_copy_n
99 template < typename_param_k D, typename_param_k S >
100 EXTL_INLINE D const* non_pod_copy_n(D* dest, S const* src, e_size_t n)
102 D* ret = dest;
103 for(; 0 != n; ++dest, ++src, --n)
105 *dest = *src;
107 return ret;
109 /// pod_cmp_n
110 template < typename_param_k D, typename_param_k S >
111 EXTL_INLINE e_int_t pod_cmp_n(D const* lhs, S const* rhs, e_size_t n)
113 #ifdef EXTL_TYPE_TRAITS_IS_POD_SUPPORT
114 EXTL_MUST_BE_POD(D);
115 EXTL_MUST_BE_POD(S);
116 #endif
117 EXTL_MUST_NOT_BE_VOID(D);
118 EXTL_MUST_NOT_BE_VOID(S);
120 EXTL_ASSERT(0 != n);
121 for(e_size_t i = 0; i < n; ++i, ++lhs, ++rhs)
123 if(*lhs != *rhs)
125 return (*lhs < *rhs)? -1 : + 1;
128 return 0;
130 /// pod_cmp_n
131 EXTL_INLINE e_int_t pod_cmp_n(e_char_t const* lhs, e_char_t const* rhs, e_size_t n)
133 return mem_cmp(lhs, rhs, n * sizeof(e_char_t));
136 /// non_pod_cmp_n
137 template < typename_param_k D, typename_param_k S >
138 EXTL_INLINE e_int_t non_pod_cmp_n(D const* lhs, S const* rhs, e_size_t n)
140 EXTL_ASSERT(NULL != lhs);
141 EXTL_ASSERT(NULL != rhs);
142 EXTL_ASSERT(0 != n);
143 for(e_size_t i = 0; i < n; ++i, ++lhs, ++rhs)
145 if(*lhs != *rhs)
147 return (*lhs < *rhs)? -1 : + 1;
150 return 0;
152 /// pod_move_n
153 template < typename_param_k D, typename_param_k S >
154 EXTL_INLINE D const* pod_move_n(D* dest, S const* src, e_size_t n)
156 #ifdef EXTL_TYPE_TRAITS_IS_POD_SUPPORT
157 EXTL_MUST_BE_POD(D);
158 EXTL_MUST_BE_POD(S);
159 #endif
161 EXTL_MUST_NOT_BE_VOID(D);
162 EXTL_MUST_NOT_BE_VOID(S);
164 return static_cast<D const*>(mem_move(dest, src, n * sizeof(D)));
166 /// non_pod_move_n
167 template < typename_param_k D, typename_param_k S >
168 EXTL_INLINE D const* non_pod_move_n(D* dest, S const* src, e_size_t n)
170 EXTL_ASSERT(NULL != dest);
171 EXTL_ASSERT(NULL != src);
173 return non_pod_copy_n(dest, src, n);
176 /// pod_fill_n
177 template < typename_param_k D, typename_param_k V >
178 EXTL_INLINE D const* pod_fill_n(D* dest, V const& value, e_size_t n)
180 #ifdef EXTL_TYPE_TRAITS_IS_POD_SUPPORT
181 EXTL_MUST_BE_POD(D);
182 EXTL_MUST_BE_POD(V);
183 #endif
184 EXTL_MUST_NOT_BE_VOID(D);
186 EXTL_ASSERT(0 != n);
188 D* ret = dest;
189 for(; 0 != n; ++dest, --n)
191 *dest = value;
193 return ret;
195 /// pod_fill_n: Optimizatiion
196 EXTL_INLINE e_char_t const* pod_fill_n(e_char_t* dest, e_char_t const& value, e_size_t n)
198 return static_cast<e_char_t const*>(mem_fill(dest, value, n * sizeof(e_char_t)));
200 /// non_pod_fill_n
201 template < typename_param_k D, typename_param_k V >
202 EXTL_INLINE D const* non_pod_fill_n(D* dest, V const& value, e_size_t n)
204 EXTL_ASSERT(NULL != dest);
205 EXTL_ASSERT(0 != n);
207 D* ret = dest;
208 for(; 0 != n; ++dest, --n)
210 *dest = value;
212 return ret;
215 /// pod_fill_n
216 template < typename_param_k D >
217 EXTL_INLINE D const* pod_fill_0_n(D* dest, e_size_t n)
219 #ifdef EXTL_TYPE_TRAITS_IS_POD_SUPPORT
220 EXTL_MUST_BE_POD(D);
221 #endif
222 EXTL_MUST_NOT_BE_VOID(D);
224 return static_cast<D const*>(mem_fill_0(dest, n * sizeof(D)));
227 /// non_pod_fill_0
228 template < typename_param_k D >
229 EXTL_INLINE D const* non_pod_fill_0_n(D* dest, e_size_t n)
231 EXTL_ASSERT(NULL != dest);
232 EXTL_ASSERT(0 != n);
234 D* ret = dest;
235 for(; 0 != n; ++dest, --n)
237 *dest = D();
239 return ret;
241 /* ///////////////////////////////////////////////////////////////////////
242 * Unit-testing
244 #ifdef EXTL_MEMORY_HELP_FUNCTIONS_TEST_ENABLE
245 # include "unit_test/help_functions_test.h"
246 #endif
248 /* ///////////////////////////////////////////////////////////////////////
249 * ::extl namespace
251 EXTL_END_NAMESPACE
253 /* //////////////////////////////////////////////////////////////////// */
254 #endif /* EXTL_MEMORY_HELP_FUNCTIONS_H */
255 /* //////////////////////////////////////////////////////////////////// */