From a86f1cec941ee7842a2ce1bce1cc4aa83eeb8d8d Mon Sep 17 00:00:00 2001 From: Andy Getzendanner Date: Tue, 17 Jan 2023 14:30:52 -0800 Subject: extern-ify NullGuard's "(null)" strings to save linker input bytes. PiperOrigin-RevId: 502689876 Change-Id: If75b00e2e257283b60c41411ef7a60dbd7cd8c6d --- absl/log/CMakeLists.txt | 2 ++ absl/log/internal/BUILD.bazel | 2 ++ absl/log/internal/nullguard.cc | 35 +++++++++++++++++++++++++++++++++++ absl/log/internal/nullguard.h | 15 ++++++++------- 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 absl/log/internal/nullguard.cc (limited to 'absl/log') diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt index 91bba002..fb1b59f5 100644 --- a/absl/log/CMakeLists.txt +++ b/absl/log/CMakeLists.txt @@ -249,6 +249,7 @@ absl_cc_library( NAME log_internal_nullguard SRCS + "internal/nullguard.cc" HDRS "internal/nullguard.h" COPTS @@ -257,6 +258,7 @@ absl_cc_library( ${ABSL_DEFAULT_LINKOPTS} DEPS absl::config + absl::core_headers ) absl_cc_library( diff --git a/absl/log/internal/BUILD.bazel b/absl/log/internal/BUILD.bazel index 08276887..a1f1a67c 100644 --- a/absl/log/internal/BUILD.bazel +++ b/absl/log/internal/BUILD.bazel @@ -226,11 +226,13 @@ cc_library( cc_library( name = "nullguard", + srcs = ["nullguard.cc"], hdrs = ["nullguard.h"], copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ "//absl/base:config", + "//absl/base:core_headers", ], ) diff --git a/absl/log/internal/nullguard.cc b/absl/log/internal/nullguard.cc new file mode 100644 index 00000000..7b785c55 --- /dev/null +++ b/absl/log/internal/nullguard.cc @@ -0,0 +1,35 @@ +// Copyright 2023 The Abseil Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/log/internal/nullguard.h" + +#include + +#include "absl/base/attributes.h" +#include "absl/base/config.h" + +namespace absl { +ABSL_NAMESPACE_BEGIN +namespace log_internal { + +ABSL_CONST_INIT const std::array kCharNull{ + {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; +ABSL_CONST_INIT const std::array kSignedCharNull{ + {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; +ABSL_CONST_INIT const std::array kUnsignedCharNull{ + {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; + +} // namespace log_internal +ABSL_NAMESPACE_END +} // namespace absl diff --git a/absl/log/internal/nullguard.h b/absl/log/internal/nullguard.h index 8ea38356..a0ed4ad7 100644 --- a/absl/log/internal/nullguard.h +++ b/absl/log/internal/nullguard.h @@ -27,26 +27,29 @@ #include #include +#include "absl/base/attributes.h" #include "absl/base/config.h" namespace absl { ABSL_NAMESPACE_BEGIN namespace log_internal { +ABSL_CONST_INIT extern const std::array kCharNull; +ABSL_CONST_INIT extern const std::array kSignedCharNull; +ABSL_CONST_INIT extern const std::array kUnsignedCharNull; + template struct NullGuard final { static const T& Guard(const T& v) { return v; } }; template <> struct NullGuard final { - static const char* Guard(const char* v) { return v ? v : "(null)"; } + static const char* Guard(const char* v) { return v ? v : kCharNull.data(); } }; template <> struct NullGuard final { - static const char* Guard(const char* v) { return v ? v : "(null)"; } + static const char* Guard(const char* v) { return v ? v : kCharNull.data(); } }; -constexpr std::array kSignedCharNull{ - {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; template <> struct NullGuard final { static const signed char* Guard(const signed char* v) { @@ -59,8 +62,6 @@ struct NullGuard final { return v ? v : kSignedCharNull.data(); } }; -constexpr std::array kUnsignedCharNull{ - {'(', 'n', 'u', 'l', 'l', ')', '\0'}}; template <> struct NullGuard final { static const unsigned char* Guard(const unsigned char* v) { @@ -75,7 +76,7 @@ struct NullGuard final { }; template <> struct NullGuard final { - static const char* Guard(const std::nullptr_t&) { return "(null)"; } + static const char* Guard(const std::nullptr_t&) { return kCharNull.data(); } }; } // namespace log_internal -- cgit v1.2.3