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