Skip to content

Commit

Permalink
Refine code.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jul 8, 2024
1 parent b51a19c commit 96ad539
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 82 deletions.
2 changes: 1 addition & 1 deletion trunk/configure
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
#
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_rtc_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <srs_app_log.hpp>
#include <srs_app_threads.hpp>
#include <srs_app_statistic.hpp>
#include <srs_core_deprecated.hpp>

#ifdef SRS_FFMPEG_FIT
#include <srs_app_rtc_codec.hpp>
Expand Down
81 changes: 0 additions & 81 deletions trunk/src/core/srs_core_autofree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,6 @@

#include <srs_core.hpp>

#include <stdlib.h>

// 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;
}
};

// Unique ptr smart pointer, only support unique ptr, with limited APIs and features,
// see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107
//
Expand Down
8 changes: 8 additions & 0 deletions trunk/src/core/srs_core_deprecated.cpp
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>

95 changes: 95 additions & 0 deletions trunk/src/core/srs_core_deprecated.hpp
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>

#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
1 change: 1 addition & 0 deletions trunk/src/kernel/srs_kernel_mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <srs_kernel_io.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_kernel_buffer.hpp>
#include <srs_core_deprecated.hpp>

#include <string.h>
#include <sstream>
Expand Down
1 change: 1 addition & 0 deletions trunk/src/kernel/srs_kernel_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_kernel_buffer.hpp>
#include <srs_kernel_flv.hpp>
#include <srs_core_deprecated.hpp>

// this value must:
// equals to (SRS_SYS_CYCLE_INTERVAL*SRS_SYS_TIME_RESOLUTION_MS_TIMES)*1000
Expand Down
1 change: 1 addition & 0 deletions trunk/src/protocol/srs_protocol_srt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
#include <srs_core_autofree.hpp>
#include <srs_core_deprecated.hpp>

#include <srt/srt.h>

Expand Down
1 change: 1 addition & 0 deletions trunk/src/protocol/srs_protocol_st.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_protocol_utility.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_core_deprecated.hpp>

// nginx also set to 512
#define SERVER_LISTEN_BACKLOG 512
Expand Down
1 change: 1 addition & 0 deletions trunk/src/utest/srs_utest_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using namespace std;
#include <srs_core_autofree.hpp>
#include <srs_protocol_conn.hpp>
#include <srs_app_conn.hpp>
#include <srs_core_deprecated.hpp>

VOID TEST(CoreAutoFreeTest, Free)
{
Expand Down
1 change: 1 addition & 0 deletions trunk/src/utest/srs_utest_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using namespace std;
#include <srs_protocol_http_conn.hpp>
#include <srs_protocol_protobuf.hpp>
#include <srs_kernel_buffer.hpp>
#include <srs_core_deprecated.hpp>

MockEmptyIO::MockEmptyIO()
{
Expand Down
1 change: 1 addition & 0 deletions trunk/src/utest/srs_utest_rtmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <srs_kernel_buffer.hpp>
#include <srs_kernel_codec.hpp>
#include <srs_protocol_utility.hpp>
#include <srs_core_deprecated.hpp>

#define SRS_DEFAULT_RECV_BUFFER_SIZE 131072

Expand Down
1 change: 1 addition & 0 deletions trunk/src/utest/srs_utest_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using namespace std;
#include <srs_app_listener.hpp>
#include <srs_protocol_st.hpp>
#include <srs_protocol_utility.hpp>
#include <srs_core_deprecated.hpp>

#include <srs_protocol_st.hpp>
#include <srs_protocol_http_conn.hpp>
Expand Down

0 comments on commit 96ad539

Please sign in to comment.