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
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue