Move vat handling to module vat and close it.
[ilari-esolangs.git] / vat.c
blobe2ee4113d60dbcf6816fa5908e8ddda94833072f
1 #include "vat.h"
2 #include "lock.h"
3 #include "object.h"
4 #include "message.h"
5 #include <stdio.h>
7 /* Vat. */
8 struct vat
10 /* Object list. */
11 struct objectlist* v_objectlist;
12 /* Object list head. */
13 struct object* v_object_list;
14 /* Message queue head. */
15 struct message_queue* v_msg_queue;
16 /* Greatest locals count for any class. */
17 size_t v_max_locals;
18 /* Message queue lock. */
19 struct lock v_msgq_lock;
22 /*****************************************************************************/
23 void vat_grab_queue_lock(struct vat* vat)
25 mutex_lock(&vat->v_msgq_lock);
28 /*****************************************************************************/
29 void vat_ungrab_queue_lock(struct vat* vat)
31 mutex_unlock(&vat->v_msgq_lock);
34 /*****************************************************************************/
35 void vat_queue_message(struct vat* vat, struct message* message)
37 message_queue_queue(vat->v_msg_queue, message);
40 /*****************************************************************************/
41 static void mark_message_live(struct message* msg)
43 object_add_anchor(message_target(msg));
44 for(size_t i = 0; i < message_parameters(msg); i++)
45 object_add_anchor(message_read_slot(msg, i));
48 /*****************************************************************************/
49 unsigned vat_perform_gc(struct vat* vat)
51 objectlist_set_gcmark_all(vat->v_objectlist);
52 message_queue_forall(vat->v_msg_queue, mark_message_live);
53 return objectlist_kill_marked(vat->v_objectlist);
56 /*****************************************************************************/
57 size_t vat_maxlocals(struct vat* vat)
59 return vat->v_max_locals;
62 /*****************************************************************************/
63 struct vat* vat_create(size_t maxlocals)
65 struct vat* vat;
66 vat = malloc(sizeof(struct vat));
67 if(!vat) {
68 fprintf(stderr, "Out of memory!\n");
69 exit(1);
71 vat->v_objectlist = objectlist_create();
72 vat->v_msg_queue = message_queue_create();
73 vat->v_max_locals = maxlocals;
74 return vat;
77 /*****************************************************************************/
78 void vat_destroy(struct vat* vat)
80 message_queue_destroy(vat->v_msg_queue);
81 objectlist_destroy(vat->v_objectlist);
82 free(vat);
85 /*****************************************************************************/
86 void vat_add_object(struct vat* vat, struct object* object)
88 objectlist_add(vat->v_objectlist, object);
91 /*****************************************************************************/
92 struct message* vat_deque_message(struct vat* vat)
94 return message_queue_deque(vat->v_msg_queue);