Service function reset
Just like the other library functions, the reset service function takes a predefined format. Here we create the module context at the first call to this function and delete it at the subsequent call. When deleting the context the reset function needs to free all space that has been allocated by the module during the execution of a model. Therefore, every time a task is created it is added to the list of tasks in the module context and it is removed from the list if it is deleted explicitly by a call to the type instance deletion function. As mentioned earlier, even if a module provides deletion functions for all the types that it defines (as in this example) it is required to implement the reset service to free any remaining allocated space because Mosel does not guarantee that the type instance deletion function gets called for every object that has been created by the module.
static void *task_reset(XPRMcontext ctx, void *libctx, int version)
{
s_taskctx *taskctx;
s_task *task;
if(libctx==NULL) /* At start: create the context */
{
taskctx=malloc(sizeof(s_taskctx));
memset(taskctx, 0, sizeof(s_taskctx));
return taskctx;
}
else /* At the end: delete everything */
{
taskctx=libctx;
while(taskctx->firsttask!=NULL)
{
task=taskctx->firsttask;
taskctx->firsttask=task->next;
free(task);
}
free(taskctx);
return NULL;
}
}
