glib.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // 内核全局通用库
  3. // Created by longjin on 2022/1/22.
  4. //
  5. #pragma once
  6. #define NULL 0
  7. #define sti() __asm__ __volatile__("sti\n\t" :: \
  8. : "memory") //开启外部中断
  9. #define cli() __asm__ __volatile__("cli\n\t" :: \
  10. : "memory") //关闭外部中断
  11. #define nop() __asm__ __volatile__("nop\n\t")
  12. //内存屏障
  13. #define io_mfence() __asm__ __volatile__("mfence\n\t" :: \
  14. : "memory") // 在mfence指令前的读写操作当必须在mfence指令后的读写操作前完成。
  15. #define io_sfence() __asm__ __volatile__("sfence\n\t" :: \
  16. : "memory") // 在sfence指令前的写操作当必须在sfence指令后的写操作前完成
  17. #define io_lfence() __asm__ __volatile__("lfence\n\t" :: \
  18. : "memory") // 在lfence指令前的读操作当必须在lfence指令后的读操作前完成。
  19. //链表数据结构
  20. struct List
  21. {
  22. struct List *prev, *next;
  23. };
  24. //初始化循环链表
  25. static inline void list_init(struct List *list)
  26. {
  27. list->next = list;
  28. list->prev = list;
  29. }
  30. static inline void list_add(struct List *entry, struct List *node)
  31. {
  32. /**
  33. * @brief 将node插入到entry后面
  34. * @param entry 给定的节点
  35. * @param node 待插入的节点
  36. */
  37. node->next = entry->next;
  38. node->next->prev = node;
  39. node->prev = entry;
  40. entry->next = node;
  41. }
  42. static inline void list_append(struct List *entry, struct List *node)
  43. {
  44. /**
  45. * @brief 将node添加到给定的list的结尾(也就是当前节点的前面)
  46. * @param entry 列表的入口
  47. * @param node 待添加的节点
  48. */
  49. struct List *tail = entry->prev;
  50. list_add(tail, node);
  51. }
  52. static inline void list_del(struct List *entry)
  53. {
  54. /**
  55. * @brief 从列表中删除节点
  56. * @param entry 待删除的节点
  57. */
  58. entry->prev->next = entry->next;
  59. entry->next = entry->prev;
  60. }
  61. static inline bool list_empty(struct List* entry)
  62. {
  63. /**
  64. * @brief 判断循环链表是否为空
  65. * @param entry 入口
  66. */
  67. if(entry->prev == entry->next)
  68. return true;
  69. else return false;
  70. }