]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiSortLib/UefiSortLib.c
ShellPkg: Fix EFIAPI usage inconsistencies
[mirror_edk2.git] / ShellPkg / Library / UefiSortLib / UefiSortLib.c
CommitLineData
a4ee2a4f 1/** @file\r
2 Library used for sorting routines.\r
3\r
a405b86d 4 Copyright (c) 2009 - 2010, 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 28STATIC EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *mDevicePathToText = NULL;\r
29STATIC 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
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
1e6e84c7 50 @param[in] CompareFunction The function to call to perform the comparison\r
a4ee2a4f 51 of any 2 elements\r
52 @param[in] Buffer Buffer of size ElementSize for use in swapping\r
53**/\r
54VOID\r
55EFIAPI\r
56QuickSortWorker (\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
121 QuickSortWorker(\r
1e6e84c7 122 BufferToSort,\r
123 NextSwapLocation,\r
124 ElementSize,\r
a4ee2a4f 125 CompareFunction,\r
126 Buffer);\r
127\r
128 QuickSortWorker(\r
129 (UINT8 *)BufferToSort + (NextSwapLocation+1) * ElementSize,\r
1e6e84c7 130 Count - NextSwapLocation - 1,\r
131 ElementSize,\r
a4ee2a4f 132 CompareFunction,\r
133 Buffer);\r
134\r
135 return;\r
136}\r
137/**\r
138 Function to perform a Quick Sort alogrithm on a buffer of comparable elements.\r
139\r
140 Each element must be equal sized.\r
141\r
142 if BufferToSort is NULL, then ASSERT.\r
143 if CompareFunction is NULL, then ASSERT.\r
144\r
145 if Count is < 2 then perform no action.\r
146 if Size is < 1 then perform no action.\r
147\r
148 @param[in,out] BufferToSort on call a Buffer of (possibly sorted) elements\r
149 on return a buffer of sorted elements\r
150 @param[in] Count the number of elements in the buffer to sort\r
151 @param[in] ElementSize Size of an element in bytes\r
1e6e84c7 152 @param[in] CompareFunction The function to call to perform the comparison\r
a4ee2a4f 153 of any 2 elements\r
154**/\r
155VOID\r
156EFIAPI\r
157PerformQuickSort (\r
158 IN OUT VOID *BufferToSort,\r
159 IN CONST UINTN Count,\r
160 IN CONST UINTN ElementSize,\r
161 IN SORT_COMPARE CompareFunction\r
125c2cf4 162 )\r
163{\r
a4ee2a4f 164 VOID *Buffer;\r
165\r
166 ASSERT(BufferToSort != NULL);\r
167 ASSERT(CompareFunction != NULL);\r
168\r
169 Buffer = AllocatePool(ElementSize);\r
170 ASSERT(Buffer != NULL);\r
171\r
172 QuickSortWorker(\r
173 BufferToSort,\r
174 Count,\r
175 ElementSize,\r
176 CompareFunction,\r
177 Buffer);\r
178\r
179 FreePool(Buffer);\r
180 return;\r
181}\r
182\r
183/**\r
184 function to compare 2 device paths for use in QuickSort\r
185\r
186 @param[in] Buffer1 pointer to Device Path poiner to compare\r
187 @param[in] Buffer2 pointer to second DevicePath pointer to compare\r
188\r
189 @retval 0 Buffer1 equal to Buffer2\r
190 @return < 0 Buffer1 is less than Buffer2\r
1e6e84c7 191 @return > 0 Buffer1 is greater than Buffer2\r
a4ee2a4f 192**/\r
193INTN\r
e26d7b59 194EFIAPI\r
a4ee2a4f 195DevicePathCompare (\r
b3011f40 196 IN CONST VOID *Buffer1,\r
197 IN CONST VOID *Buffer2\r
125c2cf4 198 )\r
199{\r
a4ee2a4f 200 EFI_DEVICE_PATH_PROTOCOL *DevicePath1;\r
201 EFI_DEVICE_PATH_PROTOCOL *DevicePath2;\r
202 CHAR16 *TextPath1;\r
203 CHAR16 *TextPath2;\r
204 EFI_STATUS Status;\r
205 INTN RetVal;\r
1e6e84c7 206\r
a4ee2a4f 207 DevicePath1 = *(EFI_DEVICE_PATH_PROTOCOL**)Buffer1;\r
208 DevicePath2 = *(EFI_DEVICE_PATH_PROTOCOL**)Buffer2;\r
209\r
210 if (DevicePath1 == NULL) {\r
211 if (DevicePath2 == NULL) {\r
212 return 0;\r
213 }\r
214\r
215 return -1;\r
216 }\r
217\r
218 if (DevicePath2 == NULL) {\r
219 return 1;\r
220 }\r
1e6e84c7 221\r
11d2decf 222 if (mDevicePathToText == NULL) {\r
a4ee2a4f 223 Status = gBS->LocateProtocol(\r
224 &gEfiDevicePathToTextProtocolGuid,\r
225 NULL,\r
11d2decf 226 (VOID**)&mDevicePathToText);\r
a4ee2a4f 227\r
228 ASSERT_EFI_ERROR(Status);\r
229 }\r
230\r
11d2decf 231 if (mUnicodeCollation == NULL) {\r
a4ee2a4f 232 Status = gBS->LocateProtocol(\r
233 &gEfiUnicodeCollation2ProtocolGuid,\r
234 NULL,\r
11d2decf 235 (VOID**)&mUnicodeCollation);\r
a4ee2a4f 236\r
237 ASSERT_EFI_ERROR(Status);\r
238 }\r
239\r
11d2decf 240 TextPath1 = mDevicePathToText->ConvertDevicePathToText(\r
a4ee2a4f 241 DevicePath1,\r
242 FALSE,\r
243 FALSE);\r
244\r
11d2decf 245 TextPath2 = mDevicePathToText->ConvertDevicePathToText(\r
a4ee2a4f 246 DevicePath2,\r
247 FALSE,\r
248 FALSE);\r
1e6e84c7 249\r
11d2decf 250 RetVal = mUnicodeCollation->StriColl(\r
251 mUnicodeCollation,\r
a4ee2a4f 252 TextPath1,\r
253 TextPath2);\r
254\r
255 FreePool(TextPath1);\r
256 FreePool(TextPath2);\r
257\r
258 return (RetVal);\r
11d2decf 259}\r
260\r
261/**\r
262 Function to compare 2 strings without regard to case of the characters.\r
263\r
264 @param[in] Buffer1 Pointer to String to compare.\r
265 @param[in] Buffer2 Pointer to second String to compare.\r
266\r
267 @retval 0 Buffer1 equal to Buffer2.\r
268 @return < 0 Buffer1 is less than Buffer2.\r
1e6e84c7 269 @return > 0 Buffer1 is greater than Buffer2.\r
11d2decf 270**/\r
271INTN\r
272EFIAPI\r
273StringNoCaseCompare (\r
b3011f40 274 IN CONST VOID *Buffer1,\r
275 IN CONST VOID *Buffer2\r
11d2decf 276 )\r
277{\r
278 EFI_STATUS Status;\r
279 if (mUnicodeCollation == NULL) {\r
280 Status = gBS->LocateProtocol(\r
281 &gEfiUnicodeCollation2ProtocolGuid,\r
282 NULL,\r
283 (VOID**)&mUnicodeCollation);\r
284\r
285 ASSERT_EFI_ERROR(Status);\r
286 }\r
287\r
288 return (mUnicodeCollation->StriColl(\r
289 mUnicodeCollation,\r
290 *(CHAR16**)Buffer1,\r
291 *(CHAR16**)Buffer2));\r
292}\r
293\r
294\r
a405b86d 295/**\r
296 Function to compare 2 strings.\r
297\r
298 @param[in] Buffer1 Pointer to String to compare (CHAR16**).\r
299 @param[in] Buffer2 Pointer to second String to compare (CHAR16**).\r
300\r
301 @retval 0 Buffer1 equal to Buffer2.\r
302 @return < 0 Buffer1 is less than Buffer2.\r
303 @return > 0 Buffer1 is greater than Buffer2.\r
304**/\r
305INTN\r
306EFIAPI\r
307StringCompare (\r
308 IN CONST VOID *Buffer1,\r
309 IN CONST VOID *Buffer2\r
310 )\r
311{\r
312 return (StrCmp(\r
313 *(CHAR16**)Buffer1,\r
314 *(CHAR16**)Buffer2));\r
315}\r