glib.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // 内核全局通用库
  3. // Created by longjin on 2022/1/22.
  4. //
  5. #pragma once
  6. // 引入对bool类型的支持
  7. #include <stdbool.h>
  8. #include <DragonOS/stdint.h>
  9. #include <common/stddef.h>
  10. #include <arch/arch.h>
  11. #include <common/compiler.h>
  12. #include <asm/asm.h>
  13. /**
  14. * @brief 根据结构体变量内某个成员变量member的基地址,计算出该结构体变量的基地址
  15. * @param ptr 指向结构体变量内的成员变量member的指针
  16. * @param type 成员变量所在的结构体
  17. * @param member 成员变量名
  18. *
  19. * 方法:使用ptr减去结构体内的偏移,得到结构体变量的基地址
  20. */
  21. #define container_of(ptr, type, member) \
  22. ({ \
  23. typeof(((type *)0)->member) *p = (ptr); \
  24. (type *)((unsigned long)p - (unsigned long)&(((type *)0)->member)); \
  25. })
  26. #define ABS(x) ((x) > 0 ? (x) : -(x)) // 绝对值
  27. // 最大最小值
  28. #define max(x, y) ((x > y) ? (x) : (y))
  29. #define min(x, y) ((x < y) ? (x) : (y))
  30. // 遮罩高32bit
  31. #define MASK_HIGH_32bit(x) (x & (0x00000000ffffffffUL))
  32. // 四舍五入成整数
  33. ul round(double x)
  34. {
  35. return (ul)(x + 0.5);
  36. }
  37. /**
  38. * @brief 地址按照align进行对齐
  39. *
  40. * @param addr
  41. * @param _align
  42. * @return ul 对齐后的地址
  43. */
  44. static __always_inline ul ALIGN(const ul addr, const ul _align)
  45. {
  46. return (ul)((addr + _align - 1) & (~(_align - 1)));
  47. }
  48. /**
  49. * @brief 将数据从src搬运到dst,并能正确处理地址重叠的问题
  50. *
  51. * @param dst 目标地址指针
  52. * @param src 源地址指针
  53. * @param size 大小
  54. * @return void* 指向目标地址的指针
  55. */
  56. void *memmove(void *dst, const void *src, uint64_t size);