Mercurial > hg > Members > kono > Cerium
annotate TaskManager/kernel/ppe/QueueInfo.h @ 955:9989dd7b9ac2
unify all QueueInfo
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 04 Aug 2010 16:46:25 +0900 |
parents | 586b4f825f24 |
children | 15026ebf7a17 |
rev | line source |
---|---|
801 | 1 #ifndef INCLUDED_QUEUE_INFO |
2 #define INCLUDED_QUEUE_INFO | |
3 | |
4 #include "base.h" | |
821 | 5 #include "types.h" |
801 | 6 |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
7 #if 0 |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
8 template <typename T> class Queue : T { |
801 | 9 public: |
10 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
11 T(); |
801 | 12 |
13 T *waiter; | |
14 T *next; | |
15 T *prev; | |
16 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
17 // virual void init(); |
801 | 18 }; |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
19 #endif |
801 | 20 |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
21 template <typename T> class QueueInfo : public T { |
801 | 22 |
23 public: | |
24 /* constructor */ | |
821 | 25 QueueInfo<T>(){ |
26 queueInfoInit(); | |
27 } | |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
28 QueueInfo<T>(QueueInfo<T> *p) { |
821 | 29 queueInfoInit(); |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
30 queuePool = p; |
821 | 31 queuePool->queuePool = queuePool; |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
32 } |
801 | 33 |
34 BASE_NEW_DELETE(QueueInfo); | |
35 | |
36 /* functions */ | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
37 T *create(); |
801 | 38 |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
39 void free_(T *queue); |
801 | 40 |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
41 void addFirst(T* e); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
42 void addLast(T* e); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
43 T* getFirst(); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
44 T* getLast(); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
45 int remove(T* e); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
46 T* poll(); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
47 void moveToFirst(T* e); // or use(); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
48 T* get(int index); |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
49 T* find(T *task); |
801 | 50 int empty(); |
51 void freePool() ; | |
955 | 52 void freeAll(); |
801 | 53 |
54 // Iterator | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
55 T* getNext(T* q) ; |
801 | 56 int length(); |
57 | |
58 private: | |
59 /* variables */ | |
60 | |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
61 /* static */ QueueInfo<T> *queuePool; |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
62 T* first; |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
63 T* last; |
801 | 64 |
65 /* functions */ | |
66 int extend_pool(int num); | |
67 void destroy(); | |
821 | 68 void queueInfoInit(); |
899 | 69 } ; |
801 | 70 |
71 | |
72 | |
73 #ifdef CHECK | |
74 #include <stdio.h> | |
75 #endif | |
76 #include <stdlib.h> | |
77 | |
78 | |
79 // Singleton T Pool | |
80 // you have to define it at where you want to use. | |
81 // template<typename T>static QueueInfo<T> QueueInfo<T>::queuePool; | |
82 | |
821 | 83 template<typename T>void QueueInfo<T>::queueInfoInit() { |
801 | 84 // 最初の一つは自分 |
85 first = last = this; | |
86 this->next = this->prev = this; | |
87 this->waiter = NULL; | |
88 } | |
89 | |
90 template<typename T>void | |
91 QueueInfo<T>::freePool() { | |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
92 for(T * p = queuePool->waiter; p; ) { |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
93 T * next = p->waiter; |
801 | 94 p->waiter = NULL; |
95 free(p); | |
96 p = next; | |
97 } | |
98 } | |
99 | |
100 template<typename T>int | |
101 QueueInfo<T>::extend_pool(int num) | |
102 { | |
821 | 103 T* q = (T*)malloc(sizeof(T)*(num+1)+DEFAULT_ALIGNMENT); |
801 | 104 |
105 // First Queue is previous pool | |
106 q->waiter = this->waiter; this->waiter = q; | |
821 | 107 q = (T*)ROUND_UP_ALIGN((long)q, DEFAULT_ALIGNMENT); |
801 | 108 q++; |
109 | |
110 /* Connect all free queue in the pool */ | |
821 | 111 T* p = q; |
112 for (; num-- > 0;) { | |
113 p->waiter = NULL; | |
114 queuePool->addLast(p); | |
115 p = (T*)ROUND_UP_ALIGN((long)(p+1),DEFAULT_ALIGNMENT); | |
801 | 116 } |
117 | |
118 return 0; | |
821 | 119 |
801 | 120 } |
121 | |
122 /** | |
123 * Task をプールから取って来て返す | |
124 * | |
125 * @param [cmd] タスクコマンド | |
126 */ | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
127 template<typename T>T * |
801 | 128 QueueInfo<T>::create() |
129 { | |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
130 T * q = queuePool->poll(); |
801 | 131 if (! q) { |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
132 queuePool->extend_pool(64); |
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
133 q = queuePool->poll(); |
801 | 134 } |
135 q->init(); | |
136 return q; | |
137 } | |
138 | |
139 | |
140 template<typename T>void | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
141 QueueInfo<T>::free_(T * q) |
801 | 142 { |
143 q->waiter = NULL; | |
820
e6eea3a208cd
give up singleton pattern.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
818
diff
changeset
|
144 queuePool->addLast(q); |
801 | 145 } |
146 | |
147 | |
148 /*! | |
149 QueueInfo<T> は空にならない。最低1個は要素が入っていて | |
150 1個目は特別扱いする。getFirst すると first->next を返す | |
151 */ | |
152 | |
153 /*! | |
154 最初の1個は特別扱いなので、それの後に追加していく | |
155 */ | |
156 template<typename T>void | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
157 QueueInfo<T>::addFirst(T* e) |
801 | 158 { |
159 e->prev = first; | |
160 e->next = first->next; | |
161 first->next->prev = e; | |
162 first->next = e; | |
163 } | |
164 | |
165 template<typename T>void | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
166 QueueInfo<T>::addLast(T* e) |
801 | 167 { |
168 #ifdef CHECK | |
169 if (find(e)) { | |
170 fprintf(stderr,"Add duplicate task %0x\n",(int)e); | |
171 return; | |
172 // ... | |
173 } | |
174 #endif | |
175 e->next = first; | |
176 e->prev = last; | |
177 last->next = e; | |
178 last = e; | |
179 } | |
180 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
181 template<typename T>T* |
801 | 182 QueueInfo<T>::getFirst() |
183 { | |
184 if (empty()) return NULL; | |
185 return first->next; | |
186 } | |
187 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
188 template<typename T>T* |
801 | 189 QueueInfo<T>::getLast() |
190 { | |
191 if (empty()) return NULL; | |
192 return last; | |
193 } | |
194 | |
195 template<typename T>int | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
196 QueueInfo<T>::remove(T* e) |
801 | 197 { |
198 #ifdef CHECK | |
199 if (!find(e)) { | |
200 fprintf(stderr,"Remove non existing task %0x\n",(int)e); | |
201 return 0; | |
202 // ... | |
203 } | |
204 #endif | |
205 e->prev->next = e->next; | |
206 e->next->prev = e->prev; | |
207 | |
208 if (first->next == e) { | |
209 first->next = e->next; | |
210 } | |
211 if (last == e) { | |
212 last = e->prev; | |
213 } | |
214 | |
215 e->prev = NULL; | |
216 e->next = NULL; | |
217 | |
218 return 1; | |
219 } | |
220 | |
221 /*! | |
222 リストの先頭を取得および削除する。リストが空の場合は NULL を返す。 | |
223 */ | |
224 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
225 template<typename T>T* |
801 | 226 QueueInfo<T>::poll() |
227 { | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
228 T* e = first->next; |
801 | 229 if (e == this) { |
230 return NULL; | |
231 } | |
232 remove(e); | |
233 return e; | |
234 } | |
235 | |
236 template<typename T>void | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
237 QueueInfo<T>::moveToFirst(T* e) |
801 | 238 { |
239 remove(e); | |
240 addFirst(e); | |
241 } | |
242 | |
243 /*! | |
244 リスト内の指定された位置にある要素を返す。 | |
245 要素数を超えた位置を指定した場合 NULL を返す。 | |
246 */ | |
247 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
248 template<typename T>T* |
801 | 249 QueueInfo<T>::get(int index) |
250 { | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
251 T* e = first->next; |
801 | 252 for (int i = 0; i < index; i++) { |
822 | 253 if (e->next == this) return NULL; |
801 | 254 e = e->next; |
255 } | |
256 return e; | |
257 } | |
258 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
259 template<typename T>T* |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
260 QueueInfo<T>::find(T* task) |
801 | 261 { |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
262 T* e = first->next; |
801 | 263 for(;;) { |
264 if (e == this) return NULL; | |
265 if (e == task) break; | |
266 e = e->next; | |
267 } | |
268 return e; | |
269 } | |
270 | |
271 template<typename T>int | |
272 QueueInfo<T>::empty() | |
273 { | |
274 return this->next == this; | |
275 } | |
276 | |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
277 template<typename T>T* |
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
278 QueueInfo<T>::getNext(T* q) |
801 | 279 { |
280 if (q->next==this) return NULL; | |
281 return q->next; | |
282 } | |
283 | |
284 template<typename T>int | |
285 QueueInfo<T>::length() | |
286 { | |
822 | 287 int i = 0; |
801 | 288 if (empty()) return 0; |
818
19c6cdeb23d6
too few template-parameter-lists
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
801
diff
changeset
|
289 T* e = first; |
801 | 290 while((e = e->next) != this ) i++; |
291 return i; | |
292 } | |
293 | |
955 | 294 template<typename T>void |
295 QueueInfo<T>::freeAll() | |
296 { | |
297 T* t; | |
298 while((t=poll())) free_(t); | |
299 } | |
801 | 300 |
301 | |
302 | |
303 /* end */ | |
304 | |
305 | |
306 | |
307 #endif |