diff options
author | Tor Egge <Tor.Egge@broadpark.no> | 2021-02-13 20:29:22 +0100 |
---|---|---|
committer | Tor Egge <Tor.Egge@broadpark.no> | 2021-02-13 20:29:22 +0100 |
commit | b316d0bcdcd345829cb4f13bf0b2d910c48f82d5 (patch) | |
tree | 78a5fd2a8a9e4ae6c8ffc6c303af96d94559fdd1 /vespalib | |
parent | 78f05fdddffb06745c38e1e020fa0c879536bd3e (diff) |
Forward declare MemoryAllocator in vespalib/util/alloc.h.
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/datastore/array_store/array_store_test.cpp | 1 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/datastore/bufferstate.cpp | 1 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/allocator.h | 2 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/alloc.cpp | 35 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/alloc.h | 29 |
5 files changed, 46 insertions, 22 deletions
diff --git a/vespalib/src/tests/datastore/array_store/array_store_test.cpp b/vespalib/src/tests/datastore/array_store/array_store_test.cpp index 168a4685ba1..72f803b441b 100644 --- a/vespalib/src/tests/datastore/array_store/array_store_test.cpp +++ b/vespalib/src/tests/datastore/array_store/array_store_test.cpp @@ -6,6 +6,7 @@ #include <vespa/vespalib/stllike/hash_map.hpp> #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/test/insertion_operators.h> +#include <vespa/vespalib/util/memory_allocator.h> #include <vespa/vespalib/util/traits.h> #include <vector> diff --git a/vespalib/src/vespa/vespalib/datastore/bufferstate.cpp b/vespalib/src/vespa/vespalib/datastore/bufferstate.cpp index 76e706435cf..d3a5402d7b0 100644 --- a/vespalib/src/vespa/vespalib/datastore/bufferstate.cpp +++ b/vespalib/src/vespa/vespalib/datastore/bufferstate.cpp @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "bufferstate.h" +#include <vespa/vespalib/util/memory_allocator.h> #include <limits> #include <cassert> diff --git a/vespalib/src/vespa/vespalib/stllike/allocator.h b/vespalib/src/vespa/vespalib/stllike/allocator.h index efe885f5c96..0a890973e09 100644 --- a/vespalib/src/vespa/vespalib/stllike/allocator.h +++ b/vespalib/src/vespa/vespalib/stllike/allocator.h @@ -2,7 +2,7 @@ #pragma once -#include <vespa/vespalib/util/alloc.h> +#include <vespa/vespalib/util/memory_allocator.h> namespace vespalib { diff --git a/vespalib/src/vespa/vespalib/util/alloc.cpp b/vespalib/src/vespa/vespalib/util/alloc.cpp index 5271a6ca779..511593ba1d6 100644 --- a/vespalib/src/vespa/vespalib/util/alloc.cpp +++ b/vespalib/src/vespa/vespalib/util/alloc.cpp @@ -1,5 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "alloc.h" +#include "memory_allocator.h" #include "round_up_to_page_size.h" #include <sys/mman.h> #include <vespa/vespalib/util/stringfmt.h> @@ -465,6 +466,34 @@ MemoryAllocator::select_allocator(size_t mmapLimit, size_t alignment) { return & AutoAllocator::getAllocator(mmapLimit, alignment); } +Alloc::Alloc(const MemoryAllocator * allocator, size_t sz) noexcept + : _alloc(allocator->alloc(sz)), + _allocator(allocator) +{ +} + +Alloc::~Alloc() +{ + if (_alloc.first != nullptr) { + _allocator->free(_alloc); + _alloc.first = nullptr; + } +} + +Alloc& +Alloc::operator=(Alloc && rhs) noexcept +{ + if (this != & rhs) { + if (_alloc.first != nullptr) { + _allocator->free(_alloc); + } + _alloc = rhs._alloc; + _allocator = rhs._allocator; + rhs.clear(); + } + return *this; +} + Alloc Alloc::allocHeap(size_t sz) { @@ -511,6 +540,12 @@ Alloc::alloc() noexcept } Alloc +Alloc::alloc(size_t sz) noexcept +{ + return Alloc(&AutoAllocator::getDefault(), sz); +} + +Alloc Alloc::alloc(size_t sz, size_t mmapLimit, size_t alignment) noexcept { return Alloc(&AutoAllocator::getAllocator(mmapLimit, alignment), sz); diff --git a/vespalib/src/vespa/vespalib/util/alloc.h b/vespalib/src/vespa/vespalib/util/alloc.h index 5d53334d8f6..5e71180f9be 100644 --- a/vespalib/src/vespa/vespalib/util/alloc.h +++ b/vespalib/src/vespa/vespalib/util/alloc.h @@ -1,12 +1,13 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include "memory_allocator.h" #include <vespa/vespalib/util/optimized.h> #include <memory> namespace vespalib::alloc { +class MemoryAllocator; + /** * This represents an allocation. * It can be created, moved, swapped. @@ -16,7 +17,7 @@ namespace vespalib::alloc { class Alloc { private: - using PtrAndSize = MemoryAllocator::PtrAndSize; + using PtrAndSize = std::pair<void *, size_t>; public: size_t size() const { return _alloc.second; } void * get() { return _alloc.first; } @@ -40,24 +41,9 @@ public: { rhs.clear(); } - Alloc & operator=(Alloc && rhs) noexcept { - if (this != & rhs) { - if (_alloc.first != nullptr) { - _allocator->free(_alloc); - } - _alloc = rhs._alloc; - _allocator = rhs._allocator; - rhs.clear(); - } - return *this; - } + Alloc & operator=(Alloc && rhs) noexcept; Alloc() noexcept : _alloc(nullptr, 0), _allocator(nullptr) { } - ~Alloc() { - if (_alloc.first != nullptr) { - _allocator->free(_alloc); - _alloc.first = nullptr; - } - } + ~Alloc(); void swap(Alloc & rhs) noexcept { std::swap(_alloc, rhs._alloc); std::swap(_allocator, rhs._allocator); @@ -73,11 +59,12 @@ public: * Optional alignment is assumed to be <= system page size, since mmap * is always used when size is above limit. */ - static Alloc alloc(size_t sz, size_t mmapLimit = MemoryAllocator::HUGEPAGE_SIZE, size_t alignment=0) noexcept; + static Alloc alloc(size_t sz) noexcept; + static Alloc alloc(size_t sz, size_t mmapLimit, size_t alignment=0) noexcept; static Alloc alloc() noexcept; static Alloc alloc_with_allocator(const MemoryAllocator* allocator) noexcept; private: - Alloc(const MemoryAllocator * allocator, size_t sz) noexcept : _alloc(allocator->alloc(sz)), _allocator(allocator) { } + Alloc(const MemoryAllocator * allocator, size_t sz) noexcept; Alloc(const MemoryAllocator * allocator) noexcept : _alloc(nullptr, 0), _allocator(allocator) { } void clear() { _alloc.first = nullptr; |