/home/travis/build/MoarVM/MoarVM/src/core/intcache.c
Line | Count | Source |
1 | | #include "moar.h" |
2 | | |
3 | 275 | void MVM_intcache_for(MVMThreadContext *tc, MVMObject *type) { |
4 | 275 | int type_index; |
5 | 275 | int right_slot = -1; |
6 | 275 | uv_mutex_lock(&tc->instance->mutex_int_const_cache); |
7 | 277 | for (type_index = 0; type_index < 4; type_index++) { |
8 | 277 | if (tc->instance->int_const_cache->types[type_index] == NULL) { |
9 | 132 | right_slot = type_index; |
10 | 132 | break; |
11 | 132 | } |
12 | 145 | else if (tc->instance->int_const_cache->types[type_index] == type) { |
13 | 143 | uv_mutex_unlock(&tc->instance->mutex_int_const_cache); |
14 | 143 | return; |
15 | 143 | } |
16 | 277 | } |
17 | 132 | if (right_slot != -1) { |
18 | 132 | int val; |
19 | 2.24k | for (val = -1; val < 15; val++) { |
20 | 2.11k | MVMObject *obj; |
21 | 2.11k | obj = MVM_repr_alloc_init(tc, type); |
22 | 2.11k | MVM_repr_set_int(tc, obj, val); |
23 | 2.11k | tc->instance->int_const_cache->cache[type_index][val + 1] = obj; |
24 | 2.11k | MVM_gc_root_add_permanent_desc(tc, |
25 | 2.11k | (MVMCollectable **)&tc->instance->int_const_cache->cache[type_index][val + 1], |
26 | 2.11k | "Boxed integer cache entry"); |
27 | 2.11k | } |
28 | 132 | tc->instance->int_const_cache->types[type_index] = type; |
29 | 132 | MVM_gc_root_add_permanent_desc(tc, |
30 | 132 | (MVMCollectable **)&tc->instance->int_const_cache->types[type_index], |
31 | 132 | "Boxed integer cache type"); |
32 | 132 | } |
33 | 132 | uv_mutex_unlock(&tc->instance->mutex_int_const_cache); |
34 | 132 | } |
35 | | |
36 | 4.76M | MVMObject *MVM_intcache_get(MVMThreadContext *tc, MVMObject *type, MVMint64 value) { |
37 | 4.76M | int type_index; |
38 | 4.76M | int right_slot = -1; |
39 | 4.76M | |
40 | 4.76M | if (value < -1 || value >= 15) |
41 | 1.15M | return NULL; |
42 | 4.76M | |
43 | 3.61M | for (type_index = 0; type_index < 4; type_index++) { |
44 | 3.61M | if (tc->instance->int_const_cache->types[type_index] == type) { |
45 | 3.61M | right_slot = type_index; |
46 | 3.61M | break; |
47 | 3.61M | } |
48 | 3.61M | } |
49 | 3.61M | if (right_slot != -1) { |
50 | 3.61M | return tc->instance->int_const_cache->cache[right_slot][value + 1]; |
51 | 3.61M | } |
52 | 287 | return NULL; |
53 | 3.61M | } |