20#include "packetpool.h"
21#include "packetpool_p.h"
32PacketPoolPrivate::PacketPoolPrivate(
int _packetSize,
int _poolSize)
33 : freePackets(new PacketPrivate *[_poolSize]),
34 packetMemory(new char[_packetSize * _poolSize]),
35 readPosition(0), writePosition(0),
36 ringBufferSize(_poolSize),
37 packetSize(_packetSize),
40 for (
int i = 0; i < _poolSize; ++i) {
41 freePackets[i] =
new PacketPrivate(&packetMemory[i *
packetSize],
this);
45PacketPoolPrivate::~PacketPoolPrivate()
47 Q_ASSERT(poolSize == ringBufferSize.loadAcquire());
48 for (
int i = 0; i < poolSize; ++i) {
49 delete freePackets[i];
52 delete[] packetMemory;
55void PacketPoolPrivate::releasePacket(
const Packet &packet)
57 const int _writePos = writePosition.fetchAndAddAcquire(1);
59 while (pos >= poolSize) {
62 writePosition.testAndSetRelease(_writePos, pos);
63 freePackets[pos] = packet.d_ptr;
67Packet PacketPoolPrivate::acquirePacket()
69 const int s = ringBufferSize.fetchAndAddRelaxed(-1);
71 ringBufferSize.fetchAndAddRelaxed(1);
74 const int _readPos = readPosition.fetchAndAddRelaxed(1);
76 while (pos >= poolSize) {
79 readPosition.testAndSetRelease(_readPos, pos);
80 freePackets[pos]->m_size = 0;
81 return Packet(*freePackets[pos]);
85 : d_ptr(new PacketPoolPrivate(packetSize, _poolSize))
98 if (d_ptr != rhs.d_ptr) {
99 if (!d_ptr->ref.deref()) {
110 if (!d_ptr->ref.deref()) {
Class to preallocate memory.
int poolSize() const
Returns the number of packets that was requested in the constructor.
int packetSize() const
Returns the packet size that was set in the constructor.
int unusedPackets() const
Returns the number of packets that are still available for use.
~PacketPool()
Destructs this object.
PacketPool(int packetSize, int numberOfPackets)
Allocates numberOfPackets packets of packetSize bytes each.
PacketPool & operator=(const PacketPool &)
Assignmen operator.