Skip to content

Commit 0c2f749

Browse files
authored
Merge pull request #733 from jszuppe/pr_vector_shrink_to_fit
Implement vector::shrink_to_fit()
2 parents 251d02b + 6c2a360 commit 0c2f749

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

include/boost/compute/algorithm/copy.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ dispatch_copy(InputIterator first,
227227
boost::shared_ptr<parameter_cache> parameters =
228228
detail::parameter_cache::get_global_cache(device);
229229

230-
size_t map_copy_threshold;
231-
size_t direct_copy_threshold;
230+
uint_ map_copy_threshold;
231+
uint_ direct_copy_threshold;
232232

233233
// calculate default values of thresholds
234234
if (device.type() & device::gpu) {
@@ -310,8 +310,8 @@ dispatch_copy(InputIterator first,
310310
boost::shared_ptr<parameter_cache> parameters =
311311
detail::parameter_cache::get_global_cache(device);
312312

313-
size_t map_copy_threshold;
314-
size_t direct_copy_threshold;
313+
uint_ map_copy_threshold;
314+
uint_ direct_copy_threshold;
315315

316316
// calculate default values of thresholds
317317
if (device.type() & device::gpu) {
@@ -505,8 +505,8 @@ dispatch_copy(InputIterator first,
505505
boost::shared_ptr<parameter_cache> parameters =
506506
detail::parameter_cache::get_global_cache(device);
507507

508-
size_t map_copy_threshold;
509-
size_t direct_copy_threshold;
508+
uint_ map_copy_threshold;
509+
uint_ direct_copy_threshold;
510510

511511
// calculate default values of thresholds
512512
if (device.type() & device::gpu) {
@@ -587,8 +587,8 @@ dispatch_copy(InputIterator first,
587587
boost::shared_ptr<parameter_cache> parameters =
588588
detail::parameter_cache::get_global_cache(device);
589589

590-
size_t map_copy_threshold;
591-
size_t direct_copy_threshold;
590+
uint_ map_copy_threshold;
591+
uint_ direct_copy_threshold;
592592

593593
// calculate default values of thresholds
594594
if (device.type() & device::gpu) {

include/boost/compute/container/vector.hpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ class vector
293293
/// Move-assigns the data from \p other to \c *this.
294294
vector& operator=(vector&& other)
295295
{
296-
if(m_size){
297-
m_allocator.deallocate(m_data, m_size);
296+
if(capacity() > 0){
297+
m_allocator.deallocate(m_data, capacity());
298298
}
299299

300300
m_data = std::move(other.m_data);
@@ -310,8 +310,8 @@ class vector
310310
/// Destroys the vector object.
311311
~vector()
312312
{
313-
if(m_size){
314-
m_allocator.deallocate(m_data, m_size);
313+
if(capacity() > 0){
314+
m_allocator.deallocate(m_data, capacity());
315315
}
316316
}
317317

@@ -401,11 +401,14 @@ class vector
401401
)
402402
);
403403

404-
// copy old values to the new buffer
405-
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
404+
if(capacity() > 0)
405+
{
406+
// copy old values to the new buffer
407+
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
406408

407-
// free old memory
408-
m_allocator.deallocate(m_data, m_size);
409+
// free old memory
410+
m_allocator.deallocate(m_data, capacity());
411+
}
409412

410413
// set new data and size
411414
m_data = new_data;
@@ -430,6 +433,10 @@ class vector
430433
/// Returns the capacity of the vector.
431434
size_type capacity() const
432435
{
436+
if(m_data == pointer()) // null pointer check
437+
{
438+
return 0;
439+
}
433440
return m_data.get_buffer().size() / sizeof(T);
434441
}
435442

@@ -447,11 +454,14 @@ class vector
447454
)
448455
);
449456

450-
// copy old values to the new buffer
451-
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
457+
if(capacity() > 0)
458+
{
459+
// copy old values to the new buffer
460+
::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
452461

453-
// free old memory
454-
m_allocator.deallocate(m_data, m_size);
462+
// free old memory
463+
m_allocator.deallocate(m_data, capacity());
464+
}
455465

456466
// set new data
457467
m_data = new_data;
@@ -467,7 +477,22 @@ class vector
467477

468478
void shrink_to_fit(command_queue &queue)
469479
{
470-
(void) queue;
480+
pointer old_data = m_data;
481+
m_data = pointer(); // null pointer
482+
if(m_size > 0)
483+
{
484+
// allocate new buffer
485+
m_data = m_allocator.allocate(m_size);
486+
487+
// copy old values to the new buffer
488+
::boost::compute::copy(old_data, old_data + m_size, m_data, queue);
489+
}
490+
491+
if(capacity() > 0)
492+
{
493+
// free old memory
494+
m_allocator.deallocate(old_data, capacity());
495+
}
471496
}
472497

473498
void shrink_to_fit()

test/test_vector.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,31 @@ BOOST_AUTO_TEST_CASE(swap_ctor_custom_alloc)
472472
CHECK_RANGE_EQUAL(int, 4, b, (11, 12, 13, 14));
473473
}
474474

475+
BOOST_AUTO_TEST_CASE(shrink_to_fit)
476+
{
477+
bc::vector<bc::int_> int_vector(5, context);
478+
BOOST_CHECK_EQUAL(int_vector.size(), 5);
479+
BOOST_CHECK(int_vector.capacity() >= 5);
480+
481+
int_vector.reserve(15);
482+
BOOST_CHECK_EQUAL(int_vector.size(), 5);
483+
BOOST_CHECK(int_vector.capacity() >= 15);
484+
485+
int_vector.shrink_to_fit();
486+
BOOST_CHECK_EQUAL(int_vector.size(), 5);
487+
BOOST_CHECK_EQUAL(int_vector.capacity(), 5);
488+
489+
int_vector.clear();
490+
BOOST_CHECK_EQUAL(int_vector.size(), 0);
491+
BOOST_CHECK_EQUAL(int_vector.capacity(), 5);
492+
493+
int_vector.shrink_to_fit();
494+
BOOST_CHECK_EQUAL(int_vector.size(), 0);
495+
BOOST_CHECK_EQUAL(int_vector.capacity(), 0);
496+
497+
int_vector.reserve(15);
498+
BOOST_CHECK_EQUAL(int_vector.size(), 0);
499+
BOOST_CHECK(int_vector.capacity() >= 15);
500+
}
501+
475502
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)