Mercurial > hg > Game > Cerium
annotate TaskManager/kernel/ppe/MailManager.cc @ 644:ed50fb6c84dd draft
minor fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 21 Nov 2009 10:59:51 +0900 |
parents | 2a611e8d8788 |
children | 7ba4ad4538b1 |
rev | line source |
---|---|
50 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include "MailManager.h" | |
4 | |
5 MailManager::MailManager(void) | |
6 :mailQueuePool(NULL), freeMailQueue(NULL) {} | |
7 | |
8 MailManager::~MailManager(void) { destroy(); } | |
9 | |
10 int | |
11 MailManager::init(int num) | |
12 { | |
13 if (!mailQueuePool) { | |
14 return extend_pool(num); | |
15 } | |
16 return 0; | |
17 } | |
18 | |
19 int | |
20 MailManager::extend_pool(int num) | |
21 { | |
22 MailQueuePtr q; | |
23 | |
24 q = (MailQueuePtr)malloc(sizeof(MailQueue)*(num+1)); | |
25 | |
26 if (q == NULL) { | |
27 return -1; | |
28 } | |
29 q->next = mailQueuePool; | |
30 mailQueuePool = q; | |
31 | |
32 /* Connect all free queue in the pool */ | |
33 for (q = mailQueuePool + 1; --num > 0; q++) { | |
34 q->next = q + 1; | |
35 } | |
36 q->next = freeMailQueue; | |
37 freeMailQueue = mailQueuePool + 1; | |
38 | |
39 return 0; | |
40 } | |
41 | |
42 MailQueuePtr | |
603
57ec231bc8ac
long -> memaddr (64 or 32)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
602
diff
changeset
|
43 MailManager::create(memaddr data) |
50 | 44 { |
45 MailQueuePtr q; | |
46 | |
47 if (!freeMailQueue) { | |
48 extend_pool(30); | |
49 } | |
50 q = freeMailQueue; | |
51 freeMailQueue = freeMailQueue->next; | |
52 | |
53 q->data = data; | |
54 q->next = NULL; | |
55 | |
56 return q; | |
57 } | |
58 | |
59 void | |
60 MailManager::free(MailQueuePtr q) | |
61 { | |
644 | 62 #if 0 |
643 | 63 if (MailManager::find(q,freeMailQueue)) { |
64 fprintf(stderr,"double free %s\n",__FUNCTION__); | |
65 return; | |
66 } | |
644 | 67 #endif |
50 | 68 q->next = freeMailQueue; |
69 freeMailQueue = q; | |
70 } | |
71 | |
72 | |
73 void | |
74 MailManager::destroy(void) | |
75 { | |
76 MailQueuePtr q; | |
77 | |
194 | 78 q = mailQueuePool; |
79 while (q) { | |
80 MailQueuePtr tmp = q->next; | |
81 free(q); | |
82 q = tmp; | |
83 } | |
50 | 84 freeMailQueue = mailQueuePool = NULL; |
85 } | |
86 | |
87 | |
88 MailQueuePtr | |
89 MailManager::append_mailQueue(MailQueuePtr list, MailQueuePtr q) | |
90 { | |
91 MailQueuePtr p = list; | |
92 | |
93 if (p == NULL) { | |
94 return q; | |
95 } else { | |
96 while(p->next) p = p->next; | |
97 p->next = q; | |
98 return list; | |
99 } | |
100 } | |
640
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
101 |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
102 int |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
103 MailManager::length(MailQueuePtr list) |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
104 { |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
105 MailQueuePtr p = list; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
106 MailQueuePtr q = p; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
107 int i = 0; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
108 |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
109 while(p) { |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
110 i++; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
111 p=p->next; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
112 if (i%2==0) { |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
113 q = q->next; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
114 } |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
115 if (p==q) |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
116 return -1; // means loop |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
117 } |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
118 return i; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
119 } |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
120 |
643 | 121 int |
122 MailManager::find(MailQueuePtr q,MailQueuePtr list) | |
123 { | |
124 MailQueuePtr p = list; | |
125 while(p) { | |
126 if (p==q) | |
127 return -1; | |
128 p = p->next; | |
129 } | |
130 return 0; | |
131 } | |
132 | |
640
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
603
diff
changeset
|
133 /* end */ |