Skip to content

Commit 559cf7c

Browse files
committed
Simplify error handling doe and use typedefs to simplify code
1 parent bd3abfd commit 559cf7c

File tree

2 files changed

+50
-67
lines changed

2 files changed

+50
-67
lines changed

include/boost/interprocess/detail/segment_manager_helper.hpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,29 @@ struct block_header
149149

150150
size_type value_offset() const
151151
{
152-
return get_rounded_size(size_type(sizeof(block_header<size_type>)), size_type(m_value_alignment));
152+
return get_rounded_size(size_type(sizeof(block_header)), size_type(m_value_alignment));
153153
}
154154

155155
template<class CharType>
156-
bool less_comp(const block_header<size_type> &b) const
156+
bool less_comp(const block_header &b) const
157157
{
158158
return m_num_char < b.m_num_char ||
159159
(m_num_char < b.m_num_char &&
160160
std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) < 0);
161161
}
162162

163163
template<class CharType>
164-
bool equal_comp(const block_header<size_type> &b) const
164+
bool equal_comp(const block_header &b) const
165165
{
166166
return m_num_char == b.m_num_char &&
167167
std::char_traits<CharType>::compare(name<CharType>(), b.name<CharType>(), m_num_char) == 0;
168168
}
169169

170170
template<class T>
171-
static block_header<size_type> *block_header_from_value(T *value)
171+
static block_header *block_header_from_value(T *value)
172172
{ return block_header_from_value(value, sizeof(T), ::boost::container::dtl::alignment_of<T>::value); }
173173

174-
static block_header<size_type> *block_header_from_value(const void *value, std::size_t sz, std::size_t algn)
174+
static block_header *block_header_from_value(const void *value, std::size_t sz, std::size_t algn)
175175
{
176176
block_header * hdr =
177177
const_cast<block_header*>
@@ -185,23 +185,23 @@ struct block_header
185185
}
186186

187187
template<class Header>
188-
static block_header<size_type> *from_first_header(Header *header)
188+
static block_header *from_first_header(Header *header)
189189
{
190-
block_header<size_type> * hdr =
191-
move_detail::force_ptr<block_header<size_type>*>(reinterpret_cast<char*>(header) +
190+
block_header * hdr =
191+
move_detail::force_ptr<block_header*>(reinterpret_cast<char*>(header) +
192192
get_rounded_size( size_type(sizeof(Header))
193-
, size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value)));
193+
, size_type(::boost::container::dtl::alignment_of<block_header >::value)));
194194
//Some sanity checks
195195
return hdr;
196196
}
197197

198198
template<class Header>
199-
static Header *to_first_header(block_header<size_type> *bheader)
199+
static Header *to_first_header(block_header *bheader)
200200
{
201201
Header * hdr =
202202
move_detail::force_ptr<Header*>(reinterpret_cast<char*>(bheader) -
203203
get_rounded_size( size_type(sizeof(Header))
204-
, size_type(::boost::container::dtl::alignment_of<block_header<size_type> >::value)));
204+
, size_type(::boost::container::dtl::alignment_of<block_header >::value)));
205205
//Some sanity checks
206206
return hdr;
207207
}
@@ -256,15 +256,16 @@ struct intrusive_value_type_impl
256256
public:
257257
typedef CharType char_type;
258258
typedef SizeType size_type;
259+
typedef block_header<size_type> block_header_t;
259260

260261
intrusive_value_type_impl(){}
261262

262-
enum { BlockHdrAlignment = ::boost::container::dtl::alignment_of<block_header<size_type> >::value };
263+
enum { BlockHdrAlignment = ::boost::container::dtl::alignment_of<block_header_t>::value };
263264

264-
block_header<size_type> *get_block_header() const
265+
block_header_t *get_block_header() const
265266
{
266-
return const_cast<block_header<size_type>*>
267-
(move_detail::force_ptr<const block_header<size_type> *>(reinterpret_cast<const char*>(this) +
267+
return const_cast<block_header_t*>
268+
(move_detail::force_ptr<const block_header_t *>(reinterpret_cast<const char*>(this) +
268269
get_rounded_size(size_type(sizeof(*this)), size_type(BlockHdrAlignment))));
269270
}
270271

@@ -274,7 +275,7 @@ struct intrusive_value_type_impl
274275
bool operator ==(const intrusive_value_type_impl<Hook, CharType, SizeType> & other) const
275276
{ return (this->get_block_header())->template equal_comp<CharType>(*other.get_block_header()); }
276277

277-
static intrusive_value_type_impl *get_intrusive_value_type(block_header<size_type> *hdr)
278+
static intrusive_value_type_impl *get_intrusive_value_type(block_header_t *hdr)
278279
{
279280
return move_detail::force_ptr<intrusive_value_type_impl*>(reinterpret_cast<char*>(hdr) -
280281
get_rounded_size(size_type(sizeof(intrusive_value_type_impl)), size_type(BlockHdrAlignment)));
@@ -478,8 +479,15 @@ inline T* null_or_bad_alloc(bool dothrow)
478479
{
479480
if (dothrow)
480481
throw bad_alloc();
481-
else
482-
return 0;
482+
return 0;
483+
}
484+
485+
template<class T>
486+
inline T* null_or_already_exists(bool dothrow)
487+
{
488+
if (dothrow)
489+
throw interprocess_exception(already_exists_error);
490+
return 0;
483491
}
484492

485493
} //namespace ipcdetail {

include/boost/interprocess/segment_manager.hpp

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class segment_manager_base
285285
void *ptr_struct = this->allocate_aligned(block_info.total_size(), table.alignment, nothrow<>::get());
286286
if(!ptr_struct){
287287
return ipcdetail::null_or_bad_alloc<void>(dothrow);
288-
}
288+
}
289289

290290
//Build scoped ptr to avoid leaks with constructor exception
291291
ipcdetail::mem_algo_deallocator<MemoryAlgorithm> mem(ptr_struct, *this);
@@ -298,7 +298,7 @@ class segment_manager_base
298298
//Now call constructors
299299
table.construct_n(ptr, num);
300300

301-
//All constructors successful, we don't want erase memory
301+
//All constructors successful, disable rollback
302302
mem.release();
303303
return ptr;
304304
}
@@ -315,7 +315,7 @@ class segment_manager_base
315315
//scoped_lock<rmutex> guard(m_header);
316316
//-------------------------------
317317

318-
//This is not an anonymous object, the pointer is wrong!
318+
//This is not an anonymous object, the pointer is wrong!
319319
BOOST_ASSERT(ctrl_data->alloc_type() == anonymous_type);
320320

321321
//Call destructors and free memory
@@ -1061,7 +1061,7 @@ class segment_manager
10611061
IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > &index, ipcdetail::true_ is_intrusive)
10621062
{
10631063
(void)is_intrusive;
1064-
std::size_t namelen = std::char_traits<CharT>::length(name);
1064+
std::size_t namelen = std::char_traits<CharT>::length(name);
10651065

10661066
block_header_t block_info ( size_type(table.size*num)
10671067
, size_type(table.alignment)
@@ -1112,34 +1112,21 @@ class segment_manager
11121112
if(try2find){
11131113
return it->get_block_header()->value();
11141114
}
1115-
if(dothrow){
1116-
throw interprocess_exception(already_exists_error);
1117-
}
1118-
else{
1119-
return 0;
1120-
}
1115+
return ipcdetail::null_or_already_exists<void>(dothrow);
11211116
}
11221117

11231118
//Allocates buffer for name + data, this can throw (it hurts)
1124-
void *buffer_ptr;
1119+
void *buffer_ptr = this->allocate
1120+
(block_info.template total_size_with_header<intrusive_value_type>(), nothrow<>::get());
11251121

11261122
//Check if there is enough memory
1127-
if(dothrow){
1128-
buffer_ptr = this->allocate
1129-
(block_info.template total_size_with_header<intrusive_value_type>());
1130-
}
1131-
else{
1132-
buffer_ptr = this->allocate
1133-
(block_info.template total_size_with_header<intrusive_value_type>(), nothrow<>::get());
1134-
if(!buffer_ptr)
1135-
return 0;
1136-
}
1123+
if (!buffer_ptr)
1124+
return ipcdetail::null_or_bad_alloc<void>(dothrow);
11371125

11381126
//Now construct the intrusive hook plus the header
11391127
intrusive_value_type * intrusive_hdr = ::new(buffer_ptr, boost_container_new_t()) intrusive_value_type();
1140-
block_header_t * hdr = ::new(intrusive_hdr->get_block_header(), boost_container_new_t())block_header_t(block_info);
1141-
void *ptr = 0; //avoid gcc warning
1142-
ptr = hdr->value();
1128+
block_header_t * hdr = ::new(intrusive_hdr->get_block_header(), boost_container_new_t()) block_header_t(block_info);
1129+
void *ptr = hdr->value();
11431130

11441131
//Copy name to memory segment and insert data
11451132
CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());
@@ -1229,15 +1216,13 @@ class segment_manager
12291216

