merge ROS Shell without integrated explorer part into trunk
[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 <ddk/ntddk.h>
16 #include <string.h>
17
18 #define NDEBUG
19 #include <debug.h>
20
21
22
23
24 /* FUNCTIONS *****************************************************************/
25
26 /******************************************************************************
27 * RtlCompareMemory [NTDLL.@]
28 *
29 * Compare one block of memory with another
30 *
31 * PARAMS
32 * Source1 [I] Source block
33 * Source2 [I] Block to compare to Source1
34 * Length [I] Number of bytes to fill
35 *
36 * RETURNS
37 * The length of the first byte at which Source1 and Source2 differ, or Length
38 * if they are the same.
39 *
40 * @implemented
41 */
42 SIZE_T STDCALL
43 RtlCompareMemory(IN const VOID *Source1,
44 IN const VOID *Source2,
45 IN SIZE_T Length)
46 {
47 SIZE_T i;
48 for(i=0; (i<Length) && (((PUCHAR)Source1)[i]==((PUCHAR)Source2)[i]); i++)
49 ;
50 return i;
51 }
52
53
54 /*
55 * @implemented
56 */
57 ULONG
58 STDCALL
59 RtlCompareMemoryUlong (
60 PVOID Source,
61 ULONG Length,
62 ULONG Value
63 )
64 /*
65 * FUNCTION: Compares a block of ULONGs with an ULONG and returns the number of equal bytes
66 * ARGUMENTS:
67 * Source = Block to compare
68 * Length = Number of bytes to compare
69 * Value = Value to compare
70 * RETURNS: Number of equal bytes
71 */
72 {
73 PULONG ptr = (PULONG)Source;
74 ULONG len = Length / sizeof(ULONG);
75 ULONG i;
76
77 for (i = 0; i < len; i++)
78 {
79 if (*ptr != Value)
80 break;
81 ptr++;
82 }
83
84 return (ULONG)((PCHAR)ptr - (PCHAR)Source);
85 }
86
87
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
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 * @implemented
161 */
162 VOID
163 STDCALL
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 */