315e392fd7b650b017e004246dc6e9683e22649c
[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 #undef RtlFillMemory
88 /*
89 * @implemented
90 */
91 VOID
92 STDCALL
93 RtlFillMemory (
94 PVOID Destination,
95 ULONG Length,
96 UCHAR Fill
97 )
98 {
99 memset(Destination, Fill, Length);
100 }
101
102
103
104 /*
105 * @implemented
106 */
107 VOID
108 STDCALL
109 RtlFillMemoryUlong (
110 PVOID Destination,
111 ULONG Length,
112 ULONG Fill
113 )
114 {
115 PULONG Dest = Destination;
116 ULONG Count = Length / sizeof(ULONG);
117
118 while (Count > 0)
119 {
120 *Dest = Fill;
121 Dest++;
122 Count--;
123 }
124 }
125
126
127 #undef RtlMoveMemory
128 /*
129 * @implemented
130 */
131 VOID
132 STDCALL
133 RtlMoveMemory (
134 PVOID Destination,
135 CONST VOID * Source,
136 ULONG Length
137 )
138 {
139 memmove (
140 Destination,
141 Source,
142 Length
143 );
144 }
145
146 /*
147 * @unimplemented
148 */
149 VOID
150 FASTCALL
151 RtlPrefetchMemoryNonTemporal(
152 IN PVOID Source,
153 IN SIZE_T Length
154 )
155 {
156 UNIMPLEMENTED;
157 }
158
159
160 #undef RtlZeroMemory
161 /*
162 * @implemented
163 */
164 VOID
165 STDCALL
166 RtlZeroMemory (
167 PVOID Destination,
168 ULONG Length
169 )
170 {
171 RtlFillMemory (
172 Destination,
173 Length,
174 0
175 );
176 }
177
178
179 /*************************************************************************
180 * RtlUshortByteSwap
181 *
182 * Swap the bytes of an unsigned short value.
183 *
184 * NOTES
185 * Based on the inline versions in Wine winternl.h
186 *
187 * @implemented
188 */
189 USHORT FASTCALL
190 RtlUshortByteSwap (IN USHORT Source)
191 {
192 return (Source >> 8) | (Source << 8);
193 }
194
195
196
197 /*************************************************************************
198 * RtlUlongByteSwap [NTDLL.@]
199 *
200 * Swap the bytes of an unsigned int value.
201 *
202 * NOTES
203 * Based on the inline versions in Wine winternl.h
204 *
205 * @implemented
206 */
207 ULONG
208 FASTCALL
209 RtlUlongByteSwap(
210 IN ULONG Source
211 )
212 {
213 #if defined(__i386__) && defined(__GNUC__)
214 ULONG ret;
215 __asm__("bswap %0" : "=r" (ret) : "0" (Source) );
216 return ret;
217 #else
218
219 return ((ULONG)RtlUshortByteSwap((USHORT)Source) << 16) | RtlUshortByteSwap((USHORT)(Source >> 16));
220 #endif
221 }
222
223
224 /*************************************************************************
225 * RtlUlonglongByteSwap
226 *
227 * Swap the bytes of an unsigned long long value.
228 *
229 * PARAMS
230 * i [I] Value to swap bytes of
231 *
232 * RETURNS
233 * The value with its bytes swapped.
234 *
235 * @implemented
236 */
237 ULONGLONG FASTCALL
238 RtlUlonglongByteSwap (IN ULONGLONG Source)
239 {
240 return ((ULONGLONG) RtlUlongByteSwap (Source) << 32) | RtlUlongByteSwap (Source>>32);
241 }
242
243
244 /* EOF */