X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Ftask.c;h=6e7ae0c39f044a7b86e3525d642797e2ed8f9c78;hb=HEAD;hp=12789ce18a6cf27b8b00db8b6e49ae1cf9d92d93;hpb=dcd48012f6e95d649b8fe3d0cd050ac1bdeafaf6;p=mTask.git diff --git a/client/task.c b/client/task.c index 12789ce..6e7ae0c 100644 --- a/client/task.c +++ b/client/task.c @@ -4,32 +4,21 @@ #include "task.h" #include "spec.h" #include "interface.h" +#include "mem.h" -#define TASKSPACE 128 +extern uint8_t *mem_top, *mem_bottom, *mem_task, *mem_sds; -uint8_t taskspace[TASKSPACE] = {0}; -uint8_t *spacepointer = (uint8_t *)&taskspace; uint8_t taskid = 0; -struct task *head = NULL; - -void task_init(void) -{ - memset(&taskspace, 0, TASKSPACE); - head = NULL; -} void task_register(void) { + debug("free memory: %lu\n", mem_free()); int i; - if(spacepointer+sizeof(struct task) > taskspace+TASKSPACE){ + if(mem_task+sizeof(struct task) > mem_sds){ die("Out of memory... Not enough to allocate taskstruct"); } - //We haven't had a task before, therefore we initialize head - struct task *t = (struct task *)spacepointer; - if(head == NULL) { - head = t; - } + struct task *t = (struct task *)mem_task; //Read interval t->interval = read16(); @@ -44,24 +33,30 @@ void task_register(void) t->tasklength = read16(); debug("task interval: %d, length: %d\n", t->interval, t->tasklength); - if(spacepointer+t->tasklength > taskspace+TASKSPACE){ + if(mem_task+t->tasklength > mem_sds){ die("Out of memory... Not enough to allocate bytes"); } - spacepointer += t->tasklength; + mem_task += sizeof(struct task); + t->bc = mem_task; + mem_task += t->tasklength; //Read task bytecode for(i = 0; itasklength; i++){ t->bc[i] = read_byte(); } + //Return the task number for later removal debug("Received a task of length %d", t->tasklength); t->lastrun = 0L; t->taskid = taskid++; + t->value = 0; write_byte('t'); write16(t->taskid); + write16(mem_free()); write_byte('\n'); + debug("free memory: %lu\n", mem_free()); } bool is_interrupt_task(struct task *t) @@ -78,52 +73,41 @@ bool had_interrupt(struct task* t) struct task *task_head(void) { - return head; + return mem_task == mem_bottom ? NULL : (struct task *)mem_bottom; } struct task *task_next(struct task *t) { - if(t == NULL){ - return NULL; - } - - uint8_t *next = (uint8_t *)t; - next += sizeof(struct task); - next += t->tasklength; - - return next > spacepointer ? NULL : (struct task *)next; + uint8_t *next = (uint8_t *)t + sizeof(struct task) + t->tasklength; + return next >= mem_task ? NULL : (struct task *)next; } -void task_delete(void) +void task_delete(uint8_t c) { - uint8_t c = read16(); - struct task *t = NULL; + debug("Going to delete task: %i", c); + struct task *t = task_head(); while(t != NULL){ if(t->taskid == c){ break; } t = task_next(t); } + //Write deletion spec + write_byte('d'); + write16(c); + write_byte('\n'); + if(t != NULL){ //We found the task, now we move everything from the end of the task up //to the spacepointer right uint8_t *start = (uint8_t *)t; uint8_t *end = start + sizeof(struct task) + t->tasklength; - debug("Moving %lu bytes\n", spacepointer-end); - for(int i = 0; i