Skip to content
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

[perf] update safeMallocTemplateCObject #418

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphMessage/GMessageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GMessageManager : public GMessageObject,
? CStatus() : CErrStatus("create topic [" + topic + "] duplicate");
} else {
// 创建一个 topic信息
auto message = UAllocator::safeMallocTemplateCObject<GMessage<TImpl>, CUInt>(size);
auto message = UAllocator::safeMallocTemplateCObject<GMessage<TImpl>>(size);
send_recv_message_map_.insert(std::pair<const std::string&, GMessagePtr<T> >(innerTopic, GMessagePtr<T>(message)));
}

Expand Down
21 changes: 11 additions & 10 deletions src/UtilsCtrl/UAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef CGRAPH_UALLOCATOR_H
#define CGRAPH_UALLOCATOR_H

#include <new>
#include <mutex>
#include <memory>

Expand All @@ -24,7 +25,7 @@ class UAllocator : public CObject {
/**
* 生成一个 CObject 对象
* @tparam T
* @return
* @return T*
*/
template<typename T,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
Expand All @@ -35,7 +36,7 @@ class UAllocator : public CObject {
/**
* 生成一个 CStruct 的对象
* @tparam T
* @return
* @return T*
*/
template<typename T,
c_enable_if_t<std::is_base_of<CStruct, T>::value, int> = 0>
Expand All @@ -48,22 +49,22 @@ class UAllocator : public CObject {
* @tparam T
* @tparam Args
* @param args
* @return
* @return T*
*/
template<typename T, typename ...Args,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static T* safeMallocTemplateCObject(Args... args) {
T* ptr = nullptr;
while (!ptr) {
ptr = new T(std::forward<Args>(args)...);
static T* safeMallocTemplateCObject(Args&&... args) {
T* result{};
while (!result) {
result = new(std::nothrow) T(std::forward<Args&&>(args)...);
}
return ptr;
return result;
}

/**
* 生成unique智能指针信息
* @tparam T
* @return
* @return std::unique_ptr<T>
*/
template<typename T,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
Expand All @@ -75,7 +76,7 @@ class UAllocator : public CObject {
/**
* 生成T类型的对象
* @tparam T
* @return
* @return T*
*/
template<class T>
static T* safeMalloc() {
Expand Down
Loading