Repository navigation
Expand file tree
/
Copy pathbufferpool.cpp
More file actions
149 lines (132 loc) · 3.28 KB
/
Copy pathbufferpool.cpp
File metadata and controls
149 lines (132 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "bufferpool.h"
#include <stdlib.h>
#include <QDebug>
BufferPool::BufferPool() {
head = nullptr;
head_free = nullptr;
for (int i = 0; i < N; i++) {
MyMsgBuf* temp = new MyMsgBuf();
temp->next = nullptr;
temp->next_free = nullptr;
temp->isUsed = false;
if (!head_free) {
head_free = temp;
} else {
MyMsgBuf* ptr = head_free;
while (ptr->next_free) {
ptr = ptr->next_free;
}
ptr->next_free = temp;
}
}
cur_bytes = 0;
number_of_que_messages = 0;
}
MyMsgBuf* BufferPool::select() {
if (!head_free) {
return nullptr;
}
MyMsgBuf* temp = head_free;
head_free = head_free->next_free;
temp->next_free = nullptr;
temp->isUsed = true;
if (!head) {
head = temp;
} else {
MyMsgBuf* current = head;
while (current &¤t->next) {
if (!current) { // 额外的检查,以避免段错误
return nullptr;
}
current = current->next;
}
current->next = temp;
}
return temp;
}
void BufferPool::que_num(int sr){
if(sr == 1){
cur_bytes += sizeof(MyMsg);
number_of_que_messages++;
}
else{
cur_bytes -= sizeof(MyMsg);
number_of_que_messages--;
}
}
void BufferPool::release(MyMsgBuf* temp) {
if (!temp || !head) return; // 额外的检查,以避免段错误
if (head == temp) {
head = head->next;
} else {
MyMsgBuf* prev = head;
while (prev->next && prev->next != temp) {
if (!prev) { // 额外的检查,以避免段错误
return;
}
prev = prev->next;
}
if (prev->next == temp) {
prev->next = temp->next;
}
}
temp->isUsed = false;
temp->next = nullptr;
if (!head_free) {
head_free = temp;
} else {
MyMsgBuf* ptr = head_free;
while (ptr->next_free) {
if (!ptr) { // 额外的检查,以避免段错误
return;
}
ptr = ptr->next_free;
}
ptr->next_free = temp;
}
}
QString BufferPool::getStatus() {
QString status = "Buffer Pool: [";
MyMsgBuf* current = head;
while (current) {
status += (current->isUsed ? "Used" : "Free");
current = current->next;
if (current) {
status += ", ";
}
}
status += "]";
return status;
}
QString BufferPool::getMessageChainStatus() const {
QString status;
MyMsgBuf* current = head;
if (!current) {
qDebug() << "Message chain is empty.";
}
while (current != nullptr) {
status += QString("Msg ID: %1, Content: %2\n").arg(current->mymsg.mtype).arg(current->mymsg.mtext);
current = current->next;
}
return status;
}
int BufferPool::getMessageChainCount() const {
int count = 0;
MyMsgBuf* current = head;
while (current) {
count++;
current = current->next;
}
return count;
}
MyMsgBuf* BufferPool::getFirstMessage() const{
return head;
}
BufferPool::~BufferPool() {
MyMsgBuf* temp;
while(head) {
temp = head;
head = head->next;
delete temp;
}
}