list.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "list.h"
  2. #include "../../kernel/interrupt.h"
  3. // 初始化双向链表list
  4. void list_init(struct list *plist)
  5. {
  6. plist->head.prev = NULL;
  7. plist->head.next = &plist->tail;
  8. plist->tail.prev = &plist->head;
  9. plist->tail.next = NULL;
  10. }
  11. // 将链表元素elem 插入在元素before之前
  12. void list_insert_before(struct list_elem *before, struct list_elem *elem)
  13. {
  14. enum intr_status old_status = intr_disable(); // 关中断
  15. before->prev->next = elem; // before 暂时脱离链表
  16. elem->prev = before->prev;
  17. elem->next = before;
  18. before->prev = elem; // before重新回到链表
  19. intr_set_status(old_status); // 恢复中断
  20. }
  21. // 将元素elem添加到链表plist队首,类似栈push操作
  22. void list_push(struct list *plist, struct list_elem *elem)
  23. {
  24. list_insert_before(plist->head.next, elem); // 在队头插入elem
  25. }
  26. void list_iterate(struct list *plist);
  27. // 添加元素到链表队尾
  28. void list_append(struct list *plist, struct list_elem *elem)
  29. {
  30. list_insert_before(&plist->tail, elem); // 在队尾的前面插入
  31. }
  32. // 从链表中删除元素pelem
  33. void list_remove(struct list_elem *pelem)
  34. {
  35. enum intr_status old_status = intr_disable(); // 关中断
  36. pelem->prev->next = pelem->next;
  37. pelem->next->prev = pelem->prev;
  38. intr_set_status(old_status); // 恢复中断
  39. }
  40. // 将链表第一个元素弹出并返回
  41. struct list_elem *list_pop(struct list *plist)
  42. {
  43. struct list_elem *elem = plist->head.next;
  44. list_remove(elem);
  45. return elem;
  46. }
  47. // 判断链表是否为空,空返回true,否则返回false
  48. bool list_empty(struct list *plist)
  49. {
  50. return (plist->head.next == &plist->tail ? true : false);
  51. }
  52. // 返回链表长度
  53. uint32_t list_len(struct list *plist)
  54. {
  55. struct list_elem *elem = plist->head.next;
  56. uint32_t length = 0;
  57. while (elem != &plist->tail)
  58. {
  59. length++;
  60. elem = elem->next;
  61. }
  62. return length;
  63. }
  64. // 把列表plist中的每个元素elem和arg传给func,
  65. // func 判断是否符合条件
  66. // 如果找到符合条件的元素返回元素指针,否则返回NULL
  67. struct list_elem *list_traversal(struct list *plist, trav_func func, int arg)
  68. {
  69. struct list_elem *elem = plist->head.next;
  70. // 如果链表为空,直接返回NULL
  71. if (list_empty(plist))
  72. {
  73. return NULL;
  74. }
  75. while (elem != &plist->tail)
  76. {
  77. if (func(elem, arg))
  78. {
  79. // func返回true,直接返回元素指针
  80. return elem;
  81. }
  82. elem = elem->next;
  83. }
  84. return NULL;
  85. }
  86. // 查找元素是否在链表中,成功返回true,失败返回false
  87. bool elem_find(struct list *plist, struct list_elem *obj_elem)
  88. {
  89. struct list_elem *elem = plist->head.next;
  90. while (elem != &plist->tail)
  91. {
  92. if (elem == obj_elem)
  93. {
  94. return true;
  95. }
  96. elem = elem->next;
  97. }
  98. return false;
  99. }