go-type-interface.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* go-type-interface.c -- hash and equality interface functions.
  2. Copyright 2009 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include "runtime.h"
  6. #include "interface.h"
  7. #include "go-type.h"
  8. /* A hash function for an interface. */
  9. uintptr_t
  10. __go_type_hash_interface (const void *vval,
  11. uintptr_t key_size __attribute__ ((unused)))
  12. {
  13. const struct __go_interface *val;
  14. const struct __go_type_descriptor *descriptor;
  15. uintptr_t size;
  16. val = (const struct __go_interface *) vval;
  17. if (val->__methods == NULL)
  18. return 0;
  19. descriptor = (const struct __go_type_descriptor *) val->__methods[0];
  20. size = descriptor->__size;
  21. if (__go_is_pointer_type (descriptor))
  22. return descriptor->__hashfn (&val->__object, size);
  23. else
  24. return descriptor->__hashfn (val->__object, size);
  25. }
  26. /* An equality function for an interface. */
  27. _Bool
  28. __go_type_equal_interface (const void *vv1, const void *vv2,
  29. uintptr_t key_size __attribute__ ((unused)))
  30. {
  31. const struct __go_interface *v1;
  32. const struct __go_interface *v2;
  33. const struct __go_type_descriptor* v1_descriptor;
  34. const struct __go_type_descriptor* v2_descriptor;
  35. v1 = (const struct __go_interface *) vv1;
  36. v2 = (const struct __go_interface *) vv2;
  37. if (v1->__methods == NULL || v2->__methods == NULL)
  38. return v1->__methods == v2->__methods;
  39. v1_descriptor = (const struct __go_type_descriptor *) v1->__methods[0];
  40. v2_descriptor = (const struct __go_type_descriptor *) v2->__methods[0];
  41. if (!__go_type_descriptors_equal (v1_descriptor, v2_descriptor))
  42. return 0;
  43. if (__go_is_pointer_type (v1_descriptor))
  44. return v1->__object == v2->__object;
  45. else
  46. return v1_descriptor->__equalfn (v1->__object, v2->__object,
  47. v1_descriptor->__size);
  48. }