summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvsey <54716634+vsey@users.noreply.github.com>2023-12-26 23:43:14 +0100
committerGitHub <noreply@github.com>2023-12-26 23:43:14 +0100
commit63ce0abc6471cd270c4587ab52b1087bfea5377c (patch)
treea76e2c6b2857eda0008efe2912bd409cbbb3ddda
parent9dd43796fd6291298de9e0d32467f7f6df9c0298 (diff)
parentad14554f32cc9c19f3cdc8e28ea6012a974a533a (diff)
Merge branch 'aristocratos:main' into battery-power-2
-rw-r--r--Makefile8
-rw-r--r--README.md33
-rw-r--r--include/robin_hood.h2544
-rw-r--r--src/btop.cpp3
-rw-r--r--src/btop_config.cpp13
-rw-r--r--src/btop_config.hpp15
-rw-r--r--src/btop_draw.cpp170
-rw-r--r--src/btop_draw.hpp9
-rw-r--r--src/btop_input.cpp4
-rw-r--r--src/btop_input.hpp5
-rw-r--r--src/btop_menu.cpp7
-rw-r--r--src/btop_menu.hpp2
-rw-r--r--src/btop_shared.cpp2
-rw-r--r--src/btop_shared.hpp29
-rw-r--r--src/btop_theme.cpp14
-rw-r--r--src/btop_theme.hpp9
-rw-r--r--src/btop_tools.cpp3
-rw-r--r--src/btop_tools.hpp106
-rw-r--r--src/freebsd/btop_collect.cpp33
-rw-r--r--src/linux/btop_collect.cpp73
-rw-r--r--src/osx/btop_collect.cpp24
21 files changed, 327 insertions, 2779 deletions
diff --git a/Makefile b/Makefile
index 970e818..2b02f3d 100644
--- a/Makefile
+++ b/Makefile
@@ -61,6 +61,10 @@ CLANG_WORKS = false
GCC_WORKS = false
MIN_CLANG_VERSION = 16
+ifeq ($(DEBUG),true)
+ override ADDFLAGS += -DBTOP_DEBUG
+endif
+
#? Supported is Clang 16.0.0 and later
ifeq ($(CXX_IS_CLANG),true)
ifeq ($(shell $(CXX) --version | grep Apple >/dev/null 2>&1; echo $$?),0)
@@ -279,13 +283,13 @@ directories:
clean:
@printf "\033[1;91mRemoving: \033[1;97mbuilt objects...\033[0m\n"
@rm -rf $(BUILDDIR)
- @cmake --build lib/rocm_smi_lib/build --target clean &> /dev/null || true
+ @test -e lib/rocm_smi_lib/build && cmake --build lib/rocm_smi_lib/build --target clean &> /dev/null || true
#? Clean Objects and Binaries
distclean: clean
@printf "\033[1;91mRemoving: \033[1;97mbuilt binaries...\033[0m\n"
@rm -rf $(TARGETDIR)
- @rm -rf lib/rocm_smi_lib/build
+ @test -e lib/rocm_smi_lib/build && rm -rf lib/rocm_smi_lib/build || true
install:
@printf "\033[1;92mInstalling binary to: \033[1;97m$(DESTDIR)$(PREFIX)/bin/btop\n"
diff --git a/README.md b/README.md
index 47f8f08..f220e86 100644
--- a/README.md
+++ b/README.md
@@ -364,11 +364,11 @@ Also needs a UTF8 locale and a font that covers:
### With Make
</summary>
-1. **Install dependencies (example for Ubuntu 21.04 Hirsute)**
+1. **Install dependencies (example for Ubuntu 21.04 Hirsute)**
```bash
sudo apt install coreutils sed git build-essential gcc-11 g++-11
- ```
+ ```
2. **Clone repository**
@@ -391,17 +391,18 @@ Also needs a UTF8 locale and a font that covers:
| `STATIC=true` | For static compilation |
| `QUIET=true` | For less verbose output |
| `STRIP=true` | To force stripping of debug symbols (adds `-s` linker flag) |
+ | `DEBUG=true` | Sets OPTFLAGS to `-O0 -g` and enables more verbose debug logging |
| `ARCH=<architecture>` | To manually set the target architecture |
| `GPU_SUPPORT=<true\|false>` | Enable/disable GPU support (Enabled by default on X86_64 Linux) |
| `RSMI_STATIC=true` | To statically link the ROCm SMI library used for querying AMDGPU |
| `ADDFLAGS=<flags>` | For appending flags to both compiler and linker |
- | `CXX=<compiler>` | Manualy set which compiler to use |
+ | `CXX=<compiler>` | Manualy set which compiler to use |
Example: `make ADDFLAGS=-march=native` might give a performance boost if compiling only for your own system.
Notice! If using LDAP Authentication, usernames will show as UID number for LDAP users if compiling statically with glibc.
-4. **Install**
+4. **Install**
```bash
sudo make install
@@ -411,7 +412,7 @@ Also needs a UTF8 locale and a font that covers:
Notice! Only use "sudo" when installing to a NON user owned directory.
-5. **(Optional) Set suid bit to make btop always run as root (or other user)**
+5. **(Optional) Set suid bit to make btop always run as root (or other user)**
```bash
sudo make setuid
@@ -561,13 +562,14 @@ Also needs a UTF8 locale and a font that covers:
| `STATIC=true` | For static compilation (only libgcc and libstdc++) |
| `QUIET=true` | For less verbose output |
| `STRIP=true` | To force stripping of debug symbols (adds `-s` linker flag) |
- | `ARCH=<architecture>` | To manually set the target architecture |
+ | `DEBUG=true` | Sets OPTFLAGS to `-O0 -g` and enables more verbose debug logging |
+ | `ARCH=<architecture>` | To manually set the target architecture |
| `ADDFLAGS=<flags>` | For appending flags to both compiler and linker |
- | `CXX=<compiler>` | Manualy set which compiler to use |
+ | `CXX=<compiler>` | Manualy set which compiler to use |
- Example: `gmake ADDFLAGS=-march=native` might give a performance boost if compiling only for your own system.
+ Example: `gmake ADDFLAGS=-march=native` might give a performance boost if compiling only for your own system.
-4. **Install**
+4. **Install**
```bash
sudo gmake install
@@ -577,7 +579,7 @@ Also needs a UTF8 locale and a font that covers:
Notice! Only use "sudo" when installing to a NON user owned directory.
-5. **(Recommended) Set suid bit to make btop always run as root (or other user)**
+5. **(Recommended) Set suid bit to make btop always run as root (or other user)**
```bash
sudo gmake setuid
@@ -726,18 +728,19 @@ Also needs a UTF8 locale and a font that covers:
| `STATIC=true` | For static compilation (only libgcc and libstdc++) |
| `QUIET=true` | For less verbose output |
| `STRIP=true` | To force stripping of debug symbols (adds `-s` linker flag) |
- | `ARCH=<architecture>` | To manually set the target architecture |
+ | `DEBUG=true` | Sets OPTFLAGS to `-O0 -g` and enables more verbose debug logging |
+ | `ARCH=<architecture>` | To manually set the target architecture |
| `ADDFLAGS=<flags>` | For appending flags to both compiler and linker |
- | `CXX=<compiler>` | Manualy set which compiler to use |
+ | `CXX=<compiler>` | Manualy set which compiler to use |
- Example: `gmake ADDFLAGS=-march=native` might give a performance boost if compiling only for your own system.
+ Example: `gmake ADDFLAGS=-march=native` might give a performance boost if compiling only for your own system.
4. **Install**
```bash
sudo gmake install
```
-
+
Append `PREFIX=/target/dir` to set target, default: `/usr/local`
Notice! Only use "sudo" when installing to a NON user owned directory.
@@ -747,7 +750,7 @@ Also needs a UTF8 locale and a font that covers:
```bash
sudo gmake setuid
```
-
+
No need for `sudo` to see information for non user owned processes and to enable signal sending to any process.
Run after make install and use same PREFIX if any was used at install.
diff --git a/include/robin_hood.h b/include/robin_hood.h
deleted file mode 100644
index 0af031f..0000000
--- a/include/robin_hood.h
+++ /dev/null
@@ -1,2544 +0,0 @@
-// ______ _____ ______ _________
-// ______________ ___ /_ ___(_)_______ ___ /_ ______ ______ ______ /
-// __ ___/_ __ \__ __ \__ / __ __ \ __ __ \_ __ \_ __ \_ __ /
-// _ / / /_/ /_ /_/ /_ / _ / / / _ / / // /_/ // /_/ // /_/ /
-// /_/ \____/ /_.___/ /_/ /_/ /_/ ________/_/ /_/ \____/ \____/ \__,_/
-// _/_____/
-//
-// Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
-// https://github.com/martinus/robin-hood-hashing
-//
-// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
-// SPDX-License-Identifier: MIT
-// Copyright (c) 2018-2021 Martin Ankerl <http://martin.ankerl.com>
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in all
-// copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-// SOFTWARE.
-
-#ifndef ROBIN_HOOD_H_INCLUDED
-#define ROBIN_HOOD_H_INCLUDED
-
-// see https://semver.org/
-#define ROBIN_HOOD_VERSION_MAJOR 3 // for incompatible API changes
-#define ROBIN_HOOD_VERSION_MINOR 11 // for adding functionality in a backwards-compatible manner
-#define ROBIN_HOOD_VERSION_PATCH 5 // for backwards-compatible bug fixes
-
-#include <algorithm>
-#include <cstdlib>
-#include <cstring>
-#include <functional>
-#include <limits>
-#include <memory> // only to support hash of smart pointers
-#include <stdexcept>
-#include <string>
-#include <type_traits>
-#include <utility>
-#if __cplusplus >= 201703L
-# include <string_view>
-#endif
-
-// #define ROBIN_HOOD_LOG_ENABLED
-#ifdef ROBIN_HOOD_LOG_ENABLED
-# include <iostream>
-# define ROBIN_HOOD_LOG(...) \
- std::cout << __FUNCTION__ << "@" << __LINE__ << ": " << __VA_ARGS__ << std::endl;
-#else
-# define ROBIN_HOOD_LOG(x)
-#endif
-
-// #define ROBIN_HOOD_TRACE_ENABLED
-#ifdef ROBIN_HOOD_TRACE_ENABLED
-# include <iostream>
-# define ROBIN_HOOD_TRACE(...) \
- std::cout << __FUNCTION__ << "@" << __LINE__ << ": " << __VA_ARGS__ << std::endl;
-#else
-# define ROBIN_HOOD_TRACE(x)
-#endif
-
-// #define ROBIN_HOOD_COUNT_ENABLED
-#ifdef ROBIN_HOOD_COUNT_ENABLED
-# include <iostream>
-# define ROBIN_HOOD_COUNT(x) ++counts().x;
-namespace robin_hood {
-struct Counts {
- uint64_t shiftUp{};
- uint64_t shiftDown{};
-};
-inline std::ostream& operator<<(std::ostream& os, Counts const& c) {
- return os << c.shiftUp << " shiftUp" << std::endl << c.shiftDown << " shiftDown" << std::endl;
-}
-
-static Counts& counts() {
- static Counts counts{};
- return counts;
-}
-} // namespace robin_hood
-#else
-# define ROBIN_HOOD_COUNT(x)
-#endif
-
-// all non-argument macros should use this facility. See
-// https://www.fluentcpp.com/2019/05/28/better-macros-better-flags/
-#define ROBIN_HOOD(x) ROBIN_HOOD_PRIVATE_DEFINITION_##x()
-
-// mark unused members with this macro
-#define ROBIN_HOOD_UNUSED(identifier)
-
-// bitness
-#if SIZE_MAX == UINT32_MAX
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BITNESS() 32
-#elif SIZE_MAX == UINT64_MAX
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BITNESS() 64
-#else
-# error Unsupported bitness
-#endif
-
-// endianess
-#ifdef _MSC_VER
-# define ROBIN_HOOD_PRIVATE_DEFINITION_LITTLE_ENDIAN() 1
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BIG_ENDIAN() 0
-#else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_LITTLE_ENDIAN() \
- (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BIG_ENDIAN() (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
-#endif
-
-// inline
-#ifdef _MSC_VER
-# define ROBIN_HOOD_PRIVATE_DEFINITION_NOINLINE() __declspec(noinline)
-#else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_NOINLINE() __attribute__((noinline))
-#endif
-
-// exceptions
-#if !defined(__cpp_exceptions) && !defined(__EXCEPTIONS) && !defined(_CPPUNWIND)
-# define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_EXCEPTIONS() 0
-#else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_EXCEPTIONS() 1
-#endif
-
-// count leading/trailing bits
-#if !defined(ROBIN_HOOD_DISABLE_INTRINSICS)
-# ifdef _MSC_VER
-# if ROBIN_HOOD(BITNESS) == 32
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BITSCANFORWARD() _BitScanForward
-# else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BITSCANFORWARD() _BitScanForward64
-# endif
-# include <intrin.h>
-# pragma intrinsic(ROBIN_HOOD(BITSCANFORWARD))
-# define ROBIN_HOOD_COUNT_TRAILING_ZEROES(x) \
- [](size_t mask) noexcept -> int { \
- unsigned long index; \
- return ROBIN_HOOD(BITSCANFORWARD)(&index, mask) ? static_cast<int>(index) \
- : ROBIN_HOOD(BITNESS); \
- }(x)
-# else
-# if ROBIN_HOOD(BITNESS) == 32
-# define ROBIN_HOOD_PRIVATE_DEFINITION_CTZ() __builtin_ctzl
-# define ROBIN_HOOD_PRIVATE_DEFINITION_CLZ() __builtin_clzl
-# else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_CTZ() __builtin_ctzll
-# define ROBIN_HOOD_PRIVATE_DEFINITION_CLZ() __builtin_clzll
-# endif
-# define ROBIN_HOOD_COUNT_LEADING_ZEROES(x) ((x) ? ROBIN_HOOD(CLZ)(x) : ROBIN_HOOD(BITNESS))
-# define ROBIN_HOOD_COUNT_TRAILING_ZEROES(x) ((x) ? ROBIN_HOOD(CTZ)(x) : ROBIN_HOOD(BITNESS))
-# endif
-#endif
-
-// fallthrough
-#ifndef __has_cpp_attribute // For backwards compatibility
-# define __has_cpp_attribute(x) 0
-#endif
-#if __has_cpp_attribute(clang::fallthrough)
-# define ROBIN_HOOD_PRIVATE_DEFINITION_FALLTHROUGH() [[clang::fallthrough]]
-#elif __has_cpp_attribute(gnu::fallthrough)
-# define ROBIN_HOOD_PRIVATE_DEFINITION_FALLTHROUGH() [[gnu::fallthrough]]
-#else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_FALLTHROUGH()
-#endif
-
-// likely/unlikely
-#ifdef _MSC_VER
-# define ROBIN_HOOD_LIKELY(condition) condition
-# define ROBIN_HOOD_UNLIKELY(condition) condition
-#else
-# define ROBIN_HOOD_LIKELY(condition) __builtin_expect(condition, 1)
-# define ROBIN_HOOD_UNLIKELY(condition) __builtin_expect(condition, 0)
-#endif
-
-// detect if native wchar_t type is availiable in MSVC
-#ifdef _MSC_VER
-# ifdef _NATIVE_WCHAR_T_DEFINED
-# define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_NATIVE_WCHART() 1
-# else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_NATIVE_WCHART() 0
-# endif
-#else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_NATIVE_WCHART() 1
-#endif
-
-// detect if MSVC supports the pair(std::piecewise_construct_t,...) consructor being constexpr
-#ifdef _MSC_VER
-# if _MSC_VER <= 1900
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BROKEN_CONSTEXPR() 1
-# else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BROKEN_CONSTEXPR() 0
-# endif
-#else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_BROKEN_CONSTEXPR() 0
-#endif
-
-// workaround missing "is_trivially_copyable" in g++ < 5.0
-// See https://stackoverflow.com/a/31798726/48181
-#if defined(__GNUC__) && __GNUC__ < 5
-# define ROBIN_HOOD_IS_TRIVIALLY_COPYABLE(...) __has_trivial_copy(__VA_ARGS__)
-#else
-# define ROBIN_HOOD_IS_TRIVIALLY_COPYABLE(...) std::is_trivially_copyable<__VA_ARGS__>::value
-#endif
-
-// helpers for C++ versions, see https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
-#define ROBIN_HOOD_PRIVATE_DEFINITION_CXX() __cplusplus
-#define ROBIN_HOOD_PRIVATE_DEFINITION_CXX98() 199711L
-#define ROBIN_HOOD_PRIVATE_DEFINITION_CXX11() 201103L
-#define ROBIN_HOOD_PRIVATE_DEFINITION_CXX14() 201402L
-#define ROBIN_HOOD_PRIVATE_DEFINITION_CXX17() 201703L
-
-#if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX17)
-# define ROBIN_HOOD_PRIVATE_DEFINITION_NODISCARD() [[nodiscard]]
-#else
-# define ROBIN_HOOD_PRIVATE_DEFINITION_NODISCARD()
-#endif
-
-namespace robin_hood {
-
-#if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX14)
-# define ROBIN_HOOD_STD std
-#else
-
-// c++11 compatibility layer
-namespace ROBIN_HOOD_STD {
-template <class T>
-struct alignment_of
- : std::integral_constant<std::size_t, alignof(typename std::remove_all_extents<T>::type)> {};
-
-template <class T, T... Ints>
-class integer_sequence {
-public:
- using value_type = T;
- static_assert(std::is_integral<value_type>::value, "not integral type");
- static constexpr std::size_t size() noexcept {
- return sizeof...(Ints);
- }
-};
-template <std::size_t... Inds>
-using index_sequence = integer_sequence<std::size_t, Inds...>;
-
-namespace detail_ {
-template <class T, T Begin, T End, bool>
-struct IntSeqImpl {
- using TValue = T;
- static_assert(std::is_integral<TValue>::value, "not integral type");
- static_assert(Begin >= 0 && Begin < End, "unexpected argument (Begin<0 || Begin<=End)");
-
- template <class, class>
- struct IntSeqCombiner;
-
- template <TValue... Inds0, TValue... Inds1>
- struct IntSeqCombiner<integer_sequence<TValue, Inds0...>, integer_sequence<TValue, Inds1...>> {
- using TResult = integer_sequence<TValue, Inds0..., Inds1...>;
- };
-
- using TResult =
- typename IntSeqCombiner<typename IntSeqImpl<TValue, Begin, Begin + (End - Begin) / 2,
- (End - Begin) / 2 == 1>::TResult,
- typename IntSeqImpl<TValue, Begin + (End - Begin) / 2, End,
- (End - Begin + 1) / 2 == 1>::TResult>::TResult;
-};
-
-template <class T, T Begin>
-struct IntSeqImpl<T, Begin, Begin, false> {
- using TValue = T;
- static_assert(std::is_integral<TValue>::value, "not integral type");
- static_assert(Begin >= 0, "unexpected argument (Begin<0)");
- using TResult = integer_sequence<TValue>;
-};
-
-template <class T, T Begin, T End>
-struct IntSeqImpl<T, Begin, End, true> {
- using TValue = T;
- static_assert(std::is_integral<TValue>::value, "not integral type");
- static_assert(Begin >= 0, "unexpected argument (Begin<0)");
- using TResult = integer_sequence<TValue, Begin>;
-};
-} // namespace detail_
-
-template <class T, T N>
-using make_integer_sequence = typename detail_::IntSeqImpl<T, 0, N, (N - 0) == 1>::TResult;
-
-template <std::size_t N>
-using make_index_sequence = make_integer_sequence<std::size_t, N>;
-
-template <class... T>
-using index_sequence_for = make_index_sequence<sizeof...(T)>;
-
-} // namespace ROBIN_HOOD_STD
-
-#endif
-
-namespace detail {
-
-// make sure we static_cast to the correct type for hash_int
-#if ROBIN_HOOD(BITNESS) == 64
-using SizeT = uint64_t;
-#else
-using SizeT = uint32_t;
-#endif
-
-template <typename T>
-T rotr(T x, unsigned k) {
- return (x >> k) | (x << (8U * sizeof(T) - k));
-}
-
-// This cast gets rid of warnings like "cast from 'uint8_t*' {aka 'unsigned char*'} to
-// 'uint64_t*' {aka 'long unsigned int*'} increases required alignment of target type". Use with
-// care!
-template <typename T>
-inline T reinterpret_cast_no_cast_align_warning(void* ptr) noexcept {
- return reinterpret_cast<T>(ptr);
-}
-
-template <typename T>
-inline T reinterpret_cast_no_cast_align_warning(void const* ptr) noexcept {
- return reinterpret_cast<T>(ptr);
-}
-
-// make sure this is not inlined as it is slow and dramatically enlarges code, thus making other
-// inlinings more difficult. Throws are also generally the slow path.
-template <typename E, typename... Args>
-[[noreturn]] ROBIN_HOOD(NOINLINE)
-#if ROBIN_HOOD(HAS_EXCEPTIONS)
- void doThrow(Args&&... args) {
- // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
- throw E(std::forward<Args>(args)...);
-}
-#else
- void doThrow(Args&&... ROBIN_HOOD_UNUSED(args) /*unused*/) {
- abort();
-}
-#endif
-
-template <typename E, typename T, typename... Args>
-T* assertNotNull(T* t, Args&&... args) {
- if (ROBIN_HOOD_UNLIKELY(nullptr == t)) {
- doThrow<E>(std::forward<Args>(args)...);
- }
- return t;
-}
-
-template <typename T>
-inline T unaligned_load(void const* ptr) noexcept {
- // using memcpy so we don't get into unaligned load problems.
- // compiler should optimize this very well anyways.
- T t;
- std::memcpy(&t, ptr, sizeof(T));
- return t;
-}
-
-// Allocates bulks of memory for objects of type T. This deallocates the memory in the destructor,
-// and keeps a linked list of the allocated memory around. Overhead per allocation is the size of a
-// pointer.
-template <typename T, size_t MinNumAllocs = 4, size_t MaxNumAllocs = 256>
-class BulkPoolAllocator {
-public:
- BulkPoolAllocator() noexcept = default;
-
- // does not copy anything, just creates a new allocator.
- BulkPoolAllocator(const BulkPoolAllocator& ROBIN_HOOD_UNUSED(o) /*unused*/) noexcept
- : mHead(nullptr)
- , mListForFree(nullptr) {}
-
- BulkPoolAllocator(BulkPoolAllocator&& o) noexcept
- : mHead(o.mHead)
- , mListForFree(o.mListForFree) {
- o.mListForFree = nullptr;
- o.mHead = nullptr;
- }
-
- BulkPoolAllocator& operator=(BulkPoolAllocator&& o) noexcept {
- reset();
- mHead = o.mHead;
- mListForFree = o.mListForFree;
- o.mListForFree = nullptr;
- o.mHead = nullptr;
- return *this;
- }
-
- BulkPoolAllocator&
- // NOLINTNEXTLINE(bugprone-unhandled-self-assignment,cert-oop54-cpp)
- operator=(const BulkPoolAllocator& ROBIN_HOOD_UNUSED(o) /*unused*/) noexcept {
- // does not do anything
- return *this;
- }
-
- ~BulkPoolAllocator() noexcept {
- reset();
- }
-
- // Deallocates all allocated memory.
- void reset() noexcept {
- while (mListForFree) {
- T* tmp = *mListForFree;
- ROBIN_HOOD_LOG("std::free")
- std::free(mListForFree);
- mListForFree = reinterpret_cast_no_cast_align_warning<T**>(tmp);
- }
- mHead = nullptr;
- }
-
- // allocates, but does NOT initialize. Use in-place new constructor, e.g.
- // T* obj = pool.allocate();
- // ::new (static_cast<void*>(obj)) T();
- T* allocate() {
- T* tmp = mHead;
- if (!tmp) {
- tmp = performAllocation();
- }
-
- mHead = *reinterpret_cast_no_cast_align_warning<T**>(tmp);
- return tmp;
- }
-
- // does not actually deallocate but puts it in store.
- // make sure you have already called the destructor! e.g. with
- // obj->~T();
- // pool.deallocate(obj);
- void deallocate(T* obj) noexcept {
- *reinterpret_cast_no_cast_align_warning<T**>(obj) = mHead;
- mHead = obj;
- }
-
- // Adds an already allocated block of memory to the allocator. This allocator is from now on
- // responsible for freeing the data (with free()). If the provided data is not large enough to
- // make use of, it is immediately freed. Otherwise it is reused and freed in the destructor.
- void addOrFree(void* ptr, const size_t numBytes) noexcept {
- // calculate number of available elements in ptr
- if (numBytes < ALIGNMENT + ALIGNED_SIZE) {
- // not enough data for at least one element. Free and return.
- ROBIN_HOOD_LOG("std::free")
- std::free(ptr);
- } else {
- ROBIN_HOOD_LOG("add to buffer")
- add(ptr, numBytes);
- }
- }
-
- void swap(BulkPoolAllocator<T, MinNumAllocs, MaxNumAllocs>& other) noexcept {
- using std::swap;
- swap(mHead, other.mHead);
- swap(mListForFree, other.mListForFree);
- }
-
-private:
- // iterates the list of allocated memory to calculate how many to alloc next.
- // Recalculating this each time saves us a size_t member.
- // This ignores the fact that memory blocks might have been added manually with addOrFree. In
- // practice, this should not matter much.
- ROBIN_HOOD(NODISCARD) size_t calcNumElementsToAlloc() const noexcept {
- auto tmp = mListForFree;
- size_t numAllocs = MinNumAllocs;
-
- while (numAllocs * 2 <= MaxNumAllocs && tmp) {
- auto x = reinterpret_cast<T***>(tmp);
- tmp = *x;
- numAllocs *= 2;
- }
-
- return numAllocs;
- }
-
- // WARNING: Underflow if numBytes < ALIGNMENT! This is guarded in addOrFree().
- void add(void* ptr, const size_t numBytes) noexcept {
- const size_t numElements = (numBytes - ALIGNMENT) / ALIGNED_SIZE;
-
- auto data = reinterpret_cast<T**>(ptr);
-
- // link free list
- auto x = reinterpret_cast<T***>(data);
- *x = mListForFree;
- mListForFree = data;
-
- // create linked list for newly allocated data
- auto* const headT =
- reinterpret_cast_no_cast_align_warning<T*>(reinterpret_cast<char*>(ptr) + ALIGNMENT);
-
- auto* const head = reinterpret_cast<char*>(headT);
-
- // Visual Studio compiler automatically unrolls this loop, which is pretty cool
- for (size_t i = 0; i < numElements; ++i) {
- *reinterpret_cast_no_cast_align_warning<char**>(head + i * ALIGNED_SIZE) =
- head + (i + 1) * ALIGNED_SIZE;
- }
-
- // last one points to 0
- *reinterpret_cast_no_cast_align_warning<T**>(head + (numElements - 1) * ALIGNED_SIZE) =
- mHead;
- mHead = headT;
- }
-
- // Called when no memory is available (mHead == 0).
- // Don't inline this slow path.
- ROBIN_HOOD(NOINLINE) T* performAllocation() {
- size_t const numElementsToAlloc = calcNumElementsToAlloc();
-
- // alloc new memory: [prev |T, T, ... T]
- size_t const bytes = ALIGNMENT + ALIGNED_SIZE * numElementsToAlloc;
- ROBIN_HOOD_LOG("std::malloc " << bytes << " = " << ALIGNMENT << " + " << ALIGNED_SIZE
- << " * " << numElementsToAlloc)
- add(assertNotNull<std::bad_alloc>(std::malloc(bytes)), bytes);
- return mHead;
- }
-
- // enforce byte alignment of the T's
-#if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX14)
- static constexpr size_t ALIGNMENT =
- (std::max)(std::alignment_of<T>::value, std::alignment_of<T*>::value);
-#else
- static const size_t ALIGNMENT =
- (ROBIN_HOOD_STD::alignment_of<T>::value > ROBIN_HOOD_STD::alignment_of<T*>::value)
- ? ROBIN_HOOD_STD::alignment_of<T>::value
- : +ROBIN_HOOD_STD::alignment_of<T*>::value; // the + is for walkarround
-#endif
-
- static constexpr size_t ALIGNED_SIZE = ((sizeof(T) - 1) / ALIGNMENT + 1) * ALIGNMENT;
-
- static_assert(MinNumAllocs >= 1, "MinNumAllocs");
- static_assert(MaxNumAllocs >= MinNumAllocs, "MaxNumAllocs");
- static_assert(ALIGNED_SIZE >= sizeof(T*), "ALIGNED_SIZE");
- static_assert(0 == (ALIGNED_SIZE % sizeof(T*)), "ALIGNED_SIZE mod");
- static_assert(ALIGNMENT >= sizeof(T*), "ALIGNMENT");
-
- T* mHead{nullptr};
- T** mListForFree{nullptr};
-};
-
-template <typename T, size_t MinSize, size_t MaxSize, bool IsFlat>
-struct NodeAllocator;
-
-// dummy allocator that does nothing
-template <typename T, size_t MinSize, size_t MaxSize>
-struct NodeAllocator<T, MinSize, MaxSize, true> {
-
- // we are not using the data, so just free it.
- void addOrFree(void* ptr, size_t ROBIN_HOOD_UNUSED(numBytes) /*unused*/) noexcept {
- ROBIN_HOOD_LOG("std::free")
- std::free(ptr);
- }
-};
-
-template <typename T, size_t MinSize, size_t MaxSize>
-struct NodeAllocator<T, MinSize, MaxSize, false> : public BulkPoolAllocator<T, MinSize, MaxSize> {};
-
-// c++14 doesn't have is_nothrow_swappable, and clang++ 6.0.1 doesn't like it either, so I'm making
-// my own here.
-namespace swappable {
-#if ROBIN_HOOD(CXX) < ROBIN_HOOD(CXX17)
-using std::swap;
-template <typename T>
-struct nothrow {
- static const bool value = noexcept(swap(std::declval<T&>(), std::declval<T&>()));
-};
-#else
-template <typename T>
-struct nothrow {
- static const bool value = std::is_nothrow_swappable<T>::value;
-};
-#endif
-} // namespace swappable
-
-} // namespace detail
-
-struct is_transparent_tag {};
-
-// A custom pair implementation is used in the map because std::pair is not is_trivially_copyable,
-// which means it would not be allowed to be used in std::memcpy. This struct is copyable, which is
-// also tested.
-template <typename T1, typename T2>
-struct pair {
- using first_type = T1;
- using second_type = T2;
-
- template <typename U1 = T1, typename U2 = T2,
- typename = typename std::enable_if<std::is_default_constructible<U1>::value &&
- std::is_default_constructible<U2>::value>::type>
- constexpr pair() noexcept(noexcept(U1()) && noexcept(U2()))
- : first()
- , second() {}
-
- // pair constructors are explicit so we don't accidentally call this ctor when we don't have to.
- explicit constexpr pair(std::pair<T1, T2> const& o) noexcept(
- noexcept(T1(std::declval<T1 const&>())) && noexcept(T2(std::declval<T2 const&>())))
- : first(o.first)
- , second(o.second) {}
-
- // pair constructors are explicit so we don't accidentally call this ctor when we don't have to.
- explicit constexpr pair(std::pair<T1, T2>&& o) noexcept(noexcept(
- T1(std::move(std::declval<T1&&>()))) && noexcept(T2(std::move(std::declval<T2&&>()))))
- : first(std::move(o.first))
- , second(std::move(o.second)) {}
-
- constexpr pair(T1&& a, T2&& b) noexcept(noexcept(
- T1(std::move(std::declval<T1&&>()))) && noexcept(T2(std::move(std::declval<T2&&>()))))
- : first(std::move(a))
- , second(std::move(b)) {}
-
- template <typename U1, typename U2>
- constexpr pair(U1&& a, U2&& b) noexcept(noexcept(T1(std::forward<U1>(
- std::declval<U1&&>()))) && noexcept(T2(std::forward<U2>(std::declval<U2&&>()))))
- : first(std::forward<U1>(a))
- , second(std::forward<U2>(b)) {}
-
- template <typename... U1, typename... U2>
- // MSVC 2015 produces error "C2476: ‘constexpr’ constructor does not initialize all members"
- // if this constructor is constexpr
-#if !ROBIN_HOOD(BROKEN_CONSTEXPR)
- constexpr
-#endif
- pair(std::piecewise_construct_t /*unused*/, std::tuple<U1...> a,
- std::tuple<U2...>
- b) noexcept(noexcept(pair(std::declval<std::tuple<U1...>&>(),
- std::declval<std::tuple<U2...>&>(),
- ROBIN_HOOD_STD::index_sequence_for<U1...>(),
- ROBIN_HOOD_STD::index_sequence_for<U2...>())))
- : pair(a, b, ROBIN_HOOD_STD::index_sequence_for<U1...>(),
- ROBIN_HOOD_STD::index_sequence_for<U2...>()) {
- }
-
- // constructor called from the std::piecewise_construct_t ctor
- template <typename... U1, size_t... I1, typename... U2, size_t... I2>
- pair(std::tuple<U1...>& a, std::tuple<U2...>& b, ROBIN_HOOD_STD::index_sequence<I1...> /*unused*/, ROBIN_HOOD_STD::index_sequence<I2...> /*unused*/) noexcept(
- noexcept(T1(std::forward<U1>(std::get<I1>(
- std::declval<std::tuple<
- U1...>&>()))...)) && noexcept(T2(std::
- forward<U2>(std::get<I2>(
- std::declval<std::tuple<U2...>&>()))...)))
- : first(std::forward<U1>(std::get<I1>(a))...)
- , second(std::forward<U2>(std::get<I2>(b))...) {
- // make visual studio compiler happy about warning about unused a & b.
- // Visual studio's pair implementation disables warning 4100.
- (void)a;
- (void)b;
- }
-
- void swap(pair<T1, T2>& o) noexcept((detail::swappable::nothrow<T1>::value) &&
- (detail::swappable::nothrow<T2>::value)) {
- using std::swap;
- swap(first, o.first);
-