diff --git a/trunk/configure b/trunk/configure index 355d484329..10cea9f86d 100755 --- a/trunk/configure +++ b/trunk/configure @@ -269,7 +269,7 @@ MODULE_ID="CORE" MODULE_DEPENDS=() ModuleLibIncs=(${SRS_OBJS}) MODULE_FILES=("srs_core" "srs_core_version" "srs_core_version5" "srs_core_autofree" "srs_core_performance" - "srs_core_time" "srs_core_platform") + "srs_core_time" "srs_core_platform" "srs_core_deprecated") CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . $SRS_WORKDIR/auto/modules.sh CORE_OBJS="${MODULE_OBJS[@]}" # diff --git a/trunk/src/app/srs_app_rtc_source.cpp b/trunk/src/app/srs_app_rtc_source.cpp index 4dfce8218d..8aebf5d530 100644 --- a/trunk/src/app/srs_app_rtc_source.cpp +++ b/trunk/src/app/srs_app_rtc_source.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef SRS_FFMPEG_FIT #include diff --git a/trunk/src/core/srs_core_autofree.hpp b/trunk/src/core/srs_core_autofree.hpp index 1f020ee9d0..b0a4231067 100644 --- a/trunk/src/core/srs_core_autofree.hpp +++ b/trunk/src/core/srs_core_autofree.hpp @@ -9,87 +9,6 @@ #include -#include - -// 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 _auto_free_##instance(&instance, false, false, NULL) -// To delete array. -#define SrsAutoFreeA(className, instance) \ - impl_SrsAutoFree _auto_free_array_##instance(&instance, true, false, NULL) -// Use free instead of delete. -#define SrsAutoFreeF(className, instance) \ - impl_SrsAutoFree _auto_free_##instance(&instance, false, true, NULL) -// Use hook instead of delete. -#define SrsAutoFreeH(className, instance, hook) \ - impl_SrsAutoFree _auto_free_##instance(&instance, false, false, hook) -// The template implementation. -template -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; - } -}; - // Unique ptr smart pointer, only support unique ptr, with limited APIs and features, // see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107 // diff --git a/trunk/src/core/srs_core_deprecated.cpp b/trunk/src/core/srs_core_deprecated.cpp new file mode 100644 index 0000000000..0560bcaf56 --- /dev/null +++ b/trunk/src/core/srs_core_deprecated.cpp @@ -0,0 +1,8 @@ +// +// Copyright (c) 2013-2024 The SRS Authors +// +// SPDX-License-Identifier: MIT +// + +#include + diff --git a/trunk/src/core/srs_core_deprecated.hpp b/trunk/src/core/srs_core_deprecated.hpp new file mode 100644 index 0000000000..dfc7e43b3e --- /dev/null +++ b/trunk/src/core/srs_core_deprecated.hpp @@ -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 + +#include + +// 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 _auto_free_##instance(&instance, false, false, NULL) +// To delete array. +#define SrsAutoFreeA(className, instance) \ + impl_SrsAutoFree _auto_free_array_##instance(&instance, true, false, NULL) +// Use free instead of delete. +#define SrsAutoFreeF(className, instance) \ + impl_SrsAutoFree _auto_free_##instance(&instance, false, true, NULL) +// Use hook instead of delete. +#define SrsAutoFreeH(className, instance, hook) \ + impl_SrsAutoFree _auto_free_##instance(&instance, false, false, hook) +// The template implementation. +template +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 diff --git a/trunk/src/kernel/srs_kernel_mp4.cpp b/trunk/src/kernel/srs_kernel_mp4.cpp index 12ecb5ca2c..b4d597f7ee 100644 --- a/trunk/src/kernel/srs_kernel_mp4.cpp +++ b/trunk/src/kernel/srs_kernel_mp4.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 356515bdd8..71ffd6d252 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -28,6 +28,7 @@ using namespace std; #include #include #include +#include // this value must: // equals to (SRS_SYS_CYCLE_INTERVAL*SRS_SYS_TIME_RESOLUTION_MS_TIMES)*1000 diff --git a/trunk/src/protocol/srs_protocol_srt.cpp b/trunk/src/protocol/srs_protocol_srt.cpp index efad018e85..3f494dddf9 100644 --- a/trunk/src/protocol/srs_protocol_srt.cpp +++ b/trunk/src/protocol/srs_protocol_srt.cpp @@ -13,6 +13,7 @@ using namespace std; #include #include #include +#include #include diff --git a/trunk/src/protocol/srs_protocol_st.cpp b/trunk/src/protocol/srs_protocol_st.cpp index e6127691f6..f15f7252c8 100644 --- a/trunk/src/protocol/srs_protocol_st.cpp +++ b/trunk/src/protocol/srs_protocol_st.cpp @@ -17,6 +17,7 @@ using namespace std; #include #include #include +#include // nginx also set to 512 #define SERVER_LISTEN_BACKLOG 512 diff --git a/trunk/src/utest/srs_utest_core.cpp b/trunk/src/utest/srs_utest_core.cpp index 85c0946559..8d2e54ba6e 100644 --- a/trunk/src/utest/srs_utest_core.cpp +++ b/trunk/src/utest/srs_utest_core.cpp @@ -10,6 +10,7 @@ using namespace std; #include #include #include +#include VOID TEST(CoreAutoFreeTest, Free) { diff --git a/trunk/src/utest/srs_utest_protocol.cpp b/trunk/src/utest/srs_utest_protocol.cpp index 57d9ffe44c..02ad943249 100644 --- a/trunk/src/utest/srs_utest_protocol.cpp +++ b/trunk/src/utest/srs_utest_protocol.cpp @@ -19,6 +19,7 @@ using namespace std; #include #include #include +#include MockEmptyIO::MockEmptyIO() { diff --git a/trunk/src/utest/srs_utest_rtmp.cpp b/trunk/src/utest/srs_utest_rtmp.cpp index aa9412c9d6..86fbcb2731 100644 --- a/trunk/src/utest/srs_utest_rtmp.cpp +++ b/trunk/src/utest/srs_utest_rtmp.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #define SRS_DEFAULT_RECV_BUFFER_SIZE 131072 diff --git a/trunk/src/utest/srs_utest_service.cpp b/trunk/src/utest/srs_utest_service.cpp index 7eeeca40cf..06ad78c5a2 100644 --- a/trunk/src/utest/srs_utest_service.cpp +++ b/trunk/src/utest/srs_utest_service.cpp @@ -11,6 +11,7 @@ using namespace std; #include #include #include +#include #include #include