1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine shared ptr message, rename initialize to create

This commit is contained in:
winlin 2014-07-06 18:23:14 +08:00
parent 891bc3fe65
commit 6a4b177475
7 changed files with 115 additions and 61 deletions

View file

@ -34,6 +34,10 @@ class SrsSharedPtrMessage;
/**
* the class to auto free the shared ptr message array.
* when need to get some messages, for instance, from Consumer queue,
* create a message array, whose msgs can used to accept the msgs,
* then send each message and set to NULL.
* @remark: when error, the message array will free the msg not sent out.
*/
class SrsSharedPtrMessageArray
{

View file

@ -1612,26 +1612,28 @@ SrsSharedPtrMessage::~SrsSharedPtrMessage()
}
}
int SrsSharedPtrMessage::initialize(SrsMessage* source)
int SrsSharedPtrMessage::create(SrsMessage* msg)
{
int ret = ERROR_SUCCESS;
if ((ret = initialize(&source->header, (char*)source->payload, source->size)) != ERROR_SUCCESS) {
if ((ret = create(&msg->header, (char*)msg->payload, msg->size)) != ERROR_SUCCESS) {
return ret;
}
// detach the payload from source
source->payload = NULL;
source->size = 0;
// to prevent double free of payload:
// initialize already attach the payload of msg,
// detach the payload to transfer the owner to shared ptr.
msg->payload = NULL;
msg->size = 0;
return ret;
}
int SrsSharedPtrMessage::initialize(SrsMessageHeader* source, char* payload, int size)
int SrsSharedPtrMessage::create(SrsMessageHeader* pheader, char* payload, int size)
{
int ret = ERROR_SUCCESS;
srs_assert(source != NULL);
srs_assert(pheader != NULL);
if (ptr) {
ret = ERROR_SYSTEM_ASSERT_FAILED;
srs_error("should not set the payload twice. ret=%d", ret);
@ -1640,28 +1642,31 @@ int SrsSharedPtrMessage::initialize(SrsMessageHeader* source, char* payload, int
return ret;
}
header = *source;
header = *pheader;
header.payload_length = size;
ptr = new __SrsSharedPtr();
// direct attach the data of common message.
// direct attach the data.
ptr->payload = payload;
ptr->size = size;
// message can access it.
SrsMessage::payload = (int8_t*)ptr->payload;
SrsMessage::size = ptr->size;
return ret;
}
int SrsSharedPtrMessage::count()
{
srs_assert(ptr);
return ptr->shared_count;
}
SrsSharedPtrMessage* SrsSharedPtrMessage::copy()
{
if (!ptr) {
srs_error("invoke initialize to initialize the ptr.");
srs_assert(false);
return NULL;
}
srs_assert(ptr);
SrsSharedPtrMessage* copy = new SrsSharedPtrMessage();

View file

@ -370,6 +370,13 @@ public:
* shared ptr message.
* for audio/video/data message that need less memory copy.
* and only for output.
*
* create first object by constructor and create(),
* use copy if need reference count message.
*
* Usage:
* SrsSharedPtrMessage msg;
*
*/
class SrsSharedPtrMessage : public SrsMessage
{
@ -390,19 +397,30 @@ public:
virtual ~SrsSharedPtrMessage();
public:
/**
* set the shared payload.
* we will detach the payload of source,
* so ensure donot use it before.
* create shared ptr message,
* copy header, manage the payload of msg,
* set the payload to NULL to prevent double free.
* @remark payload of msg set to NULL if success.
*/
virtual int initialize(SrsMessage* source);
virtual int create(SrsMessage* msg);
/**
* set the shared payload.
* use source header, and specified param payload.
* create shared ptr message,
* from the header and payload.
* @remark user should never free the payload.
*/
virtual int initialize(SrsMessageHeader* source, char* payload, int size);
virtual int create(SrsMessageHeader* pheader, char* payload, int size);
/**
* get current reference count.
* when this object created, count set to 0.
* if copy() this object, count increase 1.
* if this or copy deleted, free payload when count is 0, or count--.
* @remark, assert object is created.
*/
virtual int count();
public:
/**
* copy current shared ptr message, use ref-count.
* @remark, assert object is created.
*/
virtual SrsSharedPtrMessage* copy();
};