-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 #4109
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0c84717
UniquePtr: Support SrsUniquePtr to replace SrsAutoFree.
winlinvip b51a19c
UniquePtr: Support array for unique ptr.
winlinvip 96ad539
Refine code.
winlinvip 5a36003
Refine code.
winlinvip 688aea5
Never create consumer if error.
winlinvip 922d067
Merge branch 'develop' into pr-feature/unique-ptr
winlinvip 86ca4be
Update release to v6.0.136
winlinvip 1c45ab6
Refine script.
winlinvip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// | ||
// Copyright (c) 2013-2024 The SRS Authors | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#include <srs_core_deprecated.hpp> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Copyright (c) 2013-2024 The SRS Authors | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#ifndef SRS_CORE_DEPRECATED_HPP | ||
#define SRS_CORE_DEPRECATED_HPP | ||
|
||
#include <srs_core.hpp> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this include header, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to make sure the first included file is this one. Won't fix. |
||
|
||
#include <stdlib.h> | ||
|
||
// Note that the SrsAutoFree is deprecated, please use SrsUniquePtr instead. | ||
// | ||
// Note: Please use SrsUniquePtr if possible. Please aware that there is a slight difference between SrsAutoFree | ||
// and SrsUniquePtr. SrsAutoFree will track the address of pointer, while SrsUniquePtr will not. | ||
// MyClass* p; | ||
// SrsAutoFree(MyClass, p); // p will be freed even p is changed later. | ||
// SrsUniquePtr ptr(p); // crash because p is an invalid pointer. | ||
// | ||
// The auto free helper, which is actually the unique ptr, without the move feature, | ||
// see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107 | ||
// | ||
// To free the instance in the current scope, for instance, MyClass* ptr, | ||
// which is a ptr and this class will: | ||
// 1. free the ptr. | ||
// 2. set ptr to NULL. | ||
// | ||
// Usage: | ||
// MyClass* po = new MyClass(); | ||
// // ...... use po | ||
// SrsAutoFree(MyClass, po); | ||
// | ||
// Usage for array: | ||
// MyClass** pa = new MyClass*[size]; | ||
// // ....... use pa | ||
// SrsAutoFreeA(MyClass*, pa); | ||
// | ||
// @remark the MyClass can be basic type, for instance, SrsAutoFreeA(char, pstr), | ||
// where the char* pstr = new char[size]. | ||
// To delete object. | ||
#define SrsAutoFree(className, instance) \ | ||
impl_SrsAutoFree<className> _auto_free_##instance(&instance, false, false, NULL) | ||
// To delete array. | ||
#define SrsAutoFreeA(className, instance) \ | ||
impl_SrsAutoFree<className> _auto_free_array_##instance(&instance, true, false, NULL) | ||
// Use free instead of delete. | ||
#define SrsAutoFreeF(className, instance) \ | ||
impl_SrsAutoFree<className> _auto_free_##instance(&instance, false, true, NULL) | ||
// Use hook instead of delete. | ||
#define SrsAutoFreeH(className, instance, hook) \ | ||
impl_SrsAutoFree<className> _auto_free_##instance(&instance, false, false, hook) | ||
// The template implementation. | ||
template<class T> | ||
class impl_SrsAutoFree | ||
{ | ||
private: | ||
T** ptr; | ||
bool is_array; | ||
bool _use_free; | ||
void (*_hook)(T*); | ||
public: | ||
// If use_free, use free(void*) to release the p. | ||
// If specified hook, use hook(p) to release it. | ||
// Use delete to release p, or delete[] if p is an array. | ||
impl_SrsAutoFree(T** p, bool array, bool use_free, void (*hook)(T*)) { | ||
ptr = p; | ||
is_array = array; | ||
_use_free = use_free; | ||
_hook = hook; | ||
} | ||
|
||
virtual ~impl_SrsAutoFree() { | ||
if (ptr == NULL || *ptr == NULL) { | ||
return; | ||
} | ||
|
||
if (_use_free) { | ||
free(*ptr); | ||
} else if (_hook) { | ||
_hook(*ptr); | ||
} else { | ||
if (is_array) { | ||
delete[] *ptr; | ||
} else { | ||
delete *ptr; | ||
} | ||
} | ||
|
||
*ptr = NULL; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know whether it's a code conventions or not, but an empty cpp, seems useless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will cause the utest fail. Please file another PR if you want to remove it.
Won't fix.