Use [] instead of [0] in structures.
[ilari-esolangs.git] / vat.c
blob56d3afe0debb129b485ff2ff70d33ae7d19499cb
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));
49 /*****************************************************************************/
50 unsigned vat_perform_gc(struct vat* vat)
52 objectlist_set_gcmark_all(vat->v_objectlist);
53 message_queue_forall(vat->v_msg_queue, mark_message_live);
54 return objectlist_kill_marked(vat->v_objectlist);
57 /*****************************************************************************/
58 size_t vat_maxlocals(struct vat* vat)
60 return vat->v_max_locals;
63 /*****************************************************************************/
64 struct vat* vat_create(size_t maxlocals)
66 struct vat* vat;
67 vat = malloc(sizeof(struct vat));
68 if(!vat) {
69 fprintf(stderr, "Out of memory!\n");
70 exit(1);
72 vat->v_objectlist = objectlist_create();
73 vat->v_msg_queue = message_queue_create();
74 vat->v_max_locals = maxlocals;
75 return vat;
78 /*****************************************************************************/
79 void vat_destroy(struct vat* vat)
81 message_queue_destroy(vat->v_msg_queue);
82 objectlist_destroy(vat->v_objectlist);
83 free(vat);
86 /*****************************************************************************/
87 void vat_add_object(struct vat* vat, struct object* object)
89 objectlist_add(vat->v_objectlist, object);
92 /*****************************************************************************/
93 struct message* vat_deque_message(struct vat* vat)
95 return message_queue_deque(vat->v_msg_queue);