set eol-style:native
[reactos.git] / reactos / lib / rtl / mem.c
1 /* COPYRIGHT: See COPYING in the top level directory
2 * PROJECT: ReactOS system libraries
3 * FILE: lib/rtl/mem.c
4 * PURPOSE: Memory functions
5 * PROGRAMMER: David Welch (welch@mcmail.com)
6 */
7
8 /* INCLUDES *****************************************************************/
9
10 #include <rtl.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 #undef RtlUlonglongByteSwap
16 #undef RtlUlongByteSwap
17 #undef RtlUshortByteSwap
18
19 /* FUNCTIONS *****************************************************************/
20
21 /******************************************************************************
22 * RtlCompareMemory [NTDLL.@]
23 *
24 * Compare one block of memory with another
25 *
26 * PARAMS
27 * Source1 [I] Source block
28 * Source2 [I] Block to compare to Source1
29 * Length [I] Number of bytes to fill
30 *
31 * RETURNS
32 * The length of the first byte at which Source1 and Source2 differ, or Length
33 * if they are the same.
34 *
35 * @implemented
36 */
37 SIZE_T NTAPI
38 RtlCompareMemory(IN const VOID *Source1,
39 IN const VOID *Source2,
40 IN SIZE_T Length)
41 {
42 SIZE_T i;
43 for(i=0; (i<Length) && (((PUCHAR)Source1)[i]==((PUCHAR)Source2)[i]); i++)
44 ;
45 return i;
46 }
47
48
49 /*
50 * @implemented
51 */
52 ULONG
53 NTAPI
54 RtlCompareMemoryUlong (
55 PVOID Source,
56 ULONG Length,
57 ULONG Value
58 )
59 /*
60 * FUNCTION: Compares a block of ULONGs with an ULONG and returns the number of equal bytes
61 * ARGUMENTS:
62 * Source = Block to compare
63 * Length = Number of bytes to compare
64 * Value = Value to compare
65 * RETURNS: Number of equal bytes
66 */
67 {
68 PULONG ptr = (PULONG)Source;
69 ULONG len = Length / sizeof(ULONG);
70 ULONG i;
71
72 for (i = 0; i < len; i++)
73 {
74 if (*ptr != Value)
75 break;
76 ptr++;
77 }
78
79 return (ULONG)((PCHAR)ptr - (PCHAR)Source);
80 }
81
82
83 #undef RtlFillMemory
84 /*
85 * @implemented
86 */
87 VOID
88 NTAPI
89 RtlFillMemory (
90 PVOID Destination,
91 ULONG Length,
92 UCHAR Fill
93 )
94 {
95 memset(Destination, Fill, Length);
96 }
97
98
99
100 /*
101 * @implemented
102 */
103 VOID
104 NTAPI
105 RtlFillMemoryUlong (
106 PVOID Destination,
107 ULONG Length,
108 ULONG Fill
109 )
110 {
111 PULONG Dest = Destination;
112 ULONG Count = Length / sizeof(ULONG);
113
114 while (Count > 0)
115 {
116 *Dest = Fill;
117 Dest++;
118 Count--;
119 }
120 }
121
122
123 #undef RtlMoveMemory
124 /*
125 * @implemented
126 */
127 VOID
128 NTAPI
129 RtlMoveMemory (
130 PVOID Destination,
131 CONST VOID * Source,
132 ULONG Length
133 )
134 {
135 memmove (
136 Destination,
137 Source,
138 Length
139 );
140 }
141
142 /*
143 * @implemented
144 */
145 VOID
146 FASTCALL
147 RtlPrefetchMemoryNonTemporal(
148 IN PVOID Source,
149 IN SIZE_T Length
150 )
151 {
152 /* By nature of prefetch, this is non-portable. */
153 (void)Source;
154 (void)Length;
155 }
156
157
158 #undef RtlZeroMemory
159 /*
160 * @implemented
161 */
162 VOID
163 NTAPI
164 RtlZeroMemory (
165 PVOID Destination,
166 ULONG Length
167 )
168 {
169 RtlFillMemory (
170 Destination,
171 Length,
172 0
173 );
174 }
175
176
177 /*************************************************************************
178 * RtlUshortByteSwap
179 *
180 * Swap the bytes of an unsigned short value.
181 *
182 * NOTES
183 * Based on the inline versions in Wine winternl.h
184 *
185 * @implemented
186 */
187 USHORT FASTCALL
188 RtlUshortByteSwap (IN USHORT Source)
189 {
190 return (Source >> 8) | (Source << 8);
191 }
192
193
194
195 /*************************************************************************
196 * RtlUlongByteSwap [NTDLL.@]
197 *
198 * Swap the bytes of an unsigned int value.
199 *
200 * NOTES
201 * Based on the inline versions in Wine winternl.h
202 *
203 * @implemented
204 */
205 ULONG
206 FASTCALL
207 RtlUlongByteSwap(
208 IN ULONG Source
209 )
210 {
211 #if defined(__i386__) && defined(__GNUC__)
212 ULONG ret;
213 __asm__("bswap %0" : "=r" (ret) : "0" (Source) );
214 return ret;
215 #else
216
217 return ((ULONG)RtlUshortByteSwap((USHORT)Source) << 16) | RtlUshortByteSwap((USHORT)(Source >> 16));
218 #endif
219 }
220
221
222 /*************************************************************************
223 * RtlUlonglongByteSwap
224 *
225 * Swap the bytes of an unsigned long long value.
226 *
227 * PARAMS
228 * i [I] Value to swap bytes of
229 *
230 * RETURNS
231 * The value with its bytes swapped.
232 *
233 * @implemented
234 */
235 ULONGLONG FASTCALL
236 RtlUlonglongByteSwap (IN ULONGLONG Source)
237 {
238 return ((ULONGLONG) RtlUlongByteSwap (Source) << 32) | RtlUlongByteSwap (Source>>32);
239 }
240
241
242 /* EOF */