Identity.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #if defined(Identity_CHECK)
  20. <?js file.Identity_hash = "0x" + Constant_randHexString(16) + "ull"; ?>
  21. #define Identity_MAGIC ((unsigned long) <?js return file.Identity_hash ?>)
  22. /** This goes in each structure which will be checked. */
  23. #define Identity \
  24. unsigned long 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