Add vendored dependencies & cleanup script
This commit is contained in:
parent
ea5ad06289
commit
f55d064a0d
|
@ -7,24 +7,6 @@
|
|||
/build
|
||||
/buildtools
|
||||
/testing
|
||||
/third_party/abseil-cpp/
|
||||
/third_party/angle
|
||||
/third_party/catapult
|
||||
/third_party/clang-format
|
||||
/third_party/glfw
|
||||
/third_party/googletest
|
||||
/third_party/gpuweb
|
||||
/third_party/jinja2
|
||||
/third_party/jsoncpp
|
||||
/third_party/llvm-build
|
||||
/third_party/markupsafe
|
||||
/third_party/node-addon-api
|
||||
/third_party/node-api-headers
|
||||
/third_party/swiftshader
|
||||
/third_party/tint
|
||||
/third_party/vulkan-deps
|
||||
/third_party/vulkan_memory_allocator
|
||||
/third_party/zlib
|
||||
/tools
|
||||
/out
|
||||
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
add-dependencies.sh & README.md originally from https://github.com/hexops/dawn
|
||||
|
||||
## Updating
|
||||
|
||||
Process for updating Dawn:
|
||||
|
||||
1. `git remote add upstream https://dawn.googlesource.com/dawn`
|
||||
2. `git checkout main && git checkout -B update-nov-30` (replace date with current date)
|
||||
3. `git fetch upstream && git merge upstream/main`
|
||||
4. `rm -rf out/ third_party/ build/ && git checkout upstream/main -- third_party/`
|
||||
5. `gclient sync` At this point there will be quite a large diff as many `third_party/` dependencies we commit will have changed.
|
||||
6. Stage and review the changes to the `third_party/` directory by running `./mach/add-dependencies.sh`. Alter the script to include/exclude files as needed, commit the result. Make sure test files, documentation, `.gitmodules`, etc. are not included.
|
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/env bash
|
||||
set -exuo pipefail
|
||||
|
||||
echo "Note: You should run gclient sync before this script"
|
||||
|
||||
# Remove unneeded dependencies
|
||||
rm -rf build
|
||||
# rm -rf build_overrides
|
||||
rm -rf buildtools
|
||||
# rm -rf infra
|
||||
rm -rf testing
|
||||
rm -rf third_party/angle
|
||||
rm -rf third_party/catapult
|
||||
rm -rf third_party/googletest
|
||||
rm -rf third_party/llvm-build
|
||||
rm -rf third_party/markupsafe
|
||||
rm -rf third_party/swiftshader
|
||||
rm -rf third_party/vulkan_memory_allocator
|
||||
rm -rf third_party/vulkan-deps/glslang
|
||||
rm -rf third_party/vulkan-deps/spirv-cross
|
||||
rm -rf third_party/vulkan-deps/vulkan-headers
|
||||
rm -rf third_party/vulkan-deps/vulkan-loader
|
||||
rm -rf third_party/vulkan-deps/vulkan-tools
|
||||
rm -rf third_party/vulkan-deps/vulkan-validation-layers
|
||||
rm -rf third_party/zlib
|
||||
|
||||
# Remove gitmodules, some third_party/ repositories contain these and leaving them around would
|
||||
# cause any recursive submodule clones to fail because e.g. some reference internal Google
|
||||
# repositories. We don't want them anyway.
|
||||
find third_party -type f -name .gitmodules -delete
|
||||
|
||||
# Turn subrepositories into regular folders.
|
||||
find third_party -depth -type d -name .git -exec rm -rf {} \;
|
||||
|
||||
# Remove files that are not needed.
|
||||
find third_party -depth -type d -name tests -print -exec rm -rf {} \;
|
||||
find third_party -depth -type d -name docs -print -exec rm -rf {} \;
|
||||
find third_party -depth -type d -name samples -print -exec rm -rf {} \;
|
||||
rm -rf third_party/tint/test/
|
||||
rm -rf third_party/swiftshader/third_party/SPIRV-Tools # already in third_party/vulkan-deps/spirv-tools
|
||||
rm -rf third_party/swiftshader/third_party/SPIRV-Headers # already in third_party/vulkan-deps/spirv-headers
|
||||
|
||||
# Remove gn/gni files
|
||||
# find . -type f -name '*.gn' -delete
|
||||
# find . -type f -name '*.gni' -delete
|
||||
|
||||
git add third_party/
|
||||
echo "you may now 'git commit -s -m 'update dependencies' if you are happy with the staged changes"
|
|
@ -0,0 +1,41 @@
|
|||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: 'bug'
|
||||
assignees: ''
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
|
||||
Include a clear and concise description of what the problem is, including what
|
||||
you expected to happen, and what actually happened.
|
||||
|
||||
**Steps to reproduce the bug**
|
||||
|
||||
It's important that we are able to reproduce the problem that you are
|
||||
experiencing. Please provide all code and relevant steps to reproduce the
|
||||
problem, including your `BUILD`/`CMakeLists.txt` file and build commands. Links
|
||||
to a GitHub branch or [godbolt.org](https://godbolt.org/) that demonstrate the
|
||||
problem are also helpful.
|
||||
|
||||
**What version of Abseil are you using?**
|
||||
|
||||
**What operating system and version are you using**
|
||||
|
||||
If you are using a Linux distribution please include the name and version of the
|
||||
distribution as well.
|
||||
|
||||
**What compiler and version are you using?**
|
||||
|
||||
Please include the output of `gcc -v` or `clang -v`, or the equivalent for your
|
||||
compiler.
|
||||
|
||||
**What build system are you using?**
|
||||
|
||||
Please include the output of `bazel --version` or `cmake --version`, or the
|
||||
equivalent for your build system.
|
||||
|
||||
**Additional context**
|
||||
|
||||
Add any other context about the problem here.
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
name: Question
|
||||
about: Have a question? Ask us anything! :-)
|
||||
title: ''
|
||||
labels: 'question'
|
||||
assignees: ''
|
||||
---
|
|
@ -0,0 +1 @@
|
|||
blank_issues_enabled: true
|
|
@ -0,0 +1,22 @@
|
|||
Please submit a new Abseil Issue using the template below:
|
||||
|
||||
## [Short title of proposed API change(s)]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
## Background
|
||||
|
||||
[Provide the background information that is required in order to evaluate the
|
||||
proposed API changes. No controversial claims should be made here. If there are
|
||||
design constraints that need to be considered, they should be presented here
|
||||
**along with justification for those constraints**. Linking to other docs is
|
||||
good, but please keep the **pertinent information as self contained** as
|
||||
possible in this section.]
|
||||
|
||||
## Proposed API Change (s)
|
||||
|
||||
[Please clearly describe the API change(s) being proposed. If multiple changes,
|
||||
please keep them clearly distinguished. When possible, **use example code
|
||||
snippets to illustrate before-after API usages**. List pros-n-cons. Highlight
|
||||
the main questions that you want to be answered. Given the Abseil project compatibility requirements, describe why the API change is safe.]
|
|
@ -0,0 +1,6 @@
|
|||
# This is the list of Abseil authors for copyright purposes.
|
||||
#
|
||||
# This does not necessarily list everyone who has contributed code, since in
|
||||
# some cases, their employer may be the copyright holder. To see the full list
|
||||
# of contributors, see the revision history in source control.
|
||||
Google Inc.
|
|
@ -0,0 +1,25 @@
|
|||
#
|
||||
# Copyright 2020 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.
|
||||
#
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"]) # Apache 2.0
|
||||
|
||||
# Expose license for external usage through bazel.
|
||||
exports_files([
|
||||
"AUTHORS",
|
||||
"LICENSE",
|
||||
])
|
|
@ -0,0 +1,545 @@
|
|||
include(CMakeParseArguments)
|
||||
include(GNUInstallDirs)
|
||||
|
||||
set(ABSL_INTERNAL_DLL_FILES
|
||||
"algorithm/algorithm.h"
|
||||
"algorithm/container.h"
|
||||
"base/attributes.h"
|
||||
"base/call_once.h"
|
||||
"base/casts.h"
|
||||
"base/config.h"
|
||||
"base/const_init.h"
|
||||
"base/dynamic_annotations.h"
|
||||
"base/internal/atomic_hook.h"
|
||||
"base/internal/cycleclock.cc"
|
||||
"base/internal/cycleclock.h"
|
||||
"base/internal/direct_mmap.h"
|
||||
"base/internal/dynamic_annotations.h"
|
||||
"base/internal/endian.h"
|
||||
"base/internal/errno_saver.h"
|
||||
"base/internal/exponential_biased.cc"
|
||||
"base/internal/exponential_biased.h"
|
||||
"base/internal/fast_type_id.h"
|
||||
"base/internal/hide_ptr.h"
|
||||
"base/internal/identity.h"
|
||||
"base/internal/invoke.h"
|
||||
"base/internal/inline_variable.h"
|
||||
"base/internal/low_level_alloc.cc"
|
||||
"base/internal/low_level_alloc.h"
|
||||
"base/internal/low_level_scheduling.h"
|
||||
"base/internal/per_thread_tls.h"
|
||||
"base/internal/periodic_sampler.cc"
|
||||
"base/internal/periodic_sampler.h"
|
||||
"base/internal/pretty_function.h"
|
||||
"base/internal/raw_logging.cc"
|
||||
"base/internal/raw_logging.h"
|
||||
"base/internal/scheduling_mode.h"
|
||||
"base/internal/scoped_set_env.cc"
|
||||
"base/internal/scoped_set_env.h"
|
||||
"base/internal/strerror.h"
|
||||
"base/internal/strerror.cc"
|
||||
"base/internal/spinlock.cc"
|
||||
"base/internal/spinlock.h"
|
||||
"base/internal/spinlock_wait.cc"
|
||||
"base/internal/spinlock_wait.h"
|
||||
"base/internal/sysinfo.cc"
|
||||
"base/internal/sysinfo.h"
|
||||
"base/internal/thread_annotations.h"
|
||||
"base/internal/thread_identity.cc"
|
||||
"base/internal/thread_identity.h"
|
||||
"base/internal/throw_delegate.cc"
|
||||
"base/internal/throw_delegate.h"
|
||||
"base/internal/tsan_mutex_interface.h"
|
||||
"base/internal/unaligned_access.h"
|
||||
"base/internal/unscaledcycleclock.cc"
|
||||
"base/internal/unscaledcycleclock.h"
|
||||
"base/log_severity.cc"
|
||||
"base/log_severity.h"
|
||||
"base/macros.h"
|
||||
"base/optimization.h"
|
||||
"base/options.h"
|
||||
"base/policy_checks.h"
|
||||
"base/port.h"
|
||||
"base/thread_annotations.h"
|
||||
"cleanup/cleanup.h"
|
||||
"cleanup/internal/cleanup.h"
|
||||
"container/btree_map.h"
|
||||
"container/btree_set.h"
|
||||
"container/fixed_array.h"
|
||||
"container/flat_hash_map.h"
|
||||
"container/flat_hash_set.h"
|
||||
"container/inlined_vector.h"
|
||||
"container/internal/btree.h"
|
||||
"container/internal/btree_container.h"
|
||||
"container/internal/common.h"
|
||||
"container/internal/compressed_tuple.h"
|
||||
"container/internal/container_memory.h"
|
||||
"container/internal/counting_allocator.h"
|
||||
"container/internal/hash_function_defaults.h"
|
||||
"container/internal/hash_policy_traits.h"
|
||||
"container/internal/hashtable_debug.h"
|
||||
"container/internal/hashtable_debug_hooks.h"
|
||||
"container/internal/hashtablez_sampler.cc"
|
||||
"container/internal/hashtablez_sampler.h"
|
||||
"container/internal/hashtablez_sampler_force_weak_definition.cc"
|
||||
"container/internal/have_sse.h"
|
||||
"container/internal/inlined_vector.h"
|
||||
"container/internal/layout.h"
|
||||
"container/internal/node_hash_policy.h"
|
||||
"container/internal/raw_hash_map.h"
|
||||
"container/internal/raw_hash_set.cc"
|
||||
"container/internal/raw_hash_set.h"
|
||||
"container/internal/tracked.h"
|
||||
"container/node_hash_map.h"
|
||||
"container/node_hash_set.h"
|
||||
"debugging/failure_signal_handler.cc"
|
||||
"debugging/failure_signal_handler.h"
|
||||
"debugging/leak_check.h"
|
||||
"debugging/leak_check_disable.cc"
|
||||
"debugging/stacktrace.cc"
|
||||
"debugging/stacktrace.h"
|
||||
"debugging/symbolize.cc"
|
||||
"debugging/symbolize.h"
|
||||
"debugging/internal/address_is_readable.cc"
|
||||
"debugging/internal/address_is_readable.h"
|
||||
"debugging/internal/demangle.cc"
|
||||
"debugging/internal/demangle.h"
|
||||
"debugging/internal/elf_mem_image.cc"
|
||||
"debugging/internal/elf_mem_image.h"
|
||||
"debugging/internal/examine_stack.cc"
|
||||
"debugging/internal/examine_stack.h"
|
||||
"debugging/internal/stack_consumption.cc"
|
||||
"debugging/internal/stack_consumption.h"
|
||||
"debugging/internal/stacktrace_config.h"
|
||||
"debugging/internal/symbolize.h"
|
||||
"debugging/internal/vdso_support.cc"
|
||||
"debugging/internal/vdso_support.h"
|
||||
"functional/internal/front_binder.h"
|
||||
"functional/bind_front.h"
|
||||
"functional/function_ref.h"
|
||||
"functional/internal/function_ref.h"
|
||||
"hash/hash.h"
|
||||
"hash/internal/city.h"
|
||||
"hash/internal/city.cc"
|
||||
"hash/internal/hash.h"
|
||||
"hash/internal/hash.cc"
|
||||
"hash/internal/spy_hash_state.h"
|
||||
"hash/internal/low_level_hash.h"
|
||||
"hash/internal/low_level_hash.cc"
|
||||
"memory/memory.h"
|
||||
"meta/type_traits.h"
|
||||
"numeric/bits.h"
|
||||
"numeric/int128.cc"
|
||||
"numeric/int128.h"
|
||||
"numeric/internal/bits.h"
|
||||
"numeric/internal/representation.h"
|
||||
"random/bernoulli_distribution.h"
|
||||
"random/beta_distribution.h"
|
||||
"random/bit_gen_ref.h"
|
||||
"random/discrete_distribution.cc"
|
||||
"random/discrete_distribution.h"
|
||||
"random/distributions.h"
|
||||
"random/exponential_distribution.h"
|
||||
"random/gaussian_distribution.cc"
|
||||
"random/gaussian_distribution.h"
|
||||
"random/internal/distribution_caller.h"
|
||||
"random/internal/fastmath.h"
|
||||
"random/internal/fast_uniform_bits.h"
|
||||
"random/internal/generate_real.h"
|
||||
"random/internal/iostream_state_saver.h"
|
||||
"random/internal/mock_helpers.h"
|
||||
"random/internal/nonsecure_base.h"
|
||||
"random/internal/pcg_engine.h"
|
||||
"random/internal/platform.h"
|
||||
"random/internal/pool_urbg.cc"
|
||||
"random/internal/pool_urbg.h"
|
||||
"random/internal/randen.cc"
|
||||
"random/internal/randen.h"
|
||||
"random/internal/randen_detect.cc"
|
||||
"random/internal/randen_detect.h"
|
||||
"random/internal/randen_engine.h"
|
||||
"random/internal/randen_hwaes.cc"
|
||||
"random/internal/randen_hwaes.h"
|
||||
"random/internal/randen_round_keys.cc"
|
||||
"random/internal/randen_slow.cc"
|
||||
"random/internal/randen_slow.h"
|
||||
"random/internal/randen_traits.h"
|
||||
"random/internal/salted_seed_seq.h"
|
||||
"random/internal/seed_material.cc"
|
||||
"random/internal/seed_material.h"
|
||||
"random/internal/sequence_urbg.h"
|
||||
"random/internal/traits.h"
|
||||
"random/internal/uniform_helper.h"
|
||||
"random/internal/wide_multiply.h"
|
||||
"random/log_uniform_int_distribution.h"
|
||||
"random/poisson_distribution.h"
|
||||
"random/random.h"
|
||||
"random/seed_gen_exception.cc"
|
||||
"random/seed_gen_exception.h"
|
||||
"random/seed_sequences.cc"
|
||||
"random/seed_sequences.h"
|
||||
"random/uniform_int_distribution.h"
|
||||
"random/uniform_real_distribution.h"
|
||||
"random/zipf_distribution.h"
|
||||
"status/internal/status_internal.h"
|
||||
"status/internal/statusor_internal.h"
|
||||
"status/status.h"
|
||||
"status/status.cc"
|
||||
"status/statusor.h"
|
||||
"status/statusor.cc"
|
||||
"status/status_payload_printer.h"
|
||||
"status/status_payload_printer.cc"
|
||||
"strings/ascii.cc"
|
||||
"strings/ascii.h"
|
||||
"strings/charconv.cc"
|
||||
"strings/charconv.h"
|
||||
"strings/cord.cc"
|
||||
"strings/cord.h"
|
||||
"strings/escaping.cc"
|
||||
"strings/escaping.h"
|
||||
"strings/internal/charconv_bigint.cc"
|
||||
"strings/internal/charconv_bigint.h"
|
||||
"strings/internal/charconv_parse.cc"
|
||||
"strings/internal/charconv_parse.h"
|
||||
"strings/internal/cord_internal.cc"
|
||||
"strings/internal/cord_internal.h"
|
||||
"strings/internal/cord_rep_consume.h"
|
||||
"strings/internal/cord_rep_consume.cc"
|
||||
"strings/internal/cord_rep_btree.cc"
|
||||
"strings/internal/cord_rep_btree.h"
|
||||
"strings/internal/cord_rep_btree_navigator.cc"
|
||||
"strings/internal/cord_rep_btree_navigator.h"
|
||||
"strings/internal/cord_rep_btree_reader.cc"
|
||||
"strings/internal/cord_rep_btree_reader.h"
|
||||
"strings/internal/cord_rep_flat.h"
|
||||
"strings/internal/cord_rep_ring.cc"
|
||||
"strings/internal/cord_rep_ring.h"
|
||||
"strings/internal/cord_rep_ring_reader.h"
|
||||
"strings/internal/cordz_functions.cc"
|
||||
"strings/internal/cordz_functions.h"
|
||||
"strings/internal/cordz_handle.cc"
|
||||
"strings/internal/cordz_handle.h"
|
||||
"strings/internal/cordz_info.cc"
|
||||
"strings/internal/cordz_info.h"
|
||||
"strings/internal/cordz_sample_token.cc"
|
||||
"strings/internal/cordz_sample_token.h"
|
||||
"strings/internal/cordz_statistics.h"
|
||||
"strings/internal/cordz_update_scope.h"
|
||||
"strings/internal/cordz_update_tracker.h"
|
||||
"strings/internal/stl_type_traits.h"
|
||||
"strings/internal/string_constant.h"
|
||||
"strings/match.cc"
|
||||
"strings/match.h"
|
||||
"strings/numbers.cc"
|
||||
"strings/numbers.h"
|
||||
"strings/str_format.h"
|
||||
"strings/str_cat.cc"
|
||||
"strings/str_cat.h"
|
||||
"strings/str_join.h"
|
||||
"strings/str_replace.cc"
|
||||
"strings/str_replace.h"
|
||||
"strings/str_split.cc"
|
||||
"strings/str_split.h"
|
||||
"strings/string_view.cc"
|
||||
"strings/string_view.h"
|
||||
"strings/strip.h"
|
||||
"strings/substitute.cc"
|
||||
"strings/substitute.h"
|
||||
"strings/internal/char_map.h"
|
||||
"strings/internal/escaping.h"
|
||||
"strings/internal/escaping.cc"
|
||||
"strings/internal/memutil.cc"
|
||||
"strings/internal/memutil.h"
|
||||
"strings/internal/ostringstream.cc"
|
||||
"strings/internal/ostringstream.h"
|
||||
"strings/internal/pow10_helper.cc"
|
||||
"strings/internal/pow10_helper.h"
|
||||
"strings/internal/resize_uninitialized.h"
|
||||
"strings/internal/str_format/arg.cc"
|
||||
"strings/internal/str_format/arg.h"
|
||||
"strings/internal/str_format/bind.cc"
|
||||
"strings/internal/str_format/bind.h"
|
||||
"strings/internal/str_format/checker.h"
|
||||
"strings/internal/str_format/extension.cc"
|
||||
"strings/internal/str_format/extension.h"
|
||||
"strings/internal/str_format/float_conversion.cc"
|
||||
"strings/internal/str_format/float_conversion.h"
|
||||
"strings/internal/str_format/output.cc"
|
||||
"strings/internal/str_format/output.h"
|
||||
"strings/internal/str_format/parser.cc"
|
||||
"strings/internal/str_format/parser.h"
|
||||
"strings/internal/str_join_internal.h"
|
||||
"strings/internal/str_split_internal.h"
|
||||
"strings/internal/utf8.cc"
|
||||
"strings/internal/utf8.h"
|
||||
"synchronization/barrier.cc"
|
||||
"synchronization/barrier.h"
|
||||
"synchronization/blocking_counter.cc"
|
||||
"synchronization/blocking_counter.h"
|
||||
"synchronization/mutex.cc"
|
||||
"synchronization/mutex.h"
|
||||
"synchronization/notification.cc"
|
||||
"synchronization/notification.h"
|
||||
"synchronization/internal/create_thread_identity.cc"
|
||||
"synchronization/internal/create_thread_identity.h"
|
||||
"synchronization/internal/futex.h"
|
||||
"synchronization/internal/graphcycles.cc"
|
||||
"synchronization/internal/graphcycles.h"
|
||||
"synchronization/internal/kernel_timeout.h"
|
||||
"synchronization/internal/per_thread_sem.cc"
|
||||
"synchronization/internal/per_thread_sem.h"
|
||||
"synchronization/internal/thread_pool.h"
|
||||
"synchronization/internal/waiter.cc"
|
||||
"synchronization/internal/waiter.h"
|
||||
"time/civil_time.cc"
|
||||
"time/civil_time.h"
|
||||
"time/clock.cc"
|
||||
"time/clock.h"
|
||||
"time/duration.cc"
|
||||
"time/format.cc"
|
||||
"time/time.cc"
|
||||
"time/time.h"
|
||||
"time/internal/cctz/include/cctz/civil_time.h"
|
||||
"time/internal/cctz/include/cctz/civil_time_detail.h"
|
||||
"time/internal/cctz/include/cctz/time_zone.h"
|
||||
"time/internal/cctz/include/cctz/zone_info_source.h"
|
||||
"time/internal/cctz/src/civil_time_detail.cc"
|
||||
"time/internal/cctz/src/time_zone_fixed.cc"
|
||||
"time/internal/cctz/src/time_zone_fixed.h"
|
||||
"time/internal/cctz/src/time_zone_format.cc"
|
||||
"time/internal/cctz/src/time_zone_if.cc"
|
||||
"time/internal/cctz/src/time_zone_if.h"
|
||||
"time/internal/cctz/src/time_zone_impl.cc"
|
||||
"time/internal/cctz/src/time_zone_impl.h"
|
||||
"time/internal/cctz/src/time_zone_info.cc"
|
||||
"time/internal/cctz/src/time_zone_info.h"
|
||||
"time/internal/cctz/src/time_zone_libc.cc"
|
||||
"time/internal/cctz/src/time_zone_libc.h"
|
||||
"time/internal/cctz/src/time_zone_lookup.cc"
|
||||
"time/internal/cctz/src/time_zone_posix.cc"
|
||||
"time/internal/cctz/src/time_zone_posix.h"
|
||||
"time/internal/cctz/src/tzfile.h"
|
||||
"time/internal/cctz/src/zone_info_source.cc"
|
||||
"types/any.h"
|
||||
"types/bad_any_cast.cc"
|
||||
"types/bad_any_cast.h"
|
||||
"types/bad_optional_access.cc"
|
||||
"types/bad_optional_access.h"
|
||||
"types/bad_variant_access.cc"
|
||||
"types/bad_variant_access.h"
|
||||
"types/compare.h"
|
||||
"types/internal/conformance_aliases.h"
|
||||
"types/internal/conformance_archetype.h"
|
||||
"types/internal/conformance_profile.h"
|
||||
"types/internal/parentheses.h"
|
||||
"types/internal/transform_args.h"
|
||||
"types/internal/variant.h"
|
||||
"types/optional.h"
|
||||
"types/internal/optional.h"
|
||||
"types/span.h"
|
||||
"types/internal/span.h"
|
||||
"types/variant.h"
|
||||
"utility/utility.h"
|
||||
)
|
||||
|
||||
set(ABSL_INTERNAL_DLL_TARGETS
|
||||
"stacktrace"
|
||||
"symbolize"
|
||||
"examine_stack"
|
||||
"failure_signal_handler"
|
||||
"debugging_internal"
|
||||
"demangle_internal"
|
||||
"leak_check"
|
||||
"leak_check_disable"
|
||||
"stack_consumption"
|
||||
"debugging"
|
||||
"hash"
|
||||
"spy_hash_state"
|
||||
"city"
|
||||
"memory"
|
||||
"strings"
|
||||
"strings_internal"
|
||||
"cord"
|
||||
"str_format"
|
||||
"str_format_internal"
|
||||
"pow10_helper"
|
||||
"int128"
|
||||
"numeric"
|
||||
"utility"
|
||||
"any"
|
||||
"bad_any_cast"
|
||||
"bad_any_cast_impl"
|
||||
"span"
|
||||
"optional"
|
||||
"bad_optional_access"
|
||||
"bad_variant_access"
|
||||
"variant"
|
||||
"compare"
|
||||
"algorithm"
|
||||
"algorithm_container"
|
||||
"graphcycles_internal"
|
||||
"kernel_timeout_internal"
|
||||
"synchronization"
|
||||
"thread_pool"
|
||||
"bind_front"
|
||||
"function_ref"
|
||||
"atomic_hook"
|
||||
"log_severity"
|
||||
"raw_logging_internal"
|
||||
"spinlock_wait"
|
||||
"config"
|
||||
"dynamic_annotations"
|
||||
"core_headers"
|
||||
"malloc_internal"
|
||||
"base_internal"
|
||||
"base"
|
||||
"throw_delegate"
|
||||
"pretty_function"
|
||||
"endian"
|
||||
"bits"
|
||||
"exponential_biased"
|
||||
"periodic_sampler"
|
||||
"scoped_set_env"
|
||||
"type_traits"
|
||||
"meta"
|
||||
"random_random"
|
||||
"random_bit_gen_ref"
|
||||
"random_distributions"
|
||||
"random_seed_gen_exception"
|
||||
"random_seed_sequences"
|
||||
"random_internal_traits"
|
||||
"random_internal_distribution_caller"
|
||||
"random_internal_distributions"
|
||||
"random_internal_fast_uniform_bits"
|
||||
"random_internal_seed_material"
|
||||
"random_internal_pool_urbg"
|
||||
"random_internal_explicit_seed_seq"
|
||||
"random_internal_sequence_urbg"
|
||||
"random_internal_salted_seed_seq"
|
||||
"random_internal_iostream_state_saver"
|
||||
"random_internal_generate_real"
|
||||
"random_internal_wide_multiply"
|
||||
"random_internal_fastmath"
|
||||
"random_internal_nonsecure_base"
|
||||
"random_internal_pcg_engine"
|
||||
"random_internal_randen_engine"
|
||||
"random_internal_platform"
|
||||
"random_internal_randen"
|
||||
"random_internal_randen_slow"
|
||||
"random_internal_randen_hwaes"
|
||||
"random_internal_randen_hwaes_impl"
|
||||
"random_internal_uniform_helper"
|
||||
"status"
|
||||
"time"
|
||||
"civil_time"
|
||||
"time_zone"
|
||||
"container"
|
||||
"btree"
|
||||
"compressed_tuple"
|
||||
"fixed_array"
|
||||
"inlined_vector_internal"
|
||||
"inlined_vector"
|
||||
"counting_allocator"
|
||||
"flat_hash_map"
|
||||
"flat_hash_set"
|
||||
"node_hash_map"
|
||||
"node_hash_set"
|
||||
"container_memory"
|
||||
"hash_function_defaults"
|
||||
"hash_policy_traits"
|
||||
"hashtablez_sampler"
|
||||
"hashtable_debug"
|
||||
"hashtable_debug_hooks"
|
||||
"have_sse"
|
||||
"node_hash_policy"
|
||||
"raw_hash_map"
|
||||
"container_common"
|
||||
"raw_hash_set"
|
||||
"layout"
|
||||
"tracked"
|
||||
)
|
||||
|
||||
function(absl_internal_dll_contains)
|
||||
cmake_parse_arguments(ABSL_INTERNAL_DLL
|
||||
""
|
||||
"OUTPUT;TARGET"
|
||||
""
|
||||
${ARGN}
|
||||
)
|
||||
|
||||
STRING(REGEX REPLACE "^absl::" "" _target ${ABSL_INTERNAL_DLL_TARGET})
|
||||
|
||||
list(FIND
|
||||
ABSL_INTERNAL_DLL_TARGETS
|
||||
"${_target}"
|
||||
_index)
|
||||
|
||||
if (${_index} GREATER -1)
|
||||
set(${ABSL_INTERNAL_DLL_OUTPUT} 1 PARENT_SCOPE)
|
||||
else()
|
||||
set(${ABSL_INTERNAL_DLL_OUTPUT} 0 PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(absl_internal_dll_targets)
|
||||
cmake_parse_arguments(ABSL_INTERNAL_DLL
|
||||
""
|
||||
"OUTPUT"
|
||||
"DEPS"
|
||||
${ARGN}
|
||||
)
|
||||
|
||||
set(_deps "")
|
||||
foreach(dep IN LISTS ABSL_INTERNAL_DLL_DEPS)
|
||||
absl_internal_dll_contains(TARGET ${dep} OUTPUT _contains)
|
||||
if (_contains)
|
||||
list(APPEND _deps abseil_dll)
|
||||
else()
|
||||
list(APPEND _deps ${dep})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# Because we may have added the DLL multiple times
|
||||
list(REMOVE_DUPLICATES _deps)
|
||||
set(${ABSL_INTERNAL_DLL_OUTPUT} "${_deps}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
function(absl_make_dll)
|
||||
add_library(
|
||||
abseil_dll
|
||||
SHARED
|
||||
"${ABSL_INTERNAL_DLL_FILES}"
|
||||
)
|
||||
target_link_libraries(
|
||||
abseil_dll
|
||||
PRIVATE
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
)
|
||||
set_property(TARGET abseil_dll PROPERTY LINKER_LANGUAGE "CXX")
|
||||
target_include_directories(
|
||||
abseil_dll
|
||||
PUBLIC
|
||||
"$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>"
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
target_compile_options(
|
||||
abseil_dll
|
||||
PRIVATE
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
)
|
||||
|
||||
target_compile_definitions(
|
||||
abseil_dll
|
||||
PRIVATE
|
||||
ABSL_BUILD_DLL
|
||||
NOMINMAX
|
||||
INTERFACE
|
||||
${ABSL_CC_LIB_DEFINES}
|
||||
)
|
||||
install(TARGETS abseil_dll EXPORT ${PROJECT_NAME}Targets
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
endfunction()
|
|
@ -0,0 +1,444 @@
|
|||
#
|
||||
# Copyright 2017 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(CMakeParseArguments)
|
||||
include(AbseilConfigureCopts)
|
||||
include(AbseilDll)
|
||||
|
||||
# The IDE folder for Abseil that will be used if Abseil is included in a CMake
|
||||
# project that sets
|
||||
# set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
||||
# For example, Visual Studio supports folders.
|
||||
if(NOT DEFINED ABSL_IDE_FOLDER)
|
||||
set(ABSL_IDE_FOLDER Abseil)
|
||||
endif()
|
||||
|
||||
# absl_cc_library()
|
||||
#
|
||||
# CMake function to imitate Bazel's cc_library rule.
|
||||
#
|
||||
# Parameters:
|
||||
# NAME: name of target (see Note)
|
||||
# HDRS: List of public header files for the library
|
||||
# SRCS: List of source files for the library
|
||||
# DEPS: List of other libraries to be linked in to the binary targets
|
||||
# COPTS: List of private compile options
|
||||
# DEFINES: List of public defines
|
||||
# LINKOPTS: List of link options
|
||||
# PUBLIC: Add this so that this library will be exported under absl::
|
||||
# Also in IDE, target will appear in Abseil folder while non PUBLIC will be in Abseil/internal.
|
||||
# TESTONLY: When added, this target will only be built if BUILD_TESTING=ON.
|
||||
#
|
||||
# Note:
|
||||
# By default, absl_cc_library will always create a library named absl_${NAME},
|
||||
# and alias target absl::${NAME}. The absl:: form should always be used.
|
||||
# This is to reduce namespace pollution.
|
||||
#
|
||||
# absl_cc_library(
|
||||
# NAME
|
||||
# awesome
|
||||
# HDRS
|
||||
# "a.h"
|
||||
# SRCS
|
||||
# "a.cc"
|
||||
# )
|
||||
# absl_cc_library(
|
||||
# NAME
|
||||
# fantastic_lib
|
||||
# SRCS
|
||||
# "b.cc"
|
||||
# DEPS
|
||||
# absl::awesome # not "awesome" !
|
||||
# PUBLIC
|
||||
# )
|
||||
#
|
||||
# absl_cc_library(
|
||||
# NAME
|
||||
# main_lib
|
||||
# ...
|
||||
# DEPS
|
||||
# absl::fantastic_lib
|
||||
# )
|
||||
#
|
||||
# TODO: Implement "ALWAYSLINK"
|
||||
function(absl_cc_library)
|
||||
cmake_parse_arguments(ABSL_CC_LIB
|
||||
"DISABLE_INSTALL;PUBLIC;TESTONLY"
|
||||
"NAME"
|
||||
"HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS"
|
||||
${ARGN}
|
||||
)
|
||||
|
||||
if(ABSL_CC_LIB_TESTONLY AND NOT BUILD_TESTING)
|
||||
return()
|
||||
endif()
|
||||
|
||||
if(ABSL_ENABLE_INSTALL)
|
||||
set(_NAME "${ABSL_CC_LIB_NAME}")
|
||||
else()
|
||||
set(_NAME "absl_${ABSL_CC_LIB_NAME}")
|
||||
endif()
|
||||
|
||||
# Check if this is a header-only library
|
||||
# Note that as of February 2019, many popular OS's (for example, Ubuntu
|
||||
# 16.04 LTS) only come with cmake 3.5 by default. For this reason, we can't
|
||||
# use list(FILTER...)
|
||||
set(ABSL_CC_SRCS "${ABSL_CC_LIB_SRCS}")
|
||||
foreach(src_file IN LISTS ABSL_CC_SRCS)
|
||||
if(${src_file} MATCHES ".*\\.(h|inc)")
|
||||
list(REMOVE_ITEM ABSL_CC_SRCS "${src_file}")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(ABSL_CC_SRCS STREQUAL "")
|
||||
set(ABSL_CC_LIB_IS_INTERFACE 1)
|
||||
else()
|
||||
set(ABSL_CC_LIB_IS_INTERFACE 0)
|
||||
endif()
|
||||
|
||||
# Determine this build target's relationship to the DLL. It's one of four things:
|
||||
# 1. "dll" -- This target is part of the DLL
|
||||
# 2. "dll_dep" -- This target is not part of the DLL, but depends on the DLL.
|
||||
# Note that we assume any target not in the DLL depends on the
|
||||
# DLL. This is not a technical necessity but a convenience
|
||||
# which happens to be true, because nearly every target is
|
||||
# part of the DLL.
|
||||
# 3. "shared" -- This is a shared library, perhaps on a non-windows platform
|
||||
# where DLL doesn't make sense.
|
||||
# 4. "static" -- This target does not depend on the DLL and should be built
|
||||
# statically.
|
||||
if (${ABSL_BUILD_DLL})
|
||||
if(ABSL_ENABLE_INSTALL)
|
||||
absl_internal_dll_contains(TARGET ${_NAME} OUTPUT _in_dll)
|
||||
else()
|
||||
absl_internal_dll_contains(TARGET ${ABSL_CC_LIB_NAME} OUTPUT _in_dll)
|
||||
endif()
|
||||
if (${_in_dll})
|
||||
# This target should be replaced by the DLL
|
||||
set(_build_type "dll")
|
||||
set(ABSL_CC_LIB_IS_INTERFACE 1)
|
||||
else()
|
||||
# Building a DLL, but this target is not part of the DLL
|
||||
set(_build_type "dll_dep")
|
||||
endif()
|
||||
elseif(BUILD_SHARED_LIBS)
|
||||
set(_build_type "shared")
|
||||
else()
|
||||
set(_build_type "static")
|
||||
endif()
|
||||
|
||||
# Generate a pkg-config file for every library:
|
||||
if((_build_type STREQUAL "static" OR _build_type STREQUAL "shared")
|
||||
AND ABSL_ENABLE_INSTALL)
|
||||
if(NOT ABSL_CC_LIB_TESTONLY)
|
||||
if(absl_VERSION)
|
||||
set(PC_VERSION "${absl_VERSION}")
|
||||
else()
|
||||
set(PC_VERSION "head")
|
||||
endif()
|
||||
foreach(dep ${ABSL_CC_LIB_DEPS})
|
||||
if(${dep} MATCHES "^absl::(.*)")
|
||||
# Join deps with commas.
|
||||
if(PC_DEPS)
|
||||
set(PC_DEPS "${PC_DEPS},")
|
||||
endif()
|
||||
set(PC_DEPS "${PC_DEPS} absl_${CMAKE_MATCH_1} = ${PC_VERSION}")
|
||||
endif()
|
||||
endforeach()
|
||||
foreach(cflag ${ABSL_CC_LIB_COPTS})
|
||||
if(${cflag} MATCHES "^(-Wno|/wd)")
|
||||
# These flags are needed to suppress warnings that might fire in our headers.
|
||||
set(PC_CFLAGS "${PC_CFLAGS} ${cflag}")
|
||||
elseif(${cflag} MATCHES "^(-W|/w[1234eo])")
|
||||
# Don't impose our warnings on others.
|
||||
else()
|
||||
set(PC_CFLAGS "${PC_CFLAGS} ${cflag}")
|
||||
endif()
|
||||
endforeach()
|
||||
FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc" CONTENT "\
|
||||
prefix=${CMAKE_INSTALL_PREFIX}\n\
|
||||
exec_prefix=\${prefix}\n\
|
||||
libdir=${CMAKE_INSTALL_FULL_LIBDIR}\n\
|
||||
includedir=${CMAKE_INSTALL_FULL_INCLUDEDIR}\n\
|
||||
\n\
|
||||
Name: absl_${_NAME}\n\
|
||||
Description: Abseil ${_NAME} library\n\
|
||||
URL: https://abseil.io/\n\
|
||||
Version: ${PC_VERSION}\n\
|
||||
Requires:${PC_DEPS}\n\
|
||||
Libs: -L\${libdir} $<JOIN:${ABSL_CC_LIB_LINKOPTS}, > $<$<NOT:$<BOOL:${ABSL_CC_LIB_IS_INTERFACE}>>:-labsl_${_NAME}>\n\
|
||||
Cflags: -I\${includedir}${PC_CFLAGS}\n")
|
||||
INSTALL(FILES "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT ABSL_CC_LIB_IS_INTERFACE)
|
||||
if(_build_type STREQUAL "dll_dep")
|
||||
# This target depends on the DLL. When adding dependencies to this target,
|
||||
# any depended-on-target which is contained inside the DLL is replaced
|
||||
# with a dependency on the DLL.
|
||||
add_library(${_NAME} STATIC "")
|
||||
target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS})
|
||||
absl_internal_dll_targets(
|
||||
DEPS ${ABSL_CC_LIB_DEPS}
|
||||
OUTPUT _dll_deps
|
||||
)
|
||||
target_link_libraries(${_NAME}
|
||||
PUBLIC ${_dll_deps}
|
||||
PRIVATE
|
||||
${ABSL_CC_LIB_LINKOPTS}
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
)
|
||||
|
||||
if (ABSL_CC_LIB_TESTONLY)
|
||||
set(_gtest_link_define "GTEST_LINKED_AS_SHARED_LIBRARY=1")
|
||||
else()
|
||||
set(_gtest_link_define)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(${_NAME}
|
||||
PUBLIC
|
||||
ABSL_CONSUME_DLL
|
||||
"${_gtest_link_define}"
|
||||
)
|
||||
|
||||
elseif(_build_type STREQUAL "static" OR _build_type STREQUAL "shared")
|
||||
add_library(${_NAME} "")
|
||||
target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS})
|
||||
target_link_libraries(${_NAME}
|
||||
PUBLIC ${ABSL_CC_LIB_DEPS}
|
||||
PRIVATE
|
||||
${ABSL_CC_LIB_LINKOPTS}
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
)
|
||||
else()
|
||||
message(FATAL_ERROR "Invalid build type: ${_build_type}")
|
||||
endif()
|
||||
|
||||
# Linker language can be inferred from sources, but in the case of DLLs we
|
||||
# don't have any .cc files so it would be ambiguous. We could set it
|
||||
# explicitly only in the case of DLLs but, because "CXX" is always the
|
||||
# correct linker language for static or for shared libraries, we set it
|
||||
# unconditionally.
|
||||
set_property(TARGET ${_NAME} PROPERTY LINKER_LANGUAGE "CXX")
|
||||
|
||||
target_include_directories(${_NAME}
|
||||
PUBLIC
|
||||
"$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>"
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
target_compile_options(${_NAME}
|
||||
PRIVATE ${ABSL_CC_LIB_COPTS})
|
||||
target_compile_definitions(${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES})
|
||||
|
||||
# Add all Abseil targets to a a folder in the IDE for organization.
|
||||
if(ABSL_CC_LIB_PUBLIC)
|
||||
set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER})
|
||||
elseif(ABSL_CC_LIB_TESTONLY)
|
||||
set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test)
|
||||
else()
|
||||
set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/internal)
|
||||
endif()
|
||||
|
||||
if(ABSL_PROPAGATE_CXX_STD)
|
||||
# Abseil libraries require C++11 as the current minimum standard.
|
||||
# Top-level application CMake projects should ensure a consistent C++
|
||||
# standard for all compiled sources by setting CMAKE_CXX_STANDARD.
|
||||
target_compile_features(${_NAME} PUBLIC cxx_std_11)
|
||||
else()
|
||||
# Note: This is legacy (before CMake 3.8) behavior. Setting the
|
||||
# target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
|
||||
# initialized by CMAKE_CXX_STANDARD) should have no real effect, since
|
||||
# that is the default value anyway.
|
||||
#
|
||||
# CXX_STANDARD_REQUIRED does guard against the top-level CMake project
|
||||
# not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents
|
||||
# "decaying" to an older standard if the requested one isn't available).
|
||||
set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD})
|
||||
set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
endif()
|
||||
|
||||
# When being installed, we lose the absl_ prefix. We want to put it back
|
||||
# to have properly named lib files. This is a no-op when we are not being
|
||||
# installed.
|
||||
if(ABSL_ENABLE_INSTALL)
|
||||
set_target_properties(${_NAME} PROPERTIES
|
||||
OUTPUT_NAME "absl_${_NAME}"
|
||||
SOVERSION 0
|
||||
)
|
||||
endif()
|
||||
else()
|
||||
# Generating header-only library
|
||||
add_library(${_NAME} INTERFACE)
|
||||
target_include_directories(${_NAME}
|
||||
INTERFACE
|
||||
"$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>"
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
if (_build_type STREQUAL "dll")
|
||||
set(ABSL_CC_LIB_DEPS abseil_dll)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${_NAME}
|
||||
INTERFACE
|
||||
${ABSL_CC_LIB_DEPS}
|
||||
${ABSL_CC_LIB_LINKOPTS}
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
)
|
||||
target_compile_definitions(${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES})
|
||||
|
||||
if(ABSL_PROPAGATE_CXX_STD)
|
||||
# Abseil libraries require C++11 as the current minimum standard.
|
||||
# Top-level application CMake projects should ensure a consistent C++
|
||||
# standard for all compiled sources by setting CMAKE_CXX_STANDARD.
|
||||
target_compile_features(${_NAME} INTERFACE cxx_std_11)
|
||||
|
||||
# (INTERFACE libraries can't have the CXX_STANDARD property set, so there
|
||||
# is no legacy behavior else case).
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# TODO currently we don't install googletest alongside abseil sources, so
|
||||
# installed abseil can't be tested.
|
||||
if(NOT ABSL_CC_LIB_TESTONLY AND ABSL_ENABLE_INSTALL)
|
||||
install(TARGETS ${_NAME} EXPORT ${PROJECT_NAME}Targets
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
endif()
|
||||
|
||||
add_library(absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME})
|
||||
endfunction()
|
||||
|
||||
# absl_cc_test()
|
||||
#
|
||||
# CMake function to imitate Bazel's cc_test rule.
|
||||
#
|
||||
# Parameters:
|
||||
# NAME: name of target (see Usage below)
|
||||
# SRCS: List of source files for the binary
|
||||
# DEPS: List of other libraries to be linked in to the binary targets
|
||||
# COPTS: List of private compile options
|
||||
# DEFINES: List of public defines
|
||||
# LINKOPTS: List of link options
|
||||
#
|
||||
# Note:
|
||||
# By default, absl_cc_test will always create a binary named absl_${NAME}.
|
||||
# This will also add it to ctest list as absl_${NAME}.
|
||||
#
|
||||
# Usage:
|
||||
# absl_cc_library(
|
||||
# NAME
|
||||
# awesome
|
||||
# HDRS
|
||||
# "a.h"
|
||||
# SRCS
|
||||
# "a.cc"
|
||||
# PUBLIC
|
||||
# )
|
||||
#
|
||||
# absl_cc_test(
|
||||
# NAME
|
||||
# awesome_test
|
||||
# SRCS
|
||||
# "awesome_test.cc"
|
||||
# DEPS
|
||||
# absl::awesome
|
||||
# GTest::gmock
|
||||
# GTest::gtest_main
|
||||
# )
|
||||
function(absl_cc_test)
|
||||
if(NOT BUILD_TESTING)
|
||||
return()
|
||||
endif()
|
||||
|
||||
cmake_parse_arguments(ABSL_CC_TEST
|
||||
""
|
||||
"NAME"
|
||||
"SRCS;COPTS;DEFINES;LINKOPTS;DEPS"
|
||||
${ARGN}
|
||||
)
|
||||
|
||||
set(_NAME "absl_${ABSL_CC_TEST_NAME}")
|
||||
|
||||
add_executable(${_NAME} "")
|
||||
target_sources(${_NAME} PRIVATE ${ABSL_CC_TEST_SRCS})
|
||||
target_include_directories(${_NAME}
|
||||
PUBLIC ${ABSL_COMMON_INCLUDE_DIRS}
|
||||
PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
if (${ABSL_BUILD_DLL})
|
||||
target_compile_definitions(${_NAME}
|
||||
PUBLIC
|
||||
${ABSL_CC_TEST_DEFINES}
|
||||
ABSL_CONSUME_DLL
|
||||
GTEST_LINKED_AS_SHARED_LIBRARY=1
|
||||
)
|
||||
|
||||
# Replace dependencies on targets inside the DLL with abseil_dll itself.
|
||||
absl_internal_dll_targets(
|
||||
DEPS ${ABSL_CC_TEST_DEPS}
|
||||
OUTPUT ABSL_CC_TEST_DEPS
|
||||
)
|
||||
else()
|
||||
target_compile_definitions(${_NAME}
|
||||
PUBLIC
|
||||
${ABSL_CC_TEST_DEFINES}
|
||||
)
|
||||
endif()
|
||||
target_compile_options(${_NAME}
|
||||
PRIVATE ${ABSL_CC_TEST_COPTS}
|
||||
)
|
||||
|
||||
target_link_libraries(${_NAME}
|
||||
PUBLIC ${ABSL_CC_TEST_DEPS}
|
||||
PRIVATE ${ABSL_CC_TEST_LINKOPTS}
|
||||
)
|
||||
# Add all Abseil targets to a folder in the IDE for organization.
|
||||
set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test)
|
||||
|
||||
if(ABSL_PROPAGATE_CXX_STD)
|
||||
# Abseil libraries require C++11 as the current minimum standard.
|
||||
# Top-level application CMake projects should ensure a consistent C++
|
||||
# standard for all compiled sources by setting CMAKE_CXX_STANDARD.
|
||||
target_compile_features(${_NAME} PUBLIC cxx_std_11)
|
||||
else()
|
||||
# Note: This is legacy (before CMake 3.8) behavior. Setting the
|
||||
# target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
|
||||
# initialized by CMAKE_CXX_STANDARD) should have no real effect, since
|
||||
# that is the default value anyway.
|
||||
#
|
||||
# CXX_STANDARD_REQUIRED does guard against the top-level CMake project
|
||||
# not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents
|
||||
# "decaying" to an older standard if the requested one isn't available).
|
||||
set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD})
|
||||
set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
endif()
|
||||
|
||||
add_test(NAME ${_NAME} COMMAND ${_NAME})
|
||||
endfunction()
|
||||
|
||||
|
||||
function(check_target my_target)
|
||||
if(NOT TARGET ${my_target})
|
||||
message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project,
|
||||
see CMake/README.md for more details")
|
||||
endif(NOT TARGET ${my_target})
|
||||
endfunction()
|
|
@ -0,0 +1,14 @@
|
|||
cmake_minimum_required(VERSION 2.8.2)
|
||||
|
||||
project(googletest-external NONE)
|
||||
|
||||
include(ExternalProject)
|
||||
ExternalProject_Add(googletest
|
||||
URL "${absl_gtest_download_url}" # May be empty
|
||||
SOURCE_DIR "${absl_gtest_src_dir}"
|
||||
BINARY_DIR "${absl_gtest_build_dir}"
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
)
|
|
@ -0,0 +1,41 @@
|
|||
# Integrates googletest at configure time. Based on the instructions at
|
||||
# https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project
|
||||
|
||||
# Set up the external googletest project, downloading the latest from Github
|
||||
# master if requested.
|
||||
configure_file(
|
||||
${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in
|
||||
${CMAKE_BINARY_DIR}/googletest-external/CMakeLists.txt
|
||||
)
|
||||
|
||||
set(ABSL_SAVE_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
|
||||
set(ABSL_SAVE_CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
|
||||
if (BUILD_SHARED_LIBS)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_CREATE_SHARED_LIBRARY=1")
|
||||
endif()
|
||||
|
||||
# Configure and build the googletest source.
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-external )
|
||||
if(result)
|
||||
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
|
||||
endif()
|
||||
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} --build .
|
||||
RESULT_VARIABLE result
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-external)
|
||||
if(result)
|
||||
message(FATAL_ERROR "Build step for googletest failed: ${result}")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS ${ABSL_SAVE_CMAKE_CXX_FLAGS})
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ABSL_SAVE_CMAKE_RUNTIME_OUTPUT_DIRECTORY})
|
||||
|
||||
# Prevent overriding the parent project's compiler/linker settings on Windows
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
|
||||
# Add googletest directly to our build. This defines the gtest and gtest_main
|
||||
# targets.
|
||||
add_subdirectory(${absl_gtest_src_dir} ${absl_gtest_build_dir} EXCLUDE_FROM_ALL)
|
|
@ -0,0 +1,185 @@
|
|||
# Abseil CMake Build Instructions
|
||||
|
||||
Abseil comes with a CMake build script ([CMakeLists.txt](../CMakeLists.txt))
|
||||
that can be used on a wide range of platforms ("C" stands for cross-platform.).
|
||||
If you don't have CMake installed already, you can download it for free from
|
||||
<https://www.cmake.org/>.
|
||||
|
||||
CMake works by generating native makefiles or build projects that can
|
||||
be used in the compiler environment of your choice.
|
||||
|
||||
For API/ABI compatibility reasons, we strongly recommend building Abseil in a
|
||||
subdirectory of your project or as an embedded dependency.
|
||||
|
||||
## Incorporating Abseil Into a CMake Project
|
||||
|
||||
The recommendations below are similar to those for using CMake within the
|
||||
googletest framework
|
||||
(<https://github.com/google/googletest/blob/master/googletest/README.md#incorporating-into-an-existing-cmake-project>)
|
||||
|
||||
### Step-by-Step Instructions
|
||||
|
||||
1. If you want to build the Abseil tests, integrate the Abseil dependency
|
||||
[Google Test](https://github.com/google/googletest) into your CMake project. To disable Abseil tests, you have to pass
|
||||
`-DBUILD_TESTING=OFF` when configuring your project with CMake.
|
||||
|
||||
2. Download Abseil and copy it into a subdirectory in your CMake project or add
|
||||
Abseil as a [git submodule](https://git-scm.com/docs/git-submodule) in your
|
||||
CMake project.
|
||||
|
||||
3. You can then use the CMake command
|
||||
[`add_subdirectory()`](https://cmake.org/cmake/help/latest/command/add_subdirectory.html)
|
||||
to include Abseil directly in your CMake project.
|
||||
|
||||
4. Add the **absl::** target you wish to use to the
|
||||
[`target_link_libraries()`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html)
|
||||
section of your executable or of your library.<br>
|
||||
Here is a short CMakeLists.txt example of an application project using Abseil.
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.8.2)
|
||||
project(my_app_project)
|
||||
|
||||
# Pick the C++ standard to compile with.
|
||||
# Abseil currently supports C++11, C++14, and C++17.
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_subdirectory(abseil-cpp)
|
||||
|
||||
add_executable(my_exe source.cpp)
|
||||
target_link_libraries(my_exe absl::base absl::synchronization absl::strings)
|
||||
```
|
||||
|
||||
Note that if you are developing a library designed for use by other clients, you
|
||||
should instead leave `CMAKE_CXX_STANDARD` unset (or only set if being built as
|
||||
the current top-level CMake project) and configure the minimum required C++
|
||||
standard at the target level. If you require a later minimum C++ standard than
|
||||
Abseil does, it's a good idea to also enforce that `CMAKE_CXX_STANDARD` (which
|
||||
will control Abseil library targets) is set to at least that minimum. For
|
||||
example:
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.8.2)
|
||||
project(my_lib_project)
|
||||
|
||||
# Leave C++ standard up to the root application, so set it only if this is the
|
||||
# current top-level CMake project.
|
||||
if(CMAKE_SOURCE_DIR STREQUAL my_lib_project_SOURCE_DIR)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
endif()
|
||||
|
||||
add_subdirectory(abseil-cpp)
|
||||
|
||||
add_library(my_lib source.cpp)
|
||||
target_link_libraries(my_lib absl::base absl::synchronization absl::strings)
|
||||
|
||||
# Enforce that my_lib requires C++17. Important to document for clients that they
|
||||
# must set CMAKE_CXX_STANDARD to 17 or higher for proper Abseil ABI compatibility
|
||||
# (since otherwise, Abseil library targets could be compiled with a lower C++
|
||||
# standard than my_lib).
|
||||
target_compile_features(my_lib PUBLIC cxx_std_17)
|
||||
if(CMAKE_CXX_STANDARD LESS 17)
|
||||
message(FATAL_ERROR
|
||||
"my_lib_project requires CMAKE_CXX_STANDARD >= 17 (got: ${CMAKE_CXX_STANDARD})")
|
||||
endif()
|
||||
```
|
||||
|
||||
Then the top-level application project that uses your library is responsible for
|
||||
setting a consistent `CMAKE_CXX_STANDARD` that is sufficiently high.
|
||||
|
||||
### Running Abseil Tests with CMake
|
||||
|
||||
Use the `-DBUILD_TESTING=ON` flag to run Abseil tests.
|
||||
|
||||
You will need to provide Abseil with a Googletest dependency. There are two
|
||||
options for how to do this:
|
||||
|
||||
* Use `-DABSL_USE_GOOGLETEST_HEAD`. This will automatically download the latest
|
||||
Googletest source into the build directory at configure time. Googletest will
|
||||
then be compiled directly alongside Abseil's tests.
|
||||
* Manually integrate Googletest with your build. See
|
||||
https://github.com/google/googletest/blob/master/googletest/README.md#using-cmake
|
||||
for more information on using Googletest in a CMake project.
|
||||
|
||||
For example, to run just the Abseil tests, you could use this script:
|
||||
|
||||
```
|
||||
cd path/to/abseil-cpp
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -DBUILD_TESTING=ON -DABSL_USE_GOOGLETEST_HEAD=ON ..
|
||||
make -j
|
||||
ctest
|
||||
```
|
||||
|
||||
Currently, we only run our tests with CMake in a Linux environment, but we are
|
||||
working on the rest of our supported platforms. See
|
||||
https://github.com/abseil/abseil-cpp/projects/1 and
|
||||
https://github.com/abseil/abseil-cpp/issues/109 for more information.
|
||||
|
||||
### Available Abseil CMake Public Targets
|
||||
|
||||
Here's a non-exhaustive list of Abseil CMake public targets:
|
||||
|
||||
```cmake
|
||||
absl::algorithm
|
||||
absl::base
|
||||
absl::debugging
|
||||
absl::flat_hash_map
|
||||
absl::flags
|
||||
absl::memory
|
||||
absl::meta
|
||||
absl::numeric
|
||||
absl::random_random
|
||||
absl::strings
|
||||
absl::synchronization
|
||||
absl::time
|
||||
absl::utility
|
||||
```
|
||||
|
||||
## Traditional CMake Set-Up
|
||||
|
||||
For larger projects, it may make sense to use the traditional CMake set-up where you build and install projects separately.
|
||||
|
||||
First, you'd need to build and install Google Test:
|
||||
```
|
||||
cmake -S /source/googletest -B /build/googletest -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/installation/dir -DBUILD_GMOCK=ON
|
||||
cmake --build /build/googletest --target install
|
||||
```
|
||||
|
||||
Then you need to configure and build Abseil. Make sure you enable `ABSL_USE_EXTERNAL_GOOGLETEST` and `ABSL_FIND_GOOGLETEST`. You also need to enable `ABSL_ENABLE_INSTALL` so that you can install Abseil itself.
|
||||
```
|
||||
cmake -S /source/abseil-cpp -B /build/abseil-cpp -DCMAKE_PREFIX_PATH=/installation/dir -DCMAKE_INSTALL_PREFIX=/installation/dir -DABSL_ENABLE_INSTALL=ON -DABSL_USE_EXTERNAL_GOOGLETEST=ON -DABSL_FIND_GOOGLETEST=ON
|
||||
cmake --build /temporary/build/abseil-cpp
|
||||
```
|
||||
|
||||
(`CMAKE_PREFIX_PATH` is where you already have Google Test installed; `CMAKE_INSTALL_PREFIX` is where you want to have Abseil installed; they can be different.)
|
||||
|
||||
Run the tests:
|
||||
```
|
||||
ctest --test-dir /temporary/build/abseil-cpp
|
||||
```
|
||||
|
||||
And finally install:
|
||||
```
|
||||
cmake --build /temporary/build/abseil-cpp --target install
|
||||
```
|
||||
|
||||
# CMake Option Synposis
|
||||
|
||||
## Enable Standard CMake Installation
|
||||
|
||||
`-DABSL_ENABLE_INSTALL=ON`
|
||||
|
||||
## Google Test Options
|
||||
|
||||
`-DBUILD_TESTING=ON` must be set to enable testing
|
||||
|
||||
- Have Abseil download and build Google Test for you: `-DABSL_USE_EXTERNAL_GOOGLETEST=OFF` (default)
|
||||
- Download and build latest Google Test: `-DABSL_USE_GOOGLETEST_HEAD=ON`
|
||||
- Download specific Google Test version (ZIP archive): `-DABSL_GOOGLETEST_DOWNLOAD_URL=https://.../version.zip`
|
||||
- Use Google Test from specific local directory: `-DABSL_LOCAL_GOOGLETEST_DIR=/path/to/googletest`
|
||||
- Use Google Test included elsewhere in your project: `-DABSL_USE_EXTERNAL_GOOGLETEST=ON`
|
||||
- Use standard CMake `find_package(CTest)` to find installed Google Test: `-DABSL_USE_EXTERNAL_GOOGLETEST=ON -DABSL_FIND_GOOGLETEST=ON`
|
|
@ -0,0 +1,8 @@
|
|||
# absl CMake configuration file.
|
||||
|
||||
include(CMakeFindDependencyMacro)
|
||||
find_dependency(Threads)
|
||||
|
||||
@PACKAGE_INIT@
|
||||
|
||||
include ("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
|
|
@ -0,0 +1,25 @@
|
|||
#
|
||||
# Copyright 2019 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.
|
||||
|
||||
# A simple CMakeLists.txt for testing cmake installation
|
||||
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(absl_cmake_testing CXX)
|
||||
|
||||
add_executable(simple simple.cc)
|
||||
|
||||
find_package(absl REQUIRED)
|
||||
|
||||
target_link_libraries(simple absl::strings)
|
|
@ -0,0 +1,23 @@
|
|||
//
|
||||
// Copyright 2019 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 <iostream>
|
||||
#include "absl/strings/substitute.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
std::cout << absl::Substitute("Arg $0: $1\n", i, argv[i]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2019 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.
|
||||
#
|
||||
# Unit and integration tests for Abseil LTS CMake installation
|
||||
|
||||
# Fail on any error. Treat unset variables an error. Print commands as executed.
|
||||
set -euox pipefail
|
||||
|
||||
absl_dir=/abseil-cpp
|
||||
absl_build_dir=/buildfs
|
||||
googletest_builddir=/googletest_builddir
|
||||
project_dir="${absl_dir}"/CMake/install_test_project
|
||||
project_build_dir=/buildfs/project-build
|
||||
|
||||
build_shared_libs="OFF"
|
||||
if [ "${LINK_TYPE:-}" = "DYNAMIC" ]; then
|
||||
build_shared_libs="ON"
|
||||
fi
|
||||
|
||||
# Build and install GoogleTest
|
||||
mkdir "${googletest_builddir}"
|
||||
pushd "${googletest_builddir}"
|
||||
curl -L "${ABSL_GOOGLETEST_DOWNLOAD_URL}" --output "${ABSL_GOOGLETEST_COMMIT}".zip
|
||||
unzip "${ABSL_GOOGLETEST_COMMIT}".zip
|
||||
pushd "googletest-${ABSL_GOOGLETEST_COMMIT}"
|
||||
mkdir build
|
||||
pushd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS="${build_shared_libs}" ..
|
||||
make -j $(nproc)
|
||||
make install
|
||||
ldconfig
|
||||
popd
|
||||
popd
|
||||
popd
|
||||
|
||||
# Run the LTS transformations
|
||||
./create_lts.py 99998877
|
||||
|
||||
# Build and install Abseil
|
||||
pushd "${absl_build_dir}"
|
||||
cmake "${absl_dir}" \
|
||||
-DABSL_USE_EXTERNAL_GOOGLETEST=ON \
|
||||
-DABSL_FIND_GOOGLETEST=ON \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DBUILD_TESTING=ON \
|
||||
-DBUILD_SHARED_LIBS="${build_shared_libs}"
|
||||
make -j $(nproc)
|
||||
ctest -j $(nproc)
|
||||
make install
|
||||
ldconfig
|
||||
popd
|
||||
|
||||
# Test the project against the installed Abseil
|
||||
mkdir -p "${project_build_dir}"
|
||||
pushd "${project_build_dir}"
|
||||
cmake "${project_dir}"
|
||||
cmake --build . --target simple
|
||||
|
||||
output="$(${project_build_dir}/simple "printme" 2>&1)"
|
||||
if [[ "${output}" != *"Arg 1: printme"* ]]; then
|
||||
echo "Faulty output on simple project:"
|
||||
echo "${output}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
popd
|
||||
|
||||
if ! grep absl::strings "/usr/local/lib/cmake/absl/abslTargets.cmake"; then
|
||||
cat "/usr/local/lib/cmake/absl/abslTargets.cmake"
|
||||
echo "CMake targets named incorrectly"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
pushd "${HOME}"
|
||||
cat > hello-abseil.cc << EOF
|
||||
#include <cstdlib>
|
||||
|
||||
#include "absl/strings/str_format.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
absl::PrintF("Hello Abseil!\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
EOF
|
||||
|
||||
if [ "${LINK_TYPE:-}" != "DYNAMIC" ]; then
|
||||
pc_args=($(pkg-config --cflags --libs --static absl_str_format))
|
||||
g++ -static -o hello-abseil hello-abseil.cc "${pc_args[@]}"
|
||||
else
|
||||
pc_args=($(pkg-config --cflags --libs absl_str_format))
|
||||
g++ -o hello-abseil hello-abseil.cc "${pc_args[@]}"
|
||||
fi
|
||||
hello="$(./hello-abseil)"
|
||||
[[ "${hello}" == "Hello Abseil!" ]]
|
||||
|
||||
popd
|
||||
|
||||
echo "Install test complete!"
|
||||
exit 0
|
|
@ -0,0 +1,233 @@
|
|||
#
|
||||
# Copyright 2017 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.
|
||||
#
|
||||
|
||||
# Most widely used distributions have cmake 3.5 or greater available as of March
|
||||
# 2019. A notable exception is RHEL-7 (CentOS7). You can install a current
|
||||
# version of CMake by first installing Extra Packages for Enterprise Linux
|
||||
# (https://fedoraproject.org/wiki/EPEL#Extra_Packages_for_Enterprise_Linux_.28EPEL.29)
|
||||
# and then issuing `yum install cmake3` on the command line.
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
# Compiler id for Apple Clang is now AppleClang.
|
||||
if (POLICY CMP0025)
|
||||
cmake_policy(SET CMP0025 NEW)
|
||||
endif (POLICY CMP0025)
|
||||
|
||||
# if command can use IN_LIST
|
||||
if (POLICY CMP0057)
|
||||
cmake_policy(SET CMP0057 NEW)
|
||||
endif (POLICY CMP0057)
|
||||
|
||||
# Project version variables are the empty string if version is unspecified
|
||||
if (POLICY CMP0048)
|
||||
cmake_policy(SET CMP0048 NEW)
|
||||
endif (POLICY CMP0048)
|
||||
|
||||
# option() honor variables
|
||||
if (POLICY CMP0077)
|
||||
cmake_policy(SET CMP0077 NEW)
|
||||
endif (POLICY CMP0077)
|
||||
|
||||
# Allow the user to specify the MSVC runtime
|
||||
if (POLICY CMP0091)
|
||||
cmake_policy(SET CMP0091 NEW)
|
||||
endif (POLICY CMP0091)
|
||||
|
||||
# Set BUILD_TESTING to OFF by default.
|
||||
# This must come before the project() and include(CTest) lines.
|
||||
OPTION(BUILD_TESTING "Build tests" OFF)
|
||||
|
||||
project(absl LANGUAGES CXX)
|
||||
include(CTest)
|
||||
|
||||
# Output directory is correct by default for most build setups. However, when
|
||||
# building Abseil as a DLL, it is important to have the DLL in the same
|
||||
# directory as the executable using it. Thus, we put all executables in a single
|
||||
# /bin directory.
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp))
|
||||
# in the source tree of a project that uses it, install rules are disabled.
|
||||
if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
|
||||
option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
|
||||
else()
|
||||
option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
|
||||
endif()
|
||||
|
||||
option(ABSL_PROPAGATE_CXX_STD
|
||||
"Use CMake C++ standard meta features (e.g. cxx_std_11) that propagate to targets that link to Abseil"
|
||||
OFF) # TODO: Default to ON for CMake 3.8 and greater.
|
||||
if((${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.8) AND (NOT ABSL_PROPAGATE_CXX_STD))
|
||||
message(WARNING "A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake 3.8 and up. We recommend enabling this option to ensure your project still builds correctly.")
|
||||
endif()
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH
|
||||
${CMAKE_CURRENT_LIST_DIR}/CMake
|
||||
${CMAKE_CURRENT_LIST_DIR}/absl/copts
|
||||
)
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
include(GNUInstallDirs)
|
||||
include(AbseilDll)
|
||||
include(AbseilHelpers)
|
||||
|
||||
|
||||
##
|
||||
## Using absl targets
|
||||
##
|
||||
## all public absl targets are
|
||||
## exported with the absl:: prefix
|
||||
##
|
||||
## e.g absl::base absl::synchronization absl::strings ....
|
||||
##
|
||||
## DO NOT rely on the internal targets outside of the prefix
|
||||
|
||||
|
||||
# include current path
|
||||
list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
||||
set(ABSL_USING_CLANG ON)
|
||||
else()
|
||||
set(ABSL_USING_CLANG OFF)
|
||||
endif()
|
||||
|
||||
# find dependencies
|
||||
## pthread
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
include(CMakeDependentOption)
|
||||
|
||||
option(ABSL_USE_EXTERNAL_GOOGLETEST
|
||||
"If ON, Abseil will assume that the targets for GoogleTest are already provided by the including project. This makes sense when Abseil is used with add_subproject." OFF)
|
||||
|
||||
cmake_dependent_option(ABSL_FIND_GOOGLETEST
|
||||
"If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project."
|
||||
ON
|
||||
"ABSL_USE_EXTERNAL_GOOGLETEST"
|
||||
OFF)
|
||||
|
||||
|
||||
option(ABSL_USE_GOOGLETEST_HEAD
|
||||
"If ON, abseil will download HEAD from GoogleTest at config time." OFF)
|
||||
|
||||
set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL")
|
||||
|
||||
set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
|
||||
"If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout."
|
||||
)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
## check targets
|
||||
if (ABSL_USE_EXTERNAL_GOOGLETEST)
|
||||
if (ABSL_FIND_GOOGLETEST)
|
||||
find_package(GTest REQUIRED)
|
||||
else()
|
||||
if (NOT TARGET gtest AND NOT TARGET GTest::gtest)
|
||||
message(FATAL_ERROR "ABSL_USE_EXTERNAL_GOOGLETEST is ON and ABSL_FIND_GOOGLETEST is OFF, which means that the top-level project must build the Google Test project. However, the target gtest was not found.")
|
||||
endif()
|
||||
endif()
|
||||
else()
|
||||
set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
|
||||
if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL)
|
||||
message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL")
|
||||
endif()
|
||||
if(ABSL_USE_GOOGLETEST_HEAD)
|
||||
set(absl_gtest_download_url "https://github.com/google/googletest/archive/master.zip")
|
||||
elseif(ABSL_GOOGLETEST_DOWNLOAD_URL)
|
||||
set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL})
|
||||
endif()
|
||||
if(absl_gtest_download_url)
|
||||
set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src)
|
||||
else()
|
||||
set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR})
|
||||
endif()
|
||||
include(CMake/Googletest/DownloadGTest.cmake)
|
||||
endif()
|
||||
|
||||
if (NOT ABSL_FIND_GOOGLETEST)
|
||||
# When Google Test is included directly rather than through find_package, the aliases are missing.
|
||||
add_library(GTest::gtest_main ALIAS gtest_main)
|
||||
add_library(GTest::gtest ALIAS gtest)
|
||||
add_library(GTest::gmock ALIAS gmock)
|
||||
endif()
|
||||
|
||||
check_target(GTest::gtest)
|
||||
check_target(GTest::gtest_main)
|
||||
check_target(GTest::gmock)
|
||||
check_target(GTest::gmock_main)
|
||||
|
||||
list(APPEND ABSL_TEST_COMMON_LIBRARIES
|
||||
GTest::gtest_main
|
||||
GTest::gtest
|
||||
GTest::gmock
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
||||
endif()
|
||||
|
||||
add_subdirectory(absl)
|
||||
|
||||
if(ABSL_ENABLE_INSTALL)
|
||||
# absl:lts-remove-begin(system installation is supported for LTS releases)
|
||||
# We don't support system-wide installation
|
||||
list(APPEND SYSTEM_INSTALL_DIRS "/usr/local" "/usr" "/opt/" "/opt/local" "c:/Program Files/${PROJECT_NAME}")
|
||||
if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX IN_LIST SYSTEM_INSTALL_DIRS)
|
||||
message(WARNING "\
|
||||
The default and system-level install directories are unsupported except in LTS \
|
||||
releases of Abseil. Please set CMAKE_INSTALL_PREFIX to install Abseil in your \
|
||||
source or build tree directly.\
|
||||
")
|
||||
endif()
|
||||
# absl:lts-remove-end
|
||||
|
||||
# install as a subdirectory only
|
||||
install(EXPORT ${PROJECT_NAME}Targets
|
||||
NAMESPACE absl::
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
|
||||
)
|
||||
|
||||
configure_package_config_file(
|
||||
CMake/abslConfig.cmake.in
|
||||
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
|
||||
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
|
||||
)
|
||||
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
|
||||
)
|
||||
|
||||
# Abseil only has a version in LTS releases. This mechanism is accomplished
|
||||
# Abseil's internal Copybara (https://github.com/google/copybara) workflows and
|
||||
# isn't visible in the CMake buildsystem itself.
|
||||
if(absl_VERSION)
|
||||
write_basic_package_version_file(
|
||||
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
|
||||
COMPATIBILITY ExactVersion
|
||||
)
|
||||
|
||||
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
|
||||
)
|
||||
endif() # absl_VERSION
|
||||
|
||||
install(DIRECTORY absl
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
FILES_MATCHING
|
||||
PATTERN "*.inc"
|
||||
PATTERN "*.h"
|
||||
PATTERN "copts" EXCLUDE
|
||||
PATTERN "testdata" EXCLUDE
|
||||
)
|
||||
endif() # ABSL_ENABLE_INSTALL
|
|
@ -0,0 +1,141 @@
|
|||
# How to Contribute to Abseil
|
||||
|
||||
We'd love to accept your patches and contributions to this project. There are
|
||||
just a few small guidelines you need to follow.
|
||||
|
||||
NOTE: If you are new to GitHub, please start by reading [Pull Request
|
||||
howto](https://help.github.com/articles/about-pull-requests/)
|
||||
|
||||
## Contributor License Agreement
|
||||
|
||||
Contributions to this project must be accompanied by a Contributor License
|
||||
Agreement. You (or your employer) retain the copyright to your contribution,
|
||||
this simply gives us permission to use and redistribute your contributions as
|
||||
part of the project. Head over to <https://cla.developers.google.com/> to see
|
||||
your current agreements on file or to sign a new one.
|
||||
|
||||
You generally only need to submit a CLA once, so if you've already submitted one
|
||||
(even if it was for a different project), you probably don't need to do it
|
||||
again.
|
||||
|
||||
## Contribution Guidelines
|
||||
|
||||
Potential contributors sometimes ask us if the Abseil project is the appropriate
|
||||
home for their utility library code or for specific functions implementing
|
||||
missing portions of the standard. Often, the answer to this question is "no".
|
||||
We’d like to articulate our thinking on this issue so that our choices can be
|
||||
understood by everyone and so that contributors can have a better intuition
|
||||
about whether Abseil might be interested in adopting a new library.
|
||||
|
||||
### Priorities
|
||||
|
||||
Although our mission is to augment the C++ standard library, our goal is not to
|
||||
provide a full forward-compatible implementation of the latest standard. For us
|
||||
to consider a library for inclusion in Abseil, it is not enough that a library
|
||||
is useful. We generally choose to release a library when it meets at least one
|
||||
of the following criteria:
|
||||
|
||||
* **Widespread usage** - Using our internal codebase to help gauge usage, most
|
||||
of the libraries we've released have tens of thousands of users.
|
||||
* **Anticipated widespread usage** - Pre-adoption of some standard-compliant
|
||||
APIs may not have broad adoption initially but can be expected to pick up
|
||||
usage when it replaces legacy APIs. `absl::from_chars`, for example,
|
||||
replaces existing code that converts strings to numbers and will therefore
|
||||
likely see usage growth.
|
||||
* **High impact** - APIs that provide a key solution to a specific problem,
|
||||
such as `absl::FixedArray`, have higher impact than usage numbers may signal
|
||||
and are released because of their importance.
|
||||
* **Direct support for a library that falls under one of the above** - When we
|
||||
want access to a smaller library as an implementation detail for a
|
||||
higher-priority library we plan to release, we may release it, as we did
|
||||
with portions of `absl/meta/type_traits.h`. One consequence of this is that
|
||||
the presence of a library in Abseil does not necessarily mean that other
|
||||
similar libraries would be a high priority.
|
||||
|
||||
### API Freeze Consequences
|
||||
|
||||
Via the
|
||||
[Abseil Compatibility Guidelines](https://abseil.io/about/compatibility), we
|
||||
have promised a large degree of API stability. In particular, we will not make
|
||||
backward-incompatible changes to released APIs without also shipping a tool or
|
||||
process that can upgrade our users' code. We are not yet at the point of easily
|
||||
releasing such tools. Therefore, at this time, shipping a library establishes an
|
||||
API contract which is borderline unchangeable. (We can add new functionality,
|
||||
but we cannot easily change existing behavior.) This constraint forces us to
|
||||
very carefully review all APIs that we ship.
|
||||
|
||||
|
||||
## Coding Style
|
||||
|
||||
To keep the source consistent, readable, diffable and easy to merge, we use a
|
||||
fairly rigid coding style, as defined by the
|
||||
[google-styleguide](https://github.com/google/styleguide) project. All patches
|
||||
will be expected to conform to the style outlined
|
||||
[here](https://google.github.io/styleguide/cppguide.html).
|
||||
|
||||
## Guidelines for Pull Requests
|
||||
|
||||
* If you are a Googler, it is preferable to first create an internal CL and
|
||||
have it reviewed and submitted. The code propagation process will deliver
|
||||
the change to GitHub.
|
||||
|
||||
* Create **small PRs** that are narrowly focused on **addressing a single
|
||||
concern**. We often receive PRs that are trying to fix several things at a
|
||||
time, but if only one fix is considered acceptable, nothing gets merged and
|
||||
both author's & review's time is wasted. Create more PRs to address
|
||||
different concerns and everyone will be happy.
|
||||
|
||||
* For speculative changes, consider opening an [Abseil
|
||||
issue](https://github.com/abseil/abseil-cpp/issues) and discussing it first.
|
||||
If you are suggesting a behavioral or API change, consider starting with an
|
||||
[Abseil proposal template](ABSEIL_ISSUE_TEMPLATE.md).
|
||||
|
||||
* Provide a good **PR description** as a record of **what** change is being
|
||||
made and **why** it was made. Link to a GitHub issue if it exists.
|
||||
|
||||
* Don't fix code style and formatting unless you are already changing that
|
||||
line to address an issue. Formatting of modified lines may be done using
|
||||
`git clang-format`. PRs with irrelevant changes won't be merged. If
|
||||
you do want to fix formatting or style, do that in a separate PR.
|
||||
|
||||
* Unless your PR is trivial, you should expect there will be reviewer comments
|
||||
that you'll need to address before merging. We expect you to be reasonably
|
||||
responsive to those comments, otherwise the PR will be closed after 2-3
|
||||
weeks of inactivity.
|
||||
|
||||
* Maintain **clean commit history** and use **meaningful commit messages**.
|
||||
PRs with messy commit history are difficult to review and won't be merged.
|
||||
Use `rebase -i upstream/master` to curate your commit history and/or to
|
||||
bring in latest changes from master (but avoid rebasing in the middle of a
|
||||
code review).
|
||||
|
||||
* Keep your PR up to date with upstream/master (if there are merge conflicts,
|
||||
we can't really merge your change).
|
||||
|
||||
* **All tests need to be passing** before your change can be merged. We
|
||||
recommend you **run tests locally** (see below)
|
||||
|
||||
* Exceptions to the rules can be made if there's a compelling reason for doing
|
||||
so. That is - the rules are here to serve us, not the other way around, and
|
||||
the rules need to be serving their intended purpose to be valuable.
|
||||
|
||||
* All submissions, including submissions by project members, require review.
|
||||
|
||||
## Running Tests
|
||||
|
||||
If you have [Bazel](https://bazel.build/) installed, use `bazel test
|
||||
--test_tag_filters="-benchmark" ...` to run the unit tests.
|
||||
|
||||
If you are running the Linux operating system and have
|
||||
[Docker](https://www.docker.com/) installed, you can also run the `linux_*.sh`
|
||||
scripts under the `ci/`(https://github.com/abseil/abseil-cpp/tree/master/ci)
|
||||
directory to test Abseil under a variety of conditions.
|
||||
|
||||
## Abseil Committers
|
||||
|
||||
The current members of the Abseil engineering team are the only committers at
|
||||
present.
|
||||
|
||||
## Release Process
|
||||
|
||||
Abseil lives at head, where latest-and-greatest code can be found.
|
|
@ -0,0 +1,3 @@
|
|||
monorail {
|
||||
component: "Internals>Core"
|
||||
}
|
|
@ -0,0 +1,167 @@
|
|||
# Abseil FAQ
|
||||
|
||||
## Is Abseil the right home for my utility library?
|
||||
|
||||
Most often the answer to the question is "no." As both the [About
|
||||
Abseil](https://abseil.io/about/) page and our [contributing
|
||||
guidelines](https://github.com/abseil/abseil-cpp/blob/master/CONTRIBUTING.md#contribution-guidelines)
|
||||
explain, Abseil contains a variety of core C++ library code that is widely used
|
||||
at [Google](https://www.google.com/). As such, Abseil's primary purpose is to be
|
||||
used as a dependency by Google's open source C++ projects. While we do hope that
|
||||
Abseil is also useful to the C++ community at large, this added constraint also
|
||||
means that we are unlikely to accept a contribution of utility code that isn't
|
||||
already widely used by Google.
|
||||
|
||||
## How to I set the C++ dialect used to build Abseil?
|
||||
|
||||
The short answer is that whatever mechanism you choose, you need to make sure
|
||||
that you set this option consistently at the global level for your entire
|
||||
project. If, for example, you want to set the C++ dialect to C++17, with
|
||||
[Bazel](https://bazel/build/) as the build system and `gcc` or `clang` as the
|
||||
compiler, there several ways to do this:
|
||||
* Pass `--cxxopt=-std=c++17` on the command line (for example, `bazel build
|
||||
--cxxopt=-std=c++17 ...`)
|
||||
* Set the environment variable `BAZEL_CXXOPTS` (for example,
|
||||
`BAZEL_CXXOPTS=-std=c++17`)
|
||||
* Add `build --cxxopt=-std=c++17` to your [`.bazelrc`
|
||||
file](https://docs.bazel.build/versions/master/guide.html#bazelrc)
|
||||
|
||||
If you are using CMake as the build system, you'll need to add a line like
|
||||
`set(CMAKE_CXX_STANDARD 17)` to your top level `CMakeLists.txt` file. If you
|
||||
are developing a library designed to be used by other clients, you should
|
||||
instead leave `CMAKE_CXX_STANDARD` unset and configure the minimum C++ standard
|
||||
required by each of your library targets via `target_compile_features`. See the
|
||||
[CMake build
|
||||
instructions](https://github.com/abseil/abseil-cpp/blob/master/CMake/README.md)
|
||||
for more information.
|
||||
|
||||
For a longer answer to this question and to understand why some other approaches
|
||||
don't work, see the answer to ["What is ABI and why don't you recommend using a
|
||||
pre-compiled version of
|
||||
Abseil?"](#what-is-abi-and-why-dont-you-recommend-using-a-pre-compiled-version-of-abseil)
|
||||
|
||||
## What is ABI and why don't you recommend using a pre-compiled version of Abseil?
|
||||
|
||||
For the purposes of this discussion, you can think of
|
||||
[ABI](https://en.wikipedia.org/wiki/Application_binary_interface) as the
|
||||
compiled representation of the interfaces in code. This is in contrast to
|
||||
[API](https://en.wikipedia.org/wiki/Application_programming_interface), which
|
||||
you can think of as the interfaces as defined by the code itself. [Abseil has a
|
||||
strong promise of API compatibility, but does not make any promise of ABI
|
||||
compatibility](https://abseil.io/about/compatibility). Let's take a look at what
|
||||
this means in practice.
|
||||
|
||||
You might be tempted to do something like this in a
|
||||
[Bazel](https://bazel.build/) `BUILD` file:
|
||||
|
||||
```
|
||||
# DON'T DO THIS!!!
|
||||
cc_library(
|
||||
name = "my_library",
|
||||
srcs = ["my_library.cc"],
|
||||
copts = ["-std=c++17"], # May create a mixed-mode compile!
|
||||
deps = ["@com_google_absl//absl/strings"],
|
||||
)
|
||||
```
|
||||
|
||||
Applying `-std=c++17` to an individual target in your `BUILD` file is going to
|
||||
compile that specific target in C++17 mode, but it isn't going to ensure the
|
||||
Abseil library is built in C++17 mode, since the Abseil library itself is a
|
||||
different build target. If your code includes an Abseil header, then your
|
||||
program may contain conflicting definitions of the same
|
||||
class/function/variable/enum, etc. As a rule, all compile options that affect
|
||||
the ABI of a program need to be applied to the entire build on a global basis.
|
||||
|
||||
C++ has something called the [One Definition
|
||||
Rule](https://en.wikipedia.org/wiki/One_Definition_Rule) (ODR). C++ doesn't
|
||||
allow multiple definitions of the same class/function/variable/enum, etc. ODR
|
||||
violations sometimes result in linker errors, but linkers do not always catch
|
||||
violations. Uncaught ODR violations can result in strange runtime behaviors or
|
||||
crashes that can be hard to debug.
|
||||
|
||||
If you build the Abseil library and your code using different compile options
|
||||
that affect ABI, there is a good chance you will run afoul of the One Definition
|
||||
Rule. Examples of GCC compile options that affect ABI include (but aren't
|
||||
limited to) language dialect (e.g. `-std=`), optimization level (e.g. `-O2`),
|
||||
code generation flags (e.g. `-fexceptions`), and preprocessor defines
|
||||
(e.g. `-DNDEBUG`).
|
||||
|
||||
If you use a pre-compiled version of Abseil, (for example, from your Linux
|
||||
distribution package manager or from something like
|
||||
[vcpkg](https://github.com/microsoft/vcpkg)) you have to be very careful to
|
||||
ensure ABI compatibility across the components of your program. The only way you
|
||||
can be sure your program is going to be correct regarding ABI is to ensure
|
||||
you've used the exact same compile options as were used to build the
|
||||
pre-compiled library. This does not mean that Abseil cannot work as part of a
|
||||
Linux distribution since a knowledgeable binary packager will have ensured that
|
||||
all packages have been built with consistent compile options. This is one of the
|
||||
reasons we warn against - though do not outright reject - using Abseil as a
|
||||
pre-compiled library.
|
||||
|
||||
Another possible way that you might afoul of ABI issues is if you accidentally
|
||||
include two versions of Abseil in your program. Multiple versions of Abseil can
|
||||
end up within the same binary if your program uses the Abseil library and
|
||||
another library also transitively depends on Abseil (resulting in what is
|
||||
sometimes called the diamond dependency problem). In cases such as this you must
|
||||
structure your build so that all libraries use the same version of Abseil.
|
||||
[Abseil's strong promise of API compatibility between
|
||||
releases](https://abseil.io/about/compatibility) means the latest "HEAD" release
|
||||
of Abseil is almost certainly the right choice if you are doing as we recommend
|
||||
and building all of your code from source.
|
||||
|
||||
For these reasons we recommend you avoid pre-compiled code and build the Abseil
|
||||
library yourself in a consistent manner with the rest of your code.
|
||||
|
||||
## What is "live at head" and how do I do it?
|
||||
|
||||
From Abseil's point-of-view, "live at head" means that every Abseil source
|
||||
release (which happens on an almost daily basis) is either API compatible with
|
||||
the previous release, or comes with an automated tool that you can run over code
|
||||
to make it compatible. In practice, the need to use an automated tool is
|
||||
extremely rare. This means that upgrading from one source release to another
|
||||
should be a routine practice that can and should be performed often.
|
||||
|
||||
We recommend you update to the [latest commit in the `master` branch of
|
||||
Abseil](https://github.com/abseil/abseil-cpp/commits/master) as often as
|
||||
possible. Not only will you pick up bug fixes more quickly, but if you have good
|
||||
automated testing, you will catch and be able to fix any [Hyrum's
|
||||
Law](https://www.hyrumslaw.com/) dependency problems on an incremental basis
|
||||
instead of being overwhelmed by them and having difficulty isolating them if you
|
||||
wait longer between updates.
|
||||
|
||||
If you are using the [Bazel](https://bazel.build/) build system and its
|
||||
[external dependencies](https://docs.bazel.build/versions/master/external.html)
|
||||
feature, updating the
|
||||
[`http_archive`](https://docs.bazel.build/versions/master/repo/http.html#http_archive)
|
||||
rule in your
|
||||
[`WORKSPACE`](https://docs.bazel.build/versions/master/be/workspace.html) for
|
||||
`com_google_abseil` to point to the [latest commit in the `master` branch of
|
||||
Abseil](https://github.com/abseil/abseil-cpp/commits/master) is all you need to
|
||||
do. For example, on February 11, 2020, the latest commit to the master branch
|
||||
was `98eb410c93ad059f9bba1bf43f5bb916fc92a5ea`. To update to this commit, you
|
||||
would add the following snippet to your `WORKSPACE` file:
|
||||
|
||||
```
|
||||
http_archive(
|
||||
name = "com_google_absl",
|
||||
urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"], # 2020-02-11T18:50:53Z
|
||||
strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea",
|
||||
sha256 = "aabf6c57e3834f8dc3873a927f37eaf69975d4b28117fc7427dfb1c661542a87",
|
||||
)
|
||||
```
|
||||
|
||||
To get the `sha256` of this URL, run `curl -sL --output -
|
||||
https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip
|
||||
| sha256sum -`.
|
||||
|
||||
You can commit the updated `WORKSPACE` file to your source control every time
|
||||
you update, and if you have good automated testing, you might even consider
|
||||
automating this.
|
||||
|
||||
One thing we don't recommend is using GitHub's `master.zip` files (for example
|
||||
[https://github.com/abseil/abseil-cpp/archive/master.zip](https://github.com/abseil/abseil-cpp/archive/master.zip)),
|
||||
which are always the latest commit in the `master` branch, to implement live at
|
||||
head. Since these `master.zip` URLs are not versioned, you will lose build
|
||||
reproducibility. In addition, some build systems, including Bazel, will simply
|
||||
cache this file, which means you won't actually be updating to the latest
|
||||
release until your cache is cleared or invalidated.
|
|
@ -0,0 +1,203 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
https://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
danilchap@chromium.org
|
||||
kwiberg@chromium.org
|
||||
mbonadei@chromium.org
|
|
@ -0,0 +1,36 @@
|
|||
Name: Abseil
|
||||
Short Name: absl
|
||||
URL: https://github.com/abseil/abseil-cpp
|
||||
License: Apache 2.0
|
||||
License File: LICENSE
|
||||
Version: 0
|
||||
Revision: 637722af3a60c17915d3325604a0435ee92a41b4
|
||||
Security Critical: yes
|
||||
|
||||
Description:
|
||||
This directory contains the source code of Abseil for C++. This can be used by
|
||||
Chromium, subject to the guidance at https://chromium-cpp.appspot.com/; it can
|
||||
be used without restriction by Chromium's dependencies, except that objects
|
||||
compiled into Chromium itself cannot use anything relying on
|
||||
absl::base_internal::FastTypeId (see https://crbug.com/1096380).
|
||||
|
||||
How to update Abseil:
|
||||
|
||||
1. Download the code from the Abseil git repository (see URL).
|
||||
|
||||
2. Copy the content of the Abseil git repo to //third_party/abseil-cpp.
|
||||
|
||||
3. From your source root run third_party/abseil-cpp/generate_def_files.py to
|
||||
regenerate Windows symbol definition files.
|
||||
|
||||
Local Modifications:
|
||||
|
||||
* absl/copts.bzl has been translated to //third_party/absl-cpp/BUILD.gn. Both
|
||||
files contain lists of compiler flags in order to reduce duplication.
|
||||
|
||||
* All the BUILD.bazel files have been translated to BUILD.gn files.
|
||||
|
||||
* Patches from //third_party/abseil-cpp/patches have been applied.
|
||||
|
||||
* Increment this number to silence presubmits about modifying files in
|
||||
third_party when regenerating absl .def files: 1
|
|
@ -0,0 +1,139 @@
|
|||
# Abseil - C++ Common Libraries
|
||||
|
||||
The repository contains the Abseil C++ library code. Abseil is an open-source
|
||||
collection of C++ code (compliant to C++11) designed to augment the C++
|
||||
standard library.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [About Abseil](#about)
|
||||
- [Quickstart](#quickstart)
|
||||
- [Building Abseil](#build)
|
||||
- [Support](#support)
|
||||
- [Codemap](#codemap)
|
||||
- [Releases](#releases)
|
||||
- [License](#license)
|
||||
- [Links](#links)
|
||||
|
||||
<a name="about"></a>
|
||||
## About Abseil
|
||||
|
||||
Abseil is an open-source collection of C++ library code designed to augment
|
||||
the C++ standard library. The Abseil library code is collected from Google's
|
||||
own C++ code base, has been extensively tested and used in production, and
|
||||
is the same code we depend on in our daily coding lives.
|
||||
|
||||
In some cases, Abseil provides pieces missing from the C++ standard; in
|
||||
others, Abseil provides alternatives to the standard for special needs
|
||||
we've found through usage in the Google code base. We denote those cases
|
||||
clearly within the library code we provide you.
|
||||
|
||||
Abseil is not meant to be a competitor to the standard library; we've
|
||||
just found that many of these utilities serve a purpose within our code
|
||||
base, and we now want to provide those resources to the C++ community as
|
||||
a whole.
|
||||
|
||||
<a name="quickstart"></a>
|
||||
## Quickstart
|
||||
|
||||
If you want to just get started, make sure you at least run through the
|
||||
[Abseil Quickstart](https://abseil.io/docs/cpp/quickstart). The Quickstart
|
||||
contains information about setting up your development environment, downloading
|
||||
the Abseil code, running tests, and getting a simple binary working.
|
||||
|
||||
<a name="build"></a>
|
||||
## Building Abseil
|
||||
|
||||
[Bazel](https://bazel.build) and [CMake](https://cmake.org/) are the official
|
||||
build systems for Abseil.
|
||||
|
||||
See the [quickstart](https://abseil.io/docs/cpp/quickstart) for more information
|
||||
on building Abseil using the Bazel build system.
|
||||
|
||||
If you require CMake support, please check the [CMake build
|
||||
instructions](CMake/README.md) and [CMake
|
||||
Quickstart](https://abseil.io/docs/cpp/quickstart-cmake).
|
||||
|
||||
## Support
|
||||
|
||||
Abseil is officially supported on many platforms. See the [Abseil
|
||||
platform support
|
||||
guide](https://abseil.io/docs/cpp/platforms/platforms) for details on
|
||||
supported operating systems, compilers, CPUs, etc.
|
||||
|
||||
## Codemap
|
||||
|
||||
Abseil contains the following C++ library components:
|
||||
|
||||
* [`base`](absl/base/) Abseil Fundamentals
|
||||
<br /> The `base` library contains initialization code and other code which
|
||||
all other Abseil code depends on. Code within `base` may not depend on any
|
||||
other code (other than the C++ standard library).
|
||||
* [`algorithm`](absl/algorithm/)
|
||||
<br /> The `algorithm` library contains additions to the C++ `<algorithm>`
|
||||
library and container-based versions of such algorithms.
|
||||
* [`cleanup`](absl/cleanup/)
|
||||
<br /> The `cleanup` library contains the control-flow-construct-like type
|
||||
`absl::Cleanup` which is used for executing a callback on scope exit.
|
||||
* [`container`](absl/container/)
|
||||
<br /> The `container` library contains additional STL-style containers,
|
||||
including Abseil's unordered "Swiss table" containers.
|
||||
* [`debugging`](absl/debugging/)
|
||||
<br /> The `debugging` library contains code useful for enabling leak
|
||||
checks, and stacktrace and symbolization utilities.
|
||||
* [`hash`](absl/hash/)
|
||||
<br /> The `hash` library contains the hashing framework and default hash
|
||||
functor implementations for hashable types in Abseil.
|
||||
* [`memory`](absl/memory/)
|
||||
<br /> The `memory` library contains C++11-compatible versions of
|
||||
`std::make_unique()` and related memory management facilities.
|
||||
* [`meta`](absl/meta/)
|
||||
<br /> The `meta` library contains C++11-compatible versions of type checks
|
||||
available within C++14 and C++17 versions of the C++ `<type_traits>` library.
|
||||
* [`numeric`](absl/numeric/)
|
||||
<br /> The `numeric` library contains C++11-compatible 128-bit integers.
|
||||
* [`status`](absl/status/)
|
||||
<br /> The `status` contains abstractions for error handling, specifically
|
||||
`absl::Status` and `absl::StatusOr<T>`.
|
||||
* [`strings`](absl/strings/)
|
||||
<br /> The `strings` library contains a variety of strings routines and
|
||||
utilities, including a C++11-compatible version of the C++17
|
||||
`std::string_view` type.
|
||||
* [`synchronization`](absl/synchronization/)
|
||||
<br /> The `synchronization` library contains concurrency primitives (Abseil's
|
||||
`absl::Mutex` class, an alternative to `std::mutex`) and a variety of
|
||||
synchronization abstractions.
|
||||
* [`time`](absl/time/)
|
||||
<br /> The `time` library contains abstractions for computing with absolute
|
||||
points in time, durations of time, and formatting and parsing time within
|
||||
time zones.
|
||||
* [`types`](absl/types/)
|
||||
<br /> The `types` library contains non-container utility types, like a
|
||||
C++11-compatible version of the C++17 `std::optional` type.
|
||||
* [`utility`](absl/utility/)
|
||||
<br /> The `utility` library contains utility and helper code.
|
||||
|
||||
## Releases
|
||||
|
||||
Abseil recommends users "live-at-head" (update to the latest commit from the
|
||||
master branch as often as possible). However, we realize this philosophy doesn't
|
||||
work for every project, so we also provide [Long Term Support
|
||||
Releases](https://github.com/abseil/abseil-cpp/releases) to which we backport
|
||||
fixes for severe bugs. See our [release
|
||||
management](https://abseil.io/about/releases) document for more details.
|
||||
|
||||
## License
|
||||
|
||||
The Abseil C++ library is licensed under the terms of the Apache
|
||||
license. See [LICENSE](LICENSE) for more information.
|
||||
|
||||
## Links
|
||||
|
||||
For more information about Abseil:
|
||||
|
||||
* Consult our [Abseil Introduction](https://abseil.io/about/intro)
|
||||
* Read [Why Adopt Abseil](https://abseil.io/about/philosophy) to understand our
|
||||
design philosophy.
|
||||
* Peruse our
|
||||
[Abseil Compatibility Guarantees](https://abseil.io/about/compatibility) to
|
||||
understand both what we promise to you, and what we expect of you in return.
|
|
@ -0,0 +1,17 @@
|
|||
# C++ Upgrade Tools
|
||||
|
||||
Abseil may occassionally release API-breaking changes. As noted in our
|
||||
[Compatibility Guidelines][compatibility-guide], we will aim to provide a tool
|
||||
to do the work of effecting such API-breaking changes, when absolutely
|
||||
necessary.
|
||||
|
||||
These tools will be listed on the [C++ Upgrade Tools][upgrade-tools] guide on
|
||||
https://abseil.io.
|
||||
|
||||
For more information, the [C++ Automated Upgrade Guide][api-upgrades-guide]
|
||||
outlines this process.
|
||||
|
||||
[compatibility-guide]: https://abseil.io/about/compatibility
|
||||
[api-upgrades-guide]: https://abseil.io/docs/cpp/tools/api-upgrades
|
||||
[upgrade-tools]: https://abseil.io/docs/cpp/tools/upgrades/
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
#
|
||||
# Copyright 2019 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.
|
||||
#
|
||||
|
||||
workspace(name = "com_google_absl")
|
||||
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
|
||||
# GoogleTest/GoogleMock framework. Used by most unit-tests.
|
||||
http_archive(
|
||||
name = "com_google_googletest", # 2021-07-09T13:28:13Z
|
||||
sha256 = "12ef65654dc01ab40f6f33f9d02c04f2097d2cd9fbe48dc6001b29543583b0ad",
|
||||
strip_prefix = "googletest-8d51ffdfab10b3fba636ae69bc03da4b54f8c235",
|
||||
# Keep this URL in sync with ABSL_GOOGLETEST_COMMIT in ci/cmake_common.sh.
|
||||
urls = ["https://github.com/google/googletest/archive/8d51ffdfab10b3fba636ae69bc03da4b54f8c235.zip"],
|
||||
)
|
||||
|
||||
# Google benchmark.
|
||||
http_archive(
|
||||
name = "com_github_google_benchmark", # 2021-07-01T09:02:54Z
|
||||
sha256 = "1cb4b97a90aa1fd9c8e412a6bc29fc13fc140162a4a0db3811af40befd8c9ea5",
|
||||
strip_prefix = "benchmark-e451e50e9b8af453f076dec10bd6890847f1624e",
|
||||
urls = ["https://github.com/google/benchmark/archive/e451e50e9b8af453f076dec10bd6890847f1624e.zip"],
|
||||
)
|
||||
|
||||
# C++ rules for Bazel.
|
||||
http_archive(
|
||||
name = "rules_cc", # 2021-06-07T16:41:49Z
|
||||
sha256 = "b295cad8c5899e371dde175079c0a2cdc0151f5127acc92366a8c986beb95c76",
|
||||
strip_prefix = "rules_cc-daf6ace7cfeacd6a83e9ff2ed659f416537b6c74",
|
||||
urls = ["https://github.com/bazelbuild/rules_cc/archive/daf6ace7cfeacd6a83e9ff2ed659f416537b6c74.zip"],
|
||||
)
|
||||
|
||||
# Bazel platform rules.
|
||||
http_archive(
|
||||
name = "platforms",
|
||||
sha256 = "b601beaf841244de5c5a50d2b2eddd34839788000fa1be4260ce6603ca0d8eb7",
|
||||
strip_prefix = "platforms-98939346da932eef0b54cf808622f5bb0928f00b",
|
||||
urls = ["https://github.com/bazelbuild/platforms/archive/98939346da932eef0b54cf808622f5bb0928f00b.zip"],
|
||||
)
|
|
@ -0,0 +1,72 @@
|
|||
#
|
||||
# Copyright 2017 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.
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
config_setting(
|
||||
name = "clang_compiler",
|
||||
flag_values = {
|
||||
"@bazel_tools//tools/cpp:compiler": "clang",
|
||||
},
|
||||
visibility = [":__subpackages__"],
|
||||
)
|
||||
|
||||
config_setting(
|
||||
name = "msvc_compiler",
|
||||
flag_values = {
|
||||
"@bazel_tools//tools/cpp:compiler": "msvc-cl",
|
||||
},
|
||||
visibility = [":__subpackages__"],
|
||||
)
|
||||
|
||||
config_setting(
|
||||
name = "clang-cl_compiler",
|
||||
flag_values = {
|
||||
"@bazel_tools//tools/cpp:compiler": "clang-cl",
|
||||
},
|
||||
visibility = [":__subpackages__"],
|
||||
)
|
||||
|
||||
config_setting(
|
||||
name = "osx",
|
||||
constraint_values = [
|
||||
"@platforms//os:osx",
|
||||
],
|
||||
)
|
||||
|
||||
config_setting(
|
||||
name = "ios",
|
||||
constraint_values = [
|
||||
"@platforms//os:ios",
|
||||
],
|
||||
)
|
||||
|
||||
config_setting(
|
||||
name = "ppc",
|
||||
values = {
|
||||
"cpu": "ppc",
|
||||
},
|
||||
visibility = [":__subpackages__"],
|
||||
)
|
||||
|
||||
config_setting(
|
||||
name = "wasm",
|
||||
values = {
|
||||
"cpu": "wasm32",
|
||||
},
|
||||
visibility = [":__subpackages__"],
|
||||
)
|
|
@ -0,0 +1,38 @@
|
|||
#
|
||||
# Copyright 2017 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.
|
||||
#
|
||||
|
||||
add_subdirectory(base)
|
||||
add_subdirectory(algorithm)
|
||||
add_subdirectory(cleanup)
|
||||
add_subdirectory(container)
|
||||
add_subdirectory(debugging)
|
||||
add_subdirectory(flags)
|
||||
add_subdirectory(functional)
|
||||
add_subdirectory(hash)
|
||||
add_subdirectory(memory)
|
||||
add_subdirectory(meta)
|
||||
add_subdirectory(numeric)
|
||||
add_subdirectory(random)
|
||||
add_subdirectory(status)
|
||||
add_subdirectory(strings)
|
||||
add_subdirectory(synchronization)
|
||||
add_subdirectory(time)
|
||||
add_subdirectory(types)
|
||||
add_subdirectory(utility)
|
||||
|
||||
if (${ABSL_BUILD_DLL})
|
||||
absl_make_dll()
|
||||
endif()
|
|
@ -0,0 +1,229 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""This script generates abseil.podspec from all BUILD.bazel files.
|
||||
|
||||
This is expected to run on abseil git repository with Bazel 1.0 on Linux.
|
||||
It recursively analyzes BUILD.bazel files using query command of Bazel to
|
||||
dump its build rules in XML format. From these rules, it constructs podspec
|
||||
structure.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import collections
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import xml.etree.ElementTree
|
||||
|
||||
# Template of root podspec.
|
||||
SPEC_TEMPLATE = """
|
||||
# This file has been automatically generated from a script.
|
||||
# Please make modifications to `abseil.podspec.gen.py` instead.
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'abseil'
|
||||
s.version = '${version}'
|
||||
s.summary = 'Abseil Common Libraries (C++) from Google'
|
||||
s.homepage = 'https://abseil.io'
|
||||
s.license = 'Apache License, Version 2.0'
|
||||
s.authors = { 'Abseil Team' => 'abseil-io@googlegroups.com' }
|
||||
s.source = {
|
||||
:git => 'https://github.com/abseil/abseil-cpp.git',
|
||||
:tag => '${tag}',
|
||||
}
|
||||
s.module_name = 'absl'
|
||||
s.header_mappings_dir = 'absl'
|
||||
s.header_dir = 'absl'
|
||||
s.libraries = 'c++'
|
||||
s.compiler_flags = '-Wno-everything'
|
||||
s.pod_target_xcconfig = {
|
||||
'USER_HEADER_SEARCH_PATHS' => '$(inherited) "$(PODS_TARGET_SRCROOT)"',
|
||||
'USE_HEADERMAP' => 'NO',
|
||||
'ALWAYS_SEARCH_USER_PATHS' => 'NO',
|
||||
}
|
||||
s.ios.deployment_target = '9.0'
|
||||
s.osx.deployment_target = '10.10'
|
||||
s.tvos.deployment_target = '9.0'
|
||||
s.watchos.deployment_target = '2.0'
|
||||
"""
|
||||
|
||||
# Rule object representing the rule of Bazel BUILD.
|
||||
Rule = collections.namedtuple(
|
||||
"Rule", "type name package srcs hdrs textual_hdrs deps visibility testonly")
|
||||
|
||||
|
||||
def get_elem_value(elem, name):
|
||||
"""Returns the value of XML element with the given name."""
|
||||
for child in elem:
|
||||
if child.attrib.get("name") != name:
|
||||
continue
|
||||
if child.tag == "string":
|
||||
return child.attrib.get("value")
|
||||
if child.tag == "boolean":
|
||||
return child.attrib.get("value") == "true"
|
||||
if child.tag == "list":
|
||||
return [nested_child.attrib.get("value") for nested_child in child]
|
||||
raise "Cannot recognize tag: " + child.tag
|
||||
return None
|
||||
|
||||
|
||||
def normalize_paths(paths):
|
||||
"""Returns the list of normalized path."""
|
||||
# e.g. ["//absl/strings:dir/header.h"] -> ["absl/strings/dir/header.h"]
|
||||
return [path.lstrip("/").replace(":", "/") for path in paths]
|
||||
|
||||
|
||||
def parse_rule(elem, package):
|
||||
"""Returns a rule from bazel XML rule."""
|
||||
return Rule(
|
||||
type=elem.attrib["class"],
|
||||
name=get_elem_value(elem, "name"),
|
||||
package=package,
|
||||
srcs=normalize_paths(get_elem_value(elem, "srcs") or []),
|
||||
hdrs=normalize_paths(get_elem_value(elem, "hdrs") or []),
|
||||
textual_hdrs=normalize_paths(get_elem_value(elem, "textual_hdrs") or []),
|
||||
deps=get_elem_value(elem, "deps") or [],
|
||||
visibility=get_elem_value(elem, "visibility") or [],
|
||||
testonly=get_elem_value(elem, "testonly") or False)
|
||||
|
||||
|
||||
def read_build(package):
|
||||
"""Runs bazel query on given package file and returns all cc rules."""
|
||||
result = subprocess.check_output(
|
||||
["bazel", "query", package + ":all", "--output", "xml"])
|
||||
root = xml.etree.ElementTree.fromstring(result)
|
||||
return [
|
||||
parse_rule(elem, package)
|
||||
for elem in root
|
||||
if elem.tag == "rule" and elem.attrib["class"].startswith("cc_")
|
||||
]
|
||||
|
||||
|
||||
def collect_rules(root_path):
|
||||
"""Collects and returns all rules from root path recursively."""
|
||||
rules = []
|
||||
for cur, _, _ in os.walk(root_path):
|
||||
build_path = os.path.join(cur, "BUILD.bazel")
|
||||
if os.path.exists(build_path):
|
||||
rules.extend(read_build("//" + cur))
|
||||
return rules
|
||||
|
||||
|
||||
def relevant_rule(rule):
|
||||
"""Returns true if a given rule is relevant when generating a podspec."""
|
||||
return (
|
||||
# cc_library only (ignore cc_test, cc_binary)
|
||||
rule.type == "cc_library" and
|
||||
# ignore empty rule
|
||||
(rule.hdrs + rule.textual_hdrs + rule.srcs) and
|
||||
# ignore test-only rule
|
||||
not rule.testonly)
|
||||
|
||||
|
||||
def get_spec_var(depth):
|
||||
"""Returns the name of variable for spec with given depth."""
|
||||
return "s" if depth == 0 else "s{}".format(depth)
|
||||
|
||||
|
||||
def get_spec_name(label):
|
||||
"""Converts the label of bazel rule to the name of podspec."""
|
||||
assert label.startswith("//absl/"), "{} doesn't start with //absl/".format(
|
||||
label)
|
||||
# e.g. //absl/apple/banana -> abseil/apple/banana
|
||||
return "abseil/" + label[7:]
|
||||
|
||||
|
||||
def write_podspec(f, rules, args):
|
||||
"""Writes a podspec from given rules and args."""
|
||||
rule_dir = build_rule_directory(rules)["abseil"]
|
||||
# Write root part with given arguments
|
||||
spec = re.sub(r"\$\{(\w+)\}", lambda x: args[x.group(1)],
|
||||
SPEC_TEMPLATE).lstrip()
|
||||
f.write(spec)
|
||||
# Write all target rules
|
||||
write_podspec_map(f, rule_dir, 0)
|
||||
f.write("end\n")
|
||||
|
||||
|
||||
def build_rule_directory(rules):
|
||||
"""Builds a tree-style rule directory from given rules."""
|
||||
rule_dir = {}
|
||||
for rule in rules:
|
||||
cur = rule_dir
|
||||
for frag in get_spec_name(rule.package).split("/"):
|
||||
cur = cur.setdefault(frag, {})
|
||||
cur[rule.name] = rule
|
||||
return rule_dir
|
||||
|
||||
|
||||
def write_podspec_map(f, cur_map, depth):
|
||||
"""Writes podspec from rule map recursively."""
|
||||
for key, value in sorted(cur_map.items()):
|
||||
indent = " " * (depth + 1)
|
||||
f.write("{indent}{var0}.subspec '{key}' do |{var1}|\n".format(
|
||||
indent=indent,
|
||||
key=key,
|
||||
var0=get_spec_var(depth),
|
||||
var1=get_spec_var(depth + 1)))
|
||||
if isinstance(value, dict):
|
||||
write_podspec_map(f, value, depth + 1)
|
||||
else:
|
||||
write_podspec_rule(f, value, depth + 1)
|
||||
f.write("{indent}end\n".format(indent=indent))
|
||||
|
||||
|
||||
def write_podspec_rule(f, rule, depth):
|
||||
"""Writes podspec from given rule."""
|
||||
indent = " " * (depth + 1)
|
||||
spec_var = get_spec_var(depth)
|
||||
# Puts all files in hdrs, textual_hdrs, and srcs into source_files.
|
||||
# Since CocoaPods treats header_files a bit differently from bazel,
|
||||
# this won't generate a header_files field so that all source_files
|
||||
# are considered as header files.
|
||||
srcs = sorted(set(rule.hdrs + rule.textual_hdrs + rule.srcs))
|
||||
write_indented_list(
|
||||
f, "{indent}{var}.source_files = ".format(indent=indent, var=spec_var),
|
||||
srcs)
|
||||
# Writes dependencies of this rule.
|
||||
for dep in sorted(rule.deps):
|
||||
name = get_spec_name(dep.replace(":", "/"))
|
||||
f.write("{indent}{var}.dependency '{dep}'\n".format(
|
||||
indent=indent, var=spec_var, dep=name))
|
||||
|
||||
|
||||
def write_indented_list(f, leading, values):
|
||||
"""Writes leading values in an indented style."""
|
||||
f.write(leading)
|
||||
f.write((",\n" + " " * len(leading)).join("'{}'".format(v) for v in values))
|
||||
f.write("\n")
|
||||
|
||||
|
||||
def generate(args):
|
||||
"""Generates a podspec file from all BUILD files under absl directory."""
|
||||
rules = filter(relevant_rule, collect_rules("absl"))
|
||||
with open(args.output, "wt") as f:
|
||||
write_podspec(f, rules, vars(args))
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generates abseil.podspec from BUILD.bazel")
|
||||
parser.add_argument(
|
||||
"-v", "--version", help="The version of podspec", required=True)
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--tag",
|
||||
default=None,
|
||||
help="The name of git tag (default: version)")
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
default="abseil.podspec",
|
||||
help="The name of output file (default: abseil.podspec)")
|
||||
args = parser.parse_args()
|
||||
if args.tag is None:
|
||||
args.tag = args.version
|
||||
generate(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,91 @@
|
|||
#
|
||||
# Copyright 2017 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.
|
||||
#
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
|
||||
load(
|
||||
"//absl:copts/configure_copts.bzl",
|
||||
"ABSL_DEFAULT_COPTS",
|
||||
"ABSL_DEFAULT_LINKOPTS",
|
||||
"ABSL_TEST_COPTS",
|
||||
)
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
cc_library(
|
||||
name = "algorithm",
|
||||
hdrs = ["algorithm.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
"//absl/base:config",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "algorithm_test",
|
||||
size = "small",
|
||||
srcs = ["algorithm_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":algorithm",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "algorithm_benchmark",
|
||||
srcs = ["equal_benchmark.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
tags = ["benchmark"],
|
||||
deps = [
|
||||
":algorithm",
|
||||
"//absl/base:core_headers",
|
||||
"@com_github_google_benchmark//:benchmark_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "container",
|
||||
hdrs = [
|
||||
"container.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":algorithm",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/meta:type_traits",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "container_test",
|
||||
srcs = ["container_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":container",
|
||||
"//absl/base",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/memory",
|
||||
"//absl/types:span",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
|
@ -0,0 +1,69 @@
|
|||
#
|
||||
# Copyright 2017 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.
|
||||
#
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
algorithm
|
||||
HDRS
|
||||
"algorithm.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
PUBLIC
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
algorithm_test
|
||||
SRCS
|
||||
"algorithm_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::algorithm
|
||||
GTest::gmock_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
algorithm_container
|
||||
HDRS
|
||||
"container.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::algorithm
|
||||
absl::core_headers
|
||||
absl::meta
|
||||
PUBLIC
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
container_test
|
||||
SRCS
|
||||
"container_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::algorithm_container
|
||||
absl::base
|
||||
absl::core_headers
|
||||
absl::memory
|
||||
absl::span
|
||||
GTest::gmock_main
|
||||
)
|
|
@ -0,0 +1,159 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: algorithm.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file contains Google extensions to the standard <algorithm> C++
|
||||
// header.
|
||||
|
||||
#ifndef ABSL_ALGORITHM_ALGORITHM_H_
|
||||
#define ABSL_ALGORITHM_ALGORITHM_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
namespace algorithm_internal {
|
||||
|
||||
// Performs comparisons with operator==, similar to C++14's `std::equal_to<>`.
|
||||
struct EqualTo {
|
||||
template <typename T, typename U>
|
||||
bool operator()(const T& a, const U& b) const {
|
||||
return a == b;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename InputIter1, typename InputIter2, typename Pred>
|
||||
bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
||||
InputIter2 last2, Pred pred, std::input_iterator_tag,
|
||||
std::input_iterator_tag) {
|
||||
while (true) {
|
||||
if (first1 == last1) return first2 == last2;
|
||||
if (first2 == last2) return false;
|
||||
if (!pred(*first1, *first2)) return false;
|
||||
++first1;
|
||||
++first2;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename InputIter1, typename InputIter2, typename Pred>
|
||||
bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
||||
InputIter2 last2, Pred&& pred, std::random_access_iterator_tag,
|
||||
std::random_access_iterator_tag) {
|
||||
return (last1 - first1 == last2 - first2) &&
|
||||
std::equal(first1, last1, first2, std::forward<Pred>(pred));
|
||||
}
|
||||
|
||||
// When we are using our own internal predicate that just applies operator==, we
|
||||
// forward to the non-predicate form of std::equal. This enables an optimization
|
||||
// in libstdc++ that can result in std::memcmp being used for integer types.
|
||||
template <typename InputIter1, typename InputIter2>
|
||||
bool EqualImpl(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
||||
InputIter2 last2, algorithm_internal::EqualTo /* unused */,
|
||||
std::random_access_iterator_tag,
|
||||
std::random_access_iterator_tag) {
|
||||
return (last1 - first1 == last2 - first2) &&
|
||||
std::equal(first1, last1, first2);
|
||||
}
|
||||
|
||||
template <typename It>
|
||||
It RotateImpl(It first, It middle, It last, std::true_type) {
|
||||
return std::rotate(first, middle, last);
|
||||
}
|
||||
|
||||
template <typename It>
|
||||
It RotateImpl(It first, It middle, It last, std::false_type) {
|
||||
std::rotate(first, middle, last);
|
||||
return std::next(first, std::distance(middle, last));
|
||||
}
|
||||
|
||||
} // namespace algorithm_internal
|
||||
|
||||
// equal()
|
||||
//
|
||||
// Compares the equality of two ranges specified by pairs of iterators, using
|
||||
// the given predicate, returning true iff for each corresponding iterator i1
|
||||
// and i2 in the first and second range respectively, pred(*i1, *i2) == true
|
||||
//
|
||||
// This comparison takes at most min(`last1` - `first1`, `last2` - `first2`)
|
||||
// invocations of the predicate. Additionally, if InputIter1 and InputIter2 are
|
||||
// both random-access iterators, and `last1` - `first1` != `last2` - `first2`,
|
||||
// then the predicate is never invoked and the function returns false.
|
||||
//
|
||||
// This is a C++11-compatible implementation of C++14 `std::equal`. See
|
||||
// https://en.cppreference.com/w/cpp/algorithm/equal for more information.
|
||||
template <typename InputIter1, typename InputIter2, typename Pred>
|
||||
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
||||
InputIter2 last2, Pred&& pred) {
|
||||
return algorithm_internal::EqualImpl(
|
||||
first1, last1, first2, last2, std::forward<Pred>(pred),
|
||||
typename std::iterator_traits<InputIter1>::iterator_category{},
|
||||
typename std::iterator_traits<InputIter2>::iterator_category{});
|
||||
}
|
||||
|
||||
// Overload of equal() that performs comparison of two ranges specified by pairs
|
||||
// of iterators using operator==.
|
||||
template <typename InputIter1, typename InputIter2>
|
||||
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2,
|
||||
InputIter2 last2) {
|
||||
return absl::equal(first1, last1, first2, last2,
|
||||
algorithm_internal::EqualTo{});
|
||||
}
|
||||
|
||||
// linear_search()
|
||||
//
|
||||
// Performs a linear search for `value` using the iterator `first` up to
|
||||
// but not including `last`, returning true if [`first`, `last`) contains an
|
||||
// element equal to `value`.
|
||||
//
|
||||
// A linear search is of O(n) complexity which is guaranteed to make at most
|
||||
// n = (`last` - `first`) comparisons. A linear search over short containers
|
||||
// may be faster than a binary search, even when the container is sorted.
|
||||
template <typename InputIterator, typename EqualityComparable>
|
||||
bool linear_search(InputIterator first, InputIterator last,
|
||||
const EqualityComparable& value) {
|
||||
return std::find(first, last, value) != last;
|
||||
}
|
||||
|
||||
// rotate()
|
||||
//
|
||||
// Performs a left rotation on a range of elements (`first`, `last`) such that
|
||||
// `middle` is now the first element. `rotate()` returns an iterator pointing to
|
||||
// the first element before rotation. This function is exactly the same as
|
||||
// `std::rotate`, but fixes a bug in gcc
|
||||
// <= 4.9 where `std::rotate` returns `void` instead of an iterator.
|
||||
//
|
||||
// The complexity of this algorithm is the same as that of `std::rotate`, but if
|
||||
// `ForwardIterator` is not a random-access iterator, then `absl::rotate`
|
||||
// performs an additional pass over the range to construct the return value.
|
||||
template <typename ForwardIterator>
|
||||
ForwardIterator rotate(ForwardIterator first, ForwardIterator middle,
|
||||
ForwardIterator last) {
|
||||
return algorithm_internal::RotateImpl(
|
||||
first, middle, last,
|
||||
std::is_same<decltype(std::rotate(first, middle, last)),
|
||||
ForwardIterator>());
|
||||
}
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_ALGORITHM_ALGORITHM_H_
|
|
@ -0,0 +1,182 @@
|
|||
// Copyright 2017 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/algorithm/algorithm.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(EqualTest, DefaultComparisonRandomAccess) {
|
||||
std::vector<int> v1{1, 2, 3};
|
||||
std::vector<int> v2 = v1;
|
||||
std::vector<int> v3 = {1, 2};
|
||||
std::vector<int> v4 = {1, 2, 4};
|
||||
|
||||
EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
|
||||
}
|
||||
|
||||
TEST(EqualTest, DefaultComparison) {
|
||||
std::list<int> lst1{1, 2, 3};
|
||||
std::list<int> lst2 = lst1;
|
||||
std::list<int> lst3{1, 2};
|
||||
std::list<int> lst4{1, 2, 4};
|
||||
|
||||
EXPECT_TRUE(absl::equal(lst1.begin(), lst1.end(), lst2.begin(), lst2.end()));
|
||||
EXPECT_FALSE(absl::equal(lst1.begin(), lst1.end(), lst3.begin(), lst3.end()));
|
||||
EXPECT_FALSE(absl::equal(lst1.begin(), lst1.end(), lst4.begin(), lst4.end()));
|
||||
}
|
||||
|
||||
TEST(EqualTest, EmptyRange) {
|
||||
std::vector<int> v1{1, 2, 3};
|
||||
std::vector<int> empty1;
|
||||
std::vector<int> empty2;
|
||||
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), empty1.begin(), empty1.end()));
|
||||
EXPECT_FALSE(absl::equal(empty1.begin(), empty1.end(), v1.begin(), v1.end()));
|
||||
EXPECT_TRUE(
|
||||
absl::equal(empty1.begin(), empty1.end(), empty2.begin(), empty2.end()));
|
||||
}
|
||||
|
||||
TEST(EqualTest, MixedIterTypes) {
|
||||
std::vector<int> v1{1, 2, 3};
|
||||
std::list<int> lst1{v1.begin(), v1.end()};
|
||||
std::list<int> lst2{1, 2, 4};
|
||||
std::list<int> lst3{1, 2};
|
||||
|
||||
EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), lst1.begin(), lst1.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), lst2.begin(), lst2.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), lst3.begin(), lst3.end()));
|
||||
}
|
||||
|
||||
TEST(EqualTest, MixedValueTypes) {
|
||||
std::vector<int> v1{1, 2, 3};
|
||||
std::vector<char> v2{1, 2, 3};
|
||||
std::vector<char> v3{1, 2};
|
||||
std::vector<char> v4{1, 2, 4};
|
||||
|
||||
EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
|
||||
}
|
||||
|
||||
TEST(EqualTest, WeirdIterators) {
|
||||
std::vector<bool> v1{true, false};
|
||||
std::vector<bool> v2 = v1;
|
||||
std::vector<bool> v3{true};
|
||||
std::vector<bool> v4{true, true, true};
|
||||
|
||||
EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end()));
|
||||
}
|
||||
|
||||
TEST(EqualTest, CustomComparison) {
|
||||
int n[] = {1, 2, 3, 4};
|
||||
std::vector<int*> v1{&n[0], &n[1], &n[2]};
|
||||
std::vector<int*> v2 = v1;
|
||||
std::vector<int*> v3{&n[0], &n[1], &n[3]};
|
||||
std::vector<int*> v4{&n[0], &n[1]};
|
||||
|
||||
auto eq = [](int* a, int* b) { return *a == *b; };
|
||||
|
||||
EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(), eq));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(), eq));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v4.begin(), v4.end(), eq));
|
||||
}
|
||||
|
||||
TEST(EqualTest, MoveOnlyPredicate) {
|
||||
std::vector<int> v1{1, 2, 3};
|
||||
std::vector<int> v2{4, 5, 6};
|
||||
|
||||
// move-only equality predicate
|
||||
struct Eq {
|
||||
Eq() = default;
|
||||
Eq(Eq &&) = default;
|
||||
Eq(const Eq &) = delete;
|
||||
Eq &operator=(const Eq &) = delete;
|
||||
bool operator()(const int a, const int b) const { return a == b; }
|
||||
};
|
||||
|
||||
EXPECT_TRUE(absl::equal(v1.begin(), v1.end(), v1.begin(), v1.end(), Eq()));
|
||||
EXPECT_FALSE(absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(), Eq()));
|
||||
}
|
||||
|
||||
struct CountingTrivialPred {
|
||||
int* count;
|
||||
bool operator()(int, int) const {
|
||||
++*count;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(EqualTest, RandomAccessComplexity) {
|
||||
std::vector<int> v1{1, 1, 3};
|
||||
std::vector<int> v2 = v1;
|
||||
std::vector<int> v3{1, 2};
|
||||
|
||||
do {
|
||||
int count = 0;
|
||||
absl::equal(v1.begin(), v1.end(), v2.begin(), v2.end(),
|
||||
CountingTrivialPred{&count});
|
||||
EXPECT_LE(count, 3);
|
||||
} while (std::next_permutation(v2.begin(), v2.end()));
|
||||
|
||||
int count = 0;
|
||||
absl::equal(v1.begin(), v1.end(), v3.begin(), v3.end(),
|
||||
CountingTrivialPred{&count});
|
||||
EXPECT_EQ(count, 0);
|
||||
}
|
||||
|
||||
class LinearSearchTest : public testing::Test {
|
||||
protected:
|
||||
LinearSearchTest() : container_{1, 2, 3} {}
|
||||
|
||||
static bool Is3(int n) { return n == 3; }
|
||||
static bool Is4(int n) { return n == 4; }
|
||||
|
||||
std::vector<int> container_;
|
||||
};
|
||||
|
||||
TEST_F(LinearSearchTest, linear_search) {
|
||||
EXPECT_TRUE(absl::linear_search(container_.begin(), container_.end(), 3));
|
||||
EXPECT_FALSE(absl::linear_search(container_.begin(), container_.end(), 4));
|
||||
}
|
||||
|
||||
TEST_F(LinearSearchTest, linear_searchConst) {
|
||||
const std::vector<int> *const const_container = &container_;
|
||||
EXPECT_TRUE(
|
||||
absl::linear_search(const_container->begin(), const_container->end(), 3));
|
||||
EXPECT_FALSE(
|
||||
absl::linear_search(const_container->begin(), const_container->end(), 4));
|
||||
}
|
||||
|
||||
TEST(RotateTest, Rotate) {
|
||||
std::vector<int> v{0, 1, 2, 3, 4};
|
||||
EXPECT_EQ(*absl::rotate(v.begin(), v.begin() + 2, v.end()), 0);
|
||||
EXPECT_THAT(v, testing::ElementsAreArray({2, 3, 4, 0, 1}));
|
||||
|
||||
std::list<int> l{0, 1, 2, 3, 4};
|
||||
EXPECT_EQ(*absl::rotate(l.begin(), std::next(l.begin(), 3), l.end()), 0);
|
||||
EXPECT_THAT(l, testing::ElementsAreArray({3, 4, 0, 1, 2}));
|
||||
}
|
||||
|
||||
} // namespace
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,126 @@
|
|||
// Copyright 2017 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 <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#include "benchmark/benchmark.h"
|
||||
#include "absl/algorithm/algorithm.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// The range of sequence sizes to benchmark.
|
||||
constexpr int kMinBenchmarkSize = 1024;
|
||||
constexpr int kMaxBenchmarkSize = 8 * 1024 * 1024;
|
||||
|
||||
// A user-defined type for use in equality benchmarks. Note that we expect
|
||||
// std::memcmp to win for this type: libstdc++'s std::equal only defers to
|
||||
// memcmp for integral types. This is because it is not straightforward to
|
||||
// guarantee that std::memcmp would produce a result "as-if" compared by
|
||||
// operator== for other types (example gotchas: NaN floats, structs with
|
||||
// padding).
|
||||
struct EightBits {
|
||||
explicit EightBits(int /* unused */) : data(0) {}
|
||||
bool operator==(const EightBits& rhs) const { return data == rhs.data; }
|
||||
uint8_t data;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void BM_absl_equal_benchmark(benchmark::State& state) {
|
||||
std::vector<T> xs(state.range(0), T(0));
|
||||
std::vector<T> ys = xs;
|
||||
while (state.KeepRunning()) {
|
||||
const bool same = absl::equal(xs.begin(), xs.end(), ys.begin(), ys.end());
|
||||
benchmark::DoNotOptimize(same);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void BM_std_equal_benchmark(benchmark::State& state) {
|
||||
std::vector<T> xs(state.range(0), T(0));
|
||||
std::vector<T> ys = xs;
|
||||
while (state.KeepRunning()) {
|
||||
const bool same = std::equal(xs.begin(), xs.end(), ys.begin());
|
||||
benchmark::DoNotOptimize(same);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void BM_memcmp_benchmark(benchmark::State& state) {
|
||||
std::vector<T> xs(state.range(0), T(0));
|
||||
std::vector<T> ys = xs;
|
||||
while (state.KeepRunning()) {
|
||||
const bool same =
|
||||
std::memcmp(xs.data(), ys.data(), xs.size() * sizeof(T)) == 0;
|
||||
benchmark::DoNotOptimize(same);
|
||||
}
|
||||
}
|
||||
|
||||
// The expectation is that the compiler should be able to elide the equality
|
||||
// comparison altogether for sufficiently simple types.
|
||||
template <typename T>
|
||||
void BM_absl_equal_self_benchmark(benchmark::State& state) {
|
||||
std::vector<T> xs(state.range(0), T(0));
|
||||
while (state.KeepRunning()) {
|
||||
const bool same = absl::equal(xs.begin(), xs.end(), xs.begin(), xs.end());
|
||||
benchmark::DoNotOptimize(same);
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint8_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint8_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint8_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint8_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint16_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint16_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint16_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint16_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint32_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint32_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint32_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint32_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, uint64_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_std_equal_benchmark, uint64_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_memcmp_benchmark, uint64_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, uint64_t)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_benchmark, EightBits)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_std_equal_benchmark, EightBits)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_memcmp_benchmark, EightBits)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
BENCHMARK_TEMPLATE(BM_absl_equal_self_benchmark, EightBits)
|
||||
->Range(kMinBenchmarkSize, kMaxBenchmarkSize);
|
||||
|
||||
} // namespace
|
|
@ -0,0 +1,800 @@
|
|||
#
|
||||
# Copyright 2017 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.
|
||||
#
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
|
||||
load(
|
||||
"//absl:copts/configure_copts.bzl",
|
||||
"ABSL_DEFAULT_COPTS",
|
||||
"ABSL_DEFAULT_LINKOPTS",
|
||||
"ABSL_TEST_COPTS",
|
||||
)
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
cc_library(
|
||||
name = "atomic_hook",
|
||||
hdrs = ["internal/atomic_hook.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
":core_headers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "errno_saver",
|
||||
hdrs = ["internal/errno_saver.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [":config"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "log_severity",
|
||||
srcs = ["log_severity.cc"],
|
||||
hdrs = ["log_severity.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":config",
|
||||
":core_headers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "raw_logging_internal",
|
||||
srcs = ["internal/raw_logging.cc"],
|
||||
hdrs = ["internal/raw_logging.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":atomic_hook",
|
||||
":config",
|
||||
":core_headers",
|
||||
":log_severity",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "spinlock_wait",
|
||||
srcs = [
|
||||
"internal/spinlock_akaros.inc",
|
||||
"internal/spinlock_linux.inc",
|
||||
"internal/spinlock_posix.inc",
|
||||
"internal/spinlock_wait.cc",
|
||||
"internal/spinlock_win32.inc",
|
||||
],
|
||||
hdrs = ["internal/spinlock_wait.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl/base:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":base_internal",
|
||||
":core_headers",
|
||||
":errno_saver",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "config",
|
||||
hdrs = [
|
||||
"config.h",
|
||||
"options.h",
|
||||
"policy_checks.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "dynamic_annotations",
|
||||
srcs = [
|
||||
"internal/dynamic_annotations.h",
|
||||
],
|
||||
hdrs = [
|
||||
"dynamic_annotations.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":config",
|
||||
":core_headers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "core_headers",
|
||||
srcs = [
|
||||
"internal/thread_annotations.h",
|
||||
],
|
||||
hdrs = [
|
||||
"attributes.h",
|
||||
"const_init.h",
|
||||
"macros.h",
|
||||
"optimization.h",
|
||||
"port.h",
|
||||
"thread_annotations.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":config",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "malloc_internal",
|
||||
srcs = [
|
||||
"internal/low_level_alloc.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"internal/direct_mmap.h",
|
||||
"internal/low_level_alloc.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = select({
|
||||
"//absl:msvc_compiler": [],
|
||||
"//absl:clang-cl_compiler": [],
|
||||
"//absl:wasm": [],
|
||||
"//conditions:default": ["-pthread"],
|
||||
}) + ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//visibility:public",
|
||||
],
|
||||
deps = [
|
||||
":base",
|
||||
":base_internal",
|
||||
":config",
|
||||
":core_headers",
|
||||
":dynamic_annotations",
|
||||
":raw_logging_internal",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "base_internal",
|
||||
hdrs = [
|
||||
"internal/hide_ptr.h",
|
||||
"internal/identity.h",
|
||||
"internal/inline_variable.h",
|
||||
"internal/invoke.h",
|
||||
"internal/scheduling_mode.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
"//absl/meta:type_traits",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "base",
|
||||
srcs = [
|
||||
"internal/cycleclock.cc",
|
||||
"internal/spinlock.cc",
|
||||
"internal/sysinfo.cc",
|
||||
"internal/thread_identity.cc",
|
||||
"internal/unscaledcycleclock.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"call_once.h",
|
||||
"casts.h",
|
||||
"internal/cycleclock.h",
|
||||
"internal/low_level_scheduling.h",
|
||||
"internal/per_thread_tls.h",
|
||||
"internal/spinlock.h",
|
||||
"internal/sysinfo.h",
|
||||
"internal/thread_identity.h",
|
||||
"internal/tsan_mutex_interface.h",
|
||||
"internal/unscaledcycleclock.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = select({
|
||||
"//absl:msvc_compiler": [
|
||||
"-DEFAULTLIB:advapi32.lib",
|
||||
],
|
||||
"//absl:clang-cl_compiler": [
|
||||
"-DEFAULTLIB:advapi32.lib",
|
||||
],
|
||||
"//absl:wasm": [],
|
||||
"//conditions:default": ["-pthread"],
|
||||
}) + ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":atomic_hook",
|
||||
":base_internal",
|
||||
":config",
|
||||
":core_headers",
|
||||
":dynamic_annotations",
|
||||
":log_severity",
|
||||
":raw_logging_internal",
|
||||
":spinlock_wait",
|
||||
"//absl/meta:type_traits",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "atomic_hook_test_helper",
|
||||
testonly = 1,
|
||||
srcs = ["internal/atomic_hook_test_helper.cc"],
|
||||
hdrs = ["internal/atomic_hook_test_helper.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":atomic_hook",
|
||||
":core_headers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "atomic_hook_test",
|
||||
size = "small",
|
||||
srcs = ["internal/atomic_hook_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":atomic_hook",
|
||||
":atomic_hook_test_helper",
|
||||
":core_headers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "bit_cast_test",
|
||||
size = "small",
|
||||
srcs = [
|
||||
"bit_cast_test.cc",
|
||||
],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base",
|
||||
":core_headers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "throw_delegate",
|
||||
srcs = ["internal/throw_delegate.cc"],
|
||||
hdrs = ["internal/throw_delegate.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
":raw_logging_internal",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "throw_delegate_test",
|
||||
srcs = ["throw_delegate_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":config",
|
||||
":throw_delegate",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "errno_saver_test",
|
||||
size = "small",
|
||||
srcs = ["internal/errno_saver_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":errno_saver",
|
||||
":strerror",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "exception_testing",
|
||||
testonly = 1,
|
||||
hdrs = ["internal/exception_testing.h"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
"@com_google_googletest//:gtest",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "pretty_function",
|
||||
hdrs = ["internal/pretty_function.h"],
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = ["//absl:__subpackages__"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "exception_safety_testing",
|
||||
testonly = 1,
|
||||
srcs = ["internal/exception_safety_testing.cc"],
|
||||
hdrs = ["internal/exception_safety_testing.h"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":config",
|
||||
":pretty_function",
|
||||
"//absl/memory",
|
||||
"//absl/meta:type_traits",
|
||||
"//absl/strings",
|
||||
"//absl/utility",
|
||||
"@com_google_googletest//:gtest",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "exception_safety_testing_test",
|
||||
srcs = ["exception_safety_testing_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":exception_safety_testing",
|
||||
"//absl/memory",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "inline_variable_test",
|
||||
size = "small",
|
||||
srcs = [
|
||||
"inline_variable_test.cc",
|
||||
"inline_variable_test_a.cc",
|
||||
"inline_variable_test_b.cc",
|
||||
"internal/inline_variable_testing.h",
|
||||
],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base_internal",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "invoke_test",
|
||||
size = "small",
|
||||
srcs = ["invoke_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base_internal",
|
||||
"//absl/memory",
|
||||
"//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
# Common test library made available for use in non-absl code that overrides
|
||||
# AbslInternalSpinLockDelay and AbslInternalSpinLockWake.
|
||||
cc_library(
|
||||
name = "spinlock_test_common",
|
||||
testonly = 1,
|
||||
srcs = ["spinlock_test_common.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base",
|
||||
":base_internal",
|
||||
":config",
|
||||
":core_headers",
|
||||
"//absl/synchronization",
|
||||
"@com_google_googletest//:gtest",
|
||||
],
|
||||
alwayslink = 1,
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "spinlock_test",
|
||||
size = "medium",
|
||||
srcs = ["spinlock_test_common.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base",
|
||||
":base_internal",
|
||||
":config",
|
||||
":core_headers",
|
||||
"//absl/synchronization",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "spinlock_benchmark_common",
|
||||
testonly = 1,
|
||||
srcs = ["internal/spinlock_benchmark.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl/base:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":base",
|
||||
":base_internal",
|
||||
":raw_logging_internal",
|
||||
"//absl/synchronization",
|
||||
"@com_github_google_benchmark//:benchmark_main",
|
||||
],
|
||||
alwayslink = 1,
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "spinlock_benchmark",
|
||||
testonly = 1,
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
tags = ["benchmark"],
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
":spinlock_benchmark_common",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "endian",
|
||||
hdrs = [
|
||||
"internal/endian.h",
|
||||
"internal/unaligned_access.h",
|
||||
],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base",
|
||||
":config",
|
||||
":core_headers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "endian_test",
|
||||
srcs = ["internal/endian_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
deps = [
|
||||
":config",
|
||||
":endian",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "config_test",
|
||||
srcs = ["config_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":config",
|
||||
"//absl/synchronization:thread_pool",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "call_once_test",
|
||||
srcs = ["call_once_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base",
|
||||
":core_headers",
|
||||
"//absl/synchronization",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "raw_logging_test",
|
||||
srcs = ["raw_logging_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":raw_logging_internal",
|
||||
"//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "sysinfo_test",
|
||||
size = "small",
|
||||
srcs = ["internal/sysinfo_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base",
|
||||
"//absl/synchronization",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "low_level_alloc_test",
|
||||
size = "medium",
|
||||
srcs = ["internal/low_level_alloc_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
tags = [
|
||||
"no_test_ios_x86_64",
|
||||
],
|
||||
deps = [
|
||||
":malloc_internal",
|
||||
"//absl/container:node_hash_map",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "thread_identity_test",
|
||||
size = "small",
|
||||
srcs = ["internal/thread_identity_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":base",
|
||||
":core_headers",
|
||||
"//absl/synchronization",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "thread_identity_benchmark",
|
||||
srcs = ["internal/thread_identity_benchmark.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
tags = ["benchmark"],
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
":base",
|
||||
"//absl/synchronization",
|
||||
"@com_github_google_benchmark//:benchmark_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "exponential_biased",
|
||||
srcs = ["internal/exponential_biased.cc"],
|
||||
hdrs = ["internal/exponential_biased.h"],
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
":core_headers",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "exponential_biased_test",
|
||||
size = "small",
|
||||
srcs = ["internal/exponential_biased_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
":exponential_biased",
|
||||
"//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "periodic_sampler",
|
||||
srcs = ["internal/periodic_sampler.cc"],
|
||||
hdrs = ["internal/periodic_sampler.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":core_headers",
|
||||
":exponential_biased",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "periodic_sampler_test",
|
||||
size = "small",
|
||||
srcs = ["internal/periodic_sampler_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
":core_headers",
|
||||
":periodic_sampler",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "periodic_sampler_benchmark",
|
||||
testonly = 1,
|
||||
srcs = ["internal/periodic_sampler_benchmark.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
tags = ["benchmark"],
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
":core_headers",
|
||||
":periodic_sampler",
|
||||
"@com_github_google_benchmark//:benchmark_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "scoped_set_env",
|
||||
testonly = 1,
|
||||
srcs = ["internal/scoped_set_env.cc"],
|
||||
hdrs = ["internal/scoped_set_env.h"],
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
":raw_logging_internal",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "scoped_set_env_test",
|
||||
size = "small",
|
||||
srcs = ["internal/scoped_set_env_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":scoped_set_env",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "log_severity_test",
|
||||
size = "small",
|
||||
srcs = ["log_severity_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":log_severity",
|
||||
"//absl/flags:flag_internal",
|
||||
"//absl/flags:marshalling",
|
||||
"//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "strerror",
|
||||
srcs = ["internal/strerror.cc"],
|
||||
hdrs = ["internal/strerror.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
":core_headers",
|
||||
":errno_saver",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "strerror_test",
|
||||
size = "small",
|
||||
srcs = ["internal/strerror_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":strerror",
|
||||
"//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "strerror_benchmark",
|
||||
testonly = 1,
|
||||
srcs = ["internal/strerror_benchmark.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
tags = ["benchmark"],
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
":strerror",
|
||||
"@com_github_google_benchmark//:benchmark_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "fast_type_id",
|
||||
hdrs = ["internal/fast_type_id.h"],
|
||||
copts = ABSL_DEFAULT_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
visibility = [
|
||||
"//absl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":config",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "fast_type_id_test",
|
||||
size = "small",
|
||||
srcs = ["internal/fast_type_id_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":fast_type_id",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "unique_small_name_test",
|
||||
size = "small",
|
||||
srcs = ["internal/unique_small_name_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
linkstatic = 1,
|
||||
deps = [
|
||||
":core_headers",
|
||||
"//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "optimization_test",
|
||||
size = "small",
|
||||
srcs = ["optimization_test.cc"],
|
||||
copts = ABSL_TEST_COPTS,
|
||||
linkopts = ABSL_DEFAULT_LINKOPTS,
|
||||
deps = [
|
||||
":core_headers",
|
||||
"//absl/types:optional",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
|
@ -0,0 +1,694 @@
|
|||
#
|
||||
# Copyright 2017 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.
|
||||
#
|
||||
|
||||
find_library(LIBRT rt)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
atomic_hook
|
||||
HDRS
|
||||
"internal/atomic_hook.h"
|
||||
DEPS
|
||||
absl::config
|
||||
absl::core_headers
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
errno_saver
|
||||
HDRS
|
||||
"internal/errno_saver.h"
|
||||
DEPS
|
||||
absl::config
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
log_severity
|
||||
HDRS
|
||||
"log_severity.h"
|
||||
SRCS
|
||||
"log_severity.cc"
|
||||
DEPS
|
||||
absl::core_headers
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
raw_logging_internal
|
||||
HDRS
|
||||
"internal/raw_logging.h"
|
||||
SRCS
|
||||
"internal/raw_logging.cc"
|
||||
DEPS
|
||||
absl::atomic_hook
|
||||
absl::config
|
||||
absl::core_headers
|
||||
absl::log_severity
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
spinlock_wait
|
||||
HDRS
|
||||
"internal/spinlock_wait.h"
|
||||
SRCS
|
||||
"internal/spinlock_akaros.inc"
|
||||
"internal/spinlock_linux.inc"
|
||||
"internal/spinlock_posix.inc"
|
||||
"internal/spinlock_wait.cc"
|
||||
"internal/spinlock_win32.inc"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::base_internal
|
||||
absl::core_headers
|
||||
absl::errno_saver
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
config
|
||||
HDRS
|
||||
"config.h"
|
||||
"options.h"
|
||||
"policy_checks.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
PUBLIC
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
dynamic_annotations
|
||||
HDRS
|
||||
"dynamic_annotations.h"
|
||||
SRCS
|
||||
"internal/dynamic_annotations.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
PUBLIC
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
core_headers
|
||||
HDRS
|
||||
"attributes.h"
|
||||
"const_init.h"
|
||||
"macros.h"
|
||||
"optimization.h"
|
||||
"port.h"
|
||||
"thread_annotations.h"
|
||||
"internal/thread_annotations.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
PUBLIC
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
malloc_internal
|
||||
HDRS
|
||||
"internal/direct_mmap.h"
|
||||
"internal/low_level_alloc.h"
|
||||
SRCS
|
||||
"internal/low_level_alloc.cc"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::base_internal
|
||||
absl::config
|
||||
absl::core_headers
|
||||
absl::dynamic_annotations
|
||||
absl::raw_logging_internal
|
||||
Threads::Threads
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
base_internal
|
||||
HDRS
|
||||
"internal/hide_ptr.h"
|
||||
"internal/identity.h"
|
||||
"internal/inline_variable.h"
|
||||
"internal/invoke.h"
|
||||
"internal/scheduling_mode.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::type_traits
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
base
|
||||
HDRS
|
||||
"call_once.h"
|
||||
"casts.h"
|
||||
"internal/cycleclock.h"
|
||||
"internal/low_level_scheduling.h"
|
||||
"internal/per_thread_tls.h"
|
||||
"internal/spinlock.h"
|
||||
"internal/sysinfo.h"
|
||||
"internal/thread_identity.h"
|
||||
"internal/tsan_mutex_interface.h"
|
||||
"internal/unscaledcycleclock.h"
|
||||
SRCS
|
||||
"internal/cycleclock.cc"
|
||||
"internal/spinlock.cc"
|
||||
"internal/sysinfo.cc"
|
||||
"internal/thread_identity.cc"
|
||||
"internal/unscaledcycleclock.cc"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
LINKOPTS
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
$<$<BOOL:${LIBRT}>:-lrt>
|
||||
$<$<BOOL:${MINGW}>:"advapi32">
|
||||
DEPS
|
||||
absl::atomic_hook
|
||||
absl::base_internal
|
||||
absl::config
|
||||
absl::core_headers
|
||||
absl::dynamic_annotations
|
||||
absl::log_severity
|
||||
absl::raw_logging_internal
|
||||
absl::spinlock_wait
|
||||
absl::type_traits
|
||||
Threads::Threads
|
||||
PUBLIC
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
throw_delegate
|
||||
HDRS
|
||||
"internal/throw_delegate.h"
|
||||
SRCS
|
||||
"internal/throw_delegate.cc"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::raw_logging_internal
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
exception_testing
|
||||
HDRS
|
||||
"internal/exception_testing.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
GTest::gtest
|
||||
TESTONLY
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
pretty_function
|
||||
HDRS
|
||||
"internal/pretty_function.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
exception_safety_testing
|
||||
HDRS
|
||||
"internal/exception_safety_testing.h"
|
||||
SRCS
|
||||
"internal/exception_safety_testing.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::pretty_function
|
||||
absl::memory
|
||||
absl::meta
|
||||
absl::strings
|
||||
absl::utility
|
||||
GTest::gtest
|
||||
TESTONLY
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
absl_exception_safety_testing_test
|
||||
SRCS
|
||||
"exception_safety_testing_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::exception_safety_testing
|
||||
absl::memory
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
atomic_hook_test_helper
|
||||
SRCS
|
||||
"internal/atomic_hook_test_helper.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::atomic_hook
|
||||
absl::core_headers
|
||||
TESTONLY
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
atomic_hook_test
|
||||
SRCS
|
||||
"internal/atomic_hook_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::atomic_hook_test_helper
|
||||
absl::atomic_hook
|
||||
absl::core_headers
|
||||
GTest::gmock
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
bit_cast_test
|
||||
SRCS
|
||||
"bit_cast_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::core_headers
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
errno_saver_test
|
||||
SRCS
|
||||
"internal/errno_saver_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::errno_saver
|
||||
absl::strerror
|
||||
GTest::gmock
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
throw_delegate_test
|
||||
SRCS
|
||||
"throw_delegate_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::config
|
||||
absl::throw_delegate
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
inline_variable_test
|
||||
SRCS
|
||||
"internal/inline_variable_testing.h"
|
||||
"inline_variable_test.cc"
|
||||
"inline_variable_test_a.cc"
|
||||
"inline_variable_test_b.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base_internal
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
invoke_test
|
||||
SRCS
|
||||
"invoke_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base_internal
|
||||
absl::memory
|
||||
absl::strings
|
||||
GTest::gmock
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
spinlock_test_common
|
||||
SRCS
|
||||
"spinlock_test_common.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::config
|
||||
absl::base_internal
|
||||
absl::core_headers
|
||||
absl::synchronization
|
||||
GTest::gtest
|
||||
TESTONLY
|
||||
)
|
||||
|
||||
# On bazel BUILD this target use "alwayslink = 1" which is not implemented here
|
||||
absl_cc_test(
|
||||
NAME
|
||||
spinlock_test
|
||||
SRCS
|
||||
"spinlock_test_common.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::base_internal
|
||||
absl::config
|
||||
absl::core_headers
|
||||
absl::synchronization
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
endian
|
||||
HDRS
|
||||
"internal/endian.h"
|
||||
"internal/unaligned_access.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::config
|
||||
absl::core_headers
|
||||
PUBLIC
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
endian_test
|
||||
SRCS
|
||||
"internal/endian_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::config
|
||||
absl::endian
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
config_test
|
||||
SRCS
|
||||
"config_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::synchronization
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
call_once_test
|
||||
SRCS
|
||||
"call_once_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::core_headers
|
||||
absl::synchronization
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
raw_logging_test
|
||||
SRCS
|
||||
"raw_logging_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::raw_logging_internal
|
||||
absl::strings
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
sysinfo_test
|
||||
SRCS
|
||||
"internal/sysinfo_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::synchronization
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
low_level_alloc_test
|
||||
SRCS
|
||||
"internal/low_level_alloc_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::malloc_internal
|
||||
absl::node_hash_map
|
||||
Threads::Threads
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
thread_identity_test
|
||||
SRCS
|
||||
"internal/thread_identity_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
absl::core_headers
|
||||
absl::synchronization
|
||||
Threads::Threads
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
exponential_biased
|
||||
SRCS
|
||||
"internal/exponential_biased.cc"
|
||||
HDRS
|
||||
"internal/exponential_biased.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::core_headers
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
exponential_biased_test
|
||||
SRCS
|
||||
"internal/exponential_biased_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::exponential_biased
|
||||
absl::strings
|
||||
GTest::gmock_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
periodic_sampler
|
||||
SRCS
|
||||
"internal/periodic_sampler.cc"
|
||||
HDRS
|
||||
"internal/periodic_sampler.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::core_headers
|
||||
absl::exponential_biased
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
periodic_sampler_test
|
||||
SRCS
|
||||
"internal/periodic_sampler_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::core_headers
|
||||
absl::periodic_sampler
|
||||
GTest::gmock_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
scoped_set_env
|
||||
SRCS
|
||||
"internal/scoped_set_env.cc"
|
||||
HDRS
|
||||
"internal/scoped_set_env.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::raw_logging_internal
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
scoped_set_env_test
|
||||
SRCS
|
||||
"internal/scoped_set_env_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::scoped_set_env
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
cmake_thread_test
|
||||
SRCS
|
||||
"internal/cmake_thread_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::base
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
log_severity_test
|
||||
SRCS
|
||||
"log_severity_test.cc"
|
||||
DEPS
|
||||
absl::flags_internal
|
||||
absl::flags_marshalling
|
||||
absl::log_severity
|
||||
absl::strings
|
||||
GTest::gmock
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
strerror
|
||||
SRCS
|
||||
"internal/strerror.cc"
|
||||
HDRS
|
||||
"internal/strerror.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
LINKOPTS
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
absl::core_headers
|
||||
absl::errno_saver
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
strerror_test
|
||||
SRCS
|
||||
"internal/strerror_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::strerror
|
||||
absl::strings
|
||||
GTest::gmock
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_library(
|
||||
NAME
|
||||
fast_type_id
|
||||
HDRS
|
||||
"internal/fast_type_id.h"
|
||||
COPTS
|
||||
${ABSL_DEFAULT_COPTS}
|
||||
LINKOPTS
|
||||
${ABSL_DEFAULT_LINKOPTS}
|
||||
DEPS
|
||||
absl::config
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
fast_type_id_test
|
||||
SRCS
|
||||
"internal/fast_type_id_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::fast_type_id
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
absl_cc_test(
|
||||
NAME
|
||||
optimization_test
|
||||
SRCS
|
||||
"optimization_test.cc"
|
||||
COPTS
|
||||
${ABSL_TEST_COPTS}
|
||||
DEPS
|
||||
absl::core_headers
|
||||
absl::optional
|
||||
GTest::gtest_main
|
||||
)
|
|
@ -0,0 +1,721 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// This header file defines macros for declaring attributes for functions,
|
||||
// types, and variables.
|
||||
//
|
||||
// These macros are used within Abseil and allow the compiler to optimize, where
|
||||
// applicable, certain function calls.
|
||||
//
|
||||
// Most macros here are exposing GCC or Clang features, and are stubbed out for
|
||||
// other compilers.
|
||||
//
|
||||
// GCC attributes documentation:
|
||||
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html
|
||||
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Variable-Attributes.html
|
||||
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Type-Attributes.html
|
||||
//
|
||||
// Most attributes in this file are already supported by GCC 4.7. However, some
|
||||
// of them are not supported in older version of Clang. Thus, we check
|
||||
// `__has_attribute()` first. If the check fails, we check if we are on GCC and
|
||||
// assume the attribute exists on GCC (which is verified on GCC 4.7).
|
||||
|
||||
#ifndef ABSL_BASE_ATTRIBUTES_H_
|
||||
#define ABSL_BASE_ATTRIBUTES_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
// ABSL_HAVE_ATTRIBUTE
|
||||
//
|
||||
// A function-like feature checking macro that is a wrapper around
|
||||
// `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
|
||||
// nonzero constant integer if the attribute is supported or 0 if not.
|
||||
//
|
||||
// It evaluates to zero if `__has_attribute` is not defined by the compiler.
|
||||
//
|
||||
// GCC: https://gcc.gnu.org/gcc-5/changes.html
|
||||
// Clang: https://clang.llvm.org/docs/LanguageExtensions.html
|
||||
#ifdef __has_attribute
|
||||
#define ABSL_HAVE_ATTRIBUTE(x) __has_attribute(x)
|
||||
#else
|
||||
#define ABSL_HAVE_ATTRIBUTE(x) 0
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_CPP_ATTRIBUTE
|
||||
//
|
||||
// A function-like feature checking macro that accepts C++11 style attributes.
|
||||
// It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
|
||||
// (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
|
||||
// find `__has_cpp_attribute`, will evaluate to 0.
|
||||
#if defined(__cplusplus) && defined(__has_cpp_attribute)
|
||||
// NOTE: requiring __cplusplus above should not be necessary, but
|
||||
// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
|
||||
#define ABSL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
|
||||
#else
|
||||
#define ABSL_HAVE_CPP_ATTRIBUTE(x) 0
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function Attributes
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
|
||||
// Clang: https://clang.llvm.org/docs/AttributeReference.html
|
||||
|
||||
// ABSL_PRINTF_ATTRIBUTE
|
||||
// ABSL_SCANF_ATTRIBUTE
|
||||
//
|
||||
// Tells the compiler to perform `printf` format string checking if the
|
||||
// compiler supports it; see the 'format' attribute in
|
||||
// <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
|
||||
//
|
||||
// Note: As the GCC manual states, "[s]ince non-static C++ methods
|
||||
// have an implicit 'this' argument, the arguments of such methods
|
||||
// should be counted from two, not one."
|
||||
#if ABSL_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check) \
|
||||
__attribute__((__format__(__printf__, string_index, first_to_check)))
|
||||
#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check) \
|
||||
__attribute__((__format__(__scanf__, string_index, first_to_check)))
|
||||
#else
|
||||
#define ABSL_PRINTF_ATTRIBUTE(string_index, first_to_check)
|
||||
#define ABSL_SCANF_ATTRIBUTE(string_index, first_to_check)
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_ALWAYS_INLINE
|
||||
// ABSL_ATTRIBUTE_NOINLINE
|
||||
//
|
||||
// Forces functions to either inline or not inline. Introduced in gcc 3.1.
|
||||
#if ABSL_HAVE_ATTRIBUTE(always_inline) || \
|
||||
(defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
|
||||
#define ABSL_HAVE_ATTRIBUTE_ALWAYS_INLINE 1
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_ALWAYS_INLINE
|
||||
#endif
|
||||
|
||||
#if ABSL_HAVE_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_NOINLINE __attribute__((noinline))
|
||||
#define ABSL_HAVE_ATTRIBUTE_NOINLINE 1
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NOINLINE
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NO_TAIL_CALL
|
||||
//
|
||||
// Prevents the compiler from optimizing away stack frames for functions which
|
||||
// end in a call to another function.
|
||||
#if ABSL_HAVE_ATTRIBUTE(disable_tail_calls)
|
||||
#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
|
||||
#define ABSL_ATTRIBUTE_NO_TAIL_CALL __attribute__((disable_tail_calls))
|
||||
#elif defined(__GNUC__) && !defined(__clang__) && !defined(__e2k__)
|
||||
#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 1
|
||||
#define ABSL_ATTRIBUTE_NO_TAIL_CALL \
|
||||
__attribute__((optimize("no-optimize-sibling-calls")))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NO_TAIL_CALL
|
||||
#define ABSL_HAVE_ATTRIBUTE_NO_TAIL_CALL 0
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_WEAK
|
||||
//
|
||||
// Tags a function as weak for the purposes of compilation and linking.
|
||||
// Weak attributes did not work properly in LLVM's Windows backend before
|
||||
// 9.0.0, so disable them there. See https://bugs.llvm.org/show_bug.cgi?id=37598
|
||||
// for further information.
|
||||
// The MinGW compiler doesn't complain about the weak attribute until the link
|
||||
// step, presumably because Windows doesn't use ELF binaries.
|
||||
#if (ABSL_HAVE_ATTRIBUTE(weak) || \
|
||||
(defined(__GNUC__) && !defined(__clang__))) && \
|
||||
(!defined(_WIN32) || __clang_major__ < 9) && !defined(__MINGW32__)
|
||||
#undef ABSL_ATTRIBUTE_WEAK
|
||||
#define ABSL_ATTRIBUTE_WEAK __attribute__((weak))
|
||||
#define ABSL_HAVE_ATTRIBUTE_WEAK 1
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_WEAK
|
||||
#define ABSL_HAVE_ATTRIBUTE_WEAK 0
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NONNULL
|
||||
//
|
||||
// Tells the compiler either (a) that a particular function parameter
|
||||
// should be a non-null pointer, or (b) that all pointer arguments should
|
||||
// be non-null.
|
||||
//
|
||||
// Note: As the GCC manual states, "[s]ince non-static C++ methods
|
||||
// have an implicit 'this' argument, the arguments of such methods
|
||||
// should be counted from two, not one."
|
||||
//
|
||||
// Args are indexed starting at 1.
|
||||
//
|
||||
// For non-static class member functions, the implicit `this` argument
|
||||
// is arg 1, and the first explicit argument is arg 2. For static class member
|
||||
// functions, there is no implicit `this`, and the first explicit argument is
|
||||
// arg 1.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// /* arg_a cannot be null, but arg_b can */
|
||||
// void Function(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(1);
|
||||
//
|
||||
// class C {
|
||||
// /* arg_a cannot be null, but arg_b can */
|
||||
// void Method(void* arg_a, void* arg_b) ABSL_ATTRIBUTE_NONNULL(2);
|
||||
//
|
||||
// /* arg_a cannot be null, but arg_b can */
|
||||
// static void StaticMethod(void* arg_a, void* arg_b)
|
||||
// ABSL_ATTRIBUTE_NONNULL(1);
|
||||
// };
|
||||
//
|
||||
// If no arguments are provided, then all pointer arguments should be non-null.
|
||||
//
|
||||
// /* No pointer arguments may be null. */
|
||||
// void Function(void* arg_a, void* arg_b, int arg_c) ABSL_ATTRIBUTE_NONNULL();
|
||||
//
|
||||
// NOTE: The GCC nonnull attribute actually accepts a list of arguments, but
|
||||
// ABSL_ATTRIBUTE_NONNULL does not.
|
||||
#if ABSL_HAVE_ATTRIBUTE(nonnull) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_NONNULL(arg_index) __attribute__((nonnull(arg_index)))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NONNULL(...)
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NORETURN
|
||||
//
|
||||
// Tells the compiler that a given function never returns.
|
||||
#if ABSL_HAVE_ATTRIBUTE(noreturn) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_NORETURN __attribute__((noreturn))
|
||||
#elif defined(_MSC_VER)
|
||||
#define ABSL_ATTRIBUTE_NORETURN __declspec(noreturn)
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NORETURN
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
|
||||
//
|
||||
// Tells the AddressSanitizer (or other memory testing tools) to ignore a given
|
||||
// function. Useful for cases when a function reads random locations on stack,
|
||||
// calls _exit from a cloned subprocess, deliberately accesses buffer
|
||||
// out of bounds or does other scary things with memory.
|
||||
// NOTE: GCC supports AddressSanitizer(asan) since 4.8.
|
||||
// https://gcc.gnu.org/gcc-4.8/changes.html
|
||||
#if ABSL_HAVE_ATTRIBUTE(no_sanitize_address)
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
|
||||
//
|
||||
// Tells the MemorySanitizer to relax the handling of a given function. All "Use
|
||||
// of uninitialized value" warnings from such functions will be suppressed, and
|
||||
// all values loaded from memory will be considered fully initialized. This
|
||||
// attribute is similar to the ABSL_ATTRIBUTE_NO_SANITIZE_ADDRESS attribute
|
||||
// above, but deals with initialized-ness rather than addressability issues.
|
||||
// NOTE: MemorySanitizer(msan) is supported by Clang but not GCC.
|
||||
#if ABSL_HAVE_ATTRIBUTE(no_sanitize_memory)
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
|
||||
//
|
||||
// Tells the ThreadSanitizer to not instrument a given function.
|
||||
// NOTE: GCC supports ThreadSanitizer(tsan) since 4.8.
|
||||
// https://gcc.gnu.org/gcc-4.8/changes.html
|
||||
#if ABSL_HAVE_ATTRIBUTE(no_sanitize_thread)
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_THREAD
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
|
||||
//
|
||||
// Tells the UndefinedSanitizer to ignore a given function. Useful for cases
|
||||
// where certain behavior (eg. division by zero) is being used intentionally.
|
||||
// NOTE: GCC supports UndefinedBehaviorSanitizer(ubsan) since 4.9.
|
||||
// https://gcc.gnu.org/gcc-4.9/changes.html
|
||||
#if ABSL_HAVE_ATTRIBUTE(no_sanitize_undefined)
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
|
||||
__attribute__((no_sanitize_undefined))
|
||||
#elif ABSL_HAVE_ATTRIBUTE(no_sanitize)
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED \
|
||||
__attribute__((no_sanitize("undefined")))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_UNDEFINED
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NO_SANITIZE_CFI
|
||||
//
|
||||
// Tells the ControlFlowIntegrity sanitizer to not instrument a given function.
|
||||
// See https://clang.llvm.org/docs/ControlFlowIntegrity.html for details.
|
||||
#if ABSL_HAVE_ATTRIBUTE(no_sanitize)
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI __attribute__((no_sanitize("cfi")))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_CFI
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
|
||||
//
|
||||
// Tells the SafeStack to not instrument a given function.
|
||||
// See https://clang.llvm.org/docs/SafeStack.html for details.
|
||||
#if ABSL_HAVE_ATTRIBUTE(no_sanitize)
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK \
|
||||
__attribute__((no_sanitize("safe-stack")))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_NO_SANITIZE_SAFESTACK
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_RETURNS_NONNULL
|
||||
//
|
||||
// Tells the compiler that a particular function never returns a null pointer.
|
||||
#if ABSL_HAVE_ATTRIBUTE(returns_nonnull)
|
||||
#define ABSL_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_RETURNS_NONNULL
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_ATTRIBUTE_SECTION
|
||||
//
|
||||
// Indicates whether labeled sections are supported. Weak symbol support is
|
||||
// a prerequisite. Labeled sections are not supported on Darwin/iOS.
|
||||
#ifdef ABSL_HAVE_ATTRIBUTE_SECTION
|
||||
#error ABSL_HAVE_ATTRIBUTE_SECTION cannot be directly set
|
||||
#elif (ABSL_HAVE_ATTRIBUTE(section) || \
|
||||
(defined(__GNUC__) && !defined(__clang__))) && \
|
||||
!defined(__APPLE__) && ABSL_HAVE_ATTRIBUTE_WEAK
|
||||
#define ABSL_HAVE_ATTRIBUTE_SECTION 1
|
||||
|
||||
// ABSL_ATTRIBUTE_SECTION
|
||||
//
|
||||
// Tells the compiler/linker to put a given function into a section and define
|
||||
// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
|
||||
// This functionality is supported by GNU linker. Any function annotated with
|
||||
// `ABSL_ATTRIBUTE_SECTION` must not be inlined, or it will be placed into
|
||||
// whatever section its caller is placed into.
|
||||
//
|
||||
#ifndef ABSL_ATTRIBUTE_SECTION
|
||||
#define ABSL_ATTRIBUTE_SECTION(name) \
|
||||
__attribute__((section(#name))) __attribute__((noinline))
|
||||
#endif
|
||||
|
||||
|
||||
// ABSL_ATTRIBUTE_SECTION_VARIABLE
|
||||
//
|
||||
// Tells the compiler/linker to put a given variable into a section and define
|
||||
// `__start_ ## name` and `__stop_ ## name` symbols to bracket the section.
|
||||
// This functionality is supported by GNU linker.
|
||||
#ifndef ABSL_ATTRIBUTE_SECTION_VARIABLE
|
||||
#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name) __attribute__((section(#name)))
|
||||
#endif
|
||||
|
||||
// ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
|
||||
//
|
||||
// A weak section declaration to be used as a global declaration
|
||||
// for ABSL_ATTRIBUTE_SECTION_START|STOP(name) to compile and link
|
||||
// even without functions with ABSL_ATTRIBUTE_SECTION(name).
|
||||
// ABSL_DEFINE_ATTRIBUTE_SECTION should be in the exactly one file; it's
|
||||
// a no-op on ELF but not on Mach-O.
|
||||
//
|
||||
#ifndef ABSL_DECLARE_ATTRIBUTE_SECTION_VARS
|
||||
#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) \
|
||||
extern char __start_##name[] ABSL_ATTRIBUTE_WEAK; \
|
||||
extern char __stop_##name[] ABSL_ATTRIBUTE_WEAK
|
||||
#endif
|
||||
#ifndef ABSL_DEFINE_ATTRIBUTE_SECTION_VARS
|
||||
#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
|
||||
#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_SECTION_START
|
||||
//
|
||||
// Returns `void*` pointers to start/end of a section of code with
|
||||
// functions having ABSL_ATTRIBUTE_SECTION(name).
|
||||
// Returns 0 if no such functions exist.
|
||||
// One must ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name) for this to compile and
|
||||
// link.
|
||||
//
|
||||
#define ABSL_ATTRIBUTE_SECTION_START(name) \
|
||||
(reinterpret_cast<void *>(__start_##name))
|
||||
#define ABSL_ATTRIBUTE_SECTION_STOP(name) \
|
||||
(reinterpret_cast<void *>(__stop_##name))
|
||||
|
||||
#else // !ABSL_HAVE_ATTRIBUTE_SECTION
|
||||
|
||||
#define ABSL_HAVE_ATTRIBUTE_SECTION 0
|
||||
|
||||
// provide dummy definitions
|
||||
#define ABSL_ATTRIBUTE_SECTION(name)
|
||||
#define ABSL_ATTRIBUTE_SECTION_VARIABLE(name)
|
||||
#define ABSL_INIT_ATTRIBUTE_SECTION_VARS(name)
|
||||
#define ABSL_DEFINE_ATTRIBUTE_SECTION_VARS(name)
|
||||
#define ABSL_DECLARE_ATTRIBUTE_SECTION_VARS(name)
|
||||
#define ABSL_ATTRIBUTE_SECTION_START(name) (reinterpret_cast<void *>(0))
|
||||
#define ABSL_ATTRIBUTE_SECTION_STOP(name) (reinterpret_cast<void *>(0))
|
||||
|
||||
#endif // ABSL_ATTRIBUTE_SECTION
|
||||
|
||||
// ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
||||
//
|
||||
// Support for aligning the stack on 32-bit x86.
|
||||
#if ABSL_HAVE_ATTRIBUTE(force_align_arg_pointer) || \
|
||||
(defined(__GNUC__) && !defined(__clang__))
|
||||
#if defined(__i386__)
|
||||
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC \
|
||||
__attribute__((force_align_arg_pointer))
|
||||
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
|
||||
#elif defined(__x86_64__)
|
||||
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (1)
|
||||
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
||||
#else // !__i386__ && !__x86_64
|
||||
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
|
||||
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
||||
#endif // __i386__
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_STACK_ALIGN_FOR_OLD_LIBC
|
||||
#define ABSL_REQUIRE_STACK_ALIGN_TRAMPOLINE (0)
|
||||
#endif
|
||||
|
||||
// ABSL_MUST_USE_RESULT
|
||||
//
|
||||
// Tells the compiler to warn about unused results.
|
||||
//
|
||||
// When annotating a function, it must appear as the first part of the
|
||||
// declaration or definition. The compiler will warn if the return value from
|
||||
// such a function is unused:
|
||||
//
|
||||
// ABSL_MUST_USE_RESULT Sprocket* AllocateSprocket();
|
||||
// AllocateSprocket(); // Triggers a warning.
|
||||
//
|
||||
// When annotating a class, it is equivalent to annotating every function which
|
||||
// returns an instance.
|
||||
//
|
||||
// class ABSL_MUST_USE_RESULT Sprocket {};
|
||||
// Sprocket(); // Triggers a warning.
|
||||
//
|
||||
// Sprocket MakeSprocket();
|
||||
// MakeSprocket(); // Triggers a warning.
|
||||
//
|
||||
// Note that references and pointers are not instances:
|
||||
//
|
||||
// Sprocket* SprocketPointer();
|
||||
// SprocketPointer(); // Does *not* trigger a warning.
|
||||
//
|
||||
// ABSL_MUST_USE_RESULT allows using cast-to-void to suppress the unused result
|
||||
// warning. For that, warn_unused_result is used only for clang but not for gcc.
|
||||
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
|
||||
//
|
||||
// Note: past advice was to place the macro after the argument list.
|
||||
#if ABSL_HAVE_ATTRIBUTE(nodiscard)
|
||||
#define ABSL_MUST_USE_RESULT [[nodiscard]]
|
||||
#elif defined(__clang__) && ABSL_HAVE_ATTRIBUTE(warn_unused_result)
|
||||
#define ABSL_MUST_USE_RESULT __attribute__((warn_unused_result))
|
||||
#else
|
||||
#define ABSL_MUST_USE_RESULT
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_HOT, ABSL_ATTRIBUTE_COLD
|
||||
//
|
||||
// Tells GCC that a function is hot or cold. GCC can use this information to
|
||||
// improve static analysis, i.e. a conditional branch to a cold function
|
||||
// is likely to be not-taken.
|
||||
// This annotation is used for function declarations.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// int foo() ABSL_ATTRIBUTE_HOT;
|
||||
#if ABSL_HAVE_ATTRIBUTE(hot) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_HOT __attribute__((hot))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_HOT
|
||||
#endif
|
||||
|
||||
#if ABSL_HAVE_ATTRIBUTE(cold) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_COLD __attribute__((cold))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_COLD
|
||||
#endif
|
||||
|
||||
// ABSL_XRAY_ALWAYS_INSTRUMENT, ABSL_XRAY_NEVER_INSTRUMENT, ABSL_XRAY_LOG_ARGS
|
||||
//
|
||||
// We define the ABSL_XRAY_ALWAYS_INSTRUMENT and ABSL_XRAY_NEVER_INSTRUMENT
|
||||
// macro used as an attribute to mark functions that must always or never be
|
||||
// instrumented by XRay. Currently, this is only supported in Clang/LLVM.
|
||||
//
|
||||
// For reference on the LLVM XRay instrumentation, see
|
||||
// http://llvm.org/docs/XRay.html.
|
||||
//
|
||||
// A function with the XRAY_ALWAYS_INSTRUMENT macro attribute in its declaration
|
||||
// will always get the XRay instrumentation sleds. These sleds may introduce
|
||||
// some binary size and runtime overhead and must be used sparingly.
|
||||
//
|
||||
// These attributes only take effect when the following conditions are met:
|
||||
//
|
||||
// * The file/target is built in at least C++11 mode, with a Clang compiler
|
||||
// that supports XRay attributes.
|
||||
// * The file/target is built with the -fxray-instrument flag set for the
|
||||
// Clang/LLVM compiler.
|
||||
// * The function is defined in the translation unit (the compiler honors the
|
||||
// attribute in either the definition or the declaration, and must match).
|
||||
//
|
||||
// There are cases when, even when building with XRay instrumentation, users
|
||||
// might want to control specifically which functions are instrumented for a
|
||||
// particular build using special-case lists provided to the compiler. These
|
||||
// special case lists are provided to Clang via the
|
||||
// -fxray-always-instrument=... and -fxray-never-instrument=... flags. The
|
||||
// attributes in source take precedence over these special-case lists.
|
||||
//
|
||||
// To disable the XRay attributes at build-time, users may define
|
||||
// ABSL_NO_XRAY_ATTRIBUTES. Do NOT define ABSL_NO_XRAY_ATTRIBUTES on specific
|
||||
// packages/targets, as this may lead to conflicting definitions of functions at
|
||||
// link-time.
|
||||
//
|
||||
// XRay isn't currently supported on Android:
|
||||
// https://github.com/android/ndk/issues/368
|
||||
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_always_instrument) && \
|
||||
!defined(ABSL_NO_XRAY_ATTRIBUTES) && !defined(__ANDROID__)
|
||||
#define ABSL_XRAY_ALWAYS_INSTRUMENT [[clang::xray_always_instrument]]
|
||||
#define ABSL_XRAY_NEVER_INSTRUMENT [[clang::xray_never_instrument]]
|
||||
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::xray_log_args)
|
||||
#define ABSL_XRAY_LOG_ARGS(N) \
|
||||
[[clang::xray_always_instrument, clang::xray_log_args(N)]]
|
||||
#else
|
||||
#define ABSL_XRAY_LOG_ARGS(N) [[clang::xray_always_instrument]]
|
||||
#endif
|
||||
#else
|
||||
#define ABSL_XRAY_ALWAYS_INSTRUMENT
|
||||
#define ABSL_XRAY_NEVER_INSTRUMENT
|
||||
#define ABSL_XRAY_LOG_ARGS(N)
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_REINITIALIZES
|
||||
//
|
||||
// Indicates that a member function reinitializes the entire object to a known
|
||||
// state, independent of the previous state of the object.
|
||||
//
|
||||
// The clang-tidy check bugprone-use-after-move allows member functions marked
|
||||
// with this attribute to be called on objects that have been moved from;
|
||||
// without the attribute, this would result in a use-after-move warning.
|
||||
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
|
||||
#define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_REINITIALIZES
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Variable Attributes
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// ABSL_ATTRIBUTE_UNUSED
|
||||
//
|
||||
// Prevents the compiler from complaining about variables that appear unused.
|
||||
//
|
||||
// For code or headers that are assured to only build with C++17 and up, prefer
|
||||
// just using the standard '[[maybe_unused]]' directly over this macro.
|
||||
//
|
||||
// Due to differences in positioning requirements between the old, compiler
|
||||
// specific __attribute__ syntax and the now standard [[maybe_unused]], this
|
||||
// macro does not attempt to take advantage of '[[maybe_unused]]'.
|
||||
#if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#undef ABSL_ATTRIBUTE_UNUSED
|
||||
#define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_UNUSED
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_INITIAL_EXEC
|
||||
//
|
||||
// Tells the compiler to use "initial-exec" mode for a thread-local variable.
|
||||
// See http://people.redhat.com/drepper/tls.pdf for the gory details.
|
||||
#if ABSL_HAVE_ATTRIBUTE(tls_model) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_INITIAL_EXEC __attribute__((tls_model("initial-exec")))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_INITIAL_EXEC
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_PACKED
|
||||
//
|
||||
// Instructs the compiler not to use natural alignment for a tagged data
|
||||
// structure, but instead to reduce its alignment to 1. This attribute can
|
||||
// either be applied to members of a structure or to a structure in its
|
||||
// entirety. Applying this attribute (judiciously) to a structure in its
|
||||
// entirety to optimize the memory footprint of very commonly-used structs is
|
||||
// fine. Do not apply this attribute to a structure in its entirety if the
|
||||
// purpose is to control the offsets of the members in the structure. Instead,
|
||||
// apply this attribute only to structure members that need it.
|
||||
//
|
||||
// When applying ABSL_ATTRIBUTE_PACKED only to specific structure members the
|
||||
// natural alignment of structure members not annotated is preserved. Aligned
|
||||
// member accesses are faster than non-aligned member accesses even if the
|
||||
// targeted microprocessor supports non-aligned accesses.
|
||||
#if ABSL_HAVE_ATTRIBUTE(packed) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_PACKED __attribute__((__packed__))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_PACKED
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_FUNC_ALIGN
|
||||
//
|
||||
// Tells the compiler to align the function start at least to certain
|
||||
// alignment boundary
|
||||
#if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__))
|
||||
#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes)))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes)
|
||||
#endif
|
||||
|
||||
// ABSL_FALLTHROUGH_INTENDED
|
||||
//
|
||||
// Annotates implicit fall-through between switch labels, allowing a case to
|
||||
// indicate intentional fallthrough and turn off warnings about any lack of a
|
||||
// `break` statement. The ABSL_FALLTHROUGH_INTENDED macro should be followed by
|
||||
// a semicolon and can be used in most places where `break` can, provided that
|
||||
// no statements exist between it and the next switch label.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// switch (x) {
|
||||
// case 40:
|
||||
// case 41:
|
||||
// if (truth_is_out_there) {
|
||||
// ++x;
|
||||
// ABSL_FALLTHROUGH_INTENDED; // Use instead of/along with annotations
|
||||
// // in comments
|
||||
// } else {
|
||||
// return x;
|
||||
// }
|
||||
// case 42:
|
||||
// ...
|
||||
//
|
||||
// Notes: When supported, GCC and Clang can issue a warning on switch labels
|
||||
// with unannotated fallthrough using the warning `-Wimplicit-fallthrough`. See
|
||||
// clang documentation on language extensions for details:
|
||||
// https://clang.llvm.org/docs/AttributeReference.html#fallthrough-clang-fallthrough
|
||||
//
|
||||
// When used with unsupported compilers, the ABSL_FALLTHROUGH_INTENDED macro has
|
||||
// no effect on diagnostics. In any case this macro has no effect on runtime
|
||||
// behavior and performance of code.
|
||||
|
||||
#ifdef ABSL_FALLTHROUGH_INTENDED
|
||||
#error "ABSL_FALLTHROUGH_INTENDED should not be defined."
|
||||
#elif ABSL_HAVE_CPP_ATTRIBUTE(fallthrough)
|
||||
#define ABSL_FALLTHROUGH_INTENDED [[fallthrough]]
|
||||
#elif ABSL_HAVE_CPP_ATTRIBUTE(clang::fallthrough)
|
||||
#define ABSL_FALLTHROUGH_INTENDED [[clang::fallthrough]]
|
||||
#elif ABSL_HAVE_CPP_ATTRIBUTE(gnu::fallthrough)
|
||||
#define ABSL_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
|
||||
#else
|
||||
#define ABSL_FALLTHROUGH_INTENDED \
|
||||
do { \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
// ABSL_DEPRECATED()
|
||||
//
|
||||
// Marks a deprecated class, struct, enum, function, method and variable
|
||||
// declarations. The macro argument is used as a custom diagnostic message (e.g.
|
||||
// suggestion of a better alternative).
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// class ABSL_DEPRECATED("Use Bar instead") Foo {...};
|
||||
//
|
||||
// ABSL_DEPRECATED("Use Baz() instead") void Bar() {...}
|
||||
//
|
||||
// template <typename T>
|
||||
// ABSL_DEPRECATED("Use DoThat() instead")
|
||||
// void DoThis();
|
||||
//
|
||||
// Every usage of a deprecated entity will trigger a warning when compiled with
|
||||
// clang's `-Wdeprecated-declarations` option. This option is turned off by
|
||||
// default, but the warnings will be reported by clang-tidy.
|
||||
#if defined(__clang__) && defined(__cplusplus) && __cplusplus >= 201103L
|
||||
#define ABSL_DEPRECATED(message) __attribute__((deprecated(message)))
|
||||
#endif
|
||||
|
||||
#ifndef ABSL_DEPRECATED
|
||||
#define ABSL_DEPRECATED(message)
|
||||
#endif
|
||||
|
||||
// ABSL_CONST_INIT
|
||||
//
|
||||
// A variable declaration annotated with the `ABSL_CONST_INIT` attribute will
|
||||
// not compile (on supported platforms) unless the variable has a constant
|
||||
// initializer. This is useful for variables with static and thread storage
|
||||
// duration, because it guarantees that they will not suffer from the so-called
|
||||
// "static init order fiasco". Prefer to put this attribute on the most visible
|
||||
// declaration of the variable, if there's more than one, because code that
|
||||
// accesses the variable can then use the attribute for optimization.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// class MyClass {
|
||||
// public:
|
||||
// ABSL_CONST_INIT static MyType my_var;
|
||||
// };
|
||||
//
|
||||
// MyType MyClass::my_var = MakeMyType(...);
|
||||
//
|
||||
// Note that this attribute is redundant if the variable is declared constexpr.
|
||||
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
|
||||
#define ABSL_CONST_INIT [[clang::require_constant_initialization]]
|
||||
#else
|
||||
#define ABSL_CONST_INIT
|
||||
#endif // ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization)
|
||||
|
||||
// ABSL_ATTRIBUTE_PURE_FUNCTION
|
||||
//
|
||||
// ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure"
|
||||
// functions. A function is pure if its return value is only a function of its
|
||||
// arguments. The pure attribute prohibits a function from modifying the state
|
||||
// of the program that is observable by means other than inspecting the
|
||||
// function's return value. Declaring such functions with the pure attribute
|
||||
// allows the compiler to avoid emitting some calls in repeated invocations of
|
||||
// the function with the same argument values.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Milliseconds(Duration d);
|
||||
#if ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure)
|
||||
#define ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]]
|
||||
#elif ABSL_HAVE_ATTRIBUTE(pure)
|
||||
#define ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_PURE_FUNCTION
|
||||
#endif
|
||||
|
||||
// ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
|
||||
// parameter or implicit object parameter is retained by the return value of the
|
||||
// annotated function (or, for a parameter of a constructor, in the value of the
|
||||
// constructed object). This attribute causes warnings to be produced if a
|
||||
// temporary object does not live long enough.
|
||||
//
|
||||
// When applied to a reference parameter, the referenced object is assumed to be
|
||||
// retained by the return value of the function. When applied to a non-reference
|
||||
// parameter (for example, a pointer or a class type), all temporaries
|
||||
// referenced by the parameter are assumed to be retained by the return value of
|
||||
// the function.
|
||||
//
|
||||
// See also the upstream documentation:
|
||||
// https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
|
||||
#if ABSL_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
|
||||
#define ABSL_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
|
||||
#elif ABSL_HAVE_ATTRIBUTE(lifetimebound)
|
||||
#define ABSL_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
|
||||
#else
|
||||
#define ABSL_ATTRIBUTE_LIFETIME_BOUND
|
||||
#endif
|
||||
|
||||
#endif // ABSL_BASE_ATTRIBUTES_H_
|
|
@ -0,0 +1,109 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// Unit test for bit_cast template.
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/casts.h"
|
||||
#include "absl/base/macros.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace {
|
||||
|
||||
template <int N>
|
||||
struct marshall { char buf[N]; };
|
||||
|
||||
template <typename T>
|
||||
void TestMarshall(const T values[], int num_values) {
|
||||
for (int i = 0; i < num_values; ++i) {
|
||||
T t0 = values[i];
|
||||
marshall<sizeof(T)> m0 = absl::bit_cast<marshall<sizeof(T)> >(t0);
|
||||
T t1 = absl::bit_cast<T>(m0);
|
||||
marshall<sizeof(T)> m1 = absl::bit_cast<marshall<sizeof(T)> >(t1);
|
||||
ASSERT_EQ(0, memcmp(&t0, &t1, sizeof(T)));
|
||||
ASSERT_EQ(0, memcmp(&m0, &m1, sizeof(T)));
|
||||
}
|
||||
}
|
||||
|
||||
// Convert back and forth to an integral type. The C++ standard does
|
||||
// not guarantee this will work, but we test that this works on all the
|
||||
// platforms we support.
|
||||
//
|
||||
// Likewise, we below make assumptions about sizeof(float) and
|
||||
// sizeof(double) which the standard does not guarantee, but which hold on the
|
||||
// platforms we support.
|
||||
|
||||
template <typename T, typename I>
|
||||
void TestIntegral(const T values[], int num_values) {
|
||||
for (int i = 0; i < num_values; ++i) {
|
||||
T t0 = values[i];
|
||||
I i0 = absl::bit_cast<I>(t0);
|
||||
T t1 = absl::bit_cast<T>(i0);
|
||||
I i1 = absl::bit_cast<I>(t1);
|
||||
ASSERT_EQ(0, memcmp(&t0, &t1, sizeof(T)));
|
||||
ASSERT_EQ(i0, i1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(BitCast, Bool) {
|
||||
static const bool bool_list[] = { false, true };
|
||||
TestMarshall<bool>(bool_list, ABSL_ARRAYSIZE(bool_list));
|
||||
}
|
||||
|
||||
TEST(BitCast, Int32) {
|
||||
static const int32_t int_list[] =
|
||||
{ 0, 1, 100, 2147483647, -1, -100, -2147483647, -2147483647-1 };
|
||||
TestMarshall<int32_t>(int_list, ABSL_ARRAYSIZE(int_list));
|
||||
}
|
||||
|
||||
TEST(BitCast, Int64) {
|
||||
static const int64_t int64_list[] =
|
||||
{ 0, 1, 1LL << 40, -1, -(1LL<<40) };
|
||||
TestMarshall<int64_t>(int64_list, ABSL_ARRAYSIZE(int64_list));
|
||||
}
|
||||
|
||||
TEST(BitCast, Uint64) {
|
||||
static const uint64_t uint64_list[] =
|
||||
{ 0, 1, 1LLU << 40, 1LLU << 63 };
|
||||
TestMarshall<uint64_t>(uint64_list, ABSL_ARRAYSIZE(uint64_list));
|
||||
}
|
||||
|
||||
TEST(BitCast, Float) {
|
||||
static const float float_list[] =
|
||||
{ 0.0f, 1.0f, -1.0f, 10.0f, -10.0f,
|
||||
1e10f, 1e20f, 1e-10f, 1e-20f,
|
||||
2.71828f, 3.14159f };
|
||||
TestMarshall<float>(float_list, ABSL_ARRAYSIZE(float_list));
|
||||
TestIntegral<float, int>(float_list, ABSL_ARRAYSIZE(float_list));
|
||||
TestIntegral<float, unsigned>(float_list, ABSL_ARRAYSIZE(float_list));
|
||||
}
|
||||
|
||||
TEST(BitCast, Double) {
|
||||
static const double double_list[] =
|
||||
{ 0.0, 1.0, -1.0, 10.0, -10.0,
|
||||
1e10, 1e100, 1e-10, 1e-100,
|
||||
2.718281828459045,
|
||||
3.141592653589793238462643383279502884197169399375105820974944 };
|
||||
TestMarshall<double>(double_list, ABSL_ARRAYSIZE(double_list));
|
||||
TestIntegral<double, int64_t>(double_list, ABSL_ARRAYSIZE(double_list));
|
||||
TestIntegral<double, uint64_t>(double_list, ABSL_ARRAYSIZE(double_list));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,219 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: call_once.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file provides an Abseil version of `std::call_once` for invoking
|
||||
// a given function at most once, across all threads. This Abseil version is
|
||||
// faster than the C++11 version and incorporates the C++17 argument-passing
|
||||
// fix, so that (for example) non-const references may be passed to the invoked
|
||||
// function.
|
||||
|
||||
#ifndef ABSL_BASE_CALL_ONCE_H_
|
||||
#define ABSL_BASE_CALL_ONCE_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/internal/invoke.h"
|
||||
#include "absl/base/internal/low_level_scheduling.h"
|
||||
#include "absl/base/internal/raw_logging.h"
|
||||
#include "absl/base/internal/scheduling_mode.h"
|
||||
#include "absl/base/internal/spinlock_wait.h"
|
||||
#include "absl/base/macros.h"
|
||||
#include "absl/base/optimization.h"
|
||||
#include "absl/base/port.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
class once_flag;
|
||||
|
||||
namespace base_internal {
|
||||
std::atomic<uint32_t>* ControlWord(absl::once_flag* flag);
|
||||
} // namespace base_internal
|
||||
|
||||
// call_once()
|
||||
//
|
||||
// For all invocations using a given `once_flag`, invokes a given `fn` exactly
|
||||
// once across all threads. The first call to `call_once()` with a particular
|
||||
// `once_flag` argument (that does not throw an exception) will run the
|
||||
// specified function with the provided `args`; other calls with the same
|
||||
// `once_flag` argument will not run the function, but will wait
|
||||
// for the provided function to finish running (if it is still running).
|
||||
//
|
||||
// This mechanism provides a safe, simple, and fast mechanism for one-time
|
||||
// initialization in a multi-threaded process.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// class MyInitClass {
|
||||
// public:
|
||||
// ...
|
||||
// mutable absl::once_flag once_;
|
||||
//
|
||||
// MyInitClass* init() const {
|
||||
// absl::call_once(once_, &MyInitClass::Init, this);
|
||||
// return ptr_;
|
||||
// }
|
||||
//
|
||||
template <typename Callable, typename... Args>
|
||||
void call_once(absl::once_flag& flag, Callable&& fn, Args&&... args);
|
||||
|
||||
// once_flag
|
||||
//
|
||||
// Objects of this type are used to distinguish calls to `call_once()` and
|
||||
// ensure the provided function is only invoked once across all threads. This
|
||||
// type is not copyable or movable. However, it has a `constexpr`
|
||||
// constructor, and is safe to use as a namespace-scoped global variable.
|
||||
class once_flag {
|
||||
public:
|
||||
constexpr once_flag() : control_(0) {}
|
||||
once_flag(const once_flag&) = delete;
|
||||
once_flag& operator=(const once_flag&) = delete;
|
||||
|
||||
private:
|
||||
friend std::atomic<uint32_t>* base_internal::ControlWord(once_flag* flag);
|
||||
std::atomic<uint32_t> control_;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// End of public interfaces.
|
||||
// Implementation details follow.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace base_internal {
|
||||
|
||||
// Like call_once, but uses KERNEL_ONLY scheduling. Intended to be used to
|
||||
// initialize entities used by the scheduler implementation.
|
||||
template <typename Callable, typename... Args>
|
||||
void LowLevelCallOnce(absl::once_flag* flag, Callable&& fn, Args&&... args);
|
||||
|
||||
// Disables scheduling while on stack when scheduling mode is non-cooperative.
|
||||
// No effect for cooperative scheduling modes.
|
||||
class SchedulingHelper {
|
||||
public:
|
||||
explicit SchedulingHelper(base_internal::SchedulingMode mode) : mode_(mode) {
|
||||
if (mode_ == base_internal::SCHEDULE_KERNEL_ONLY) {
|
||||
guard_result_ = base_internal::SchedulingGuard::DisableRescheduling();
|
||||
}
|
||||
}
|
||||
|
||||
~SchedulingHelper() {
|
||||
if (mode_ == base_internal::SCHEDULE_KERNEL_ONLY) {
|
||||
base_internal::SchedulingGuard::EnableRescheduling(guard_result_);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
base_internal::SchedulingMode mode_;
|
||||
bool guard_result_;
|
||||
};
|
||||
|
||||
// Bit patterns for call_once state machine values. Internal implementation
|
||||
// detail, not for use by clients.
|
||||
//
|
||||
// The bit patterns are arbitrarily chosen from unlikely values, to aid in
|
||||
// debugging. However, kOnceInit must be 0, so that a zero-initialized
|
||||
// once_flag will be valid for immediate use.
|
||||
enum {
|
||||
kOnceInit = 0,
|
||||
kOnceRunning = 0x65C2937B,
|
||||
kOnceWaiter = 0x05A308D2,
|
||||
// A very small constant is chosen for kOnceDone so that it fit in a single
|
||||
// compare with immediate instruction for most common ISAs. This is verified
|
||||
// for x86, POWER and ARM.
|
||||
kOnceDone = 221, // Random Number
|
||||
};
|
||||
|
||||
template <typename Callable, typename... Args>
|
||||
ABSL_ATTRIBUTE_NOINLINE
|
||||
void CallOnceImpl(std::atomic<uint32_t>* control,
|
||||
base_internal::SchedulingMode scheduling_mode, Callable&& fn,
|
||||
Args&&... args) {
|
||||
#ifndef NDEBUG
|
||||
{
|
||||
uint32_t old_control = control->load(std::memory_order_relaxed);
|
||||
if (old_control != kOnceInit &&
|
||||
old_control != kOnceRunning &&
|
||||
old_control != kOnceWaiter &&
|
||||
old_control != kOnceDone) {
|
||||
ABSL_RAW_LOG(FATAL, "Unexpected value for control word: 0x%lx",
|
||||
static_cast<unsigned long>(old_control)); // NOLINT
|
||||
}
|
||||
}
|
||||
#endif // NDEBUG
|
||||
static const base_internal::SpinLockWaitTransition trans[] = {
|
||||
{kOnceInit, kOnceRunning, true},
|
||||
{kOnceRunning, kOnceWaiter, false},
|
||||
{kOnceDone, kOnceDone, true}};
|
||||
|
||||
// Must do this before potentially modifying control word's state.
|
||||
base_internal::SchedulingHelper maybe_disable_scheduling(scheduling_mode);
|
||||
// Short circuit the simplest case to avoid procedure call overhead.
|
||||
// The base_internal::SpinLockWait() call returns either kOnceInit or
|
||||
// kOnceDone. If it returns kOnceDone, it must have loaded the control word
|
||||
// with std::memory_order_acquire and seen a value of kOnceDone.
|
||||
uint32_t old_control = kOnceInit;
|
||||
if (control->compare_exchange_strong(old_control, kOnceRunning,
|
||||
std::memory_order_relaxed) ||
|
||||
base_internal::SpinLockWait(control, ABSL_ARRAYSIZE(trans), trans,
|
||||
scheduling_mode) == kOnceInit) {
|
||||
base_internal::invoke(std::forward<Callable>(fn),
|
||||
std::forward<Args>(args)...);
|
||||
old_control =
|
||||
control->exchange(base_internal::kOnceDone, std::memory_order_release);
|
||||
if (old_control == base_internal::kOnceWaiter) {
|
||||
base_internal::SpinLockWake(control, true);
|
||||
}
|
||||
} // else *control is already kOnceDone
|
||||
}
|
||||
|
||||
inline std::atomic<uint32_t>* ControlWord(once_flag* flag) {
|
||||
return &flag->control_;
|
||||
}
|
||||
|
||||
template <typename Callable, typename... Args>
|
||||
void LowLevelCallOnce(absl::once_flag* flag, Callable&& fn, Args&&... args) {
|
||||
std::atomic<uint32_t>* once = base_internal::ControlWord(flag);
|
||||
uint32_t s = once->load(std::memory_order_acquire);
|
||||
if (ABSL_PREDICT_FALSE(s != base_internal::kOnceDone)) {
|
||||
base_internal::CallOnceImpl(once, base_internal::SCHEDULE_KERNEL_ONLY,
|
||||
std::forward<Callable>(fn),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
|
||||
template <typename Callable, typename... Args>
|
||||
void call_once(absl::once_flag& flag, Callable&& fn, Args&&... args) {
|
||||
std::atomic<uint32_t>* once = base_internal::ControlWord(&flag);
|
||||
uint32_t s = once->load(std::memory_order_acquire);
|
||||
if (ABSL_PREDICT_FALSE(s != base_internal::kOnceDone)) {
|
||||
base_internal::CallOnceImpl(
|
||||
once, base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL,
|
||||
std::forward<Callable>(fn), std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_CALL_ONCE_H_
|
|
@ -0,0 +1,107 @@
|
|||
// Copyright 2017 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/base/call_once.h"
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/const_init.h"
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace {
|
||||
|
||||
absl::once_flag once;
|
||||
|
||||
ABSL_CONST_INIT Mutex counters_mu(absl::kConstInit);
|
||||
|
||||
int running_thread_count ABSL_GUARDED_BY(counters_mu) = 0;
|
||||
int call_once_invoke_count ABSL_GUARDED_BY(counters_mu) = 0;
|
||||
int call_once_finished_count ABSL_GUARDED_BY(counters_mu) = 0;
|
||||
int call_once_return_count ABSL_GUARDED_BY(counters_mu) = 0;
|
||||
bool done_blocking ABSL_GUARDED_BY(counters_mu) = false;
|
||||
|
||||
// Function to be called from absl::call_once. Waits for a notification.
|
||||
void WaitAndIncrement() {
|
||||
counters_mu.Lock();
|
||||
++call_once_invoke_count;
|
||||
counters_mu.Unlock();
|
||||
|
||||
counters_mu.LockWhen(Condition(&done_blocking));
|
||||
++call_once_finished_count;
|
||||
counters_mu.Unlock();
|
||||
}
|
||||
|
||||
void ThreadBody() {
|
||||
counters_mu.Lock();
|
||||
++running_thread_count;
|
||||
counters_mu.Unlock();
|
||||
|
||||
absl::call_once(once, WaitAndIncrement);
|
||||
|
||||
counters_mu.Lock();
|
||||
++call_once_return_count;
|
||||
counters_mu.Unlock();
|
||||
}
|
||||
|
||||
// Returns true if all threads are set up for the test.
|
||||
bool ThreadsAreSetup(void*) ABSL_EXCLUSIVE_LOCKS_REQUIRED(counters_mu) {
|
||||
// All ten threads must be running, and WaitAndIncrement should be blocked.
|
||||
return running_thread_count == 10 && call_once_invoke_count == 1;
|
||||
}
|
||||
|
||||
TEST(CallOnceTest, ExecutionCount) {
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
// Start 10 threads all calling call_once on the same once_flag.
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
threads.emplace_back(ThreadBody);
|
||||
}
|
||||
|
||||
|
||||
// Wait until all ten threads have started, and WaitAndIncrement has been
|
||||
// invoked.
|
||||
counters_mu.LockWhen(Condition(ThreadsAreSetup, nullptr));
|
||||
|
||||
// WaitAndIncrement should have been invoked by exactly one call_once()
|
||||
// instance. That thread should be blocking on a notification, and all other
|
||||
// call_once instances should be blocking as well.
|
||||
EXPECT_EQ(call_once_invoke_count, 1);
|
||||
EXPECT_EQ(call_once_finished_count, 0);
|
||||
EXPECT_EQ(call_once_return_count, 0);
|
||||
|
||||
// Allow WaitAndIncrement to finish executing. Once it does, the other
|
||||
// call_once waiters will be unblocked.
|
||||
done_blocking = true;
|
||||
counters_mu.Unlock();
|
||||
|
||||
for (std::thread& thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
counters_mu.Lock();
|
||||
EXPECT_EQ(call_once_invoke_count, 1);
|
||||
EXPECT_EQ(call_once_finished_count, 1);
|
||||
EXPECT_EQ(call_once_return_count, 10);
|
||||
counters_mu.Unlock();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,187 @@
|
|||
//
|
||||
// Copyright 2017 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: casts.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file defines casting templates to fit use cases not covered by
|
||||
// the standard casts provided in the C++ standard. As with all cast operations,
|
||||
// use these with caution and only if alternatives do not exist.
|
||||
|
||||
#ifndef ABSL_BASE_CASTS_H_
|
||||
#define ABSL_BASE_CASTS_H_
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/internal/identity.h"
|
||||
#include "absl/base/macros.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
namespace internal_casts {
|
||||
|
||||
template <class Dest, class Source>
|
||||
struct is_bitcastable
|
||||
: std::integral_constant<
|
||||
bool,
|
||||
sizeof(Dest) == sizeof(Source) &&
|
||||
type_traits_internal::is_trivially_copyable<Source>::value &&
|
||||
type_traits_internal::is_trivially_copyable<Dest>::value &&
|
||||
std::is_default_constructible<Dest>::value> {};
|
||||
|
||||
} // namespace internal_casts
|
||||
|
||||
// implicit_cast()
|
||||
//
|
||||
// Performs an implicit conversion between types following the language
|
||||
// rules for implicit conversion; if an implicit conversion is otherwise
|
||||
// allowed by the language in the given context, this function performs such an
|
||||
// implicit conversion.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // If the context allows implicit conversion:
|
||||
// From from;
|
||||
// To to = from;
|
||||
//
|
||||
// // Such code can be replaced by:
|
||||
// implicit_cast<To>(from);
|
||||
//
|
||||
// An `implicit_cast()` may also be used to annotate numeric type conversions
|
||||
// that, although safe, may produce compiler warnings (such as `long` to `int`).
|
||||
// Additionally, an `implicit_cast()` is also useful within return statements to
|
||||
// indicate a specific implicit conversion is being undertaken.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// return implicit_cast<double>(size_in_bytes) / capacity_;
|
||||
//
|
||||
// Annotating code with `implicit_cast()` allows you to explicitly select
|
||||
// particular overloads and template instantiations, while providing a safer
|
||||
// cast than `reinterpret_cast()` or `static_cast()`.
|
||||
//
|
||||
// Additionally, an `implicit_cast()` can be used to allow upcasting within a
|
||||
// type hierarchy where incorrect use of `static_cast()` could accidentally
|
||||
// allow downcasting.
|
||||
//
|
||||
// Finally, an `implicit_cast()` can be used to perform implicit conversions
|
||||
// from unrelated types that otherwise couldn't be implicitly cast directly;
|
||||
// C++ will normally only implicitly cast "one step" in such conversions.
|
||||
//
|
||||
// That is, if C is a type which can be implicitly converted to B, with B being
|
||||
// a type that can be implicitly converted to A, an `implicit_cast()` can be
|
||||
// used to convert C to B (which the compiler can then implicitly convert to A
|
||||
// using language rules).
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Assume an object C is convertible to B, which is implicitly convertible
|
||||
// // to A
|
||||
// A a = implicit_cast<B>(C);
|
||||
//
|
||||
// Such implicit cast chaining may be useful within template logic.
|
||||
template <typename To>
|
||||
constexpr To implicit_cast(typename absl::internal::identity_t<To> to) {
|
||||
return to;
|
||||
}
|
||||
|
||||
// bit_cast()
|
||||
//
|
||||
// Performs a bitwise cast on a type without changing the underlying bit
|
||||
// representation of that type's value. The two types must be of the same size
|
||||
// and both types must be trivially copyable. As with most casts, use with
|
||||
// caution. A `bit_cast()` might be needed when you need to temporarily treat a
|
||||
// type as some other type, such as in the following cases:
|
||||
//
|
||||
// * Serialization (casting temporarily to `char *` for those purposes is
|
||||
// always allowed by the C++ standard)
|
||||
// * Managing the individual bits of a type within mathematical operations
|
||||
// that are not normally accessible through that type
|
||||
// * Casting non-pointer types to pointer types (casting the other way is
|
||||
// allowed by `reinterpret_cast()` but round-trips cannot occur the other
|
||||
// way).
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// float f = 3.14159265358979;
|
||||
// int i = bit_cast<int32_t>(f);
|
||||
// // i = 0x40490fdb
|
||||
//
|
||||
// Casting non-pointer types to pointer types and then dereferencing them
|
||||
// traditionally produces undefined behavior.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // WRONG
|
||||
// float f = 3.14159265358979; // WRONG
|
||||
// int i = * reinterpret_cast<int*>(&f); // WRONG
|
||||
//
|
||||
// The address-casting method produces undefined behavior according to the ISO
|
||||
// C++ specification section [basic.lval]. Roughly, this section says: if an
|
||||
// object in memory has one type, and a program accesses it with a different
|
||||
// type, the result is undefined behavior for most values of "different type".
|
||||
//
|
||||
// Such casting results in type punning: holding an object in memory of one type
|
||||
// and reading its bits back using a different type. A `bit_cast()` avoids this
|
||||
// issue by implementing its casts using `memcpy()`, which avoids introducing
|
||||
// this undefined behavior.
|
||||
//
|
||||
// NOTE: The requirements here are more strict than the bit_cast of standard
|
||||
// proposal p0476 due to the need for workarounds and lack of intrinsics.
|
||||
// Specifically, this implementation also requires `Dest` to be
|
||||
// default-constructible.
|
||||
template <
|
||||
typename Dest, typename Source,
|
||||
typename std::enable_if<internal_casts::is_bitcastable<Dest, Source>::value,
|
||||
int>::type = 0>
|
||||
inline Dest bit_cast(const Source& source) {
|
||||
Dest dest;
|
||||
memcpy(static_cast<void*>(std::addressof(dest)),
|
||||
static_cast<const void*>(std::addressof(source)), sizeof(dest));
|
||||
return dest;
|
||||
}
|
||||
|
||||
// NOTE: This overload is only picked if the requirements of bit_cast are
|
||||
// not met. It is therefore UB, but is provided temporarily as previous
|
||||
// versions of this function template were unchecked. Do not use this in
|
||||
// new code.
|
||||
template <
|
||||
typename Dest, typename Source,
|
||||
typename std::enable_if<
|
||||
!internal_casts::is_bitcastable<Dest, Source>::value,
|
||||
int>::type = 0>
|
||||
ABSL_DEPRECATED(
|
||||
"absl::bit_cast type requirements were violated. Update the types "
|
||||
"being used such that they are the same size and are both "
|
||||
"TriviallyCopyable.")
|
||||
inline Dest bit_cast(const Source& source) {
|
||||
static_assert(sizeof(Dest) == sizeof(Source),
|
||||
"Source and destination types should have equal sizes.");
|
||||
|
||||
Dest dest;
|
||||
memcpy(&dest, &source, sizeof(dest));
|
||||
return dest;
|
||||
}
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_CASTS_H_
|
|
@ -0,0 +1,740 @@
|
|||
//
|
||||
// Copyright 2017 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: config.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file defines a set of macros for checking the presence of
|
||||
// important compiler and platform features. Such macros can be used to
|
||||
// produce portable code by parameterizing compilation based on the presence or
|
||||
// lack of a given feature.
|
||||
//
|
||||
// We define a "feature" as some interface we wish to program to: for example,
|
||||
// a library function or system call. A value of `1` indicates support for
|
||||
// that feature; any other value indicates the feature support is undefined.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// Suppose a programmer wants to write a program that uses the 'mmap()' system
|
||||
// call. The Abseil macro for that feature (`ABSL_HAVE_MMAP`) allows you to
|
||||
// selectively include the `mmap.h` header and bracket code using that feature
|
||||
// in the macro:
|
||||
//
|
||||
// #include "absl/base/config.h"
|
||||
//
|
||||
// #ifdef ABSL_HAVE_MMAP
|
||||
// #include "sys/mman.h"
|
||||
// #endif //ABSL_HAVE_MMAP
|
||||
//
|
||||
// ...
|
||||
// #ifdef ABSL_HAVE_MMAP
|
||||
// void *ptr = mmap(...);
|
||||
// ...
|
||||
// #endif // ABSL_HAVE_MMAP
|
||||
|
||||
#ifndef ABSL_BASE_CONFIG_H_
|
||||
#define ABSL_BASE_CONFIG_H_
|
||||
|
||||
// Included for the __GLIBC__ macro (or similar macros on other systems).
|
||||
#include <limits.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
// Included for __GLIBCXX__, _LIBCPP_VERSION
|
||||
#include <cstddef>
|
||||
#endif // __cplusplus
|
||||
|
||||
#if defined(__APPLE__)
|
||||
// Included for TARGET_OS_IPHONE, __IPHONE_OS_VERSION_MIN_REQUIRED,
|
||||
// __IPHONE_8_0.
|
||||
#include <Availability.h>
|
||||
#include <TargetConditionals.h>
|
||||
#endif
|
||||
|
||||
#include "absl/base/options.h"
|
||||
#include "absl/base/policy_checks.h"
|
||||
|
||||
// Helper macro to convert a CPP variable to a string literal.
|
||||
#define ABSL_INTERNAL_DO_TOKEN_STR(x) #x
|
||||
#define ABSL_INTERNAL_TOKEN_STR(x) ABSL_INTERNAL_DO_TOKEN_STR(x)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Abseil namespace annotations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// ABSL_NAMESPACE_BEGIN/ABSL_NAMESPACE_END
|
||||
//
|
||||
// An annotation placed at the beginning/end of each `namespace absl` scope.
|
||||
// This is used to inject an inline namespace.
|
||||
//
|
||||
// The proper way to write Abseil code in the `absl` namespace is:
|
||||
//
|
||||
// namespace absl {
|
||||
// ABSL_NAMESPACE_BEGIN
|
||||
//
|
||||
// void Foo(); // absl::Foo().
|
||||
//
|
||||
// ABSL_NAMESPACE_END
|
||||
// } // namespace absl
|
||||
//
|
||||
// Users of Abseil should not use these macros, because users of Abseil should
|
||||
// not write `namespace absl {` in their own code for any reason. (Abseil does
|
||||
// not support forward declarations of its own types, nor does it support
|
||||
// user-provided specialization of Abseil templates. Code that violates these
|
||||
// rules may be broken without warning.)
|
||||
#if !defined(ABSL_OPTION_USE_INLINE_NAMESPACE) || \
|
||||
!defined(ABSL_OPTION_INLINE_NAMESPACE_NAME)
|
||||
#error options.h is misconfigured.
|
||||
#endif
|
||||
|
||||
// Check that ABSL_OPTION_INLINE_NAMESPACE_NAME is neither "head" nor ""
|
||||
#if defined(__cplusplus) && ABSL_OPTION_USE_INLINE_NAMESPACE == 1
|
||||
|
||||
#define ABSL_INTERNAL_INLINE_NAMESPACE_STR \
|
||||
ABSL_INTERNAL_TOKEN_STR(ABSL_OPTION_INLINE_NAMESPACE_NAME)
|
||||
|
||||
static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != '\0',
|
||||
"options.h misconfigured: ABSL_OPTION_INLINE_NAMESPACE_NAME must "
|
||||
"not be empty.");
|
||||
static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != 'h' ||
|
||||
ABSL_INTERNAL_INLINE_NAMESPACE_STR[1] != 'e' ||
|
||||
ABSL_INTERNAL_INLINE_NAMESPACE_STR[2] != 'a' ||
|
||||
ABSL_INTERNAL_INLINE_NAMESPACE_STR[3] != 'd' ||
|
||||
ABSL_INTERNAL_INLINE_NAMESPACE_STR[4] != '\0',
|
||||
"options.h misconfigured: ABSL_OPTION_INLINE_NAMESPACE_NAME must "
|
||||
"be changed to a new, unique identifier name.");
|
||||
|
||||
#endif
|
||||
|
||||
#if ABSL_OPTION_USE_INLINE_NAMESPACE == 0
|
||||
#define ABSL_NAMESPACE_BEGIN
|
||||
#define ABSL_NAMESPACE_END
|
||||
#define ABSL_INTERNAL_C_SYMBOL(x) x
|
||||
#elif ABSL_OPTION_USE_INLINE_NAMESPACE == 1
|
||||
#define ABSL_NAMESPACE_BEGIN \
|
||||
inline namespace ABSL_OPTION_INLINE_NAMESPACE_NAME {
|
||||
#define ABSL_NAMESPACE_END }
|
||||
#define ABSL_INTERNAL_C_SYMBOL_HELPER_2(x, v) x##_##v
|
||||
#define ABSL_INTERNAL_C_SYMBOL_HELPER_1(x, v) \
|
||||
ABSL_INTERNAL_C_SYMBOL_HELPER_2(x, v)
|
||||
#define ABSL_INTERNAL_C_SYMBOL(x) \
|
||||
ABSL_INTERNAL_C_SYMBOL_HELPER_1(x, ABSL_OPTION_INLINE_NAMESPACE_NAME)
|
||||
#else
|
||||
#error options.h is misconfigured.
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Compiler Feature Checks
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// ABSL_HAVE_BUILTIN()
|
||||
//
|
||||
// Checks whether the compiler supports a Clang Feature Checking Macro, and if
|
||||
// so, checks whether it supports the provided builtin function "x" where x
|
||||
// is one of the functions noted in
|
||||
// https://clang.llvm.org/docs/LanguageExtensions.html
|
||||
//
|
||||
// Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
|
||||
// http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html
|
||||
#ifdef __has_builtin
|
||||
#define ABSL_HAVE_BUILTIN(x) __has_builtin(x)
|
||||
#else
|
||||
#define ABSL_HAVE_BUILTIN(x) 0
|
||||
#endif
|
||||
|
||||
#if defined(__is_identifier)
|
||||
#define ABSL_INTERNAL_HAS_KEYWORD(x) !(__is_identifier(x))
|
||||
#else
|
||||
#define ABSL_INTERNAL_HAS_KEYWORD(x) 0
|
||||
#endif
|
||||
|
||||
#ifdef __has_feature
|
||||
#define ABSL_HAVE_FEATURE(f) __has_feature(f)
|
||||
#else
|
||||
#define ABSL_HAVE_FEATURE(f) 0
|
||||
#endif
|
||||
|
||||
// Portable check for GCC minimum version:
|
||||
// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
|
||||
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
|
||||
#define ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(x, y) \
|
||||
(__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
|
||||
#else
|
||||
#define ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(x, y) 0
|
||||
#endif
|
||||
|
||||
#if defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__)
|
||||
#define ABSL_INTERNAL_HAVE_MIN_CLANG_VERSION(x, y) \
|
||||
(__clang_major__ > (x) || __clang_major__ == (x) && __clang_minor__ >= (y))
|
||||
#else
|
||||
#define ABSL_INTERNAL_HAVE_MIN_CLANG_VERSION(x, y) 0
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_TLS is defined to 1 when __thread should be supported.
|
||||
// We assume __thread is supported on Linux when compiled with Clang or compiled
|
||||
// against libstdc++ with _GLIBCXX_HAVE_TLS defined.
|
||||
#ifdef ABSL_HAVE_TLS
|
||||
#error ABSL_HAVE_TLS cannot be directly set
|
||||
#elif defined(__linux__) && (defined(__clang__) || defined(_GLIBCXX_HAVE_TLS))
|
||||
#define ABSL_HAVE_TLS 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
|
||||
//
|
||||
// Checks whether `std::is_trivially_destructible<T>` is supported.
|
||||
//
|
||||
// Notes: All supported compilers using libc++ support this feature, as does
|
||||
// gcc >= 4.8.1 using libstdc++, and Visual Studio.
|
||||
#ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE
|
||||
#error ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE cannot be directly set
|
||||
#elif defined(_LIBCPP_VERSION) || defined(_MSC_VER) || \
|
||||
(!defined(__clang__) && defined(__GLIBCXX__) && \
|
||||
ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(4, 8))
|
||||
#define ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE
|
||||
//
|
||||
// Checks whether `std::is_trivially_default_constructible<T>` and
|
||||
// `std::is_trivially_copy_constructible<T>` are supported.
|
||||
|
||||
// ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
|
||||
//
|
||||
// Checks whether `std::is_trivially_copy_assignable<T>` is supported.
|
||||
|
||||
// Notes: Clang with libc++ supports these features, as does gcc >= 5.1 with
|
||||
// either libc++ or libstdc++, and Visual Studio (but not NVCC).
|
||||
#if defined(ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE)
|
||||
#error ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE cannot be directly set
|
||||
#elif defined(ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE)
|
||||
#error ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE cannot directly set
|
||||
#elif (defined(__clang__) && defined(_LIBCPP_VERSION)) || \
|
||||
(!defined(__clang__) && ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(7, 4) && \
|
||||
(defined(_LIBCPP_VERSION) || defined(__GLIBCXX__))) || \
|
||||
(defined(_MSC_VER) && !defined(__NVCC__))
|
||||
#define ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE 1
|
||||
#define ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_SOURCE_LOCATION_CURRENT
|
||||
//
|
||||
// Indicates whether `absl::SourceLocation::current()` will return useful
|
||||
// information in some contexts.
|
||||
#ifndef ABSL_HAVE_SOURCE_LOCATION_CURRENT
|
||||
#if ABSL_INTERNAL_HAS_KEYWORD(__builtin_LINE) && \
|
||||
ABSL_INTERNAL_HAS_KEYWORD(__builtin_FILE)
|
||||
#define ABSL_HAVE_SOURCE_LOCATION_CURRENT 1
|
||||
#elif ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(5, 0)
|
||||
#define ABSL_HAVE_SOURCE_LOCATION_CURRENT 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_THREAD_LOCAL
|
||||
//
|
||||
// Checks whether C++11's `thread_local` storage duration specifier is
|
||||
// supported.
|
||||
#ifdef ABSL_HAVE_THREAD_LOCAL
|
||||
#error ABSL_HAVE_THREAD_LOCAL cannot be directly set
|
||||
#elif defined(__APPLE__)
|
||||
// Notes:
|
||||
// * Xcode's clang did not support `thread_local` until version 8, and
|
||||
// even then not for all iOS < 9.0.
|
||||
// * Xcode 9.3 started disallowing `thread_local` for 32-bit iOS simulator
|
||||
// targeting iOS 9.x.
|
||||
// * Xcode 10 moves the deployment target check for iOS < 9.0 to link time
|
||||
// making ABSL_HAVE_FEATURE unreliable there.
|
||||
//
|
||||
#if ABSL_HAVE_FEATURE(cxx_thread_local) && \
|
||||
!(TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)
|
||||
#define ABSL_HAVE_THREAD_LOCAL 1
|
||||
#endif
|
||||
#else // !defined(__APPLE__)
|
||||
#define ABSL_HAVE_THREAD_LOCAL 1
|
||||
#endif
|
||||
|
||||
// There are platforms for which TLS should not be used even though the compiler
|
||||
// makes it seem like it's supported (Android NDK < r12b for example).
|
||||
// This is primarily because of linker problems and toolchain misconfiguration:
|
||||
// Abseil does not intend to support this indefinitely. Currently, the newest
|
||||
// toolchain that we intend to support that requires this behavior is the
|
||||
// r11 NDK - allowing for a 5 year support window on that means this option
|
||||
// is likely to be removed around June of 2021.
|
||||
// TLS isn't supported until NDK r12b per
|
||||
// https://developer.android.com/ndk/downloads/revision_history.html
|
||||
// Since NDK r16, `__NDK_MAJOR__` and `__NDK_MINOR__` are defined in
|
||||
// <android/ndk-version.h>. For NDK < r16, users should define these macros,
|
||||
// e.g. `-D__NDK_MAJOR__=11 -D__NKD_MINOR__=0` for NDK r11.
|
||||
#if defined(__ANDROID__) && defined(__clang__)
|
||||
#if __has_include(<android/ndk-version.h>)
|
||||
#include <android/ndk-version.h>
|
||||
#endif // __has_include(<android/ndk-version.h>)
|
||||
#if defined(__ANDROID__) && defined(__clang__) && defined(__NDK_MAJOR__) && \
|
||||
defined(__NDK_MINOR__) && \
|
||||
((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))
|
||||
#undef ABSL_HAVE_TLS
|
||||
#undef ABSL_HAVE_THREAD_LOCAL
|
||||
#endif
|
||||
#endif // defined(__ANDROID__) && defined(__clang__)
|
||||
|
||||
// ABSL_HAVE_INTRINSIC_INT128
|
||||
//
|
||||
// Checks whether the __int128 compiler extension for a 128-bit integral type is
|
||||
// supported.
|
||||
//
|
||||
// Note: __SIZEOF_INT128__ is defined by Clang and GCC when __int128 is
|
||||
// supported, but we avoid using it in certain cases:
|
||||
// * On Clang:
|
||||
// * Building using Clang for Windows, where the Clang runtime library has
|
||||
// 128-bit support only on LP64 architectures, but Windows is LLP64.
|
||||
// * On Nvidia's nvcc:
|
||||
// * nvcc also defines __GNUC__ and __SIZEOF_INT128__, but not all versions
|
||||
// actually support __int128.
|
||||
#ifdef ABSL_HAVE_INTRINSIC_INT128
|
||||
#error ABSL_HAVE_INTRINSIC_INT128 cannot be directly set
|
||||
#elif defined(__SIZEOF_INT128__)
|
||||
#if (defined(__clang__) && !defined(_WIN32)) || \
|
||||
(defined(__CUDACC__) && __CUDACC_VER_MAJOR__ >= 9) || \
|
||||
(defined(__GNUC__) && !defined(__clang__) && !defined(__CUDACC__))
|
||||
#define ABSL_HAVE_INTRINSIC_INT128 1
|
||||
#elif defined(__CUDACC__)
|
||||
// __CUDACC_VER__ is a full version number before CUDA 9, and is defined to a
|
||||
// string explaining that it has been removed starting with CUDA 9. We use
|
||||
// nested #ifs because there is no short-circuiting in the preprocessor.
|
||||
// NOTE: `__CUDACC__` could be undefined while `__CUDACC_VER__` is defined.
|
||||
#if __CUDACC_VER__ >= 70000
|
||||
#define ABSL_HAVE_INTRINSIC_INT128 1
|
||||
#endif // __CUDACC_VER__ >= 70000
|
||||
#endif // defined(__CUDACC__)
|
||||
#endif // ABSL_HAVE_INTRINSIC_INT128
|
||||
|
||||
// ABSL_HAVE_EXCEPTIONS
|
||||
//
|
||||
// Checks whether the compiler both supports and enables exceptions. Many
|
||||
// compilers support a "no exceptions" mode that disables exceptions.
|
||||
//
|
||||
// Generally, when ABSL_HAVE_EXCEPTIONS is not defined:
|
||||
//
|
||||
// * Code using `throw` and `try` may not compile.
|
||||
// * The `noexcept` specifier will still compile and behave as normal.
|
||||
// * The `noexcept` operator may still return `false`.
|
||||
//
|
||||
// For further details, consult the compiler's documentation.
|
||||
#ifdef ABSL_HAVE_EXCEPTIONS
|
||||
#error ABSL_HAVE_EXCEPTIONS cannot be directly set.
|
||||
#elif ABSL_INTERNAL_HAVE_MIN_CLANG_VERSION(3, 6)
|
||||
// Clang >= 3.6
|
||||
#if ABSL_HAVE_FEATURE(cxx_exceptions)
|
||||
#define ABSL_HAVE_EXCEPTIONS 1
|
||||
#endif // ABSL_HAVE_FEATURE(cxx_exceptions)
|
||||
#elif defined(__clang__)
|
||||
// Clang < 3.6
|
||||
// http://releases.llvm.org/3.6.0/tools/clang/docs/ReleaseNotes.html#the-exceptions-macro
|
||||
#if defined(__EXCEPTIONS) && ABSL_HAVE_FEATURE(cxx_exceptions)
|
||||
#define ABSL_HAVE_EXCEPTIONS 1
|
||||
#endif // defined(__EXCEPTIONS) && ABSL_HAVE_FEATURE(cxx_exceptions)
|
||||
// Handle remaining special cases and default to exceptions being supported.
|
||||
#elif !(defined(__GNUC__) && (__GNUC__ < 5) && !defined(__EXCEPTIONS)) && \
|
||||
!(ABSL_INTERNAL_HAVE_MIN_GNUC_VERSION(5, 0) && \
|
||||
!defined(__cpp_exceptions)) && \
|
||||
!(defined(_MSC_VER) && !defined(_CPPUNWIND))
|
||||
#define ABSL_HAVE_EXCEPTIONS 1
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Platform Feature Checks
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Currently supported operating systems and associated preprocessor
|
||||
// symbols:
|
||||
//
|
||||
// Linux and Linux-derived __linux__
|
||||
// Android __ANDROID__ (implies __linux__)
|
||||
// Linux (non-Android) __linux__ && !__ANDROID__
|
||||
// Darwin (macOS and iOS) __APPLE__
|
||||
// Akaros (http://akaros.org) __ros__
|
||||
// Windows _WIN32
|
||||
// NaCL __native_client__
|
||||
// AsmJS __asmjs__
|
||||
// WebAssembly __wasm__
|
||||
// Fuchsia __Fuchsia__
|
||||
//
|
||||
// Note that since Android defines both __ANDROID__ and __linux__, one
|
||||
// may probe for either Linux or Android by simply testing for __linux__.
|
||||
|
||||
// ABSL_HAVE_MMAP
|
||||
//
|
||||
// Checks whether the platform has an mmap(2) implementation as defined in
|
||||
// POSIX.1-2001.
|
||||
#ifdef ABSL_HAVE_MMAP
|
||||
#error ABSL_HAVE_MMAP cannot be directly set
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
|
||||
defined(__ros__) || defined(__native_client__) || defined(__asmjs__) || \
|
||||
defined(__wasm__) || defined(__Fuchsia__) || defined(__sun) || \
|
||||
defined(__ASYLO__) || defined(__myriad2__)
|
||||
#define ABSL_HAVE_MMAP 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_PTHREAD_GETSCHEDPARAM
|
||||
//
|
||||
// Checks whether the platform implements the pthread_(get|set)schedparam(3)
|
||||
// functions as defined in POSIX.1-2001.
|
||||
#ifdef ABSL_HAVE_PTHREAD_GETSCHEDPARAM
|
||||
#error ABSL_HAVE_PTHREAD_GETSCHEDPARAM cannot be directly set
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
|
||||
defined(__ros__)
|
||||
#define ABSL_HAVE_PTHREAD_GETSCHEDPARAM 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_SCHED_GETCPU
|
||||
//
|
||||
// Checks whether sched_getcpu is available.
|
||||
#ifdef ABSL_HAVE_SCHED_GETCPU
|
||||
#error ABSL_HAVE_SCHED_GETCPU cannot be directly set
|
||||
#elif defined(__linux__)
|
||||
#define ABSL_HAVE_SCHED_GETCPU 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_SCHED_YIELD
|
||||
//
|
||||
// Checks whether the platform implements sched_yield(2) as defined in
|
||||
// POSIX.1-2001.
|
||||
#ifdef ABSL_HAVE_SCHED_YIELD
|
||||
#error ABSL_HAVE_SCHED_YIELD cannot be directly set
|
||||
#elif defined(__linux__) || defined(__ros__) || defined(__native_client__)
|
||||
#define ABSL_HAVE_SCHED_YIELD 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_SEMAPHORE_H
|
||||
//
|
||||
// Checks whether the platform supports the <semaphore.h> header and sem_init(3)
|
||||
// family of functions as standardized in POSIX.1-2001.
|
||||
//
|
||||
// Note: While Apple provides <semaphore.h> for both iOS and macOS, it is
|
||||
// explicitly deprecated and will cause build failures if enabled for those
|
||||
// platforms. We side-step the issue by not defining it here for Apple
|
||||
// platforms.
|
||||
#ifdef ABSL_HAVE_SEMAPHORE_H
|
||||
#error ABSL_HAVE_SEMAPHORE_H cannot be directly set
|
||||
#elif defined(__linux__) || defined(__ros__)
|
||||
#define ABSL_HAVE_SEMAPHORE_H 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_ALARM
|
||||
//
|
||||
// Checks whether the platform supports the <signal.h> header and alarm(2)
|
||||
// function as standardized in POSIX.1-2001.
|
||||
#ifdef ABSL_HAVE_ALARM
|
||||
#error ABSL_HAVE_ALARM cannot be directly set
|
||||
#elif defined(__GOOGLE_GRTE_VERSION__)
|
||||
// feature tests for Google's GRTE
|
||||
#define ABSL_HAVE_ALARM 1
|
||||
#elif defined(__GLIBC__)
|
||||
// feature test for glibc
|
||||
#define ABSL_HAVE_ALARM 1
|
||||
#elif defined(_MSC_VER)
|
||||
// feature tests for Microsoft's library
|
||||
#elif defined(__MINGW32__)
|
||||
// mingw32 doesn't provide alarm(2):
|
||||
// https://osdn.net/projects/mingw/scm/git/mingw-org-wsl/blobs/5.2-trunk/mingwrt/include/unistd.h
|
||||
// mingw-w64 provides a no-op implementation:
|
||||
// https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-crt/misc/alarm.c
|
||||
#elif defined(__EMSCRIPTEN__)
|
||||
// emscripten doesn't support signals
|
||||
#elif defined(__Fuchsia__)
|
||||
// Signals don't exist on fuchsia.
|
||||
#elif defined(__native_client__)
|
||||
#else
|
||||
// other standard libraries
|
||||
#define ABSL_HAVE_ALARM 1
|
||||
#endif
|
||||
|
||||
// ABSL_IS_LITTLE_ENDIAN
|
||||
// ABSL_IS_BIG_ENDIAN
|
||||
//
|
||||
// Checks the endianness of the platform.
|
||||
//
|
||||
// Notes: uses the built in endian macros provided by GCC (since 4.6) and
|
||||
// Clang (since 3.2); see
|
||||
// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html.
|
||||
// Otherwise, if _WIN32, assume little endian. Otherwise, bail with an error.
|
||||
#if defined(ABSL_IS_BIG_ENDIAN)
|
||||
#error "ABSL_IS_BIG_ENDIAN cannot be directly set."
|
||||
#endif
|
||||
#if defined(ABSL_IS_LITTLE_ENDIAN)
|
||||
#error "ABSL_IS_LITTLE_ENDIAN cannot be directly set."
|
||||
#endif
|
||||
|
||||
#if (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
|
||||
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
|
||||
#define ABSL_IS_LITTLE_ENDIAN 1
|
||||
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
|
||||
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define ABSL_IS_BIG_ENDIAN 1
|
||||
#elif defined(_WIN32)
|
||||
#define ABSL_IS_LITTLE_ENDIAN 1
|
||||
#else
|
||||
#error "absl endian detection needs to be set up for your compiler"
|
||||
#endif
|
||||
|
||||
// macOS 10.13 and iOS 10.11 don't let you use <any>, <optional>, or <variant>
|
||||
// even though the headers exist and are publicly noted to work. See
|
||||
// https://github.com/abseil/abseil-cpp/issues/207 and
|
||||
// https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
|
||||
// libc++ spells out the availability requirements in the file
|
||||
// llvm-project/libcxx/include/__config via the #define
|
||||
// _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS.
|
||||
#if defined(__APPLE__) && defined(_LIBCPP_VERSION) && \
|
||||
((defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
|
||||
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101400) || \
|
||||
(defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) && \
|
||||
__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 120000) || \
|
||||
(defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && \
|
||||
__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 50000) || \
|
||||
(defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__) && \
|
||||
__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ < 120000))
|
||||
#define ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE 1
|
||||
#else
|
||||
#define ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE 0
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_STD_ANY
|
||||
//
|
||||
// Checks whether C++17 std::any is available by checking whether <any> exists.
|
||||
#ifdef ABSL_HAVE_STD_ANY
|
||||
#error "ABSL_HAVE_STD_ANY cannot be directly set."
|
||||
#endif
|
||||
|
||||
#ifdef __has_include
|
||||
#if __has_include(<any>) && defined(__cplusplus) && __cplusplus >= 201703L && \
|
||||
!ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE
|
||||
#define ABSL_HAVE_STD_ANY 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_STD_OPTIONAL
|
||||
//
|
||||
// Checks whether C++17 std::optional is available.
|
||||
#ifdef ABSL_HAVE_STD_OPTIONAL
|
||||
#error "ABSL_HAVE_STD_OPTIONAL cannot be directly set."
|
||||
#endif
|
||||
|
||||
#ifdef __has_include
|
||||
#if __has_include(<optional>) && defined(__cplusplus) && \
|
||||
__cplusplus >= 201703L && !ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE
|
||||
#define ABSL_HAVE_STD_OPTIONAL 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_STD_VARIANT
|
||||
//
|
||||
// Checks whether C++17 std::variant is available.
|
||||
#ifdef ABSL_HAVE_STD_VARIANT
|
||||
#error "ABSL_HAVE_STD_VARIANT cannot be directly set."
|
||||
#endif
|
||||
|
||||
#ifdef __has_include
|
||||
#if __has_include(<variant>) && defined(__cplusplus) && \
|
||||
__cplusplus >= 201703L && !ABSL_INTERNAL_APPLE_CXX17_TYPES_UNAVAILABLE
|
||||
#define ABSL_HAVE_STD_VARIANT 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_STD_STRING_VIEW
|
||||
//
|
||||
// Checks whether C++17 std::string_view is available.
|
||||
#ifdef ABSL_HAVE_STD_STRING_VIEW
|
||||
#error "ABSL_HAVE_STD_STRING_VIEW cannot be directly set."
|
||||
#endif
|
||||
|
||||
#ifdef __has_include
|
||||
#if __has_include(<string_view>) && defined(__cplusplus) && \
|
||||
__cplusplus >= 201703L
|
||||
#define ABSL_HAVE_STD_STRING_VIEW 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// For MSVC, `__has_include` is supported in VS 2017 15.3, which is later than
|
||||
// the support for <optional>, <any>, <string_view>, <variant>. So we use
|
||||
// _MSC_VER to check whether we have VS 2017 RTM (when <optional>, <any>,
|
||||
// <string_view>, <variant> is implemented) or higher. Also, `__cplusplus` is
|
||||
// not correctly set by MSVC, so we use `_MSVC_LANG` to check the language
|
||||
// version.
|
||||
// TODO(zhangxy): fix tests before enabling aliasing for `std::any`.
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1910 && \
|
||||
((defined(_MSVC_LANG) && _MSVC_LANG > 201402) || \
|
||||
(defined(__cplusplus) && __cplusplus > 201402))
|
||||
// #define ABSL_HAVE_STD_ANY 1
|
||||
#define ABSL_HAVE_STD_OPTIONAL 1
|
||||
#define ABSL_HAVE_STD_VARIANT 1
|
||||
#define ABSL_HAVE_STD_STRING_VIEW 1
|
||||
#endif
|
||||
|
||||
// ABSL_USES_STD_ANY
|
||||
//
|
||||
// Indicates whether absl::any is an alias for std::any.
|
||||
#if !defined(ABSL_OPTION_USE_STD_ANY)
|
||||
#error options.h is misconfigured.
|
||||
#elif ABSL_OPTION_USE_STD_ANY == 0 || \
|
||||
(ABSL_OPTION_USE_STD_ANY == 2 && !defined(ABSL_HAVE_STD_ANY))
|
||||
#undef ABSL_USES_STD_ANY
|
||||
#elif ABSL_OPTION_USE_STD_ANY == 1 || \
|
||||
(ABSL_OPTION_USE_STD_ANY == 2 && defined(ABSL_HAVE_STD_ANY))
|
||||
#define ABSL_USES_STD_ANY 1
|
||||
#else
|
||||
#error options.h is misconfigured.
|
||||
#endif
|
||||
|
||||
// ABSL_USES_STD_OPTIONAL
|
||||
//
|
||||
// Indicates whether absl::optional is an alias for std::optional.
|
||||
#if !defined(ABSL_OPTION_USE_STD_OPTIONAL)
|
||||
#error options.h is misconfigured.
|
||||
#elif ABSL_OPTION_USE_STD_OPTIONAL == 0 || \
|
||||
(ABSL_OPTION_USE_STD_OPTIONAL == 2 && !defined(ABSL_HAVE_STD_OPTIONAL))
|
||||
#undef ABSL_USES_STD_OPTIONAL
|
||||
#elif ABSL_OPTION_USE_STD_OPTIONAL == 1 || \
|
||||
(ABSL_OPTION_USE_STD_OPTIONAL == 2 && defined(ABSL_HAVE_STD_OPTIONAL))
|
||||
#define ABSL_USES_STD_OPTIONAL 1
|
||||
#else
|
||||
#error options.h is misconfigured.
|
||||
#endif
|
||||
|
||||
// ABSL_USES_STD_VARIANT
|
||||
//
|
||||
// Indicates whether absl::variant is an alias for std::variant.
|
||||
#if !defined(ABSL_OPTION_USE_STD_VARIANT)
|
||||
#error options.h is misconfigured.
|
||||
#elif ABSL_OPTION_USE_STD_VARIANT == 0 || \
|
||||
(ABSL_OPTION_USE_STD_VARIANT == 2 && !defined(ABSL_HAVE_STD_VARIANT))
|
||||
#undef ABSL_USES_STD_VARIANT
|
||||
#elif ABSL_OPTION_USE_STD_VARIANT == 1 || \
|
||||
(ABSL_OPTION_USE_STD_VARIANT == 2 && defined(ABSL_HAVE_STD_VARIANT))
|
||||
#define ABSL_USES_STD_VARIANT 1
|
||||
#else
|
||||
#error options.h is misconfigured.
|
||||
#endif
|
||||
|
||||
// ABSL_USES_STD_STRING_VIEW
|
||||
//
|
||||
// Indicates whether absl::string_view is an alias for std::string_view.
|
||||
#if !defined(ABSL_OPTION_USE_STD_STRING_VIEW)
|
||||
#error options.h is misconfigured.
|
||||
#elif ABSL_OPTION_USE_STD_STRING_VIEW == 0 || \
|
||||
(ABSL_OPTION_USE_STD_STRING_VIEW == 2 && \
|
||||
!defined(ABSL_HAVE_STD_STRING_VIEW))
|
||||
#undef ABSL_USES_STD_STRING_VIEW
|
||||
#elif ABSL_OPTION_USE_STD_STRING_VIEW == 1 || \
|
||||
(ABSL_OPTION_USE_STD_STRING_VIEW == 2 && \
|
||||
defined(ABSL_HAVE_STD_STRING_VIEW))
|
||||
#define ABSL_USES_STD_STRING_VIEW 1
|
||||
#else
|
||||
#error options.h is misconfigured.
|
||||
#endif
|
||||
|
||||
// In debug mode, MSVC 2017's std::variant throws a EXCEPTION_ACCESS_VIOLATION
|
||||
// SEH exception from emplace for variant<SomeStruct> when constructing the
|
||||
// struct can throw. This defeats some of variant_test and
|
||||
// variant_exception_safety_test.
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_DEBUG)
|
||||
#define ABSL_INTERNAL_MSVC_2017_DBG_MODE
|
||||
#endif
|
||||
|
||||
// ABSL_INTERNAL_MANGLED_NS
|
||||
// ABSL_INTERNAL_MANGLED_BACKREFERENCE
|
||||
//
|
||||
// Internal macros for building up mangled names in our internal fork of CCTZ.
|
||||
// This implementation detail is only needed and provided for the MSVC build.
|
||||
//
|
||||
// These macros both expand to string literals. ABSL_INTERNAL_MANGLED_NS is
|
||||
// the mangled spelling of the `absl` namespace, and
|
||||
// ABSL_INTERNAL_MANGLED_BACKREFERENCE is a back-reference integer representing
|
||||
// the proper count to skip past the CCTZ fork namespace names. (This number
|
||||
// is one larger when there is an inline namespace name to skip.)
|
||||
#if defined(_MSC_VER)
|
||||
#if ABSL_OPTION_USE_INLINE_NAMESPACE == 0
|
||||
#define ABSL_INTERNAL_MANGLED_NS "absl"
|
||||
#define ABSL_INTERNAL_MANGLED_BACKREFERENCE "5"
|
||||
#else
|
||||
#define ABSL_INTERNAL_MANGLED_NS \
|
||||
ABSL_INTERNAL_TOKEN_STR(ABSL_OPTION_INLINE_NAMESPACE_NAME) "@absl"
|
||||
#define ABSL_INTERNAL_MANGLED_BACKREFERENCE "6"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#undef ABSL_INTERNAL_HAS_KEYWORD
|
||||
|
||||
// ABSL_DLL
|
||||
//
|
||||
// When building Abseil as a DLL, this macro expands to `__declspec(dllexport)`
|
||||
// so we can annotate symbols appropriately as being exported. When used in
|
||||
// headers consuming a DLL, this macro expands to `__declspec(dllimport)` so
|
||||
// that consumers know the symbol is defined inside the DLL. In all other cases,
|
||||
// the macro expands to nothing.
|
||||
#if defined(_MSC_VER)
|
||||
#if defined(ABSL_BUILD_DLL)
|
||||
#define ABSL_DLL __declspec(dllexport)
|
||||
#elif defined(ABSL_CONSUME_DLL)
|
||||
#define ABSL_DLL __declspec(dllimport)
|
||||
#else
|
||||
#define ABSL_DLL
|
||||
#endif
|
||||
#else
|
||||
#define ABSL_DLL
|
||||
#endif // defined(_MSC_VER)
|
||||
|
||||
// ABSL_HAVE_MEMORY_SANITIZER
|
||||
//
|
||||
// MemorySanitizer (MSan) is a detector of uninitialized reads. It consists of
|
||||
// a compiler instrumentation module and a run-time library.
|
||||
#ifdef ABSL_HAVE_MEMORY_SANITIZER
|
||||
#error "ABSL_HAVE_MEMORY_SANITIZER cannot be directly set."
|
||||
#elif defined(__SANITIZE_MEMORY__)
|
||||
#define ABSL_HAVE_MEMORY_SANITIZER 1
|
||||
#elif !defined(__native_client__) && ABSL_HAVE_FEATURE(memory_sanitizer)
|
||||
#define ABSL_HAVE_MEMORY_SANITIZER 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_THREAD_SANITIZER
|
||||
//
|
||||
// ThreadSanitizer (TSan) is a fast data race detector.
|
||||
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
||||
#error "ABSL_HAVE_THREAD_SANITIZER cannot be directly set."
|
||||
#elif defined(__SANITIZE_THREAD__)
|
||||
#define ABSL_HAVE_THREAD_SANITIZER 1
|
||||
#elif ABSL_HAVE_FEATURE(thread_sanitizer)
|
||||
#define ABSL_HAVE_THREAD_SANITIZER 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_ADDRESS_SANITIZER
|
||||
//
|
||||
// AddressSanitizer (ASan) is a fast memory error detector.
|
||||
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
|
||||
#error "ABSL_HAVE_ADDRESS_SANITIZER cannot be directly set."
|
||||
#elif defined(__SANITIZE_ADDRESS__)
|
||||
#define ABSL_HAVE_ADDRESS_SANITIZER 1
|
||||
#elif ABSL_HAVE_FEATURE(address_sanitizer)
|
||||
#define ABSL_HAVE_ADDRESS_SANITIZER 1
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
|
||||
//
|
||||
// Class template argument deduction is a language feature added in C++17.
|
||||
#ifdef ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
|
||||
#error "ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION cannot be directly set."
|
||||
#elif defined(__cpp_deduction_guides)
|
||||
#define ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION 1
|
||||
#endif
|
||||
|
||||
#endif // ABSL_BASE_CONFIG_H_
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright 2017 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/base/config.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/synchronization/internal/thread_pool.h"
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(ConfigTest, Endianness) {
|
||||
union {
|
||||
uint32_t value;
|
||||
uint8_t data[sizeof(uint32_t)];
|
||||
} number;
|
||||
number.data[0] = 0x00;
|
||||
number.data[1] = 0x01;
|
||||
number.data[2] = 0x02;
|
||||
number.data[3] = 0x03;
|
||||
#if defined(ABSL_IS_LITTLE_ENDIAN) && defined(ABSL_IS_BIG_ENDIAN)
|
||||
#error Both ABSL_IS_LITTLE_ENDIAN and ABSL_IS_BIG_ENDIAN are defined
|
||||
#elif defined(ABSL_IS_LITTLE_ENDIAN)
|
||||
EXPECT_EQ(UINT32_C(0x03020100), number.value);
|
||||
#elif defined(ABSL_IS_BIG_ENDIAN)
|
||||
EXPECT_EQ(UINT32_C(0x00010203), number.value);
|
||||
#else
|
||||
#error Unknown endianness
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(ABSL_HAVE_THREAD_LOCAL)
|
||||
TEST(ConfigTest, ThreadLocal) {
|
||||
static thread_local int mine_mine_mine = 16;
|
||||
EXPECT_EQ(16, mine_mine_mine);
|
||||
{
|
||||
absl::synchronization_internal::ThreadPool pool(1);
|
||||
pool.Schedule([&] {
|
||||
EXPECT_EQ(16, mine_mine_mine);
|
||||
mine_mine_mine = 32;
|
||||
EXPECT_EQ(32, mine_mine_mine);
|
||||
});
|
||||
}
|
||||
EXPECT_EQ(16, mine_mine_mine);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
|
@ -0,0 +1,76 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------------
|
||||
// kConstInit
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// A constructor tag used to mark an object as safe for use as a global
|
||||
// variable, avoiding the usual lifetime issues that can affect globals.
|
||||
|
||||
#ifndef ABSL_BASE_CONST_INIT_H_
|
||||
#define ABSL_BASE_CONST_INIT_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
// In general, objects with static storage duration (such as global variables)
|
||||
// can trigger tricky object lifetime situations. Attempting to access them
|
||||
// from the constructors or destructors of other global objects can result in
|
||||
// undefined behavior, unless their constructors and destructors are designed
|
||||
// with this issue in mind.
|
||||
//
|
||||
// The normal way to deal with this issue in C++11 is to use constant
|
||||
// initialization and trivial destructors.
|
||||
//
|
||||
// Constant initialization is guaranteed to occur before any other code
|
||||
// executes. Constructors that are declared 'constexpr' are eligible for
|
||||
// constant initialization. You can annotate a variable declaration with the
|
||||
// ABSL_CONST_INIT macro to express this intent. For compilers that support
|
||||
// it, this annotation will cause a compilation error for declarations that
|
||||
// aren't subject to constant initialization (perhaps because a runtime value
|
||||
// was passed as a constructor argument).
|
||||
//
|
||||
// On program shutdown, lifetime issues can be avoided on global objects by
|
||||
// ensuring that they contain trivial destructors. A class has a trivial
|
||||
// destructor unless it has a user-defined destructor, a virtual method or base
|
||||
// class, or a data member or base class with a non-trivial destructor of its
|
||||
// own. Objects with static storage duration and a trivial destructor are not
|
||||
// cleaned up on program shutdown, and are thus safe to access from other code
|
||||
// running during shutdown.
|
||||
//
|
||||
// For a few core Abseil classes, we make a best effort to allow for safe global
|
||||
// instances, even though these classes have non-trivial destructors. These
|
||||
// objects can be created with the absl::kConstInit tag. For example:
|
||||
// ABSL_CONST_INIT absl::Mutex global_mutex(absl::kConstInit);
|
||||
//
|
||||
// The line above declares a global variable of type absl::Mutex which can be
|
||||
// accessed at any point during startup or shutdown. global_mutex's destructor
|
||||
// will still run, but will not invalidate the object. Note that C++ specifies
|
||||
// that accessing an object after its destructor has run results in undefined
|
||||
// behavior, but this pattern works on the toolchains we support.
|
||||
//
|
||||
// The absl::kConstInit tag should only be used to define objects with static
|
||||
// or thread_local storage duration.
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
enum ConstInitType {
|
||||
kConstInit,
|
||||
};
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_CONST_INIT_H_
|
|
@ -0,0 +1,493 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// This file defines dynamic annotations for use with dynamic analysis tool
|
||||
// such as valgrind, PIN, etc.
|
||||
//
|
||||
// Dynamic annotation is a source code annotation that affects the generated
|
||||
// code (that is, the annotation is not a comment). Each such annotation is
|
||||
// attached to a particular instruction and/or to a particular object (address)
|
||||
// in the program.
|
||||
//
|
||||
// The annotations that should be used by users are macros in all upper-case
|
||||
// (e.g., ABSL_ANNOTATE_THREAD_NAME).
|
||||
//
|
||||
// Actual implementation of these macros may differ depending on the dynamic
|
||||
// analysis tool being used.
|
||||
//
|
||||
// This file supports the following configurations:
|
||||
// - Dynamic Annotations enabled (with static thread-safety warnings disabled).
|
||||
// In this case, macros expand to functions implemented by Thread Sanitizer,
|
||||
// when building with TSan. When not provided an external implementation,
|
||||
// dynamic_annotations.cc provides no-op implementations.
|
||||
//
|
||||
// - Static Clang thread-safety warnings enabled.
|
||||
// When building with a Clang compiler that supports thread-safety warnings,
|
||||
// a subset of annotations can be statically-checked at compile-time. We
|
||||
// expand these macros to static-inline functions that can be analyzed for
|
||||
// thread-safety, but afterwards elided when building the final binary.
|
||||
//
|
||||
// - All annotations are disabled.
|
||||
// If neither Dynamic Annotations nor Clang thread-safety warnings are
|
||||
// enabled, then all annotation-macros expand to empty.
|
||||
|
||||
#ifndef ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
|
||||
#define ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
#ifdef __cplusplus
|
||||
#include "absl/base/macros.h"
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Decide which features are enabled.
|
||||
|
||||
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
||||
|
||||
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 1
|
||||
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 1
|
||||
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 1
|
||||
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
|
||||
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED 1
|
||||
|
||||
#else
|
||||
|
||||
#define ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED 0
|
||||
#define ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED 0
|
||||
#define ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED 0
|
||||
|
||||
// Clang provides limited support for static thread-safety analysis through a
|
||||
// feature called Annotalysis. We configure macro-definitions according to
|
||||
// whether Annotalysis support is available. When running in opt-mode, GCC
|
||||
// will issue a warning, if these attributes are compiled. Only include them
|
||||
// when compiling using Clang.
|
||||
|
||||
#if defined(__clang__)
|
||||
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 1
|
||||
#if !defined(SWIG)
|
||||
#define ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED 1
|
||||
#endif
|
||||
#else
|
||||
#define ABSL_INTERNAL_ANNOTALYSIS_ENABLED 0
|
||||
#endif
|
||||
|
||||
// Read/write annotations are enabled in Annotalysis mode; disabled otherwise.
|
||||
#define ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED \
|
||||
ABSL_INTERNAL_ANNOTALYSIS_ENABLED
|
||||
|
||||
#endif // ABSL_HAVE_THREAD_SANITIZER
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define ABSL_INTERNAL_BEGIN_EXTERN_C extern "C" {
|
||||
#define ABSL_INTERNAL_END_EXTERN_C } // extern "C"
|
||||
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) ::F
|
||||
#define ABSL_INTERNAL_STATIC_INLINE inline
|
||||
#else
|
||||
#define ABSL_INTERNAL_BEGIN_EXTERN_C // empty
|
||||
#define ABSL_INTERNAL_END_EXTERN_C // empty
|
||||
#define ABSL_INTERNAL_GLOBAL_SCOPED(F) F
|
||||
#define ABSL_INTERNAL_STATIC_INLINE static inline
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Define race annotations.
|
||||
|
||||
#if ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 1
|
||||
// Some of the symbols used in this section (e.g. AnnotateBenignRaceSized) are
|
||||
// defined by the compiler-based santizer implementation, not by the Abseil
|
||||
// library. Therefore they do not use ABSL_INTERNAL_C_SYMBOL.
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Annotations that suppress errors. It is usually better to express the
|
||||
// program's synchronization using the other annotations, but these can be used
|
||||
// when all else fails.
|
||||
|
||||
// Report that we may have a benign race at `pointer`, with size
|
||||
// "sizeof(*(pointer))". `pointer` must be a non-void* pointer. Insert at the
|
||||
// point where `pointer` has been allocated, preferably close to the point
|
||||
// where the race happens. See also ABSL_ANNOTATE_BENIGN_RACE_STATIC.
|
||||
#define ABSL_ANNOTATE_BENIGN_RACE(pointer, description) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
|
||||
(__FILE__, __LINE__, pointer, sizeof(*(pointer)), description)
|
||||
|
||||
// Same as ABSL_ANNOTATE_BENIGN_RACE(`address`, `description`), but applies to
|
||||
// the memory range [`address`, `address`+`size`).
|
||||
#define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateBenignRaceSized) \
|
||||
(__FILE__, __LINE__, address, size, description)
|
||||
|
||||
// Enable (`enable`!=0) or disable (`enable`==0) race detection for all threads.
|
||||
// This annotation could be useful if you want to skip expensive race analysis
|
||||
// during some period of program execution, e.g. during initialization.
|
||||
#define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateEnableRaceDetection) \
|
||||
(__FILE__, __LINE__, enable)
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Annotations useful for debugging.
|
||||
|
||||
// Report the current thread `name` to a race detector.
|
||||
#define ABSL_ANNOTATE_THREAD_NAME(name) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateThreadName)(__FILE__, __LINE__, name)
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Annotations useful when implementing locks. They are not normally needed by
|
||||
// modules that merely use locks. The `lock` argument is a pointer to the lock
|
||||
// object.
|
||||
|
||||
// Report that a lock has been created at address `lock`.
|
||||
#define ABSL_ANNOTATE_RWLOCK_CREATE(lock) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
|
||||
|
||||
// Report that a linker initialized lock has been created at address `lock`.
|
||||
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
||||
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockCreateStatic) \
|
||||
(__FILE__, __LINE__, lock)
|
||||
#else
|
||||
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) \
|
||||
ABSL_ANNOTATE_RWLOCK_CREATE(lock)
|
||||
#endif
|
||||
|
||||
// Report that the lock at address `lock` is about to be destroyed.
|
||||
#define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
|
||||
|
||||
// Report that the lock at address `lock` has been acquired.
|
||||
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
|
||||
#define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockAcquired) \
|
||||
(__FILE__, __LINE__, lock, is_w)
|
||||
|
||||
// Report that the lock at address `lock` is about to be released.
|
||||
// `is_w`=1 for writer lock, `is_w`=0 for reader lock.
|
||||
#define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateRWLockReleased) \
|
||||
(__FILE__, __LINE__, lock, is_w)
|
||||
|
||||
// Apply ABSL_ANNOTATE_BENIGN_RACE_SIZED to a static variable `static_var`.
|
||||
#define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
|
||||
namespace { \
|
||||
class static_var##_annotator { \
|
||||
public: \
|
||||
static_var##_annotator() { \
|
||||
ABSL_ANNOTATE_BENIGN_RACE_SIZED(&static_var, sizeof(static_var), \
|
||||
#static_var ": " description); \
|
||||
} \
|
||||
}; \
|
||||
static static_var##_annotator the##static_var##_annotator; \
|
||||
} // namespace
|
||||
|
||||
// Function prototypes of annotations provided by the compiler-based sanitizer
|
||||
// implementation.
|
||||
ABSL_INTERNAL_BEGIN_EXTERN_C
|
||||
void AnnotateRWLockCreate(const char* file, int line,
|
||||
const volatile void* lock);
|
||||
void AnnotateRWLockCreateStatic(const char* file, int line,
|
||||
const volatile void* lock);
|
||||
void AnnotateRWLockDestroy(const char* file, int line,
|
||||
const volatile void* lock);
|
||||
void AnnotateRWLockAcquired(const char* file, int line,
|
||||
const volatile void* lock, long is_w); // NOLINT
|
||||
void AnnotateRWLockReleased(const char* file, int line,
|
||||
const volatile void* lock, long is_w); // NOLINT
|
||||
void AnnotateBenignRace(const char* file, int line,
|
||||
const volatile void* address, const char* description);
|
||||
void AnnotateBenignRaceSized(const char* file, int line,
|
||||
const volatile void* address, size_t size,
|
||||
const char* description);
|
||||
void AnnotateThreadName(const char* file, int line, const char* name);
|
||||
void AnnotateEnableRaceDetection(const char* file, int line, int enable);
|
||||
ABSL_INTERNAL_END_EXTERN_C
|
||||
|
||||
#else // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED == 0
|
||||
|
||||
#define ABSL_ANNOTATE_RWLOCK_CREATE(lock) // empty
|
||||
#define ABSL_ANNOTATE_RWLOCK_CREATE_STATIC(lock) // empty
|
||||
#define ABSL_ANNOTATE_RWLOCK_DESTROY(lock) // empty
|
||||
#define ABSL_ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) // empty
|
||||
#define ABSL_ANNOTATE_RWLOCK_RELEASED(lock, is_w) // empty
|
||||
#define ABSL_ANNOTATE_BENIGN_RACE(address, description) // empty
|
||||
#define ABSL_ANNOTATE_BENIGN_RACE_SIZED(address, size, description) // empty
|
||||
#define ABSL_ANNOTATE_THREAD_NAME(name) // empty
|
||||
#define ABSL_ANNOTATE_ENABLE_RACE_DETECTION(enable) // empty
|
||||
#define ABSL_ANNOTATE_BENIGN_RACE_STATIC(static_var, description) // empty
|
||||
|
||||
#endif // ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Define memory annotations.
|
||||
|
||||
#ifdef ABSL_HAVE_MEMORY_SANITIZER
|
||||
|
||||
#include <sanitizer/msan_interface.h>
|
||||
|
||||
#define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
|
||||
__msan_unpoison(address, size)
|
||||
|
||||
#define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
|
||||
__msan_allocated_memory(address, size)
|
||||
|
||||
#else // !defined(ABSL_HAVE_MEMORY_SANITIZER)
|
||||
|
||||
// TODO(rogeeff): remove this branch
|
||||
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
||||
#define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
|
||||
do { \
|
||||
(void)(address); \
|
||||
(void)(size); \
|
||||
} while (0)
|
||||
#define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) \
|
||||
do { \
|
||||
(void)(address); \
|
||||
(void)(size); \
|
||||
} while (0)
|
||||
#else
|
||||
|
||||
#define ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) // empty
|
||||
#define ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(address, size) // empty
|
||||
|
||||
#endif
|
||||
|
||||
#endif // ABSL_HAVE_MEMORY_SANITIZER
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Define IGNORE_READS_BEGIN/_END attributes.
|
||||
|
||||
#if defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
||||
|
||||
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE \
|
||||
__attribute((exclusive_lock_function("*")))
|
||||
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE \
|
||||
__attribute((unlock_function("*")))
|
||||
|
||||
#else // !defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
||||
|
||||
#define ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE // empty
|
||||
#define ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE // empty
|
||||
|
||||
#endif // defined(ABSL_INTERNAL_IGNORE_READS_ATTRIBUTE_ENABLED)
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Define IGNORE_READS_BEGIN/_END annotations.
|
||||
|
||||
#if ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED == 1
|
||||
// Some of the symbols used in this section (e.g. AnnotateIgnoreReadsBegin) are
|
||||
// defined by the compiler-based implementation, not by the Abseil
|
||||
// library. Therefore they do not use ABSL_INTERNAL_C_SYMBOL.
|
||||
|
||||
// Request the analysis tool to ignore all reads in the current thread until
|
||||
// ABSL_ANNOTATE_IGNORE_READS_END is called. Useful to ignore intentional racey
|
||||
// reads, while still checking other reads and all writes.
|
||||
// See also ABSL_ANNOTATE_UNPROTECTED_READ.
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsBegin) \
|
||||
(__FILE__, __LINE__)
|
||||
|
||||
// Stop ignoring reads.
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_END() \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreReadsEnd) \
|
||||
(__FILE__, __LINE__)
|
||||
|
||||
// Function prototypes of annotations provided by the compiler-based sanitizer
|
||||
// implementation.
|
||||
ABSL_INTERNAL_BEGIN_EXTERN_C
|
||||
void AnnotateIgnoreReadsBegin(const char* file, int line)
|
||||
ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE;
|
||||
void AnnotateIgnoreReadsEnd(const char* file,
|
||||
int line) ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE;
|
||||
ABSL_INTERNAL_END_EXTERN_C
|
||||
|
||||
#elif defined(ABSL_INTERNAL_ANNOTALYSIS_ENABLED)
|
||||
|
||||
// When Annotalysis is enabled without Dynamic Annotations, the use of
|
||||
// static-inline functions allows the annotations to be read at compile-time,
|
||||
// while still letting the compiler elide the functions from the final build.
|
||||
//
|
||||
// TODO(delesley) -- The exclusive lock here ignores writes as well, but
|
||||
// allows IGNORE_READS_AND_WRITES to work properly.
|
||||
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_BEGIN() \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED( \
|
||||
ABSL_INTERNAL_C_SYMBOL(AbslInternalAnnotateIgnoreReadsBegin)) \
|
||||
()
|
||||
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_END() \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED( \
|
||||
ABSL_INTERNAL_C_SYMBOL(AbslInternalAnnotateIgnoreReadsEnd)) \
|
||||
()
|
||||
|
||||
ABSL_INTERNAL_STATIC_INLINE void ABSL_INTERNAL_C_SYMBOL(
|
||||
AbslInternalAnnotateIgnoreReadsBegin)()
|
||||
ABSL_INTERNAL_IGNORE_READS_BEGIN_ATTRIBUTE {}
|
||||
|
||||
ABSL_INTERNAL_STATIC_INLINE void ABSL_INTERNAL_C_SYMBOL(
|
||||
AbslInternalAnnotateIgnoreReadsEnd)()
|
||||
ABSL_INTERNAL_IGNORE_READS_END_ATTRIBUTE {}
|
||||
|
||||
#else
|
||||
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_BEGIN() // empty
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_END() // empty
|
||||
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Define IGNORE_WRITES_BEGIN/_END annotations.
|
||||
|
||||
#if ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED == 1
|
||||
|
||||
// Similar to ABSL_ANNOTATE_IGNORE_READS_BEGIN, but ignore writes instead.
|
||||
#define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
|
||||
|
||||
// Stop ignoring writes.
|
||||
#define ABSL_ANNOTATE_IGNORE_WRITES_END() \
|
||||
ABSL_INTERNAL_GLOBAL_SCOPED(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
|
||||
|
||||
// Function prototypes of annotations provided by the compiler-based sanitizer
|
||||
// implementation.
|
||||
ABSL_INTERNAL_BEGIN_EXTERN_C
|
||||
void AnnotateIgnoreWritesBegin(const char* file, int line);
|
||||
void AnnotateIgnoreWritesEnd(const char* file, int line);
|
||||
ABSL_INTERNAL_END_EXTERN_C
|
||||
|
||||
#else
|
||||
|
||||
#define ABSL_ANNOTATE_IGNORE_WRITES_BEGIN() // empty
|
||||
#define ABSL_ANNOTATE_IGNORE_WRITES_END() // empty
|
||||
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Define the ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_* annotations using the more
|
||||
// primitive annotations defined above.
|
||||
//
|
||||
// Instead of doing
|
||||
// ABSL_ANNOTATE_IGNORE_READS_BEGIN();
|
||||
// ... = x;
|
||||
// ABSL_ANNOTATE_IGNORE_READS_END();
|
||||
// one can use
|
||||
// ... = ABSL_ANNOTATE_UNPROTECTED_READ(x);
|
||||
|
||||
#if defined(ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED)
|
||||
|
||||
// Start ignoring all memory accesses (both reads and writes).
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
|
||||
do { \
|
||||
ABSL_ANNOTATE_IGNORE_READS_BEGIN(); \
|
||||
ABSL_ANNOTATE_IGNORE_WRITES_BEGIN(); \
|
||||
} while (0)
|
||||
|
||||
// Stop ignoring both reads and writes.
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() \
|
||||
do { \
|
||||
ABSL_ANNOTATE_IGNORE_WRITES_END(); \
|
||||
ABSL_ANNOTATE_IGNORE_READS_END(); \
|
||||
} while (0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
// ABSL_ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
|
||||
#define ABSL_ANNOTATE_UNPROTECTED_READ(x) \
|
||||
absl::base_internal::AnnotateUnprotectedRead(x)
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
template <typename T>
|
||||
inline T AnnotateUnprotectedRead(const volatile T& x) { // NOLINT
|
||||
ABSL_ANNOTATE_IGNORE_READS_BEGIN();
|
||||
T res = x;
|
||||
ABSL_ANNOTATE_IGNORE_READS_END();
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() // empty
|
||||
#define ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END() // empty
|
||||
#define ABSL_ANNOTATE_UNPROTECTED_READ(x) (x)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
#ifdef ABSL_HAVE_THREAD_SANITIZER
|
||||
ABSL_INTERNAL_BEGIN_EXTERN_C
|
||||
int RunningOnValgrind();
|
||||
double ValgrindSlowdown();
|
||||
ABSL_INTERNAL_END_EXTERN_C
|
||||
#else
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
ABSL_DEPRECATED(
|
||||
"Don't use this interface. It is misleading and is being deleted.")
|
||||
ABSL_ATTRIBUTE_ALWAYS_INLINE inline int RunningOnValgrind() { return 0; }
|
||||
ABSL_DEPRECATED(
|
||||
"Don't use this interface. It is misleading and is being deleted.")
|
||||
ABSL_ATTRIBUTE_ALWAYS_INLINE inline double ValgrindSlowdown() { return 1.0; }
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
using absl::base_internal::RunningOnValgrind;
|
||||
using absl::base_internal::ValgrindSlowdown;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Address sanitizer annotations
|
||||
|
||||
#ifdef ABSL_HAVE_ADDRESS_SANITIZER
|
||||
// Describe the current state of a contiguous container such as e.g.
|
||||
// std::vector or std::string. For more details see
|
||||
// sanitizer/common_interface_defs.h, which is provided by the compiler.
|
||||
#include <sanitizer/common_interface_defs.h>
|
||||
|
||||
#define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) \
|
||||
__sanitizer_annotate_contiguous_container(beg, end, old_mid, new_mid)
|
||||
#define ABSL_ADDRESS_SANITIZER_REDZONE(name) \
|
||||
struct { \
|
||||
alignas(8) char x[8]; \
|
||||
} name
|
||||
|
||||
#else
|
||||
|
||||
#define ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(beg, end, old_mid, new_mid) // empty
|
||||
#define ABSL_ADDRESS_SANITIZER_REDZONE(name) static_assert(true, "")
|
||||
|
||||
#endif // ABSL_HAVE_ADDRESS_SANITIZER
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Undefine the macros intended only for this file.
|
||||
|
||||
#undef ABSL_INTERNAL_RACE_ANNOTATIONS_ENABLED
|
||||
#undef ABSL_INTERNAL_READS_ANNOTATIONS_ENABLED
|
||||
#undef ABSL_INTERNAL_WRITES_ANNOTATIONS_ENABLED
|
||||
#undef ABSL_INTERNAL_ANNOTALYSIS_ENABLED
|
||||
#undef ABSL_INTERNAL_READS_WRITES_ANNOTATIONS_ENABLED
|
||||
#undef ABSL_INTERNAL_BEGIN_EXTERN_C
|
||||
#undef ABSL_INTERNAL_END_EXTERN_C
|
||||
#undef ABSL_INTERNAL_STATIC_INLINE
|
||||
|
||||
#endif // ABSL_BASE_DYNAMIC_ANNOTATIONS_H_
|
|
@ -0,0 +1,956 @@
|
|||
// Copyright 2017 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/base/internal/exception_safety_testing.h"
|
||||
|
||||
#ifdef ABSL_HAVE_EXCEPTIONS
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest-spi.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/memory/memory.h"
|
||||
|
||||
namespace testing {
|
||||
|
||||
namespace {
|
||||
|
||||
using ::testing::exceptions_internal::SetCountdown;
|
||||
using ::testing::exceptions_internal::TestException;
|
||||
using ::testing::exceptions_internal::UnsetCountdown;
|
||||
|
||||
// EXPECT_NO_THROW can't inspect the thrown inspection in general.
|
||||
template <typename F>
|
||||
void ExpectNoThrow(const F& f) {
|
||||
try {
|
||||
f();
|
||||
} catch (const TestException& e) {
|
||||
ADD_FAILURE() << "Unexpected exception thrown from " << e.what();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, Throws) {
|
||||
SetCountdown();
|
||||
EXPECT_THROW(ThrowingValue<> bomb, TestException);
|
||||
|
||||
// It's not guaranteed that every operator only throws *once*. The default
|
||||
// ctor only throws once, though, so use it to make sure we only throw when
|
||||
// the countdown hits 0
|
||||
SetCountdown(2);
|
||||
ExpectNoThrow([]() { ThrowingValue<> bomb; });
|
||||
ExpectNoThrow([]() { ThrowingValue<> bomb; });
|
||||
EXPECT_THROW(ThrowingValue<> bomb, TestException);
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
// Tests that an operation throws when the countdown is at 0, doesn't throw when
|
||||
// the countdown doesn't hit 0, and doesn't modify the state of the
|
||||
// ThrowingValue if it throws
|
||||
template <typename F>
|
||||
void TestOp(const F& f) {
|
||||
ExpectNoThrow(f);
|
||||
|
||||
SetCountdown();
|
||||
EXPECT_THROW(f(), TestException);
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingCtors) {
|
||||
ThrowingValue<> bomb;
|
||||
|
||||
TestOp([]() { ThrowingValue<> bomb(1); });
|
||||
TestOp([&]() { ThrowingValue<> bomb1 = bomb; });
|
||||
TestOp([&]() { ThrowingValue<> bomb1 = std::move(bomb); });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingAssignment) {
|
||||
ThrowingValue<> bomb, bomb1;
|
||||
|
||||
TestOp([&]() { bomb = bomb1; });
|
||||
TestOp([&]() { bomb = std::move(bomb1); });
|
||||
|
||||
// Test that when assignment throws, the assignment should fail (lhs != rhs)
|
||||
// and strong guarantee fails (lhs != lhs_copy).
|
||||
{
|
||||
ThrowingValue<> lhs(39), rhs(42);
|
||||
ThrowingValue<> lhs_copy(lhs);
|
||||
SetCountdown();
|
||||
EXPECT_THROW(lhs = rhs, TestException);
|
||||
UnsetCountdown();
|
||||
EXPECT_NE(lhs, rhs);
|
||||
EXPECT_NE(lhs_copy, lhs);
|
||||
}
|
||||
{
|
||||
ThrowingValue<> lhs(39), rhs(42);
|
||||
ThrowingValue<> lhs_copy(lhs), rhs_copy(rhs);
|
||||
SetCountdown();
|
||||
EXPECT_THROW(lhs = std::move(rhs), TestException);
|
||||
UnsetCountdown();
|
||||
EXPECT_NE(lhs, rhs_copy);
|
||||
EXPECT_NE(lhs_copy, lhs);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingComparisons) {
|
||||
ThrowingValue<> bomb1, bomb2;
|
||||
TestOp([&]() { return bomb1 == bomb2; });
|
||||
TestOp([&]() { return bomb1 != bomb2; });
|
||||
TestOp([&]() { return bomb1 < bomb2; });
|
||||
TestOp([&]() { return bomb1 <= bomb2; });
|
||||
TestOp([&]() { return bomb1 > bomb2; });
|
||||
TestOp([&]() { return bomb1 >= bomb2; });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingArithmeticOps) {
|
||||
ThrowingValue<> bomb1(1), bomb2(2);
|
||||
|
||||
TestOp([&bomb1]() { +bomb1; });
|
||||
TestOp([&bomb1]() { -bomb1; });
|
||||
TestOp([&bomb1]() { ++bomb1; });
|
||||
TestOp([&bomb1]() { bomb1++; });
|
||||
TestOp([&bomb1]() { --bomb1; });
|
||||
TestOp([&bomb1]() { bomb1--; });
|
||||
|
||||
TestOp([&]() { bomb1 + bomb2; });
|
||||
TestOp([&]() { bomb1 - bomb2; });
|
||||
TestOp([&]() { bomb1* bomb2; });
|
||||
TestOp([&]() { bomb1 / bomb2; });
|
||||
TestOp([&]() { bomb1 << 1; });
|
||||
TestOp([&]() { bomb1 >> 1; });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingLogicalOps) {
|
||||
ThrowingValue<> bomb1, bomb2;
|
||||
|
||||
TestOp([&bomb1]() { !bomb1; });
|
||||
TestOp([&]() { bomb1&& bomb2; });
|
||||
TestOp([&]() { bomb1 || bomb2; });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingBitwiseOps) {
|
||||
ThrowingValue<> bomb1, bomb2;
|
||||
|
||||
TestOp([&bomb1]() { ~bomb1; });
|
||||
TestOp([&]() { bomb1& bomb2; });
|
||||
TestOp([&]() { bomb1 | bomb2; });
|
||||
TestOp([&]() { bomb1 ^ bomb2; });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingCompoundAssignmentOps) {
|
||||
ThrowingValue<> bomb1(1), bomb2(2);
|
||||
|
||||
TestOp([&]() { bomb1 += bomb2; });
|
||||
TestOp([&]() { bomb1 -= bomb2; });
|
||||
TestOp([&]() { bomb1 *= bomb2; });
|
||||
TestOp([&]() { bomb1 /= bomb2; });
|
||||
TestOp([&]() { bomb1 %= bomb2; });
|
||||
TestOp([&]() { bomb1 &= bomb2; });
|
||||
TestOp([&]() { bomb1 |= bomb2; });
|
||||
TestOp([&]() { bomb1 ^= bomb2; });
|
||||
TestOp([&]() { bomb1 *= bomb2; });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingStreamOps) {
|
||||
ThrowingValue<> bomb;
|
||||
|
||||
TestOp([&]() {
|
||||
std::istringstream stream;
|
||||
stream >> bomb;
|
||||
});
|
||||
TestOp([&]() {
|
||||
std::stringstream stream;
|
||||
stream << bomb;
|
||||
});
|
||||
}
|
||||
|
||||
// Tests the operator<< of ThrowingValue by forcing ConstructorTracker to emit
|
||||
// a nonfatal failure that contains the string representation of the Thrower
|
||||
TEST(ThrowingValueTest, StreamOpsOutput) {
|
||||
using ::testing::TypeSpec;
|
||||
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
||||
|
||||
// Test default spec list (kEverythingThrows)
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
{
|
||||
using Thrower = ThrowingValue<TypeSpec{}>;
|
||||
auto thrower = Thrower(123);
|
||||
thrower.~Thrower();
|
||||
},
|
||||
"ThrowingValue<>(123)");
|
||||
|
||||
// Test with one item in spec list (kNoThrowCopy)
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
{
|
||||
using Thrower = ThrowingValue<TypeSpec::kNoThrowCopy>;
|
||||
auto thrower = Thrower(234);
|
||||
thrower.~Thrower();
|
||||
},
|
||||
"ThrowingValue<kNoThrowCopy>(234)");
|
||||
|
||||
// Test with multiple items in spec list (kNoThrowMove, kNoThrowNew)
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
{
|
||||
using Thrower =
|
||||
ThrowingValue<TypeSpec::kNoThrowMove | TypeSpec::kNoThrowNew>;
|
||||
auto thrower = Thrower(345);
|
||||
thrower.~Thrower();
|
||||
},
|
||||
"ThrowingValue<kNoThrowMove | kNoThrowNew>(345)");
|
||||
|
||||
// Test with all items in spec list (kNoThrowCopy, kNoThrowMove, kNoThrowNew)
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
{
|
||||
using Thrower = ThrowingValue<static_cast<TypeSpec>(-1)>;
|
||||
auto thrower = Thrower(456);
|
||||
thrower.~Thrower();
|
||||
},
|
||||
"ThrowingValue<kNoThrowCopy | kNoThrowMove | kNoThrowNew>(456)");
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
void TestAllocatingOp(const F& f) {
|
||||
ExpectNoThrow(f);
|
||||
|
||||
SetCountdown();
|
||||
EXPECT_THROW(f(), exceptions_internal::TestBadAllocException);
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingAllocatingOps) {
|
||||
// make_unique calls unqualified operator new, so these exercise the
|
||||
// ThrowingValue overloads.
|
||||
TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>>(1); });
|
||||
TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>[]>(2); });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingMoveCtor) {
|
||||
ThrowingValue<TypeSpec::kNoThrowMove> nothrow_ctor;
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([¬hrow_ctor]() {
|
||||
ThrowingValue<TypeSpec::kNoThrowMove> nothrow1 = std::move(nothrow_ctor);
|
||||
});
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingMoveAssign) {
|
||||
ThrowingValue<TypeSpec::kNoThrowMove> nothrow_assign1, nothrow_assign2;
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([¬hrow_assign1, ¬hrow_assign2]() {
|
||||
nothrow_assign1 = std::move(nothrow_assign2);
|
||||
});
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingCopyCtor) {
|
||||
ThrowingValue<> tv;
|
||||
|
||||
TestOp([&]() { ThrowingValue<> tv_copy(tv); });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingCopyAssign) {
|
||||
ThrowingValue<> tv1, tv2;
|
||||
|
||||
TestOp([&]() { tv1 = tv2; });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingCopyCtor) {
|
||||
ThrowingValue<TypeSpec::kNoThrowCopy> nothrow_ctor;
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([¬hrow_ctor]() {
|
||||
ThrowingValue<TypeSpec::kNoThrowCopy> nothrow1(nothrow_ctor);
|
||||
});
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingCopyAssign) {
|
||||
ThrowingValue<TypeSpec::kNoThrowCopy> nothrow_assign1, nothrow_assign2;
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([¬hrow_assign1, ¬hrow_assign2]() {
|
||||
nothrow_assign1 = nothrow_assign2;
|
||||
});
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, ThrowingSwap) {
|
||||
ThrowingValue<> bomb1, bomb2;
|
||||
TestOp([&]() { std::swap(bomb1, bomb2); });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingSwap) {
|
||||
ThrowingValue<TypeSpec::kNoThrowMove> bomb1, bomb2;
|
||||
ExpectNoThrow([&]() { std::swap(bomb1, bomb2); });
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingAllocation) {
|
||||
ThrowingValue<TypeSpec::kNoThrowNew>* allocated;
|
||||
ThrowingValue<TypeSpec::kNoThrowNew>* array;
|
||||
|
||||
ExpectNoThrow([&allocated]() {
|
||||
allocated = new ThrowingValue<TypeSpec::kNoThrowNew>(1);
|
||||
delete allocated;
|
||||
});
|
||||
ExpectNoThrow([&array]() {
|
||||
array = new ThrowingValue<TypeSpec::kNoThrowNew>[2];
|
||||
delete[] array;
|
||||
});
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingDelete) {
|
||||
auto* allocated = new ThrowingValue<>(1);
|
||||
auto* array = new ThrowingValue<>[2];
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([allocated]() { delete allocated; });
|
||||
SetCountdown();
|
||||
ExpectNoThrow([array]() { delete[] array; });
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingPlacementDelete) {
|
||||
constexpr int kArrayLen = 2;
|
||||
// We intentionally create extra space to store the tag allocated by placement
|
||||
// new[].
|
||||
constexpr int kStorageLen = 4;
|
||||
|
||||
alignas(ThrowingValue<>) unsigned char buf[sizeof(ThrowingValue<>)];
|
||||
alignas(ThrowingValue<>) unsigned char
|
||||
array_buf[sizeof(ThrowingValue<>[kStorageLen])];
|
||||
auto* placed = new (&buf) ThrowingValue<>(1);
|
||||
auto placed_array = new (&array_buf) ThrowingValue<>[kArrayLen];
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([placed, &buf]() {
|
||||
placed->~ThrowingValue<>();
|
||||
ThrowingValue<>::operator delete(placed, &buf);
|
||||
});
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([&, placed_array]() {
|
||||
for (int i = 0; i < kArrayLen; ++i) placed_array[i].~ThrowingValue<>();
|
||||
ThrowingValue<>::operator delete[](placed_array, &array_buf);
|
||||
});
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTest, NonThrowingDestructor) {
|
||||
auto* allocated = new ThrowingValue<>();
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([allocated]() { delete allocated; });
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingBoolTest, ThrowingBool) {
|
||||
ThrowingBool t = true;
|
||||
|
||||
// Test that it's contextually convertible to bool
|
||||
if (t) { // NOLINT(whitespace/empty_if_body)
|
||||
}
|
||||
EXPECT_TRUE(t);
|
||||
|
||||
TestOp([&]() { (void)!t; });
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, MemoryManagement) {
|
||||
// Just exercise the memory management capabilities under LSan to make sure we
|
||||
// don't leak.
|
||||
ThrowingAllocator<int> int_alloc;
|
||||
int* ip = int_alloc.allocate(1);
|
||||
int_alloc.deallocate(ip, 1);
|
||||
int* i_array = int_alloc.allocate(2);
|
||||
int_alloc.deallocate(i_array, 2);
|
||||
|
||||
ThrowingAllocator<ThrowingValue<>> tv_alloc;
|
||||
ThrowingValue<>* ptr = tv_alloc.allocate(1);
|
||||
tv_alloc.deallocate(ptr, 1);
|
||||
ThrowingValue<>* tv_array = tv_alloc.allocate(2);
|
||||
tv_alloc.deallocate(tv_array, 2);
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, CallsGlobalNew) {
|
||||
ThrowingAllocator<ThrowingValue<>, AllocSpec::kNoThrowAllocate> nothrow_alloc;
|
||||
ThrowingValue<>* ptr;
|
||||
|
||||
SetCountdown();
|
||||
// This will only throw if ThrowingValue::new is called.
|
||||
ExpectNoThrow([&]() { ptr = nothrow_alloc.allocate(1); });
|
||||
nothrow_alloc.deallocate(ptr, 1);
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, ThrowingConstructors) {
|
||||
ThrowingAllocator<int> int_alloc;
|
||||
int* ip = nullptr;
|
||||
|
||||
SetCountdown();
|
||||
EXPECT_THROW(ip = int_alloc.allocate(1), TestException);
|
||||
ExpectNoThrow([&]() { ip = int_alloc.allocate(1); });
|
||||
|
||||
*ip = 1;
|
||||
SetCountdown();
|
||||
EXPECT_THROW(int_alloc.construct(ip, 2), TestException);
|
||||
EXPECT_EQ(*ip, 1);
|
||||
int_alloc.deallocate(ip, 1);
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, NonThrowingConstruction) {
|
||||
{
|
||||
ThrowingAllocator<int, AllocSpec::kNoThrowAllocate> int_alloc;
|
||||
int* ip = nullptr;
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([&]() { ip = int_alloc.allocate(1); });
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([&]() { int_alloc.construct(ip, 2); });
|
||||
|
||||
EXPECT_EQ(*ip, 2);
|
||||
int_alloc.deallocate(ip, 1);
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
{
|
||||
ThrowingAllocator<int> int_alloc;
|
||||
int* ip = nullptr;
|
||||
ExpectNoThrow([&]() { ip = int_alloc.allocate(1); });
|
||||
ExpectNoThrow([&]() { int_alloc.construct(ip, 2); });
|
||||
EXPECT_EQ(*ip, 2);
|
||||
int_alloc.deallocate(ip, 1);
|
||||
}
|
||||
|
||||
{
|
||||
ThrowingAllocator<ThrowingValue<>, AllocSpec::kNoThrowAllocate>
|
||||
nothrow_alloc;
|
||||
ThrowingValue<>* ptr;
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([&]() { ptr = nothrow_alloc.allocate(1); });
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow(
|
||||
[&]() { nothrow_alloc.construct(ptr, 2, testing::nothrow_ctor); });
|
||||
|
||||
EXPECT_EQ(ptr->Get(), 2);
|
||||
nothrow_alloc.destroy(ptr);
|
||||
nothrow_alloc.deallocate(ptr, 1);
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
|
||||
{
|
||||
ThrowingAllocator<int> a;
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([&]() { ThrowingAllocator<double> a1 = a; });
|
||||
|
||||
SetCountdown();
|
||||
ExpectNoThrow([&]() { ThrowingAllocator<double> a1 = std::move(a); });
|
||||
|
||||
UnsetCountdown();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, ThrowingAllocatorConstruction) {
|
||||
ThrowingAllocator<int> a;
|
||||
TestOp([]() { ThrowingAllocator<int> a; });
|
||||
TestOp([&]() { a.select_on_container_copy_construction(); });
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, State) {
|
||||
ThrowingAllocator<int> a1, a2;
|
||||
EXPECT_NE(a1, a2);
|
||||
|
||||
auto a3 = a1;
|
||||
EXPECT_EQ(a3, a1);
|
||||
int* ip = a1.allocate(1);
|
||||
EXPECT_EQ(a3, a1);
|
||||
a3.deallocate(ip, 1);
|
||||
EXPECT_EQ(a3, a1);
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, InVector) {
|
||||
std::vector<ThrowingValue<>, ThrowingAllocator<ThrowingValue<>>> v;
|
||||
for (int i = 0; i < 20; ++i) v.push_back({});
|
||||
for (int i = 0; i < 20; ++i) v.pop_back();
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTest, InList) {
|
||||
std::list<ThrowingValue<>, ThrowingAllocator<ThrowingValue<>>> l;
|
||||
for (int i = 0; i < 20; ++i) l.push_back({});
|
||||
for (int i = 0; i < 20; ++i) l.pop_back();
|
||||
for (int i = 0; i < 20; ++i) l.push_front({});
|
||||
for (int i = 0; i < 20; ++i) l.pop_front();
|
||||
}
|
||||
|
||||
template <typename TesterInstance, typename = void>
|
||||
struct NullaryTestValidator : public std::false_type {};
|
||||
|
||||
template <typename TesterInstance>
|
||||
struct NullaryTestValidator<
|
||||
TesterInstance,
|
||||
absl::void_t<decltype(std::declval<TesterInstance>().Test())>>
|
||||
: public std::true_type {};
|
||||
|
||||
template <typename TesterInstance>
|
||||
bool HasNullaryTest(const TesterInstance&) {
|
||||
return NullaryTestValidator<TesterInstance>::value;
|
||||
}
|
||||
|
||||
void DummyOp(void*) {}
|
||||
|
||||
template <typename TesterInstance, typename = void>
|
||||
struct UnaryTestValidator : public std::false_type {};
|
||||
|
||||
template <typename TesterInstance>
|
||||
struct UnaryTestValidator<
|
||||
TesterInstance,
|
||||
absl::void_t<decltype(std::declval<TesterInstance>().Test(DummyOp))>>
|
||||
: public std::true_type {};
|
||||
|
||||
template <typename TesterInstance>
|
||||
bool HasUnaryTest(const TesterInstance&) {
|
||||
return UnaryTestValidator<TesterInstance>::value;
|
||||
}
|
||||
|
||||
TEST(ExceptionSafetyTesterTest, IncompleteTypesAreNotTestable) {
|
||||
using T = exceptions_internal::UninitializedT;
|
||||
auto op = [](T* t) {};
|
||||
auto inv = [](T*) { return testing::AssertionSuccess(); };
|
||||
auto fac = []() { return absl::make_unique<T>(); };
|
||||
|
||||
// Test that providing operation and inveriants still does not allow for the
|
||||
// the invocation of .Test() and .Test(op) because it lacks a factory
|
||||
auto without_fac =
|
||||
testing::MakeExceptionSafetyTester().WithOperation(op).WithContracts(
|
||||
inv, testing::strong_guarantee);
|
||||
EXPECT_FALSE(HasNullaryTest(without_fac));
|
||||
EXPECT_FALSE(HasUnaryTest(without_fac));
|
||||
|
||||
// Test that providing contracts and factory allows the invocation of
|
||||
// .Test(op) but does not allow for .Test() because it lacks an operation
|
||||
auto without_op = testing::MakeExceptionSafetyTester()
|
||||
.WithContracts(inv, testing::strong_guarantee)
|
||||
.WithFactory(fac);
|
||||
EXPECT_FALSE(HasNullaryTest(without_op));
|
||||
EXPECT_TRUE(HasUnaryTest(without_op));
|
||||
|
||||
// Test that providing operation and factory still does not allow for the
|
||||
// the invocation of .Test() and .Test(op) because it lacks contracts
|
||||
auto without_inv =
|
||||
testing::MakeExceptionSafetyTester().WithOperation(op).WithFactory(fac);
|
||||
EXPECT_FALSE(HasNullaryTest(without_inv));
|
||||
EXPECT_FALSE(HasUnaryTest(without_inv));
|
||||
}
|
||||
|
||||
struct ExampleStruct {};
|
||||
|
||||
std::unique_ptr<ExampleStruct> ExampleFunctionFactory() {
|
||||
return absl::make_unique<ExampleStruct>();
|
||||
}
|
||||
|
||||
void ExampleFunctionOperation(ExampleStruct*) {}
|
||||
|
||||
testing::AssertionResult ExampleFunctionContract(ExampleStruct*) {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
|
||||
struct {
|
||||
std::unique_ptr<ExampleStruct> operator()() const {
|
||||
return ExampleFunctionFactory();
|
||||
}
|
||||
} example_struct_factory;
|
||||
|
||||
struct {
|
||||
void operator()(ExampleStruct*) const {}
|
||||
} example_struct_operation;
|
||||
|
||||
struct {
|
||||
testing::AssertionResult operator()(ExampleStruct* example_struct) const {
|
||||
return ExampleFunctionContract(example_struct);
|
||||
}
|
||||
} example_struct_contract;
|
||||
|
||||
auto example_lambda_factory = []() { return ExampleFunctionFactory(); };
|
||||
|
||||
auto example_lambda_operation = [](ExampleStruct*) {};
|
||||
|
||||
auto example_lambda_contract = [](ExampleStruct* example_struct) {
|
||||
return ExampleFunctionContract(example_struct);
|
||||
};
|
||||
|
||||
// Testing that function references, pointers, structs with operator() and
|
||||
// lambdas can all be used with ExceptionSafetyTester
|
||||
TEST(ExceptionSafetyTesterTest, MixedFunctionTypes) {
|
||||
// function reference
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(ExampleFunctionFactory)
|
||||
.WithOperation(ExampleFunctionOperation)
|
||||
.WithContracts(ExampleFunctionContract)
|
||||
.Test());
|
||||
|
||||
// function pointer
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(&ExampleFunctionFactory)
|
||||
.WithOperation(&ExampleFunctionOperation)
|
||||
.WithContracts(&ExampleFunctionContract)
|
||||
.Test());
|
||||
|
||||
// struct
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(example_struct_factory)
|
||||
.WithOperation(example_struct_operation)
|
||||
.WithContracts(example_struct_contract)
|
||||
.Test());
|
||||
|
||||
// lambda
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(example_lambda_factory)
|
||||
.WithOperation(example_lambda_operation)
|
||||
.WithContracts(example_lambda_contract)
|
||||
.Test());
|
||||
}
|
||||
|
||||
struct NonNegative {
|
||||
bool operator==(const NonNegative& other) const { return i == other.i; }
|
||||
int i;
|
||||
};
|
||||
|
||||
testing::AssertionResult CheckNonNegativeInvariants(NonNegative* g) {
|
||||
if (g->i >= 0) {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
return testing::AssertionFailure()
|
||||
<< "i should be non-negative but is " << g->i;
|
||||
}
|
||||
|
||||
struct {
|
||||
template <typename T>
|
||||
void operator()(T* t) const {
|
||||
(*t)();
|
||||
}
|
||||
} invoker;
|
||||
|
||||
auto tester =
|
||||
testing::MakeExceptionSafetyTester().WithOperation(invoker).WithContracts(
|
||||
CheckNonNegativeInvariants);
|
||||
auto strong_tester = tester.WithContracts(testing::strong_guarantee);
|
||||
|
||||
struct FailsBasicGuarantee : public NonNegative {
|
||||
void operator()() {
|
||||
--i;
|
||||
ThrowingValue<> bomb;
|
||||
++i;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(ExceptionCheckTest, BasicGuaranteeFailure) {
|
||||
EXPECT_FALSE(tester.WithInitialValue(FailsBasicGuarantee{}).Test());
|
||||
}
|
||||
|
||||
struct FollowsBasicGuarantee : public NonNegative {
|
||||
void operator()() {
|
||||
++i;
|
||||
ThrowingValue<> bomb;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(ExceptionCheckTest, BasicGuarantee) {
|
||||
EXPECT_TRUE(tester.WithInitialValue(FollowsBasicGuarantee{}).Test());
|
||||
}
|
||||
|
||||
TEST(ExceptionCheckTest, StrongGuaranteeFailure) {
|
||||
EXPECT_FALSE(strong_tester.WithInitialValue(FailsBasicGuarantee{}).Test());
|
||||
EXPECT_FALSE(strong_tester.WithInitialValue(FollowsBasicGuarantee{}).Test());
|
||||
}
|
||||
|
||||
struct BasicGuaranteeWithExtraContracts : public NonNegative {
|
||||
// After operator(), i is incremented. If operator() throws, i is set to 9999
|
||||
void operator()() {
|
||||
int old_i = i;
|
||||
i = kExceptionSentinel;
|
||||
ThrowingValue<> bomb;
|
||||
i = ++old_i;
|
||||
}
|
||||
|
||||
static constexpr int kExceptionSentinel = 9999;
|
||||
};
|
||||
constexpr int BasicGuaranteeWithExtraContracts::kExceptionSentinel;
|
||||
|
||||
TEST(ExceptionCheckTest, BasicGuaranteeWithExtraContracts) {
|
||||
auto tester_with_val =
|
||||
tester.WithInitialValue(BasicGuaranteeWithExtraContracts{});
|
||||
EXPECT_TRUE(tester_with_val.Test());
|
||||
EXPECT_TRUE(
|
||||
tester_with_val
|
||||
.WithContracts([](BasicGuaranteeWithExtraContracts* o) {
|
||||
if (o->i == BasicGuaranteeWithExtraContracts::kExceptionSentinel) {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
return testing::AssertionFailure()
|
||||
<< "i should be "
|
||||
<< BasicGuaranteeWithExtraContracts::kExceptionSentinel
|
||||
<< ", but is " << o->i;
|
||||
})
|
||||
.Test());
|
||||
}
|
||||
|
||||
struct FollowsStrongGuarantee : public NonNegative {
|
||||
void operator()() { ThrowingValue<> bomb; }
|
||||
};
|
||||
|
||||
TEST(ExceptionCheckTest, StrongGuarantee) {
|
||||
EXPECT_TRUE(tester.WithInitialValue(FollowsStrongGuarantee{}).Test());
|
||||
EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{}).Test());
|
||||
}
|
||||
|
||||
struct HasReset : public NonNegative {
|
||||
void operator()() {
|
||||
i = -1;
|
||||
ThrowingValue<> bomb;
|
||||
i = 1;
|
||||
}
|
||||
|
||||
void reset() { i = 0; }
|
||||
};
|
||||
|
||||
testing::AssertionResult CheckHasResetContracts(HasReset* h) {
|
||||
h->reset();
|
||||
return testing::AssertionResult(h->i == 0);
|
||||
}
|
||||
|
||||
TEST(ExceptionCheckTest, ModifyingChecker) {
|
||||
auto set_to_1000 = [](FollowsBasicGuarantee* g) {
|
||||
g->i = 1000;
|
||||
return testing::AssertionSuccess();
|
||||
};
|
||||
auto is_1000 = [](FollowsBasicGuarantee* g) {
|
||||
return testing::AssertionResult(g->i == 1000);
|
||||
};
|
||||
auto increment = [](FollowsStrongGuarantee* g) {
|
||||
++g->i;
|
||||
return testing::AssertionSuccess();
|
||||
};
|
||||
|
||||
EXPECT_FALSE(tester.WithInitialValue(FollowsBasicGuarantee{})
|
||||
.WithContracts(set_to_1000, is_1000)
|
||||
.Test());
|
||||
EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{})
|
||||
.WithContracts(increment)
|
||||
.Test());
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithInitialValue(HasReset{})
|
||||
.WithContracts(CheckHasResetContracts)
|
||||
.Test(invoker));
|
||||
}
|
||||
|
||||
TEST(ExceptionSafetyTesterTest, ResetsCountdown) {
|
||||
auto test =
|
||||
testing::MakeExceptionSafetyTester()
|
||||
.WithInitialValue(ThrowingValue<>())
|
||||
.WithContracts([](ThrowingValue<>*) { return AssertionSuccess(); })
|
||||
.WithOperation([](ThrowingValue<>*) {});
|
||||
ASSERT_TRUE(test.Test());
|
||||
// If the countdown isn't reset because there were no exceptions thrown, then
|
||||
// this will fail with a termination from an unhandled exception
|
||||
EXPECT_TRUE(test.Test());
|
||||
}
|
||||
|
||||
struct NonCopyable : public NonNegative {
|
||||
NonCopyable(const NonCopyable&) = delete;
|
||||
NonCopyable() : NonNegative{0} {}
|
||||
|
||||
void operator()() { ThrowingValue<> bomb; }
|
||||
};
|
||||
|
||||
TEST(ExceptionCheckTest, NonCopyable) {
|
||||
auto factory = []() { return absl::make_unique<NonCopyable>(); };
|
||||
EXPECT_TRUE(tester.WithFactory(factory).Test());
|
||||
EXPECT_TRUE(strong_tester.WithFactory(factory).Test());
|
||||
}
|
||||
|
||||
struct NonEqualityComparable : public NonNegative {
|
||||
void operator()() { ThrowingValue<> bomb; }
|
||||
|
||||
void ModifyOnThrow() {
|
||||
++i;
|
||||
ThrowingValue<> bomb;
|
||||
static_cast<void>(bomb);
|
||||
--i;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(ExceptionCheckTest, NonEqualityComparable) {
|
||||
auto nec_is_strong = [](NonEqualityComparable* nec) {
|
||||
return testing::AssertionResult(nec->i == NonEqualityComparable().i);
|
||||
};
|
||||
auto strong_nec_tester = tester.WithInitialValue(NonEqualityComparable{})
|
||||
.WithContracts(nec_is_strong);
|
||||
|
||||
EXPECT_TRUE(strong_nec_tester.Test());
|
||||
EXPECT_FALSE(strong_nec_tester.Test(
|
||||
[](NonEqualityComparable* n) { n->ModifyOnThrow(); }));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct ExhaustivenessTester {
|
||||
void operator()() {
|
||||
successes |= 1;
|
||||
T b1;
|
||||
static_cast<void>(b1);
|
||||
successes |= (1 << 1);
|
||||
T b2;
|
||||
static_cast<void>(b2);
|
||||
successes |= (1 << 2);
|
||||
T b3;
|
||||
static_cast<void>(b3);
|
||||
successes |= (1 << 3);
|
||||
}
|
||||
|
||||
bool operator==(const ExhaustivenessTester<ThrowingValue<>>&) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
static unsigned char successes;
|
||||
};
|
||||
|
||||
struct {
|
||||
template <typename T>
|
||||
testing::AssertionResult operator()(ExhaustivenessTester<T>*) const {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
} CheckExhaustivenessTesterContracts;
|
||||
|
||||
template <typename T>
|
||||
unsigned char ExhaustivenessTester<T>::successes = 0;
|
||||
|
||||
TEST(ExceptionCheckTest, Exhaustiveness) {
|
||||
auto exhaust_tester = testing::MakeExceptionSafetyTester()
|
||||
.WithContracts(CheckExhaustivenessTesterContracts)
|
||||
.WithOperation(invoker);
|
||||
|
||||
EXPECT_TRUE(
|
||||
exhaust_tester.WithInitialValue(ExhaustivenessTester<int>{}).Test());
|
||||
EXPECT_EQ(ExhaustivenessTester<int>::successes, 0xF);
|
||||
|
||||
EXPECT_TRUE(
|
||||
exhaust_tester.WithInitialValue(ExhaustivenessTester<ThrowingValue<>>{})
|
||||
.WithContracts(testing::strong_guarantee)
|
||||
.Test());
|
||||
EXPECT_EQ(ExhaustivenessTester<ThrowingValue<>>::successes, 0xF);
|
||||
}
|
||||
|
||||
struct LeaksIfCtorThrows : private exceptions_internal::TrackedObject {
|
||||
LeaksIfCtorThrows() : TrackedObject(ABSL_PRETTY_FUNCTION) {
|
||||
++counter;
|
||||
ThrowingValue<> v;
|
||||
static_cast<void>(v);
|
||||
--counter;
|
||||
}
|
||||
LeaksIfCtorThrows(const LeaksIfCtorThrows&) noexcept
|
||||
: TrackedObject(ABSL_PRETTY_FUNCTION) {}
|
||||
static int counter;
|
||||
};
|
||||
int LeaksIfCtorThrows::counter = 0;
|
||||
|
||||
TEST(ExceptionCheckTest, TestLeakyCtor) {
|
||||
testing::TestThrowingCtor<LeaksIfCtorThrows>();
|
||||
EXPECT_EQ(LeaksIfCtorThrows::counter, 1);
|
||||
LeaksIfCtorThrows::counter = 0;
|
||||
}
|
||||
|
||||
struct Tracked : private exceptions_internal::TrackedObject {
|
||||
Tracked() : TrackedObject(ABSL_PRETTY_FUNCTION) {}
|
||||
};
|
||||
|
||||
TEST(ConstructorTrackerTest, CreatedBefore) {
|
||||
Tracked a, b, c;
|
||||
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
||||
}
|
||||
|
||||
TEST(ConstructorTrackerTest, CreatedAfter) {
|
||||
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
||||
Tracked a, b, c;
|
||||
}
|
||||
|
||||
TEST(ConstructorTrackerTest, NotDestroyedAfter) {
|
||||
alignas(Tracked) unsigned char storage[sizeof(Tracked)];
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
{
|
||||
exceptions_internal::ConstructorTracker ct(
|
||||
exceptions_internal::countdown);
|
||||
new (&storage) Tracked();
|
||||
},
|
||||
"not destroyed");
|
||||
}
|
||||
|
||||
TEST(ConstructorTrackerTest, DestroyedTwice) {
|
||||
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
{
|
||||
Tracked t;
|
||||
t.~Tracked();
|
||||
},
|
||||
"re-destroyed");
|
||||
}
|
||||
|
||||
TEST(ConstructorTrackerTest, ConstructedTwice) {
|
||||
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
||||
alignas(Tracked) unsigned char storage[sizeof(Tracked)];
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
{
|
||||
new (&storage) Tracked();
|
||||
new (&storage) Tracked();
|
||||
reinterpret_cast<Tracked*>(&storage)->~Tracked();
|
||||
},
|
||||
"re-constructed");
|
||||
}
|
||||
|
||||
TEST(ThrowingValueTraitsTest, RelationalOperators) {
|
||||
ThrowingValue<> a, b;
|
||||
EXPECT_TRUE((std::is_convertible<decltype(a == b), bool>::value));
|
||||
EXPECT_TRUE((std::is_convertible<decltype(a != b), bool>::value));
|
||||
EXPECT_TRUE((std::is_convertible<decltype(a < b), bool>::value));
|
||||
EXPECT_TRUE((std::is_convertible<decltype(a <= b), bool>::value));
|
||||
EXPECT_TRUE((std::is_convertible<decltype(a > b), bool>::value));
|
||||
EXPECT_TRUE((std::is_convertible<decltype(a >= b), bool>::value));
|
||||
}
|
||||
|
||||
TEST(ThrowingAllocatorTraitsTest, Assignablility) {
|
||||
EXPECT_TRUE(absl::is_move_assignable<ThrowingAllocator<int>>::value);
|
||||
EXPECT_TRUE(absl::is_copy_assignable<ThrowingAllocator<int>>::value);
|
||||
EXPECT_TRUE(std::is_nothrow_move_assignable<ThrowingAllocator<int>>::value);
|
||||
EXPECT_TRUE(std::is_nothrow_copy_assignable<ThrowingAllocator<int>>::value);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace testing
|
||||
|
||||
#endif // ABSL_HAVE_EXCEPTIONS
|
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2017 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 <type_traits>
|
||||
|
||||
#include "absl/base/internal/inline_variable.h"
|
||||
#include "absl/base/internal/inline_variable_testing.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace inline_variable_testing_internal {
|
||||
namespace {
|
||||
|
||||
TEST(InlineVariableTest, Constexpr) {
|
||||
static_assert(inline_variable_foo.value == 5, "");
|
||||
static_assert(other_inline_variable_foo.value == 5, "");
|
||||
static_assert(inline_variable_int == 5, "");
|
||||
static_assert(other_inline_variable_int == 5, "");
|
||||
}
|
||||
|
||||
TEST(InlineVariableTest, DefaultConstructedIdentityEquality) {
|
||||
EXPECT_EQ(get_foo_a().value, 5);
|
||||
EXPECT_EQ(get_foo_b().value, 5);
|
||||
EXPECT_EQ(&get_foo_a(), &get_foo_b());
|
||||
}
|
||||
|
||||
TEST(InlineVariableTest, DefaultConstructedIdentityInequality) {
|
||||
EXPECT_NE(&inline_variable_foo, &other_inline_variable_foo);
|
||||
}
|
||||
|
||||
TEST(InlineVariableTest, InitializedIdentityEquality) {
|
||||
EXPECT_EQ(get_int_a(), 5);
|
||||
EXPECT_EQ(get_int_b(), 5);
|
||||
EXPECT_EQ(&get_int_a(), &get_int_b());
|
||||
}
|
||||
|
||||
TEST(InlineVariableTest, InitializedIdentityInequality) {
|
||||
EXPECT_NE(&inline_variable_int, &other_inline_variable_int);
|
||||
}
|
||||
|
||||
TEST(InlineVariableTest, FunPtrType) {
|
||||
static_assert(
|
||||
std::is_same<void(*)(),
|
||||
std::decay<decltype(inline_variable_fun_ptr)>::type>::value,
|
||||
"");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace inline_variable_testing_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2017 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/base/internal/inline_variable_testing.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace inline_variable_testing_internal {
|
||||
|
||||
const Foo& get_foo_a() { return inline_variable_foo; }
|
||||
|
||||
const int& get_int_a() { return inline_variable_int; }
|
||||
|
||||
} // namespace inline_variable_testing_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2017 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/base/internal/inline_variable_testing.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace inline_variable_testing_internal {
|
||||
|
||||
const Foo& get_foo_b() { return inline_variable_foo; }
|
||||
|
||||
const int& get_int_b() { return inline_variable_int; }
|
||||
|
||||
} // namespace inline_variable_testing_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,200 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_
|
||||
#define ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
#define ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT 0
|
||||
#else
|
||||
#define ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT 1
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define ABSL_HAVE_WORKING_ATOMIC_POINTER 0
|
||||
#else
|
||||
#define ABSL_HAVE_WORKING_ATOMIC_POINTER 1
|
||||
#endif
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
template <typename T>
|
||||
class AtomicHook;
|
||||
|
||||
// To workaround AtomicHook not being constant-initializable on some platforms,
|
||||
// prefer to annotate instances with `ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES`
|
||||
// instead of `ABSL_CONST_INIT`.
|
||||
#if ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
||||
#define ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_CONST_INIT
|
||||
#else
|
||||
#define ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
|
||||
#endif
|
||||
|
||||
// `AtomicHook` is a helper class, templatized on a raw function pointer type,
|
||||
// for implementing Abseil customization hooks. It is a callable object that
|
||||
// dispatches to the registered hook. Objects of type `AtomicHook` must have
|
||||
// static or thread storage duration.
|
||||
//
|
||||
// A default constructed object performs a no-op (and returns a default
|
||||
// constructed object) if no hook has been registered.
|
||||
//
|
||||
// Hooks can be pre-registered via constant initialization, for example:
|
||||
//
|
||||
// ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static AtomicHook<void(*)()>
|
||||
// my_hook(DefaultAction);
|
||||
//
|
||||
// and then changed at runtime via a call to `Store()`.
|
||||
//
|
||||
// Reads and writes guarantee memory_order_acquire/memory_order_release
|
||||
// semantics.
|
||||
template <typename ReturnType, typename... Args>
|
||||
class AtomicHook<ReturnType (*)(Args...)> {
|
||||
public:
|
||||
using FnPtr = ReturnType (*)(Args...);
|
||||
|
||||
// Constructs an object that by default performs a no-op (and
|
||||
// returns a default constructed object) when no hook as been registered.
|
||||
constexpr AtomicHook() : AtomicHook(DummyFunction) {}
|
||||
|
||||
// Constructs an object that by default dispatches to/returns the
|
||||
// pre-registered default_fn when no hook has been registered at runtime.
|
||||
#if ABSL_HAVE_WORKING_ATOMIC_POINTER && ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
||||
explicit constexpr AtomicHook(FnPtr default_fn)
|
||||
: hook_(default_fn), default_fn_(default_fn) {}
|
||||
#elif ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
||||
explicit constexpr AtomicHook(FnPtr default_fn)
|
||||
: hook_(kUninitialized), default_fn_(default_fn) {}
|
||||
#else
|
||||
// As of January 2020, on all known versions of MSVC this constructor runs in
|
||||
// the global constructor sequence. If `Store()` is called by a dynamic
|
||||
// initializer, we want to preserve the value, even if this constructor runs
|
||||
// after the call to `Store()`. If not, `hook_` will be
|
||||
// zero-initialized by the linker and we have no need to set it.
|
||||
// https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html
|
||||
explicit constexpr AtomicHook(FnPtr default_fn)
|
||||
: /* hook_(deliberately omitted), */ default_fn_(default_fn) {
|
||||
static_assert(kUninitialized == 0, "here we rely on zero-initialization");
|
||||
}
|
||||
#endif
|
||||
|
||||
// Stores the provided function pointer as the value for this hook.
|
||||
//
|
||||
// This is intended to be called once. Multiple calls are legal only if the
|
||||
// same function pointer is provided for each call. The store is implemented
|
||||
// as a memory_order_release operation, and read accesses are implemented as
|
||||
// memory_order_acquire.
|
||||
void Store(FnPtr fn) {
|
||||
bool success = DoStore(fn);
|
||||
static_cast<void>(success);
|
||||
assert(success);
|
||||
}
|
||||
|
||||
// Invokes the registered callback. If no callback has yet been registered, a
|
||||
// default-constructed object of the appropriate type is returned instead.
|
||||
template <typename... CallArgs>
|
||||
ReturnType operator()(CallArgs&&... args) const {
|
||||
return DoLoad()(std::forward<CallArgs>(args)...);
|
||||
}
|
||||
|
||||
// Returns the registered callback, or nullptr if none has been registered.
|
||||
// Useful if client code needs to conditionalize behavior based on whether a
|
||||
// callback was registered.
|
||||
//
|
||||
// Note that atomic_hook.Load()() and atomic_hook() have different semantics:
|
||||
// operator()() will perform a no-op if no callback was registered, while
|
||||
// Load()() will dereference a null function pointer. Prefer operator()() to
|
||||
// Load()() unless you must conditionalize behavior on whether a hook was
|
||||
// registered.
|
||||
FnPtr Load() const {
|
||||
FnPtr ptr = DoLoad();
|
||||
return (ptr == DummyFunction) ? nullptr : ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
static ReturnType DummyFunction(Args...) {
|
||||
return ReturnType();
|
||||
}
|
||||
|
||||
// Current versions of MSVC (as of September 2017) have a broken
|
||||
// implementation of std::atomic<T*>: Its constructor attempts to do the
|
||||
// equivalent of a reinterpret_cast in a constexpr context, which is not
|
||||
// allowed.
|
||||
//
|
||||
// This causes an issue when building with LLVM under Windows. To avoid this,
|
||||
// we use a less-efficient, intptr_t-based implementation on Windows.
|
||||
#if ABSL_HAVE_WORKING_ATOMIC_POINTER
|
||||
// Return the stored value, or DummyFunction if no value has been stored.
|
||||
FnPtr DoLoad() const { return hook_.load(std::memory_order_acquire); }
|
||||
|
||||
// Store the given value. Returns false if a different value was already
|
||||
// stored to this object.
|
||||
bool DoStore(FnPtr fn) {
|
||||
assert(fn);
|
||||
FnPtr expected = default_fn_;
|
||||
const bool store_succeeded = hook_.compare_exchange_strong(
|
||||
expected, fn, std::memory_order_acq_rel, std::memory_order_acquire);
|
||||
const bool same_value_already_stored = (expected == fn);
|
||||
return store_succeeded || same_value_already_stored;
|
||||
}
|
||||
|
||||
std::atomic<FnPtr> hook_;
|
||||
#else // !ABSL_HAVE_WORKING_ATOMIC_POINTER
|
||||
// Use a sentinel value unlikely to be the address of an actual function.
|
||||
static constexpr intptr_t kUninitialized = 0;
|
||||
|
||||
static_assert(sizeof(intptr_t) >= sizeof(FnPtr),
|
||||
"intptr_t can't contain a function pointer");
|
||||
|
||||
FnPtr DoLoad() const {
|
||||
const intptr_t value = hook_.load(std::memory_order_acquire);
|
||||
if (value == kUninitialized) {
|
||||
return default_fn_;
|
||||
}
|
||||
return reinterpret_cast<FnPtr>(value);
|
||||
}
|
||||
|
||||
bool DoStore(FnPtr fn) {
|
||||
assert(fn);
|
||||
const auto value = reinterpret_cast<intptr_t>(fn);
|
||||
intptr_t expected = kUninitialized;
|
||||
const bool store_succeeded = hook_.compare_exchange_strong(
|
||||
expected, value, std::memory_order_acq_rel, std::memory_order_acquire);
|
||||
const bool same_value_already_stored = (expected == value);
|
||||
return store_succeeded || same_value_already_stored;
|
||||
}
|
||||
|
||||
std::atomic<intptr_t> hook_;
|
||||
#endif
|
||||
|
||||
const FnPtr default_fn_;
|
||||
};
|
||||
|
||||
#undef ABSL_HAVE_WORKING_ATOMIC_POINTER
|
||||
#undef ABSL_HAVE_WORKING_CONSTEXPR_STATIC_INIT
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_ATOMIC_HOOK_H_
|
|
@ -0,0 +1,97 @@
|
|||
// Copyright 2018 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/base/internal/atomic_hook.h"
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/internal/atomic_hook_test_helper.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using ::testing::Eq;
|
||||
|
||||
int value = 0;
|
||||
void TestHook(int x) { value = x; }
|
||||
|
||||
TEST(AtomicHookTest, NoDefaultFunction) {
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
|
||||
void (*)(int)>
|
||||
hook;
|
||||
value = 0;
|
||||
|
||||
// Test the default DummyFunction.
|
||||
EXPECT_TRUE(hook.Load() == nullptr);
|
||||
EXPECT_EQ(value, 0);
|
||||
hook(1);
|
||||
EXPECT_EQ(value, 0);
|
||||
|
||||
// Test a stored hook.
|
||||
hook.Store(TestHook);
|
||||
EXPECT_TRUE(hook.Load() == TestHook);
|
||||
EXPECT_EQ(value, 0);
|
||||
hook(1);
|
||||
EXPECT_EQ(value, 1);
|
||||
|
||||
// Calling Store() with the same hook should not crash.
|
||||
hook.Store(TestHook);
|
||||
EXPECT_TRUE(hook.Load() == TestHook);
|
||||
EXPECT_EQ(value, 1);
|
||||
hook(2);
|
||||
EXPECT_EQ(value, 2);
|
||||
}
|
||||
|
||||
TEST(AtomicHookTest, WithDefaultFunction) {
|
||||
// Set the default value to TestHook at compile-time.
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static absl::base_internal::AtomicHook<
|
||||
void (*)(int)>
|
||||
hook(TestHook);
|
||||
value = 0;
|
||||
|
||||
// Test the default value is TestHook.
|
||||
EXPECT_TRUE(hook.Load() == TestHook);
|
||||
EXPECT_EQ(value, 0);
|
||||
hook(1);
|
||||
EXPECT_EQ(value, 1);
|
||||
|
||||
// Calling Store() with the same hook should not crash.
|
||||
hook.Store(TestHook);
|
||||
EXPECT_TRUE(hook.Load() == TestHook);
|
||||
EXPECT_EQ(value, 1);
|
||||
hook(2);
|
||||
EXPECT_EQ(value, 2);
|
||||
}
|
||||
|
||||
ABSL_CONST_INIT int override_func_calls = 0;
|
||||
void OverrideFunc() { override_func_calls++; }
|
||||
static struct OverrideInstaller {
|
||||
OverrideInstaller() { absl::atomic_hook_internal::func.Store(OverrideFunc); }
|
||||
} override_installer;
|
||||
|
||||
TEST(AtomicHookTest, DynamicInitFromAnotherTU) {
|
||||
// MSVC 14.2 doesn't do constexpr static init correctly; in particular it
|
||||
// tends to sequence static init (i.e. defaults) of `AtomicHook` objects
|
||||
// after their dynamic init (i.e. overrides), overwriting whatever value was
|
||||
// written during dynamic init. This regression test validates the fix.
|
||||
// https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html
|
||||
EXPECT_THAT(absl::atomic_hook_internal::default_func_calls, Eq(0));
|
||||
EXPECT_THAT(override_func_calls, Eq(0));
|
||||
absl::atomic_hook_internal::func();
|
||||
EXPECT_THAT(absl::atomic_hook_internal::default_func_calls, Eq(0));
|
||||
EXPECT_THAT(override_func_calls, Eq(1));
|
||||
EXPECT_THAT(absl::atomic_hook_internal::func.Load(), Eq(OverrideFunc));
|
||||
}
|
||||
|
||||
} // namespace
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright 2017 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/base/internal/atomic_hook_test_helper.h"
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/internal/atomic_hook.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace atomic_hook_internal {
|
||||
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES absl::base_internal::AtomicHook<VoidF>
|
||||
func(DefaultFunc);
|
||||
ABSL_CONST_INIT int default_func_calls = 0;
|
||||
void DefaultFunc() { default_func_calls++; }
|
||||
void RegisterFunc(VoidF f) { func.Store(f); }
|
||||
|
||||
} // namespace atomic_hook_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#ifndef ABSL_BASE_ATOMIC_HOOK_TEST_HELPER_H_
|
||||
#define ABSL_BASE_ATOMIC_HOOK_TEST_HELPER_H_
|
||||
|
||||
#include "absl/base/internal/atomic_hook.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace atomic_hook_internal {
|
||||
|
||||
using VoidF = void (*)();
|
||||
extern absl::base_internal::AtomicHook<VoidF> func;
|
||||
extern int default_func_calls;
|
||||
void DefaultFunc();
|
||||
void RegisterFunc(VoidF func);
|
||||
|
||||
} // namespace atomic_hook_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_ATOMIC_HOOK_TEST_HELPER_H_
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2018 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 <iostream>
|
||||
#include "absl/base/internal/thread_identity.h"
|
||||
|
||||
int main() {
|
||||
auto* tid = absl::base_internal::CurrentThreadIdentityIfPresent();
|
||||
// Make sure the above call can't be optimized out
|
||||
std::cout << (void*)tid << std::endl;
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// The implementation of CycleClock::Frequency.
|
||||
//
|
||||
// NOTE: only i386 and x86_64 have been well tested.
|
||||
// PPC, sparc, alpha, and ia64 are based on
|
||||
// http://peter.kuscsik.com/wordpress/?p=14
|
||||
// with modifications by m3b. See also
|
||||
// https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
|
||||
|
||||
#include "absl/base/internal/cycleclock.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono> // NOLINT(build/c++11)
|
||||
|
||||
#include "absl/base/internal/unscaledcycleclock.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
#if ABSL_USE_UNSCALED_CYCLECLOCK
|
||||
|
||||
namespace {
|
||||
|
||||
#ifdef NDEBUG
|
||||
#ifdef ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
|
||||
// Not debug mode and the UnscaledCycleClock frequency is the CPU
|
||||
// frequency. Scale the CycleClock to prevent overflow if someone
|
||||
// tries to represent the time as cycles since the Unix epoch.
|
||||
static constexpr int32_t kShift = 1;
|
||||
#else
|
||||
// Not debug mode and the UnscaledCycleClock isn't operating at the
|
||||
// raw CPU frequency. There is no need to do any scaling, so don't
|
||||
// needlessly sacrifice precision.
|
||||
static constexpr int32_t kShift = 0;
|
||||
#endif
|
||||
#else
|
||||
// In debug mode use a different shift to discourage depending on a
|
||||
// particular shift value.
|
||||
static constexpr int32_t kShift = 2;
|
||||
#endif
|
||||
|
||||
static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
|
||||
static std::atomic<CycleClockSourceFunc> cycle_clock_source;
|
||||
|
||||
CycleClockSourceFunc LoadCycleClockSource() {
|
||||
// Optimize for the common case (no callback) by first doing a relaxed load;
|
||||
// this is significantly faster on non-x86 platforms.
|
||||
if (cycle_clock_source.load(std::memory_order_relaxed) == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
// This corresponds to the store(std::memory_order_release) in
|
||||
// CycleClockSource::Register, and makes sure that any updates made prior to
|
||||
// registering the callback are visible to this thread before the callback is
|
||||
// invoked.
|
||||
return cycle_clock_source.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int64_t CycleClock::Now() {
|
||||
auto fn = LoadCycleClockSource();
|
||||
if (fn == nullptr) {
|
||||
return base_internal::UnscaledCycleClock::Now() >> kShift;
|
||||
}
|
||||
return fn() >> kShift;
|
||||
}
|
||||
|
||||
double CycleClock::Frequency() {
|
||||
return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
|
||||
}
|
||||
|
||||
void CycleClockSource::Register(CycleClockSourceFunc source) {
|
||||
// Corresponds to the load(std::memory_order_acquire) in LoadCycleClockSource.
|
||||
cycle_clock_source.store(source, std::memory_order_release);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int64_t CycleClock::Now() {
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
|
||||
double CycleClock::Frequency() {
|
||||
return 1e9;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,94 @@
|
|||
//
|
||||
// Copyright 2017 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.
|
||||
//
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File: cycleclock.h
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// This header file defines a `CycleClock`, which yields the value and frequency
|
||||
// of a cycle counter that increments at a rate that is approximately constant.
|
||||
//
|
||||
// NOTE:
|
||||
//
|
||||
// The cycle counter frequency is not necessarily related to the core clock
|
||||
// frequency and should not be treated as such. That is, `CycleClock` cycles are
|
||||
// not necessarily "CPU cycles" and code should not rely on that behavior, even
|
||||
// if experimentally observed.
|
||||
//
|
||||
// An arbitrary offset may have been added to the counter at power on.
|
||||
//
|
||||
// On some platforms, the rate and offset of the counter may differ
|
||||
// slightly when read from different CPUs of a multiprocessor. Usually,
|
||||
// we try to ensure that the operating system adjusts values periodically
|
||||
// so that values agree approximately. If you need stronger guarantees,
|
||||
// consider using alternate interfaces.
|
||||
//
|
||||
// The CPU is not required to maintain the ordering of a cycle counter read
|
||||
// with respect to surrounding instructions.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_CYCLECLOCK_H_
|
||||
#define ABSL_BASE_INTERNAL_CYCLECLOCK_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// CycleClock
|
||||
// -----------------------------------------------------------------------------
|
||||
class CycleClock {
|
||||
public:
|
||||
// CycleClock::Now()
|
||||
//
|
||||
// Returns the value of a cycle counter that counts at a rate that is
|
||||
// approximately constant.
|
||||
static int64_t Now();
|
||||
|
||||
// CycleClock::Frequency()
|
||||
//
|
||||
// Returns the amount by which `CycleClock::Now()` increases per second. Note
|
||||
// that this value may not necessarily match the core CPU clock frequency.
|
||||
static double Frequency();
|
||||
|
||||
private:
|
||||
CycleClock() = delete; // no instances
|
||||
CycleClock(const CycleClock&) = delete;
|
||||
CycleClock& operator=(const CycleClock&) = delete;
|
||||
};
|
||||
|
||||
using CycleClockSourceFunc = int64_t (*)();
|
||||
|
||||
class CycleClockSource {
|
||||
private:
|
||||
// CycleClockSource::Register()
|
||||
//
|
||||
// Register a function that provides an alternate source for the unscaled CPU
|
||||
// cycle count value. The source function must be async signal safe, must not
|
||||
// call CycleClock::Now(), and must have a frequency that matches that of the
|
||||
// unscaled clock used by CycleClock. A nullptr value resets CycleClock to use
|
||||
// the default source.
|
||||
static void Register(CycleClockSourceFunc source);
|
||||
};
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_CYCLECLOCK_H_
|
|
@ -0,0 +1,169 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// Functions for directly invoking mmap() via syscall, avoiding the case where
|
||||
// mmap() has been locally overridden.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
|
||||
#define ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
#if ABSL_HAVE_MMAP
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
#include <sys/types.h>
|
||||
#ifdef __BIONIC__
|
||||
#include <sys/syscall.h>
|
||||
#else
|
||||
#include <syscall.h>
|
||||
#endif
|
||||
|
||||
#include <linux/unistd.h>
|
||||
#include <unistd.h>
|
||||
#include <cerrno>
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
|
||||
#ifdef __mips__
|
||||
// Include definitions of the ABI currently in use.
|
||||
#ifdef __BIONIC__
|
||||
// Android doesn't have sgidefs.h, but does have asm/sgidefs.h, which has the
|
||||
// definitions we need.
|
||||
#include <asm/sgidefs.h>
|
||||
#else
|
||||
#include <sgidefs.h>
|
||||
#endif // __BIONIC__
|
||||
#endif // __mips__
|
||||
|
||||
// SYS_mmap and SYS_munmap are not defined in Android.
|
||||
#ifdef __BIONIC__
|
||||
extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
|
||||
#if defined(__NR_mmap) && !defined(SYS_mmap)
|
||||
#define SYS_mmap __NR_mmap
|
||||
#endif
|
||||
#ifndef SYS_munmap
|
||||
#define SYS_munmap __NR_munmap
|
||||
#endif
|
||||
#endif // __BIONIC__
|
||||
|
||||
#if defined(__NR_mmap2) && !defined(SYS_mmap2)
|
||||
#define SYS_mmap2 __NR_mmap2
|
||||
#endif
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// Platform specific logic extracted from
|
||||
// https://chromium.googlesource.com/linux-syscall-support/+/master/linux_syscall_support.h
|
||||
inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
|
||||
off64_t offset) noexcept {
|
||||
#if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
|
||||
defined(__m68k__) || defined(__sh__) || \
|
||||
(defined(__hppa__) && !defined(__LP64__)) || \
|
||||
(defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
|
||||
(defined(__PPC__) && !defined(__PPC64__)) || \
|
||||
(defined(__riscv) && __riscv_xlen == 32) || \
|
||||
(defined(__s390__) && !defined(__s390x__)) || \
|
||||
(defined(__sparc__) && !defined(__arch64__))
|
||||
// On these architectures, implement mmap with mmap2.
|
||||
static int pagesize = 0;
|
||||
if (pagesize == 0) {
|
||||
#if defined(__wasm__) || defined(__asmjs__)
|
||||
pagesize = getpagesize();
|
||||
#else
|
||||
pagesize = sysconf(_SC_PAGESIZE);
|
||||
#endif
|
||||
}
|
||||
if (offset < 0 || offset % pagesize != 0) {
|
||||
errno = EINVAL;
|
||||
return MAP_FAILED;
|
||||
}
|
||||
#ifdef __BIONIC__
|
||||
// SYS_mmap2 has problems on Android API level <= 16.
|
||||
// Workaround by invoking __mmap2() instead.
|
||||
return __mmap2(start, length, prot, flags, fd, offset / pagesize);
|
||||
#else
|
||||
return reinterpret_cast<void*>(
|
||||
syscall(SYS_mmap2, start, length, prot, flags, fd,
|
||||
static_cast<off_t>(offset / pagesize)));
|
||||
#endif
|
||||
#elif defined(__s390x__)
|
||||
// On s390x, mmap() arguments are passed in memory.
|
||||
unsigned long buf[6] = {reinterpret_cast<unsigned long>(start), // NOLINT
|
||||
static_cast<unsigned long>(length), // NOLINT
|
||||
static_cast<unsigned long>(prot), // NOLINT
|
||||
static_cast<unsigned long>(flags), // NOLINT
|
||||
static_cast<unsigned long>(fd), // NOLINT
|
||||
static_cast<unsigned long>(offset)}; // NOLINT
|
||||
return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
|
||||
#elif defined(__x86_64__)
|
||||
// The x32 ABI has 32 bit longs, but the syscall interface is 64 bit.
|
||||
// We need to explicitly cast to an unsigned 64 bit type to avoid implicit
|
||||
// sign extension. We can't cast pointers directly because those are
|
||||
// 32 bits, and gcc will dump ugly warnings about casting from a pointer
|
||||
// to an integer of a different size. We also need to make sure __off64_t
|
||||
// isn't truncated to 32-bits under x32.
|
||||
#define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
|
||||
return reinterpret_cast<void*>(
|
||||
syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length),
|
||||
MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags),
|
||||
MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
|
||||
#undef MMAP_SYSCALL_ARG
|
||||
#else // Remaining 64-bit aritectures.
|
||||
static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
|
||||
return reinterpret_cast<void*>(
|
||||
syscall(SYS_mmap, start, length, prot, flags, fd, offset));
|
||||
#endif
|
||||
}
|
||||
|
||||
inline int DirectMunmap(void* start, size_t length) {
|
||||
return static_cast<int>(syscall(SYS_munmap, start, length));
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#else // !__linux__
|
||||
|
||||
// For non-linux platforms where we have mmap, just dispatch directly to the
|
||||
// actual mmap()/munmap() methods.
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
|
||||
off_t offset) {
|
||||
return mmap(start, length, prot, flags, fd, offset);
|
||||
}
|
||||
|
||||
inline int DirectMunmap(void* start, size_t length) {
|
||||
return munmap(start, length);
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // __linux__
|
||||
|
||||
#endif // ABSL_HAVE_MMAP
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
|
|
@ -0,0 +1,327 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_ENDIAN_H_
|
||||
#define ABSL_BASE_INTERNAL_ENDIAN_H_
|
||||
|
||||
// The following guarantees declaration of the byte swap functions
|
||||
#ifdef _MSC_VER
|
||||
#include <stdlib.h> // NOLINT(build/include)
|
||||
#elif defined(__FreeBSD__)
|
||||
#include <sys/endian.h>
|
||||
#elif defined(__GLIBC__)
|
||||
#include <byteswap.h> // IWYU pragma: export
|
||||
#endif
|
||||
|
||||
#include <cstdint>
|
||||
#include "absl/base/casts.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/unaligned_access.h"
|
||||
#include "absl/base/port.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
|
||||
// Use compiler byte-swapping intrinsics if they are available. 32-bit
|
||||
// and 64-bit versions are available in Clang and GCC as of GCC 4.3.0.
|
||||
// The 16-bit version is available in Clang and GCC only as of GCC 4.8.0.
|
||||
// For simplicity, we enable them all only for GCC 4.8.0 or later.
|
||||
#if defined(__clang__) || \
|
||||
(defined(__GNUC__) && \
|
||||
((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5))
|
||||
inline uint64_t gbswap_64(uint64_t host_int) {
|
||||
return __builtin_bswap64(host_int);
|
||||
}
|
||||
inline uint32_t gbswap_32(uint32_t host_int) {
|
||||
return __builtin_bswap32(host_int);
|
||||
}
|
||||
inline uint16_t gbswap_16(uint16_t host_int) {
|
||||
return __builtin_bswap16(host_int);
|
||||
}
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
inline uint64_t gbswap_64(uint64_t host_int) {
|
||||
return _byteswap_uint64(host_int);
|
||||
}
|
||||
inline uint32_t gbswap_32(uint32_t host_int) {
|
||||
return _byteswap_ulong(host_int);
|
||||
}
|
||||
inline uint16_t gbswap_16(uint16_t host_int) {
|
||||
return _byteswap_ushort(host_int);
|
||||
}
|
||||
|
||||
#else
|
||||
inline uint64_t gbswap_64(uint64_t host_int) {
|
||||
#if defined(__GNUC__) && defined(__x86_64__) && !defined(__APPLE__)
|
||||
// Adapted from /usr/include/byteswap.h. Not available on Mac.
|
||||
if (__builtin_constant_p(host_int)) {
|
||||
return __bswap_constant_64(host_int);
|
||||
} else {
|
||||
uint64_t result;
|
||||
__asm__("bswap %0" : "=r"(result) : "0"(host_int));
|
||||
return result;
|
||||
}
|
||||
#elif defined(__GLIBC__)
|
||||
return bswap_64(host_int);
|
||||
#else
|
||||
return (((host_int & uint64_t{0xFF}) << 56) |
|
||||
((host_int & uint64_t{0xFF00}) << 40) |
|
||||
((host_int & uint64_t{0xFF0000}) << 24) |
|
||||
((host_int & uint64_t{0xFF000000}) << 8) |
|
||||
((host_int & uint64_t{0xFF00000000}) >> 8) |
|
||||
((host_int & uint64_t{0xFF0000000000}) >> 24) |
|
||||
((host_int & uint64_t{0xFF000000000000}) >> 40) |
|
||||
((host_int & uint64_t{0xFF00000000000000}) >> 56));
|
||||
#endif // bswap_64
|
||||
}
|
||||
|
||||
inline uint32_t gbswap_32(uint32_t host_int) {
|
||||
#if defined(__GLIBC__)
|
||||
return bswap_32(host_int);
|
||||
#else
|
||||
return (((host_int & uint32_t{0xFF}) << 24) |
|
||||
((host_int & uint32_t{0xFF00}) << 8) |
|
||||
((host_int & uint32_t{0xFF0000}) >> 8) |
|
||||
((host_int & uint32_t{0xFF000000}) >> 24));
|
||||
#endif
|
||||
}
|
||||
|
||||
inline uint16_t gbswap_16(uint16_t host_int) {
|
||||
#if defined(__GLIBC__)
|
||||
return bswap_16(host_int);
|
||||
#else
|
||||
return (((host_int & uint16_t{0xFF}) << 8) |
|
||||
((host_int & uint16_t{0xFF00}) >> 8));
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // intrinsics available
|
||||
|
||||
#ifdef ABSL_IS_LITTLE_ENDIAN
|
||||
|
||||
// Definitions for ntohl etc. that don't require us to include
|
||||
// netinet/in.h. We wrap gbswap_32 and gbswap_16 in functions rather
|
||||
// than just #defining them because in debug mode, gcc doesn't
|
||||
// correctly handle the (rather involved) definitions of bswap_32.
|
||||
// gcc guarantees that inline functions are as fast as macros, so
|
||||
// this isn't a performance hit.
|
||||
inline uint16_t ghtons(uint16_t x) { return gbswap_16(x); }
|
||||
inline uint32_t ghtonl(uint32_t x) { return gbswap_32(x); }
|
||||
inline uint64_t ghtonll(uint64_t x) { return gbswap_64(x); }
|
||||
|
||||
#elif defined ABSL_IS_BIG_ENDIAN
|
||||
|
||||
// These definitions are simpler on big-endian machines
|
||||
// These are functions instead of macros to avoid self-assignment warnings
|
||||
// on calls such as "i = ghtnol(i);". This also provides type checking.
|
||||
inline uint16_t ghtons(uint16_t x) { return x; }
|
||||
inline uint32_t ghtonl(uint32_t x) { return x; }
|
||||
inline uint64_t ghtonll(uint64_t x) { return x; }
|
||||
|
||||
#else
|
||||
#error \
|
||||
"Unsupported byte order: Either ABSL_IS_BIG_ENDIAN or " \
|
||||
"ABSL_IS_LITTLE_ENDIAN must be defined"
|
||||
#endif // byte order
|
||||
|
||||
inline uint16_t gntohs(uint16_t x) { return ghtons(x); }
|
||||
inline uint32_t gntohl(uint32_t x) { return ghtonl(x); }
|
||||
inline uint64_t gntohll(uint64_t x) { return ghtonll(x); }
|
||||
|
||||
// Utilities to convert numbers between the current hosts's native byte
|
||||
// order and little-endian byte order
|
||||
//
|
||||
// Load/Store methods are alignment safe
|
||||
namespace little_endian {
|
||||
// Conversion functions.
|
||||
#ifdef ABSL_IS_LITTLE_ENDIAN
|
||||
|
||||
inline uint16_t FromHost16(uint16_t x) { return x; }
|
||||
inline uint16_t ToHost16(uint16_t x) { return x; }
|
||||
|
||||
inline uint32_t FromHost32(uint32_t x) { return x; }
|
||||
inline uint32_t ToHost32(uint32_t x) { return x; }
|
||||
|
||||
inline uint64_t FromHost64(uint64_t x) { return x; }
|
||||
inline uint64_t ToHost64(uint64_t x) { return x; }
|
||||
|
||||
inline constexpr bool IsLittleEndian() { return true; }
|
||||
|
||||
#elif defined ABSL_IS_BIG_ENDIAN
|
||||
|
||||
inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
|
||||
inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
|
||||
|
||||
inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
|
||||
inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
|
||||
|
||||
inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
|
||||
inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
|
||||
|
||||
inline constexpr bool IsLittleEndian() { return false; }
|
||||
|
||||
#endif /* ENDIAN */
|
||||
|
||||
inline uint8_t FromHost(uint8_t x) { return x; }
|
||||
inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
|
||||
inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
|
||||
inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
|
||||
inline uint8_t ToHost(uint8_t x) { return x; }
|
||||
inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
|
||||
inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
|
||||
inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
|
||||
|
||||
inline int8_t FromHost(int8_t x) { return x; }
|
||||
inline int16_t FromHost(int16_t x) {
|
||||
return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
|
||||
}
|
||||
inline int32_t FromHost(int32_t x) {
|
||||
return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
|
||||
}
|
||||
inline int64_t FromHost(int64_t x) {
|
||||
return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
|
||||
}
|
||||
inline int8_t ToHost(int8_t x) { return x; }
|
||||
inline int16_t ToHost(int16_t x) {
|
||||
return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
|
||||
}
|
||||
inline int32_t ToHost(int32_t x) {
|
||||
return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
|
||||
}
|
||||
inline int64_t ToHost(int64_t x) {
|
||||
return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
|
||||
}
|
||||
|
||||
// Functions to do unaligned loads and stores in little-endian order.
|
||||
inline uint16_t Load16(const void *p) {
|
||||
return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
|
||||
}
|
||||
|
||||
inline void Store16(void *p, uint16_t v) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
|
||||
}
|
||||
|
||||
inline uint32_t Load32(const void *p) {
|
||||
return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
|
||||
}
|
||||
|
||||
inline void Store32(void *p, uint32_t v) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
|
||||
}
|
||||
|
||||
inline uint64_t Load64(const void *p) {
|
||||
return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
|
||||
}
|
||||
|
||||
inline void Store64(void *p, uint64_t v) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
|
||||
}
|
||||
|
||||
} // namespace little_endian
|
||||
|
||||
// Utilities to convert numbers between the current hosts's native byte
|
||||
// order and big-endian byte order (same as network byte order)
|
||||
//
|
||||
// Load/Store methods are alignment safe
|
||||
namespace big_endian {
|
||||
#ifdef ABSL_IS_LITTLE_ENDIAN
|
||||
|
||||
inline uint16_t FromHost16(uint16_t x) { return gbswap_16(x); }
|
||||
inline uint16_t ToHost16(uint16_t x) { return gbswap_16(x); }
|
||||
|
||||
inline uint32_t FromHost32(uint32_t x) { return gbswap_32(x); }
|
||||
inline uint32_t ToHost32(uint32_t x) { return gbswap_32(x); }
|
||||
|
||||
inline uint64_t FromHost64(uint64_t x) { return gbswap_64(x); }
|
||||
inline uint64_t ToHost64(uint64_t x) { return gbswap_64(x); }
|
||||
|
||||
inline constexpr bool IsLittleEndian() { return true; }
|
||||
|
||||
#elif defined ABSL_IS_BIG_ENDIAN
|
||||
|
||||
inline uint16_t FromHost16(uint16_t x) { return x; }
|
||||
inline uint16_t ToHost16(uint16_t x) { return x; }
|
||||
|
||||
inline uint32_t FromHost32(uint32_t x) { return x; }
|
||||
inline uint32_t ToHost32(uint32_t x) { return x; }
|
||||
|
||||
inline uint64_t FromHost64(uint64_t x) { return x; }
|
||||
inline uint64_t ToHost64(uint64_t x) { return x; }
|
||||
|
||||
inline constexpr bool IsLittleEndian() { return false; }
|
||||
|
||||
#endif /* ENDIAN */
|
||||
|
||||
inline uint8_t FromHost(uint8_t x) { return x; }
|
||||
inline uint16_t FromHost(uint16_t x) { return FromHost16(x); }
|
||||
inline uint32_t FromHost(uint32_t x) { return FromHost32(x); }
|
||||
inline uint64_t FromHost(uint64_t x) { return FromHost64(x); }
|
||||
inline uint8_t ToHost(uint8_t x) { return x; }
|
||||
inline uint16_t ToHost(uint16_t x) { return ToHost16(x); }
|
||||
inline uint32_t ToHost(uint32_t x) { return ToHost32(x); }
|
||||
inline uint64_t ToHost(uint64_t x) { return ToHost64(x); }
|
||||
|
||||
inline int8_t FromHost(int8_t x) { return x; }
|
||||
inline int16_t FromHost(int16_t x) {
|
||||
return bit_cast<int16_t>(FromHost16(bit_cast<uint16_t>(x)));
|
||||
}
|
||||
inline int32_t FromHost(int32_t x) {
|
||||
return bit_cast<int32_t>(FromHost32(bit_cast<uint32_t>(x)));
|
||||
}
|
||||
inline int64_t FromHost(int64_t x) {
|
||||
return bit_cast<int64_t>(FromHost64(bit_cast<uint64_t>(x)));
|
||||
}
|
||||
inline int8_t ToHost(int8_t x) { return x; }
|
||||
inline int16_t ToHost(int16_t x) {
|
||||
return bit_cast<int16_t>(ToHost16(bit_cast<uint16_t>(x)));
|
||||
}
|
||||
inline int32_t ToHost(int32_t x) {
|
||||
return bit_cast<int32_t>(ToHost32(bit_cast<uint32_t>(x)));
|
||||
}
|
||||
inline int64_t ToHost(int64_t x) {
|
||||
return bit_cast<int64_t>(ToHost64(bit_cast<uint64_t>(x)));
|
||||
}
|
||||
|
||||
// Functions to do unaligned loads and stores in big-endian order.
|
||||
inline uint16_t Load16(const void *p) {
|
||||
return ToHost16(ABSL_INTERNAL_UNALIGNED_LOAD16(p));
|
||||
}
|
||||
|
||||
inline void Store16(void *p, uint16_t v) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE16(p, FromHost16(v));
|
||||
}
|
||||
|
||||
inline uint32_t Load32(const void *p) {
|
||||
return ToHost32(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
|
||||
}
|
||||
|
||||
inline void Store32(void *p, uint32_t v) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE32(p, FromHost32(v));
|
||||
}
|
||||
|
||||
inline uint64_t Load64(const void *p) {
|
||||
return ToHost64(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
|
||||
}
|
||||
|
||||
inline void Store64(void *p, uint64_t v) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE64(p, FromHost64(v));
|
||||
}
|
||||
|
||||
} // namespace big_endian
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_ENDIAN_H_
|
|
@ -0,0 +1,263 @@
|
|||
// Copyright 2017 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/base/internal/endian.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace {
|
||||
|
||||
const uint64_t kInitialNumber{0x0123456789abcdef};
|
||||
const uint64_t k64Value{kInitialNumber};
|
||||
const uint32_t k32Value{0x01234567};
|
||||
const uint16_t k16Value{0x0123};
|
||||
const int kNumValuesToTest = 1000000;
|
||||
const int kRandomSeed = 12345;
|
||||
|
||||
#if defined(ABSL_IS_BIG_ENDIAN)
|
||||
const uint64_t kInitialInNetworkOrder{kInitialNumber};
|
||||
const uint64_t k64ValueLE{0xefcdab8967452301};
|
||||
const uint32_t k32ValueLE{0x67452301};
|
||||
const uint16_t k16ValueLE{0x2301};
|
||||
|
||||
const uint64_t k64ValueBE{kInitialNumber};
|
||||
const uint32_t k32ValueBE{k32Value};
|
||||
const uint16_t k16ValueBE{k16Value};
|
||||
#elif defined(ABSL_IS_LITTLE_ENDIAN)
|
||||
const uint64_t kInitialInNetworkOrder{0xefcdab8967452301};
|
||||
const uint64_t k64ValueLE{kInitialNumber};
|
||||
const uint32_t k32ValueLE{k32Value};
|
||||
const uint16_t k16ValueLE{k16Value};
|
||||
|
||||
const uint64_t k64ValueBE{0xefcdab8967452301};
|
||||
const uint32_t k32ValueBE{0x67452301};
|
||||
const uint16_t k16ValueBE{0x2301};
|
||||
#endif
|
||||
|
||||
std::vector<uint16_t> GenerateAllUint16Values() {
|
||||
std::vector<uint16_t> result;
|
||||
result.reserve(size_t{1} << (sizeof(uint16_t) * 8));
|
||||
for (uint32_t i = std::numeric_limits<uint16_t>::min();
|
||||
i <= std::numeric_limits<uint16_t>::max(); ++i) {
|
||||
result.push_back(static_cast<uint16_t>(i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::vector<T> GenerateRandomIntegers(size_t num_values_to_test) {
|
||||
std::vector<T> result;
|
||||
result.reserve(num_values_to_test);
|
||||
std::mt19937_64 rng(kRandomSeed);
|
||||
for (size_t i = 0; i < num_values_to_test; ++i) {
|
||||
result.push_back(rng());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void ManualByteSwap(char* bytes, int length) {
|
||||
if (length == 1)
|
||||
return;
|
||||
|
||||
EXPECT_EQ(0, length % 2);
|
||||
for (int i = 0; i < length / 2; ++i) {
|
||||
int j = (length - 1) - i;
|
||||
using std::swap;
|
||||
swap(bytes[i], bytes[j]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T UnalignedLoad(const char* p) {
|
||||
static_assert(
|
||||
sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
|
||||
"Unexpected type size");
|
||||
|
||||
switch (sizeof(T)) {
|
||||
case 1: return *reinterpret_cast<const T*>(p);
|
||||
case 2:
|
||||
return ABSL_INTERNAL_UNALIGNED_LOAD16(p);
|
||||
case 4:
|
||||
return ABSL_INTERNAL_UNALIGNED_LOAD32(p);
|
||||
case 8:
|
||||
return ABSL_INTERNAL_UNALIGNED_LOAD64(p);
|
||||
default:
|
||||
// Suppresses invalid "not all control paths return a value" on MSVC
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename ByteSwapper>
|
||||
static void GBSwapHelper(const std::vector<T>& host_values_to_test,
|
||||
const ByteSwapper& byte_swapper) {
|
||||
// Test byte_swapper against a manual byte swap.
|
||||
for (typename std::vector<T>::const_iterator it = host_values_to_test.begin();
|
||||
it != host_values_to_test.end(); ++it) {
|
||||
T host_value = *it;
|
||||
|
||||
char actual_value[sizeof(host_value)];
|
||||
memcpy(actual_value, &host_value, sizeof(host_value));
|
||||
byte_swapper(actual_value);
|
||||
|
||||
char expected_value[sizeof(host_value)];
|
||||
memcpy(expected_value, &host_value, sizeof(host_value));
|
||||
ManualByteSwap(expected_value, sizeof(host_value));
|
||||
|
||||
ASSERT_EQ(0, memcmp(actual_value, expected_value, sizeof(host_value)))
|
||||
<< "Swap output for 0x" << std::hex << host_value << " does not match. "
|
||||
<< "Expected: 0x" << UnalignedLoad<T>(expected_value) << "; "
|
||||
<< "actual: 0x" << UnalignedLoad<T>(actual_value);
|
||||
}
|
||||
}
|
||||
|
||||
void Swap16(char* bytes) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE16(
|
||||
bytes, gbswap_16(ABSL_INTERNAL_UNALIGNED_LOAD16(bytes)));
|
||||
}
|
||||
|
||||
void Swap32(char* bytes) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE32(
|
||||
bytes, gbswap_32(ABSL_INTERNAL_UNALIGNED_LOAD32(bytes)));
|
||||
}
|
||||
|
||||
void Swap64(char* bytes) {
|
||||
ABSL_INTERNAL_UNALIGNED_STORE64(
|
||||
bytes, gbswap_64(ABSL_INTERNAL_UNALIGNED_LOAD64(bytes)));
|
||||
}
|
||||
|
||||
TEST(EndianessTest, Uint16) {
|
||||
GBSwapHelper(GenerateAllUint16Values(), &Swap16);
|
||||
}
|
||||
|
||||
TEST(EndianessTest, Uint32) {
|
||||
GBSwapHelper(GenerateRandomIntegers<uint32_t>(kNumValuesToTest), &Swap32);
|
||||
}
|
||||
|
||||
TEST(EndianessTest, Uint64) {
|
||||
GBSwapHelper(GenerateRandomIntegers<uint64_t>(kNumValuesToTest), &Swap64);
|
||||
}
|
||||
|
||||
TEST(EndianessTest, ghtonll_gntohll) {
|
||||
// Test that absl::ghtonl compiles correctly
|
||||
uint32_t test = 0x01234567;
|
||||
EXPECT_EQ(absl::gntohl(absl::ghtonl(test)), test);
|
||||
|
||||
uint64_t comp = absl::ghtonll(kInitialNumber);
|
||||
EXPECT_EQ(comp, kInitialInNetworkOrder);
|
||||
comp = absl::gntohll(kInitialInNetworkOrder);
|
||||
EXPECT_EQ(comp, kInitialNumber);
|
||||
|
||||
// Test that htonll and ntohll are each others' inverse functions on a
|
||||
// somewhat assorted batch of numbers. 37 is chosen to not be anything
|
||||
// particularly nice base 2.
|
||||
uint64_t value = 1;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
comp = absl::ghtonll(absl::gntohll(value));
|
||||
EXPECT_EQ(value, comp);
|
||||
comp = absl::gntohll(absl::ghtonll(value));
|
||||
EXPECT_EQ(value, comp);
|
||||
value *= 37;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EndianessTest, little_endian) {
|
||||
// Check little_endian uint16_t.
|
||||
uint64_t comp = little_endian::FromHost16(k16Value);
|
||||
EXPECT_EQ(comp, k16ValueLE);
|
||||
comp = little_endian::ToHost16(k16ValueLE);
|
||||
EXPECT_EQ(comp, k16Value);
|
||||
|
||||
// Check little_endian uint32_t.
|
||||
comp = little_endian::FromHost32(k32Value);
|
||||
EXPECT_EQ(comp, k32ValueLE);
|
||||
comp = little_endian::ToHost32(k32ValueLE);
|
||||
EXPECT_EQ(comp, k32Value);
|
||||
|
||||
// Check little_endian uint64_t.
|
||||
comp = little_endian::FromHost64(k64Value);
|
||||
EXPECT_EQ(comp, k64ValueLE);
|
||||
comp = little_endian::ToHost64(k64ValueLE);
|
||||
EXPECT_EQ(comp, k64Value);
|
||||
|
||||
// Check little-endian Load and store functions.
|
||||
uint16_t u16Buf;
|
||||
uint32_t u32Buf;
|
||||
uint64_t u64Buf;
|
||||
|
||||
little_endian::Store16(&u16Buf, k16Value);
|
||||
EXPECT_EQ(u16Buf, k16ValueLE);
|
||||
comp = little_endian::Load16(&u16Buf);
|
||||
EXPECT_EQ(comp, k16Value);
|
||||
|
||||
little_endian::Store32(&u32Buf, k32Value);
|
||||
EXPECT_EQ(u32Buf, k32ValueLE);
|
||||
comp = little_endian::Load32(&u32Buf);
|
||||
EXPECT_EQ(comp, k32Value);
|
||||
|
||||
little_endian::Store64(&u64Buf, k64Value);
|
||||
EXPECT_EQ(u64Buf, k64ValueLE);
|
||||
comp = little_endian::Load64(&u64Buf);
|
||||
EXPECT_EQ(comp, k64Value);
|
||||
}
|
||||
|
||||
TEST(EndianessTest, big_endian) {
|
||||
// Check big-endian Load and store functions.
|
||||
uint16_t u16Buf;
|
||||
uint32_t u32Buf;
|
||||
uint64_t u64Buf;
|
||||
|
||||
unsigned char buffer[10];
|
||||
big_endian::Store16(&u16Buf, k16Value);
|
||||
EXPECT_EQ(u16Buf, k16ValueBE);
|
||||
uint64_t comp = big_endian::Load16(&u16Buf);
|
||||
EXPECT_EQ(comp, k16Value);
|
||||
|
||||
big_endian::Store32(&u32Buf, k32Value);
|
||||
EXPECT_EQ(u32Buf, k32ValueBE);
|
||||
comp = big_endian::Load32(&u32Buf);
|
||||
EXPECT_EQ(comp, k32Value);
|
||||
|
||||
big_endian::Store64(&u64Buf, k64Value);
|
||||
EXPECT_EQ(u64Buf, k64ValueBE);
|
||||
comp = big_endian::Load64(&u64Buf);
|
||||
EXPECT_EQ(comp, k64Value);
|
||||
|
||||
big_endian::Store16(buffer + 1, k16Value);
|
||||
EXPECT_EQ(u16Buf, k16ValueBE);
|
||||
comp = big_endian::Load16(buffer + 1);
|
||||
EXPECT_EQ(comp, k16Value);
|
||||
|
||||
big_endian::Store32(buffer + 1, k32Value);
|
||||
EXPECT_EQ(u32Buf, k32ValueBE);
|
||||
comp = big_endian::Load32(buffer + 1);
|
||||
EXPECT_EQ(comp, k32Value);
|
||||
|
||||
big_endian::Store64(buffer + 1, k64Value);
|
||||
EXPECT_EQ(u64Buf, k64ValueBE);
|
||||
comp = big_endian::Load64(buffer + 1);
|
||||
EXPECT_EQ(comp, k64Value);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_ERRNO_SAVER_H_
|
||||
#define ABSL_BASE_INTERNAL_ERRNO_SAVER_H_
|
||||
|
||||
#include <cerrno>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// `ErrnoSaver` captures the value of `errno` upon construction and restores it
|
||||
// upon deletion. It is used in low-level code and must be super fast. Do not
|
||||
// add instrumentation, even in debug modes.
|
||||
class ErrnoSaver {
|
||||
public:
|
||||
ErrnoSaver() : saved_errno_(errno) {}
|
||||
~ErrnoSaver() { errno = saved_errno_; }
|
||||
int operator()() const { return saved_errno_; }
|
||||
|
||||
private:
|
||||
const int saved_errno_;
|
||||
};
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_ERRNO_SAVER_H_
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright 2018 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/base/internal/errno_saver.h"
|
||||
|
||||
#include <cerrno>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/internal/strerror.h"
|
||||
|
||||
namespace {
|
||||
using ::testing::Eq;
|
||||
|
||||
struct ErrnoPrinter {
|
||||
int no;
|
||||
};
|
||||
std::ostream &operator<<(std::ostream &os, ErrnoPrinter ep) {
|
||||
return os << absl::base_internal::StrError(ep.no) << " [" << ep.no << "]";
|
||||
}
|
||||
bool operator==(ErrnoPrinter one, ErrnoPrinter two) { return one.no == two.no; }
|
||||
|
||||
TEST(ErrnoSaverTest, Works) {
|
||||
errno = EDOM;
|
||||
{
|
||||
absl::base_internal::ErrnoSaver errno_saver;
|
||||
EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{EDOM}));
|
||||
errno = ERANGE;
|
||||
EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{ERANGE}));
|
||||
EXPECT_THAT(ErrnoPrinter{errno_saver()}, Eq(ErrnoPrinter{EDOM}));
|
||||
}
|
||||
EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{EDOM}));
|
||||
}
|
||||
} // namespace
|
|
@ -0,0 +1,79 @@
|
|||
// Copyright 2017 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/base/internal/exception_safety_testing.h"
|
||||
|
||||
#ifdef ABSL_HAVE_EXCEPTIONS
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/meta/type_traits.h"
|
||||
|
||||
namespace testing {
|
||||
|
||||
exceptions_internal::NoThrowTag nothrow_ctor;
|
||||
|
||||
exceptions_internal::StrongGuaranteeTagType strong_guarantee;
|
||||
|
||||
exceptions_internal::ExceptionSafetyTestBuilder<> MakeExceptionSafetyTester() {
|
||||
return {};
|
||||
}
|
||||
|
||||
namespace exceptions_internal {
|
||||
|
||||
int countdown = -1;
|
||||
|
||||
ConstructorTracker* ConstructorTracker::current_tracker_instance_ = nullptr;
|
||||
|
||||
void MaybeThrow(absl::string_view msg, bool throw_bad_alloc) {
|
||||
if (countdown-- == 0) {
|
||||
if (throw_bad_alloc) throw TestBadAllocException(msg);
|
||||
throw TestException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
testing::AssertionResult FailureMessage(const TestException& e,
|
||||
int countdown) noexcept {
|
||||
return testing::AssertionFailure() << "Exception thrown from " << e.what();
|
||||
}
|
||||
|
||||
std::string GetSpecString(TypeSpec spec) {
|
||||
std::string out;
|
||||
absl::string_view sep;
|
||||
const auto append = [&](absl::string_view s) {
|
||||
absl::StrAppend(&out, sep, s);
|
||||
sep = " | ";
|
||||
};
|
||||
if (static_cast<bool>(TypeSpec::kNoThrowCopy & spec)) {
|
||||
append("kNoThrowCopy");
|
||||
}
|
||||
if (static_cast<bool>(TypeSpec::kNoThrowMove & spec)) {
|
||||
append("kNoThrowMove");
|
||||
}
|
||||
if (static_cast<bool>(TypeSpec::kNoThrowNew & spec)) {
|
||||
append("kNoThrowNew");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string GetSpecString(AllocSpec spec) {
|
||||
return static_cast<bool>(AllocSpec::kNoThrowAllocate & spec)
|
||||
? "kNoThrowAllocate"
|
||||
: "";
|
||||
}
|
||||
|
||||
} // namespace exceptions_internal
|
||||
|
||||
} // namespace testing
|
||||
|
||||
#endif // ABSL_HAVE_EXCEPTIONS
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,42 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// Testing utilities for ABSL types which throw exceptions.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_EXCEPTION_TESTING_H_
|
||||
#define ABSL_BASE_INTERNAL_EXCEPTION_TESTING_H_
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/config.h"
|
||||
|
||||
// ABSL_BASE_INTERNAL_EXPECT_FAIL tests either for a specified thrown exception
|
||||
// if exceptions are enabled, or for death with a specified text in the error
|
||||
// message
|
||||
#ifdef ABSL_HAVE_EXCEPTIONS
|
||||
|
||||
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text) \
|
||||
EXPECT_THROW(expr, exception_t)
|
||||
|
||||
#elif defined(__ANDROID__)
|
||||
// Android asserts do not log anywhere that gtest can currently inspect.
|
||||
// So we expect exit, but cannot match the message.
|
||||
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text) \
|
||||
EXPECT_DEATH(expr, ".*")
|
||||
#else
|
||||
#define ABSL_BASE_INTERNAL_EXPECT_FAIL(expr, exception_t, text) \
|
||||
EXPECT_DEATH_IF_SUPPORTED(expr, text)
|
||||
|
||||
#endif
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_EXCEPTION_TESTING_H_
|
|
@ -0,0 +1,93 @@
|
|||
// Copyright 2019 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/base/internal/exponential_biased.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/optimization.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// The algorithm generates a random number between 0 and 1 and applies the
|
||||
// inverse cumulative distribution function for an exponential. Specifically:
|
||||
// Let m be the inverse of the sample period, then the probability
|
||||
// distribution function is m*exp(-mx) so the CDF is
|
||||
// p = 1 - exp(-mx), so
|
||||
// q = 1 - p = exp(-mx)
|
||||
// log_e(q) = -mx
|
||||
// -log_e(q)/m = x
|
||||
// log_2(q) * (-log_e(2) * 1/m) = x
|
||||
// In the code, q is actually in the range 1 to 2**26, hence the -26 below
|
||||
int64_t ExponentialBiased::GetSkipCount(int64_t mean) {
|
||||
if (ABSL_PREDICT_FALSE(!initialized_)) {
|
||||
Initialize();
|
||||
}
|
||||
|
||||
uint64_t rng = NextRandom(rng_);
|
||||
rng_ = rng;
|
||||
|
||||
// Take the top 26 bits as the random number
|
||||
// (This plus the 1<<58 sampling bound give a max possible step of
|
||||
// 5194297183973780480 bytes.)
|
||||
// The uint32_t cast is to prevent a (hard-to-reproduce) NAN
|
||||
// under piii debug for some binaries.
|
||||
double q = static_cast<uint32_t>(rng >> (kPrngNumBits - 26)) + 1.0;
|
||||
// Put the computed p-value through the CDF of a geometric.
|
||||
double interval = bias_ + (std::log2(q) - 26) * (-std::log(2.0) * mean);
|
||||
// Very large values of interval overflow int64_t. To avoid that, we will
|
||||
// cheat and clamp any huge values to (int64_t max)/2. This is a potential
|
||||
// source of bias, but the mean would need to be such a large value that it's
|
||||
// not likely to come up. For example, with a mean of 1e18, the probability of
|
||||
// hitting this condition is about 1/1000. For a mean of 1e17, standard
|
||||
// calculators claim that this event won't happen.
|
||||
if (interval > static_cast<double>(std::numeric_limits<int64_t>::max() / 2)) {
|
||||
// Assume huge values are bias neutral, retain bias for next call.
|
||||
return std::numeric_limits<int64_t>::max() / 2;
|
||||
}
|
||||
double value = std::rint(interval);
|
||||
bias_ = interval - value;
|
||||
return value;
|
||||
}
|
||||
|
||||
int64_t ExponentialBiased::GetStride(int64_t mean) {
|
||||
return GetSkipCount(mean - 1) + 1;
|
||||
}
|
||||
|
||||
void ExponentialBiased::Initialize() {
|
||||
// We don't get well distributed numbers from `this` so we call NextRandom() a
|
||||
// bunch to mush the bits around. We use a global_rand to handle the case
|
||||
// where the same thread (by memory address) gets created and destroyed
|
||||
// repeatedly.
|
||||
ABSL_CONST_INIT static std::atomic<uint32_t> global_rand(0);
|
||||
uint64_t r = reinterpret_cast<uint64_t>(this) +
|
||||
global_rand.fetch_add(1, std::memory_order_relaxed);
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
r = NextRandom(r);
|
||||
}
|
||||
rng_ = r;
|
||||
initialized_ = true;
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,130 @@
|
|||
// Copyright 2019 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_EXPONENTIAL_BIASED_H_
|
||||
#define ABSL_BASE_INTERNAL_EXPONENTIAL_BIASED_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/macros.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// ExponentialBiased provides a small and fast random number generator for a
|
||||
// rounded exponential distribution. This generator manages very little state,
|
||||
// and imposes no synchronization overhead. This makes it useful in specialized
|
||||
// scenarios requiring minimum overhead, such as stride based periodic sampling.
|
||||
//
|
||||
// ExponentialBiased provides two closely related functions, GetSkipCount() and
|
||||
// GetStride(), both returning a rounded integer defining a number of events
|
||||
// required before some event with a given mean probability occurs.
|
||||
//
|
||||
// The distribution is useful to generate a random wait time or some periodic
|
||||
// event with a given mean probability. For example, if an action is supposed to
|
||||
// happen on average once every 'N' events, then we can get a random 'stride'
|
||||
// counting down how long before the event to happen. For example, if we'd want
|
||||
// to sample one in every 1000 'Frobber' calls, our code could look like this:
|
||||
//
|
||||
// Frobber::Frobber() {
|
||||
// stride_ = exponential_biased_.GetStride(1000);
|
||||
// }
|
||||
//
|
||||
// void Frobber::Frob(int arg) {
|
||||
// if (--stride == 0) {
|
||||
// SampleFrob(arg);
|
||||
// stride_ = exponential_biased_.GetStride(1000);
|
||||
// }
|
||||
// ...
|
||||
// }
|
||||
//
|
||||
// The rounding of the return value creates a bias, especially for smaller means
|
||||
// where the distribution of the fraction is not evenly distributed. We correct
|
||||
// this bias by tracking the fraction we rounded up or down on each iteration,
|
||||
// effectively tracking the distance between the cumulative value, and the
|
||||
// rounded cumulative value. For example, given a mean of 2:
|
||||
//
|
||||
// raw = 1.63076, cumulative = 1.63076, rounded = 2, bias = -0.36923
|
||||
// raw = 0.14624, cumulative = 1.77701, rounded = 2, bias = 0.14624
|
||||
// raw = 4.93194, cumulative = 6.70895, rounded = 7, bias = -0.06805
|
||||
// raw = 0.24206, cumulative = 6.95101, rounded = 7, bias = 0.24206
|
||||
// etc...
|
||||
//
|
||||
// Adjusting with rounding bias is relatively trivial:
|
||||
//
|
||||
// double value = bias_ + exponential_distribution(mean)();
|
||||
// double rounded_value = std::rint(value);
|
||||
// bias_ = value - rounded_value;
|
||||
// return rounded_value;
|
||||
//
|
||||
// This class is thread-compatible.
|
||||
class ExponentialBiased {
|
||||
public:
|
||||
// The number of bits set by NextRandom.
|
||||
static constexpr int kPrngNumBits = 48;
|
||||
|
||||
// `GetSkipCount()` returns the number of events to skip before some chosen
|
||||
// event happens. For example, randomly tossing a coin, we will on average
|
||||
// throw heads once before we get tails. We can simulate random coin tosses
|
||||
// using GetSkipCount() as:
|
||||
//
|
||||
// ExponentialBiased eb;
|
||||
// for (...) {
|
||||
// int number_of_heads_before_tail = eb.GetSkipCount(1);
|
||||
// for (int flips = 0; flips < number_of_heads_before_tail; ++flips) {
|
||||
// printf("head...");
|
||||
// }
|
||||
// printf("tail\n");
|
||||
// }
|
||||
//
|
||||
int64_t GetSkipCount(int64_t mean);
|
||||
|
||||
// GetStride() returns the number of events required for a specific event to
|
||||
// happen. See the class comments for a usage example. `GetStride()` is
|
||||
// equivalent to `GetSkipCount(mean - 1) + 1`. When to use `GetStride()` or
|
||||
// `GetSkipCount()` depends mostly on what best fits the use case.
|
||||
int64_t GetStride(int64_t mean);
|
||||
|
||||
// Computes a random number in the range [0, 1<<(kPrngNumBits+1) - 1]
|
||||
//
|
||||
// This is public to enable testing.
|
||||
static uint64_t NextRandom(uint64_t rnd);
|
||||
|
||||
private:
|
||||
void Initialize();
|
||||
|
||||
uint64_t rng_{0};
|
||||
double bias_{0};
|
||||
bool initialized_{false};
|
||||
};
|
||||
|
||||
// Returns the next prng value.
|
||||
// pRNG is: aX+b mod c with a = 0x5DEECE66D, b = 0xB, c = 1<<48
|
||||
// This is the lrand64 generator.
|
||||
inline uint64_t ExponentialBiased::NextRandom(uint64_t rnd) {
|
||||
const uint64_t prng_mult = uint64_t{0x5DEECE66D};
|
||||
const uint64_t prng_add = 0xB;
|
||||
const uint64_t prng_mod_power = 48;
|
||||
const uint64_t prng_mod_mask =
|
||||
~((~static_cast<uint64_t>(0)) << prng_mod_power);
|
||||
return (prng_mult * rnd + prng_add) & prng_mod_mask;
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_EXPONENTIAL_BIASED_H_
|
|
@ -0,0 +1,199 @@
|
|||
// Copyright 2019 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/base/internal/exponential_biased.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
|
||||
using ::testing::Ge;
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
MATCHER_P2(IsBetween, a, b,
|
||||
absl::StrCat(std::string(negation ? "isn't" : "is"), " between ", a,
|
||||
" and ", b)) {
|
||||
return a <= arg && arg <= b;
|
||||
}
|
||||
|
||||
// Tests of the quality of the random numbers generated
|
||||
// This uses the Anderson Darling test for uniformity.
|
||||
// See "Evaluating the Anderson-Darling Distribution" by Marsaglia
|
||||
// for details.
|
||||
|
||||
// Short cut version of ADinf(z), z>0 (from Marsaglia)
|
||||
// This returns the p-value for Anderson Darling statistic in
|
||||
// the limit as n-> infinity. For finite n, apply the error fix below.
|
||||
double AndersonDarlingInf(double z) {
|
||||
if (z < 2) {
|
||||
return exp(-1.2337141 / z) / sqrt(z) *
|
||||
(2.00012 +
|
||||
(0.247105 -
|
||||
(0.0649821 - (0.0347962 - (0.011672 - 0.00168691 * z) * z) * z) *
|
||||
z) *
|
||||
z);
|
||||
}
|
||||
return exp(
|
||||
-exp(1.0776 -
|
||||
(2.30695 -
|
||||
(0.43424 - (0.082433 - (0.008056 - 0.0003146 * z) * z) * z) * z) *
|
||||
z));
|
||||
}
|
||||
|
||||
// Corrects the approximation error in AndersonDarlingInf for small values of n
|
||||
// Add this to AndersonDarlingInf to get a better approximation
|
||||
// (from Marsaglia)
|
||||
double AndersonDarlingErrFix(int n, double x) {
|
||||
if (x > 0.8) {
|
||||
return (-130.2137 +
|
||||
(745.2337 -
|
||||
(1705.091 - (1950.646 - (1116.360 - 255.7844 * x) * x) * x) * x) *
|
||||
x) /
|
||||
n;
|
||||
}
|
||||
double cutoff = 0.01265 + 0.1757 / n;
|
||||
if (x < cutoff) {
|
||||
double t = x / cutoff;
|
||||
t = sqrt(t) * (1 - t) * (49 * t - 102);
|
||||
return t * (0.0037 / (n * n) + 0.00078 / n + 0.00006) / n;
|
||||
} else {
|
||||
double t = (x - cutoff) / (0.8 - cutoff);
|
||||
t = -0.00022633 +
|
||||
(6.54034 - (14.6538 - (14.458 - (8.259 - 1.91864 * t) * t) * t) * t) *
|
||||
t;
|
||||
return t * (0.04213 + 0.01365 / n) / n;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the AndersonDarling p-value given n and the value of the statistic
|
||||
double AndersonDarlingPValue(int n, double z) {
|
||||
double ad = AndersonDarlingInf(z);
|
||||
double errfix = AndersonDarlingErrFix(n, ad);
|
||||
return ad + errfix;
|
||||
}
|
||||
|
||||
double AndersonDarlingStatistic(const std::vector<double>& random_sample) {
|
||||
int n = random_sample.size();
|
||||
double ad_sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
ad_sum += (2 * i + 1) *
|
||||
std::log(random_sample[i] * (1 - random_sample[n - 1 - i]));
|
||||
}
|
||||
double ad_statistic = -n - 1 / static_cast<double>(n) * ad_sum;
|
||||
return ad_statistic;
|
||||
}
|
||||
|
||||
// Tests if the array of doubles is uniformly distributed.
|
||||
// Returns the p-value of the Anderson Darling Statistic
|
||||
// for the given set of sorted random doubles
|
||||
// See "Evaluating the Anderson-Darling Distribution" by
|
||||
// Marsaglia and Marsaglia for details.
|
||||
double AndersonDarlingTest(const std::vector<double>& random_sample) {
|
||||
double ad_statistic = AndersonDarlingStatistic(random_sample);
|
||||
double p = AndersonDarlingPValue(random_sample.size(), ad_statistic);
|
||||
return p;
|
||||
}
|
||||
|
||||
TEST(ExponentialBiasedTest, CoinTossDemoWithGetSkipCount) {
|
||||
ExponentialBiased eb;
|
||||
for (int runs = 0; runs < 10; ++runs) {
|
||||
for (int flips = eb.GetSkipCount(1); flips > 0; --flips) {
|
||||
printf("head...");
|
||||
}
|
||||
printf("tail\n");
|
||||
}
|
||||
int heads = 0;
|
||||
for (int i = 0; i < 10000000; i += 1 + eb.GetSkipCount(1)) {
|
||||
++heads;
|
||||
}
|
||||
printf("Heads = %d (%f%%)\n", heads, 100.0 * heads / 10000000);
|
||||
}
|
||||
|
||||
TEST(ExponentialBiasedTest, SampleDemoWithStride) {
|
||||
ExponentialBiased eb;
|
||||
int stride = eb.GetStride(10);
|
||||
int samples = 0;
|
||||
for (int i = 0; i < 10000000; ++i) {
|
||||
if (--stride == 0) {
|
||||
++samples;
|
||||
stride = eb.GetStride(10);
|
||||
}
|
||||
}
|
||||
printf("Samples = %d (%f%%)\n", samples, 100.0 * samples / 10000000);
|
||||
}
|
||||
|
||||
|
||||
// Testing that NextRandom generates uniform random numbers. Applies the
|
||||
// Anderson-Darling test for uniformity
|
||||
TEST(ExponentialBiasedTest, TestNextRandom) {
|
||||
for (auto n : std::vector<int>({
|
||||
10, // Check short-range correlation
|
||||
100, 1000,
|
||||
10000 // Make sure there's no systemic error
|
||||
})) {
|
||||
uint64_t x = 1;
|
||||
// This assumes that the prng returns 48 bit numbers
|
||||
uint64_t max_prng_value = static_cast<uint64_t>(1) << 48;
|
||||
// Initialize.
|
||||
for (int i = 1; i <= 20; i++) {
|
||||
x = ExponentialBiased::NextRandom(x);
|
||||
}
|
||||
std::vector<uint64_t> int_random_sample(n);
|
||||
// Collect samples
|
||||
for (int i = 0; i < n; i++) {
|
||||
int_random_sample[i] = x;
|
||||
x = ExponentialBiased::NextRandom(x);
|
||||
}
|
||||
// First sort them...
|
||||
std::sort(int_random_sample.begin(), int_random_sample.end());
|
||||
std::vector<double> random_sample(n);
|
||||
// Convert them to uniform randoms (in the range [0,1])
|
||||
for (int i = 0; i < n; i++) {
|
||||
random_sample[i] =
|
||||
static_cast<double>(int_random_sample[i]) / max_prng_value;
|
||||
}
|
||||
// Now compute the Anderson-Darling statistic
|
||||
double ad_pvalue = AndersonDarlingTest(random_sample);
|
||||
EXPECT_GT(std::min(ad_pvalue, 1 - ad_pvalue), 0.0001)
|
||||
<< "prng is not uniform: n = " << n << " p = " << ad_pvalue;
|
||||
}
|
||||
}
|
||||
|
||||
// The generator needs to be available as a thread_local and as a static
|
||||
// variable.
|
||||
TEST(ExponentialBiasedTest, InitializationModes) {
|
||||
ABSL_CONST_INIT static ExponentialBiased eb_static;
|
||||
EXPECT_THAT(eb_static.GetSkipCount(2), Ge(0));
|
||||
|
||||
#ifdef ABSL_HAVE_THREAD_LOCAL
|
||||
thread_local ExponentialBiased eb_thread;
|
||||
EXPECT_THAT(eb_thread.GetSkipCount(2), Ge(0));
|
||||
#endif
|
||||
|
||||
ExponentialBiased eb_stack;
|
||||
EXPECT_THAT(eb_stack.GetSkipCount(2), Ge(0));
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,48 @@
|
|||
//
|
||||
// Copyright 2020 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.
|
||||
//
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_FAST_TYPE_ID_H_
|
||||
#define ABSL_BASE_INTERNAL_FAST_TYPE_ID_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
template <typename Type>
|
||||
struct FastTypeTag {
|
||||
constexpr static char dummy_var = 0;
|
||||
};
|
||||
|
||||
template <typename Type>
|
||||
constexpr char FastTypeTag<Type>::dummy_var;
|
||||
|
||||
// FastTypeId<Type>() evaluates at compile/link-time to a unique pointer for the
|
||||
// passed-in type. These are meant to be good match for keys into maps or
|
||||
// straight up comparisons.
|
||||
using FastTypeIdType = const void*;
|
||||
|
||||
template <typename Type>
|
||||
constexpr inline FastTypeIdType FastTypeId() {
|
||||
return &FastTypeTag<Type>::dummy_var;
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_FAST_TYPE_ID_H_
|
|
@ -0,0 +1,123 @@
|
|||
// Copyright 2020 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/base/internal/fast_type_id.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace {
|
||||
namespace bi = absl::base_internal;
|
||||
|
||||
// NOLINTNEXTLINE
|
||||
#define PRIM_TYPES(A) \
|
||||
A(bool) \
|
||||
A(short) \
|
||||
A(unsigned short) \
|
||||
A(int) \
|
||||
A(unsigned int) \
|
||||
A(long) \
|
||||
A(unsigned long) \
|
||||
A(long long) \
|
||||
A(unsigned long long) \
|
||||
A(float) \
|
||||
A(double) \
|
||||
A(long double)
|
||||
|
||||
TEST(FastTypeIdTest, PrimitiveTypes) {
|
||||
bi::FastTypeIdType type_ids[] = {
|
||||
#define A(T) bi::FastTypeId<T>(),
|
||||
PRIM_TYPES(A)
|
||||
#undef A
|
||||
#define A(T) bi::FastTypeId<const T>(),
|
||||
PRIM_TYPES(A)
|
||||
#undef A
|
||||
#define A(T) bi::FastTypeId<volatile T>(),
|
||||
PRIM_TYPES(A)
|
||||
#undef A
|
||||
#define A(T) bi::FastTypeId<const volatile T>(),
|
||||
PRIM_TYPES(A)
|
||||
#undef A
|
||||
};
|
||||
size_t total_type_ids = sizeof(type_ids) / sizeof(bi::FastTypeIdType);
|
||||
|
||||
for (int i = 0; i < total_type_ids; ++i) {
|
||||
EXPECT_EQ(type_ids[i], type_ids[i]);
|
||||
for (int j = 0; j < i; ++j) {
|
||||
EXPECT_NE(type_ids[i], type_ids[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define FIXED_WIDTH_TYPES(A) \
|
||||
A(int8_t) \
|
||||
A(uint8_t) \
|
||||
A(int16_t) \
|
||||
A(uint16_t) \
|
||||
A(int32_t) \
|
||||
A(uint32_t) \
|
||||
A(int64_t) \
|
||||
A(uint64_t)
|
||||
|
||||
TEST(FastTypeIdTest, FixedWidthTypes) {
|
||||
bi::FastTypeIdType type_ids[] = {
|
||||
#define A(T) bi::FastTypeId<T>(),
|
||||
FIXED_WIDTH_TYPES(A)
|
||||
#undef A
|
||||
#define A(T) bi::FastTypeId<const T>(),
|
||||
FIXED_WIDTH_TYPES(A)
|
||||
#undef A
|
||||
#define A(T) bi::FastTypeId<volatile T>(),
|
||||
FIXED_WIDTH_TYPES(A)
|
||||
#undef A
|
||||
#define A(T) bi::FastTypeId<const volatile T>(),
|
||||
FIXED_WIDTH_TYPES(A)
|
||||
#undef A
|
||||
};
|
||||
size_t total_type_ids = sizeof(type_ids) / sizeof(bi::FastTypeIdType);
|
||||
|
||||
for (int i = 0; i < total_type_ids; ++i) {
|
||||
EXPECT_EQ(type_ids[i], type_ids[i]);
|
||||
for (int j = 0; j < i; ++j) {
|
||||
EXPECT_NE(type_ids[i], type_ids[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(FastTypeIdTest, AliasTypes) {
|
||||
using int_alias = int;
|
||||
EXPECT_EQ(bi::FastTypeId<int_alias>(), bi::FastTypeId<int>());
|
||||
}
|
||||
|
||||
TEST(FastTypeIdTest, TemplateSpecializations) {
|
||||
EXPECT_NE(bi::FastTypeId<std::vector<int>>(),
|
||||
bi::FastTypeId<std::vector<long>>());
|
||||
|
||||
EXPECT_NE((bi::FastTypeId<std::map<int, float>>()),
|
||||
(bi::FastTypeId<std::map<int, double>>()));
|
||||
}
|
||||
|
||||
struct Base {};
|
||||
struct Derived : Base {};
|
||||
struct PDerived : private Base {};
|
||||
|
||||
TEST(FastTypeIdTest, Inheritance) {
|
||||
EXPECT_NE(bi::FastTypeId<Base>(), bi::FastTypeId<Derived>());
|
||||
EXPECT_NE(bi::FastTypeId<Base>(), bi::FastTypeId<PDerived>());
|
||||
}
|
||||
|
||||
} // namespace
|
|
@ -0,0 +1,51 @@
|
|||
// Copyright 2018 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_HIDE_PTR_H_
|
||||
#define ABSL_BASE_INTERNAL_HIDE_PTR_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// Arbitrary value with high bits set. Xor'ing with it is unlikely
|
||||
// to map one valid pointer to another valid pointer.
|
||||
constexpr uintptr_t HideMask() {
|
||||
return (uintptr_t{0xF03A5F7BU} << (sizeof(uintptr_t) - 4) * 8) | 0xF03A5F7BU;
|
||||
}
|
||||
|
||||
// Hide a pointer from the leak checker. For internal use only.
|
||||
// Differs from absl::IgnoreLeak(ptr) in that absl::IgnoreLeak(ptr) causes ptr
|
||||
// and all objects reachable from ptr to be ignored by the leak checker.
|
||||
template <class T>
|
||||
inline uintptr_t HidePtr(T* ptr) {
|
||||
return reinterpret_cast<uintptr_t>(ptr) ^ HideMask();
|
||||
}
|
||||
|
||||
// Return a pointer that has been hidden from the leak checker.
|
||||
// For internal use only.
|
||||
template <class T>
|
||||
inline T* UnhidePtr(uintptr_t hidden) {
|
||||
return reinterpret_cast<T*>(hidden ^ HideMask());
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_HIDE_PTR_H_
|
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_IDENTITY_H_
|
||||
#define ABSL_BASE_INTERNAL_IDENTITY_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace internal {
|
||||
|
||||
template <typename T>
|
||||
struct identity {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using identity_t = typename identity<T>::type;
|
||||
|
||||
} // namespace internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_IDENTITY_H_
|
|
@ -0,0 +1,107 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
|
||||
#define ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/internal/identity.h"
|
||||
|
||||
// File:
|
||||
// This file define a macro that allows the creation of or emulation of C++17
|
||||
// inline variables based on whether or not the feature is supported.
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Macro: ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init)
|
||||
//
|
||||
// Description:
|
||||
// Expands to the equivalent of an inline constexpr instance of the specified
|
||||
// `type` and `name`, initialized to the value `init`. If the compiler being
|
||||
// used is detected as supporting actual inline variables as a language
|
||||
// feature, then the macro expands to an actual inline variable definition.
|
||||
//
|
||||
// Requires:
|
||||
// `type` is a type that is usable in an extern variable declaration.
|
||||
//
|
||||
// Requires: `name` is a valid identifier
|
||||
//
|
||||
// Requires:
|
||||
// `init` is an expression that can be used in the following definition:
|
||||
// constexpr type name = init;
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// // Equivalent to: `inline constexpr size_t variant_npos = -1;`
|
||||
// ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1);
|
||||
//
|
||||
// Differences in implementation:
|
||||
// For a direct, language-level inline variable, decltype(name) will be the
|
||||
// type that was specified along with const qualification, whereas for
|
||||
// emulated inline variables, decltype(name) may be different (in practice
|
||||
// it will likely be a reference type).
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __cpp_inline_variables
|
||||
|
||||
// Clang's -Wmissing-variable-declarations option erroneously warned that
|
||||
// inline constexpr objects need to be pre-declared. This has now been fixed,
|
||||
// but we will need to support this workaround for people building with older
|
||||
// versions of clang.
|
||||
//
|
||||
// Bug: https://bugs.llvm.org/show_bug.cgi?id=35862
|
||||
//
|
||||
// Note:
|
||||
// identity_t is used here so that the const and name are in the
|
||||
// appropriate place for pointer types, reference types, function pointer
|
||||
// types, etc..
|
||||
#if defined(__clang__)
|
||||
#define ABSL_INTERNAL_EXTERN_DECL(type, name) \
|
||||
extern const ::absl::internal::identity_t<type> name;
|
||||
#else // Otherwise, just define the macro to do nothing.
|
||||
#define ABSL_INTERNAL_EXTERN_DECL(type, name)
|
||||
#endif // defined(__clang__)
|
||||
|
||||
// See above comment at top of file for details.
|
||||
#define ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) \
|
||||
ABSL_INTERNAL_EXTERN_DECL(type, name) \
|
||||
inline constexpr ::absl::internal::identity_t<type> name = init
|
||||
|
||||
#else
|
||||
|
||||
// See above comment at top of file for details.
|
||||
//
|
||||
// Note:
|
||||
// identity_t is used here so that the const and name are in the
|
||||
// appropriate place for pointer types, reference types, function pointer
|
||||
// types, etc..
|
||||
#define ABSL_INTERNAL_INLINE_CONSTEXPR(var_type, name, init) \
|
||||
template <class /*AbslInternalDummy*/ = void> \
|
||||
struct AbslInternalInlineVariableHolder##name { \
|
||||
static constexpr ::absl::internal::identity_t<var_type> kInstance = init; \
|
||||
}; \
|
||||
\
|
||||
template <class AbslInternalDummy> \
|
||||
constexpr ::absl::internal::identity_t<var_type> \
|
||||
AbslInternalInlineVariableHolder##name<AbslInternalDummy>::kInstance; \
|
||||
\
|
||||
static constexpr const ::absl::internal::identity_t<var_type>& \
|
||||
name = /* NOLINT */ \
|
||||
AbslInternalInlineVariableHolder##name<>::kInstance; \
|
||||
static_assert(sizeof(void (*)(decltype(name))) != 0, \
|
||||
"Silence unused variable warnings.")
|
||||
|
||||
#endif // __cpp_inline_variables
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#ifndef ABSL_BASE_INLINE_VARIABLE_TESTING_H_
|
||||
#define ABSL_BASE_INLINE_VARIABLE_TESTING_H_
|
||||
|
||||
#include "absl/base/internal/inline_variable.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace inline_variable_testing_internal {
|
||||
|
||||
struct Foo {
|
||||
int value = 5;
|
||||
};
|
||||
|
||||
ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, inline_variable_foo, {});
|
||||
ABSL_INTERNAL_INLINE_CONSTEXPR(Foo, other_inline_variable_foo, {});
|
||||
|
||||
ABSL_INTERNAL_INLINE_CONSTEXPR(int, inline_variable_int, 5);
|
||||
ABSL_INTERNAL_INLINE_CONSTEXPR(int, other_inline_variable_int, 5);
|
||||
|
||||
ABSL_INTERNAL_INLINE_CONSTEXPR(void(*)(), inline_variable_fun_ptr, nullptr);
|
||||
|
||||
const Foo& get_foo_a();
|
||||
const Foo& get_foo_b();
|
||||
|
||||
const int& get_int_a();
|
||||
const int& get_int_b();
|
||||
|
||||
} // namespace inline_variable_testing_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INLINE_VARIABLE_TESTING_H_
|
|
@ -0,0 +1,187 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// absl::base_internal::invoke(f, args...) is an implementation of
|
||||
// INVOKE(f, args...) from section [func.require] of the C++ standard.
|
||||
//
|
||||
// [func.require]
|
||||
// Define INVOKE (f, t1, t2, ..., tN) as follows:
|
||||
// 1. (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
|
||||
// and t1 is an object of type T or a reference to an object of type T or a
|
||||
// reference to an object of a type derived from T;
|
||||
// 2. ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
|
||||
// class T and t1 is not one of the types described in the previous item;
|
||||
// 3. t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is
|
||||
// an object of type T or a reference to an object of type T or a reference
|
||||
// to an object of a type derived from T;
|
||||
// 4. (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1
|
||||
// is not one of the types described in the previous item;
|
||||
// 5. f(t1, t2, ..., tN) in all other cases.
|
||||
//
|
||||
// The implementation is SFINAE-friendly: substitution failure within invoke()
|
||||
// isn't an error.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_INVOKE_H_
|
||||
#define ABSL_BASE_INTERNAL_INVOKE_H_
|
||||
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/meta/type_traits.h"
|
||||
|
||||
// The following code is internal implementation detail. See the comment at the
|
||||
// top of this file for the API documentation.
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// The five classes below each implement one of the clauses from the definition
|
||||
// of INVOKE. The inner class template Accept<F, Args...> checks whether the
|
||||
// clause is applicable; static function template Invoke(f, args...) does the
|
||||
// invocation.
|
||||
//
|
||||
// By separating the clause selection logic from invocation we make sure that
|
||||
// Invoke() does exactly what the standard says.
|
||||
|
||||
template <typename Derived>
|
||||
struct StrippedAccept {
|
||||
template <typename... Args>
|
||||
struct Accept : Derived::template AcceptImpl<typename std::remove_cv<
|
||||
typename std::remove_reference<Args>::type>::type...> {};
|
||||
};
|
||||
|
||||
// (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T
|
||||
// and t1 is an object of type T or a reference to an object of type T or a
|
||||
// reference to an object of a type derived from T.
|
||||
struct MemFunAndRef : StrippedAccept<MemFunAndRef> {
|
||||
template <typename... Args>
|
||||
struct AcceptImpl : std::false_type {};
|
||||
|
||||
template <typename MemFunType, typename C, typename Obj, typename... Args>
|
||||
struct AcceptImpl<MemFunType C::*, Obj, Args...>
|
||||
: std::integral_constant<bool, std::is_base_of<C, Obj>::value &&
|
||||
absl::is_function<MemFunType>::value> {
|
||||
};
|
||||
|
||||
template <typename MemFun, typename Obj, typename... Args>
|
||||
static decltype((std::declval<Obj>().*
|
||||
std::declval<MemFun>())(std::declval<Args>()...))
|
||||
Invoke(MemFun&& mem_fun, Obj&& obj, Args&&... args) {
|
||||
return (std::forward<Obj>(obj).*
|
||||
std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
// ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a
|
||||
// class T and t1 is not one of the types described in the previous item.
|
||||
struct MemFunAndPtr : StrippedAccept<MemFunAndPtr> {
|
||||
template <typename... Args>
|
||||
struct AcceptImpl : std::false_type {};
|
||||
|
||||
template <typename MemFunType, typename C, typename Ptr, typename... Args>
|
||||
struct AcceptImpl<MemFunType C::*, Ptr, Args...>
|
||||
: std::integral_constant<bool, !std::is_base_of<C, Ptr>::value &&
|
||||
absl::is_function<MemFunType>::value> {
|
||||
};
|
||||
|
||||
template <typename MemFun, typename Ptr, typename... Args>
|
||||
static decltype(((*std::declval<Ptr>()).*
|
||||
std::declval<MemFun>())(std::declval<Args>()...))
|
||||
Invoke(MemFun&& mem_fun, Ptr&& ptr, Args&&... args) {
|
||||
return ((*std::forward<Ptr>(ptr)).*
|
||||
std::forward<MemFun>(mem_fun))(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
// t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is
|
||||
// an object of type T or a reference to an object of type T or a reference
|
||||
// to an object of a type derived from T.
|
||||
struct DataMemAndRef : StrippedAccept<DataMemAndRef> {
|
||||
template <typename... Args>
|
||||
struct AcceptImpl : std::false_type {};
|
||||
|
||||
template <typename R, typename C, typename Obj>
|
||||
struct AcceptImpl<R C::*, Obj>
|
||||
: std::integral_constant<bool, std::is_base_of<C, Obj>::value &&
|
||||
!absl::is_function<R>::value> {};
|
||||
|
||||
template <typename DataMem, typename Ref>
|
||||
static decltype(std::declval<Ref>().*std::declval<DataMem>()) Invoke(
|
||||
DataMem&& data_mem, Ref&& ref) {
|
||||
return std::forward<Ref>(ref).*std::forward<DataMem>(data_mem);
|
||||
}
|
||||
};
|
||||
|
||||
// (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1
|
||||
// is not one of the types described in the previous item.
|
||||
struct DataMemAndPtr : StrippedAccept<DataMemAndPtr> {
|
||||
template <typename... Args>
|
||||
struct AcceptImpl : std::false_type {};
|
||||
|
||||
template <typename R, typename C, typename Ptr>
|
||||
struct AcceptImpl<R C::*, Ptr>
|
||||
: std::integral_constant<bool, !std::is_base_of<C, Ptr>::value &&
|
||||
!absl::is_function<R>::value> {};
|
||||
|
||||
template <typename DataMem, typename Ptr>
|
||||
static decltype((*std::declval<Ptr>()).*std::declval<DataMem>()) Invoke(
|
||||
DataMem&& data_mem, Ptr&& ptr) {
|
||||
return (*std::forward<Ptr>(ptr)).*std::forward<DataMem>(data_mem);
|
||||
}
|
||||
};
|
||||
|
||||
// f(t1, t2, ..., tN) in all other cases.
|
||||
struct Callable {
|
||||
// Callable doesn't have Accept because it's the last clause that gets picked
|
||||
// when none of the previous clauses are applicable.
|
||||
template <typename F, typename... Args>
|
||||
static decltype(std::declval<F>()(std::declval<Args>()...)) Invoke(
|
||||
F&& f, Args&&... args) {
|
||||
return std::forward<F>(f)(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
// Resolves to the first matching clause.
|
||||
template <typename... Args>
|
||||
struct Invoker {
|
||||
typedef typename std::conditional<
|
||||
MemFunAndRef::Accept<Args...>::value, MemFunAndRef,
|
||||
typename std::conditional<
|
||||
MemFunAndPtr::Accept<Args...>::value, MemFunAndPtr,
|
||||
typename std::conditional<
|
||||
DataMemAndRef::Accept<Args...>::value, DataMemAndRef,
|
||||
typename std::conditional<DataMemAndPtr::Accept<Args...>::value,
|
||||
DataMemAndPtr, Callable>::type>::type>::
|
||||
type>::type type;
|
||||
};
|
||||
|
||||
// The result type of Invoke<F, Args...>.
|
||||
template <typename F, typename... Args>
|
||||
using invoke_result_t = decltype(Invoker<F, Args...>::type::Invoke(
|
||||
std::declval<F>(), std::declval<Args>()...));
|
||||
|
||||
// Invoke(f, args...) is an implementation of INVOKE(f, args...) from section
|
||||
// [func.require] of the C++ standard.
|
||||
template <typename F, typename... Args>
|
||||
invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) {
|
||||
return Invoker<F, Args...>::type::Invoke(std::forward<F>(f),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_INVOKE_H_
|
|
@ -0,0 +1,620 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
// A low-level allocator that can be used by other low-level
|
||||
// modules without introducing dependency cycles.
|
||||
// This allocator is slow and wasteful of memory;
|
||||
// it should not be used when performance is key.
|
||||
|
||||
#include "absl/base/internal/low_level_alloc.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "absl/base/call_once.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/direct_mmap.h"
|
||||
#include "absl/base/internal/scheduling_mode.h"
|
||||
#include "absl/base/macros.h"
|
||||
#include "absl/base/thread_annotations.h"
|
||||
|
||||
// LowLevelAlloc requires that the platform support low-level
|
||||
// allocation of virtual memory. Platforms lacking this cannot use
|
||||
// LowLevelAlloc.
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_MISSING
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <cstddef>
|
||||
#include <new> // for placement-new
|
||||
|
||||
#include "absl/base/dynamic_annotations.h"
|
||||
#include "absl/base/internal/raw_logging.h"
|
||||
#include "absl/base/internal/spinlock.h"
|
||||
|
||||
// MAP_ANONYMOUS
|
||||
#if defined(__APPLE__)
|
||||
// For mmap, Linux defines both MAP_ANONYMOUS and MAP_ANON and says MAP_ANON is
|
||||
// deprecated. In Darwin, MAP_ANON is all there is.
|
||||
#if !defined MAP_ANONYMOUS
|
||||
#define MAP_ANONYMOUS MAP_ANON
|
||||
#endif // !MAP_ANONYMOUS
|
||||
#endif // __APPLE__
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// A first-fit allocator with amortized logarithmic free() time.
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
static const int kMaxLevel = 30;
|
||||
|
||||
namespace {
|
||||
// This struct describes one allocated block, or one free block.
|
||||
struct AllocList {
|
||||
struct Header {
|
||||
// Size of entire region, including this field. Must be
|
||||
// first. Valid in both allocated and unallocated blocks.
|
||||
uintptr_t size;
|
||||
|
||||
// kMagicAllocated or kMagicUnallocated xor this.
|
||||
uintptr_t magic;
|
||||
|
||||
// Pointer to parent arena.
|
||||
LowLevelAlloc::Arena *arena;
|
||||
|
||||
// Aligns regions to 0 mod 2*sizeof(void*).
|
||||
void *dummy_for_alignment;
|
||||
} header;
|
||||
|
||||
// Next two fields: in unallocated blocks: freelist skiplist data
|
||||
// in allocated blocks: overlaps with client data
|
||||
|
||||
// Levels in skiplist used.
|
||||
int levels;
|
||||
|
||||
// Actually has levels elements. The AllocList node may not have room
|
||||
// for all kMaxLevel entries. See max_fit in LLA_SkiplistLevels().
|
||||
AllocList *next[kMaxLevel];
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// A trivial skiplist implementation. This is used to keep the freelist
|
||||
// in address order while taking only logarithmic time per insert and delete.
|
||||
|
||||
// An integer approximation of log2(size/base)
|
||||
// Requires size >= base.
|
||||
static int IntLog2(size_t size, size_t base) {
|
||||
int result = 0;
|
||||
for (size_t i = size; i > base; i >>= 1) { // i == floor(size/2**result)
|
||||
result++;
|
||||
}
|
||||
// floor(size / 2**result) <= base < floor(size / 2**(result-1))
|
||||
// => log2(size/(base+1)) <= result < 1+log2(size/base)
|
||||
// => result ~= log2(size/base)
|
||||
return result;
|
||||
}
|
||||
|
||||
// Return a random integer n: p(n)=1/(2**n) if 1 <= n; p(n)=0 if n < 1.
|
||||
static int Random(uint32_t *state) {
|
||||
uint32_t r = *state;
|
||||
int result = 1;
|
||||
while ((((r = r*1103515245 + 12345) >> 30) & 1) == 0) {
|
||||
result++;
|
||||
}
|
||||
*state = r;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Return a number of skiplist levels for a node of size bytes, where
|
||||
// base is the minimum node size. Compute level=log2(size / base)+n
|
||||
// where n is 1 if random is false and otherwise a random number generated with
|
||||
// the standard distribution for a skiplist: See Random() above.
|
||||
// Bigger nodes tend to have more skiplist levels due to the log2(size / base)
|
||||
// term, so first-fit searches touch fewer nodes. "level" is clipped so
|
||||
// level<kMaxLevel and next[level-1] will fit in the node.
|
||||
// 0 < LLA_SkiplistLevels(x,y,false) <= LLA_SkiplistLevels(x,y,true) < kMaxLevel
|
||||
static int LLA_SkiplistLevels(size_t size, size_t base, uint32_t *random) {
|
||||
// max_fit is the maximum number of levels that will fit in a node for the
|
||||
// given size. We can't return more than max_fit, no matter what the
|
||||
// random number generator says.
|
||||
size_t max_fit = (size - offsetof(AllocList, next)) / sizeof(AllocList *);
|
||||
int level = IntLog2(size, base) + (random != nullptr ? Random(random) : 1);
|
||||
if (static_cast<size_t>(level) > max_fit) level = static_cast<int>(max_fit);
|
||||
if (level > kMaxLevel-1) level = kMaxLevel - 1;
|
||||
ABSL_RAW_CHECK(level >= 1, "block not big enough for even one level");
|
||||
return level;
|
||||
}
|
||||
|
||||
// Return "atleast", the first element of AllocList *head s.t. *atleast >= *e.
|
||||
// For 0 <= i < head->levels, set prev[i] to "no_greater", where no_greater
|
||||
// points to the last element at level i in the AllocList less than *e, or is
|
||||
// head if no such element exists.
|
||||
static AllocList *LLA_SkiplistSearch(AllocList *head,
|
||||
AllocList *e, AllocList **prev) {
|
||||
AllocList *p = head;
|
||||
for (int level = head->levels - 1; level >= 0; level--) {
|
||||
for (AllocList *n; (n = p->next[level]) != nullptr && n < e; p = n) {
|
||||
}
|
||||
prev[level] = p;
|
||||
}
|
||||
return (head->levels == 0) ? nullptr : prev[0]->next[0];
|
||||
}
|
||||
|
||||
// Insert element *e into AllocList *head. Set prev[] as LLA_SkiplistSearch.
|
||||
// Requires that e->levels be previously set by the caller (using
|
||||
// LLA_SkiplistLevels())
|
||||
static void LLA_SkiplistInsert(AllocList *head, AllocList *e,
|
||||
AllocList **prev) {
|
||||
LLA_SkiplistSearch(head, e, prev);
|
||||
for (; head->levels < e->levels; head->levels++) { // extend prev pointers
|
||||
prev[head->levels] = head; // to all *e's levels
|
||||
}
|
||||
for (int i = 0; i != e->levels; i++) { // add element to list
|
||||
e->next[i] = prev[i]->next[i];
|
||||
prev[i]->next[i] = e;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove element *e from AllocList *head. Set prev[] as LLA_SkiplistSearch().
|
||||
// Requires that e->levels be previous set by the caller (using
|
||||
// LLA_SkiplistLevels())
|
||||
static void LLA_SkiplistDelete(AllocList *head, AllocList *e,
|
||||
AllocList **prev) {
|
||||
AllocList *found = LLA_SkiplistSearch(head, e, prev);
|
||||
ABSL_RAW_CHECK(e == found, "element not in freelist");
|
||||
for (int i = 0; i != e->levels && prev[i]->next[i] == e; i++) {
|
||||
prev[i]->next[i] = e->next[i];
|
||||
}
|
||||
while (head->levels > 0 && head->next[head->levels - 1] == nullptr) {
|
||||
head->levels--; // reduce head->levels if level unused
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Arena implementation
|
||||
|
||||
// Metadata for an LowLevelAlloc arena instance.
|
||||
struct LowLevelAlloc::Arena {
|
||||
// Constructs an arena with the given LowLevelAlloc flags.
|
||||
explicit Arena(uint32_t flags_value);
|
||||
|
||||
base_internal::SpinLock mu;
|
||||
// Head of free list, sorted by address
|
||||
AllocList freelist ABSL_GUARDED_BY(mu);
|
||||
// Count of allocated blocks
|
||||
int32_t allocation_count ABSL_GUARDED_BY(mu);
|
||||
// flags passed to NewArena
|
||||
const uint32_t flags;
|
||||
// Result of sysconf(_SC_PAGESIZE)
|
||||
const size_t pagesize;
|
||||
// Lowest power of two >= max(16, sizeof(AllocList))
|
||||
const size_t round_up;
|
||||
// Smallest allocation block size
|
||||
const size_t min_size;
|
||||
// PRNG state
|
||||
uint32_t random ABSL_GUARDED_BY(mu);
|
||||
};
|
||||
|
||||
namespace {
|
||||
// Static storage space for the lazily-constructed, default global arena
|
||||
// instances. We require this space because the whole point of LowLevelAlloc
|
||||
// is to avoid relying on malloc/new.
|
||||
alignas(LowLevelAlloc::Arena) unsigned char default_arena_storage[sizeof(
|
||||
LowLevelAlloc::Arena)];
|
||||
alignas(LowLevelAlloc::Arena) unsigned char unhooked_arena_storage[sizeof(
|
||||
LowLevelAlloc::Arena)];
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
alignas(
|
||||
LowLevelAlloc::Arena) unsigned char unhooked_async_sig_safe_arena_storage
|
||||
[sizeof(LowLevelAlloc::Arena)];
|
||||
#endif
|
||||
|
||||
// We must use LowLevelCallOnce here to construct the global arenas, rather than
|
||||
// using function-level statics, to avoid recursively invoking the scheduler.
|
||||
absl::once_flag create_globals_once;
|
||||
|
||||
void CreateGlobalArenas() {
|
||||
new (&default_arena_storage)
|
||||
LowLevelAlloc::Arena(LowLevelAlloc::kCallMallocHook);
|
||||
new (&unhooked_arena_storage) LowLevelAlloc::Arena(0);
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
new (&unhooked_async_sig_safe_arena_storage)
|
||||
LowLevelAlloc::Arena(LowLevelAlloc::kAsyncSignalSafe);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Returns a global arena that does not call into hooks. Used by NewArena()
|
||||
// when kCallMallocHook is not set.
|
||||
LowLevelAlloc::Arena* UnhookedArena() {
|
||||
base_internal::LowLevelCallOnce(&create_globals_once, CreateGlobalArenas);
|
||||
return reinterpret_cast<LowLevelAlloc::Arena*>(&unhooked_arena_storage);
|
||||
}
|
||||
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
// Returns a global arena that is async-signal safe. Used by NewArena() when
|
||||
// kAsyncSignalSafe is set.
|
||||
LowLevelAlloc::Arena *UnhookedAsyncSigSafeArena() {
|
||||
base_internal::LowLevelCallOnce(&create_globals_once, CreateGlobalArenas);
|
||||
return reinterpret_cast<LowLevelAlloc::Arena *>(
|
||||
&unhooked_async_sig_safe_arena_storage);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
// Returns the default arena, as used by LowLevelAlloc::Alloc() and friends.
|
||||
LowLevelAlloc::Arena *LowLevelAlloc::DefaultArena() {
|
||||
base_internal::LowLevelCallOnce(&create_globals_once, CreateGlobalArenas);
|
||||
return reinterpret_cast<LowLevelAlloc::Arena*>(&default_arena_storage);
|
||||
}
|
||||
|
||||
// magic numbers to identify allocated and unallocated blocks
|
||||
static const uintptr_t kMagicAllocated = 0x4c833e95U;
|
||||
static const uintptr_t kMagicUnallocated = ~kMagicAllocated;
|
||||
|
||||
namespace {
|
||||
class ABSL_SCOPED_LOCKABLE ArenaLock {
|
||||
public:
|
||||
explicit ArenaLock(LowLevelAlloc::Arena *arena)
|
||||
ABSL_EXCLUSIVE_LOCK_FUNCTION(arena->mu)
|
||||
: arena_(arena) {
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
if ((arena->flags & LowLevelAlloc::kAsyncSignalSafe) != 0) {
|
||||
sigset_t all;
|
||||
sigfillset(&all);
|
||||
mask_valid_ = pthread_sigmask(SIG_BLOCK, &all, &mask_) == 0;
|
||||
}
|
||||
#endif
|
||||
arena_->mu.Lock();
|
||||
}
|
||||
~ArenaLock() { ABSL_RAW_CHECK(left_, "haven't left Arena region"); }
|
||||
void Leave() ABSL_UNLOCK_FUNCTION() {
|
||||
arena_->mu.Unlock();
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
if (mask_valid_) {
|
||||
const int err = pthread_sigmask(SIG_SETMASK, &mask_, nullptr);
|
||||
if (err != 0) {
|
||||
ABSL_RAW_LOG(FATAL, "pthread_sigmask failed: %d", err);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
left_ = true;
|
||||
}
|
||||
|
||||
private:
|
||||
bool left_ = false; // whether left region
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
bool mask_valid_ = false;
|
||||
sigset_t mask_; // old mask of blocked signals
|
||||
#endif
|
||||
LowLevelAlloc::Arena *arena_;
|
||||
ArenaLock(const ArenaLock &) = delete;
|
||||
ArenaLock &operator=(const ArenaLock &) = delete;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// create an appropriate magic number for an object at "ptr"
|
||||
// "magic" should be kMagicAllocated or kMagicUnallocated
|
||||
inline static uintptr_t Magic(uintptr_t magic, AllocList::Header *ptr) {
|
||||
return magic ^ reinterpret_cast<uintptr_t>(ptr);
|
||||
}
|
||||
|
||||
namespace {
|
||||
size_t GetPageSize() {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO system_info;
|
||||
GetSystemInfo(&system_info);
|
||||
return std::max(system_info.dwPageSize, system_info.dwAllocationGranularity);
|
||||
#elif defined(__wasm__) || defined(__asmjs__)
|
||||
return getpagesize();
|
||||
#else
|
||||
return sysconf(_SC_PAGESIZE);
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t RoundedUpBlockSize() {
|
||||
// Round up block sizes to a power of two close to the header size.
|
||||
size_t round_up = 16;
|
||||
while (round_up < sizeof(AllocList::Header)) {
|
||||
round_up += round_up;
|
||||
}
|
||||
return round_up;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
LowLevelAlloc::Arena::Arena(uint32_t flags_value)
|
||||
: mu(base_internal::SCHEDULE_KERNEL_ONLY),
|
||||
allocation_count(0),
|
||||
flags(flags_value),
|
||||
pagesize(GetPageSize()),
|
||||
round_up(RoundedUpBlockSize()),
|
||||
min_size(2 * round_up),
|
||||
random(0) {
|
||||
freelist.header.size = 0;
|
||||
freelist.header.magic =
|
||||
Magic(kMagicUnallocated, &freelist.header);
|
||||
freelist.header.arena = this;
|
||||
freelist.levels = 0;
|
||||
memset(freelist.next, 0, sizeof(freelist.next));
|
||||
}
|
||||
|
||||
// L < meta_data_arena->mu
|
||||
LowLevelAlloc::Arena *LowLevelAlloc::NewArena(int32_t flags) {
|
||||
Arena *meta_data_arena = DefaultArena();
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
if ((flags & LowLevelAlloc::kAsyncSignalSafe) != 0) {
|
||||
meta_data_arena = UnhookedAsyncSigSafeArena();
|
||||
} else // NOLINT(readability/braces)
|
||||
#endif
|
||||
if ((flags & LowLevelAlloc::kCallMallocHook) == 0) {
|
||||
meta_data_arena = UnhookedArena();
|
||||
}
|
||||
Arena *result =
|
||||
new (AllocWithArena(sizeof (*result), meta_data_arena)) Arena(flags);
|
||||
return result;
|
||||
}
|
||||
|
||||
// L < arena->mu, L < arena->arena->mu
|
||||
bool LowLevelAlloc::DeleteArena(Arena *arena) {
|
||||
ABSL_RAW_CHECK(
|
||||
arena != nullptr && arena != DefaultArena() && arena != UnhookedArena(),
|
||||
"may not delete default arena");
|
||||
ArenaLock section(arena);
|
||||
if (arena->allocation_count != 0) {
|
||||
section.Leave();
|
||||
return false;
|
||||
}
|
||||
while (arena->freelist.next[0] != nullptr) {
|
||||
AllocList *region = arena->freelist.next[0];
|
||||
size_t size = region->header.size;
|
||||
arena->freelist.next[0] = region->next[0];
|
||||
ABSL_RAW_CHECK(
|
||||
region->header.magic == Magic(kMagicUnallocated, ®ion->header),
|
||||
"bad magic number in DeleteArena()");
|
||||
ABSL_RAW_CHECK(region->header.arena == arena,
|
||||
"bad arena pointer in DeleteArena()");
|
||||
ABSL_RAW_CHECK(size % arena->pagesize == 0,
|
||||
"empty arena has non-page-aligned block size");
|
||||
ABSL_RAW_CHECK(reinterpret_cast<uintptr_t>(region) % arena->pagesize == 0,
|
||||
"empty arena has non-page-aligned block");
|
||||
int munmap_result;
|
||||
#ifdef _WIN32
|
||||
munmap_result = VirtualFree(region, 0, MEM_RELEASE);
|
||||
ABSL_RAW_CHECK(munmap_result != 0,
|
||||
"LowLevelAlloc::DeleteArena: VitualFree failed");
|
||||
#else
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
if ((arena->flags & LowLevelAlloc::kAsyncSignalSafe) == 0) {
|
||||
munmap_result = munmap(region, size);
|
||||
} else {
|
||||
munmap_result = base_internal::DirectMunmap(region, size);
|
||||
}
|
||||
#else
|
||||
munmap_result = munmap(region, size);
|
||||
#endif // ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
if (munmap_result != 0) {
|
||||
ABSL_RAW_LOG(FATAL, "LowLevelAlloc::DeleteArena: munmap failed: %d",
|
||||
errno);
|
||||
}
|
||||
#endif // _WIN32
|
||||
}
|
||||
section.Leave();
|
||||
arena->~Arena();
|
||||
Free(arena);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Addition, checking for overflow. The intent is to die if an external client
|
||||
// manages to push through a request that would cause arithmetic to fail.
|
||||
static inline uintptr_t CheckedAdd(uintptr_t a, uintptr_t b) {
|
||||
uintptr_t sum = a + b;
|
||||
ABSL_RAW_CHECK(sum >= a, "LowLevelAlloc arithmetic overflow");
|
||||
return sum;
|
||||
}
|
||||
|
||||
// Return value rounded up to next multiple of align.
|
||||
// align must be a power of two.
|
||||
static inline uintptr_t RoundUp(uintptr_t addr, uintptr_t align) {
|
||||
return CheckedAdd(addr, align - 1) & ~(align - 1);
|
||||
}
|
||||
|
||||
// Equivalent to "return prev->next[i]" but with sanity checking
|
||||
// that the freelist is in the correct order, that it
|
||||
// consists of regions marked "unallocated", and that no two regions
|
||||
// are adjacent in memory (they should have been coalesced).
|
||||
// L >= arena->mu
|
||||
static AllocList *Next(int i, AllocList *prev, LowLevelAlloc::Arena *arena) {
|
||||
ABSL_RAW_CHECK(i < prev->levels, "too few levels in Next()");
|
||||
AllocList *next = prev->next[i];
|
||||
if (next != nullptr) {
|
||||
ABSL_RAW_CHECK(
|
||||
next->header.magic == Magic(kMagicUnallocated, &next->header),
|
||||
"bad magic number in Next()");
|
||||
ABSL_RAW_CHECK(next->header.arena == arena, "bad arena pointer in Next()");
|
||||
if (prev != &arena->freelist) {
|
||||
ABSL_RAW_CHECK(prev < next, "unordered freelist");
|
||||
ABSL_RAW_CHECK(reinterpret_cast<char *>(prev) + prev->header.size <
|
||||
reinterpret_cast<char *>(next),
|
||||
"malformed freelist");
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
// Coalesce list item "a" with its successor if they are adjacent.
|
||||
static void Coalesce(AllocList *a) {
|
||||
AllocList *n = a->next[0];
|
||||
if (n != nullptr && reinterpret_cast<char *>(a) + a->header.size ==
|
||||
reinterpret_cast<char *>(n)) {
|
||||
LowLevelAlloc::Arena *arena = a->header.arena;
|
||||
a->header.size += n->header.size;
|
||||
n->header.magic = 0;
|
||||
n->header.arena = nullptr;
|
||||
AllocList *prev[kMaxLevel];
|
||||
LLA_SkiplistDelete(&arena->freelist, n, prev);
|
||||
LLA_SkiplistDelete(&arena->freelist, a, prev);
|
||||
a->levels = LLA_SkiplistLevels(a->header.size, arena->min_size,
|
||||
&arena->random);
|
||||
LLA_SkiplistInsert(&arena->freelist, a, prev);
|
||||
}
|
||||
}
|
||||
|
||||
// Adds block at location "v" to the free list
|
||||
// L >= arena->mu
|
||||
static void AddToFreelist(void *v, LowLevelAlloc::Arena *arena) {
|
||||
AllocList *f = reinterpret_cast<AllocList *>(
|
||||
reinterpret_cast<char *>(v) - sizeof (f->header));
|
||||
ABSL_RAW_CHECK(f->header.magic == Magic(kMagicAllocated, &f->header),
|
||||
"bad magic number in AddToFreelist()");
|
||||
ABSL_RAW_CHECK(f->header.arena == arena,
|
||||
"bad arena pointer in AddToFreelist()");
|
||||
f->levels = LLA_SkiplistLevels(f->header.size, arena->min_size,
|
||||
&arena->random);
|
||||
AllocList *prev[kMaxLevel];
|
||||
LLA_SkiplistInsert(&arena->freelist, f, prev);
|
||||
f->header.magic = Magic(kMagicUnallocated, &f->header);
|
||||
Coalesce(f); // maybe coalesce with successor
|
||||
Coalesce(prev[0]); // maybe coalesce with predecessor
|
||||
}
|
||||
|
||||
// Frees storage allocated by LowLevelAlloc::Alloc().
|
||||
// L < arena->mu
|
||||
void LowLevelAlloc::Free(void *v) {
|
||||
if (v != nullptr) {
|
||||
AllocList *f = reinterpret_cast<AllocList *>(
|
||||
reinterpret_cast<char *>(v) - sizeof (f->header));
|
||||
LowLevelAlloc::Arena *arena = f->header.arena;
|
||||
ArenaLock section(arena);
|
||||
AddToFreelist(v, arena);
|
||||
ABSL_RAW_CHECK(arena->allocation_count > 0, "nothing in arena to free");
|
||||
arena->allocation_count--;
|
||||
section.Leave();
|
||||
}
|
||||
}
|
||||
|
||||
// allocates and returns a block of size bytes, to be freed with Free()
|
||||
// L < arena->mu
|
||||
static void *DoAllocWithArena(size_t request, LowLevelAlloc::Arena *arena) {
|
||||
void *result = nullptr;
|
||||
if (request != 0) {
|
||||
AllocList *s; // will point to region that satisfies request
|
||||
ArenaLock section(arena);
|
||||
// round up with header
|
||||
size_t req_rnd = RoundUp(CheckedAdd(request, sizeof (s->header)),
|
||||
arena->round_up);
|
||||
for (;;) { // loop until we find a suitable region
|
||||
// find the minimum levels that a block of this size must have
|
||||
int i = LLA_SkiplistLevels(req_rnd, arena->min_size, nullptr) - 1;
|
||||
if (i < arena->freelist.levels) { // potential blocks exist
|
||||
AllocList *before = &arena->freelist; // predecessor of s
|
||||
while ((s = Next(i, before, arena)) != nullptr &&
|
||||
s->header.size < req_rnd) {
|
||||
before = s;
|
||||
}
|
||||
if (s != nullptr) { // we found a region
|
||||
break;
|
||||
}
|
||||
}
|
||||
// we unlock before mmap() both because mmap() may call a callback hook,
|
||||
// and because it may be slow.
|
||||
arena->mu.Unlock();
|
||||
// mmap generous 64K chunks to decrease
|
||||
// the chances/impact of fragmentation:
|
||||
size_t new_pages_size = RoundUp(req_rnd, arena->pagesize * 16);
|
||||
void *new_pages;
|
||||
#ifdef _WIN32
|
||||
new_pages = VirtualAlloc(0, new_pages_size,
|
||||
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
ABSL_RAW_CHECK(new_pages != nullptr, "VirtualAlloc failed");
|
||||
#else
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
if ((arena->flags & LowLevelAlloc::kAsyncSignalSafe) != 0) {
|
||||
new_pages = base_internal::DirectMmap(nullptr, new_pages_size,
|
||||
PROT_WRITE|PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
|
||||
} else {
|
||||
new_pages = mmap(nullptr, new_pages_size, PROT_WRITE | PROT_READ,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
}
|
||||
#else
|
||||
new_pages = mmap(nullptr, new_pages_size, PROT_WRITE | PROT_READ,
|
||||
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
#endif // ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
if (new_pages == MAP_FAILED) {
|
||||
ABSL_RAW_LOG(FATAL, "mmap error: %d", errno);
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
||||
arena->mu.Lock();
|
||||
s = reinterpret_cast<AllocList *>(new_pages);
|
||||
s->header.size = new_pages_size;
|
||||
// Pretend the block is allocated; call AddToFreelist() to free it.
|
||||
s->header.magic = Magic(kMagicAllocated, &s->header);
|
||||
s->header.arena = arena;
|
||||
AddToFreelist(&s->levels, arena); // insert new region into free list
|
||||
}
|
||||
AllocList *prev[kMaxLevel];
|
||||
LLA_SkiplistDelete(&arena->freelist, s, prev); // remove from free list
|
||||
// s points to the first free region that's big enough
|
||||
if (CheckedAdd(req_rnd, arena->min_size) <= s->header.size) {
|
||||
// big enough to split
|
||||
AllocList *n = reinterpret_cast<AllocList *>
|
||||
(req_rnd + reinterpret_cast<char *>(s));
|
||||
n->header.size = s->header.size - req_rnd;
|
||||
n->header.magic = Magic(kMagicAllocated, &n->header);
|
||||
n->header.arena = arena;
|
||||
s->header.size = req_rnd;
|
||||
AddToFreelist(&n->levels, arena);
|
||||
}
|
||||
s->header.magic = Magic(kMagicAllocated, &s->header);
|
||||
ABSL_RAW_CHECK(s->header.arena == arena, "");
|
||||
arena->allocation_count++;
|
||||
section.Leave();
|
||||
result = &s->levels;
|
||||
}
|
||||
ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(result, request);
|
||||
return result;
|
||||
}
|
||||
|
||||
void *LowLevelAlloc::Alloc(size_t request) {
|
||||
void *result = DoAllocWithArena(request, DefaultArena());
|
||||
return result;
|
||||
}
|
||||
|
||||
void *LowLevelAlloc::AllocWithArena(size_t request, Arena *arena) {
|
||||
ABSL_RAW_CHECK(arena != nullptr, "must pass a valid arena");
|
||||
void *result = DoAllocWithArena(request, arena);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_LOW_LEVEL_ALLOC_MISSING
|
|
@ -0,0 +1,126 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
|
||||
#define ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
|
||||
|
||||
// A simple thread-safe memory allocator that does not depend on
|
||||
// mutexes or thread-specific data. It is intended to be used
|
||||
// sparingly, and only when malloc() would introduce an unwanted
|
||||
// dependency, such as inside the heap-checker, or the Mutex
|
||||
// implementation.
|
||||
|
||||
// IWYU pragma: private, include "base/low_level_alloc.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
|
||||
// LowLevelAlloc requires that the platform support low-level
|
||||
// allocation of virtual memory. Platforms lacking this cannot use
|
||||
// LowLevelAlloc.
|
||||
#ifdef ABSL_LOW_LEVEL_ALLOC_MISSING
|
||||
#error ABSL_LOW_LEVEL_ALLOC_MISSING cannot be directly set
|
||||
#elif !defined(ABSL_HAVE_MMAP) && !defined(_WIN32)
|
||||
#define ABSL_LOW_LEVEL_ALLOC_MISSING 1
|
||||
#endif
|
||||
|
||||
// Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows or
|
||||
// asm.js / WebAssembly.
|
||||
// See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
|
||||
// for more information.
|
||||
#ifdef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
#error ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING cannot be directly set
|
||||
#elif defined(_WIN32) || defined(__asmjs__) || defined(__wasm__)
|
||||
#define ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING 1
|
||||
#endif
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "absl/base/port.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
class LowLevelAlloc {
|
||||
public:
|
||||
struct Arena; // an arena from which memory may be allocated
|
||||
|
||||
// Returns a pointer to a block of at least "request" bytes
|
||||
// that have been newly allocated from the specific arena.
|
||||
// for Alloc() call the DefaultArena() is used.
|
||||
// Returns 0 if passed request==0.
|
||||
// Does not return 0 under other circumstances; it crashes if memory
|
||||
// is not available.
|
||||
static void *Alloc(size_t request) ABSL_ATTRIBUTE_SECTION(malloc_hook);
|
||||
static void *AllocWithArena(size_t request, Arena *arena)
|
||||
ABSL_ATTRIBUTE_SECTION(malloc_hook);
|
||||
|
||||
// Deallocates a region of memory that was previously allocated with
|
||||
// Alloc(). Does nothing if passed 0. "s" must be either 0,
|
||||
// or must have been returned from a call to Alloc() and not yet passed to
|
||||
// Free() since that call to Alloc(). The space is returned to the arena
|
||||
// from which it was allocated.
|
||||
static void Free(void *s) ABSL_ATTRIBUTE_SECTION(malloc_hook);
|
||||
|
||||
// ABSL_ATTRIBUTE_SECTION(malloc_hook) for Alloc* and Free
|
||||
// are to put all callers of MallocHook::Invoke* in this module
|
||||
// into special section,
|
||||
// so that MallocHook::GetCallerStackTrace can function accurately.
|
||||
|
||||
// Create a new arena.
|
||||
// The root metadata for the new arena is allocated in the
|
||||
// meta_data_arena; the DefaultArena() can be passed for meta_data_arena.
|
||||
// These values may be ored into flags:
|
||||
enum {
|
||||
// Report calls to Alloc() and Free() via the MallocHook interface.
|
||||
// Set in the DefaultArena.
|
||||
kCallMallocHook = 0x0001,
|
||||
|
||||
#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
|
||||
// Make calls to Alloc(), Free() be async-signal-safe. Not set in
|
||||
// DefaultArena(). Not supported on all platforms.
|
||||
kAsyncSignalSafe = 0x0002,
|
||||
#endif
|
||||
};
|
||||
// Construct a new arena. The allocation of the underlying metadata honors
|
||||
// the provided flags. For example, the call NewArena(kAsyncSignalSafe)
|
||||
// is itself async-signal-safe, as well as generatating an arena that provides
|
||||
// async-signal-safe Alloc/Free.
|
||||
static Arena *NewArena(int32_t flags);
|
||||
|
||||
// Destroys an arena allocated by NewArena and returns true,
|
||||
// provided no allocated blocks remain in the arena.
|
||||
// If allocated blocks remain in the arena, does nothing and
|
||||
// returns false.
|
||||
// It is illegal to attempt to destroy the DefaultArena().
|
||||
static bool DeleteArena(Arena *arena);
|
||||
|
||||
// The default arena that always exists.
|
||||
static Arena *DefaultArena();
|
||||
|
||||
private:
|
||||
LowLevelAlloc(); // no instances
|
||||
};
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_LOW_LEVEL_ALLOC_H_
|
|
@ -0,0 +1,181 @@
|
|||
// Copyright 2017 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/base/internal/low_level_alloc.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <thread> // NOLINT(build/c++11)
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#endif
|
||||
|
||||
#include "absl/container/node_hash_map.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
namespace {
|
||||
|
||||
// This test doesn't use gtest since it needs to test that everything
|
||||
// works before main().
|
||||
#define TEST_ASSERT(x) \
|
||||
if (!(x)) { \
|
||||
printf("TEST_ASSERT(%s) FAILED ON LINE %d\n", #x, __LINE__); \
|
||||
abort(); \
|
||||
}
|
||||
|
||||
// a block of memory obtained from the allocator
|
||||
struct BlockDesc {
|
||||
char *ptr; // pointer to memory
|
||||
int len; // number of bytes
|
||||
int fill; // filled with data starting with this
|
||||
};
|
||||
|
||||
// Check that the pattern placed in the block d
|
||||
// by RandomizeBlockDesc is still there.
|
||||
static void CheckBlockDesc(const BlockDesc &d) {
|
||||
for (int i = 0; i != d.len; i++) {
|
||||
TEST_ASSERT((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff));
|
||||
}
|
||||
}
|
||||
|
||||
// Fill the block "*d" with a pattern
|
||||
// starting with a random byte.
|
||||
static void RandomizeBlockDesc(BlockDesc *d) {
|
||||
d->fill = rand() & 0xff;
|
||||
for (int i = 0; i != d->len; i++) {
|
||||
d->ptr[i] = (d->fill + i) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
// Use to indicate to the malloc hooks that
|
||||
// this calls is from LowLevelAlloc.
|
||||
static bool using_low_level_alloc = false;
|
||||
|
||||
// n times, toss a coin, and based on the outcome
|
||||
// either allocate a new block or deallocate an old block.
|
||||
// New blocks are placed in a std::unordered_map with a random key
|
||||
// and initialized with RandomizeBlockDesc().
|
||||
// If keys conflict, the older block is freed.
|
||||
// Old blocks are always checked with CheckBlockDesc()
|
||||
// before being freed. At the end of the run,
|
||||
// all remaining allocated blocks are freed.
|
||||
// If use_new_arena is true, use a fresh arena, and then delete it.
|
||||
// If call_malloc_hook is true and user_arena is true,
|
||||
// allocations and deallocations are reported via the MallocHook
|
||||
// interface.
|
||||
static void Test(bool use_new_arena, bool call_malloc_hook, int n) {
|
||||
typedef absl::node_hash_map<int, BlockDesc> AllocMap;
|
||||
AllocMap allocated;
|
||||
AllocMap::iterator it;
|
||||
BlockDesc block_desc;
|
||||
int rnd;
|
||||
LowLevelAlloc::Arena *arena = 0;
|
||||
if (use_new_arena) {
|
||||
int32_t flags = call_malloc_hook ? LowLevelAlloc::kCallMallocHook : 0;
|
||||
arena = LowLevelAlloc::NewArena(flags);
|
||||
}
|
||||
for (int i = 0; i != n; i++) {
|
||||
if (i != 0 && i % 10000 == 0) {
|
||||
printf(".");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
switch (rand() & 1) { // toss a coin
|
||||
case 0: // coin came up heads: add a block
|
||||
using_low_level_alloc = true;
|
||||
block_desc.len = rand() & 0x3fff;
|
||||
block_desc.ptr =
|
||||
reinterpret_cast<char *>(
|
||||
arena == 0
|
||||
? LowLevelAlloc::Alloc(block_desc.len)
|
||||
: LowLevelAlloc::AllocWithArena(block_desc.len, arena));
|
||||
using_low_level_alloc = false;
|
||||
RandomizeBlockDesc(&block_desc);
|
||||
rnd = rand();
|
||||
it = allocated.find(rnd);
|
||||
if (it != allocated.end()) {
|
||||
CheckBlockDesc(it->second);
|
||||
using_low_level_alloc = true;
|
||||
LowLevelAlloc::Free(it->second.ptr);
|
||||
using_low_level_alloc = false;
|
||||
it->second = block_desc;
|
||||
} else {
|
||||
allocated[rnd] = block_desc;
|
||||
}
|
||||
break;
|
||||
case 1: // coin came up tails: remove a block
|
||||
it = allocated.begin();
|
||||
if (it != allocated.end()) {
|
||||
CheckBlockDesc(it->second);
|
||||
using_low_level_alloc = true;
|
||||
LowLevelAlloc::Free(it->second.ptr);
|
||||
using_low_level_alloc = false;
|
||||
allocated.erase(it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
// remove all remaining blocks
|
||||
while ((it = allocated.begin()) != allocated.end()) {
|
||||
CheckBlockDesc(it->second);
|
||||
using_low_level_alloc = true;
|
||||
LowLevelAlloc::Free(it->second.ptr);
|
||||
using_low_level_alloc = false;
|
||||
allocated.erase(it);
|
||||
}
|
||||
if (use_new_arena) {
|
||||
TEST_ASSERT(LowLevelAlloc::DeleteArena(arena));
|
||||
}
|
||||
}
|
||||
|
||||
// LowLevelAlloc is designed to be safe to call before main().
|
||||
static struct BeforeMain {
|
||||
BeforeMain() {
|
||||
Test(false, false, 50000);
|
||||
Test(true, false, 50000);
|
||||
Test(true, true, 50000);
|
||||
}
|
||||
} before_main;
|
||||
|
||||
} // namespace
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// The actual test runs in the global constructor of `before_main`.
|
||||
printf("PASS\n");
|
||||
#ifdef __EMSCRIPTEN__
|
||||
// clang-format off
|
||||
// This is JS here. Don't try to format it.
|
||||
MAIN_THREAD_EM_ASM({
|
||||
if (ENVIRONMENT_IS_WEB) {
|
||||
if (typeof TEST_FINISH === 'function') {
|
||||
TEST_FINISH($0);
|
||||
} else {
|
||||
console.error('Attempted to exit with status ' + $0);
|
||||
console.error('But TEST_FINSIHED is not a function.');
|
||||
}
|
||||
}
|
||||
}, 0);
|
||||
// clang-format on
|
||||
#endif
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// Core interfaces and definitions used by by low-level interfaces such as
|
||||
// SpinLock.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
|
||||
#define ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
|
||||
|
||||
#include "absl/base/internal/raw_logging.h"
|
||||
#include "absl/base/internal/scheduling_mode.h"
|
||||
#include "absl/base/macros.h"
|
||||
|
||||
// The following two declarations exist so SchedulingGuard may friend them with
|
||||
// the appropriate language linkage. These callbacks allow libc internals, such
|
||||
// as function level statics, to schedule cooperatively when locking.
|
||||
extern "C" bool __google_disable_rescheduling(void);
|
||||
extern "C" void __google_enable_rescheduling(bool disable_result);
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
class CondVar;
|
||||
class Mutex;
|
||||
|
||||
namespace synchronization_internal {
|
||||
int MutexDelay(int32_t c, int mode);
|
||||
} // namespace synchronization_internal
|
||||
|
||||
namespace base_internal {
|
||||
|
||||
class SchedulingHelper; // To allow use of SchedulingGuard.
|
||||
class SpinLock; // To allow use of SchedulingGuard.
|
||||
|
||||
// SchedulingGuard
|
||||
// Provides guard semantics that may be used to disable cooperative rescheduling
|
||||
// of the calling thread within specific program blocks. This is used to
|
||||
// protect resources (e.g. low-level SpinLocks or Domain code) that cooperative
|
||||
// scheduling depends on.
|
||||
//
|
||||
// Domain implementations capable of rescheduling in reaction to involuntary
|
||||
// kernel thread actions (e.g blocking due to a pagefault or syscall) must
|
||||
// guarantee that an annotated thread is not allowed to (cooperatively)
|
||||
// reschedule until the annotated region is complete.
|
||||
//
|
||||
// It is an error to attempt to use a cooperatively scheduled resource (e.g.
|
||||
// Mutex) within a rescheduling-disabled region.
|
||||
//
|
||||
// All methods are async-signal safe.
|
||||
class SchedulingGuard {
|
||||
public:
|
||||
// Returns true iff the calling thread may be cooperatively rescheduled.
|
||||
static bool ReschedulingIsAllowed();
|
||||
SchedulingGuard(const SchedulingGuard&) = delete;
|
||||
SchedulingGuard& operator=(const SchedulingGuard&) = delete;
|
||||
|
||||
private:
|
||||
// Disable cooperative rescheduling of the calling thread. It may still
|
||||
// initiate scheduling operations (e.g. wake-ups), however, it may not itself
|
||||
// reschedule. Nestable. The returned result is opaque, clients should not
|
||||
// attempt to interpret it.
|
||||
// REQUIRES: Result must be passed to a pairing EnableScheduling().
|
||||
static bool DisableRescheduling();
|
||||
|
||||
// Marks the end of a rescheduling disabled region, previously started by
|
||||
// DisableRescheduling().
|
||||
// REQUIRES: Pairs with innermost call (and result) of DisableRescheduling().
|
||||
static void EnableRescheduling(bool disable_result);
|
||||
|
||||
// A scoped helper for {Disable, Enable}Rescheduling().
|
||||
// REQUIRES: destructor must run in same thread as constructor.
|
||||
struct ScopedDisable {
|
||||
ScopedDisable() { disabled = SchedulingGuard::DisableRescheduling(); }
|
||||
~ScopedDisable() { SchedulingGuard::EnableRescheduling(disabled); }
|
||||
|
||||
bool disabled;
|
||||
};
|
||||
|
||||
// A scoped helper to enable rescheduling temporarily.
|
||||
// REQUIRES: destructor must run in same thread as constructor.
|
||||
class ScopedEnable {
|
||||
public:
|
||||
ScopedEnable();
|
||||
~ScopedEnable();
|
||||
|
||||
private:
|
||||
int scheduling_disabled_depth_;
|
||||
};
|
||||
|
||||
// Access to SchedulingGuard is explicitly permitted.
|
||||
friend class absl::CondVar;
|
||||
friend class absl::Mutex;
|
||||
friend class SchedulingHelper;
|
||||
friend class SpinLock;
|
||||
friend int absl::synchronization_internal::MutexDelay(int32_t c, int mode);
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// End of public interfaces.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
inline bool SchedulingGuard::ReschedulingIsAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool SchedulingGuard::DisableRescheduling() {
|
||||
return false;
|
||||
}
|
||||
|
||||
inline void SchedulingGuard::EnableRescheduling(bool /* disable_result */) {
|
||||
return;
|
||||
}
|
||||
|
||||
inline SchedulingGuard::ScopedEnable::ScopedEnable()
|
||||
: scheduling_disabled_depth_(0) {}
|
||||
inline SchedulingGuard::ScopedEnable::~ScopedEnable() {
|
||||
ABSL_RAW_CHECK(scheduling_disabled_depth_ == 0, "disable unused warning");
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_LOW_LEVEL_SCHEDULING_H_
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_PER_THREAD_TLS_H_
|
||||
#define ABSL_BASE_INTERNAL_PER_THREAD_TLS_H_
|
||||
|
||||
// This header defines two macros:
|
||||
//
|
||||
// If the platform supports thread-local storage:
|
||||
//
|
||||
// * ABSL_PER_THREAD_TLS_KEYWORD is the C keyword needed to declare a
|
||||
// thread-local variable
|
||||
// * ABSL_PER_THREAD_TLS is 1
|
||||
//
|
||||
// Otherwise:
|
||||
//
|
||||
// * ABSL_PER_THREAD_TLS_KEYWORD is empty
|
||||
// * ABSL_PER_THREAD_TLS is 0
|
||||
//
|
||||
// Microsoft C supports thread-local storage.
|
||||
// GCC supports it if the appropriate version of glibc is available,
|
||||
// which the programmer can indicate by defining ABSL_HAVE_TLS
|
||||
|
||||
#include "absl/base/port.h" // For ABSL_HAVE_TLS
|
||||
|
||||
#if defined(ABSL_PER_THREAD_TLS)
|
||||
#error ABSL_PER_THREAD_TLS cannot be directly set
|
||||
#elif defined(ABSL_PER_THREAD_TLS_KEYWORD)
|
||||
#error ABSL_PER_THREAD_TLS_KEYWORD cannot be directly set
|
||||
#elif defined(ABSL_HAVE_TLS)
|
||||
#define ABSL_PER_THREAD_TLS_KEYWORD __thread
|
||||
#define ABSL_PER_THREAD_TLS 1
|
||||
#elif defined(_MSC_VER)
|
||||
#define ABSL_PER_THREAD_TLS_KEYWORD __declspec(thread)
|
||||
#define ABSL_PER_THREAD_TLS 1
|
||||
#else
|
||||
#define ABSL_PER_THREAD_TLS_KEYWORD
|
||||
#define ABSL_PER_THREAD_TLS 0
|
||||
#endif
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_PER_THREAD_TLS_H_
|
|
@ -0,0 +1,53 @@
|
|||
// Copyright 2019 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/base/internal/periodic_sampler.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "absl/base/internal/exponential_biased.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
int64_t PeriodicSamplerBase::GetExponentialBiased(int period) noexcept {
|
||||
return rng_.GetStride(period);
|
||||
}
|
||||
|
||||
bool PeriodicSamplerBase::SubtleConfirmSample() noexcept {
|
||||
int current_period = period();
|
||||
|
||||
// Deal with period case 0 (always off) and 1 (always on)
|
||||
if (ABSL_PREDICT_FALSE(current_period < 2)) {
|
||||
stride_ = 0;
|
||||
return current_period == 1;
|
||||
}
|
||||
|
||||
// Check if this is the first call to Sample()
|
||||
if (ABSL_PREDICT_FALSE(stride_ == 1)) {
|
||||
stride_ = static_cast<uint64_t>(-GetExponentialBiased(current_period));
|
||||
if (static_cast<int64_t>(stride_) < -1) {
|
||||
++stride_;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
stride_ = static_cast<uint64_t>(-GetExponentialBiased(current_period));
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,211 @@
|
|||
// Copyright 2019 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_PERIODIC_SAMPLER_H_
|
||||
#define ABSL_BASE_INTERNAL_PERIODIC_SAMPLER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "absl/base/internal/exponential_biased.h"
|
||||
#include "absl/base/optimization.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// PeriodicSamplerBase provides the basic period sampler implementation.
|
||||
//
|
||||
// This is the base class for the templated PeriodicSampler class, which holds
|
||||
// a global std::atomic value identified by a user defined tag, such that
|
||||
// each specific PeriodSampler implementation holds its own global period.
|
||||
//
|
||||
// PeriodicSamplerBase is thread-compatible except where stated otherwise.
|
||||
class PeriodicSamplerBase {
|
||||
public:
|
||||
// PeriodicSamplerBase is trivial / copyable / movable / destructible.
|
||||
PeriodicSamplerBase() = default;
|
||||
PeriodicSamplerBase(PeriodicSamplerBase&&) = default;
|
||||
PeriodicSamplerBase(const PeriodicSamplerBase&) = default;
|
||||
|
||||
// Returns true roughly once every `period` calls. This is established by a
|
||||
// randomly picked `stride` that is counted down on each call to `Sample`.
|
||||
// This stride is picked such that the probability of `Sample()` returning
|
||||
// true is 1 in `period`.
|
||||
inline bool Sample() noexcept;
|
||||
|
||||
// The below methods are intended for optimized use cases where the
|
||||
// size of the inlined fast path code is highly important. Applications
|
||||
// should use the `Sample()` method unless they have proof that their
|
||||
// specific use case requires the optimizations offered by these methods.
|
||||
//
|
||||
// An example of such a use case is SwissTable sampling. All sampling checks
|
||||
// are in inlined SwissTable methods, and the number of call sites is huge.
|
||||
// In this case, the inlined code size added to each translation unit calling
|
||||
// SwissTable methods is non-trivial.
|
||||
//
|
||||
// The `SubtleMaybeSample()` function spuriously returns true even if the
|
||||
// function should not be sampled, applications MUST match each call to
|
||||
// 'SubtleMaybeSample()' returning true with a `SubtleConfirmSample()` call,
|
||||
// and use the result of the latter as the sampling decision.
|
||||
// In other words: the code should logically be equivalent to:
|
||||
//
|
||||
// if (SubtleMaybeSample() && SubtleConfirmSample()) {
|
||||
// // Sample this call
|
||||
// }
|
||||
//
|
||||
// In the 'inline-size' optimized case, the `SubtleConfirmSample()` call can
|
||||
// be placed out of line, for example, the typical use case looks as follows:
|
||||
//
|
||||
// // --- frobber.h -----------
|
||||
// void FrobberSampled();
|
||||
//
|
||||
// inline void FrobberImpl() {
|
||||
// // ...
|
||||
// }
|
||||
//
|
||||
// inline void Frobber() {
|
||||
// if (ABSL_PREDICT_FALSE(sampler.SubtleMaybeSample())) {
|
||||
// FrobberSampled();
|
||||
// } else {
|
||||
// FrobberImpl();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // --- frobber.cc -----------
|
||||
// void FrobberSampled() {
|
||||
// if (!sampler.SubtleConfirmSample())) {
|
||||
// // Spurious false positive
|
||||
// FrobberImpl();
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // Sampled execution
|
||||
// // ...
|
||||
// }
|
||||
inline bool SubtleMaybeSample() noexcept;
|
||||
bool SubtleConfirmSample() noexcept;
|
||||
|
||||
protected:
|
||||
// We explicitly don't use a virtual destructor as this class is never
|
||||
// virtually destroyed, and it keeps the class trivial, which avoids TLS
|
||||
// prologue and epilogue code for our TLS instances.
|
||||
~PeriodicSamplerBase() = default;
|
||||
|
||||
// Returns the next stride for our sampler.
|
||||
// This function is virtual for testing purposes only.
|
||||
virtual int64_t GetExponentialBiased(int period) noexcept;
|
||||
|
||||
private:
|
||||
// Returns the current period of this sampler. Thread-safe.
|
||||
virtual int period() const noexcept = 0;
|
||||
|
||||
// Keep and decrement stride_ as an unsigned integer, but compare the value
|
||||
// to zero casted as a signed int. clang and msvc do not create optimum code
|
||||
// if we use signed for the combined decrement and sign comparison.
|
||||
//
|
||||
// Below 3 alternative options, all compiles generate the best code
|
||||
// using the unsigned increment <---> signed int comparison option.
|
||||
//
|
||||
// Option 1:
|
||||
// int64_t stride_;
|
||||
// if (ABSL_PREDICT_TRUE(++stride_ < 0)) { ... }
|
||||
//
|
||||
// GCC x64 (OK) : https://gcc.godbolt.org/z/R5MzzA
|
||||
// GCC ppc (OK) : https://gcc.godbolt.org/z/z7NZAt
|
||||
// Clang x64 (BAD): https://gcc.godbolt.org/z/t4gPsd
|
||||
// ICC x64 (OK) : https://gcc.godbolt.org/z/rE6s8W
|
||||
// MSVC x64 (OK) : https://gcc.godbolt.org/z/ARMXqS
|
||||
//
|
||||
// Option 2:
|
||||
// int64_t stride_ = 0;
|
||||
// if (ABSL_PREDICT_TRUE(--stride_ >= 0)) { ... }
|
||||
//
|
||||
// GCC x64 (OK) : https://gcc.godbolt.org/z/jSQxYK
|
||||
// GCC ppc (OK) : https://gcc.godbolt.org/z/VJdYaA
|
||||
// Clang x64 (BAD): https://gcc.godbolt.org/z/Xm4NjX
|
||||
// ICC x64 (OK) : https://gcc.godbolt.org/z/4snaFd
|
||||
// MSVC x64 (BAD): https://gcc.godbolt.org/z/BgnEKE
|
||||
//
|
||||
// Option 3:
|
||||
// uint64_t stride_;
|
||||
// if (ABSL_PREDICT_TRUE(static_cast<int64_t>(++stride_) < 0)) { ... }
|
||||
//
|
||||
// GCC x64 (OK) : https://gcc.godbolt.org/z/bFbfPy
|
||||
// GCC ppc (OK) : https://gcc.godbolt.org/z/S9KkUE
|
||||
// Clang x64 (OK) : https://gcc.godbolt.org/z/UYzRb4
|
||||
// ICC x64 (OK) : https://gcc.godbolt.org/z/ptTNfD
|
||||
// MSVC x64 (OK) : https://gcc.godbolt.org/z/76j4-5
|
||||
uint64_t stride_ = 0;
|
||||
ExponentialBiased rng_;
|
||||
};
|
||||
|
||||
inline bool PeriodicSamplerBase::SubtleMaybeSample() noexcept {
|
||||
// See comments on `stride_` for the unsigned increment / signed compare.
|
||||
if (ABSL_PREDICT_TRUE(static_cast<int64_t>(++stride_) < 0)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PeriodicSamplerBase::Sample() noexcept {
|
||||
return ABSL_PREDICT_FALSE(SubtleMaybeSample()) ? SubtleConfirmSample()
|
||||
: false;
|
||||
}
|
||||
|
||||
// PeriodicSampler is a concreted periodic sampler implementation.
|
||||
// The user provided Tag identifies the implementation, and is required to
|
||||
// isolate the global state of this instance from other instances.
|
||||
//
|
||||
// Typical use case:
|
||||
//
|
||||
// struct HashTablezTag {};
|
||||
// thread_local PeriodicSampler sampler;
|
||||
//
|
||||
// void HashTableSamplingLogic(...) {
|
||||
// if (sampler.Sample()) {
|
||||
// HashTableSlowSamplePath(...);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
template <typename Tag, int default_period = 0>
|
||||
class PeriodicSampler final : public PeriodicSamplerBase {
|
||||
public:
|
||||
~PeriodicSampler() = default;
|
||||
|
||||
int period() const noexcept final {
|
||||
return period_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// Sets the global period for this sampler. Thread-safe.
|
||||
// Setting a period of 0 disables the sampler, i.e., every call to Sample()
|
||||
// will return false. Setting a period of 1 puts the sampler in 'always on'
|
||||
// mode, i.e., every call to Sample() returns true.
|
||||
static void SetGlobalPeriod(int period) {
|
||||
period_.store(period, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
private:
|
||||
static std::atomic<int> period_;
|
||||
};
|
||||
|
||||
template <typename Tag, int default_period>
|
||||
std::atomic<int> PeriodicSampler<Tag, default_period>::period_(default_period);
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_PERIODIC_SAMPLER_H_
|
|
@ -0,0 +1,79 @@
|
|||
// Copyright 2019 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 "benchmark/benchmark.h"
|
||||
#include "absl/base/internal/periodic_sampler.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
namespace {
|
||||
|
||||
template <typename Sampler>
|
||||
void BM_Sample(Sampler* sampler, benchmark::State& state) {
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(sampler);
|
||||
benchmark::DoNotOptimize(sampler->Sample());
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Sampler>
|
||||
void BM_SampleMinunumInlined(Sampler* sampler, benchmark::State& state) {
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(sampler);
|
||||
if (ABSL_PREDICT_FALSE(sampler->SubtleMaybeSample())) {
|
||||
benchmark::DoNotOptimize(sampler->SubtleConfirmSample());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BM_PeriodicSampler_TinySample(benchmark::State& state) {
|
||||
struct Tag {};
|
||||
PeriodicSampler<Tag, 10> sampler;
|
||||
BM_Sample(&sampler, state);
|
||||
}
|
||||
BENCHMARK(BM_PeriodicSampler_TinySample);
|
||||
|
||||
void BM_PeriodicSampler_ShortSample(benchmark::State& state) {
|
||||
struct Tag {};
|
||||
PeriodicSampler<Tag, 1024> sampler;
|
||||
BM_Sample(&sampler, state);
|
||||
}
|
||||
BENCHMARK(BM_PeriodicSampler_ShortSample);
|
||||
|
||||
void BM_PeriodicSampler_LongSample(benchmark::State& state) {
|
||||
struct Tag {};
|
||||
PeriodicSampler<Tag, 1024 * 1024> sampler;
|
||||
BM_Sample(&sampler, state);
|
||||
}
|
||||
BENCHMARK(BM_PeriodicSampler_LongSample);
|
||||
|
||||
void BM_PeriodicSampler_LongSampleMinunumInlined(benchmark::State& state) {
|
||||
struct Tag {};
|
||||
PeriodicSampler<Tag, 1024 * 1024> sampler;
|
||||
BM_SampleMinunumInlined(&sampler, state);
|
||||
}
|
||||
BENCHMARK(BM_PeriodicSampler_LongSampleMinunumInlined);
|
||||
|
||||
void BM_PeriodicSampler_Disabled(benchmark::State& state) {
|
||||
struct Tag {};
|
||||
PeriodicSampler<Tag, 0> sampler;
|
||||
BM_Sample(&sampler, state);
|
||||
}
|
||||
BENCHMARK(BM_PeriodicSampler_Disabled);
|
||||
|
||||
} // namespace
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,177 @@
|
|||
// Copyright 2019 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/base/internal/periodic_sampler.h"
|
||||
|
||||
#include <thread> // NOLINT(build/c++11)
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/macros.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
namespace {
|
||||
|
||||
using testing::Eq;
|
||||
using testing::Return;
|
||||
using testing::StrictMock;
|
||||
|
||||
class MockPeriodicSampler : public PeriodicSamplerBase {
|
||||
public:
|
||||
virtual ~MockPeriodicSampler() = default;
|
||||
|
||||
MOCK_METHOD(int, period, (), (const, noexcept));
|
||||
MOCK_METHOD(int64_t, GetExponentialBiased, (int), (noexcept));
|
||||
};
|
||||
|
||||
TEST(PeriodicSamplerBaseTest, Sample) {
|
||||
StrictMock<MockPeriodicSampler> sampler;
|
||||
|
||||
EXPECT_CALL(sampler, period()).Times(3).WillRepeatedly(Return(16));
|
||||
EXPECT_CALL(sampler, GetExponentialBiased(16))
|
||||
.WillOnce(Return(2))
|
||||
.WillOnce(Return(3))
|
||||
.WillOnce(Return(4));
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerBaseTest, ImmediatelySample) {
|
||||
StrictMock<MockPeriodicSampler> sampler;
|
||||
|
||||
EXPECT_CALL(sampler, period()).Times(2).WillRepeatedly(Return(16));
|
||||
EXPECT_CALL(sampler, GetExponentialBiased(16))
|
||||
.WillOnce(Return(1))
|
||||
.WillOnce(Return(2))
|
||||
.WillOnce(Return(3));
|
||||
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerBaseTest, Disabled) {
|
||||
StrictMock<MockPeriodicSampler> sampler;
|
||||
|
||||
EXPECT_CALL(sampler, period()).Times(3).WillRepeatedly(Return(0));
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerBaseTest, AlwaysOn) {
|
||||
StrictMock<MockPeriodicSampler> sampler;
|
||||
|
||||
EXPECT_CALL(sampler, period()).Times(3).WillRepeatedly(Return(1));
|
||||
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerBaseTest, Disable) {
|
||||
StrictMock<MockPeriodicSampler> sampler;
|
||||
|
||||
EXPECT_CALL(sampler, period()).WillOnce(Return(16));
|
||||
EXPECT_CALL(sampler, GetExponentialBiased(16)).WillOnce(Return(3));
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
|
||||
EXPECT_CALL(sampler, period()).Times(2).WillRepeatedly(Return(0));
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerBaseTest, Enable) {
|
||||
StrictMock<MockPeriodicSampler> sampler;
|
||||
|
||||
EXPECT_CALL(sampler, period()).WillOnce(Return(0));
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
|
||||
EXPECT_CALL(sampler, period()).Times(2).WillRepeatedly(Return(16));
|
||||
EXPECT_CALL(sampler, GetExponentialBiased(16))
|
||||
.Times(2)
|
||||
.WillRepeatedly(Return(3));
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_TRUE(sampler.Sample());
|
||||
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
EXPECT_FALSE(sampler.Sample());
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerTest, ConstructConstInit) {
|
||||
struct Tag {};
|
||||
ABSL_CONST_INIT static PeriodicSampler<Tag> sampler;
|
||||
(void)sampler;
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerTest, DefaultPeriod0) {
|
||||
struct Tag {};
|
||||
PeriodicSampler<Tag> sampler;
|
||||
EXPECT_THAT(sampler.period(), Eq(0));
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerTest, DefaultPeriod) {
|
||||
struct Tag {};
|
||||
PeriodicSampler<Tag, 100> sampler;
|
||||
EXPECT_THAT(sampler.period(), Eq(100));
|
||||
}
|
||||
|
||||
TEST(PeriodicSamplerTest, SetGlobalPeriod) {
|
||||
struct Tag1 {};
|
||||
struct Tag2 {};
|
||||
PeriodicSampler<Tag1, 25> sampler1;
|
||||
PeriodicSampler<Tag2, 50> sampler2;
|
||||
|
||||
EXPECT_THAT(sampler1.period(), Eq(25));
|
||||
EXPECT_THAT(sampler2.period(), Eq(50));
|
||||
|
||||
std::thread thread([] {
|
||||
PeriodicSampler<Tag1, 25> sampler1;
|
||||
PeriodicSampler<Tag2, 50> sampler2;
|
||||
EXPECT_THAT(sampler1.period(), Eq(25));
|
||||
EXPECT_THAT(sampler2.period(), Eq(50));
|
||||
sampler1.SetGlobalPeriod(10);
|
||||
sampler2.SetGlobalPeriod(20);
|
||||
});
|
||||
thread.join();
|
||||
|
||||
EXPECT_THAT(sampler1.period(), Eq(10));
|
||||
EXPECT_THAT(sampler2.period(), Eq(20));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2017 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.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_PRETTY_FUNCTION_H_
|
||||
#define ABSL_BASE_INTERNAL_PRETTY_FUNCTION_H_
|
||||
|
||||
// ABSL_PRETTY_FUNCTION
|
||||
//
|
||||
// In C++11, __func__ gives the undecorated name of the current function. That
|
||||
// is, "main", not "int main()". Various compilers give extra macros to get the
|
||||
// decorated function name, including return type and arguments, to
|
||||
// differentiate between overload sets. ABSL_PRETTY_FUNCTION is a portable
|
||||
// version of these macros which forwards to the correct macro on each compiler.
|
||||
#if defined(_MSC_VER)
|
||||
#define ABSL_PRETTY_FUNCTION __FUNCSIG__
|
||||
#elif defined(__GNUC__)
|
||||
#define ABSL_PRETTY_FUNCTION __PRETTY_FUNCTION__
|
||||
#else
|
||||
#error "Unsupported compiler"
|
||||
#endif
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_PRETTY_FUNCTION_H_
|
|
@ -0,0 +1,242 @@
|
|||
// Copyright 2017 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/base/internal/raw_logging.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/atomic_hook.h"
|
||||
#include "absl/base/log_severity.h"
|
||||
|
||||
// We know how to perform low-level writes to stderr in POSIX and Windows. For
|
||||
// these platforms, we define the token ABSL_LOW_LEVEL_WRITE_SUPPORTED.
|
||||
// Much of raw_logging.cc becomes a no-op when we can't output messages,
|
||||
// although a FATAL ABSL_RAW_LOG message will still abort the process.
|
||||
|
||||
// ABSL_HAVE_POSIX_WRITE is defined when the platform provides posix write()
|
||||
// (as from unistd.h)
|
||||
//
|
||||
// This preprocessor token is also defined in raw_io.cc. If you need to copy
|
||||
// this, consider moving both to config.h instead.
|
||||
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
|
||||
defined(__Fuchsia__) || defined(__native_client__) || \
|
||||
defined(__EMSCRIPTEN__) || defined(__ASYLO__)
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#define ABSL_HAVE_POSIX_WRITE 1
|
||||
#define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
|
||||
#else
|
||||
#undef ABSL_HAVE_POSIX_WRITE
|
||||
#endif
|
||||
|
||||
// ABSL_HAVE_SYSCALL_WRITE is defined when the platform provides the syscall
|
||||
// syscall(SYS_write, /*int*/ fd, /*char* */ buf, /*size_t*/ len);
|
||||
// for low level operations that want to avoid libc.
|
||||
#if (defined(__linux__) || defined(__FreeBSD__)) && !defined(__ANDROID__)
|
||||
#include <sys/syscall.h>
|
||||
#define ABSL_HAVE_SYSCALL_WRITE 1
|
||||
#define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
|
||||
#else
|
||||
#undef ABSL_HAVE_SYSCALL_WRITE
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
|
||||
#define ABSL_HAVE_RAW_IO 1
|
||||
#define ABSL_LOW_LEVEL_WRITE_SUPPORTED 1
|
||||
#else
|
||||
#undef ABSL_HAVE_RAW_IO
|
||||
#endif
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace raw_logging_internal {
|
||||
namespace {
|
||||
|
||||
// TODO(gfalcon): We want raw-logging to work on as many platforms as possible.
|
||||
// Explicitly `#error` out when not `ABSL_LOW_LEVEL_WRITE_SUPPORTED`, except for
|
||||
// a selected set of platforms for which we expect not to be able to raw log.
|
||||
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
|
||||
absl::base_internal::AtomicHook<LogPrefixHook>
|
||||
log_prefix_hook;
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
|
||||
absl::base_internal::AtomicHook<AbortHook>
|
||||
abort_hook;
|
||||
|
||||
#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||
constexpr char kTruncated[] = " ... (message truncated)\n";
|
||||
|
||||
// sprintf the format to the buffer, adjusting *buf and *size to reflect the
|
||||
// consumed bytes, and return whether the message fit without truncation. If
|
||||
// truncation occurred, if possible leave room in the buffer for the message
|
||||
// kTruncated[].
|
||||
bool VADoRawLog(char** buf, int* size, const char* format, va_list ap)
|
||||
ABSL_PRINTF_ATTRIBUTE(3, 0);
|
||||
bool VADoRawLog(char** buf, int* size, const char* format, va_list ap) {
|
||||
int n = vsnprintf(*buf, *size, format, ap);
|
||||
bool result = true;
|
||||
if (n < 0 || n > *size) {
|
||||
result = false;
|
||||
if (static_cast<size_t>(*size) > sizeof(kTruncated)) {
|
||||
n = *size - sizeof(kTruncated); // room for truncation message
|
||||
} else {
|
||||
n = 0; // no room for truncation message
|
||||
}
|
||||
}
|
||||
*size -= n;
|
||||
*buf += n;
|
||||
return result;
|
||||
}
|
||||
#endif // ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||
|
||||
constexpr int kLogBufSize = 3000;
|
||||
|
||||
// CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
|
||||
// that invoke malloc() and getenv() that might acquire some locks.
|
||||
|
||||
// Helper for RawLog below.
|
||||
// *DoRawLog writes to *buf of *size and move them past the written portion.
|
||||
// It returns true iff there was no overflow or error.
|
||||
bool DoRawLog(char** buf, int* size, const char* format, ...)
|
||||
ABSL_PRINTF_ATTRIBUTE(3, 4);
|
||||
bool DoRawLog(char** buf, int* size, const char* format, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
int n = vsnprintf(*buf, *size, format, ap);
|
||||
va_end(ap);
|
||||
if (n < 0 || n > *size) return false;
|
||||
*size -= n;
|
||||
*buf += n;
|
||||
return true;
|
||||
}
|
||||
|
||||
void RawLogVA(absl::LogSeverity severity, const char* file, int line,
|
||||
const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0);
|
||||
void RawLogVA(absl::LogSeverity severity, const char* file, int line,
|
||||
const char* format, va_list ap) {
|
||||
char buffer[kLogBufSize];
|
||||
char* buf = buffer;
|
||||
int size = sizeof(buffer);
|
||||
#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||
bool enabled = true;
|
||||
#else
|
||||
bool enabled = false;
|
||||
#endif
|
||||
|
||||
#ifdef ABSL_MIN_LOG_LEVEL
|
||||
if (severity < static_cast<absl::LogSeverity>(ABSL_MIN_LOG_LEVEL) &&
|
||||
severity < absl::LogSeverity::kFatal) {
|
||||
enabled = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto log_prefix_hook_ptr = log_prefix_hook.Load();
|
||||
if (log_prefix_hook_ptr) {
|
||||
enabled = log_prefix_hook_ptr(severity, file, line, &buf, &size);
|
||||
} else {
|
||||
if (enabled) {
|
||||
DoRawLog(&buf, &size, "[%s : %d] RAW: ", file, line);
|
||||
}
|
||||
}
|
||||
const char* const prefix_end = buf;
|
||||
|
||||
#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||
if (enabled) {
|
||||
bool no_chop = VADoRawLog(&buf, &size, format, ap);
|
||||
if (no_chop) {
|
||||
DoRawLog(&buf, &size, "\n");
|
||||
} else {
|
||||
DoRawLog(&buf, &size, "%s", kTruncated);
|
||||
}
|
||||
SafeWriteToStderr(buffer, strlen(buffer));
|
||||
}
|
||||
#else
|
||||
static_cast<void>(format);
|
||||
static_cast<void>(ap);
|
||||
#endif
|
||||
|
||||
// Abort the process after logging a FATAL message, even if the output itself
|
||||
// was suppressed.
|
||||
if (severity == absl::LogSeverity::kFatal) {
|
||||
abort_hook(file, line, buffer, prefix_end, buffer + kLogBufSize);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
// Non-formatting version of RawLog().
|
||||
//
|
||||
// TODO(gfalcon): When string_view no longer depends on base, change this
|
||||
// interface to take its message as a string_view instead.
|
||||
void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line,
|
||||
const std::string& message) {
|
||||
RawLog(severity, file, line, "%.*s", static_cast<int>(message.size()),
|
||||
message.data());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void SafeWriteToStderr(const char *s, size_t len) {
|
||||
#if defined(ABSL_HAVE_SYSCALL_WRITE)
|
||||
syscall(SYS_write, STDERR_FILENO, s, len);
|
||||
#elif defined(ABSL_HAVE_POSIX_WRITE)
|
||||
write(STDERR_FILENO, s, len);
|
||||
#elif defined(ABSL_HAVE_RAW_IO)
|
||||
_write(/* stderr */ 2, s, len);
|
||||
#else
|
||||
// stderr logging unsupported on this platform
|
||||
(void) s;
|
||||
(void) len;
|
||||
#endif
|
||||
}
|
||||
|
||||
void RawLog(absl::LogSeverity severity, const char* file, int line,
|
||||
const char* format, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
RawLogVA(severity, file, line, format, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
bool RawLoggingFullySupported() {
|
||||
#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||
return true;
|
||||
#else // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||
return false;
|
||||
#endif // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||
}
|
||||
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_DLL
|
||||
absl::base_internal::AtomicHook<InternalLogFunction>
|
||||
internal_log_function(DefaultInternalLog);
|
||||
|
||||
void RegisterLogPrefixHook(LogPrefixHook func) { log_prefix_hook.Store(func); }
|
||||
|
||||
void RegisterAbortHook(AbortHook func) { abort_hook.Store(func); }
|
||||
|
||||
void RegisterInternalLogFunction(InternalLogFunction func) {
|
||||
internal_log_function.Store(func);
|
||||
}
|
||||
|
||||
} // namespace raw_logging_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,195 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// Thread-safe logging routines that do not allocate any memory or
|
||||
// acquire any locks, and can therefore be used by low-level memory
|
||||
// allocation, synchronization, and signal-handling code.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
||||
#define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "absl/base/internal/atomic_hook.h"
|
||||
#include "absl/base/log_severity.h"
|
||||
#include "absl/base/macros.h"
|
||||
#include "absl/base/optimization.h"
|
||||
#include "absl/base/port.h"
|
||||
|
||||
// This is similar to LOG(severity) << format..., but
|
||||
// * it is to be used ONLY by low-level modules that can't use normal LOG()
|
||||
// * it is designed to be a low-level logger that does not allocate any
|
||||
// memory and does not need any locks, hence:
|
||||
// * it logs straight and ONLY to STDERR w/o buffering
|
||||
// * it uses an explicit printf-format and arguments list
|
||||
// * it will silently chop off really long message strings
|
||||
// Usage example:
|
||||
// ABSL_RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
|
||||
// This will print an almost standard log line like this to stderr only:
|
||||
// E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
|
||||
|
||||
#define ABSL_RAW_LOG(severity, ...) \
|
||||
do { \
|
||||
constexpr const char* absl_raw_logging_internal_basename = \
|
||||
::absl::raw_logging_internal::Basename(__FILE__, \
|
||||
sizeof(__FILE__) - 1); \
|
||||
::absl::raw_logging_internal::RawLog(ABSL_RAW_LOGGING_INTERNAL_##severity, \
|
||||
absl_raw_logging_internal_basename, \
|
||||
__LINE__, __VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
// Similar to CHECK(condition) << message, but for low-level modules:
|
||||
// we use only ABSL_RAW_LOG that does not allocate memory.
|
||||
// We do not want to provide args list here to encourage this usage:
|
||||
// if (!cond) ABSL_RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
|
||||
// so that the args are not computed when not needed.
|
||||
#define ABSL_RAW_CHECK(condition, message) \
|
||||
do { \
|
||||
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
||||
ABSL_RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
// ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
|
||||
// except that if the richer log library is linked into the binary, we dispatch
|
||||
// to that instead. This is potentially useful for internal logging and
|
||||
// assertions, where we are using RAW_LOG neither for its async-signal-safety
|
||||
// nor for its non-allocating nature, but rather because raw logging has very
|
||||
// few other dependencies.
|
||||
//
|
||||
// The API is a subset of the above: each macro only takes two arguments. Use
|
||||
// StrCat if you need to build a richer message.
|
||||
#define ABSL_INTERNAL_LOG(severity, message) \
|
||||
do { \
|
||||
constexpr const char* absl_raw_logging_internal_filename = __FILE__; \
|
||||
::absl::raw_logging_internal::internal_log_function( \
|
||||
ABSL_RAW_LOGGING_INTERNAL_##severity, \
|
||||
absl_raw_logging_internal_filename, __LINE__, message); \
|
||||
if (ABSL_RAW_LOGGING_INTERNAL_##severity == ::absl::LogSeverity::kFatal) \
|
||||
ABSL_INTERNAL_UNREACHABLE; \
|
||||
} while (0)
|
||||
|
||||
#define ABSL_INTERNAL_CHECK(condition, message) \
|
||||
do { \
|
||||
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
||||
std::string death_message = "Check " #condition " failed: "; \
|
||||
death_message += std::string(message); \
|
||||
ABSL_INTERNAL_LOG(FATAL, death_message); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
|
||||
#define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
|
||||
#define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
|
||||
#define ABSL_RAW_LOGGING_INTERNAL_FATAL ::absl::LogSeverity::kFatal
|
||||
#define ABSL_RAW_LOGGING_INTERNAL_LEVEL(severity) \
|
||||
::absl::NormalizeLogSeverity(severity)
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace raw_logging_internal {
|
||||
|
||||
// Helper function to implement ABSL_RAW_LOG
|
||||
// Logs format... at "severity" level, reporting it
|
||||
// as called from file:line.
|
||||
// This does not allocate memory or acquire locks.
|
||||
void RawLog(absl::LogSeverity severity, const char* file, int line,
|
||||
const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
|
||||
|
||||
// Writes the provided buffer directly to stderr, in a safe, low-level manner.
|
||||
//
|
||||
// In POSIX this means calling write(), which is async-signal safe and does
|
||||
// not malloc. If the platform supports the SYS_write syscall, we invoke that
|
||||
// directly to side-step any libc interception.
|
||||
void SafeWriteToStderr(const char *s, size_t len);
|
||||
|
||||
// compile-time function to get the "base" filename, that is, the part of
|
||||
// a filename after the last "/" or "\" path separator. The search starts at
|
||||
// the end of the string; the second parameter is the length of the string.
|
||||
constexpr const char* Basename(const char* fname, int offset) {
|
||||
return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
|
||||
? fname + offset
|
||||
: Basename(fname, offset - 1);
|
||||
}
|
||||
|
||||
// For testing only.
|
||||
// Returns true if raw logging is fully supported. When it is not
|
||||
// fully supported, no messages will be emitted, but a log at FATAL
|
||||
// severity will cause an abort.
|
||||
//
|
||||
// TODO(gfalcon): Come up with a better name for this method.
|
||||
bool RawLoggingFullySupported();
|
||||
|
||||
// Function type for a raw_logging customization hook for suppressing messages
|
||||
// by severity, and for writing custom prefixes on non-suppressed messages.
|
||||
//
|
||||
// The installed hook is called for every raw log invocation. The message will
|
||||
// be logged to stderr only if the hook returns true. FATAL errors will cause
|
||||
// the process to abort, even if writing to stderr is suppressed. The hook is
|
||||
// also provided with an output buffer, where it can write a custom log message
|
||||
// prefix.
|
||||
//
|
||||
// The raw_logging system does not allocate memory or grab locks. User-provided
|
||||
// hooks must avoid these operations, and must not throw exceptions.
|
||||
//
|
||||
// 'severity' is the severity level of the message being written.
|
||||
// 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
|
||||
// was located.
|
||||
// 'buffer' and 'buf_size' are pointers to the buffer and buffer size. If the
|
||||
// hook writes a prefix, it must increment *buffer and decrement *buf_size
|
||||
// accordingly.
|
||||
using LogPrefixHook = bool (*)(absl::LogSeverity severity, const char* file,
|
||||
int line, char** buffer, int* buf_size);
|
||||
|
||||
// Function type for a raw_logging customization hook called to abort a process
|
||||
// when a FATAL message is logged. If the provided AbortHook() returns, the
|
||||
// logging system will call abort().
|
||||
//
|
||||
// 'file' and 'line' are the file and line number where the ABSL_RAW_LOG macro
|
||||
// was located.
|
||||
// The NUL-terminated logged message lives in the buffer between 'buf_start'
|
||||
// and 'buf_end'. 'prefix_end' points to the first non-prefix character of the
|
||||
// buffer (as written by the LogPrefixHook.)
|
||||
using AbortHook = void (*)(const char* file, int line, const char* buf_start,
|
||||
const char* prefix_end, const char* buf_end);
|
||||
|
||||
// Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
|
||||
//
|
||||
// TODO(gfalcon): When string_view no longer depends on base, change this
|
||||
// interface to take its message as a string_view instead.
|
||||
using InternalLogFunction = void (*)(absl::LogSeverity severity,
|
||||
const char* file, int line,
|
||||
const std::string& message);
|
||||
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_DLL extern base_internal::AtomicHook<
|
||||
InternalLogFunction>
|
||||
internal_log_function;
|
||||
|
||||
// Registers hooks of the above types. Only a single hook of each type may be
|
||||
// registered. It is an error to call these functions multiple times with
|
||||
// different input arguments.
|
||||
//
|
||||
// These functions are safe to call at any point during initialization; they do
|
||||
// not block or malloc, and are async-signal safe.
|
||||
void RegisterLogPrefixHook(LogPrefixHook func);
|
||||
void RegisterAbortHook(AbortHook func);
|
||||
void RegisterInternalLogFunction(InternalLogFunction func);
|
||||
|
||||
} // namespace raw_logging_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// Core interfaces and definitions used by by low-level interfaces such as
|
||||
// SpinLock.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_SCHEDULING_MODE_H_
|
||||
#define ABSL_BASE_INTERNAL_SCHEDULING_MODE_H_
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
// Used to describe how a thread may be scheduled. Typically associated with
|
||||
// the declaration of a resource supporting synchronized access.
|
||||
//
|
||||
// SCHEDULE_COOPERATIVE_AND_KERNEL:
|
||||
// Specifies that when waiting, a cooperative thread (e.g. a Fiber) may
|
||||
// reschedule (using base::scheduling semantics); allowing other cooperative
|
||||
// threads to proceed.
|
||||
//
|
||||
// SCHEDULE_KERNEL_ONLY: (Also described as "non-cooperative")
|
||||
// Specifies that no cooperative scheduling semantics may be used, even if the
|
||||
// current thread is itself cooperatively scheduled. This means that
|
||||
// cooperative threads will NOT allow other cooperative threads to execute in
|
||||
// their place while waiting for a resource of this type. Host operating system
|
||||
// semantics (e.g. a futex) may still be used.
|
||||
//
|
||||
// When optional, clients should strongly prefer SCHEDULE_COOPERATIVE_AND_KERNEL
|
||||
// by default. SCHEDULE_KERNEL_ONLY should only be used for resources on which
|
||||
// base::scheduling (e.g. the implementation of a Scheduler) may depend.
|
||||
//
|
||||
// NOTE: Cooperative resources may not be nested below non-cooperative ones.
|
||||
// This means that it is invalid to to acquire a SCHEDULE_COOPERATIVE_AND_KERNEL
|
||||
// resource if a SCHEDULE_KERNEL_ONLY resource is already held.
|
||||
enum SchedulingMode {
|
||||
SCHEDULE_KERNEL_ONLY = 0, // Allow scheduling only the host OS.
|
||||
SCHEDULE_COOPERATIVE_AND_KERNEL, // Also allow cooperative scheduling.
|
||||
};
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_SCHEDULING_MODE_H_
|
|
@ -0,0 +1,81 @@
|
|||
// Copyright 2019 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/base/internal/scoped_set_env.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include "absl/base/internal/raw_logging.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
namespace {
|
||||
|
||||
#ifdef _WIN32
|
||||
const int kMaxEnvVarValueSize = 1024;
|
||||
#endif
|
||||
|
||||
void SetEnvVar(const char* name, const char* value) {
|
||||
#ifdef _WIN32
|
||||
SetEnvironmentVariableA(name, value);
|
||||
#else
|
||||
if (value == nullptr) {
|
||||
::unsetenv(name);
|
||||
} else {
|
||||
::setenv(name, value, 1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ScopedSetEnv::ScopedSetEnv(const char* var_name, const char* new_value)
|
||||
: var_name_(var_name), was_unset_(false) {
|
||||
#ifdef _WIN32
|
||||
char buf[kMaxEnvVarValueSize];
|
||||
auto get_res = GetEnvironmentVariableA(var_name_.c_str(), buf, sizeof(buf));
|
||||
ABSL_INTERNAL_CHECK(get_res < sizeof(buf), "value exceeds buffer size");
|
||||
|
||||
if (get_res == 0) {
|
||||
was_unset_ = (GetLastError() == ERROR_ENVVAR_NOT_FOUND);
|
||||
} else {
|
||||
old_value_.assign(buf, get_res);
|
||||
}
|
||||
|
||||
SetEnvironmentVariableA(var_name_.c_str(), new_value);
|
||||
#else
|
||||
const char* val = ::getenv(var_name_.c_str());
|
||||
if (val == nullptr) {
|
||||
was_unset_ = true;
|
||||
} else {
|
||||
old_value_ = val;
|
||||
}
|
||||
#endif
|
||||
|
||||
SetEnvVar(var_name_.c_str(), new_value);
|
||||
}
|
||||
|
||||
ScopedSetEnv::~ScopedSetEnv() {
|
||||
SetEnvVar(var_name_.c_str(), was_unset_ ? nullptr : old_value_.c_str());
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,45 @@
|
|||
//
|
||||
// Copyright 2019 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.
|
||||
//
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_SCOPED_SET_ENV_H_
|
||||
#define ABSL_BASE_INTERNAL_SCOPED_SET_ENV_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
class ScopedSetEnv {
|
||||
public:
|
||||
ScopedSetEnv(const char* var_name, const char* new_value);
|
||||
~ScopedSetEnv();
|
||||
|
||||
private:
|
||||
std::string var_name_;
|
||||
std::string old_value_;
|
||||
|
||||
// True if the environment variable was initially not set.
|
||||
bool was_unset_;
|
||||
};
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_SCOPED_SET_ENV_H_
|
|
@ -0,0 +1,99 @@
|
|||
// Copyright 2019 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.
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/base/internal/scoped_set_env.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using absl::base_internal::ScopedSetEnv;
|
||||
|
||||
std::string GetEnvVar(const char* name) {
|
||||
#ifdef _WIN32
|
||||
char buf[1024];
|
||||
auto get_res = GetEnvironmentVariableA(name, buf, sizeof(buf));
|
||||
if (get_res >= sizeof(buf)) {
|
||||
return "TOO_BIG";
|
||||
}
|
||||
|
||||
if (get_res == 0) {
|
||||
return "UNSET";
|
||||
}
|
||||
|
||||
return std::string(buf, get_res);
|
||||
#else
|
||||
const char* val = ::getenv(name);
|
||||
if (val == nullptr) {
|
||||
return "UNSET";
|
||||
}
|
||||
|
||||
return val;
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(ScopedSetEnvTest, SetNonExistingVarToString) {
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
|
||||
|
||||
{
|
||||
ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
|
||||
}
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
|
||||
}
|
||||
|
||||
TEST(ScopedSetEnvTest, SetNonExistingVarToNull) {
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
|
||||
|
||||
{
|
||||
ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", nullptr);
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
|
||||
}
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
|
||||
}
|
||||
|
||||
TEST(ScopedSetEnvTest, SetExistingVarToString) {
|
||||
ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
|
||||
|
||||
{
|
||||
ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "new_value");
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "new_value");
|
||||
}
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
|
||||
}
|
||||
|
||||
TEST(ScopedSetEnvTest, SetExistingVarToNull) {
|
||||
ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", "value");
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
|
||||
|
||||
{
|
||||
ScopedSetEnv scoped_set("SCOPED_SET_ENV_TEST_VAR", nullptr);
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "UNSET");
|
||||
}
|
||||
|
||||
EXPECT_EQ(GetEnvVar("SCOPED_SET_ENV_TEST_VAR"), "value");
|
||||
}
|
||||
|
||||
} // namespace
|
|
@ -0,0 +1,229 @@
|
|||
// Copyright 2017 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/base/internal/spinlock.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <limits>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/internal/atomic_hook.h"
|
||||
#include "absl/base/internal/cycleclock.h"
|
||||
#include "absl/base/internal/spinlock_wait.h"
|
||||
#include "absl/base/internal/sysinfo.h" /* For NumCPUs() */
|
||||
#include "absl/base/call_once.h"
|
||||
|
||||
// Description of lock-word:
|
||||
// 31..00: [............................3][2][1][0]
|
||||
//
|
||||
// [0]: kSpinLockHeld
|
||||
// [1]: kSpinLockCooperative
|
||||
// [2]: kSpinLockDisabledScheduling
|
||||
// [31..3]: ONLY kSpinLockSleeper OR
|
||||
// Wait time in cycles >> PROFILE_TIMESTAMP_SHIFT
|
||||
//
|
||||
// Detailed descriptions:
|
||||
//
|
||||
// Bit [0]: The lock is considered held iff kSpinLockHeld is set.
|
||||
//
|
||||
// Bit [1]: Eligible waiters (e.g. Fibers) may co-operatively reschedule when
|
||||
// contended iff kSpinLockCooperative is set.
|
||||
//
|
||||
// Bit [2]: This bit is exclusive from bit [1]. It is used only by a
|
||||
// non-cooperative lock. When set, indicates that scheduling was
|
||||
// successfully disabled when the lock was acquired. May be unset,
|
||||
// even if non-cooperative, if a ThreadIdentity did not yet exist at
|
||||
// time of acquisition.
|
||||
//
|
||||
// Bit [3]: If this is the only upper bit ([31..3]) set then this lock was
|
||||
// acquired without contention, however, at least one waiter exists.
|
||||
//
|
||||
// Otherwise, bits [31..3] represent the time spent by the current lock
|
||||
// holder to acquire the lock. There may be outstanding waiter(s).
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES static base_internal::AtomicHook<void (*)(
|
||||
const void *lock, int64_t wait_cycles)>
|
||||
submit_profile_data;
|
||||
|
||||
void RegisterSpinLockProfiler(void (*fn)(const void *contendedlock,
|
||||
int64_t wait_cycles)) {
|
||||
submit_profile_data.Store(fn);
|
||||
}
|
||||
|
||||
// Static member variable definitions.
|
||||
constexpr uint32_t SpinLock::kSpinLockHeld;
|
||||
constexpr uint32_t SpinLock::kSpinLockCooperative;
|
||||
constexpr uint32_t SpinLock::kSpinLockDisabledScheduling;
|
||||
constexpr uint32_t SpinLock::kSpinLockSleeper;
|
||||
constexpr uint32_t SpinLock::kWaitTimeMask;
|
||||
|
||||
// Uncommon constructors.
|
||||
SpinLock::SpinLock(base_internal::SchedulingMode mode)
|
||||
: lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) {
|
||||
ABSL_TSAN_MUTEX_CREATE(this, __tsan_mutex_not_static);
|
||||
}
|
||||
|
||||
// Monitor the lock to see if its value changes within some time period
|
||||
// (adaptive_spin_count loop iterations). The last value read from the lock
|
||||
// is returned from the method.
|
||||
uint32_t SpinLock::SpinLoop() {
|
||||
// We are already in the slow path of SpinLock, initialize the
|
||||
// adaptive_spin_count here.
|
||||
ABSL_CONST_INIT static absl::once_flag init_adaptive_spin_count;
|
||||
ABSL_CONST_INIT static int adaptive_spin_count = 0;
|
||||
base_internal::LowLevelCallOnce(&init_adaptive_spin_count, []() {
|
||||
adaptive_spin_count = base_internal::NumCPUs() > 1 ? 1000 : 1;
|
||||
});
|
||||
|
||||
int c = adaptive_spin_count;
|
||||
uint32_t lock_value;
|
||||
do {
|
||||
lock_value = lockword_.load(std::memory_order_relaxed);
|
||||
} while ((lock_value & kSpinLockHeld) != 0 && --c > 0);
|
||||
return lock_value;
|
||||
}
|
||||
|
||||
void SpinLock::SlowLock() {
|
||||
uint32_t lock_value = SpinLoop();
|
||||
lock_value = TryLockInternal(lock_value, 0);
|
||||
if ((lock_value & kSpinLockHeld) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
base_internal::SchedulingMode scheduling_mode;
|
||||
if ((lock_value & kSpinLockCooperative) != 0) {
|
||||
scheduling_mode = base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL;
|
||||
} else {
|
||||
scheduling_mode = base_internal::SCHEDULE_KERNEL_ONLY;
|
||||
}
|
||||
|
||||
// The lock was not obtained initially, so this thread needs to wait for
|
||||
// it. Record the current timestamp in the local variable wait_start_time
|
||||
// so the total wait time can be stored in the lockword once this thread
|
||||
// obtains the lock.
|
||||
int64_t wait_start_time = CycleClock::Now();
|
||||
uint32_t wait_cycles = 0;
|
||||
int lock_wait_call_count = 0;
|
||||
while ((lock_value & kSpinLockHeld) != 0) {
|
||||
// If the lock is currently held, but not marked as having a sleeper, mark
|
||||
// it as having a sleeper.
|
||||
if ((lock_value & kWaitTimeMask) == 0) {
|
||||
// Here, just "mark" that the thread is going to sleep. Don't store the
|
||||
// lock wait time in the lock -- the lock word stores the amount of time
|
||||
// that the current holder waited before acquiring the lock, not the wait
|
||||
// time of any thread currently waiting to acquire it.
|
||||
if (lockword_.compare_exchange_strong(
|
||||
lock_value, lock_value | kSpinLockSleeper,
|
||||
std::memory_order_relaxed, std::memory_order_relaxed)) {
|
||||
// Successfully transitioned to kSpinLockSleeper. Pass
|
||||
// kSpinLockSleeper to the SpinLockWait routine to properly indicate
|
||||
// the last lock_value observed.
|
||||
lock_value |= kSpinLockSleeper;
|
||||
} else if ((lock_value & kSpinLockHeld) == 0) {
|
||||
// Lock is free again, so try and acquire it before sleeping. The
|
||||
// new lock state will be the number of cycles this thread waited if
|
||||
// this thread obtains the lock.
|
||||
lock_value = TryLockInternal(lock_value, wait_cycles);
|
||||
continue; // Skip the delay at the end of the loop.
|
||||
} else if ((lock_value & kWaitTimeMask) == 0) {
|
||||
// The lock is still held, without a waiter being marked, but something
|
||||
// else about the lock word changed, causing our CAS to fail. For
|
||||
// example, a new lock holder may have acquired the lock with
|
||||
// kSpinLockDisabledScheduling set, whereas the previous holder had not
|
||||
// set that flag. In this case, attempt again to mark ourselves as a
|
||||
// waiter.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// SpinLockDelay() calls into fiber scheduler, we need to see
|
||||
// synchronization there to avoid false positives.
|
||||
ABSL_TSAN_MUTEX_PRE_DIVERT(this, 0);
|
||||
// Wait for an OS specific delay.
|
||||
base_internal::SpinLockDelay(&lockword_, lock_value, ++lock_wait_call_count,
|
||||
scheduling_mode);
|
||||
ABSL_TSAN_MUTEX_POST_DIVERT(this, 0);
|
||||
// Spin again after returning from the wait routine to give this thread
|
||||
// some chance of obtaining the lock.
|
||||
lock_value = SpinLoop();
|
||||
wait_cycles = EncodeWaitCycles(wait_start_time, CycleClock::Now());
|
||||
lock_value = TryLockInternal(lock_value, wait_cycles);
|
||||
}
|
||||
}
|
||||
|
||||
void SpinLock::SlowUnlock(uint32_t lock_value) {
|
||||
base_internal::SpinLockWake(&lockword_,
|
||||
false); // wake waiter if necessary
|
||||
|
||||
// If our acquisition was contended, collect contentionz profile info. We
|
||||
// reserve a unitary wait time to represent that a waiter exists without our
|
||||
// own acquisition having been contended.
|
||||
if ((lock_value & kWaitTimeMask) != kSpinLockSleeper) {
|
||||
const uint64_t wait_cycles = DecodeWaitCycles(lock_value);
|
||||
ABSL_TSAN_MUTEX_PRE_DIVERT(this, 0);
|
||||
submit_profile_data(this, wait_cycles);
|
||||
ABSL_TSAN_MUTEX_POST_DIVERT(this, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// We use the upper 29 bits of the lock word to store the time spent waiting to
|
||||
// acquire this lock. This is reported by contentionz profiling. Since the
|
||||
// lower bits of the cycle counter wrap very quickly on high-frequency
|
||||
// processors we divide to reduce the granularity to 2^kProfileTimestampShift
|
||||
// sized units. On a 4Ghz machine this will lose track of wait times greater
|
||||
// than (2^29/4 Ghz)*128 =~ 17.2 seconds. Such waits should be extremely rare.
|
||||
static constexpr int kProfileTimestampShift = 7;
|
||||
|
||||
// We currently reserve the lower 3 bits.
|
||||
static constexpr int kLockwordReservedShift = 3;
|
||||
|
||||
uint32_t SpinLock::EncodeWaitCycles(int64_t wait_start_time,
|
||||
int64_t wait_end_time) {
|
||||
static const int64_t kMaxWaitTime =
|
||||
std::numeric_limits<uint32_t>::max() >> kLockwordReservedShift;
|
||||
int64_t scaled_wait_time =
|
||||
(wait_end_time - wait_start_time) >> kProfileTimestampShift;
|
||||
|
||||
// Return a representation of the time spent waiting that can be stored in
|
||||
// the lock word's upper bits.
|
||||
uint32_t clamped = static_cast<uint32_t>(
|
||||
std::min(scaled_wait_time, kMaxWaitTime) << kLockwordReservedShift);
|
||||
|
||||
if (clamped == 0) {
|
||||
return kSpinLockSleeper; // Just wake waiters, but don't record contention.
|
||||
}
|
||||
// Bump up value if necessary to avoid returning kSpinLockSleeper.
|
||||
const uint32_t kMinWaitTime =
|
||||
kSpinLockSleeper + (1 << kLockwordReservedShift);
|
||||
if (clamped == kSpinLockSleeper) {
|
||||
return kMinWaitTime;
|
||||
}
|
||||
return clamped;
|
||||
}
|
||||
|
||||
uint64_t SpinLock::DecodeWaitCycles(uint32_t lock_value) {
|
||||
// Cast to uint32_t first to ensure bits [63:32] are cleared.
|
||||
const uint64_t scaled_wait_time =
|
||||
static_cast<uint32_t>(lock_value & kWaitTimeMask);
|
||||
return scaled_wait_time << (kProfileTimestampShift - kLockwordReservedShift);
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
|
@ -0,0 +1,246 @@
|
|||
//
|
||||
// Copyright 2017 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.
|
||||
//
|
||||
|
||||
// Most users requiring mutual exclusion should use Mutex.
|
||||
// SpinLock is provided for use in two situations:
|
||||
// - for use in code that Mutex itself depends on
|
||||
// - for async signal safety (see below)
|
||||
|
||||
// SpinLock is async signal safe. If a spinlock is used within a signal
|
||||
// handler, all code that acquires the lock must ensure that the signal cannot
|
||||
// arrive while they are holding the lock. Typically, this is done by blocking
|
||||
// the signal.
|
||||
|
||||
#ifndef ABSL_BASE_INTERNAL_SPINLOCK_H_
|
||||
#define ABSL_BASE_INTERNAL_SPINLOCK_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/const_init.h"
|
||||
#include "absl/base/dynamic_annotations.h"
|
||||
#include "absl/base/internal/low_level_scheduling.h"
|
||||
#include "absl/base/internal/raw_logging.h"
|
||||
#include "absl/base/internal/scheduling_mode.h"
|
||||
#include "absl/base/internal/tsan_mutex_interface.h"
|
||||
#include "absl/base/macros.h"
|
||||
#include "absl/base/port.h"
|
||||
#include "absl/base/thread_annotations.h"
|
||||
|
||||
namespace absl {
|
||||
ABSL_NAMESPACE_BEGIN
|
||||
namespace base_internal {
|
||||
|
||||
class ABSL_LOCKABLE SpinLock {
|
||||
public:
|
||||
SpinLock() : lockword_(kSpinLockCooperative) {
|
||||
ABSL_TSAN_MUTEX_CREATE(this, __tsan_mutex_not_static);
|
||||
}
|
||||
|
||||
// Constructors that allow non-cooperative spinlocks to be created for use
|
||||
// inside thread schedulers. Normal clients should not use these.
|
||||
explicit SpinLock(base_internal::SchedulingMode mode);
|
||||
|
||||
// Constructor for global SpinLock instances. See absl/base/const_init.h.
|
||||
constexpr SpinLock(absl::ConstInitType, base_internal::SchedulingMode mode)
|
||||
: lockword_(IsCooperative(mode) ? kSpinLockCooperative : 0) {}
|
||||
|
||||
// For global SpinLock instances prefer trivial destructor when possible.
|
||||
// Default but non-trivial destructor in some build configurations causes an
|
||||
// extra static initializer.
|
||||
#ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE
|
||||
~SpinLock() { ABSL_TSAN_MUTEX_DESTROY(this, __tsan_mutex_not_static); }
|
||||
#else
|
||||
~SpinLock() = default;
|
||||
#endif
|
||||
|
||||
// Acquire this SpinLock.
|
||||
inline void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() {
|
||||
ABSL_TSAN_MUTEX_PRE_LOCK(this, 0);
|
||||
if (!TryLockImpl()) {
|
||||
SlowLock();
|
||||
}
|
||||
ABSL_TSAN_MUTEX_POST_LOCK(this, 0, 0);
|
||||
}
|
||||
|
||||
// Try to acquire this SpinLock without blocking and return true if the
|
||||
// acquisition was successful. If the lock was not acquired, false is
|
||||
// returned. If this SpinLock is free at the time of the call, TryLock
|
||||
// will return true with high probability.
|
||||
inline bool TryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
|
||||
ABSL_TSAN_MUTEX_PRE_LOCK(this, __tsan_mutex_try_lock);
|
||||
bool res = TryLockImpl();
|
||||
ABSL_TSAN_MUTEX_POST_LOCK(
|
||||
this, __tsan_mutex_try_lock | (res ? 0 : __tsan_mutex_try_lock_failed),
|
||||
0);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Release this SpinLock, which must be held by the calling thread.
|
||||
inline void Unlock() ABSL_UNLOCK_FUNCTION() {
|
||||
ABSL_TSAN_MUTEX_PRE_UNLOCK(this, 0);
|
||||
uint32_t lock_value = lockword_.load(std::memory_order_relaxed);
|
||||
lock_value = lockword_.exchange(lock_value & kSpinLockCooperative,
|
||||
std::memory_order_release);
|
||||
|
||||
if ((lock_value & kSpinLockDisabledScheduling) != 0) {
|
||||
base_internal::SchedulingGuard::EnableRescheduling(true);
|
||||
}
|
||||
if ((lock_value & kWaitTimeMask) != 0) {
|
||||
// Collect contentionz profile info, and speed the wakeup of any waiter.
|
||||
// The wait_cycles value indicates how long this thread spent waiting
|
||||
// for the lock.
|
||||
SlowUnlock(lock_value);
|
||||
}
|
||||
ABSL_TSAN_MUTEX_POST_UNLOCK(this, 0);
|
||||
}
|
||||
|
||||
// Determine if the lock is held. When the lock is held by the invoking
|
||||
// thread, true will always be returned. Intended to be used as
|
||||
// CHECK(lock.IsHeld()).
|
||||
inline bool IsHeld() const {
|
||||
return (lockword_.load(std::memory_order_relaxed) & kSpinLockHeld) != 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
// These should not be exported except for testing.
|
||||
|
||||
// Store number of cycles between wait_start_time and wait_end_time in a
|
||||
// lock value.
|
||||
static uint32_t EncodeWaitCycles(int64_t wait_start_time,
|
||||
int64_t wait_end_time);
|
||||
|
||||
// Extract number of wait cycles in a lock value.
|
||||
static uint64_t DecodeWaitCycles(uint32_t lock_value);
|
||||
|
||||
// Provide access to protected method above. Use for testing only.
|
||||
friend struct SpinLockTest;
|
||||
|
||||
private:
|
||||
// lockword_ is used to store the following:
|
||||
//
|
||||
// bit[0] encodes whether a lock is being held.
|
||||
// bit[1] encodes whether a lock uses cooperative scheduling.
|
||||
// bit[2] encodes whether the current lock holder disabled scheduling when
|
||||
// acquiring the lock. Only set when kSpinLockHeld is also set.
|
||||
// bit[3:31] encodes time a lock spent on waiting as a 29-bit unsigned int.
|
||||
// This is set by the lock holder to indicate how long it waited on
|
||||
// the lock before eventually acquiring it. The number of cycles is
|
||||
// encoded as a 29-bit unsigned int, or in the case that the current
|
||||
// holder did not wait but another waiter is queued, the LSB
|
||||
// (kSpinLockSleeper) is set. The implementation does not explicitly
|
||||
// track the number of queued waiters beyond this. It must always be
|
||||
// assumed that waiters may exist if the current holder was required to
|
||||
// queue.
|
||||
//
|
||||
// Invariant: if the lock is not held, the value is either 0 or
|
||||
// kSpinLockCooperative.
|
||||
static constexpr uint32_t kSpinLockHeld = 1;
|
||||
static constexpr uint32_t kSpinLockCooperative = 2;
|
||||
static constexpr uint32_t kSpinLockDisabledScheduling = 4;
|
||||
static constexpr uint32_t kSpinLockSleeper = 8;
|
||||
// Includes kSpinLockSleeper.
|
||||
static constexpr uint32_t kWaitTimeMask =
|
||||
~(kSpinLockHeld | kSpinLockCooperative | kSpinLockDisabledScheduling);
|
||||
|
||||
// Returns true if the provided scheduling mode is cooperative.
|
||||
static constexpr bool IsCooperative(
|
||||
base_internal::SchedulingMode scheduling_mode) {
|
||||
return scheduling_mode == base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL;
|
||||
}
|
||||
|
||||
uint32_t TryLockInternal(uint32_t lock_value, uint32_t wait_cycles);
|
||||
void SlowLock() ABSL_ATTRIBUTE_COLD;
|
||||
void SlowUnlock(uint32_t lock_value) ABSL_ATTRIBUTE_COLD;
|
||||
uint32_t SpinLoop();
|
||||
|
||||
inline bool TryLockImpl() {
|
||||
uint32_t lock_value = lockword_.load(std::memory_order_relaxed);
|
||||
return (TryLockInternal(lock_value, 0) & kSpinLockHeld) == 0;
|
||||
}
|
||||
|
||||
std::atomic<uint32_t> lockword_;
|
||||
|
||||
SpinLock(const SpinLock&) = delete;
|
||||
SpinLock& operator=(const SpinLock&) = delete;
|
||||
};
|
||||
|
||||
// Corresponding locker object that arranges to acquire a spinlock for
|
||||
// the duration of a C++ scope.
|
||||
class ABSL_SCOPED_LOCKABLE SpinLockHolder {
|
||||
public:
|
||||
inline explicit SpinLockHolder(SpinLock* l) ABSL_EXCLUSIVE_LOCK_FUNCTION(l)
|
||||
: lock_(l) {
|
||||
l->Lock();
|
||||
}
|
||||
inline ~SpinLockHolder() ABSL_UNLOCK_FUNCTION() { lock_->Unlock(); }
|
||||
|
||||
SpinLockHolder(const SpinLockHolder&) = delete;
|
||||
SpinLockHolder& operator=(const SpinLockHolder&) = delete;
|
||||
|
||||
private:
|
||||
SpinLock* lock_;
|
||||
};
|
||||
|
||||
// Register a hook for profiling support.
|
||||
//
|
||||
// The function pointer registered here will be called whenever a spinlock is
|
||||
// contended. The callback is given an opaque handle to the contended spinlock
|
||||
// and the number of wait cycles. This is thread-safe, but only a single
|
||||
// profiler can be registered. It is an error to call this function multiple
|
||||
// times with different arguments.
|
||||
void RegisterSpinLockProfiler(void (*fn)(const void* lock,
|
||||
int64_t wait_cycles));
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Public interface ends here.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// If (result & kSpinLockHeld) == 0, then *this was successfully locked.
|
||||
// Otherwise, returns last observed value for lockword_.
|
||||
inline uint32_t SpinLock::TryLockInternal(uint32_t lock_value,
|
||||
uint32_t wait_cycles) {
|
||||
if ((lock_value & kSpinLockHeld) != 0) {
|
||||
return lock_value;
|
||||
}
|
||||
|
||||
uint32_t sched_disabled_bit = 0;
|
||||
if ((lock_value & kSpinLockCooperative) == 0) {
|
||||
// For non-cooperative locks we must make sure we mark ourselves as
|
||||
// non-reschedulable before we attempt to CompareAndSwap.
|
||||
if (base_internal::SchedulingGuard::DisableRescheduling()) {
|
||||
sched_disabled_bit = kSpinLockDisabledScheduling;
|
||||
}
|
||||
}
|
||||
|
||||
if (!lockword_.compare_exchange_strong(
|
||||
lock_value,
|
||||
kSpinLockHeld | lock_value | wait_cycles | sched_disabled_bit,
|
||||
std::memory_order_acquire, std::memory_order_relaxed)) {
|
||||
base_internal::SchedulingGuard::EnableRescheduling(sched_disabled_bit != 0);
|
||||
}
|
||||
|
||||
return lock_value;
|
||||
}
|
||||
|
||||
} // namespace base_internal
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
#endif // ABSL_BASE_INTERNAL_SPINLOCK_H_
|
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// This file is an Akaros-specific part of spinlock_wait.cc
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "absl/base/internal/scheduling_mode.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
||||
std::atomic<uint32_t>* /* lock_word */, uint32_t /* value */,
|
||||
int /* loop */, absl::base_internal::SchedulingMode /* mode */) {
|
||||
// In Akaros, one must take care not to call anything that could cause a
|
||||
// malloc(), a blocking system call, or a uthread_yield() while holding a
|
||||
// spinlock. Our callers assume will not call into libraries or other
|
||||
// arbitrary code.
|
||||
}
|
||||
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
|
||||
std::atomic<uint32_t>* /* lock_word */, bool /* all */) {}
|
||||
|
||||
} // extern "C"
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright 2018 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.
|
||||
|
||||
// See also //absl/synchronization:mutex_benchmark for a comparison of SpinLock
|
||||
// and Mutex performance under varying levels of contention.
|
||||
|
||||
#include "absl/base/internal/raw_logging.h"
|
||||
#include "absl/base/internal/scheduling_mode.h"
|
||||
#include "absl/base/internal/spinlock.h"
|
||||
#include "absl/synchronization/internal/create_thread_identity.h"
|
||||
#include "benchmark/benchmark.h"
|
||||
|
||||
namespace {
|
||||
|
||||
template <absl::base_internal::SchedulingMode scheduling_mode>
|
||||
static void BM_SpinLock(benchmark::State& state) {
|
||||
// Ensure a ThreadIdentity is installed.
|
||||
ABSL_INTERNAL_CHECK(
|
||||
absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() !=
|
||||
nullptr,
|
||||
"GetOrCreateCurrentThreadIdentity() failed");
|
||||
|
||||
static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode);
|
||||
for (auto _ : state) {
|
||||
absl::base_internal::SpinLockHolder holder(spinlock);
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK_TEMPLATE(BM_SpinLock,
|
||||
absl::base_internal::SCHEDULE_KERNEL_ONLY)
|
||||
->UseRealTime()
|
||||
->Threads(1)
|
||||
->ThreadPerCpu();
|
||||
|
||||
BENCHMARK_TEMPLATE(BM_SpinLock,
|
||||
absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL)
|
||||
->UseRealTime()
|
||||
->Threads(1)
|
||||
->ThreadPerCpu();
|
||||
|
||||
} // namespace
|
|
@ -0,0 +1,74 @@
|
|||
// Copyright 2018 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.
|
||||
//
|
||||
// This file is a Linux-specific part of spinlock_wait.cc
|
||||
|
||||
#include <linux/futex.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/internal/errno_saver.h"
|
||||
|
||||
// The SpinLock lockword is `std::atomic<uint32_t>`. Here we assert that
|
||||
// `std::atomic<uint32_t>` is bitwise equivalent of the `int` expected
|
||||
// by SYS_futex. We also assume that reads/writes done to the lockword
|
||||
// by SYS_futex have rational semantics with regard to the
|
||||
// std::atomic<> API. C++ provides no guarantees of these assumptions,
|
||||
// but they are believed to hold in practice.
|
||||
static_assert(sizeof(std::atomic<uint32_t>) == sizeof(int),
|
||||
"SpinLock lockword has the wrong size for a futex");
|
||||
|
||||
// Some Android headers are missing these definitions even though they
|
||||
// support these futex operations.
|
||||
#ifdef __BIONIC__
|
||||
#ifndef SYS_futex
|
||||
#define SYS_futex __NR_futex
|
||||
#endif
|
||||
#ifndef FUTEX_PRIVATE_FLAG
|
||||
#define FUTEX_PRIVATE_FLAG 128
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__NR_futex_time64) && !defined(SYS_futex_time64)
|
||||
#define SYS_futex_time64 __NR_futex_time64
|
||||
#endif
|
||||
|
||||
#if defined(SYS_futex_time64) && !defined(SYS_futex)
|
||||
#define SYS_futex SYS_futex_time64
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
||||
std::atomic<uint32_t> *w, uint32_t value, int loop,
|
||||
absl::base_internal::SchedulingMode) {
|
||||
absl::base_internal::ErrnoSaver errno_saver;
|
||||
struct timespec tm;
|
||||
tm.tv_sec = 0;
|
||||
tm.tv_nsec = absl::base_internal::SpinLockSuggestedDelayNS(loop);
|
||||
syscall(SYS_futex, w, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, &tm);
|
||||
}
|
||||
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
|
||||
std::atomic<uint32_t> *w, bool all) {
|
||||
syscall(SYS_futex, w, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, all ? INT_MAX : 1, 0);
|
||||
}
|
||||
|
||||
} // extern "C"
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2017 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.
|
||||
//
|
||||
// This file is a Posix-specific part of spinlock_wait.cc
|
||||
|
||||
#include <sched.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <ctime>
|
||||
|
||||
#include "absl/base/internal/errno_saver.h"
|
||||
#include "absl/base/internal/scheduling_mode.h"
|
||||
#include "absl/base/port.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
|
||||
std::atomic<uint32_t>* /* lock_word */, uint32_t /* value */, int loop,
|
||||
absl::base_internal::SchedulingMode /* mode */) {
|
||||
absl::base_internal::ErrnoSaver errno_saver;
|
||||
if (loop == 0) {
|
||||
} else if (loop == 1) {
|
||||
sched_yield();
|
||||
} else {
|
||||
struct timespec tm;
|
||||
tm.tv_sec = 0;
|
||||
tm.tv_nsec = absl::base_internal::SpinLockSuggestedDelayNS(loop);
|
||||
nanosleep(&tm, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
|
||||
std::atomic<uint32_t>* /* lock_word */, bool /* all */) {}
|
||||
|
||||
} // extern "C"
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue