glib.h 2.6 KB

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