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