Skip to content

Commit ac523b2

Browse files
committed
Add more BOOST_CONTAINER_ATTRIBUTE_NODISCARD attributes and fix warnings triggered by this attribute
1 parent 8ce8b9e commit ac523b2

14 files changed

+76
-26
lines changed

include/boost/container/adaptive_pool.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ class adaptive_pool
150150

151151
//!Returns the number of elements that could be allocated.
152152
//!Never throws
153+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
153154
size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
154155
{ return size_type(-1)/(2u*sizeof(T)); }
155156

156157
//!Allocate memory for an array of count elements.
157158
//!Throws bad_alloc if there is no enough memory
159+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
158160
pointer allocate(size_type count, const void * = 0)
159161
{
160162
if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T))))
@@ -187,6 +189,7 @@ class adaptive_pool
187189
}
188190
}
189191

192+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
190193
pointer allocation_command(allocation_type command,
191194
size_type limit_size,
192195
size_type &prefer_in_recvd_out_size,
@@ -206,6 +209,7 @@ class adaptive_pool
206209
//!Allocates just one object. Memory allocated with this function
207210
//!must be deallocated only with deallocate_one().
208211
//!Throws bad_alloc if there is no enough memory
212+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
209213
pointer allocate_one()
210214
{
211215
typedef dtl::shared_adaptive_node_pool
@@ -317,11 +321,13 @@ class adaptive_pool
317321

318322
//!An allocator always compares to true, as memory allocated with one
319323
//!instance can be deallocated by another instance
324+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
320325
friend bool operator==(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
321326
{ return true; }
322327

323328
//!An allocator always compares to false, as memory allocated with one
324329
//!instance can be deallocated by another instance
330+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
325331
friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
326332
{ return false; }
327333

@@ -466,11 +472,13 @@ class private_adaptive_pool
466472

467473
//!Returns the number of elements that could be allocated.
468474
//!Never throws
475+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
469476
size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
470477
{ return size_type(-1)/(2u*sizeof(T)); }
471478

472479
//!Allocate memory for an array of count elements.
473480
//!Throws bad_alloc if there is no enough memory
481+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
474482
pointer allocate(size_type count, const void * = 0)
475483
{
476484
if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T))))
@@ -497,6 +505,7 @@ class private_adaptive_pool
497505
}
498506
}
499507

