/home/travis/build/MoarVM/MoarVM/src/6model/reprs/MVMOSHandle.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 MVMOSHandle_this_repr; |
5 | | |
6 | | /* Creates a new type object of this representation, and associates it with |
7 | | * the given HOW. */ |
8 | 130 | static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) { |
9 | 130 | MVMSTable *st = MVM_gc_allocate_stable(tc, &MVMOSHandle_this_repr, HOW); |
10 | 130 | |
11 | 130 | MVMROOT(tc, st, { |
12 | 130 | MVMObject *obj = MVM_gc_allocate_type_object(tc, st); |
13 | 130 | MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj); |
14 | 130 | st->size = sizeof(MVMOSHandle); |
15 | 130 | }); |
16 | 130 | |
17 | 130 | return st->WHAT; |
18 | 130 | } |
19 | | |
20 | | /* Initializes the handle with the mutex all handles need. */ |
21 | 592 | static void initialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) { |
22 | 592 | MVMOSHandleBody *handle = (MVMOSHandleBody *)data; |
23 | 592 | handle->mutex = MVM_malloc(sizeof(uv_mutex_t)); |
24 | 592 | uv_mutex_init(handle->mutex); |
25 | 592 | } |
26 | | |
27 | | /* Copies the body of one object to another. */ |
28 | 0 | static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) { |
29 | 0 | /* can't be copied because then we could never know when gc_free should |
30 | 0 | * close the handle (unless we did some refcounting on a shared container). |
31 | 0 | * note - 12:25 <jnthn> I mean, Perl 6 will has an attribute which |
32 | 0 | * is the MoarVM handle, so a .clone() on a Perl 6 IO object |
33 | 0 | * won't trigger cloning of the underlying handle. */ |
34 | 0 | MVM_exception_throw_adhoc(tc, "Cannot copy object with repr OSHandle"); |
35 | 0 | } |
36 | | |
37 | | /* Called by the VM to mark any GCable items. */ |
38 | 6 | static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) { |
39 | 6 | MVMOSHandleBody *handle = (MVMOSHandleBody *)data; |
40 | 6 | if (handle->ops && handle->ops->gc_mark) |
41 | 0 | handle->ops->gc_mark(tc, handle->data, worklist); |
42 | 6 | } |
43 | | |
44 | | /* Called by the VM in order to free memory associated with this object. */ |
45 | 52 | static void gc_free(MVMThreadContext *tc, MVMObject *obj) { |
46 | 52 | MVMOSHandle *handle = (MVMOSHandle *)obj; |
47 | 52 | if (handle->body.ops && handle->body.ops->gc_free) |
48 | 52 | handle->body.ops->gc_free(tc, obj, handle->body.data); |
49 | 52 | if (handle->body.mutex) { |
50 | 52 | uv_mutex_destroy(handle->body.mutex); |
51 | 52 | MVM_free(handle->body.mutex); |
52 | 52 | } |
53 | 52 | } |
54 | | |
55 | | |
56 | | static const MVMStorageSpec storage_spec = { |
57 | | MVM_STORAGE_SPEC_REFERENCE, /* inlineable */ |
58 | | 0, /* bits */ |
59 | | 0, /* align */ |
60 | | MVM_STORAGE_SPEC_BP_NONE, /* boxed_primitive */ |
61 | | 0, /* can_box */ |
62 | | 0, /* is_unsigned */ |
63 | | }; |
64 | | |
65 | | |
66 | | /* Gets the storage specification for this representation. */ |
67 | 0 | static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) { |
68 | 0 | return &storage_spec; |
69 | 0 | } |
70 | | |
71 | | /* Compose the representation. */ |
72 | 0 | static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) { |
73 | 0 | /* Nothing to do for this REPR. */ |
74 | 0 | } |
75 | | |
76 | | /* Initializes the representation. */ |
77 | 130 | const MVMREPROps * MVMOSHandle_initialize(MVMThreadContext *tc) { |
78 | 130 | return &MVMOSHandle_this_repr; |
79 | 130 | } |
80 | | |
81 | | static const MVMREPROps MVMOSHandle_this_repr = { |
82 | | type_object_for, |
83 | | MVM_gc_allocate_object, |
84 | | initialize, |
85 | | copy_to, |
86 | | MVM_REPR_DEFAULT_ATTR_FUNCS, |
87 | | MVM_REPR_DEFAULT_BOX_FUNCS, |
88 | | MVM_REPR_DEFAULT_POS_FUNCS, |
89 | | MVM_REPR_DEFAULT_ASS_FUNCS, |
90 | | MVM_REPR_DEFAULT_ELEMS, |
91 | | get_storage_spec, |
92 | | NULL, /* change_type */ |
93 | | NULL, /* serialize */ |
94 | | NULL, /* deserialize */ |
95 | | NULL, /* serialize_repr_data */ |
96 | | NULL, /* deserialize_repr_data */ |
97 | | NULL, /* deserialize_stable_size */ |
98 | | gc_mark, |
99 | | gc_free, |
100 | | NULL, /* gc_cleanup */ |
101 | | NULL, /* gc_mark_repr_data */ |
102 | | NULL, /* gc_free_repr_data */ |
103 | | compose, |
104 | | NULL, /* spesh */ |
105 | | "MVMOSHandle", /* name */ |
106 | | MVM_REPR_ID_MVMOSHandle, |
107 | | NULL, /* unmanaged_size */ |
108 | | NULL, /* describe_refs */ |
109 | | }; |