Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "Messages.h"
#include "NetworkInterfaceBase.h"
#include "KBDebug.h"
#include "ObjectPool.h"

static ObjectPool<Bundle> _g_bundlePool;

Bundle::Bundle():
pCurrPacket_(NULL),
Expand All @@ -18,18 +21,29 @@ Bundle::Bundle():

Bundle::~Bundle()
{
MemoryStream::reclaimObject(pCurrPacket_);
pCurrPacket_ = NULL;
if (pCurrPacket_)
{
delete pCurrPacket_;
pCurrPacket_ = NULL;
}

for (int i = 0; i < streams_.Num(); ++i)
{
delete streams_[i];
}

streams_.Empty();
}

Bundle* Bundle::createObject()
{
return new Bundle();
return _g_bundlePool.createObject();
}

void Bundle::reclaimObject(Bundle* obj)
{
delete obj;
obj->clear();
_g_bundlePool.reclaimObject(obj);
}

void Bundle::newMessage(Message* pMsg)
Expand Down Expand Up @@ -87,17 +101,6 @@ void Bundle::send(NetworkInterfaceBase* pNetworkInterface)
ERROR_MSG("Bundle::send(): networkInterface invalid!");
}

// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
for (int i = 0; i < streams_.Num(); ++i)
{
MemoryStream::reclaimObject(streams_[i]);
}

streams_.Empty();

if(pCurrPacket_)
pCurrPacket_->clear(true);

// 我们认为,发送完成,就视为这个bundle不再使用了,
// 所以我们会把它放回对象池,以减少垃圾回收带来的消耗,
// 如果需要继续使用,应该重新Bundle.createObject(),
Expand Down Expand Up @@ -135,6 +138,28 @@ void Bundle::checkStream(uint32 v)
messageLength_ += v;
}

void Bundle::clear()
{
// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
for (int i = 0; i < streams_.Num(); ++i)
{
if(pCurrPacket_ != streams_[i])
MemoryStream::reclaimObject(streams_[i]);
}

streams_.Empty();

if (pCurrPacket_)
pCurrPacket_->clear(false);
else
pCurrPacket_ = MemoryStream::createObject();

numMessage_ = 0;
messageLength_ = 0;
pMsgtype_ = NULL;
curMsgStreamIndex_ = 0;
}

Bundle &Bundle::operator<<(uint8 value)
{
checkStream(sizeof(uint8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class KBENGINEPLUGINS_API Bundle

void checkStream(uint32 v);

void clear();

public:
Bundle &operator<<(uint8 value);
Bundle &operator<<(uint16 value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@

#include "MemoryStream.h"
#include "KBDebug.h"
#include "ObjectPool.h"

static ObjectPool<MemoryStream> _g_memoryStreamPool;

MemoryStream* MemoryStream::createObject()
{
return new MemoryStream();
return _g_memoryStreamPool.createObject();
}

void MemoryStream::reclaimObject(MemoryStream* obj)
{
delete obj;
obj->clear(false);
_g_memoryStreamPool.reclaimObject(obj);
}

void MemoryStream::print_storage()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

template<typename T>
class ObjectPool
{
public:
~ObjectPool()
{
while (objects_.Num() > 0)
{
TDoubleLinkedList< T* >::TDoubleLinkedListNode* node = objects_.GetHead();
T* t = node->GetValue();
delete t;
objects_.RemoveNode(node);
}
}

T* createObject()
{
if (objects_.Num() > 0)
{
TDoubleLinkedList< T* >::TDoubleLinkedListNode* node = objects_.GetHead();
T* t = node->GetValue();
objects_.RemoveNode(node);
return t;
}
else
{
T* t = new T();
return t;
}
}

void reclaimObject(T* obj)
{
objects_.AddTail(obj);
}

private:
TDoubleLinkedList< T* > objects_;
};