12301217
index_it it = insert_ret.first;
12311218

1232-
//If found and this is find or construct, return data
1233-
//else return null
1219+
//If found and this is find or construct, return data, error otherwise
12341220
if(!insert_ret.second){
12351221
if(try2find){
1236-
block_header_t *hdr = static_cast<block_header_t*>
1237-
(ipcdetail::to_raw_pointer(it->second.m_ptr));
1222+
block_header_t *hdr = static_cast<block_header_t*>(ipcdetail::to_raw_pointer(it->second.m_ptr));
12381223
return hdr->value();
12391224
}
1240-
return 0;
1225+
return ipcdetail::null_or_already_exists<void>(dothrow);
12411226
}
12421227
//Initialize the node value_eraser to erase inserted node
12431228
//if something goes wrong
@@ -1248,34 +1233,24 @@ class segment_manager
12481233
block_header_t * hdr;
12491234

12501235
//Allocate and construct the headers
1251-
if(is_node_index_t::value){
1236+
BOOST_IF_CONSTEXPR(is_node_index_t::value){
12521237
size_type total_size = block_info.template total_size_with_header<index_it>();
1253-
if(dothrow){
1254-
buffer_ptr = this->allocate(total_size);
1255-
}
1256-
else{
1257-
buffer_ptr = this->allocate(total_size, nothrow<>::get());
1258-
if(!buffer_ptr)
1259-
return 0;
1260-
}
1238+
buffer_ptr = this->allocate(total_size, nothrow<>::get());
1239+
if(!buffer_ptr)
1240+
return ipcdetail::null_or_bad_alloc<void>(dothrow);
12611241
index_it *idr = ::new(buffer_ptr, boost_container_new_t()) index_it(it);
12621242
hdr = block_header_t::template from_first_header<index_it>(idr);
12631243
}
12641244
else{
1265-
if(dothrow){
1266-
buffer_ptr = this->allocate(block_info.total_size());
1267-
}
1268-
else{
1269-
buffer_ptr = this->allocate(block_info.total_size(), nothrow<>::get());
1270-
if(!buffer_ptr)
1271-
return 0;
1272-
}
1245+
buffer_ptr = this->allocate(block_info.total_size(), nothrow<>::get());
1246+
//Check if there is enough memory
1247+
if (!buffer_ptr)
1248+
return ipcdetail::null_or_bad_alloc<void>(dothrow);
12731249
hdr = static_cast<block_header_t*>(buffer_ptr);
12741250
}
12751251

1276-
hdr = ::new(hdr, boost_container_new_t())block_header_t(block_info);
1277-
void *ptr = 0; //avoid gcc warning
1278-
ptr = hdr->value();
1252+
hdr = ::new(hdr, boost_container_new_t()) block_header_t(block_info);
1253+
void *ptr = hdr->value();
12791254

12801255
//Copy name to memory segment and insert data
12811256
CharT *name_ptr = static_cast<CharT *>(hdr->template name<CharT>());

0 commit comments

Comments
 (0)