glib.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // 定义类型的缩写
  22. typedef unsigned long ul;
  23. typedef unsigned long long ull;
  24. typedef long long ll;
  25. #define ABS(x) ((x) > 0 ? (x) : -(x)) // 绝对值
  26. // 四舍五入成整数
  27. ul round(double x)
  28. {
  29. return (ul)(x+0.5);
  30. }
  31. //链表数据结构
  32. struct List
  33. {
  34. struct List *prev, *next;
  35. };
  36. //初始化循环链表
  37. static inline void list_init(struct List *list)
  38. {
  39. list->next = list;
  40. list->prev = list;
  41. }
  42. static inline void list_add(struct List *entry, struct List *node)
  43. {
  44. /**
  45. * @brief 将node插入到entry后面
  46. * @param entry 给定的节点
  47. * @param node 待插入的节点
  48. */
  49. node->next = entry->next;
  50. node->next->prev = node;
  51. node->prev = entry;
  52. entry->next = node;
  53. }
  54. static inline void list_append(struct List *entry, struct List *node)
  55. {
  56. /**
  57. * @brief 将node添加到给定的list的结尾(也就是当前节点的前面)
  58. * @param entry 列表的入口
  59. * @param node 待添加的节点
  60. */
  61. struct List *tail = entry->prev;
  62. list_add(tail, node);
  63. }
  64. static inline void list_del(struct List *entry)
  65. {
  66. /**
  67. * @brief 从列表中删除节点
  68. * @param entry 待删除的节点
  69. */
  70. entry->prev->next = entry->next;
  71. entry->next = entry->prev;
  72. }
  73. static inline bool list_empty(struct List *entry)
  74. {
  75. /**
  76. * @brief 判断循环链表是否为空
  77. * @param entry 入口
  78. */
  79. if (entry->prev == entry->next)
  80. return true;
  81. else
  82. return false;
  83. }
  84. //计算字符串的长度(经过测试,该版本比采用repne/scasb汇编的运行速度快16.8%左右)
  85. static inline int strlen(char *s)
  86. {
  87. register int __res = 0;
  88. while (s[__res] != '\0')
  89. {
  90. ++__res;
  91. }
  92. return __res;
  93. }
  94. inline void *memset(void *dst, unsigned char C, ul Count)
  95. {
  96. int d0, d1;
  97. unsigned long tmp = C * 0x0101010101010101UL;
  98. __asm__ __volatile__("cld \n\t"
  99. "rep \n\t"
  100. "stosq \n\t"
  101. "testb $4, %b3 \n\t"
  102. "je 1f \n\t"
  103. "stosl \n\t"
  104. "1:\ttestb $2, %b3 \n\t"
  105. "je 2f\n\t"
  106. "stosw \n\t"
  107. "2:\ttestb $1, %b3 \n\t"
  108. "je 3f \n\t"
  109. "stosb \n\t"
  110. "3: \n\t"
  111. : "=&c"(d0), "=&D"(d1)
  112. : "a"(tmp), "q"(Count), "0"(Count / 8), "1"(dst)
  113. : "memory");
  114. return dst;
  115. }