comparison TaskManager/kernel/ppe/QueueInfo.h @ 958:58ff7b6fdbce

add freeOnce()
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 04 Aug 2010 17:42:36 +0900
parents 15026ebf7a17
children 02f1a707ee82
comparison
equal deleted inserted replaced
957:559d041313dc 958:58ff7b6fdbce
12 12
13 T *waiter; 13 T *waiter;
14 T *next; 14 T *next;
15 T *prev; 15 T *prev;
16 16
17 void initOnce(); // to initialize object in pool
18 void freeOnce(); // to destroy object in pool
19
17 // virual void init(); 20 // virual void init();
18 }; 21 };
19 #endif 22 #endif
20 23
21 template <typename T> class QueueInfo : public T { 24 template <typename T> class QueueInfo : public T {
22 25
23 public: 26 public:
24 /* constructor */ 27 /* constructor */
28
29 /**
30 singleton constructor
31 use this in non initialize envrionment is wrong.
32 */
25 QueueInfo<T>(){ 33 QueueInfo<T>(){
26 queueInfoInit(); 34 queueInfoInit();
27 } 35 }
36 /**
37 normal constructor requires
38 */
28 QueueInfo<T>(QueueInfo<T> *p) { 39 QueueInfo<T>(QueueInfo<T> *p) {
29 queueInfoInit(); 40 queueInfoInit();
30 queuePool = p; 41 queuePool = p;
31 queuePool->queuePool = queuePool; 42 queuePool->queuePool = queuePool;
32 } 43 }
56 int length(); 67 int length();
57 68
58 private: 69 private:
59 /* variables */ 70 /* variables */
60 71
72 /* we cannot use static in template */
61 /* static */ QueueInfo<T> *queuePool; 73 /* static */ QueueInfo<T> *queuePool;
62 T* first; 74 T* first;
63 T* last; 75 T* last;
64 76
65 /* functions */ 77 /* functions */
74 #include <stdio.h> 86 #include <stdio.h>
75 #endif 87 #endif
76 #include <stdlib.h> 88 #include <stdlib.h>
77 89
78 90
79 // Singleton T Pool 91 /**
80 // you have to define it at where you want to use. 92 singleton constructor
81 // template<typename T>static QueueInfo<T> QueueInfo<T>::queuePool; 93 all queueInfo should share this as a pool.
94
95 exteren QueueInfo<H> pool;
96 QueueInfo<H> pool = new QueueInfo<H>();
97
98 use this in non initialize envrionment is wrong.
99 */
82 100
83 template<typename T>void QueueInfo<T>::queueInfoInit() { 101 template<typename T>void QueueInfo<T>::queueInfoInit() {
84 // 最初の一つは自分 102 // 最初の一つは自分
85 first = last = this; 103 first = last = this;
86 this->next = this->prev = this; 104 this->next = this->prev = this;
90 template<typename T>void 108 template<typename T>void
91 QueueInfo<T>::freePool() { 109 QueueInfo<T>::freePool() {
92 for(T * p = queuePool->waiter; p; ) { 110 for(T * p = queuePool->waiter; p; ) {
93 T * next = p->waiter; 111 T * next = p->waiter;
94 p->waiter = NULL; 112 p->waiter = NULL;
113 p->freeOnce();
95 free(p); 114 free(p);
96 p = next; 115 p = next;
97 } 116 }
98 } 117 }
99 118
119 /**
120 all pools are shared among QueueInfo (separated by size and type).
121 it automatically extended by 64.
122 */
100 template<typename T>int 123 template<typename T>int
101 QueueInfo<T>::extend_pool(int num) 124 QueueInfo<T>::extend_pool(int num)
102 { 125 {
103 T* q = (T*)malloc(sizeof(T)*(num+1)+DEFAULT_ALIGNMENT); 126 T* q = (T*)malloc(sizeof(T)*(num+1)+DEFAULT_ALIGNMENT);
104 127