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
|
93
|
24 static int i = 0;
|
|
25 printf("extend %d\n", ++i);
|
|
26
|
46
|
27 q = (TaskQueuePtr)malloc(sizeof(TaskQueue)*(num+1));
|
|
28
|
|
29 if (q == NULL) {
|
|
30 return -1;
|
|
31 }
|
|
32 q->next = taskQueuePool;
|
|
33 taskQueuePool = q;
|
|
34
|
|
35 /* Connect all free queue in the pool */
|
|
36 for (q = taskQueuePool + 1; --num > 0; q++) {
|
|
37 q->next = q + 1;
|
|
38 }
|
|
39 q->next = freeTaskQueue;
|
|
40 freeTaskQueue = taskQueuePool + 1;
|
|
41
|
|
42 return 0;
|
|
43 }
|
|
44
|
|
45 TaskQueuePtr
|
|
46 TaskQueueInfo::create(HTaskPtr task)
|
|
47 {
|
|
48 TaskQueuePtr q;
|
93
|
49
|
|
50 static int i = 0;
|
|
51
|
|
52 printf("create %d\n", ++i);
|
46
|
53
|
|
54 if (freeTaskQueue == NULL) {
|
|
55 extend_pool(100);
|
|
56 }
|
|
57 q = freeTaskQueue;
|
|
58 freeTaskQueue = freeTaskQueue->next;
|
|
59
|
|
60 q->task = task;
|
|
61 q->next = NULL;
|
|
62
|
|
63 return q;
|
|
64 }
|
|
65
|
|
66
|
|
67 void
|
|
68 TaskQueueInfo::free(TaskQueuePtr q)
|
|
69 {
|
93
|
70 static int i = 0;
|
|
71
|
|
72 printf("free %d\n", ++i);
|
|
73 printf("## command %d ##\n", q->task->command);
|
|
74
|
46
|
75 q->next = freeTaskQueue;
|
|
76 freeTaskQueue = q;
|
|
77 }
|
|
78
|
|
79
|
|
80 void
|
|
81 TaskQueueInfo::destroy(void)
|
|
82 {
|
|
83 TaskQueuePtr q;
|
|
84
|
|
85 for (q = taskQueuePool; q; q = q->next) {
|
|
86 free(q);
|
|
87 }
|
|
88 freeTaskQueue = taskQueuePool = NULL;
|
|
89 }
|
|
90
|
|
91
|
|
92 TaskQueuePtr
|
|
93 TaskQueueInfo::append(TaskQueuePtr list, TaskQueuePtr q)
|
|
94 {
|
|
95 TaskQueuePtr p = list;
|
|
96
|
|
97 if (!p) {
|
|
98 return q;
|
|
99 } else {
|
|
100 while(p->next) p = p->next;
|
|
101 p->next = q;
|
|
102 return list;
|
|
103 }
|
|
104 }
|