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