/home/travis/build/MoarVM/MoarVM/src/6model/reprs/MVMString.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "moar.h" |
2 | | |
3 | | /* This representation's function pointer table. */ |
4 | | static const MVMREPROps MVMString_this_repr; |
5 | | |
6 | | /* Creates a new type object of this representation, and associates it with |
7 | | * the given HOW. */ |
8 | 144 | static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) { |
9 | 144 | MVMSTable *st = MVM_gc_allocate_stable(tc, &MVMString_this_repr, HOW); |
10 | 144 | |
11 | 144 | MVMROOT(tc, st, { |
12 | 144 | MVMObject *obj = MVM_gc_allocate_type_object(tc, st); |
13 | 144 | MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj); |
14 | 144 | st->size = sizeof(MVMString); |
15 | 144 | }); |
16 | 144 | |
17 | 144 | return st->WHAT; |
18 | 144 | } |
19 | | |
20 | | /* Copies the body of one object to another. */ |
21 | 0 | static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) { |
22 | 0 | MVMStringBody *src_body = (MVMStringBody *)src; |
23 | 0 | MVMStringBody *dest_body = (MVMStringBody *)dest; |
24 | 0 | dest_body->storage_type = src_body->storage_type; |
25 | 0 | dest_body->num_strands = src_body->num_strands; |
26 | 0 | dest_body->num_graphs = src_body->num_graphs; |
27 | 0 | dest_body->cached_hash_code = src_body->cached_hash_code; |
28 | 0 | switch (dest_body->storage_type) { |
29 | 0 | case MVM_STRING_GRAPHEME_32: |
30 | 0 | if (dest_body->num_graphs) { |
31 | 0 | dest_body->storage.blob_32 = MVM_malloc(dest_body->num_graphs * sizeof(MVMGrapheme32)); |
32 | 0 | memcpy(dest_body->storage.blob_32, src_body->storage.blob_32, |
33 | 0 | dest_body->num_graphs * sizeof(MVMGrapheme32)); |
34 | 0 | } |
35 | 0 | break; |
36 | 0 | case MVM_STRING_GRAPHEME_ASCII: |
37 | 0 | case MVM_STRING_GRAPHEME_8: |
38 | 0 | if (dest_body->num_graphs) { |
39 | 0 | dest_body->storage.blob_8 = MVM_malloc(dest_body->num_graphs); |
40 | 0 | memcpy(dest_body->storage.blob_8, src_body->storage.blob_8, |
41 | 0 | dest_body->num_graphs); |
42 | 0 | } |
43 | 0 | break; |
44 | 0 | case MVM_STRING_STRAND: |
45 | 0 | dest_body->storage.strands = MVM_malloc(dest_body->num_strands * sizeof(MVMStringStrand)); |
46 | 0 | memcpy(dest_body->storage.strands, src_body->storage.strands, |
47 | 0 | dest_body->num_strands * sizeof(MVMStringStrand)); |
48 | 0 | break; |
49 | 0 | default: |
50 | 0 | MVM_exception_throw_adhoc(tc, "Internal string corruption"); |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | /* Adds held objects to the GC worklist. */ |
55 | 235k | static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) { |
56 | 235k | MVMStringBody *body = (MVMStringBody *)data; |
57 | 235k | if (body->storage_type == MVM_STRING_STRAND) { |
58 | 182k | MVMStringStrand *strands = body->storage.strands; |
59 | 182k | MVMuint16 i; |
60 | 488k | for (i = 0; i < body->num_strands; i++) |
61 | 306k | MVM_gc_worklist_add(tc, worklist, &(strands[i].blob_string)); |
62 | 182k | } |
63 | 235k | } |
64 | | |
65 | | /* Called by the VM in order to free memory associated with this object. */ |
66 | 1.57M | static void gc_free(MVMThreadContext *tc, MVMObject *obj) { |
67 | 1.57M | MVMString *str = (MVMString *)obj; |
68 | 1.57M | MVM_free(str->body.storage.any); |
69 | 1.57M | str->body.num_graphs = str->body.num_strands = 0; |
70 | 1.57M | } |
71 | | |
72 | | static const MVMStorageSpec storage_spec = { |
73 | | MVM_STORAGE_SPEC_REFERENCE, /* inlineable */ |
74 | | 0, /* bits */ |
75 | | 0, /* align */ |
76 | | MVM_STORAGE_SPEC_BP_NONE, /* boxed_primitive */ |
77 | | 0, /* can_box */ |
78 | | 0, /* is_unsigned */ |
79 | | }; |
80 | | |
81 | | /* Gets the storage specification for this representation. */ |
82 | 0 | static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) { |
83 | 0 | return &storage_spec; |
84 | 0 | } |
85 | | |
86 | | /* Compose the representation. */ |
87 | 0 | static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) { |
88 | 0 | /* Nothing to do for this REPR. */ |
89 | 0 | } |
90 | | |
91 | | /* Calculates the non-GC-managed memory we hold on to. */ |
92 | 88.4k | static MVMuint64 unmanaged_size(MVMThreadContext *tc, MVMSTable *st, void *data) { |
93 | 88.4k | MVMStringBody *body = (MVMStringBody *)data; |
94 | 88.4k | switch (body->storage_type) { |
95 | 18.8k | case MVM_STRING_GRAPHEME_32: |
96 | 18.8k | return sizeof(MVMGrapheme32) * body->num_graphs; |
97 | 68.9k | case MVM_STRING_STRAND: |
98 | 68.9k | return sizeof(MVMStringStrand) * body->num_strands; |
99 | 627 | default: |
100 | 627 | return body->num_graphs; |
101 | 88.4k | } |
102 | 88.4k | } |
103 | | |
104 | | /* Initializes the representation. */ |
105 | 288 | const MVMREPROps * MVMString_initialize(MVMThreadContext *tc) { |
106 | 288 | return &MVMString_this_repr; |
107 | 288 | } |
108 | | |
109 | | static const MVMREPROps MVMString_this_repr = { |
110 | | type_object_for, |
111 | | MVM_gc_allocate_object, |
112 | | NULL, /* initialize */ |
113 | | copy_to, |
114 | | MVM_REPR_DEFAULT_ATTR_FUNCS, |
115 | | MVM_REPR_DEFAULT_BOX_FUNCS, |
116 | | MVM_REPR_DEFAULT_POS_FUNCS, |
117 | | MVM_REPR_DEFAULT_ASS_FUNCS, |
118 | | MVM_REPR_DEFAULT_ELEMS, |
119 | | get_storage_spec, |
120 | | NULL, /* change_type */ |
121 | | NULL, /* serialize */ |
122 | | NULL, /* deserialize */ |
123 | | NULL, /* serialize_repr_data */ |
124 | | NULL, /* deserialize_repr_data */ |
125 | | NULL, /* deserialize_stable_size */ |
126 | | gc_mark, |
127 | | gc_free, |
128 | | NULL, /* gc_cleanup */ |
129 | | NULL, /* gc_mark_repr_data */ |
130 | | NULL, /* gc_free_repr_data */ |
131 | | compose, |
132 | | NULL, /* spesh */ |
133 | | "MVMString", /* name */ |
134 | | MVM_REPR_ID_MVMString, |
135 | | unmanaged_size, |
136 | | NULL, /* describe_refs */ |
137 | | }; |