forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
PacketStoreTs.cpp
Go to the documentation of this file.
1 
9 #include "PacketStoreTs.h"
10 
11 namespace forest {
12 
17  n = 0;
18  pkt = new Packet[N+1];
19  buff = new buffer_t[N+1];
20  freePkts = new List(N);
21 
22  for (int i = 1; i <= N; i++) {
23  pkt[i].buffer = &buff[i];
24  freePkts->addLast(i);
25  }
26 
27  pthread_mutex_init(&lock,NULL);
28 };
29 
30 PacketStoreTs::~PacketStoreTs() {
31  delete [] pkt; delete [] buff; delete freePkts;
32 }
33 
38  pthread_mutex_lock(&lock);
39 
40  pktx px;
41  if (freePkts->empty()) {
42  px = 0;
43  } else {
44  px = freePkts->get(1); freePkts->removeFirst(); n++;
45  }
46 
47  pthread_mutex_unlock(&lock);
48  if (px == 0) {
49  cerr << "PacketStoreTs::alloc: no packets left to "
50  "allocate\n";
51  }
52  return px;
53 }
54 
59 void PacketStoreTs::free(pktx px) {
60  pthread_mutex_lock(&lock);
61 
62  if (px >= 1 && px <= N && !freePkts->member(px)) {
63  freePkts->addFirst(px); n--;
64  }
65 
66  pthread_mutex_unlock(&lock);
67 }
68 
73 pktx PacketStoreTs::fullCopy(pktx px) {
74  int px1 = alloc();
75  if (px1 == 0) return 0;
76  Packet& p = getPacket(px); Packet& p1 = getPacket(px1);
77  buffer_t* tmp = p1.buffer; p1 = p; p1.buffer = tmp;
78  int len = (p.length+3)/4;
79  buffer_t& buf = *p.buffer; buffer_t& buf1 = *p1.buffer;
80  for (int i = 0; i < len; i++) buf1[i] = buf[i];
81  return px1;
82 }
83 
84 } // ends namespace