summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-02-09 17:20:01 +0100
committerGitHub <noreply@github.com>2022-02-09 17:20:01 +0100
commit5603653ace6fce5bba2bf515685dc1de97a42088 (patch)
treec2cb5044ebc60c41054c8cec2dbad7bff0f8b026
parentc95a8a1a82459df6a2d2eb5a785a82aa07e330f7 (diff)
parente9b9584a587af65817fe0d04bedd939ec004a185 (diff)
Merge pull request #21125 from vespa-engine/toregge/add-reset-and-create-methods-to-vespalib-array
Add reset and create methods to vespalib::Array.
-rw-r--r--vespalib/src/tests/array/array_test.cpp48
-rw-r--r--vespalib/src/vespa/vespalib/util/alloc.h7
-rw-r--r--vespalib/src/vespa/vespalib/util/array.h2
-rw-r--r--vespalib/src/vespa/vespalib/util/array.hpp15
4 files changed, 72 insertions, 0 deletions
diff --git a/vespalib/src/tests/array/array_test.cpp b/vespalib/src/tests/array/array_test.cpp
index 719623c9401..5f80ee893da 100644
--- a/vespalib/src/tests/array/array_test.cpp
+++ b/vespalib/src/tests/array/array_test.cpp
@@ -2,6 +2,7 @@
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/testkit/testapp.h>
+#include <vespa/vespalib/test/memory_allocator_observer.h>
#include <vespa/vespalib/util/array.hpp>
#include <vespa/vespalib/util/size_literals.h>
#include <deque>
@@ -27,6 +28,11 @@ std::ostream & operator << (std::ostream & os, const Array<T> & a)
}
+using alloc::Alloc;
+using alloc::MemoryAllocator;
+using MyMemoryAllocator = vespalib::alloc::test::MemoryAllocatorObserver;
+using AllocStats = MyMemoryAllocator::Stats;
+
class Clever {
public:
Clever() : _counter(&_global) { (*_counter)++; }
@@ -351,4 +357,46 @@ TEST_F("require that try_unreserve() succeedes if mmap can be shrinked", Unreser
EXPECT_EQUAL(oldPtr, newPtr);
}
+struct Fixture {
+ AllocStats stats;
+ std::unique_ptr<MemoryAllocator> allocator;
+ Alloc initial_alloc;
+ Array<int> arr;
+
+ Fixture();
+ ~Fixture();
+};
+
+Fixture::Fixture()
+ : stats(),
+ allocator(std::make_unique<MyMemoryAllocator>(stats)),
+ initial_alloc(Alloc::alloc_with_allocator(allocator.get())),
+ arr(initial_alloc)
+{
+}
+
+Fixture::~Fixture() = default;
+
+TEST_F("require that memory allocator can be set", Fixture)
+{
+ f.arr.resize(1);
+ EXPECT_EQUAL(AllocStats(1, 0), f.stats);
+}
+
+TEST_F("require that memory allocator is preserved across reset", Fixture)
+{
+ f.arr.resize(1);
+ f.arr.reset();
+ f.arr.resize(1);
+ EXPECT_EQUAL(AllocStats(2, 1), f.stats);
+}
+
+TEST_F("require that created array uses same memory allocator", Fixture)
+{
+ auto arr2 = f.arr.create();
+ EXPECT_EQUAL(AllocStats(0, 0), f.stats);
+ arr2.resize(1);
+ EXPECT_EQUAL(AllocStats(1, 0), f.stats);
+}
+
TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/vespalib/src/vespa/vespalib/util/alloc.h b/vespalib/src/vespa/vespalib/util/alloc.h
index d25fb6f6a7c..4066894b4e3 100644
--- a/vespalib/src/vespa/vespalib/util/alloc.h
+++ b/vespalib/src/vespa/vespalib/util/alloc.h
@@ -62,6 +62,13 @@ public:
std::swap(_alloc, rhs._alloc);
std::swap(_allocator, rhs._allocator);
}
+ void reset() {
+ if (_alloc.first != nullptr) {
+ _allocator->free(_alloc);
+ _alloc.first = nullptr;
+ _alloc.second = 0u;
+ }
+ }
Alloc create(size_t sz) const noexcept {
return (sz == 0) ? Alloc(_allocator) : Alloc(_allocator, sz);
}
diff --git a/vespalib/src/vespa/vespalib/util/array.h b/vespalib/src/vespa/vespalib/util/array.h
index 30d87cd98f6..cb5d5c7cc63 100644
--- a/vespalib/src/vespa/vespalib/util/array.h
+++ b/vespalib/src/vespa/vespalib/util/array.h
@@ -135,6 +135,7 @@ public:
std::destroy(array(0), array(_sz));
_sz = 0;
}
+ void reset();
bool empty() const { return _sz == 0; }
T & operator [] (size_t i) { return *array(i); }
const T & operator [] (size_t i) const { return *array(i); }
@@ -145,6 +146,7 @@ public:
rhs._sz = 0;
return std::move(rhs._array);
}
+ Array<T> create() const;
private:
T * array(size_t i) { return static_cast<T *>(_array.get()) + i; }
const T * array(size_t i) const { return static_cast<const T *>(_array.get()) + i; }
diff --git a/vespalib/src/vespa/vespalib/util/array.hpp b/vespalib/src/vespa/vespalib/util/array.hpp
index e9070f5759c..72178f0391b 100644
--- a/vespalib/src/vespa/vespalib/util/array.hpp
+++ b/vespalib/src/vespa/vespalib/util/array.hpp
@@ -205,5 +205,20 @@ void Array<T>::cleanup()
Alloc().swap(_array);
}
+template <typename T>
+void Array<T>::reset()
+{
+ std::destroy(array(0), array(_sz));
+ _sz = 0;
+ _array.reset();
+}
+
+template <typename T>
+Array<T>
+Array<T>::create() const
+{
+ return Array<T>(_array); // Use same memory allocator
+}
+
}