forest-net
an overlay networks for large-scale virtual worlds
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
PacketStore.cpp
Go to the documentation of this file.
1
9
#include "PacketStore.h"
10
11
namespace
forest {
12
13
18
PacketStore::PacketStore
(
int
N1,
int
M1) : N(N1), M(M1) {
19
n
=
m
= 0;
20
pkt
=
new
Packet
[
N
+1];
pb
=
new
int
[
N
+1];
21
buff
=
new
buffer_t[
M
+1];
22
ref
=
new
int
[
M
+1];
23
freePkts
=
new
List(
N
);
freeBufs
=
new
List(
M
);
24
25
int
i;
26
for
(i = 1; i <=
N
; i++) {
freePkts
->addLast(i);
pb
[i] = 0; }
27
for
(i = 1; i <=
M
; i++) {
freeBufs
->addLast(i);
ref
[i] = 0; }
28
pb
[0] =
ref
[0] = 0;
29
};
30
31
PacketStore::~PacketStore() {
32
delete
[]
pkt
;
delete
[]
pb
;
delete
[]
buff
;
delete
[]
ref
;
33
delete
freePkts
;
delete
freeBufs
;
34
}
35
39
pktx
PacketStore::alloc
() {
40
unique_lock<mutex> lck(
mtx
);
41
42
if
(
freePkts
->empty() ||
freeBufs
->empty()) {
43
lck.unlock();
return
0;
44
}
45
n
++;
m
++;
46
pktx px =
freePkts
->get(1);
freePkts
->removeFirst();
47
int
b =
freeBufs
->get(1);
freeBufs
->removeFirst();
48
ref
[b] = 1;
49
pkt
[px].
buffer
= &
buff
[b];
50
lck.unlock();
51
return
px;
52
}
53
58
void
PacketStore::free
(pktx px) {
59
unique_lock<mutex> lck(
mtx
);
60
if
(px < 1 || px >
N
||
freePkts
->member(px)) {
61
lck.unlock();
return
;
62
}
63
int
b =
pkt
[px].
buffer
-
buff
;
64
freePkts
->addFirst(px);
n
--;
65
if
((--
ref
[b]) == 0) {
freeBufs
->addFirst(b);
m
--; }
66
lck.unlock();
67
}
68
75
pktx
PacketStore::clone
(pktx px) {
76
unique_lock<mutex> lck(
mtx
);
77
if
(
freePkts
->empty()) {
78
lck.unlock();
return
0;
79
}
80
n
++;
81
pktx px1 =
freePkts
->get(1);
freePkts
->removeFirst();
82
pkt
[px1] =
pkt
[px];
83
int
b =
pkt
[px].
buffer
-
buff
;
ref
[b]++;
84
lck.unlock();
85
return
px1;
86
}
87
92
pktx
PacketStore::fullCopy
(pktx px) {
93
unique_lock<mutex> lck(
mtx
);
94
int
px1 =
alloc
();
95
if
(px1 == 0) {
96
lck.unlock();
return
0;
97
}
98
lck.unlock();
99
Packet
& p =
getPacket
(px);
Packet
& p1 =
getPacket
(px1);
100
int
len = (p.
length
+3)/4;
101
std::copy(p.
buffer
, p.
buffer
+len, p1.
buffer
);
102
return
px1;
103
}
104
105
}
// ends namespace
106
forest-net
cpp
common
PacketStore.cpp
Generated on Mon Aug 4 2014 11:13:35 for forest-net by
1.8.4