relptr.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*-------------------------------------------------------------------------
  2. *
  3. * relptr.h
  4. * This file contains basic declarations for relative pointers.
  5. *
  6. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/utils/relptr.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef RELPTR_H
  14. #define RELPTR_H
  15. /*
  16. * Relative pointers are intended to be used when storing an address that may
  17. * be relative either to the base of the process's address space or some
  18. * dynamic shared memory segment mapped therein.
  19. *
  20. * The idea here is that you declare a relative pointer as relptr(type)
  21. * and then use relptr_access to dereference it and relptr_store to change
  22. * it. The use of a union here is a hack, because what's stored in the
  23. * relptr is always a Size, never an actual pointer. But including a pointer
  24. * in the union allows us to use stupid macro tricks to provide some measure
  25. * of type-safety.
  26. */
  27. #define relptr(type) union { type *relptr_type; Size relptr_off; }
  28. /*
  29. * pgindent gets confused by declarations that use "relptr(type)" directly,
  30. * so preferred style is to write
  31. * typedef struct ... SomeStruct;
  32. * relptr_declare(SomeStruct, RelptrSomeStruct);
  33. * and then declare pointer variables as "RelptrSomeStruct someptr".
  34. */
  35. #define relptr_declare(type, relptrtype) \
  36. typedef relptr(type) relptrtype
  37. #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
  38. #define relptr_access(base, rp) \
  39. (AssertVariableIsOfTypeMacro(base, char *), \
  40. (__typeof__((rp).relptr_type)) ((rp).relptr_off == 0 ? NULL : \
  41. (base) + (rp).relptr_off - 1))
  42. #else
  43. /*
  44. * If we don't have __builtin_types_compatible_p, assume we might not have
  45. * __typeof__ either.
  46. */
  47. #define relptr_access(base, rp) \
  48. (AssertVariableIsOfTypeMacro(base, char *), \
  49. (void *) ((rp).relptr_off == 0 ? NULL : (base) + (rp).relptr_off - 1))
  50. #endif
  51. #define relptr_is_null(rp) \
  52. ((rp).relptr_off == 0)
  53. #define relptr_offset(rp) \
  54. ((rp).relptr_off - 1)
  55. /* We use this inline to avoid double eval of "val" in relptr_store */
  56. static inline Size
  57. relptr_store_eval(char *base, char *val)
  58. {
  59. if (val == NULL)
  60. return 0;
  61. else
  62. {
  63. Assert(val >= base);
  64. return val - base + 1;
  65. }
  66. }
  67. #ifdef HAVE__BUILTIN_TYPES_COMPATIBLE_P
  68. #define relptr_store(base, rp, val) \
  69. (AssertVariableIsOfTypeMacro(base, char *), \
  70. AssertVariableIsOfTypeMacro(val, __typeof__((rp).relptr_type)), \
  71. (rp).relptr_off = relptr_store_eval((base), (char *) (val)))
  72. #else
  73. /*
  74. * If we don't have __builtin_types_compatible_p, assume we might not have
  75. * __typeof__ either.
  76. */
  77. #define relptr_store(base, rp, val) \
  78. (AssertVariableIsOfTypeMacro(base, char *), \
  79. (rp).relptr_off = relptr_store_eval((base), (char *) (val)))
  80. #endif
  81. #define relptr_copy(rp1, rp2) \
  82. ((rp1).relptr_off = (rp2).relptr_off)
  83. #endif /* RELPTR_H */