]>
Commit | Line | Data |
---|---|---|
250e4b0d KM |
1 | /** @file\r |
2 | An ordered collection library interface.\r | |
3 | \r | |
4 | The library class provides a set of APIs to manage an ordered collection of\r | |
5 | items.\r | |
6 | \r | |
7 | Copyright (C) 2014, Red Hat, Inc.\r | |
8 | \r | |
9 | This program and the accompanying materials are licensed and made available\r | |
10 | under the terms and conditions of the BSD License that accompanies this\r | |
11 | distribution. The full text of the license may be found at\r | |
12 | http://opensource.org/licenses/bsd-license.php.\r | |
13 | \r | |
14 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r | |
15 | WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
16 | **/\r | |
17 | \r | |
18 | #ifndef __ORDERED_COLLECTION_LIB__\r | |
19 | #define __ORDERED_COLLECTION_LIB__\r | |
20 | \r | |
21 | #include <Base.h>\r | |
22 | \r | |
23 | //\r | |
24 | // Opaque structure for a collection.\r | |
25 | //\r | |
26 | typedef struct ORDERED_COLLECTION ORDERED_COLLECTION;\r | |
27 | \r | |
28 | //\r | |
29 | // Opaque structure for collection entries.\r | |
30 | //\r | |
31 | // Collection entries do not take ownership of the associated user structures,\r | |
32 | // they only link them. This makes it easy to link the same user structure into\r | |
33 | // several collections. If reference counting is required, the caller is\r | |
34 | // responsible for implementing it, as part of the user structure.\r | |
35 | //\r | |
36 | // A pointer-to-ORDERED_COLLECTION_ENTRY is considered an "iterator". Multiple,\r | |
37 | // simultaneous iterations are supported.\r | |
38 | //\r | |
39 | typedef struct ORDERED_COLLECTION_ENTRY ORDERED_COLLECTION_ENTRY;\r | |
40 | \r | |
41 | //\r | |
42 | // Altering the key field of an in-collection user structure (ie. the portion\r | |
43 | // of the user structure that ORDERED_COLLECTION_USER_COMPARE and\r | |
44 | // ORDERED_COLLECTION_KEY_COMPARE, below, read) is not allowed in-place. The\r | |
45 | // caller is responsible for bracketing the key change with the deletion and\r | |
46 | // the reinsertion of the user structure, so that the changed key value is\r | |
47 | // reflected in the collection.\r | |
48 | //\r | |
49 | \r | |
50 | /**\r | |
51 | Comparator function type for two user structures.\r | |
52 | \r | |
53 | @param[in] UserStruct1 Pointer to the first user structure.\r | |
54 | \r | |
55 | @param[in] UserStruct2 Pointer to the second user structure.\r | |
56 | \r | |
57 | @retval <0 If UserStruct1 compares less than UserStruct2.\r | |
58 | \r | |
59 | @retval 0 If UserStruct1 compares equal to UserStruct2.\r | |
60 | \r | |
61 | @retval >0 If UserStruct1 compares greater than UserStruct2.\r | |
62 | **/\r | |
63 | typedef\r | |
64 | INTN\r | |
65 | (EFIAPI *ORDERED_COLLECTION_USER_COMPARE)(\r | |
66 | IN CONST VOID *UserStruct1,\r | |
67 | IN CONST VOID *UserStruct2\r | |
68 | );\r | |
69 | \r | |
70 | /**\r | |
71 | Compare a standalone key against a user structure containing an embedded key.\r | |
72 | \r | |
73 | @param[in] StandaloneKey Pointer to the bare key.\r | |
74 | \r | |
75 | @param[in] UserStruct Pointer to the user structure with the embedded\r | |
76 | key.\r | |
77 | \r | |
78 | @retval <0 If StandaloneKey compares less than UserStruct's key.\r | |
79 | \r | |
80 | @retval 0 If StandaloneKey compares equal to UserStruct's key.\r | |
81 | \r | |
82 | @retval >0 If StandaloneKey compares greater than UserStruct's key.\r | |
83 | **/\r | |
84 | typedef\r | |
85 | INTN\r | |
86 | (EFIAPI *ORDERED_COLLECTION_KEY_COMPARE)(\r | |
87 | IN CONST VOID *StandaloneKey,\r | |
88 | IN CONST VOID *UserStruct\r | |
89 | );\r | |
90 | \r | |
91 | \r | |
92 | //\r | |
93 | // Some functions below are read-only, while others are read-write. If any\r | |
94 | // write operation is expected to run concurrently with any other operation on\r | |
95 | // the same collection, then the caller is responsible for implementing locking\r | |
96 | // for the whole collection.\r | |
97 | //\r | |
98 | \r | |
99 | /**\r | |
100 | Retrieve the user structure linked by the specified collection entry.\r | |
101 | \r | |
102 | Read-only operation.\r | |
103 | \r | |
104 | @param[in] Entry Pointer to the collection entry whose associated user\r | |
105 | structure we want to retrieve. The caller is responsible\r | |
106 | for passing a non-NULL argument.\r | |
107 | \r | |
108 | @return Pointer to user structure linked by Entry.\r | |
109 | **/\r | |
110 | VOID *\r | |
111 | EFIAPI\r | |
112 | OrderedCollectionUserStruct (\r | |
113 | IN CONST ORDERED_COLLECTION_ENTRY *Entry\r | |
114 | );\r | |
115 | \r | |
116 | \r | |
117 | /**\r | |
118 | Allocate and initialize the ORDERED_COLLECTION structure.\r | |
119 | \r | |
120 | @param[in] UserStructCompare This caller-provided function will be used to\r | |
121 | order two user structures linked into the\r | |
122 | collection, during the insertion procedure.\r | |
123 | \r | |
124 | @param[in] KeyCompare This caller-provided function will be used to\r | |
125 | order the standalone search key against user\r | |
126 | structures linked into the collection, during\r | |
127 | the lookup procedure.\r | |
128 | \r | |
129 | @retval NULL If allocation failed.\r | |
130 | \r | |
131 | @return Pointer to the allocated, initialized ORDERED_COLLECTION\r | |
132 | structure, otherwise.\r | |
133 | **/\r | |
134 | ORDERED_COLLECTION *\r | |
135 | EFIAPI\r | |
136 | OrderedCollectionInit (\r | |
137 | IN ORDERED_COLLECTION_USER_COMPARE UserStructCompare,\r | |
138 | IN ORDERED_COLLECTION_KEY_COMPARE KeyCompare\r | |
139 | );\r | |
140 | \r | |
141 | \r | |
142 | /**\r | |
143 | Check whether the collection is empty (has no entries).\r | |
144 | \r | |
145 | Read-only operation.\r | |
146 | \r | |
147 | @param[in] Collection The collection to check for emptiness.\r | |
148 | \r | |
149 | @retval TRUE The collection is empty.\r | |
150 | \r | |
151 | @retval FALSE The collection is not empty.\r | |
152 | **/\r | |
153 | BOOLEAN\r | |
154 | EFIAPI\r | |
155 | OrderedCollectionIsEmpty (\r | |
156 | IN CONST ORDERED_COLLECTION *Collection\r | |
157 | );\r | |
158 | \r | |
159 | \r | |
160 | /**\r | |
161 | Uninitialize and release an empty ORDERED_COLLECTION structure.\r | |
162 | \r | |
163 | Read-write operation.\r | |
164 | \r | |
165 | It is the caller's responsibility to delete all entries from the collection\r | |
166 | before calling this function.\r | |
167 | \r | |
168 | @param[in] Collection The empty collection to uninitialize and release.\r | |
169 | **/\r | |
170 | VOID\r | |
171 | EFIAPI\r | |
172 | OrderedCollectionUninit (\r | |
173 | IN ORDERED_COLLECTION *Collection\r | |
174 | );\r | |
175 | \r | |
176 | \r | |
177 | /**\r | |
178 | Look up the collection entry that links the user structure that matches the\r | |
179 | specified standalone key.\r | |
180 | \r | |
181 | Read-only operation.\r | |
182 | \r | |
183 | @param[in] Collection The collection to search for StandaloneKey.\r | |
184 | \r | |
185 | @param[in] StandaloneKey The key to locate among the user structures linked\r | |
186 | into Collection. StandaloneKey will be passed to\r | |
187 | ORDERED_COLLECTION_KEY_COMPARE.\r | |
188 | \r | |
189 | @retval NULL StandaloneKey could not be found.\r | |
190 | \r | |
191 | @return The collection entry that links to the user structure matching\r | |
192 | StandaloneKey, otherwise.\r | |
193 | **/\r | |
194 | ORDERED_COLLECTION_ENTRY *\r | |
195 | EFIAPI\r | |
196 | OrderedCollectionFind (\r | |
197 | IN CONST ORDERED_COLLECTION *Collection,\r | |
198 | IN CONST VOID *StandaloneKey\r | |
199 | );\r | |
200 | \r | |
201 | \r | |
202 | /**\r | |
203 | Find the collection entry of the minimum user structure stored in the\r | |
204 | collection.\r | |
205 | \r | |
206 | Read-only operation.\r | |
207 | \r | |
208 | @param[in] Collection The collection to return the minimum entry of. The\r | |
209 | user structure linked by the minimum entry compares\r | |
210 | less than all other user structures in the collection.\r | |
211 | \r | |
212 | @retval NULL If Collection is empty.\r | |
213 | \r | |
214 | @return The collection entry that links the minimum user structure,\r | |
215 | otherwise.\r | |
216 | **/\r | |
217 | ORDERED_COLLECTION_ENTRY *\r | |
218 | EFIAPI\r | |
219 | OrderedCollectionMin (\r | |
220 | IN CONST ORDERED_COLLECTION *Collection\r | |
221 | );\r | |
222 | \r | |
223 | \r | |
224 | /**\r | |
225 | Find the collection entry of the maximum user structure stored in the\r | |
226 | collection.\r | |
227 | \r | |
228 | Read-only operation.\r | |
229 | \r | |
230 | @param[in] Collection The collection to return the maximum entry of. The\r | |
231 | user structure linked by the maximum entry compares\r | |
232 | greater than all other user structures in the\r | |
233 | collection.\r | |
234 | \r | |
235 | @retval NULL If Collection is empty.\r | |
236 | \r | |
237 | @return The collection entry that links the maximum user structure,\r | |
238 | otherwise.\r | |
239 | **/\r | |
240 | ORDERED_COLLECTION_ENTRY *\r | |
241 | EFIAPI\r | |
242 | OrderedCollectionMax (\r | |
243 | IN CONST ORDERED_COLLECTION *Collection\r | |
244 | );\r | |
245 | \r | |
246 | \r | |
247 | /**\r | |
248 | Get the collection entry of the least user structure that is greater than the\r | |
249 | one linked by Entry.\r | |
250 | \r | |
251 | Read-only operation.\r | |
252 | \r | |
253 | @param[in] Entry The entry to get the successor entry of.\r | |
254 | \r | |
255 | @retval NULL If Entry is NULL, or Entry is the maximum entry of its\r | |
256 | containing collection (ie. Entry has no successor entry).\r | |
257 | \r | |
258 | @return The collection entry linking the least user structure that is\r | |
259 | greater than the one linked by Entry, otherwise.\r | |
260 | **/\r | |
261 | ORDERED_COLLECTION_ENTRY *\r | |
262 | EFIAPI\r | |
263 | OrderedCollectionNext (\r | |
264 | IN CONST ORDERED_COLLECTION_ENTRY *Entry\r | |
265 | );\r | |
266 | \r | |
267 | \r | |
268 | /**\r | |
269 | Get the collection entry of the greatest user structure that is less than the\r | |
270 | one linked by Entry.\r | |
271 | \r | |
272 | Read-only operation.\r | |
273 | \r | |
274 | @param[in] Entry The entry to get the predecessor entry of.\r | |
275 | \r | |
276 | @retval NULL If Entry is NULL, or Entry is the minimum entry of its\r | |
277 | containing collection (ie. Entry has no predecessor entry).\r | |
278 | \r | |
279 | @return The collection entry linking the greatest user structure that\r | |
280 | is less than the one linked by Entry, otherwise.\r | |
281 | **/\r | |
282 | ORDERED_COLLECTION_ENTRY *\r | |
283 | EFIAPI\r | |
284 | OrderedCollectionPrev (\r | |
285 | IN CONST ORDERED_COLLECTION_ENTRY *Entry\r | |
286 | );\r | |
287 | \r | |
288 | \r | |
289 | /**\r | |
290 | Insert (link) a user structure into the collection, allocating a new\r | |
291 | collection entry.\r | |
292 | \r | |
293 | Read-write operation.\r | |
294 | \r | |
295 | @param[in,out] Collection The collection to insert UserStruct into.\r | |
296 | \r | |
297 | @param[out] Entry The meaning of this optional, output-only\r | |
298 | parameter depends on the return value of the\r | |
299 | function.\r | |
300 | \r | |
301 | When insertion is successful (RETURN_SUCCESS),\r | |
302 | Entry is set on output to the new collection entry\r | |
303 | that now links UserStruct.\r | |
304 | \r | |
305 | When insertion fails due to lack of memory\r | |
306 | (RETURN_OUT_OF_RESOURCES), Entry is not changed.\r | |
307 | \r | |
308 | When insertion fails due to key collision (ie.\r | |
309 | another user structure is already in the\r | |
310 | collection that compares equal to UserStruct),\r | |
311 | with return value RETURN_ALREADY_STARTED, then\r | |
312 | Entry is set on output to the entry that links the\r | |
313 | colliding user structure. This enables\r | |
314 | "find-or-insert" in one function call, or helps\r | |
315 | with later removal of the colliding element.\r | |
316 | \r | |
317 | @param[in] UserStruct The user structure to link into the collection.\r | |
318 | UserStruct is ordered against in-collection user\r | |
319 | structures with the\r | |
320 | ORDERED_COLLECTION_USER_COMPARE function.\r | |
321 | \r | |
322 | @retval RETURN_SUCCESS Insertion successful. A new collection entry\r | |
323 | has been allocated, linking UserStruct. The\r | |
324 | new collection entry is reported back in\r | |
325 | Entry (if the caller requested it).\r | |
326 | \r | |
327 | Existing ORDERED_COLLECTION_ENTRY pointers\r | |
328 | into Collection remain valid. For example,\r | |
329 | on-going iterations in the caller can\r | |
330 | continue with OrderedCollectionNext() /\r | |
331 | OrderedCollectionPrev(), and they will\r | |
332 | return the new entry at some point if user\r | |
333 | structure order dictates it.\r | |
334 | \r | |
335 | @retval RETURN_OUT_OF_RESOURCES The function failed to allocate memory for\r | |
336 | the new collection entry. The collection has\r | |
337 | not been changed. Existing\r | |
338 | ORDERED_COLLECTION_ENTRY pointers into\r | |
339 | Collection remain valid.\r | |
340 | \r | |
341 | @retval RETURN_ALREADY_STARTED A user structure has been found in the\r | |
342 | collection that compares equal to\r | |
343 | UserStruct. The entry linking the colliding\r | |
344 | user structure is reported back in Entry (if\r | |
345 | the caller requested it). The collection has\r | |
346 | not been changed. Existing\r | |
347 | ORDERED_COLLECTION_ENTRY pointers into\r | |
348 | Collection remain valid.\r | |
349 | **/\r | |
350 | RETURN_STATUS\r | |
351 | EFIAPI\r | |
352 | OrderedCollectionInsert (\r | |
353 | IN OUT ORDERED_COLLECTION *Collection,\r | |
354 | OUT ORDERED_COLLECTION_ENTRY **Entry OPTIONAL,\r | |
355 | IN VOID *UserStruct\r | |
356 | );\r | |
357 | \r | |
358 | \r | |
359 | /**\r | |
360 | Delete an entry from the collection, unlinking the associated user structure.\r | |
361 | \r | |
362 | Read-write operation.\r | |
363 | \r | |
364 | @param[in,out] Collection The collection to delete Entry from.\r | |
365 | \r | |
366 | @param[in] Entry The collection entry to delete from Collection.\r | |
367 | The caller is responsible for ensuring that Entry\r | |
368 | belongs to Collection, and that Entry is non-NULL\r | |
369 | and valid. Entry is typically an earlier return\r | |
370 | value, or output parameter, of:\r | |
371 | \r | |
372 | - OrderedCollectionFind(), for deleting an entry\r | |
373 | by user structure key,\r | |
374 | \r | |
375 | - OrderedCollectionMin() / OrderedCollectionMax(),\r | |
376 | for deleting the minimum / maximum entry,\r | |
377 | \r | |
378 | - OrderedCollectionNext() /\r | |
379 | OrderedCollectionPrev(), for deleting an entry\r | |
380 | found during an iteration,\r | |
381 | \r | |
382 | - OrderedCollectionInsert() with return value\r | |
383 | RETURN_ALREADY_STARTED, for deleting an entry\r | |
384 | whose linked user structure caused collision\r | |
385 | during insertion.\r | |
386 | \r | |
387 | Existing ORDERED_COLLECTION_ENTRY pointers (ie.\r | |
388 | iterators) *different* from Entry remain valid.\r | |
389 | For example:\r | |
390 | \r | |
391 | - OrderedCollectionNext() /\r | |
392 | OrderedCollectionPrev() iterations in the caller\r | |
393 | can be continued from Entry, if\r | |
394 | OrderedCollectionNext() or\r | |
395 | OrderedCollectionPrev() is called on Entry\r | |
396 | *before* OrderedCollectionDelete() is. That is,\r | |
397 | fetch the successor / predecessor entry first,\r | |
398 | then delete Entry.\r | |
399 | \r | |
400 | - On-going iterations in the caller that would\r | |
401 | have otherwise returned Entry at some point, as\r | |
402 | dictated by user structure order, will correctly\r | |
403 | reflect the absence of Entry after\r | |
404 | OrderedCollectionDelete() is called\r | |
405 | mid-iteration.\r | |
406 | \r | |
407 | @param[out] UserStruct If the caller provides this optional output-only\r | |
408 | parameter, then on output it is set to the user\r | |
409 | structure originally linked by Entry (which is now\r | |
410 | freed).\r | |
411 | \r | |
412 | This is a convenience that may save the caller a\r | |
413 | OrderedCollectionUserStruct() invocation before\r | |
414 | calling OrderedCollectionDelete(), in order to\r | |
415 | retrieve the user structure being unlinked.\r | |
416 | **/\r | |
417 | VOID\r | |
418 | EFIAPI\r | |
419 | OrderedCollectionDelete (\r | |
420 | IN OUT ORDERED_COLLECTION *Collection,\r | |
421 | IN ORDERED_COLLECTION_ENTRY *Entry,\r | |
422 | OUT VOID **UserStruct OPTIONAL\r | |
423 | );\r | |
424 | \r | |
425 | #endif\r |