shares are dynamically allocated as well:)
[mTask.git] / client / task.c
index c5fc41d..e36600d 100644 (file)
@@ -4,28 +4,24 @@
 #include "task.h"
 #include "spec.h"
 #include "interface.h"
+#include "mem.h"
 
-#define TASKSPACE 128
+extern uint8_t *mem_top;
+extern uint8_t *mem_bottom;
+extern uint8_t *mem_task;
+extern uint8_t *mem_sds;
 
-uint8_t taskspace[TASKSPACE] = {0};
-uint8_t *spacepointer = (uint8_t *)&taskspace;
 uint8_t taskid = 0;
 
-void task_init(void)
-{
-       memset(&taskspace, 0, TASKSPACE);
-}
-
 void task_register(void)
 {
-       debug("free memory: %lu\n", ((uint8_t *)taskspace)-spacepointer+TASKSPACE);
+       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;
+       struct task *t = (struct task *)mem_task;
 
        //Read interval
        t->interval = read16();
@@ -40,13 +36,13 @@ 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 += sizeof(struct task);
-       t->bc = spacepointer;
-       spacepointer += t->tasklength;
+       mem_task += sizeof(struct task);
+       t->bc = mem_task;
+       mem_task += t->tasklength;
 
        //Read task bytecode
        for(i = 0; i<t->tasklength; i++){
@@ -61,7 +57,7 @@ void task_register(void)
        write_byte('t');
        write16(t->taskid);
        write_byte('\n');
-       debug("free memory: %lu\n", ((uint8_t *)taskspace)+TASKSPACE-spacepointer);
+       debug("free memory: %lu\n", mem_free());
 }
 
 bool is_interrupt_task(struct task *t)
@@ -78,23 +74,19 @@ bool had_interrupt(struct task* t)
 
 struct task *task_head(void)
 {
-       return spacepointer == (uint8_t *)&taskspace ? NULL
-               : (struct task *)&taskspace;
+       return mem_task == mem_bottom ? NULL : (struct task *)mem_bottom;
 }
 
 struct task *task_next(struct task *t)
 {
-       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(uint8_t c)
 {
        debug("Going to delete task: %i", c);
-       debug("spacepointer: %p", spacepointer);
+       debug("mem_task: %p", mem_task);
        struct task *t = task_head();
        while(t != NULL){
                if(t->taskid == c){
@@ -108,13 +100,13 @@ void task_delete(uint8_t c)
                //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<spacepointer-end; i++){
+               debug("Moving %lu bytes\n", mem_task-end);
+               for(int i = 0; i<mem_task-end; i++){
                        start[i] = end[i];
                }
 
                //Decrement the spacepointer
-               spacepointer -= end-start;
+               mem_task -= end-start;
        }
-       debug("spacepointer: %p", spacepointer);
+       debug("mem_task: %p", mem_task);
 }