mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Refine object cache.
This commit is contained in:
parent
2ec03bf56a
commit
edbabf840d
3 changed files with 36 additions and 33 deletions
|
@ -313,7 +313,7 @@ public:
|
|||
virtual ~SrsSharedPtrMessage();
|
||||
public:
|
||||
// For object cache to reset and reuse it.
|
||||
bool reset() { return true; }
|
||||
bool recycle() { return true; }
|
||||
// Create shared ptr message,
|
||||
// copy header, manage the payload of msg,
|
||||
// set the payload to NULL to prevent double free.
|
||||
|
|
|
@ -811,21 +811,25 @@ ISrsRtpPacketDecodeHandler::~ISrsRtpPacketDecodeHandler()
|
|||
|
||||
SrsRtpPacket2::SrsRtpPacket2()
|
||||
{
|
||||
payload_ = NULL; payload_type_ = SrsRtpPacketPayloadTypeUnknown;
|
||||
payload_ = NULL;
|
||||
payload_type_ = SrsRtpPacketPayloadTypeUnknown;
|
||||
shared_msg = NULL;
|
||||
|
||||
reset();
|
||||
nalu_type = SrsAvcNaluTypeReserved;
|
||||
frame_type = SrsFrameTypeReserved;
|
||||
cached_payload_size = 0;
|
||||
decode_handler = NULL;
|
||||
|
||||
++_srs_pps_objs_rtps->sugar;
|
||||
}
|
||||
|
||||
SrsRtpPacket2::~SrsRtpPacket2()
|
||||
{
|
||||
reuse_payload();
|
||||
reuse_shared_msg();
|
||||
recycle_payload();
|
||||
recycle_shared_msg();
|
||||
}
|
||||
|
||||
void SrsRtpPacket2::reuse_payload()
|
||||
void SrsRtpPacket2::recycle_payload()
|
||||
{
|
||||
if (!payload_) {
|
||||
return;
|
||||
|
@ -848,7 +852,7 @@ void SrsRtpPacket2::reuse_payload()
|
|||
payload_ = NULL; payload_type_ = SrsRtpPacketPayloadTypeUnknown;
|
||||
}
|
||||
|
||||
void SrsRtpPacket2::reuse_shared_msg()
|
||||
void SrsRtpPacket2::recycle_shared_msg()
|
||||
{
|
||||
if (!shared_msg) {
|
||||
return;
|
||||
|
@ -870,7 +874,7 @@ void SrsRtpPacket2::reuse_shared_msg()
|
|||
}
|
||||
}
|
||||
|
||||
bool SrsRtpPacket2::reset()
|
||||
bool SrsRtpPacket2::recycle()
|
||||
{
|
||||
nalu_type = SrsAvcNaluTypeReserved;
|
||||
frame_type = SrsFrameTypeReserved;
|
||||
|
@ -878,8 +882,9 @@ bool SrsRtpPacket2::reset()
|
|||
decode_handler = NULL;
|
||||
|
||||
header.reset();
|
||||
reuse_payload();
|
||||
reuse_shared_msg();
|
||||
|
||||
recycle_payload();
|
||||
recycle_shared_msg();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -929,7 +934,7 @@ char* SrsRtpPacket2::wrap(char* data, int size)
|
|||
char* SrsRtpPacket2::wrap(SrsSharedPtrMessage* msg)
|
||||
{
|
||||
// Recycle the shared message.
|
||||
reuse_shared_msg();
|
||||
recycle_shared_msg();
|
||||
|
||||
// Copy from the new message.
|
||||
shared_msg = msg->copy();
|
||||
|
@ -968,8 +973,8 @@ SrsRtpPacket2* SrsRtpPacket2::copy()
|
|||
SrsRtpPacket2* cp = _srs_rtp_cache->allocate();
|
||||
|
||||
// We got packet from cache, so we must recycle it.
|
||||
cp->reuse_payload();
|
||||
cp->reuse_shared_msg();
|
||||
cp->recycle_payload();
|
||||
cp->recycle_shared_msg();
|
||||
|
||||
cp->header = header;
|
||||
cp->payload_ = payload_? payload_->copy():NULL;
|
||||
|
|
|
@ -318,11 +318,11 @@ public:
|
|||
SrsRtpPacket2();
|
||||
virtual ~SrsRtpPacket2();
|
||||
private:
|
||||
void reuse_payload();
|
||||
void reuse_shared_msg();
|
||||
void recycle_payload();
|
||||
void recycle_shared_msg();
|
||||
public:
|
||||
// Recycle the object to reuse it.
|
||||
virtual bool reset();
|
||||
virtual bool recycle();
|
||||
// Wrap buffer to shared_message, which is managed by us.
|
||||
char* wrap(int size);
|
||||
char* wrap(char* data, int size);
|
||||
|
@ -414,7 +414,6 @@ public:
|
|||
// Try to allocate from cache, create new object if no cache.
|
||||
// SrsRtpObjectCacheManager::allocate
|
||||
T* allocate() {
|
||||
while (true) {
|
||||
if (!enabled_ || cache_objs_.empty()) {
|
||||
return new T();
|
||||
}
|
||||
|
@ -422,15 +421,8 @@ public:
|
|||
T* obj = cache_objs_.back();
|
||||
cache_objs_.pop_back();
|
||||
|
||||
// If reset the object fail, drop the cached object.
|
||||
if (!obj->reset()) {
|
||||
srs_freep(obj);
|
||||
continue;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
// Recycle the object to cache.
|
||||
// @remark User can directly free the packet.
|
||||
// SrsRtpObjectCacheManager::recycle
|
||||
|
@ -446,6 +438,12 @@ public:
|
|||
return;
|
||||
}
|
||||
|
||||
// If recycle the object fail, drop the cached object.
|
||||
if (!p->recycle()) {
|
||||
srs_freep(p);
|
||||
return;
|
||||
}
|
||||
|
||||
// If exceed the capacity, drop the object.
|
||||
if (cache_objs_.size() > capacity_) {
|
||||
++_srs_pps_objs_drop->sugar;
|
||||
|
@ -471,7 +469,7 @@ public:
|
|||
SrsRtpRawPayload();
|
||||
virtual ~SrsRtpRawPayload();
|
||||
public:
|
||||
bool reset() { return true; }
|
||||
bool recycle() { return true; }
|
||||
// interface ISrsRtpPayloader
|
||||
public:
|
||||
virtual uint64_t nb_bytes();
|
||||
|
@ -571,7 +569,7 @@ public:
|
|||
SrsRtpFUAPayload2();
|
||||
virtual ~SrsRtpFUAPayload2();
|
||||
public:
|
||||
bool reset() { return true; }
|
||||
bool recycle() { return true; }
|
||||
// interface ISrsRtpPayloader
|
||||
public:
|
||||
virtual uint64_t nb_bytes();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue