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
1
trunk/3rdparty/st-srs/README.md
vendored
1
trunk/3rdparty/st-srs/README.md
vendored
|
@ -119,6 +119,7 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) was patched an
|
|||
- [x] Check capability for backtrack.
|
||||
- [x] Support set specifics for any thread.
|
||||
- [x] Support st_destroy to free resources for asan.
|
||||
- [x] Support free the stack, [#38](https://github.com/ossrs/state-threads/issues/38).
|
||||
- [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12).
|
||||
|
||||
## GDB Tools
|
||||
|
|
6
trunk/3rdparty/st-srs/stk.c
vendored
6
trunk/3rdparty/st-srs/stk.c
vendored
|
@ -65,6 +65,7 @@ _st_stack_t *_st_stack_new(int stack_size)
|
|||
_st_stack_t *ts;
|
||||
int extra;
|
||||
|
||||
/* If cache stack, we try to use stack from the cache list. */
|
||||
#ifdef MD_CACHE_STACK
|
||||
for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) {
|
||||
ts = _ST_THREAD_STACK_PTR(qp);
|
||||
|
@ -80,10 +81,13 @@ _st_stack_t *_st_stack_new(int stack_size)
|
|||
#endif
|
||||
|
||||
extra = _st_randomize_stacks ? _ST_PAGE_SIZE : 0;
|
||||
/* If not cache stack, we will free all stack in the list, which contains the stack to be freed.
|
||||
* Note that we should never directly free it at _st_stack_free, because it is still be used,
|
||||
* and will cause crash. */
|
||||
#ifndef MD_CACHE_STACK
|
||||
for (qp = _st_free_stacks.next; qp != &_st_free_stacks;) {
|
||||
ts = _ST_THREAD_STACK_PTR(qp);
|
||||
// Before qp is freed, move to next one, because the qp will be freed when free the ts.
|
||||
/* Before qp is freed, move to next one, because the qp will be freed when free the ts. */
|
||||
qp = qp->next;
|
||||
|
||||
ST_REMOVE_LINK(&ts->links);
|
||||
|
|
2
trunk/3rdparty/st-srs/utest/st_utest.hpp
vendored
2
trunk/3rdparty/st-srs/utest/st_utest.hpp
vendored
|
@ -44,7 +44,7 @@ struct ErrorObject {
|
|||
};
|
||||
extern std::ostream& operator<<(std::ostream& out, const ErrorObject* err);
|
||||
#define ST_ASSERT_ERROR(error, r0, message) if (error) return new ErrorObject(r0, message)
|
||||
#define ST_COROUTINE_JOIN(trd, r0) ErrorObject* r0 = NULL; SrsAutoFree(ErrorObject, r0); if (trd) st_thread_join(trd, (void**)&r0)
|
||||
#define ST_COROUTINE_JOIN(trd, r0) ErrorObject* r0 = NULL; if (trd) st_thread_join(trd, (void**)&r0); SrsUniquePtr<ErrorObject> r0_uptr(r0)
|
||||
#define ST_EXPECT_SUCCESS(r0) EXPECT_TRUE(!r0) << r0
|
||||
#define ST_EXPECT_FAILED(r0) EXPECT_TRUE(r0) << r0
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue