author | rubidium |
Wed, 09 Jul 2008 13:32:13 +0000 | |
branch | noai |
changeset 11126 | 72d4c9314c72 |
parent 11044 | 097ea3e7ec56 |
permissions | -rw-r--r-- |
11044 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file mem_func.hpp Functions related to memory operations. */ |
|
4 |
||
5 |
#ifndef MEM_FUNC_HPP |
|
6 |
#define MEM_FUNC_HPP |
|
7 |
||
8 |
#include <string.h> |
|
9 |
#include "math_func.hpp" |
|
10 |
||
11 |
/** |
|
12 |
* Type-safe version of memcpy(). |
|
13 |
* |
|
14 |
* @param destination Pointer to the destination buffer |
|
15 |
* @param source Pointer to the source buffer |
|
16 |
* @param num number of items to be copied. (!not number of bytes!) |
|
17 |
*/ |
|
18 |
template <typename T> |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
19 |
static FORCEINLINE void MemCpyT(T *destination, const T *source, uint num = 1) |
11044 | 20 |
{ |
21 |
memcpy(destination, source, num * sizeof(T)); |
|
22 |
} |
|
23 |
||
24 |
/** |
|
25 |
* Type-safe version of memmove(). |
|
26 |
* |
|
27 |
* @param destination Pointer to the destination buffer |
|
28 |
* @param source Pointer to the source buffer |
|
29 |
* @param num number of items to be copied. (!not number of bytes!) |
|
30 |
*/ |
|
31 |
template <typename T> |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
32 |
static FORCEINLINE void MemMoveT(T *destination, const T *source, uint num = 1) |
11044 | 33 |
{ |
34 |
memmove(destination, source, num * sizeof(T)); |
|
35 |
} |
|
36 |
||
37 |
/** |
|
38 |
* Type-safe version of memset(). |
|
39 |
* |
|
40 |
* @param ptr Pointer to the destination buffer |
|
41 |
* @param value Value to be set |
|
42 |
* @param num number of items to be set (!not number of bytes!) |
|
43 |
*/ |
|
44 |
template <typename T> |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
45 |
static FORCEINLINE void MemSetT(T *ptr, int value, uint num = 1) |
11044 | 46 |
{ |
47 |
memset(ptr, value, num * sizeof(T)); |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Type-safe version of memcmp(). |
|
52 |
* |
|
53 |
* @param ptr1 Pointer to the first buffer |
|
54 |
* @param ptr2 Pointer to the second buffer |
|
55 |
* @param num Number of items to compare. (!not number of bytes!) |
|
56 |
* @return an int value indicating the relationship between the content of the two buffers |
|
57 |
*/ |
|
58 |
template <typename T> |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
59 |
static FORCEINLINE int MemCmpT(const T *ptr1, const T *ptr2, uint num = 1) |
11044 | 60 |
{ |
61 |
return memcmp(ptr1, ptr2, num * sizeof(T)); |
|
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* Type safe memory reverse operation. |
|
66 |
* Reverse a block of memory in steps given by the |
|
67 |
* type of the pointers. |
|
68 |
* |
|
69 |
* @param ptr1 Start-pointer to the block of memory. |
|
70 |
* @param ptr2 End-pointer to the block of memory. |
|
71 |
*/ |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
72 |
template <typename T> |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
73 |
static FORCEINLINE void MemReverseT(T *ptr1, T *ptr2) |
11044 | 74 |
{ |
75 |
assert(ptr1 != NULL && ptr2 != NULL); |
|
76 |
assert(ptr1 < ptr2); |
|
77 |
||
78 |
do { |
|
79 |
Swap(*ptr1, *ptr2); |
|
80 |
} while (++ptr1 < --ptr2); |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Type safe memory reverse operation (overloaded) |
|
85 |
* |
|
86 |
* @param ptr Pointer to the block of memory. |
|
87 |
* @param num The number of items we want to reverse. |
|
88 |
*/ |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
89 |
template <typename T> |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
90 |
static FORCEINLINE void MemReverseT(T *ptr, uint num) |
11044 | 91 |
{ |
92 |
assert(ptr != NULL); |
|
93 |
||
94 |
MemReverseT(ptr, ptr + (num - 1)); |
|
95 |
} |
|
96 |
||
97 |
#endif /* MEM_FUNC_HPP */ |