508+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
500509
pointer allocation_command(allocation_type command,
501510
size_type limit_size,
502511
size_type &prefer_in_recvd_out_size,
@@ -516,6 +525,7 @@ class private_adaptive_pool
516525
//!Allocates just one object. Memory allocated with this function
517526
//!must be deallocated only with deallocate_one().
518527
//!Throws bad_alloc if there is no enough memory
528+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
519529
pointer allocate_one()
520530
{
521531
return (pointer)m_pool.allocate_node();
@@ -583,11 +593,13 @@ class private_adaptive_pool
583593

584594
//!An allocator always compares to true, as memory allocated with one
585595
//!instance can be deallocated by another instance
596+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
586597
friend bool operator==(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
587598
{ return true; }
588599

589600
//!An allocator always compares to false, as memory allocated with one
590601
//!instance can be deallocated by another instance
602+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
591603
friend bool operator!=(const private_adaptive_pool &, const private_adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
592604
{ return false; }
593605

include/boost/container/allocator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class allocator
197197

198198
//!Returns the maximum number of elements that could be allocated.
199199
//!Never throws
200-
inline size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
200+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
201201
{ return size_type(-1)/(2u*sizeof(T)); }
202202

203203
//!Swaps two allocators, does nothing

include/boost/container/allocator_traits.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ struct allocator_traits
423423

424424
//! <b>Returns</b>: <code>a.allocate(n)</code>
425425
//!
426+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
426427
inline static pointer allocate(Allocator &a, size_type n)
427428
{ return a.allocate(n); }
428429

@@ -434,6 +435,7 @@ struct allocator_traits
434435

435436
//! <b>Effects</b>: calls <code>a.allocate(n, p)</code> if that call is well-formed;
436437
//! otherwise, invokes <code>a.allocate(n)</code>
438+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
437439
inline static pointer allocate(Allocator &a, size_type n, const_void_pointer p)
438440
{
439441
const bool value = boost::container::dtl::
@@ -458,6 +460,7 @@ struct allocator_traits
458460

459461
//! <b>Returns</b>: <code>a.max_size()</code> if that expression is well-formed; otherwise,
460462
//! <code>numeric_limits<size_type>::max()</code>.
463+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
461464
inline static size_type max_size(const Allocator &a) BOOST_NOEXCEPT_OR_NOTHROW
462465
{
463466
const bool value = allocator_traits_detail::has_max_size<Allocator, size_type (Allocator::*)() const>::value;

include/boost/container/new_allocator.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class new_allocator
151151

152152
//!Allocates memory for an array of count elements.
153153
//!Throws bad_alloc if there is no enough memory
154+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
154155
pointer allocate(size_type count)
155156
{
156157
return dtl::operator_new_allocate<T>(count);
@@ -165,6 +166,7 @@ class new_allocator
165166

166167
//!Returns the maximum number of elements that could be allocated.
167168
//!Never throws
169+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
168170
inline size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
169171
{ return std::size_t(-1)/(2*sizeof(T)); }
170172

@@ -175,11 +177,13 @@ class new_allocator
175177

176178
//!An new_allocator always compares to true, as memory allocated with one
177179
//!instance can be deallocated by another instance
180+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
178181
inline friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
179182
{ return true; }
180183

181184
//!An new_allocator always compares to false, as memory allocated with one
182185
//!instance can be deallocated by another instance
186+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
183187
inline friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
184188
{ return false; }
185189
};

include/boost/container/node_allocator.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ class node_allocator
137137

138138
//!Returns the number of elements that could be allocated.
139139
//!Never throws
140+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
140141
size_type max_size() const
141142
{ return size_type(-1)/sizeof(T); }
142143

143144
//!Allocate memory for an array of count elements.
144145
//!Throws bad_alloc if there is no enough memory
146+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
145147
pointer allocate(size_type count, const void * = 0)
146148
{
147149
if(BOOST_UNLIKELY(count > this->max_size()))
@@ -186,6 +188,7 @@ class node_allocator
186188
singleton_t::instance().deallocate_free_blocks();
187189
}
188190

191+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
189192
pointer allocation_command
190193
(allocation_type command, size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
191194
{
@@ -198,6 +201,7 @@ class node_allocator
198201

199202
//!Returns maximum the number of objects the previously allocated memory
200203
//!pointed by p can hold.
204+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
201205
size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
202206
{
203207
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
@@ -207,6 +211,7 @@ class node_allocator
207211
//!Allocates just one object. Memory allocated with this function
208212
//!must be deallocated only with deallocate_one().
209213
//!Throws bad_alloc if there is no enough memory
214+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
210215
pointer allocate_one()
211216
{
212217
BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
@@ -307,11 +312,13 @@ class node_allocator
307312

308313
//!An allocator always compares to true, as memory allocated with one
309314
//!instance can be deallocated by another instance
315+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
310316
friend bool operator==(const node_allocator &, const node_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
311317
{ return true; }
312318

313319
//!An allocator always compares to false, as memory allocated with one
314320
//!instance can be deallocated by another instance
321+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
315322
friend bool operator!=(const node_allocator &, const node_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
316323
{ return false; }
317324

include/boost/container/pmr/memory_resource.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ class BOOST_CONTAINER_NOVTABLE memory_resource
6262

6363
//! <b>Returns</b>:
6464
//! `&a == &b || a.is_equal(b)`.
65+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
6566
friend bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
6667
{ return &a == &b || a.is_equal(b); }
6768

6869
//! <b>Returns</b>:
6970
//! !(a == b).
71+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
7072
friend bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
7173
{ return !(a == b); }
7274

include/boost/container/pmr/monotonic_buffer_resource.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class BOOST_CONTAINER_DECL monotonic_buffer_resource
112112
~monotonic_buffer_resource() BOOST_OVERRIDE;
113113

114114
//! <b>Effects</b>: `upstream_resource()->deallocate()` as necessary to release all allocated memory.
115+
//! Resets *this to its initial state at construction.
115116
//! [Note: memory is released back to `upstream_resource()` even if some blocks that were allocated
116117
//! from this have not been deallocated from this. - end note]
117118
void release() BOOST_NOEXCEPT;

include/boost/container/pmr/polymorphic_allocator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class polymorphic_allocator
7878

7979
//! <b>Returns</b>: Equivalent to
8080
//! `static_cast<T*>(m_resource->allocate(n * sizeof(T), alignof(T)))`.
81-
T* allocate(size_t n)
81+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD T* allocate(size_t n)
8282
{ return static_cast<T*>(m_resource->allocate(n*sizeof(T), ::boost::move_detail::alignment_of<T>::value)); }
8383

8484
//! <b>Requires</b>: p was allocated from a memory resource, x, equal to *m_resource,

include/boost/container/scoped_allocator.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ class scoped_allocator_adaptor
734734

735735
//! <b>Returns</b>:
736736
//! <code>allocator_traits<OuterAlloc>:: max_size(outer_allocator())</code>.
737+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
737738
inline size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
738739
{ return outer_traits_type::max_size(this->outer_allocator()); }
739740

@@ -748,11 +749,13 @@ class scoped_allocator_adaptor
748749

749750
//! <b>Returns</b>:
750751
//! <code>allocator_traits<OuterAlloc>::allocate(outer_allocator(), n)</code>.
752+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
751753
inline pointer allocate(size_type n)
752754
{ return outer_traits_type::allocate(this->outer_allocator(), n); }
753755

754756
//! <b>Returns</b>:
755757
//! <code>allocator_traits<OuterAlloc>::allocate(outer_allocator(), n, hint)</code>.
758+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
756759
inline pointer allocate(size_type n, const_void_pointer hint)
757760
{ return outer_traits_type::allocate(this->outer_allocator(), n, hint); }
758761

@@ -880,6 +883,7 @@ struct scoped_allocator_operator_equal<true>
880883
/// @endcond
881884

882885
template <typename OuterA1, typename OuterA2, BOOST_CONTAINER_SCOPEDALLOC_ALLINNERCLASS>
886+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
883887
inline bool operator==(const scoped_allocator_adaptor<OuterA1, BOOST_CONTAINER_SCOPEDALLOC_ALLINNER>& a
884888
,const scoped_allocator_adaptor<OuterA2, BOOST_CONTAINER_SCOPEDALLOC_ALLINNER>& b)
885889
{
@@ -894,6 +898,7 @@ inline bool operator==(const scoped_allocator_adaptor<OuterA1, BOOST_CONTAINER_S
894898
}
895899

896900
template <typename OuterA1, typename OuterA2, BOOST_CONTAINER_SCOPEDALLOC_ALLINNERCLASS>
901+
BOOST_CONTAINER_ATTRIBUTE_NODISCARD
897902
inline bool operator!=(const scoped_allocator_adaptor<OuterA1, BOOST_CONTAINER_SCOPEDALLOC_ALLINNER>& a
898903
,const scoped_allocator_adaptor<OuterA2, BOOST_CONTAINER_SCOPEDALLOC_ALLINNER>& b)
899904
{ return !(a == b); }

test/allocator_traits_test.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -320,27 +320,36 @@ int main()
320320
CAlloc c_alloc;
321321
SAlloc s_alloc;
322322

323-
//allocate
324-
CAllocTraits::allocate(c_alloc, 1);
325-
BOOST_TEST(c_alloc.allocate_called());
326-
327-
SAllocTraits::allocate(s_alloc, 1);
328-
BOOST_TEST(s_alloc.allocate_called());
323+
//allocate/deallocate
324+
{
325+
CAllocTraits::pointer p = CAllocTraits::allocate(c_alloc, 1);
326+
BOOST_TEST(c_alloc.allocate_called());
329327

330-
//deallocate
331-
CAllocTraits::deallocate(c_alloc, CAllocTraits::pointer(), 1);
332-
BOOST_TEST(c_alloc.deallocate_called());
328+
CAllocTraits::deallocate(c_alloc, p, 1);
329+
BOOST_TEST(c_alloc.deallocate_called());
330+
}
331+
{
332+
SAllocTraits::pointer p = SAllocTraits::allocate(s_alloc, 1);
333+
BOOST_TEST(s_alloc.allocate_called());
333334

334-
SAllocTraits::deallocate(s_alloc, SAllocTraits::pointer(), 1);
335-
BOOST_TEST(s_alloc.deallocate_called());
335+
SAllocTraits::deallocate(s_alloc, p, 1);
336+
BOOST_TEST(s_alloc.deallocate_called());
337+
}
336338

337339
//allocate with hint
338-
CAllocTraits::allocate(c_alloc, 1, CAllocTraits::const_void_pointer());
339-
BOOST_TEST(c_alloc.allocate_hint_called());
340-
341-
s_alloc.allocate_called_ = false;
342-
SAllocTraits::allocate(s_alloc, 1, SAllocTraits::const_void_pointer());
343-
BOOST_TEST(s_alloc.allocate_called());
340+
{
341+
CAllocTraits::pointer p = CAllocTraits::allocate(c_alloc, 1, CAllocTraits::const_void_pointer());
342+
BOOST_TEST(c_alloc.allocate_hint_called());
343+
CAllocTraits::deallocate(c_alloc, p, 1);
344+
BOOST_TEST(c_alloc.deallocate_called());
345+
}
346+
{
347+
s_alloc.allocate_called_ = false;
348+
SAllocTraits::pointer p = SAllocTraits::allocate(s_alloc, 1, SAllocTraits::const_void_pointer());
349+
BOOST_TEST(s_alloc.allocate_called());
350+
SAllocTraits::deallocate(s_alloc, p, 1);
351+
BOOST_TEST(s_alloc.deallocate_called());
352+
}
344353

345354
//destroy
346355
float dummy;
@@ -350,7 +359,7 @@ int main()
350359
SAllocTraits::destroy(s_alloc, &dummy);
351360

352361
//max_size
353-
CAllocTraits::max_size(c_alloc);
362+
BOOST_TEST(0 != CAllocTraits::max_size(c_alloc));
354363
BOOST_TEST(c_alloc.max_size_called());
355364

356365
BOOST_TEST(SAllocTraits::size_type(-1)/sizeof(SAllocTraits::value_type) == SAllocTraits::max_size(s_alloc));

0 commit comments

Comments
 (0)