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