diff options
-rw-r--r-- | staging_vespalib/src/tests/memorydatastore/memorydatastore.cpp | 2 | ||||
-rw-r--r-- | vespalib/src/tests/alloc/alloc_test.cpp | 26 | ||||
-rw-r--r-- | vespalib/src/tests/array/array_test.cpp | 6 | ||||
-rw-r--r-- | vespalib/src/tests/exception_classes/mmap.cpp | 6 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/alloc.cpp | 20 |
5 files changed, 43 insertions, 17 deletions
diff --git a/staging_vespalib/src/tests/memorydatastore/memorydatastore.cpp b/staging_vespalib/src/tests/memorydatastore/memorydatastore.cpp index 2bb5a272783..57a18fc5573 100644 --- a/staging_vespalib/src/tests/memorydatastore/memorydatastore.cpp +++ b/staging_vespalib/src/tests/memorydatastore/memorydatastore.cpp @@ -21,7 +21,7 @@ public: void MemoryDataStoreTest::testMemoryDataStore() { - MemoryDataStore s(256); + MemoryDataStore s(DefaultAlloc::create(256)); std::vector<MemoryDataStore::Reference> v; v.push_back(s.push_back("mumbo", 5)); for (size_t i(0); i < 50; i++) { diff --git a/vespalib/src/tests/alloc/alloc_test.cpp b/vespalib/src/tests/alloc/alloc_test.cpp index 5f1ba897f61..c1ba982aa32 100644 --- a/vespalib/src/tests/alloc/alloc_test.cpp +++ b/vespalib/src/tests/alloc/alloc_test.cpp @@ -8,6 +8,7 @@ LOG_SETUP("alloc_test"); #include <vespa/vespalib/util/exceptions.h> using namespace vespalib; +using namespace vespalib::alloc; class Test : public TestApp { @@ -36,7 +37,7 @@ Test::testSwap(T & a, T & b) void * tmpB(b.get()); EXPECT_EQUAL(100u, a.size()); EXPECT_EQUAL(200u, b.size()); - swap(a, b); + std::swap(a, b); EXPECT_EQUAL(100u, b.size()); EXPECT_EQUAL(200u, a.size()); EXPECT_EQUAL(tmpA, b.get()); @@ -47,31 +48,36 @@ void Test::testBasic() { { - HeapAlloc h(100); + Alloc h = HeapAllocFactory::create(100); EXPECT_EQUAL(100u, h.size()); EXPECT_TRUE(h.get() != NULL); } { - EXPECT_EXCEPTION(AlignedHeapAlloc(100, 0), IllegalArgumentException, "posix_memalign(100, 0) failed with code 22"); - AlignedHeapAlloc h(100, 1024); + EXPECT_EXCEPTION(AlignedHeapAllocFactory::create(100, 0), IllegalArgumentException, "posix_memalign(100, 0) failed with code 22"); + Alloc h = AlignedHeapAllocFactory::create(100, 1024); EXPECT_EQUAL(100u, h.size()); EXPECT_TRUE(h.get() != NULL); } { - MMapAlloc h(100); + Alloc h = MMapAllocFactory::create(100); EXPECT_EQUAL(100u, h.size()); EXPECT_TRUE(h.get() != NULL); } { - HeapAlloc a(100), b(200); + Alloc a = HeapAllocFactory::create(100), b = HeapAllocFactory::create(200); testSwap(a, b); } { - MMapAlloc a(100), b(200); + Alloc a = MMapAllocFactory::create(100), b = MMapAllocFactory::create(200); testSwap(a, b); } { - AlignedHeapAlloc a(100, 1024), b(200, 1024); + Alloc a = AlignedHeapAllocFactory::create(100, 1024), b = AlignedHeapAllocFactory::create(200, 1024); + testSwap(a, b); + } + { + Alloc a = HeapAllocFactory::create(100); + Alloc b = MMapAllocFactory::create(200); testSwap(a, b); } } @@ -80,13 +86,13 @@ void Test::testAlignedAllocation() { { - AutoAlloc<2048, 1024> buf(10); + Alloc buf = AutoAllocFactory::create(10, 2048, 1024); EXPECT_TRUE(reinterpret_cast<ptrdiff_t>(buf.get()) % 1024 == 0); } { // Mmapped pointers are page-aligned, but sanity test anyway. - AutoAlloc<1024, 512> buf(3000); + Alloc buf = AutoAllocFactory::create(3000, 1024, 512); EXPECT_TRUE(reinterpret_cast<ptrdiff_t>(buf.get()) % 512 == 0); } } diff --git a/vespalib/src/tests/array/array_test.cpp b/vespalib/src/tests/array/array_test.cpp index 03b00769e7a..70eb07131f5 100644 --- a/vespalib/src/tests/array/array_test.cpp +++ b/vespalib/src/tests/array/array_test.cpp @@ -28,8 +28,8 @@ private: namespace vespalib { -template <typename T, typename B> -std::ostream & operator << (std::ostream & os, const Array<T,B> & a) +template <typename T> +std::ostream & operator << (std::ostream & os, const Array<T> & a) { os << '{'; if (! a.empty()) { @@ -118,7 +118,7 @@ Test::Main() void Test::testThatOrganicGrowthIsBy2InNAndReserveResizeAreExact() { - Array<char, DefaultAlloc> c(256); + Array<char> c(256); EXPECT_EQUAL(256u, c.size()); EXPECT_EQUAL(256u, c.capacity()); c.reserve(258); diff --git a/vespalib/src/tests/exception_classes/mmap.cpp b/vespalib/src/tests/exception_classes/mmap.cpp index 56c2b5d8d67..fe0a8fc4556 100644 --- a/vespalib/src/tests/exception_classes/mmap.cpp +++ b/vespalib/src/tests/exception_classes/mmap.cpp @@ -1,6 +1,6 @@ #include <vespa/vespalib/util/alloc.h> -using vespalib::MMapAlloc; +using namespace vespalib::alloc; int main(int argc, char *argv[]) { if (argc != 4) { @@ -13,9 +13,9 @@ int main(int argc, char *argv[]) { virtualLimit.rlim_cur = virt; virtualLimit.rlim_max = virt; assert(setrlimit(RLIMIT_AS, &virtualLimit) == 0); - std::vector<MMapAlloc> mappings; + std::vector<Alloc> mappings; for (size_t i(0); i < numBlocks; i++) { - mappings.emplace_back(blockSize); + mappings.emplace_back(MMapAllocFactory::create(blockSize)); memset(mappings.back().get(), 0xa5, mappings.back().size()); } return 0; diff --git a/vespalib/src/vespa/vespalib/util/alloc.cpp b/vespalib/src/vespa/vespalib/util/alloc.cpp index d719d7386bb..abe4c193860 100644 --- a/vespalib/src/vespa/vespalib/util/alloc.cpp +++ b/vespalib/src/vespa/vespalib/util/alloc.cpp @@ -256,6 +256,26 @@ void AutoAllocator::free(void *p, size_t sz) const { } Alloc +HeapAllocFactory::create(size_t sz) +{ + return Alloc(&HeapAllocator::getDefault(), sz); +} + +Alloc +AlignedHeapAllocFactory::create(size_t sz, size_t alignment) +{ + if (alignment == 0) { + return Alloc(&AlignedHeapAllocator::getDefault(), sz); + } else if (alignment == 0x200) { + return Alloc(&AlignedHeapAllocator::get512B(), sz); + } else if (alignment == 0x1000) { + return Alloc(&AlignedHeapAllocator::get4K(), sz); + } else { + abort(); + } +} + +Alloc MMapAllocFactory::create(size_t sz) { return Alloc(&MMapAllocator::getDefault(), sz); |