《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 rivejä
2.3 KiB

12 vuotta sitten
  1. #ifndef __LIBS_DEFS_H__
  2. #define __LIBS_DEFS_H__
  3. #ifndef NULL
  4. #define NULL ((void *)0)
  5. #endif
  6. #define __always_inline inline __attribute__((always_inline))
  7. #define __noinline __attribute__((noinline))
  8. #define __noreturn __attribute__((noreturn))
  9. #define CHAR_BIT 8
  10. /* Represents true-or-false values */
  11. typedef int bool;
  12. /* Explicitly-sized versions of integer types */
  13. typedef char int8_t;
  14. typedef unsigned char uint8_t;
  15. typedef short int16_t;
  16. typedef unsigned short uint16_t;
  17. typedef int int32_t;
  18. typedef unsigned int uint32_t;
  19. typedef long long int64_t;
  20. typedef unsigned long long uint64_t;
  21. /* *
  22. * Pointers and addresses are 32 bits long.
  23. * We use pointer types to represent addresses,
  24. * uintptr_t to represent the numerical values of addresses.
  25. * */
  26. typedef int32_t intptr_t;
  27. typedef uint32_t uintptr_t;
  28. /* size_t is used for memory object sizes */
  29. typedef uintptr_t size_t;
  30. /* off_t is used for file offsets and lengths */
  31. typedef intptr_t off_t;
  32. /* used for page numbers */
  33. typedef size_t ppn_t;
  34. /* *
  35. * Rounding operations (efficient when n is a power of 2)
  36. * Round down to the nearest multiple of n
  37. * */
  38. #define ROUNDDOWN(a, n) ({ \
  39. size_t __a = (size_t)(a); \
  40. (typeof(a))(__a - __a % (n)); \
  41. })
  42. /* Round up to the nearest multiple of n */
  43. #define ROUNDUP(a, n) ({ \
  44. size_t __n = (size_t)(n); \
  45. (typeof(a))(ROUNDDOWN((size_t)(a) + __n - 1, __n)); \
  46. })
  47. /* Round up the result of dividing of n */
  48. #define ROUNDUP_DIV(a, n) ({ \
  49. uint32_t __n = (uint32_t)(n); \
  50. (typeof(a))(((a) + __n - 1) / __n); \
  51. })
  52. /* Return the offset of 'member' relative to the beginning of a struct type */
  53. #define offsetof(type, member) \
  54. ((size_t)(&((type *)0)->member))
  55. /* *
  56. * to_struct - get the struct from a ptr
  57. * @ptr: a struct pointer of member
  58. * @type: the type of the struct this is embedded in
  59. * @member: the name of the member within the struct
  60. * */
  61. #define to_struct(ptr, type, member) \
  62. ((type *)((char *)(ptr) - offsetof(type, member)))
  63. #endif /* !__LIBS_DEFS_H__ */