diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-02-09 13:50:33 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-02-09 13:50:33 +0000 |
commit | 3df91301de5e6eb415174a6e5cd0786b8927ba47 (patch) | |
tree | e16a0d0af8ca04e74be2d040560fa9897d6267a1 /staging_vespalib | |
parent | 3903e12659b57760a8ffe3140747daa13d01068d (diff) |
Add a simple MallocMmapGuard
Diffstat (limited to 'staging_vespalib')
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.cpp | 22 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.h | 27 |
2 files changed, 49 insertions, 0 deletions
diff --git a/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.cpp b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.cpp new file mode 100644 index 00000000000..2b932981823 --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.cpp @@ -0,0 +1,22 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include "malloc_mmap_guard.h" +#include <malloc.h> +#include <limits> +#include <cassert> + +namespace vespalib { + +MallocMmapGuard::MallocMmapGuard(size_t mmapLimit) : + _threadId(std::this_thread::get_id()) +{ + int limit = mmapLimit <= std::numeric_limits<int>::max() ? mmapLimit : std::numeric_limits<int>::max(); + mallopt(M_MMAP_THRESHOLD, limit); +} + +MallocMmapGuard::~MallocMmapGuard() +{ + assert(_threadId == std::this_thread::get_id()); + mallopt(M_MMAP_THRESHOLD, -1); +} + +} diff --git a/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.h b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.h new file mode 100644 index 00000000000..3cf26202f9b --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/util/malloc_mmap_guard.h @@ -0,0 +1,27 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include <thread> + +namespace vespalib { + +/** + * Provides a hint to malloc implementation that all allocations in the scope of this guard + * will use mmap directly for allocation larger than the givem limit. + * The effect is implementation dependent. vespamalloc applies this only for the calling thread. + **/ +class MallocMmapGuard +{ +public: + MallocMmapGuard(size_t mmapLimit); + MallocMmapGuard(const MallocMmapGuard &) = delete; + MallocMmapGuard & operator=(const MallocMmapGuard &) = delete; + MallocMmapGuard(MallocMmapGuard &&) = delete; + MallocMmapGuard & operator=(MallocMmapGuard &&) = delete; + ~MallocMmapGuard(); +private: + std::thread::id _threadId; +}; + +} // namespace vespalib + |