view TaskManager/kernel/ppe/TaskListInfo.cc @ 247:0098b5ff0d11

change example
author aaa
date Mon, 01 Jun 2009 19:33:09 +0900
parents 5c194c71eca8
children e083c4ff91c1
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include "TaskListInfo.h"

TaskListInfo::TaskListInfo(void)
    :taskListPool(NULL), freeTaskList(NULL) {}

TaskListInfo::~TaskListInfo(void) { destroy(); }

int
TaskListInfo::init(int num)
{
    if (taskListPool == NULL) {
	return extend_pool(num);
    }
    return 0;
}

int
TaskListInfo::extend_pool(int num)
{
    TaskListPtr q = NULL;

    q = (TaskListPtr)malloc(sizeof(TaskList)*(num+1));

    if (q == NULL) {
	return -1;
    }

    q->next = taskListPool;
    taskListPool = q;

    /* Connect all free pack_list in the pool */
    for (q = taskListPool + 1; --num > 0; q++) {
	q->next = q + 1;
    }
    q->next = freeTaskList;
    freeTaskList = taskListPool + 1;

    return 0;
}

TaskListPtr
TaskListInfo::create(void)
{
    TaskListPtr q;

    if (freeTaskList == NULL) {
	extend_pool(10);
    }
    q = freeTaskList;
    freeTaskList = freeTaskList->next;

    q->length = 0;
    q->next = 0;

    return q;
}

void
TaskListInfo::free(TaskListPtr q)
{
    q->next = freeTaskList;
    freeTaskList = q;
}

void
TaskListInfo::destroy(void)
{
    TaskListPtr q, tmp;

    //for (q = taskListPool; q; q = q->next) {
    q = taskListPool;
    while (q) {
	tmp = q->next;
	free(q);
	q = tmp;
    }
    freeTaskList = taskListPool = NULL;
}

TaskListPtr
TaskListInfo::append(TaskListPtr list, TaskListPtr q)
{
    TaskListPtr p = list;

    if (!p) {
	return q;
    } else {
	while (p->next) p = p->next;
	p->next = q;
	return list;
    }
}