comparison TaskManager/kernel/ppe/QueueInfo.h @ 818:5d48fa762a24 draft

too few template-parameter-lists
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 May 2010 10:55:25 +0900
parents fb49e881f2ed
children 3c508c837ad8
comparison
equal deleted inserted replaced
817:c7d737993da7 818:5d48fa762a24
1 #ifndef INCLUDED_QUEUE_INFO 1 #ifndef INCLUDED_QUEUE_INFO
2 #define INCLUDED_QUEUE_INFO 2 #define INCLUDED_QUEUE_INFO
3 3
4 #include "base.h" 4 #include "base.h"
5 5
6 template <typename T> class Queue { 6 #if 0
7 template <typename T> class Queue : T {
7 public: 8 public:
8 9
9 Queue<T>(); 10 T();
10 11
11 T *waiter; 12 T *waiter;
12 T *next; 13 T *next;
13 T *prev; 14 T *prev;
14 15
15 void init(); 16 // virual void init();
16 }; 17 };
17 18 #endif
18 template <typename T> class QueueInfo : public Queue<T> { 19
20 template <typename T> class QueueInfo : public T {
19 21
20 public: 22 public:
21 /* constructor */ 23 /* constructor */
22 QueueInfo<T>(); 24 QueueInfo<T>();
23 25
24 BASE_NEW_DELETE(QueueInfo); 26 BASE_NEW_DELETE(QueueInfo);
25 27
26 /* functions */ 28 /* functions */
27 Queue<T> *create(); 29 T *create();
28 30
29 void free_(Queue<T> *queue); 31 void free_(T *queue);
30 32
31 void addFirst(Queue<T>* e); 33 void addFirst(T* e);
32 void addLast(Queue<T>* e); 34 void addLast(T* e);
33 Queue<T>* getFirst(); 35 T* getFirst();
34 Queue<T>* getLast(); 36 T* getLast();
35 int remove(Queue<T>* e); 37 int remove(T* e);
36 Queue<T>* poll(); 38 T* poll();
37 void moveToFirst(Queue<T>* e); // or use(); 39 void moveToFirst(T* e); // or use();
38 Queue<T>* get(int index); 40 T* get(int index);
39 Queue<T>* find(Queue<T> *task); 41 T* find(T *task);
40 int empty(); 42 int empty();
41 void freePool() ; 43 void freePool() ;
42 44
43 // Iterator 45 // Iterator
44 Queue<T>* getNext(Queue<T>* q) ; 46 T* getNext(T* q) ;
45 int length(); 47 int length();
46 48
47 private: 49 private:
48 /* variables */ 50 /* variables */
49 51
50 static QueueInfo<T> queuePool; 52 static QueueInfo<T> queuePool;
51 Queue<T>* first; 53 T* first;
52 Queue<T>* last; 54 T* last;
53 55
54 /* functions */ 56 /* functions */
55 int extend_pool(int num); 57 int extend_pool(int num);
56 void destroy(); 58 void destroy();
57 }; 59 };
75 this->waiter = NULL; 77 this->waiter = NULL;
76 } 78 }
77 79
78 template<typename T>void 80 template<typename T>void
79 QueueInfo<T>::freePool() { 81 QueueInfo<T>::freePool() {
80 for(Queue<T> * p = queuePool.waiter; p; ) { 82 for(T * p = queuePool.waiter; p; ) {
81 Queue<T> * next = p->waiter; 83 T * next = p->waiter;
82 p->waiter = NULL; 84 p->waiter = NULL;
83 free(p); 85 free(p);
84 p = next; 86 p = next;
85 } 87 }
86 } 88 }
87 89
88 template<typename T>int 90 template<typename T>int
89 QueueInfo<T>::extend_pool(int num) 91 QueueInfo<T>::extend_pool(int num)
90 { 92 {
91 Queue<T> * q = (Queue<T> *)malloc(sizeof(Queue<T>)*(num+1)); 93 T * q = (T *)malloc(sizeof(T)*(num+1));
92 94
93 // First Queue is previous pool 95 // First Queue is previous pool
94 q->waiter = this->waiter; this->waiter = q; 96 q->waiter = this->waiter; this->waiter = q;
95 q++; 97 q++;
96 98
97 /* Connect all free queue in the pool */ 99 /* Connect all free queue in the pool */
98 Queue<T> * p = q; 100 T * p = q;
99 for (; num-- > 0; p++) { 101 for (; num-- > 0; p++) {
100 p->waiter = NULL; 102 p->waiter = NULL;
101 queuePool.addLast(p); 103 queuePool.addLast(p);
102 } 104 }
103 105
107 /** 109 /**
108 * Task をプールから取って来て返す 110 * Task をプールから取って来て返す
109 * 111 *
110 * @param [cmd] タスクコマンド 112 * @param [cmd] タスクコマンド
111 */ 113 */
112 template<typename T>Queue<T> * 114 template<typename T>T *
113 QueueInfo<T>::create() 115 QueueInfo<T>::create()
114 { 116 {
115 Queue<T> * q = queuePool.poll(); 117 T * q = queuePool.poll();
116 if (! q) { 118 if (! q) {
117 queuePool.extend_pool(64); 119 queuePool.extend_pool(64);
118 q = queuePool.poll(); 120 q = queuePool.poll();
119 } 121 }
120 q->init(); 122 q->init();
121 return q; 123 return q;
122 } 124 }
123 125
124 126
125 template<typename T>void 127 template<typename T>void
126 QueueInfo<T>::free_(Queue<T> * q) 128 QueueInfo<T>::free_(T * q)
127 { 129 {
128 q->waiter = NULL; 130 q->waiter = NULL;
129 queuePool.addLast(q); 131 queuePool.addLast(q);
130 } 132 }
131 133
137 139
138 /*! 140 /*!
139 最初の1個は特別扱いなので、それの後に追加していく 141 最初の1個は特別扱いなので、それの後に追加していく
140 */ 142 */
141 template<typename T>void 143 template<typename T>void
142 QueueInfo<T>::addFirst(Queue<T>* e) 144 QueueInfo<T>::addFirst(T* e)
143 { 145 {
144 e->prev = first; 146 e->prev = first;
145 e->next = first->next; 147 e->next = first->next;
146 first->next->prev = e; 148 first->next->prev = e;
147 first->next = e; 149 first->next = e;
148 } 150 }
149 151
150 template<typename T>void 152 template<typename T>void
151 QueueInfo<T>::addLast(Queue<T>* e) 153 QueueInfo<T>::addLast(T* e)
152 { 154 {
153 #ifdef CHECK 155 #ifdef CHECK
154 if (find(e)) { 156 if (find(e)) {
155 fprintf(stderr,"Add duplicate task %0x\n",(int)e); 157 fprintf(stderr,"Add duplicate task %0x\n",(int)e);
156 return; 158 return;
161 e->prev = last; 163 e->prev = last;
162 last->next = e; 164 last->next = e;
163 last = e; 165 last = e;
164 } 166 }
165 167
166 template<typename T>Queue<T>* 168 template<typename T>T*
167 QueueInfo<T>::getFirst() 169 QueueInfo<T>::getFirst()
168 { 170 {
169 if (empty()) return NULL; 171 if (empty()) return NULL;
170 return first->next; 172 return first->next;
171 } 173 }
172 174
173 template<typename T>Queue<T>* 175 template<typename T>T*
174 QueueInfo<T>::getLast() 176 QueueInfo<T>::getLast()
175 { 177 {
176 if (empty()) return NULL; 178 if (empty()) return NULL;
177 return last; 179 return last;
178 } 180 }
179 181
180 template<typename T>int 182 template<typename T>int
181 QueueInfo<T>::remove(Queue<T>* e) 183 QueueInfo<T>::remove(T* e)
182 { 184 {
183 #ifdef CHECK 185 #ifdef CHECK
184 if (!find(e)) { 186 if (!find(e)) {
185 fprintf(stderr,"Remove non existing task %0x\n",(int)e); 187 fprintf(stderr,"Remove non existing task %0x\n",(int)e);
186 return 0; 188 return 0;
205 207
206 /*! 208 /*!
207 リストの先頭を取得および削除する。リストが空の場合は NULL を返す。 209 リストの先頭を取得および削除する。リストが空の場合は NULL を返す。
208 */ 210 */
209 211
210 template<typename T>Queue<T>* 212 template<typename T>T*
211 QueueInfo<T>::poll() 213 QueueInfo<T>::poll()
212 { 214 {
213 Queue<T>* e = first->next; 215 T* e = first->next;
214 if (e == this) { 216 if (e == this) {
215 return NULL; 217 return NULL;
216 } 218 }
217 remove(e); 219 remove(e);
218 return e; 220 return e;
219 } 221 }
220 222
221 template<typename T>void 223 template<typename T>void
222 QueueInfo<T>::moveToFirst(Queue<T>* e) 224 QueueInfo<T>::moveToFirst(T* e)
223 { 225 {
224 remove(e); 226 remove(e);
225 addFirst(e); 227 addFirst(e);
226 } 228 }
227 229
228 /*! 230 /*!
229 リスト内の指定された位置にある要素を返す。 231 リスト内の指定された位置にある要素を返す。
230 要素数を超えた位置を指定した場合 NULL を返す。 232 要素数を超えた位置を指定した場合 NULL を返す。
231 */ 233 */
232 234
233 template<typename T>Queue<T>* 235 template<typename T>T*
234 QueueInfo<T>::get(int index) 236 QueueInfo<T>::get(int index)
235 { 237 {
236 Queue<T>* e = first->next; 238 T* e = first->next;
237 for (int i = 0; i < index; i++) { 239 for (int i = 0; i < index; i++) {
238 if (e == this) return NULL; 240 if (e == this) return NULL;
239 e = e->next; 241 e = e->next;
240 } 242 }
241 return e; 243 return e;
242 } 244 }
243 245
244 template<typename T>Queue<T>* 246 template<typename T>T*
245 QueueInfo<T>::find(Queue<T>* task) 247 QueueInfo<T>::find(T* task)
246 { 248 {
247 Queue<T>* e = first->next; 249 T* e = first->next;
248 for(;;) { 250 for(;;) {
249 if (e == this) return NULL; 251 if (e == this) return NULL;
250 if (e == task) break; 252 if (e == task) break;
251 e = e->next; 253 e = e->next;
252 } 254 }
257 QueueInfo<T>::empty() 259 QueueInfo<T>::empty()
258 { 260 {
259 return this->next == this; 261 return this->next == this;
260 } 262 }
261 263
262 template<typename T>Queue<T>* 264 template<typename T>T*
263 QueueInfo<T>::getNext(Queue<T>* q) 265 QueueInfo<T>::getNext(T* q)
264 { 266 {
265 if (q->next==this) return NULL; 267 if (q->next==this) return NULL;
266 return q->next; 268 return q->next;
267 } 269 }
268 270
269 template<typename T>int 271 template<typename T>int
270 QueueInfo<T>::length() 272 QueueInfo<T>::length()
271 { 273 {
272 int i = 1; 274 int i = 1;
273 if (empty()) return 0; 275 if (empty()) return 0;
274 Queue<T>* e = first; 276 T* e = first;
275 while((e = e->next) != this ) i++; 277 while((e = e->next) != this ) i++;
276 return i; 278 return i;
277 } 279 }
278 280
279 281