change:更改内存删除分配方式。

This commit is contained in:
taynpg 2025-04-09 17:29:57 +08:00
parent 99607c6721
commit 61ac79c93e
4 changed files with 22 additions and 21 deletions

View File

@ -123,15 +123,6 @@ void CClient::print_help(bool detail)
TLOGI("{}", sp);
}
void CClient::free_buf_manual(CFrameBuffer* buf)
{
if (buf == nullptr) {
return;
}
delete buf->data_;
buf->data_ = nullptr;
}
void CClient::run(const std::string& ip, const std::string& port, const std::string& config_dir)
{
fs::path fp(config_dir);
@ -1099,8 +1090,6 @@ void CClient::handle_frame(CFrameBuffer* buf)
buf->type_ = TYPE_GET_DIRFILES_DONE;
msg_info.str = out;
}
delete[] buf->data_;
buf->data_ = nullptr;
serialize(msg_info, &buf->data_, buf->len_);
std::swap(buf->tid_, buf->fid_);
if (!send_frame(buf)) {
@ -1216,10 +1205,15 @@ void CClient::handle_frame(CFrameBuffer* buf)
TLOGE("{} GetList deserialize failed.", __LINE__);
break;
}
delete[] buf->data_;
buf->data_ = nullptr;
msg_info.str = variable_and_reverse_files(msg_info.str);
serialize(msg_info, &buf->data_, buf->len_);
CMessageInfo tp(buf->fid_);
if (!deserialize(buf->data_, buf->len_, tp)) {
TLOGE("{} GetList deserialize failed.", __LINE__);
break;
}
std::swap(buf->tid_, buf->fid_);
buf->type_ = TYPE_REQUEST_UPDATE_LIST;
if (!send_frame(buf)) {
@ -1229,12 +1223,12 @@ void CClient::handle_frame(CFrameBuffer* buf)
break;
};
case TYPE_REQUEST_UPDATE_LIST: {
CMessageInfo msg_info(buf->fid_);
std::map<std::string, std::string> files;
if (down_ && down_->trans_state_ == TRANS_REDAY) {
TLOGW("Update Busy......, Ignore {}", buf->fid_);
buf->type_ = TYPE_BUSY_UPDATE_LIST;
} else {
CMessageInfo msg_info(buf->fid_);
if (!deserialize(buf->data_, buf->len_, msg_info)) {
TLOGE("{} GetList deserialize failed.", __LINE__);
break;
@ -1246,6 +1240,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
buf->type_ = TYPE_UNCONFIRM_UPDATE_LIST;
}
}
serialize(msg_info, &buf->data_, buf->len_);
std::swap(buf->tid_, buf->fid_);
if (!send_frame(buf)) {
TLOGE("Send Failed {}.", __LINE__);
@ -1360,7 +1355,9 @@ void CClient::send_file_data_th(const char* keys)
return;
}
free_buf_manual(buf.get());
delete[] buf->data_;
buf->data_ = nullptr;
buf->type_ = TYPE_TRANS_FILE;
buf->mark_ = 1;
@ -1377,7 +1374,7 @@ void CClient::send_file_data_th(const char* keys)
cur_send_size = t->file_.gcount();
msg_info.data.resize(cur_send_size);
send_size += cur_send_size;
serialize(msg_info, &buf->data_, buf->len_);
serialize(msg_info, &buf->data_, buf->len_, true);
if (!send_frame(buf.get())) {
report_trans_ret(TRANS_FAILED, str_key);
TLOGE("Stop Trans {} To {} failed.", t->cur_file_, str_key);

View File

@ -71,7 +71,6 @@ private:
std::string read_uuid();
void get_id();
void print_help(bool detail);
void free_buf_manual(CFrameBuffer* buf);
private:
void handle_frame(CFrameBuffer* buf);

View File

@ -134,7 +134,7 @@ CFrameBuffer::~CFrameBuffer()
len_ = 0;
}
void serialize(CMessageInfo& msg_info, char** out_buf, int& len)
void serialize(CMessageInfo& msg_info, char** out_buf, int& len, bool reuse_mem)
{
auto& info = msg_info;
info.id = localtou8(info.id);
@ -149,9 +149,14 @@ void serialize(CMessageInfo& msg_info, char** out_buf, int& len)
// 调用方负责确保内存够用性(len <= 可用最大空间长度)和内存可用性。
// 即,如果调用方及高频率调用 serialize, 且每次 len <= 已分配空间就复用内存,完了再释放。
// 低频率或者 len 不固定时,每次都释放内存,并置 nullptr。
if (*out_buf == nullptr) {
if (*out_buf) {
if (!reuse_mem) {
delete[] *out_buf;
*out_buf = new char[len]; // 分配内存(调用方负责释放)
}
} else {
*out_buf = new char[len];
}
char* ptr = *out_buf + kz + 1;

View File

@ -53,7 +53,7 @@ struct CMessageInfo {
std::string data;
};
void serialize(CMessageInfo& msg_info, char** out_buf, int& len);
void serialize(CMessageInfo& msg_info, char** out_buf, int& len, bool reuse_mem = false);
bool deserialize(char* data, int len, CMessageInfo& msg_info);
std::string u8tolocal(const std::string& str);
std::string localtou8(const std::string& str);