glib.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // 内核全局通用库
  3. // Created by longjin on 2022/1/22.
  4. //
  5. #pragma once
  6. //引入对bool类型的支持
  7. #include <stdbool.h>
  8. #define NULL 0
  9. #define sti() __asm__ __volatile__("sti\n\t" :: \
  10. : "memory") //开启外部中断
  11. #define cli() __asm__ __volatile__("cli\n\t" :: \
  12. : "memory") //关闭外部中断
  13. #define nop() __asm__ __volatile__("nop\n\t")
  14. //内存屏障
  15. #define io_mfence() __asm__ __volatile__("mfence\n\t" :: \
  16. : "memory") // 在mfence指令前的读写操作当必须在mfence指令后的读写操作前完成。
  17. #define io_sfence() __asm__ __volatile__("sfence\n\t" :: \
  18. : "memory") // 在sfence指令前的写操作当必须在sfence指令后的写操作前完成
  19. #define io_lfence() __asm__ __volatile__("lfence\n\t" :: \
  20. : "memory") // 在lfence指令前的读操作当必须在lfence指令后的读操作前完成。
  21. #define ABS(x) ((x) > 0 ? (x) : -(x)) // 绝对值
  22. // 定义类型的缩写
  23. typedef unsigned long ul;
  24. typedef unsigned long long ull;
  25. typedef long long ll;
  26. //链表数据结构
  27. struct List
  28. {
  29. struct List *prev, *next;
  30. };
  31. //初始化循环链表
  32. static inline void list_init(struct List *list)
  33. {
  34. list->next = list;
  35. list->prev = list;
  36. }
  37. static inline void list_add(struct List *entry, struct List *node)
  38. {
  39. /**
  40. * @brief 将node插入到entry后面
  41. * @param entry 给定的节点
  42. * @param node 待插入的节点
  43. */
  44. node->next = entry->next;
  45. node->next->prev = node;
  46. node->prev = entry;
  47. entry->next = node;
  48. }
  49. static inline void list_append(struct List *entry, struct List *node)
  50. {
  51. /**
  52. * @brief 将node添加到给定的list的结尾(也就是当前节点的前面)
  53. * @param entry 列表的入口
  54. * @param node 待添加的节点
  55. */
  56. struct List *tail = entry->prev;
  57. list_add(tail, node);
  58. }
  59. static inline void list_del(struct List *entry)
  60. {
  61. /**
  62. * @brief 从列表中删除节点
  63. * @param entry 待删除的节点
  64. */
  65. entry->prev->next = entry->next;
  66. entry->next = entry->prev;
  67. }
  68. static inline bool list_empty(struct List *entry)
  69. {
  70. /**
  71. * @brief 判断循环链表是否为空
  72. * @param entry 入口
  73. */
  74. if (entry->prev == entry->next)
  75. return true;
  76. else
  77. return false;
  78. }
  79. //计算字符串的长度(经过测试,该版本比采用repne/scasb汇编的运行速度快16.8%左右)
  80. static inline int strlen(char *s)
  81. {
  82. register int __res = 0;
  83. while (s[__res] != '\0')
  84. {
  85. ++__res;
  86. }
  87. return __res;
  88. }