Identity.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Identity_H
  16. #define Identity_H
  17. #include "util/Assert.h"
  18. #include "util/Constant.h"
  19. #include "util/Js.h"
  20. #include <stdint.h>
  21. #if defined(Identity_CHECK)
  22. Js({ this.Identity_hash = "0x" + Constant_randHexString(16) + "ull"; })
  23. #define Identity_MAGIC ((uintptr_t) Js_or({ return this.Identity_hash }, 1))
  24. /** This goes in each structure which will be checked. */
  25. #define Identity uintptr_t Identity_verifier;
  26. #define Identity_set(pointer) \
  27. (pointer)->Identity_verifier = Identity_MAGIC
  28. #define Identity_check(pointer) \
  29. (__extension__ ({ \
  30. __typeof__(pointer) Identity_ptr = (pointer); \
  31. Assert_true(Identity_ptr->Identity_verifier == Identity_MAGIC); \
  32. Identity_ptr; \
  33. }))
  34. #define Identity_ncheck(pointer) \
  35. (__extension__ ({ \
  36. __typeof__(pointer) Identity_ptr = (pointer); \
  37. Assert_true(!Identity_ptr || Identity_ptr->Identity_verifier == Identity_MAGIC); \
  38. Identity_ptr; \
  39. }))
  40. #else
  41. #define Identity
  42. #define Identity_set(pointer)
  43. #define Identity_check(pointer) \
  44. (__extension__ ({ \
  45. (pointer); \
  46. }))
  47. #define Identity_ncheck(pointer) Identity_check(pointer)
  48. #endif
  49. #define Identity_containerOf(ptr, type, member) \
  50. (__extension__ ({ \
  51. const __typeof__(((type*)0)->member)*__mptr = (ptr); \
  52. Identity_check( (type*)((char*)__mptr - offsetof(type, member)) ); \
  53. }))
  54. #endif