From 8ade602be12ec43612c1c0a76147596ebc064c56 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Sat, 28 Jul 2018 03:42:00 -0500 Subject: [PATCH] use unrestricted union instead of struct --- common.h | 2 +- connection.cpp | 6 ------ connection.h | 27 +++++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/common.h b/common.h index 7cc8fad..0b7f662 100644 --- a/common.h +++ b/common.h @@ -48,7 +48,7 @@ #else -#include +//#include #include #include #include diff --git a/connection.cpp b/connection.cpp index fd79f60..ba5a41f 100644 --- a/connection.cpp +++ b/connection.cpp @@ -131,12 +131,6 @@ conn_manager_t conn_manager; { assert(0==1); //mylog(log_error,"called!!!!!!!!!!!!!\n"); - *this=b; - if(blob!=0) - { - blob=new blob_t(*b.blob); - - } } conn_info_t& conn_info_t::operator=(const conn_info_t& b) diff --git a/connection.h b/connection.h index 0b21f6d..c8704aa 100644 --- a/connection.h +++ b/connection.h @@ -219,12 +219,35 @@ struct conv_manager_t // manage the udp connections struct blob_t:not_copy_able_t //used in conn_info_t. { - struct //conv_manager_t is here to avoid copying when a connection is recovered - //TODO maybe an unconstrained union is better, but struct is okay since conv_manger is small when no data is filled in. + union tmp_union_t//conv_manager_t is here to avoid copying when a connection is recovered { conv_manager_t c; conv_manager_t s; //avoid templates here and there, avoid pointer and type cast + tmp_union_t() + { + if(program_mode==client_mode) + { + new( &c ) conv_manager_t(); + } + else + { + assert(program_mode==server_mode); + new( &s ) conv_manager_t(); + } + } + ~tmp_union_t() + { + if(program_mode==client_mode) + { + c.~conv_manager_t(); + } + else + { + assert(program_mode==server_mode); + s.~conv_manager_t(); + } + } }conv_manager; anti_replay_t anti_replay;//anti_replay_t is here bc its huge,its allocation is delayed.