Ver código fonte

[list] Add list_is_first_entry() and list_is_last_entry()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 6 anos atrás
pai
commit
6be010d919
2 arquivos alterados com 43 adições e 0 exclusões
  1. 22
    0
      src/include/ipxe/list.h
  2. 21
    0
      src/tests/list_test.c

+ 22
- 0
src/include/ipxe/list.h Ver arquivo

@@ -376,6 +376,28 @@ extern void extern_list_splice_tail_init ( struct list_head *list,
376 376
 					 member );		\
377 377
 	( ( &prev->member == (head) ) ? NULL : prev ); } )
378 378
 
379
+/**
380
+ * Test if entry is first in a list
381
+ *
382
+ * @v entry		List entry
383
+ * @v head		List head
384
+ * @v member		Name of list field within iterator's type
385
+ * @ret is_first	Entry is first in the list
386
+ */
387
+#define list_is_first_entry( entry, head, member )		\
388
+	( (head)->next == &(entry)->member )
389
+
390
+/**
391
+ * Test if entry is last in a list
392
+ *
393
+ * @v entry		List entry
394
+ * @v head		List head
395
+ * @v member		Name of list field within iterator's type
396
+ * @ret is_last		Entry is last in the list
397
+ */
398
+#define list_is_last_entry( entry, head, member )		\
399
+	( (head)->prev == &(entry)->member )
400
+
379 401
 /**
380 402
  * Iterate over a list
381 403
  *

+ 21
- 0
src/tests/list_test.c Ver arquivo

@@ -419,6 +419,27 @@ static void list_test_exec ( void ) {
419 419
 	ok ( list_prev_entry ( &list_tests[1], list, list ) == &list_tests[5] );
420 420
 	ok ( list_next_entry ( &list_tests[1], list, list ) == NULL );
421 421
 
422
+	/* Test list_is_first_entry() and list_is_last_entry() */
423
+	INIT_LIST_HEAD ( list );
424
+	list_add_tail ( &list_tests[4].list, list );
425
+	list_add_tail ( &list_tests[8].list, list );
426
+	list_add_tail ( &list_tests[3].list, list );
427
+	list_add_tail ( &list_tests[6].list, list );
428
+	ok ( list_is_first_entry ( &list_tests[4], list, list ) );
429
+	ok ( ! list_is_first_entry ( &list_tests[8], list, list ) );
430
+	ok ( ! list_is_first_entry ( &list_tests[3], list, list ) );
431
+	ok ( ! list_is_first_entry ( &list_tests[6], list, list ) );
432
+	ok ( ! list_is_last_entry ( &list_tests[4], list, list ) );
433
+	ok ( ! list_is_last_entry ( &list_tests[8], list, list ) );
434
+	ok ( ! list_is_last_entry ( &list_tests[3], list, list ) );
435
+	ok ( list_is_last_entry ( &list_tests[6], list, list ) );
436
+	list_del ( &list_tests[4].list );
437
+	ok ( list_is_first_entry ( &list_tests[8], list, list ) );
438
+	list_del ( &list_tests[8].list );
439
+	list_del ( &list_tests[6].list );
440
+	ok ( list_is_first_entry ( &list_tests[3], list, list ) );
441
+	ok ( list_is_last_entry ( &list_tests[3], list, list ) );
442
+
422 443
 	/* Test list_for_each() */
423 444
 	INIT_LIST_HEAD ( list );
424 445
 	list_add_tail ( &list_tests[6].list, list );

Carregando…
Cancelar
Salvar