1
0

Identity.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <stdint.h>
  20. #if defined(Identity_CHECK)
  21. <?js file.Identity_hash = "0x" + Constant_randHexString(16) + "ull"; ?>
  22. #define Identity_MAGIC ((uintptr_t) <?js return file.Identity_hash ?>)
  23. /** This goes in each structure which will be checked. */
  24. #define Identity uintptr_t Identity_verifier;
  25. #define Identity_set(pointer) \
  26. (pointer)->Identity_verifier = Identity_MAGIC
  27. #define Identity_check(pointer) \
  28. (__extension__ ({ \
  29. __typeof__(pointer) Identity_ptr = (pointer); \
  30. Assert_true(Identity_ptr->Identity_verifier == Identity_MAGIC); \
  31. Identity_ptr; \
  32. }))
  33. #define Identity_ncheck(pointer) \
  34. (__extension__ ({ \
  35. __typeof__(pointer) Identity_ptr = (pointer); \
  36. Assert_true(!Identity_ptr || Identity_ptr->Identity_verifier == Identity_MAGIC); \
  37. Identity_ptr; \
  38. }))
  39. #else
  40. #define Identity
  41. #define Identity_set(pointer)
  42. #define Identity_check(pointer) \
  43. (__extension__ ({ \
  44. (pointer); \
  45. }))
  46. #define Identity_ncheck(pointer) Identity_check(pointer)
  47. #endif
  48. #define Identity_containerOf(ptr, type, member) \
  49. (__extension__ ({ \
  50. const __typeof__(((type*)0)->member)*__mptr = (ptr); \
  51. Identity_check( (type*)((char*)__mptr - offsetof(type, member)) ); \
  52. }))
  53. #endif