46
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include "TaskListInfo.h"
|
|
4
|
|
5 TaskListInfo::TaskListInfo(void)
|
|
6 :taskListPool(NULL), freeTaskList(NULL) {}
|
|
7
|
|
8 TaskListInfo::~TaskListInfo(void) { destroy(); }
|
|
9
|
|
10 int
|
|
11 TaskListInfo::init(int num)
|
|
12 {
|
|
13 if (taskListPool == NULL) {
|
|
14 return extend_pool(num);
|
|
15 }
|
|
16 return 0;
|
|
17 }
|
|
18
|
|
19 int
|
|
20 TaskListInfo::extend_pool(int num)
|
|
21 {
|
|
22 TaskListPtr q = NULL;
|
|
23
|
|
24 q = (TaskListPtr)malloc(sizeof(TaskList)*(num+1));
|
|
25
|
|
26 if (q == NULL) {
|
|
27 return -1;
|
|
28 }
|
|
29
|
|
30 q->next = taskListPool;
|
|
31 taskListPool = q;
|
|
32
|
|
33 /* Connect all free pack_list in the pool */
|
|
34 for (q = taskListPool + 1; --num > 0; q++) {
|
|
35 q->next = q + 1;
|
|
36 }
|
|
37 q->next = freeTaskList;
|
|
38 freeTaskList = taskListPool + 1;
|
|
39
|
|
40 return 0;
|
|
41 }
|
|
42
|
|
43 TaskListPtr
|
|
44 TaskListInfo::create(void)
|
|
45 {
|
|
46 TaskListPtr q;
|
|
47
|
|
48 if (freeTaskList == NULL) {
|
|
49 extend_pool(10);
|
|
50 }
|
|
51 q = freeTaskList;
|
|
52 freeTaskList = freeTaskList->next;
|
|
53
|
|
54 q->length = 0;
|
|
55 q->next = 0;
|
|
56
|
|
57 return q;
|
|
58 }
|
|
59
|
|
60 void
|
|
61 TaskListInfo::free(TaskListPtr q)
|
|
62 {
|
|
63 q->next = freeTaskList;
|
|
64 freeTaskList = q;
|
|
65 }
|
|
66
|
|
67 void
|
|
68 TaskListInfo::destroy(void)
|
|
69 {
|
|
70 TaskListPtr q;
|
|
71
|
|
72 for (q = taskListPool; q; q = q->next) {
|
|
73 free(q);
|
|
74 }
|
|
75 freeTaskList = taskListPool = NULL;
|
|
76 }
|
|
77
|
|
78 TaskListPtr
|
|
79 TaskListInfo::append(TaskListPtr list, TaskListPtr q)
|
|
80 {
|
|
81 TaskListPtr p = list;
|
|
82
|
|
83 if (!p) {
|
|
84 return q;
|
|
85 } else {
|
|
86 while (p->next) p = p->next;
|
|
87 p->next = q;
|
|
88 return list;
|
|
89 }
|
|
90 }
|