《操作系统》的实验代码。

68 line
2.0 KiB

12 年之前
  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. /* Represents true-or-false values */
  10. typedef int bool;
  11. /* Explicitly-sized versions of integer types */
  12. typedef char int8_t;
  13. typedef unsigned char uint8_t;
  14. typedef short int16_t;
  15. typedef unsigned short uint16_t;
  16. typedef int int32_t;
  17. typedef unsigned int uint32_t;
  18. typedef long long int64_t;
  19. typedef unsigned long long uint64_t;
  20. /* *
  21. * Pointers and addresses are 32 bits long.
  22. * We use pointer types to represent addresses,
  23. * uintptr_t to represent the numerical values of addresses.
  24. * */
  25. typedef int32_t intptr_t;
  26. typedef uint32_t uintptr_t;
  27. /* size_t is used for memory object sizes */
  28. typedef uintptr_t size_t;
  29. /* used for page numbers */
  30. typedef size_t ppn_t;
  31. /* *
  32. * Rounding operations (efficient when n is a power of 2)
  33. * Round down to the nearest multiple of n
  34. * */
  35. #define ROUNDDOWN(a, n) ({ \
  36. size_t __a = (size_t)(a); \
  37. (typeof(a))(__a - __a % (n)); \
  38. })
  39. /* Round up to the nearest multiple of n */
  40. #define ROUNDUP(a, n) ({ \
  41. size_t __n = (size_t)(n); \
  42. (typeof(a))(ROUNDDOWN((size_t)(a) + __n - 1, __n)); \
  43. })
  44. /* Return the offset of 'member' relative to the beginning of a struct type */
  45. #define offsetof(type, member) \
  46. ((size_t)(&((type *)0)->member))
  47. /* *
  48. * to_struct - get the struct from a ptr
  49. * @ptr: a struct pointer of member
  50. * @type: the type of the struct this is embedded in
  51. * @member: the name of the member within the struct
  52. * */
  53. #define to_struct(ptr, type, member) \
  54. ((type *)((char *)(ptr) - offsetof(type, member)))
  55. #endif /* !__LIBS_DEFS_H__ */