mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 (#4109)
To manage an object: ```cpp // Before MyClass* ptr = new MyClass(); SrsAutoFree(MyClass, ptr); ptr->do_something(); // Now SrsUniquePtr<MyClass> ptr(new MyClass()); ptr->do_something(); ``` To manage an array of objects: ```cpp // Before char* ptr = new char[10]; SrsAutoFreeA(char, ptr); ptr[0] = 0xf; // Now SrsUniquePtr<char[]> ptr(new char[10]); ptr[0] = 0xf; ``` In fact, SrsUniquePtr is a limited subset of SrsAutoFree, mainly managing pointers and arrays. SrsUniquePtr is better than SrsAutoFree because it has the same API to standard unique ptr. ```cpp SrsUniquePtr<MyClass> ptr(new MyClass()); ptr->do_something(); MyClass* p = ptr.get(); ``` SrsAutoFree actually uses a pointer to a pointer, so it can be set to NULL, allowing the pointer's value to be changed later (this usage is different from SrsUniquePtr). ```cpp // OK to free ptr correctly. MyClass* ptr; SrsAutoFree(MyClass, ptr); ptr = new MyClass(); // Crash because ptr is an invalid pointer. MyClass* ptr; SrsUniquePtr<MyClass> ptr(ptr); ptr = new MyClass(); ``` Additionally, SrsAutoFreeH can use specific release functions, which SrsUniquePtr does not support. --------- Co-authored-by: Jacob Su <suzp1984@gmail.com>
This commit is contained in:
parent
baf22d01c1
commit
23d2602c34
72 changed files with 1720 additions and 1669 deletions
|
@ -584,10 +584,9 @@ void SrsFlvTransmuxer::cache_metadata(char type, char* data, int size, char* cac
|
|||
(char)0x00, // TimestampExtended UI8
|
||||
(char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
|
||||
};*/
|
||||
|
||||
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
|
||||
SrsAutoFree(SrsBuffer, tag_stream);
|
||||
|
||||
|
||||
SrsUniquePtr<SrsBuffer> tag_stream(new SrsBuffer(cache, 11));
|
||||
|
||||
// write data size.
|
||||
tag_stream->write_1bytes(type);
|
||||
tag_stream->write_3bytes(size);
|
||||
|
@ -610,10 +609,9 @@ void SrsFlvTransmuxer::cache_audio(int64_t timestamp, char* data, int size, char
|
|||
(char)0x00, // TimestampExtended UI8
|
||||
(char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
|
||||
};*/
|
||||
|
||||
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
|
||||
SrsAutoFree(SrsBuffer, tag_stream);
|
||||
|
||||
|
||||
SrsUniquePtr<SrsBuffer> tag_stream(new SrsBuffer(cache, 11));
|
||||
|
||||
// write data size.
|
||||
tag_stream->write_1bytes(SrsFrameTypeAudio);
|
||||
tag_stream->write_3bytes(size);
|
||||
|
@ -637,10 +635,9 @@ void SrsFlvTransmuxer::cache_video(int64_t timestamp, char* data, int size, char
|
|||
(char)0x00, // TimestampExtended UI8
|
||||
(char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
|
||||
};*/
|
||||
|
||||
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
|
||||
SrsAutoFree(SrsBuffer, tag_stream);
|
||||
|
||||
|
||||
SrsUniquePtr<SrsBuffer> tag_stream(new SrsBuffer(cache, 11));
|
||||
|
||||
// write data size.
|
||||
tag_stream->write_1bytes(SrsFrameTypeVideo);
|
||||
tag_stream->write_3bytes(size);
|
||||
|
@ -652,8 +649,7 @@ void SrsFlvTransmuxer::cache_video(int64_t timestamp, char* data, int size, char
|
|||
|
||||
void SrsFlvTransmuxer::cache_pts(int size, char* cache)
|
||||
{
|
||||
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
|
||||
SrsAutoFree(SrsBuffer, tag_stream);
|
||||
SrsUniquePtr<SrsBuffer> tag_stream(new SrsBuffer(cache, 11));
|
||||
tag_stream->write_4bytes(size);
|
||||
}
|
||||
|
||||
|
@ -860,10 +856,9 @@ srs_error_t SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart
|
|||
if ((err = reader->read(tag_header, SRS_FLV_TAG_HEADER_SIZE, NULL)) != srs_success) {
|
||||
return srs_error_wrap(err, "read tag header");
|
||||
}
|
||||
|
||||
SrsBuffer* tag_stream = new SrsBuffer(tag_header, SRS_FLV_TAG_HEADER_SIZE);
|
||||
SrsAutoFree(SrsBuffer, tag_stream);
|
||||
|
||||
|
||||
SrsUniquePtr<SrsBuffer> tag_stream(new SrsBuffer(tag_header, SRS_FLV_TAG_HEADER_SIZE));
|
||||
|
||||
int8_t tag_type = tag_stream->read_1bytes();
|
||||
int32_t data_size = tag_stream->read_3bytes();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue