]>
Commit | Line | Data |
---|---|---|
a4ee2a4f | 1 | /** @file\r |
2 | Library used for sorting routines.\r | |
3 | \r | |
9586a351 | 4 | Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved. <BR>\r |
1e6e84c7 | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
a4ee2a4f | 9 | \r |
1e6e84c7 | 10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
a4ee2a4f | 12 | \r |
13 | **/\r | |
14 | \r | |
15 | #include <Uefi.h>\r | |
16 | \r | |
17 | #include <Protocol/UnicodeCollation.h>\r | |
18 | #include <Protocol/DevicePath.h>\r | |
19 | #include <Protocol/DevicePathToText.h>\r | |
20 | \r | |
21 | #include <Library/UefiBootServicesTableLib.h>\r | |
22 | #include <Library/BaseLib.h>\r | |
23 | #include <Library/BaseMemoryLib.h>\r | |
24 | #include <Library/DebugLib.h>\r | |
25 | #include <Library/MemoryAllocationLib.h>\r | |
1e6e84c7 | 26 | #include <Library/SortLib.h>\r |
a4ee2a4f | 27 | \r |
11d2decf | 28 | STATIC EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *mDevicePathToText = NULL;\r |
29 | STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollation = NULL;\r | |
30 | \r | |
31 | \r | |
a4ee2a4f | 32 | /**\r |
1e6e84c7 | 33 | Worker function for QuickSorting. This function is identical to PerformQuickSort,\r |
34 | except that is uses the pre-allocated buffer so the in place sorting does not need to\r | |
a4ee2a4f | 35 | allocate and free buffers constantly.\r |
36 | \r | |
37 | Each element must be equal sized.\r | |
38 | \r | |
39 | if BufferToSort is NULL, then ASSERT.\r | |
40 | if CompareFunction is NULL, then ASSERT.\r | |
41 | if Buffer is NULL, then ASSERT.\r | |
42 | \r | |
43 | if Count is < 2 then perform no action.\r | |
44 | if Size is < 1 then perform no action.\r | |
45 | \r | |
4ff7e37b ED |
46 | @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements\r |
47 | on return a buffer of sorted elements\r | |
48 | @param[in] Count the number of elements in the buffer to sort\r | |
49 | @param[in] ElementSize Size of an element in bytes\r | |
50 | @param[in] CompareFunction The function to call to perform the comparison\r | |
51 | of any 2 elements\r | |
52 | @param[in] Buffer Buffer of size ElementSize for use in swapping\r | |
a4ee2a4f | 53 | **/\r |
54 | VOID\r | |
55 | EFIAPI\r | |
56 | QuickSortWorker (\r | |
57 | IN OUT VOID *BufferToSort,\r | |
58 | IN CONST UINTN Count,\r | |
59 | IN CONST UINTN ElementSize,\r | |
60 | IN SORT_COMPARE CompareFunction,\r | |
61 | IN VOID *Buffer\r | |
125c2cf4 | 62 | )\r |
63 | {\r | |
a4ee2a4f | 64 | VOID *Pivot;\r |
65 | UINTN LoopCount;\r | |
66 | UINTN NextSwapLocation;\r | |
67 | \r | |
68 | ASSERT(BufferToSort != NULL);\r | |
69 | ASSERT(CompareFunction != NULL);\r | |
70 | ASSERT(Buffer != NULL);\r | |
71 | \r | |
1e6e84c7 | 72 | if ( Count < 2\r |
a4ee2a4f | 73 | || ElementSize < 1\r |
a405b86d | 74 | ){\r |
a4ee2a4f | 75 | return;\r |
76 | }\r | |
77 | \r | |
78 | NextSwapLocation = 0;\r | |
79 | \r | |
80 | //\r | |
81 | // pick a pivot (we choose last element)\r | |
82 | //\r | |
83 | Pivot = ((UINT8*)BufferToSort+((Count-1)*ElementSize));\r | |
84 | \r | |
85 | //\r | |
86 | // Now get the pivot such that all on "left" are below it\r | |
87 | // and everything "right" are above it\r | |
88 | //\r | |
89 | for ( LoopCount = 0\r | |
1e6e84c7 | 90 | ; LoopCount < Count -1\r |
a4ee2a4f | 91 | ; LoopCount++\r |
a405b86d | 92 | ){\r |
a4ee2a4f | 93 | //\r |
94 | // if the element is less than the pivot\r | |
95 | //\r | |
96 | if (CompareFunction((VOID*)((UINT8*)BufferToSort+((LoopCount)*ElementSize)),Pivot) <= 0){\r | |
97 | //\r | |
1e6e84c7 | 98 | // swap\r |
a4ee2a4f | 99 | //\r |
100 | CopyMem (Buffer, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r | |
101 | CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), (UINT8*)BufferToSort+((LoopCount)*ElementSize), ElementSize);\r | |
102 | CopyMem ((UINT8*)BufferToSort+((LoopCount)*ElementSize), Buffer, ElementSize);\r | |
103 | \r | |
104 | //\r | |
105 | // increment NextSwapLocation\r | |
1e6e84c7 | 106 | //\r |
a4ee2a4f | 107 | NextSwapLocation++;\r |
108 | }\r | |
109 | }\r | |
110 | //\r | |
111 | // swap pivot to it's final position (NextSwapLocaiton)\r | |
112 | //\r | |
113 | CopyMem (Buffer, Pivot, ElementSize);\r | |
114 | CopyMem (Pivot, (UINT8*)BufferToSort+(NextSwapLocation*ElementSize), ElementSize);\r | |
115 | CopyMem ((UINT8*)BufferToSort+(NextSwapLocation*ElementSize), Buffer, ElementSize);\r | |
116 | \r | |
117 | //\r | |
1e6e84c7 | 118 | // Now recurse on 2 paritial lists. neither of these will have the 'pivot' element\r |
a4ee2a4f | 119 | // IE list is sorted left half, pivot element, sorted right half...\r |
120 | //\r | |
5dcb5355 | 121 | if (NextSwapLocation >= 2) {\r |
122 | QuickSortWorker(\r | |
123 | BufferToSort,\r | |
124 | NextSwapLocation,\r | |
125 | ElementSize,\r | |
126 | CompareFunction,\r | |
127 | Buffer);\r | |
128 | }\r | |
129 | \r | |
130 | if ((Count - NextSwapLocation - 1) >= 2) {\r | |
131 | QuickSortWorker(\r | |
132 | (UINT8 *)BufferToSort + (NextSwapLocation+1) * ElementSize,\r | |
133 | Count - NextSwapLocation - 1,\r | |
134 | ElementSize,\r | |
135 | CompareFunction,\r | |
136 | Buffer);\r | |
137 | }\r | |
a4ee2a4f | 138 | \r |
139 | return;\r | |
140 | }\r | |
141 | /**\r | |
142 | Function to perform a Quick Sort alogrithm on a buffer of comparable elements.\r | |
143 | \r | |
144 | Each element must be equal sized.\r | |
145 | \r | |
146 | if BufferToSort is NULL, then ASSERT.\r | |
147 | if CompareFunction is NULL, then ASSERT.\r | |
148 | \r | |
149 | if Count is < 2 then perform no action.\r | |
150 | if Size is < 1 then perform no action.\r | |
151 | \r | |
4ff7e37b ED |
152 | @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements\r |
153 | on return a buffer of sorted elements\r | |
154 | @param[in] Count the number of elements in the buffer to sort\r | |
155 | @param[in] ElementSize Size of an element in bytes\r | |
156 | @param[in] CompareFunction The function to call to perform the comparison\r | |
157 | of any 2 elements\r | |
a4ee2a4f | 158 | **/\r |
159 | VOID\r | |
160 | EFIAPI\r | |
161 | PerformQuickSort (\r | |
162 | IN OUT VOID *BufferToSort,\r | |
163 | IN CONST UINTN Count,\r | |
164 | IN CONST UINTN ElementSize,\r | |
165 | IN SORT_COMPARE CompareFunction\r | |
125c2cf4 | 166 | )\r |
167 | {\r | |
a4ee2a4f | 168 | VOID *Buffer;\r |
169 | \r | |
170 | ASSERT(BufferToSort != NULL);\r | |
171 | ASSERT(CompareFunction != NULL);\r | |
172 | \r | |
9586a351 | 173 | Buffer = AllocateZeroPool(ElementSize);\r |
a4ee2a4f | 174 | ASSERT(Buffer != NULL);\r |
175 | \r | |
176 | QuickSortWorker(\r | |
177 | BufferToSort,\r | |
178 | Count,\r | |
179 | ElementSize,\r | |
180 | CompareFunction,\r | |
181 | Buffer);\r | |
182 | \r | |
183 | FreePool(Buffer);\r | |
184 | return;\r | |
185 | }\r | |
186 | \r | |
187 | /**\r | |
da70d025 | 188 | Function to compare 2 device paths for use in QuickSort.\r |
a4ee2a4f | 189 | \r |
190 | @param[in] Buffer1 pointer to Device Path poiner to compare\r | |
191 | @param[in] Buffer2 pointer to second DevicePath pointer to compare\r | |
192 | \r | |
193 | @retval 0 Buffer1 equal to Buffer2\r | |
194 | @return < 0 Buffer1 is less than Buffer2\r | |
1e6e84c7 | 195 | @return > 0 Buffer1 is greater than Buffer2\r |
a4ee2a4f | 196 | **/\r |
197 | INTN\r | |
e26d7b59 | 198 | EFIAPI\r |
a4ee2a4f | 199 | DevicePathCompare (\r |
b3011f40 | 200 | IN CONST VOID *Buffer1,\r |
201 | IN CONST VOID *Buffer2\r | |
125c2cf4 | 202 | )\r |
203 | {\r | |
a4ee2a4f | 204 | EFI_DEVICE_PATH_PROTOCOL *DevicePath1;\r |
205 | EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r | |
206 | CHAR16 *TextPath1;\r | |
207 | CHAR16 *TextPath2;\r | |
208 | EFI_STATUS Status;\r | |
209 | INTN RetVal;\r | |
1e6e84c7 | 210 | \r |
a4ee2a4f | 211 | DevicePath1 = *(EFI_DEVICE_PATH_PROTOCOL**)Buffer1;\r |
212 | DevicePath2 = *(EFI_DEVICE_PATH_PROTOCOL**)Buffer2;\r | |
213 | \r | |
214 | if (DevicePath1 == NULL) {\r | |
215 | if (DevicePath2 == NULL) {\r | |
216 | return 0;\r | |
217 | }\r | |
218 | \r | |
219 | return -1;\r | |
220 | }\r | |
221 | \r | |
222 | if (DevicePath2 == NULL) {\r | |
223 | return 1;\r | |
224 | }\r | |
1e6e84c7 | 225 | \r |
11d2decf | 226 | if (mDevicePathToText == NULL) {\r |
a4ee2a4f | 227 | Status = gBS->LocateProtocol(\r |
228 | &gEfiDevicePathToTextProtocolGuid,\r | |
229 | NULL,\r | |
11d2decf | 230 | (VOID**)&mDevicePathToText);\r |
a4ee2a4f | 231 | \r |
232 | ASSERT_EFI_ERROR(Status);\r | |
233 | }\r | |
234 | \r | |
11d2decf | 235 | if (mUnicodeCollation == NULL) {\r |
a4ee2a4f | 236 | Status = gBS->LocateProtocol(\r |
237 | &gEfiUnicodeCollation2ProtocolGuid,\r | |
238 | NULL,\r | |
11d2decf | 239 | (VOID**)&mUnicodeCollation);\r |
a4ee2a4f | 240 | \r |
241 | ASSERT_EFI_ERROR(Status);\r | |
242 | }\r | |
243 | \r | |
11d2decf | 244 | TextPath1 = mDevicePathToText->ConvertDevicePathToText(\r |
a4ee2a4f | 245 | DevicePath1,\r |
246 | FALSE,\r | |
247 | FALSE);\r | |
248 | \r | |
11d2decf | 249 | TextPath2 = mDevicePathToText->ConvertDevicePathToText(\r |
a4ee2a4f | 250 | DevicePath2,\r |
251 | FALSE,\r | |
252 | FALSE);\r | |
1e6e84c7 | 253 | \r |
11d2decf | 254 | RetVal = mUnicodeCollation->StriColl(\r |
255 | mUnicodeCollation,\r | |
a4ee2a4f | 256 | TextPath1,\r |
257 | TextPath2);\r | |
258 | \r | |
259 | FreePool(TextPath1);\r | |
260 | FreePool(TextPath2);\r | |
261 | \r | |
262 | return (RetVal);\r | |
11d2decf | 263 | }\r |
264 | \r | |
265 | /**\r | |
266 | Function to compare 2 strings without regard to case of the characters.\r | |
267 | \r | |
268 | @param[in] Buffer1 Pointer to String to compare.\r | |
269 | @param[in] Buffer2 Pointer to second String to compare.\r | |
270 | \r | |
271 | @retval 0 Buffer1 equal to Buffer2.\r | |
272 | @return < 0 Buffer1 is less than Buffer2.\r | |
1e6e84c7 | 273 | @return > 0 Buffer1 is greater than Buffer2.\r |
11d2decf | 274 | **/\r |
275 | INTN\r | |
276 | EFIAPI\r | |
277 | StringNoCaseCompare (\r | |
b3011f40 | 278 | IN CONST VOID *Buffer1,\r |
279 | IN CONST VOID *Buffer2\r | |
11d2decf | 280 | )\r |
281 | {\r | |
282 | EFI_STATUS Status;\r | |
283 | if (mUnicodeCollation == NULL) {\r | |
284 | Status = gBS->LocateProtocol(\r | |
285 | &gEfiUnicodeCollation2ProtocolGuid,\r | |
286 | NULL,\r | |
287 | (VOID**)&mUnicodeCollation);\r | |
288 | \r | |
289 | ASSERT_EFI_ERROR(Status);\r | |
290 | }\r | |
291 | \r | |
292 | return (mUnicodeCollation->StriColl(\r | |
293 | mUnicodeCollation,\r | |
294 | *(CHAR16**)Buffer1,\r | |
295 | *(CHAR16**)Buffer2));\r | |
296 | }\r | |
297 | \r | |
298 | \r | |
a405b86d | 299 | /**\r |
300 | Function to compare 2 strings.\r | |
301 | \r | |
302 | @param[in] Buffer1 Pointer to String to compare (CHAR16**).\r | |
303 | @param[in] Buffer2 Pointer to second String to compare (CHAR16**).\r | |
304 | \r | |
305 | @retval 0 Buffer1 equal to Buffer2.\r | |
306 | @return < 0 Buffer1 is less than Buffer2.\r | |
307 | @return > 0 Buffer1 is greater than Buffer2.\r | |
308 | **/\r | |
309 | INTN\r | |
310 | EFIAPI\r | |
311 | StringCompare (\r | |
312 | IN CONST VOID *Buffer1,\r | |
313 | IN CONST VOID *Buffer2\r | |
314 | )\r | |
315 | {\r | |
316 | return (StrCmp(\r | |
317 | *(CHAR16**)Buffer1,\r | |
318 | *(CHAR16**)Buffer2));\r | |
319 | }\r |