/home/travis/build/MoarVM/MoarVM/src/6model/reprs/MVMHash.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* Representation used by VM-level hashes. */ |
2 | | |
3 | | struct MVMHashEntry { |
4 | | /* value object */ |
5 | | MVMObject *value; |
6 | | |
7 | | /* the uthash hash handle inline struct, including the key. */ |
8 | | UT_hash_handle hash_handle; |
9 | | }; |
10 | | |
11 | | struct MVMHashBody { |
12 | | /* uthash updates this pointer directly. */ |
13 | | MVMHashEntry *hash_head; |
14 | | }; |
15 | | struct MVMHash { |
16 | | MVMObject common; |
17 | | MVMHashBody body; |
18 | | }; |
19 | | |
20 | | /* Function for REPR setup. */ |
21 | | const MVMREPROps * MVMHash_initialize(MVMThreadContext *tc); |
22 | | |
23 | | #define MVM_HASH_BIND(tc, hash, key, value) \ |
24 | 1.88M | do { \ |
25 | 1.88M | if (!MVM_is_null(tc, (MVMObject *)key) && REPR(key)->ID == MVM_REPR_ID_MVMString \ |
26 | 1.88M | && IS_CONCRETE(key)) { \ |
27 | 1.88M | HASH_ADD_KEYPTR_VM_STR(tc, hash_handle, hash, key, value); \ |
28 | 1.88M | } \ |
29 | 0 | else { \ |
30 | 0 | MVM_exception_throw_adhoc(tc, "Hash keys must be concrete strings"); \ |
31 | 0 | } \ |
32 | 1.88M | } while (0); |
33 | | |
34 | | #define MVM_HASH_GET(tc, hash, key, value) \ |
35 | 18.9M | do { \ |
36 | 18.9M | if (!MVM_is_null(tc, (MVMObject *)key) && REPR(key)->ID == MVM_REPR_ID_MVMString \ |
37 | 18.9M | && IS_CONCRETE(key)) { \ |
38 | 18.9M | HASH_FIND_VM_STR(tc, hash_handle, hash, key, value); \ |
39 | 18.9M | } \ |
40 | 0 | else { \ |
41 | 0 | MVM_exception_throw_adhoc(tc, "Hash keys must be concrete strings"); \ |
42 | 0 | } \ |
43 | 18.9M | } while (0); |
44 | | |
45 | 809k | #define MVM_HASH_KEY(entry) ((MVMString *)(entry)->hash_handle.key) |
46 | | |
47 | 1.18k | #define MVM_HASH_DESTROY(hash_handle, hashentry_type, head_node) do { \ |
48 | 1.18k | hashentry_type *current, *tmp; \ |
49 | 1.18k | unsigned bucket_tmp; \ |
50 | 3.39k | HASH_ITER(hash_handle, head_node, current, tmp, bucket_tmp) { \ |
51 | 3.39k | if (current != head_node) \ |
52 | 2.29k | MVM_free(current); \ |
53 | 3.39k | } \ |
54 | 1.18k | tmp = head_node; \ |
55 | 1.18k | HASH_CLEAR(hash_handle, head_node); \ |
56 | 1.18k | MVM_free(tmp); \ |
57 | 1.18k | } while (0) |