X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Ftask.c;h=c5fc41da28dae7d1b2aab345f4dd5e66d2e8e73b;hb=09b207a39b7791098daafd7d87c3ad9d3db3e19f;hp=12789ce18a6cf27b8b00db8b6e49ae1cf9d92d93;hpb=dcd48012f6e95d649b8fe3d0cd050ac1bdeafaf6;p=mTask.git diff --git a/client/task.c b/client/task.c index 12789ce..c5fc41d 100644 --- a/client/task.c +++ b/client/task.c @@ -10,16 +10,15 @@ 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", ((uint8_t *)taskspace)-spacepointer+TASKSPACE); int i; if(spacepointer+sizeof(struct task) > taskspace+TASKSPACE){ die("Out of memory... Not enough to allocate taskstruct"); @@ -27,9 +26,6 @@ void task_register(void) //We haven't had a task before, therefore we initialize head struct task *t = (struct task *)spacepointer; - if(head == NULL) { - head = t; - } //Read interval t->interval = read16(); @@ -48,12 +44,15 @@ void task_register(void) die("Out of memory... Not enough to allocate bytes"); } + spacepointer += sizeof(struct task); + t->bc = spacepointer; spacepointer += 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; @@ -62,6 +61,7 @@ void task_register(void) write_byte('t'); write16(t->taskid); write_byte('\n'); + debug("free memory: %lu\n", ((uint8_t *)taskspace)+TASKSPACE-spacepointer); } bool is_interrupt_task(struct task *t) @@ -78,32 +78,31 @@ bool had_interrupt(struct task* t) struct task *task_head(void) { - return head; + return spacepointer == (uint8_t *)&taskspace ? NULL + : (struct task *)&taskspace; } 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; + return next >= spacepointer ? 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); + debug("spacepointer: %p", spacepointer); + struct task *t = task_head(); while(t != NULL){ if(t->taskid == c){ break; } t = task_next(t); } + if(t != NULL){ //We found the task, now we move everything from the end of the task up //to the spacepointer right @@ -116,14 +115,6 @@ void task_delete(void) //Decrement the spacepointer spacepointer -= end-start; - - if(t == head){ - head = task_next(t); - } - - //Write acknowledgement - write_byte('d'); - write16(c); - write_byte('\n'); } + debug("spacepointer: %p", spacepointer); }