glib.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. struct List
  23. {
  24. struct List *prev, *next;
  25. };
  26. //初始化循环链表
  27. static inline void list_init(struct List *list)
  28. {
  29. list->next = list;
  30. list->prev = list;
  31. }
  32. static inline void list_add(struct List *entry, struct List *node)
  33. {
  34. /**
  35. * @brief 将node插入到entry后面
  36. * @param entry 给定的节点
  37. * @param node 待插入的节点
  38. */
  39. node->next = entry->next;
  40. node->next->prev = node;
  41. node->prev = entry;
  42. entry->next = node;
  43. }
  44. static inline void list_append(struct List *entry, struct List *node)
  45. {
  46. /**
  47. * @brief 将node添加到给定的list的结尾(也就是当前节点的前面)
  48. * @param entry 列表的入口
  49. * @param node 待添加的节点
  50. */
  51. struct List *tail = entry->prev;
  52. list_add(tail, node);
  53. }
  54. static inline void list_del(struct List *entry)
  55. {
  56. /**
  57. * @brief 从列表中删除节点
  58. * @param entry 待删除的节点
  59. */
  60. entry->prev->next = entry->next;
  61. entry->next = entry->prev;
  62. }
  63. static inline bool list_empty(struct List* entry)
  64. {
  65. /**
  66. * @brief 判断循环链表是否为空
  67. * @param entry 入口
  68. */
  69. if(entry->prev == entry->next)
  70. return true;
  71. else return false;
  72. }
  73. //计算字符串的长度(经过测试,该版本比采用repne/scasb汇编的运行速度快16.8%左右)
  74. static inline int strlen(char* s)
  75. {
  76. register int __res = 0;
  77. while (s[__res] != '\0')
  78. {
  79. ++__res;
  80. }
  81. return __res;
  82. }