circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

December 1998, Issue 101

A Minimalist Multitasking Executive


by Richard Man & Christina Willrich
Start Task Function Scheduling and Timeslice Task Context Task Control Block Global Variables Memory Management API Assembly Functions Give it a Try Enhancements References, Sources & PDF

MEMORY MANAGEMENT

µexec needs to allocate memory to hold the task-control blocks and the tasks’ stacks. This allocation can be done by using the C-library function memory-management routines malloc and free or by using statically allocated global arrays.

Since µexec is meant to be used in a small system, the overhead of and possible fragmentation of the memory space by using malloc and free are of concern. So, we use a simple array allocator instead. However, you can easily modify the system to use malloc and free.

Task stacks are supplied as an argument to UEXC_CreateTask. You should define statically allocated arrays and supply them to UEXC_CreateTask:

static unsigned char task_stack[UEXC_MIN_STACK_SIZE];

All the task-control blocks are allocated from a global array:

static TaskControlBlock free_list[NUM_TASKS], *free_list_ptr;

A task-control block is allocated from the free_list_ptr every time a task is created. UEXC_Createtask fails if no more task-control blocks are available.

The system initializes free_list_ ptr with the elements in free_list, so you can adjust the total number of task-control blocks by changing the value of NUM_TASKS. When a task is killed or when a task function returns, its task-control block is released back to the free_list_ptr pool.