diff options
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/util/alloc.cpp | 26 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/alloc.h | 32 |
2 files changed, 24 insertions, 34 deletions
diff --git a/vespalib/src/vespa/vespalib/util/alloc.cpp b/vespalib/src/vespa/vespalib/util/alloc.cpp index b6003f43792..5f1faa785ba 100644 --- a/vespalib/src/vespa/vespalib/util/alloc.cpp +++ b/vespalib/src/vespa/vespalib/util/alloc.cpp @@ -467,32 +467,6 @@ 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) -{ -} - -void -Alloc::free() { - _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) { diff --git a/vespalib/src/vespa/vespalib/util/alloc.h b/vespalib/src/vespa/vespalib/util/alloc.h index f1cb0fb0337..f608a244035 100644 --- a/vespalib/src/vespa/vespalib/util/alloc.h +++ b/vespalib/src/vespa/vespalib/util/alloc.h @@ -1,13 +1,12 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #pragma once -#include <vespa/vespalib/util/optimized.h> +#include "optimized.h" +#include "memory_allocator.h" #include <memory> namespace vespalib::alloc { -class MemoryAllocator; - /** * This represents an allocation. * It can be created, moved, swapped. @@ -41,11 +40,22 @@ public: { rhs.clear(); } - Alloc & operator=(Alloc && rhs) noexcept; + 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() noexcept : _alloc(nullptr, 0), _allocator(nullptr) { } ~Alloc() { if (_alloc.first != nullptr) { - free(); + _allocator->free(_alloc); + _alloc.first = nullptr; } } void swap(Alloc & rhs) noexcept { @@ -69,14 +79,20 @@ public: static Alloc alloc() noexcept; static Alloc alloc_with_allocator(const MemoryAllocator* allocator) noexcept; private: - Alloc(const MemoryAllocator * allocator, size_t sz) noexcept; - Alloc(const MemoryAllocator * allocator) noexcept : _alloc(nullptr, 0), _allocator(allocator) { } + Alloc(const MemoryAllocator * allocator, size_t sz) noexcept + : _alloc(allocator->alloc(sz)), + _allocator(allocator) + { + } + Alloc(const MemoryAllocator * allocator) noexcept + : _alloc(nullptr, 0), + _allocator(allocator) + { } void clear() { _alloc.first = nullptr; _alloc.second = 0; _allocator = nullptr; } - void free(); PtrAndSize _alloc; const MemoryAllocator * _allocator; }; |