Skip to content

Commit

Permalink
Give basic_memory_buffer allocator [[no_unique_address]]
Browse files Browse the repository at this point in the history
This allows stateless allocators to take up no space while still avoiding the empty base class optimization.
  • Loading branch information
Minty-Meeo committed Jun 12, 2023
1 parent de0757b commit 96054f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
#define FMT_HAS_CPP17_ATTRIBUTE(attribute) \
(FMT_CPLUSPLUS >= 201703L && FMT_HAS_CPP_ATTRIBUTE(attribute))

#define FMT_HAS_CPP20_ATTRIBUTE(attribute) \
(FMT_CPLUSPLUS >= 202002L && FMT_HAS_CPP_ATTRIBUTE(attribute))

// Check if relaxed C++14 constexpr is supported.
// GCC doesn't allow throw in constexpr until version 6 (bug 67371).
#ifndef FMT_USE_CONSTEXPR
Expand Down Expand Up @@ -154,6 +157,16 @@
# endif
#endif

#ifndef FMT_NO_UNIQUE_ADDRESS
# if FMT_HAS_CPP20_ATTRIBUTE(no_unique_address)
# define FMT_NO_UNIQUE_ADDRESS [[no_unique_address]]
# elif FMT_MSC_VERSION >= 1929 && FMT_CPLUSPLUS >= 202002L // VS2019 v16.10 and later
# define FMT_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
# else
# define FMT_NO_UNIQUE_ADDRESS
# endif
#endif

#ifndef FMT_INLINE
# if FMT_GCC_VERSION || FMT_CLANG_VERSION
# define FMT_INLINE inline __attribute__((always_inline))
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ class basic_memory_buffer final : public detail::buffer<T> {
T store_[SIZE];

// Don't inherit from Allocator avoid generating type_info for it.
Allocator alloc_;
FMT_NO_UNIQUE_ADDRESS Allocator alloc_;

// Deallocate memory allocated by the buffer.
FMT_CONSTEXPR20 void deallocate() {
Expand Down

0 comments on commit 96054f4

Please sign in to comment.