icfp12/ovm/task.c
2009-06-27 02:21:49 +02:00

79 lines
1.9 KiB
C

#include "task.h"
#include <glib/gstdio.h>
void task_free(task_t *task) {
if (!task) return;
g_slice_free1(task->in_size * sizeof(gdouble), task->in_old);
g_slice_free1(task->in_size * sizeof(gdouble), task->in);
g_slice_free1(task->out_size * sizeof(gdouble), task->out);
g_slice_free(task_t, task);
}
task_t* task_new(guint scenario) {
task_t *task = g_slice_new(task_t);
task->scenario = scenario;
task->timestamp = 0;
task->in_size = ovm_ins;
task->out_size = ovm_outs;
task->in = g_slice_alloc0(task->in_size * sizeof(gdouble));
task->in_old = g_slice_alloc0(task->in_size * sizeof(gdouble));
task->out = g_slice_alloc0(task->out_size * sizeof(gdouble));
return task;
}
void task_trace_start(task_t *task, const gchar *tracefile) {
guint32 buf[3];
task->tracefd = g_open(tracefile, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (-1 == task->tracefd) {
fprintf(stderr, "Couldn't open file %s: %s\n", tracefile, g_strerror(errno));
exit(2);
}
buf[0] = 0xCAFEBABE;
buf[1] = 439;
buf[2] = (guint32) task->scenario;
write(task->tracefd, buf, 12);
}
void task_trace_end(task_t *task) {
guint32 buf[2];
buf[0] = task->timestamp;
buf[1] = 0;
write(task->tracefd, buf, 8);
}
void task_trace_step(task_t *task) {
gboolean ch2 = task->in_old[2] != task->in[2], ch3 = task->in_old[3] != task->in[3];
if ((0 == task->timestamp) || ch2 || ch3) {
guint32 buf[2+3*3], *cur = buf;
buf[0] = task->timestamp;
buf[1] = 0;
cur = &buf[2];
if (ch2) {
buf[1]++;
*cur++ = 2;
memcpy(cur, &task->in[2], sizeof(gdouble));
task->in_old[2] = task->in[2];
cur += 2;
}
if (ch3) {
buf[1]++;
*cur++ = 3;
memcpy(cur, &task->in[3], sizeof(gdouble));
task->in_old[3] = task->in[3];
cur += 2;
}
if (0 == task->timestamp) {
gdouble s = task->scenario;
buf[1]++;
*cur++ = 0x3e80;
memcpy(cur, &s, sizeof(gdouble));
cur += 2;
}
write(task->tracefd, buf, (cur-buf)*4);
}
}