Identity.h 2.4 KB

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