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
|
|
43 MailManager::create(unsigned int data)
|
|
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 {
|
|
62 q->next = freeMailQueue;
|
|
63 freeMailQueue = q;
|
|
64 }
|
|
65
|
|
66
|
|
67 void
|
|
68 MailManager::destroy(void)
|
|
69 {
|
|
70 MailQueuePtr q;
|
|
71
|
194
|
72 #if 0
|
50
|
73 for (q = mailQueuePool; q; q = q->next) {
|
|
74 free(q);
|
|
75 }
|
194
|
76 #else
|
|
77 q = mailQueuePool;
|
|
78 while (q) {
|
|
79 MailQueuePtr tmp = q->next;
|
|
80 free(q);
|
|
81 q = tmp;
|
|
82 }
|
|
83 #endif
|
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 }
|