├── .clang-format
├── .gitattributes
├── .gitignore
├── .style.yapf
├── AUTHORS
├── BUILD.gn
├── LICENSE
├── OWNERS
├── README.md
├── base
├── BUILD.gn
├── apple
│ ├── bridging.h
│ ├── foundation_util.h
│ ├── foundation_util.mm
│ ├── mach_logging.cc
│ ├── mach_logging.h
│ ├── scoped_cftyperef.h
│ ├── scoped_mach_port.cc
│ ├── scoped_mach_port.h
│ ├── scoped_mach_vm.cc
│ ├── scoped_mach_vm.h
│ ├── scoped_nsautorelease_pool.h
│ ├── scoped_nsautorelease_pool.mm
│ └── scoped_typeref.h
├── atomicops.h
├── atomicops_internals_atomicword_compat.h
├── atomicops_internals_portable.h
├── auto_reset.h
├── bit_cast.h
├── check.h
├── check_op.h
├── compiler_specific.h
├── containers
│ ├── checked_iterators.h
│ ├── dynamic_extent.h
│ ├── heap_array.h
│ ├── span.h
│ └── util.h
├── cxx17_backports.h
├── debug
│ ├── alias.cc
│ └── alias.h
├── files
│ ├── file_path.cc
│ ├── file_path.h
│ ├── file_util.h
│ ├── file_util_posix.cc
│ ├── scoped_file.cc
│ └── scoped_file.h
├── format_macros.h
├── fuchsia
│ ├── fuchsia_logging.cc
│ └── fuchsia_logging.h
├── immediate_crash.h
├── logging.cc
├── logging.h
├── mac
│ ├── close_nocancel.cc
│ ├── scoped_ioobject.h
│ └── scoped_launch_data.h
├── memory
│ ├── free_deleter.h
│ ├── page_size.h
│ ├── page_size_posix.cc
│ ├── page_size_win.cc
│ ├── raw_ptr_exclusion.h
│ └── scoped_policy.h
├── metrics
│ ├── histogram_functions.h
│ ├── histogram_macros.h
│ └── persistent_histogram_allocator.h
├── notreached.h
├── numerics
│ ├── basic_ops_impl.h
│ ├── byte_conversions.h
│ ├── checked_math.h
│ ├── checked_math_impl.h
│ ├── clamped_math.h
│ ├── clamped_math_impl.h
│ ├── safe_conversions.h
│ ├── safe_conversions_arm_impl.h
│ ├── safe_conversions_impl.h
│ ├── safe_math.h
│ ├── safe_math_arm_impl.h
│ ├── safe_math_clang_gcc_impl.h
│ └── safe_math_shared_impl.h
├── posix
│ ├── eintr_wrapper.h
│ ├── safe_strerror.cc
│ └── safe_strerror.h
├── process
│ ├── memory.cc
│ └── memory.h
├── rand_util.cc
├── rand_util.h
├── scoped_clear_last_error.h
├── scoped_clear_last_error_win.cc
├── scoped_generic.h
├── strings
│ ├── pattern.cc
│ ├── pattern.h
│ ├── strcat.cc
│ ├── strcat.h
│ ├── strcat_internal.h
│ ├── string_number_conversions.cc
│ ├── string_number_conversions.h
│ ├── string_piece.h
│ ├── string_util.cc
│ ├── string_util.h
│ ├── string_util_posix.h
│ ├── string_util_win.cc
│ ├── string_util_win.h
│ ├── stringprintf.cc
│ ├── stringprintf.h
│ ├── sys_string_conversions.h
│ ├── sys_string_conversions_mac.mm
│ ├── utf_string_conversion_utils.cc
│ ├── utf_string_conversion_utils.h
│ ├── utf_string_conversions.cc
│ └── utf_string_conversions.h
├── synchronization
│ ├── condition_variable.h
│ ├── condition_variable_posix.cc
│ ├── lock.cc
│ ├── lock.h
│ ├── lock_impl.h
│ ├── lock_impl_posix.cc
│ └── lock_impl_win.cc
├── sys_byteorder.h
├── template_util.h
├── third_party
│ └── icu
│ │ ├── LICENSE
│ │ ├── README.chromium
│ │ ├── icu_utf.cc
│ │ └── icu_utf.h
├── threading
│ ├── thread_local_storage.cc
│ ├── thread_local_storage.h
│ ├── thread_local_storage_posix.cc
│ └── thread_local_storage_win.cc
└── types
│ ├── cxx23_to_underlying.h
│ └── to_address.h
├── build
├── BUILD.gn
├── build_config.h
├── buildflag.h
├── buildflag_header.gni
├── compiler.gni
├── config
│ └── BUILD.gn
├── find_mac_sdk.py
├── ios
│ ├── Application-Info.plist
│ ├── BUILD.gn
│ ├── BuildInfo.plist
│ ├── Module-Info.plist
│ ├── XCTRunnerAddition+Info.plist
│ ├── codesign.py
│ ├── entitlements.plist
│ ├── find_signing_identity.py
│ ├── ios_sdk.gni
│ ├── plist_util.py
│ ├── rules.gni
│ ├── sdk_info.py
│ └── strip_arm64e.py
├── platform.gni
├── sysroot.gni
├── win_helper.py
└── write_buildflag_header.py
├── codereview.settings
└── testing
├── BUILD.gn
└── platform_test.h
/.clang-format:
--------------------------------------------------------------------------------
1 | # Copyright 2013 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | {
6 | BasedOnStyle: Chromium,
7 | AlignTrailingComments: false,
8 | BinPackArguments: false,
9 | InsertBraces: true,
10 | InsertNewlineAtEOF: true,
11 | Standard: Cpp11,
12 | }
13 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Copyright 2019 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | *.c text eol=lf
6 | *.cc text eol=lf
7 | *.gn text eol=lf
8 | *.gni text eol=lf
9 | *.gyp text eol=lf
10 | *.gypi text eol=lf
11 | *.h text eol=lf
12 | *.m text eol=lf
13 | *.md text eol=lf
14 | *.mm text eol=lf
15 | *.plist text eol=lf
16 | *.py text eol=lf
17 | .clang-format text eol=lf
18 | .gitattributes text eol=lf
19 | .gitignore text eol=lf
20 | /AUTHORS text eol=lf
21 | /LICENSE text eol=lf
22 | /codereview.settings text eol=lf
23 | README.chromium text eol=lf
24 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Copyright 2012 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | *.Makefile
6 | *.ninja
7 | *.pyc
8 | *.target.mk
9 | *.xcodeproj
10 | *~
11 | .*.sw?
12 | .DS_Store
13 | .gdb_history
14 | .gdbinit
15 | /Makefile
16 | /out
17 | /xcodebuild
18 |
--------------------------------------------------------------------------------
/.style.yapf:
--------------------------------------------------------------------------------
1 | # Copyright 2022 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | [style]
6 | based_on_style = google
7 |
--------------------------------------------------------------------------------
/AUTHORS:
--------------------------------------------------------------------------------
1 | # Copyright 2012 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | # See https://chromium.googlesource.com/chromium/src/+/main/AUTHORS
6 |
--------------------------------------------------------------------------------
/BUILD.gn:
--------------------------------------------------------------------------------
1 | # Copyright 2016 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | group("mini_chromium") {
6 | deps = [ "//base" ]
7 | }
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | // Copyright 2006-2008 The Chromium Authors
2 | //
3 | // Redistribution and use in source and binary forms, with or without
4 | // modification, are permitted provided that the following conditions are
5 | // met:
6 | //
7 | // * Redistributions of source code must retain the above copyright
8 | // notice, this list of conditions and the following disclaimer.
9 | // * Redistributions in binary form must reproduce the above
10 | // copyright notice, this list of conditions and the following disclaimer
11 | // in the documentation and/or other materials provided with the
12 | // distribution.
13 | // * Neither the name of Google Inc. nor the names of its
14 | // contributors may be used to endorse or promote products derived from
15 | // this software without specific prior written permission.
16 | //
17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
--------------------------------------------------------------------------------
/OWNERS:
--------------------------------------------------------------------------------
1 | # Copyright 2025 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | set noparent
6 |
7 | jperaza@chromium.org
8 | justincohen@chromium.org
9 | lgrey@chromium.org
10 | mark@chromium.org
11 | pbos@chromium.org
12 | wfh@chromium.org
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
6 |
7 | # mini_chromium
8 |
9 | This is mini_chromium, a small collection of useful low-level (“base”) routines
10 | from the [Chromium open-source project](https://dev.chromium.org/Home). Chromium
11 | is large, sprawling, full of dependencies, and a web browser. mini_chromium is
12 | small, self-contained, and a library. mini_chromium is especially useful as a
13 | dependency of other code that wishes to use Chromium’s base routines. By using
14 | mini_chromium, other projects’ code can function in a standalone environment
15 | outside of Chromium without having to treat all of Chromium as a dependency.
16 | When building as part of Chromium, those projects’ code can use Chromium’s own
17 | (non-mini_chromium) base implementation.
18 |
19 | Code provided in mini_chromium provides the same interface as the equivalent
20 | code in Chromium.
21 |
22 | While it’s a goal of mini_chromium to maintain interface compatibility with
23 | Chromium’s base library for the interfaces it does implement, there’s no
24 | requirement that it use the same implementations as Chromium’s base library.
25 | Many of the implementations used in mini_chromium are identical to Chromium’s,
26 | but many others have been modified to eliminate dependencies that are not
27 | desired in mini_chromium, and a few are completely distinct from Chromium’s
28 | altogether. Additionally, when mini_chromium provides an interface in the form
29 | of a file or class present in Chromium, it’s not bound to provide all functions,
30 | methods, or types that the Chromium equivalent does. The differences noted above
31 | notwithstanding, the interfaces exposed by mini_chromium’s base are and must
32 | remain a strict subset of Chromium’s.
33 |
34 | [Crashpad](https://crashpad.chromium.org/) is the chief consumer of
35 | mini_chromium.
36 |
37 | Mark Mentovai
38 | mark@chromium.org
39 |
--------------------------------------------------------------------------------
/base/BUILD.gn:
--------------------------------------------------------------------------------
1 | # Copyright 2016 The Chromium Authors
2 | # Use of this source code is governed by a BSD-style license that can be
3 | # found in the LICENSE file.
4 |
5 | import("../build/platform.gni")
6 |
7 | static_library("base") {
8 | sources = [
9 | "atomicops.h",
10 | "atomicops_internals_atomicword_compat.h",
11 | "atomicops_internals_portable.h",
12 | "auto_reset.h",
13 | "bit_cast.h",
14 | "check.h",
15 | "check_op.h",
16 | "compiler_specific.h",
17 | "containers/checked_iterators.h",
18 | "containers/dynamic_extent.h",
19 | "containers/span.h",
20 | "containers/util.h",
21 | "debug/alias.cc",
22 | "debug/alias.h",
23 | "files/file_path.cc",
24 | "files/file_path.h",
25 | "files/file_util.h",
26 | "files/scoped_file.cc",
27 | "files/scoped_file.h",
28 | "format_macros.h",
29 | "immediate_crash.h",
30 | "logging.cc",
31 | "logging.h",
32 | "memory/free_deleter.h",
33 | "memory/page_size.h",
34 | "memory/raw_ptr_exclusion.h",
35 | "memory/scoped_policy.h",
36 | "metrics/histogram_functions.h",
37 | "metrics/histogram_macros.h",
38 | "metrics/persistent_histogram_allocator.h",
39 | "notreached.h",
40 | "numerics/basic_ops_impl.h",
41 | "numerics/byte_conversions.h",
42 | "numerics/checked_math.h",
43 | "numerics/checked_math_impl.h",
44 | "numerics/clamped_math.h",
45 | "numerics/clamped_math_impl.h",
46 | "numerics/safe_conversions.h",
47 | "numerics/safe_conversions_arm_impl.h",
48 | "numerics/safe_conversions_impl.h",
49 | "numerics/safe_math.h",
50 | "numerics/safe_math_arm_impl.h",
51 | "numerics/safe_math_clang_gcc_impl.h",
52 | "numerics/safe_math_shared_impl.h",
53 | "process/memory.cc",
54 | "process/memory.h",
55 | "rand_util.cc",
56 | "rand_util.h",
57 | "scoped_clear_last_error.h",
58 | "scoped_generic.h",
59 | "strings/pattern.cc",
60 | "strings/pattern.h",
61 | "strings/strcat.cc",
62 | "strings/strcat.h",
63 | "strings/strcat_internal.h",
64 | "strings/string_number_conversions.cc",
65 | "strings/string_number_conversions.h",
66 | "strings/string_piece.h",
67 | "strings/string_util.h",
68 | "strings/stringprintf.cc",
69 | "strings/stringprintf.h",
70 | "strings/sys_string_conversions.h",
71 | "strings/utf_string_conversion_utils.cc",
72 | "strings/utf_string_conversion_utils.h",
73 | "strings/utf_string_conversions.cc",
74 | "strings/utf_string_conversions.h",
75 | "synchronization/condition_variable.h",
76 | "synchronization/lock.cc",
77 | "synchronization/lock.h",
78 | "synchronization/lock_impl.h",
79 | "sys_byteorder.h",
80 | "template_util.h",
81 | "third_party/icu/icu_utf.cc",
82 | "third_party/icu/icu_utf.h",
83 | "threading/thread_local_storage.cc",
84 | "threading/thread_local_storage.h",
85 | "types/cxx23_to_underlying.h",
86 | "types/to_address.h",
87 | ]
88 |
89 | if (mini_chromium_is_posix || mini_chromium_is_fuchsia) {
90 | sources += [
91 | "files/file_util_posix.cc",
92 | "memory/page_size_posix.cc",
93 | "posix/eintr_wrapper.h",
94 | "posix/safe_strerror.cc",
95 | "posix/safe_strerror.h",
96 | "strings/string_util_posix.h",
97 | "synchronization/condition_variable_posix.cc",
98 | "synchronization/lock_impl_posix.cc",
99 | "threading/thread_local_storage_posix.cc",
100 | ]
101 | }
102 |
103 | if (mini_chromium_is_apple) {
104 | sources += [
105 | "apple/bridging.h",
106 | "apple/foundation_util.h",
107 | "apple/foundation_util.mm",
108 | "apple/mach_logging.cc",
109 | "apple/mach_logging.h",
110 | "apple/scoped_cftyperef.h",
111 | "apple/scoped_mach_port.cc",
112 | "apple/scoped_mach_port.h",
113 | "apple/scoped_mach_vm.cc",
114 | "apple/scoped_mach_vm.h",
115 | "apple/scoped_nsautorelease_pool.h",
116 | "apple/scoped_nsautorelease_pool.mm",
117 | "apple/scoped_typeref.h",
118 | "strings/sys_string_conversions_mac.mm",
119 | ]
120 | frameworks = [
121 | "CoreFoundation.framework",
122 | "Foundation.framework",
123 | "Security.framework",
124 | ]
125 | }
126 | if (mini_chromium_is_mac) {
127 | sources += [
128 | "mac/close_nocancel.cc",
129 | "mac/scoped_ioobject.h",
130 | "mac/scoped_launch_data.h",
131 | ]
132 | frameworks += [
133 | "ApplicationServices.framework",
134 | "IOKit.framework",
135 | ]
136 | } else if (mini_chromium_is_ios) {
137 | frameworks += [
138 | "CoreGraphics.framework",
139 | "CoreText.framework",
140 | ]
141 | } else if (mini_chromium_is_win) {
142 | sources += [
143 | "memory/page_size_win.cc",
144 | "scoped_clear_last_error_win.cc",
145 | "strings/string_util_win.cc",
146 | "strings/string_util_win.h",
147 | "synchronization/lock_impl_win.cc",
148 | "threading/thread_local_storage_win.cc",
149 | ]
150 | libs = [ "advapi32.lib" ]
151 | } else if (mini_chromium_is_fuchsia) {
152 | sources += [
153 | "fuchsia/fuchsia_logging.cc",
154 | "fuchsia/fuchsia_logging.h",
155 | ]
156 |
157 | if (defined(is_fuchsia_tree) && is_fuchsia_tree) {
158 | deps = [ "//zircon/system/ulib/syslog" ]
159 | } else {
160 | deps = [ "//third_party/fuchsia/sdk/$host_os-amd64/pkg/syslog" ]
161 | }
162 | }
163 |
164 | public_configs = [ "../build:mini_chromium_config" ]
165 |
166 | public_deps = [ "../build" ]
167 |
168 | if (mini_chromium_is_apple) {
169 | public_configs += [ "../build/config:apple_enable_arc" ]
170 | }
171 |
172 | if (mini_chromium_is_android) {
173 | libs = [ "log" ]
174 | }
175 | }
176 |
--------------------------------------------------------------------------------
/base/apple/foundation_util.h:
--------------------------------------------------------------------------------
1 | // Copyright 2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_APPLE_FOUNDATION_UTIL_H_
6 | #define MINI_CHROMIUM_BASE_APPLE_FOUNDATION_UTIL_H_
7 |
8 | #include "base/logging.h"
9 | #include "build/build_config.h"
10 |
11 | #if BUILDFLAG(IS_IOS)
12 | #include
13 | #else
14 | #include
15 | #endif
16 |
17 | #if defined(__OBJC__)
18 | #import
19 | #else // defined(__OBJC__)
20 | #include
21 | #endif // defined(__OBJC__)
22 |
23 | namespace base {
24 | namespace apple {
25 |
26 | // CFCast<>() and CFCastStrict<>() cast a basic CFTypeRef to a more
27 | // specific CoreFoundation type. The compatibility of the passed
28 | // object is found by comparing its opaque type against the
29 | // requested type identifier. If the supplied object is not
30 | // compatible with the requested return type, CFCast<>() returns
31 | // NULL and CFCastStrict<>() will DCHECK. Providing a NULL pointer
32 | // to either variant results in NULL being returned without
33 | // triggering any DCHECK.
34 | //
35 | // Example usage:
36 | // CFNumberRef some_number = base::apple::CFCast(
37 | // CFArrayGetValueAtIndex(array, index));
38 | //
39 | // CFTypeRef hello = CFSTR("hello world");
40 | // CFStringRef some_string = base::apple::CFCastStrict(hello);
41 |
42 | template
43 | T CFCast(const CFTypeRef& cf_val);
44 |
45 | template
46 | T CFCastStrict(const CFTypeRef& cf_val);
47 |
48 | #define CF_CAST_DECL(TypeCF) \
49 | template <> \
50 | TypeCF##Ref CFCast(const CFTypeRef& cf_val); \
51 | \
52 | template <> \
53 | TypeCF##Ref CFCastStrict(const CFTypeRef& cf_val)
54 |
55 | CF_CAST_DECL(CFArray);
56 | CF_CAST_DECL(CFBag);
57 | CF_CAST_DECL(CFBoolean);
58 | CF_CAST_DECL(CFData);
59 | CF_CAST_DECL(CFDate);
60 | CF_CAST_DECL(CFDictionary);
61 | CF_CAST_DECL(CFNull);
62 | CF_CAST_DECL(CFNumber);
63 | CF_CAST_DECL(CFSet);
64 | CF_CAST_DECL(CFString);
65 | CF_CAST_DECL(CFURL);
66 | CF_CAST_DECL(CFUUID);
67 |
68 | CF_CAST_DECL(CGColor);
69 |
70 | CF_CAST_DECL(CTFont);
71 | CF_CAST_DECL(CTRun);
72 |
73 | #undef CF_CAST_DECL
74 |
75 | #if defined(__OBJC__)
76 |
77 | // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more
78 | // specific (NSObject-derived) type. The compatibility of the passed
79 | // object is found by checking if it's a kind of the requested type
80 | // identifier. If the supplied object is not compatible with the
81 | // requested return type, ObjCCast<>() returns nil and
82 | // ObjCCastStrict<>() will DCHECK. Providing a nil pointer to either
83 | // variant results in nil being returned without triggering any DCHECK.
84 | //
85 | // The strict variant is useful when retrieving a value from a
86 | // collection which only has values of a specific type, e.g. an
87 | // NSArray of NSStrings. The non-strict variant is useful when
88 | // retrieving values from data that you can't fully control. For
89 | // example, a plist read from disk may be beyond your exclusive
90 | // control, so you'd only want to check that the values you retrieve
91 | // from it are of the expected types, but not crash if they're not.
92 | //
93 | // Example usage:
94 | // NSString* version = base::apple::ObjCCast(
95 | // [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
96 | //
97 | // NSString* str = base::apple::ObjCCastStrict(
98 | // [ns_arr_of_ns_strs objectAtIndex:0]);
99 | template
100 | T* ObjCCast(id objc_val) {
101 | if ([objc_val isKindOfClass:[T class]]) {
102 | return reinterpret_cast(objc_val);
103 | }
104 | return nil;
105 | }
106 |
107 | template
108 | T* ObjCCastStrict(id objc_val) {
109 | T* rv = ObjCCast(objc_val);
110 | DCHECK(objc_val == nil || rv);
111 | return rv;
112 | }
113 |
114 | #endif // defined(__OBJC__)
115 |
116 | } // namespace apple
117 | } // namespace base
118 |
119 | #endif // MINI_CHROMIUM_BASE_APPLE_FOUNDATION_UTIL_H_
120 |
--------------------------------------------------------------------------------
/base/apple/foundation_util.mm:
--------------------------------------------------------------------------------
1 | // Copyright 2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/apple/foundation_util.h"
6 |
7 | #include "base/check.h"
8 |
9 | namespace base {
10 | namespace apple {
11 |
12 | #define CF_CAST_DEFN(TypeCF) \
13 | template <> \
14 | TypeCF##Ref CFCast(const CFTypeRef& cf_val) { \
15 | if (cf_val == NULL) { \
16 | return NULL; \
17 | } \
18 | if (CFGetTypeID(cf_val) == TypeCF##GetTypeID()) { \
19 | return (TypeCF##Ref)(cf_val); \
20 | } \
21 | return NULL; \
22 | } \
23 | \
24 | template <> \
25 | TypeCF##Ref CFCastStrict(const CFTypeRef& cf_val) { \
26 | TypeCF##Ref rv = CFCast(cf_val); \
27 | DCHECK(cf_val == NULL || rv); \
28 | return rv; \
29 | }
30 |
31 | CF_CAST_DEFN(CFArray)
32 | CF_CAST_DEFN(CFBag)
33 | CF_CAST_DEFN(CFBoolean)
34 | CF_CAST_DEFN(CFData)
35 | CF_CAST_DEFN(CFDate)
36 | CF_CAST_DEFN(CFDictionary)
37 | CF_CAST_DEFN(CFNull)
38 | CF_CAST_DEFN(CFNumber)
39 | CF_CAST_DEFN(CFSet)
40 | CF_CAST_DEFN(CFString)
41 | CF_CAST_DEFN(CFURL)
42 | CF_CAST_DEFN(CFUUID)
43 |
44 | CF_CAST_DEFN(CGColor)
45 |
46 | CF_CAST_DEFN(CTFont)
47 | CF_CAST_DEFN(CTRun)
48 |
49 | #undef CF_CAST_DEFN
50 |
51 | } // namespace apple
52 | } // namespace base
53 |
--------------------------------------------------------------------------------
/base/apple/mach_logging.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/apple/mach_logging.h"
6 |
7 | #include
8 | #include
9 |
10 | #include "base/immediate_crash.h"
11 | #include "base/strings/stringprintf.h"
12 |
13 | #if !BUILDFLAG(IS_IOS)
14 | #include
15 | #endif // !BUILDFLAG(IS_IOS)
16 |
17 | namespace {
18 |
19 | std::string FormatMachErrorNumber(mach_error_t mach_err) {
20 | // For the os/kern subsystem, give the error number in decimal as in
21 | // . Otherwise, give it in hexadecimal to make it easier
22 | // to visualize the various bits. See .
23 | if (mach_err >= 0 && mach_err < KERN_RETURN_MAX) {
24 | return base::StringPrintf(" (%d)", mach_err);
25 | }
26 | return base::StringPrintf(" (0x%08x)", mach_err);
27 | }
28 |
29 | } // namespace
30 |
31 | namespace logging {
32 |
33 | MachLogMessage::MachLogMessage(const char* function,
34 | const char* file_path,
35 | int line,
36 | LogSeverity severity,
37 | mach_error_t mach_err)
38 | : LogMessage(function, file_path, line, severity), mach_err_(mach_err) {}
39 |
40 | MachLogMessage::~MachLogMessage() {
41 | AppendError();
42 | }
43 |
44 | void MachLogMessage::AppendError() {
45 | stream() << ": " << mach_error_string(mach_err_)
46 | << FormatMachErrorNumber(mach_err_);
47 | }
48 |
49 | MachLogMessageFatal::~MachLogMessageFatal() {
50 | AppendError();
51 | Flush();
52 | base::ImmediateCrash();
53 | }
54 |
55 | #if !BUILDFLAG(IS_IOS)
56 |
57 | BootstrapLogMessage::BootstrapLogMessage(const char* function,
58 | const char* file_path,
59 | int line,
60 | LogSeverity severity,
61 | kern_return_t bootstrap_err)
62 | : LogMessage(function, file_path, line, severity),
63 | bootstrap_err_(bootstrap_err) {}
64 |
65 | BootstrapLogMessage::~BootstrapLogMessage() {
66 | AppendError();
67 | }
68 |
69 | void BootstrapLogMessage::AppendError() {
70 | stream() << ": " << bootstrap_strerror(bootstrap_err_);
71 |
72 | switch (bootstrap_err_) {
73 | case BOOTSTRAP_SUCCESS:
74 | case BOOTSTRAP_NOT_PRIVILEGED:
75 | case BOOTSTRAP_NAME_IN_USE:
76 | case BOOTSTRAP_UNKNOWN_SERVICE:
77 | case BOOTSTRAP_SERVICE_ACTIVE:
78 | case BOOTSTRAP_BAD_COUNT:
79 | case BOOTSTRAP_NO_MEMORY:
80 | case BOOTSTRAP_NO_CHILDREN: {
81 | // Show known bootstrap errors in decimal because that's how they're
82 | // defined in .
83 | stream() << " (" << bootstrap_err_ << ")";
84 | break;
85 | }
86 |
87 | default: {
88 | // bootstrap_strerror passes unknown errors to mach_error_string, so
89 | // format them as they would be if they were handled by
90 | // MachErrorMessage.
91 | stream() << FormatMachErrorNumber(bootstrap_err_);
92 | break;
93 | }
94 | }
95 | }
96 |
97 | BootstrapLogMessageFatal::~BootstrapLogMessageFatal() {
98 | AppendError();
99 | Flush();
100 | base::ImmediateCrash();
101 | }
102 |
103 | #endif // !BUILDFLAG(IS_IOS)
104 |
105 | } // namespace logging
106 |
--------------------------------------------------------------------------------
/base/apple/scoped_cftyperef.h:
--------------------------------------------------------------------------------
1 | // Copyright 2006-2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_APPLE_SCOPED_CFTYPEREF_H_
6 | #define MINI_CHROMIUM_BASE_APPLE_SCOPED_CFTYPEREF_H_
7 |
8 | #include
9 |
10 | #include "base/apple/scoped_typeref.h"
11 |
12 | namespace base {
13 | namespace apple {
14 |
15 | namespace internal {
16 |
17 | template
18 | struct ScopedCFTypeRefTraits {
19 | static CFT InvalidValue() { return nullptr; }
20 | static CFT Retain(CFT object) {
21 | CFRetain(object);
22 | return object;
23 | }
24 | static void Release(CFT object) { CFRelease(object); }
25 | };
26 |
27 | } // namespace internal
28 |
29 | template
30 | using ScopedCFTypeRef =
31 | ScopedTypeRef>;
32 |
33 | } // namespace apple
34 | } // namespace base
35 |
36 | #endif // MINI_CHROMIUM_BASE_APPLE_SCOPED_CFTYPEREF_H_
37 |
--------------------------------------------------------------------------------
/base/apple/scoped_mach_port.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2012 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/apple/scoped_mach_port.h"
6 |
7 | #include "base/apple/mach_logging.h"
8 |
9 | namespace base {
10 | namespace apple {
11 | namespace internal {
12 |
13 | void SendRightTraits::Free(mach_port_t port) {
14 | kern_return_t kr = mach_port_deallocate(mach_task_self(), port);
15 | MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr) << "mach_port_deallocate";
16 | }
17 |
18 | void ReceiveRightTraits::Free(mach_port_t port) {
19 | kern_return_t kr =
20 | mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
21 | MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr) << "mach_port_mod_refs";
22 | }
23 |
24 | void PortSetTraits::Free(mach_port_t port) {
25 | kern_return_t kr =
26 | mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_PORT_SET, -1);
27 | MACH_LOG_IF(ERROR, kr != KERN_SUCCESS, kr) << "mach_port_mod_refs";
28 | }
29 |
30 | } // namespace internal
31 | } // namespace apple
32 | } // namespace base
33 |
--------------------------------------------------------------------------------
/base/apple/scoped_mach_port.h:
--------------------------------------------------------------------------------
1 | // Copyright 2012 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_APPLE_SCOPED_MACH_PORT_H_
6 | #define MINI_CHROMIUM_BASE_APPLE_SCOPED_MACH_PORT_H_
7 |
8 | #include
9 |
10 | #include "base/scoped_generic.h"
11 |
12 | namespace base {
13 | namespace apple {
14 |
15 | namespace internal {
16 |
17 | struct SendRightTraits {
18 | static mach_port_t InvalidValue() { return MACH_PORT_NULL; }
19 | static void Free(mach_port_t port);
20 | };
21 |
22 | struct ReceiveRightTraits {
23 | static mach_port_t InvalidValue() { return MACH_PORT_NULL; }
24 | static void Free(mach_port_t port);
25 | };
26 |
27 | struct PortSetTraits {
28 | static mach_port_t InvalidValue() { return MACH_PORT_NULL; }
29 | static void Free(mach_port_t port);
30 | };
31 |
32 | } // namespace internal
33 |
34 | using ScopedMachSendRight =
35 | ScopedGeneric;
36 | using ScopedMachReceiveRight =
37 | ScopedGeneric;
38 | using ScopedMachPortSet = ScopedGeneric;
39 |
40 | } // namespace apple
41 | } // namespace base
42 |
43 | #endif // MINI_CHROMIUM_BASE_APPLE_SCOPED_MACH_PORT_H_
44 |
--------------------------------------------------------------------------------
/base/apple/scoped_mach_vm.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/apple/scoped_mach_vm.h"
6 |
7 | namespace base {
8 | namespace apple {
9 |
10 | void ScopedMachVM::reset(vm_address_t address, vm_size_t size) {
11 | DCHECK(address % PAGE_SIZE == 0);
12 | DCHECK(size % PAGE_SIZE == 0);
13 |
14 | if (size_) {
15 | if (address_ < address) {
16 | vm_deallocate(
17 | mach_task_self(), address_, std::min(size_, address - address_));
18 | }
19 | if (address_ + size_ > address + size) {
20 | vm_address_t deallocate_start = std::max(address_, address + size);
21 | vm_deallocate(mach_task_self(),
22 | deallocate_start,
23 | address_ + size_ - deallocate_start);
24 | }
25 | }
26 |
27 | address_ = address;
28 | size_ = size;
29 | }
30 |
31 | } // namespace apple
32 | } // namespace base
33 |
--------------------------------------------------------------------------------
/base/apple/scoped_mach_vm.h:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_APPLE_SCOPED_MACH_VM_H_
6 | #define MINI_CHROMIUM_BASE_APPLE_SCOPED_MACH_VM_H_
7 |
8 | #include
9 |
10 | #include
11 |
12 | #include "base/check.h"
13 |
14 | // Use ScopedMachVM to supervise ownership of pages in the current process
15 | // through the Mach VM subsystem. Pages allocated with vm_allocate can be
16 | // released when exiting a scope with ScopedMachVM.
17 | //
18 | // The Mach VM subsystem operates on a page-by-page basis, and a single VM
19 | // allocation managed by a ScopedMachVM object may span multiple pages. As far
20 | // as Mach is concerned, allocated pages may be deallocated individually. This
21 | // is in contrast to higher-level allocators such as malloc, where the base
22 | // address of an allocation implies the size of an allocated block.
23 | // Consequently, it is not sufficient to just pass the base address of an
24 | // allocation to ScopedMachVM, it also needs to know the size of the
25 | // allocation. To avoid any confusion, both the base address and size must
26 | // be page-aligned.
27 | //
28 | // When dealing with Mach VM, base addresses will naturally be page-aligned,
29 | // but user-specified sizes may not be. If there's a concern that a size is
30 | // not page-aligned, use the mach_vm_round_page macro to correct it.
31 | //
32 | // Example:
33 | //
34 | // vm_address_t address = 0;
35 | // vm_size_t size = 12345; // This requested size is not page-aligned.
36 | // kern_return_t kr =
37 | // vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
38 | // if (kr != KERN_SUCCESS) {
39 | // return false;
40 | // }
41 | // ScopedMachVM vm_owner(address, mach_vm_round_page(size));
42 |
43 | namespace base {
44 | namespace apple {
45 |
46 | class ScopedMachVM {
47 | public:
48 | explicit ScopedMachVM(vm_address_t address = 0, vm_size_t size = 0)
49 | : address_(address), size_(size) {
50 | DCHECK(address % PAGE_SIZE == 0);
51 | DCHECK(size % PAGE_SIZE == 0);
52 | }
53 |
54 | ScopedMachVM(const ScopedMachVM&) = delete;
55 | ScopedMachVM& operator=(const ScopedMachVM&) = delete;
56 |
57 | ~ScopedMachVM() {
58 | if (size_) {
59 | vm_deallocate(mach_task_self(), address_, size_);
60 | }
61 | }
62 |
63 | void reset(vm_address_t address = 0, vm_size_t size = 0);
64 |
65 | vm_address_t address() const { return address_; }
66 |
67 | vm_size_t size() const { return size_; }
68 |
69 | void swap(ScopedMachVM& that) {
70 | std::swap(address_, that.address_);
71 | std::swap(size_, that.size_);
72 | }
73 |
74 | void release() {
75 | address_ = 0;
76 | size_ = 0;
77 | }
78 |
79 | private:
80 | vm_address_t address_;
81 | vm_size_t size_;
82 | };
83 |
84 | } // namespace apple
85 | } // namespace base
86 |
87 | #endif // MINI_CHROMIUM_BASE_APPLE_SCOPED_MACH_VM_H_
88 |
--------------------------------------------------------------------------------
/base/apple/scoped_nsautorelease_pool.h:
--------------------------------------------------------------------------------
1 | // Copyright 2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_APPLE_SCOPED_NSAUTORELEASE_POOL_H_
6 | #define MINI_CHROMIUM_BASE_APPLE_SCOPED_NSAUTORELEASE_POOL_H_
7 |
8 | #include "build/build_config.h"
9 |
10 | namespace base {
11 | namespace apple {
12 |
13 | // On the Mac, ScopedNSAutoreleasePool creates an autorelease pool when
14 | // instantiated and pops it when destroyed. This allows an autorelease pool to
15 | // be maintained in ordinary C++ code without bringing in any direct Objective-C
16 | // dependency.
17 | //
18 | // On other platforms, ScopedNSAutoreleasePool is an empty object with no
19 | // effects. This allows it to be used directly in cross-platform code without
20 | // ugly #ifdefs.
21 | class ScopedNSAutoreleasePool {
22 | public:
23 | #if !BUILDFLAG(IS_APPLE)
24 | ScopedNSAutoreleasePool() {}
25 | void Recycle() {}
26 | #else // BUILDFLAG(IS_APPLE)
27 | ScopedNSAutoreleasePool();
28 |
29 | ScopedNSAutoreleasePool(const ScopedNSAutoreleasePool&) = delete;
30 | ScopedNSAutoreleasePool& operator=(const ScopedNSAutoreleasePool&) = delete;
31 |
32 | ~ScopedNSAutoreleasePool();
33 |
34 | // Clear out the pool in case its position on the stack causes it to be
35 | // alive for long periods of time (such as the entire length of the app).
36 | // Only use then when you're certain the items currently in the pool are
37 | // no longer needed.
38 | void Recycle();
39 |
40 | private:
41 | void* autorelease_pool_;
42 | #endif // BUILDFLAG(IS_APPLE)
43 | };
44 |
45 | } // namespace apple
46 | } // namespace base
47 |
48 | #endif // MINI_CHROMIUM_BASE_APPLE_SCOPED_NSAUTORELEASE_POOL_H_
49 |
--------------------------------------------------------------------------------
/base/apple/scoped_nsautorelease_pool.mm:
--------------------------------------------------------------------------------
1 | // Copyright 2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/apple/scoped_nsautorelease_pool.h"
6 |
7 | #include "base/logging.h"
8 |
9 | // Note that this uses the direct runtime interface to the autorelease pool.
10 | // https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support
11 | // This is so this can work when compiled for ARC.
12 |
13 | extern "C" {
14 | void* objc_autoreleasePoolPush(void);
15 | void objc_autoreleasePoolPop(void* pool);
16 | }
17 |
18 | namespace base {
19 | namespace apple {
20 |
21 | ScopedNSAutoreleasePool::ScopedNSAutoreleasePool()
22 | : autorelease_pool_(objc_autoreleasePoolPush()) {}
23 |
24 | ScopedNSAutoreleasePool::~ScopedNSAutoreleasePool() {
25 | objc_autoreleasePoolPop(autorelease_pool_);
26 | }
27 |
28 | // Cycle the internal pool, allowing everything there to get cleaned up and
29 | // start anew.
30 | void ScopedNSAutoreleasePool::Recycle() {
31 | objc_autoreleasePoolPop(autorelease_pool_);
32 | autorelease_pool_ = objc_autoreleasePoolPush();
33 | }
34 |
35 | } // namespace apple
36 | } // namespace base
37 |
--------------------------------------------------------------------------------
/base/apple/scoped_typeref.h:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_APPLE_SCOPED_TYPEREF_H_
6 | #define MINI_CHROMIUM_BASE_APPLE_SCOPED_TYPEREF_H_
7 |
8 | #include "base/check.h"
9 | #include "base/memory/scoped_policy.h"
10 |
11 | namespace base {
12 | namespace apple {
13 |
14 | template
15 | struct ScopedTypeRefTraits;
16 |
17 | template >
18 | class ScopedTypeRef {
19 | public:
20 | using element_type = T;
21 |
22 | // Construction from underlying type
23 |
24 | explicit constexpr ScopedTypeRef(
25 | element_type object = Traits::InvalidValue(),
26 | base::scoped_policy::OwnershipPolicy policy = base::scoped_policy::ASSUME)
27 | : object_(object) {
28 | if (object_ != Traits::InvalidValue() &&
29 | policy == base::scoped_policy::RETAIN) {
30 | object_ = Traits::Retain(object_);
31 | }
32 | }
33 |
34 | // Copy construction
35 |
36 | ScopedTypeRef(const ScopedTypeRef& that) : object_(that.get()) {
37 | if (object_ != Traits::InvalidValue()) {
38 | object_ = Traits::Retain(object_);
39 | }
40 | }
41 |
42 | template
43 | ScopedTypeRef(const ScopedTypeRef& that) : object_(that.get()) {
44 | if (object_ != Traits::InvalidValue()) {
45 | object_ = Traits::Retain(object_);
46 | }
47 | }
48 |
49 | // Copy assignment
50 |
51 | ScopedTypeRef& operator=(const ScopedTypeRef& that) {
52 | reset(that.get(), base::scoped_policy::RETAIN);
53 | return *this;
54 | }
55 |
56 | template
57 | ScopedTypeRef& operator=(const ScopedTypeRef& that) {
58 | reset(that.get(), base::scoped_policy::RETAIN);
59 | return *this;
60 | }
61 |
62 | // Move construction
63 |
64 | ScopedTypeRef(ScopedTypeRef&& that) : object_(that.release()) {}
65 |
66 | template
67 | ScopedTypeRef(ScopedTypeRef&& that) : object_(that.release()) {}
68 |
69 | // Move assignment
70 |
71 | ScopedTypeRef& operator=(ScopedTypeRef&& that) {
72 | reset(that.release(), base::scoped_policy::ASSUME);
73 | return *this;
74 | }
75 |
76 | template
77 | ScopedTypeRef& operator=(ScopedTypeRef&& that) {
78 | reset(that.release(), base::scoped_policy::ASSUME);
79 | return *this;
80 | }
81 |
82 | // Resetting
83 |
84 | template
85 | void reset(const ScopedTypeRef& that) {
86 | reset(that.get(), base::scoped_policy::RETAIN);
87 | }
88 |
89 | void reset(element_type object = Traits::InvalidValue(),
90 | base::scoped_policy::OwnershipPolicy policy =
91 | base::scoped_policy::ASSUME) {
92 | if (object != Traits::InvalidValue() &&
93 | policy == base::scoped_policy::RETAIN) {
94 | object = Traits::Retain(object);
95 | }
96 | if (object_ != Traits::InvalidValue()) {
97 | Traits::Release(object_);
98 | }
99 | object_ = object;
100 | }
101 |
102 | // Destruction
103 |
104 | ~ScopedTypeRef() {
105 | if (object_ != Traits::InvalidValue()) {
106 | Traits::Release(object_);
107 | }
108 | }
109 |
110 | [[nodiscard]] element_type* InitializeInto() {
111 | CHECK_EQ(object_, Traits::InvalidValue());
112 | return &object_;
113 | }
114 |
115 | bool operator==(const ScopedTypeRef& that) const {
116 | return object_ == that.object_;
117 | }
118 | bool operator!=(const ScopedTypeRef& that) const {
119 | return object_ != that.object_;
120 | }
121 | explicit operator bool() const { return object_ != Traits::InvalidValue(); }
122 |
123 | element_type get() const { return object_; }
124 |
125 | void swap(ScopedTypeRef& that) {
126 | element_type temp = that.object_;
127 | that.object_ = object_;
128 | object_ = temp;
129 | }
130 |
131 | [[nodiscard]] element_type release() {
132 | element_type temp = object_;
133 | object_ = Traits::InvalidValue();
134 | return temp;
135 | }
136 |
137 | private:
138 | element_type object_;
139 | };
140 |
141 | } // namespace apple
142 | } // namespace base
143 |
144 | #endif // MINI_CHROMIUM_BASE_APPLE_SCOPED_TYPEREF_H_
145 |
--------------------------------------------------------------------------------
/base/atomicops_internals_atomicword_compat.h:
--------------------------------------------------------------------------------
1 | // Copyright 2011 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | // This file is an internal atomic implementation, use base/atomicops.h instead.
6 |
7 | #ifndef MINI_CHROMIUM_BASE_ATOMICOPS_INTERNALS_ATOMICWORD_COMPAT_H_
8 | #define MINI_CHROMIUM_BASE_ATOMICOPS_INTERNALS_ATOMICWORD_COMPAT_H_
9 |
10 | // AtomicWord is a synonym for intptr_t, and Atomic32 is a synonym for int32,
11 | // which in turn means int. On some LP32 platforms, intptr_t is an int, but
12 | // on others, it's a long. When AtomicWord and Atomic32 are based on different
13 | // fundamental types, their pointers are incompatible.
14 | //
15 | // This file defines function overloads to allow both AtomicWord and Atomic32
16 | // data to be used with this interface.
17 | //
18 | // On LP64 platforms, AtomicWord and Atomic64 are both always long,
19 | // so this problem doesn't occur.
20 |
21 | #if !defined(ARCH_CPU_64_BITS)
22 |
23 | namespace base {
24 | namespace subtle {
25 |
26 | inline AtomicWord NoBarrier_CompareAndSwap(volatile AtomicWord* ptr,
27 | AtomicWord old_value,
28 | AtomicWord new_value) {
29 | return NoBarrier_CompareAndSwap(
30 | reinterpret_cast(ptr), old_value, new_value);
31 | }
32 |
33 | inline AtomicWord NoBarrier_AtomicExchange(volatile AtomicWord* ptr,
34 | AtomicWord new_value) {
35 | return NoBarrier_AtomicExchange(
36 | reinterpret_cast(ptr), new_value);
37 | }
38 |
39 | inline AtomicWord NoBarrier_AtomicIncrement(volatile AtomicWord* ptr,
40 | AtomicWord increment) {
41 | return NoBarrier_AtomicIncrement(
42 | reinterpret_cast(ptr), increment);
43 | }
44 |
45 | inline AtomicWord Barrier_AtomicIncrement(volatile AtomicWord* ptr,
46 | AtomicWord increment) {
47 | return Barrier_AtomicIncrement(
48 | reinterpret_cast(ptr), increment);
49 | }
50 |
51 | inline AtomicWord Acquire_CompareAndSwap(volatile AtomicWord* ptr,
52 | AtomicWord old_value,
53 | AtomicWord new_value) {
54 | return base::subtle::Acquire_CompareAndSwap(
55 | reinterpret_cast(ptr), old_value, new_value);
56 | }
57 |
58 | inline AtomicWord Release_CompareAndSwap(volatile AtomicWord* ptr,
59 | AtomicWord old_value,
60 | AtomicWord new_value) {
61 | return base::subtle::Release_CompareAndSwap(
62 | reinterpret_cast(ptr), old_value, new_value);
63 | }
64 |
65 | inline void NoBarrier_Store(volatile AtomicWord *ptr, AtomicWord value) {
66 | NoBarrier_Store(
67 | reinterpret_cast(ptr), value);
68 | }
69 |
70 | inline void Acquire_Store(volatile AtomicWord* ptr, AtomicWord value) {
71 | return base::subtle::Acquire_Store(
72 | reinterpret_cast(ptr), value);
73 | }
74 |
75 | inline void Release_Store(volatile AtomicWord* ptr, AtomicWord value) {
76 | return base::subtle::Release_Store(
77 | reinterpret_cast(ptr), value);
78 | }
79 |
80 | inline AtomicWord NoBarrier_Load(volatile const AtomicWord *ptr) {
81 | return NoBarrier_Load(
82 | reinterpret_cast(ptr));
83 | }
84 |
85 | inline AtomicWord Acquire_Load(volatile const AtomicWord* ptr) {
86 | return base::subtle::Acquire_Load(
87 | reinterpret_cast(ptr));
88 | }
89 |
90 | inline AtomicWord Release_Load(volatile const AtomicWord* ptr) {
91 | return base::subtle::Release_Load(
92 | reinterpret_cast(ptr));
93 | }
94 |
95 | } // namespace base::subtle
96 | } // namespace base
97 |
98 | #endif // !defined(ARCH_CPU_64_BITS)
99 |
100 | #endif // MINI_CHROMIUM_BASE_ATOMICOPS_INTERNALS_ATOMICWORD_COMPAT_H_
101 |
--------------------------------------------------------------------------------
/base/auto_reset.h:
--------------------------------------------------------------------------------
1 | // Copyright 2009 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_AUTO_RESET_H_
6 | #define MINI_CHROMIUM_BASE_AUTO_RESET_H_
7 |
8 | namespace base {
9 |
10 | template
11 | class AutoReset {
12 | public:
13 | AutoReset(T* scoped_variable, T new_value)
14 | : scoped_variable_(scoped_variable),
15 | original_value_(*scoped_variable) {
16 | *scoped_variable_ = new_value;
17 | }
18 |
19 | AutoReset(const AutoReset&) = delete;
20 | AutoReset& operator=(const AutoReset&) = delete;
21 |
22 | ~AutoReset() { *scoped_variable_ = original_value_; }
23 |
24 | private:
25 | T* scoped_variable_;
26 | T original_value_;
27 | };
28 |
29 | } // namespace base
30 |
31 | #endif // MINI_CHROMIUM_BASE_AUTO_RESET_H_
32 |
--------------------------------------------------------------------------------
/base/bit_cast.h:
--------------------------------------------------------------------------------
1 | // Copyright 2016 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_BIT_CAST_H_
6 | #define MINI_CHROMIUM_BASE_BIT_CAST_H_
7 |
8 | #include
9 |
10 | namespace base {
11 |
12 | // This is an equivalent to C++20's std::bit_cast<>(), but with additional
13 | // warnings. It morally does what `*reinterpret_cast(&source)` does, but
14 | // the cast/deref pair is undefined behavior, while bit_cast<>() isn't.
15 | //
16 | // This is not a magic "get out of UB free" card. This must only be used on
17 | // values, not on references or pointers. For pointers, use
18 | // reinterpret_cast<>(), and then look at https://eel.is/c++draft/basic.lval#11
19 | // as that's probably UB also.
20 |
21 | template
22 | constexpr Dest bit_cast(const Source& source) {
23 | static_assert(!std::is_pointer_v,
24 | "bit_cast must not be used on pointer types");
25 | static_assert(!std::is_pointer_v,
26 | "bit_cast must not be used on pointer types");
27 | static_assert(!std::is_reference_v,
28 | "bit_cast must not be used on reference types");
29 | static_assert(!std::is_reference_v,
30 | "bit_cast must not be used on reference types");
31 | static_assert(
32 | sizeof(Dest) == sizeof(Source),
33 | "bit_cast requires source and destination types to be the same size");
34 | static_assert(std::is_trivially_copyable_v,
35 | "bit_cast requires the source type to be trivially copyable");
36 | static_assert(
37 | std::is_trivially_copyable_v,
38 | "bit_cast requires the destination type to be trivially copyable");
39 |
40 | return __builtin_bit_cast(Dest, source);
41 | }
42 |
43 | } // namespace base
44 |
45 | #endif // MINI_CHROMIUM_BASE_BIT_CAST_H_
46 |
--------------------------------------------------------------------------------
/base/check.h:
--------------------------------------------------------------------------------
1 | // Copyright 2020 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_CHECK_H_
6 | #define MINI_CHROMIUM_BASE_CHECK_H_
7 |
8 | #include "base/logging.h"
9 |
10 | #define CHECK(condition) \
11 | LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
12 | << "Check failed: " # condition << ". "
13 |
14 | #define PCHECK(condition) \
15 | LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \
16 | << "Check failed: " # condition << ". "
17 |
18 | #define DCHECK(condition) \
19 | LAZY_STREAM(LOG_STREAM(FATAL), DCHECK_IS_ON() ? !(condition) : false) \
20 | << "Check failed: " # condition << ". "
21 |
22 | #define DPCHECK(condition) \
23 | LAZY_STREAM(PLOG_STREAM(FATAL), DCHECK_IS_ON() ? !(condition) : false) \
24 | << "Check failed: " # condition << ". "
25 |
26 | #endif // MINI_CHROMIUM_BASE_CHECK_H_
27 |
--------------------------------------------------------------------------------
/base/check_op.h:
--------------------------------------------------------------------------------
1 | // Copyright 2020 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_CHECK_OP_H_
6 | #define MINI_CHROMIUM_BASE_CHECK_OP_H_
7 |
8 | #include "base/check.h"
9 | #include "base/logging.h"
10 |
11 | namespace logging {
12 |
13 | template
14 | std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
15 | std::ostringstream ss;
16 | ss << names << " (" << v1 << " vs. " << v2 << ")";
17 | std::string* msg = new std::string(ss.str());
18 | return msg;
19 | }
20 |
21 | #define DEFINE_CHECK_OP_IMPL(name, op) \
22 | template \
23 | inline std::string* Check ## name ## Impl(const t1& v1, const t2& v2, \
24 | const char* names) { \
25 | if (v1 op v2) { \
26 | return NULL; \
27 | } else { \
28 | return MakeCheckOpString(v1, v2, names); \
29 | } \
30 | } \
31 | inline std::string* Check ## name ## Impl(int v1, int v2, \
32 | const char* names) { \
33 | if (v1 op v2) { \
34 | return NULL; \
35 | } else { \
36 | return MakeCheckOpString(v1, v2, names); \
37 | } \
38 | }
39 |
40 | DEFINE_CHECK_OP_IMPL(EQ, ==)
41 | DEFINE_CHECK_OP_IMPL(NE, !=)
42 | DEFINE_CHECK_OP_IMPL(LE, <=)
43 | DEFINE_CHECK_OP_IMPL(LT, <)
44 | DEFINE_CHECK_OP_IMPL(GE, >=)
45 | DEFINE_CHECK_OP_IMPL(GT, >)
46 |
47 | #undef DEFINE_CHECK_OP_IMPL
48 |
49 | } // namespace logging
50 |
51 | #define CHECK_OP(name, op, val1, val2) \
52 | if (std::string* _result = \
53 | logging::Check ## name ## Impl((val1), (val2), \
54 | # val1 " " # op " " # val2)) \
55 | logging::LogMessage(FUNCTION_SIGNATURE, __FILE__, __LINE__, \
56 | _result).stream()
57 |
58 | #define CHECK_EQ(val1, val2) CHECK_OP(EQ, ==, val1, val2)
59 | #define CHECK_NE(val1, val2) CHECK_OP(NE, !=, val1, val2)
60 | #define CHECK_LE(val1, val2) CHECK_OP(LE, <=, val1, val2)
61 | #define CHECK_LT(val1, val2) CHECK_OP(LT, <, val1, val2)
62 | #define CHECK_GE(val1, val2) CHECK_OP(GE, >=, val1, val2)
63 | #define CHECK_GT(val1, val2) CHECK_OP(GT, >, val1, val2)
64 |
65 |
66 | #define DCHECK_OP(name, op, val1, val2) \
67 | if (DCHECK_IS_ON()) \
68 | if (std::string* _result = \
69 | logging::Check ## name ## Impl((val1), (val2), \
70 | # val1 " " # op " " # val2)) \
71 | logging::LogMessage(FUNCTION_SIGNATURE, __FILE__, __LINE__, \
72 | _result).stream()
73 |
74 | #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
75 | #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
76 | #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
77 | #define DCHECK_LT(val1, val2) DCHECK_OP(LT, <, val1, val2)
78 | #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
79 | #define DCHECK_GT(val1, val2) DCHECK_OP(GT, >, val1, val2)
80 |
81 | #endif // MINI_CHROMIUM_BASE_CHECK_OP_H_
82 |
--------------------------------------------------------------------------------
/base/containers/dynamic_extent.h:
--------------------------------------------------------------------------------
1 | // Copyright 2024 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_CONTAINERS_DYNAMIC_EXTENT_H_
6 | #define MINI_CHROMIUM_BASE_CONTAINERS_DYNAMIC_EXTENT_H_
7 |
8 | #include
9 | #include
10 |
11 | namespace base {
12 |
13 | // [views.constants]
14 | inline constexpr size_t dynamic_extent = std::numeric_limits::max();
15 |
16 | } // namespace base
17 |
18 | #endif // MINI_CHROMIUM_BASE_CONTAINERS_DYNAMIC_EXTENT_H_
19 |
--------------------------------------------------------------------------------
/base/containers/util.h:
--------------------------------------------------------------------------------
1 | // Copyright 2018 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_CONTAINERS_UTIL_H_
6 | #define MINI_CHROMIUM_BASE_CONTAINERS_UTIL_H_
7 |
8 | #include
9 |
10 | namespace base {
11 |
12 | // TODO(crbug.com/817982): What we really need is for checked_math.h to be
13 | // able to do checked arithmetic on pointers.
14 | template
15 | inline uintptr_t get_uintptr(const T* t) {
16 | return reinterpret_cast(t);
17 | }
18 |
19 | } // namespace base
20 |
21 | #endif // MINI_CHROMIUM_BASE_CONTAINERS_UTIL_H_
22 |
--------------------------------------------------------------------------------
/base/cxx17_backports.h:
--------------------------------------------------------------------------------
1 | // Copyright 2006-2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_CXX17_BACKPORTS_H_
6 | #define MINI_CHROMIUM_BASE_CXX17_BACKPORTS_H_
7 |
8 | #include
9 |
10 | namespace base {
11 |
12 | template
13 | constexpr size_t size(const T (&array)[N]) noexcept {
14 | return N;
15 | }
16 |
17 | } // namespace base
18 |
19 | #endif // MINI_CHROMIUM_BASE_CXX17_BACKPORTS_H_
20 |
--------------------------------------------------------------------------------
/base/debug/alias.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2011 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/debug/alias.h"
6 |
7 | #include "build/build_config.h"
8 |
9 | namespace base {
10 | namespace debug {
11 |
12 | #if defined(COMPILER_MSVC)
13 | #pragma optimize("", off)
14 | #endif
15 |
16 | void Alias(const void* var) {
17 | }
18 |
19 | #if defined(COMPILER_MSVC)
20 | #pragma optimize("", on)
21 | #endif
22 |
23 | } // namespace debug
24 | } // namespace base
25 |
--------------------------------------------------------------------------------
/base/debug/alias.h:
--------------------------------------------------------------------------------
1 | // Copyright 2011 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_DEBUG_ALIAS_H_
6 | #define MINI_CHROMIUM_BASE_DEBUG_ALIAS_H_
7 |
8 | namespace base {
9 | namespace debug {
10 |
11 | // Make the optimizer think that var is aliased. This is to prevent it from
12 | // optimizing out variables that that would not otherwise be live at the point
13 | // of a potential crash.
14 | void Alias(const void* var);
15 |
16 | } // namespace debug
17 | } // namespace base
18 |
19 | #endif // MINI_CHROMIUM_BASE_DEBUG_ALIAS_H_
20 |
--------------------------------------------------------------------------------
/base/files/file_util.h:
--------------------------------------------------------------------------------
1 | // Copyright 2006-2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_FILES_FILE_UTIL_H_
6 | #define MINI_CHROMIUM_BASE_FILES_FILE_UTIL_H_
7 |
8 | #include "build/build_config.h"
9 |
10 | #if BUILDFLAG(IS_POSIX)
11 |
12 | #include
13 |
14 | namespace base {
15 |
16 | bool ReadFromFD(int fd, char* buffer, size_t bytes);
17 |
18 | } // namespace base
19 |
20 | #endif // BUILDFLAG(IS_POSIX)
21 |
22 | #endif // MINI_CHROMIUM_BASE_FILES_FILE_UTIL_H_
23 |
--------------------------------------------------------------------------------
/base/files/file_util_posix.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2006-2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/files/file_util.h"
6 |
7 | #include
8 |
9 | #include "base/posix/eintr_wrapper.h"
10 |
11 | namespace base {
12 |
13 | bool ReadFromFD(int fd, char* buffer, size_t bytes) {
14 | size_t total_read = 0;
15 | while (total_read < bytes) {
16 | ssize_t bytes_read =
17 | HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read));
18 | if (bytes_read <= 0) {
19 | break;
20 | }
21 | total_read += bytes_read;
22 | }
23 | return total_read == bytes;
24 | }
25 |
26 | } // namespace base
27 |
--------------------------------------------------------------------------------
/base/files/scoped_file.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/files/scoped_file.h"
6 |
7 | #include
8 |
9 | #include "base/logging.h"
10 |
11 | #if BUILDFLAG(IS_POSIX)
12 | #include
13 |
14 | #include "base/check.h"
15 | #include "base/posix/eintr_wrapper.h"
16 | #endif
17 |
18 | namespace base {
19 | namespace internal {
20 |
21 | #if BUILDFLAG(IS_POSIX)
22 | void ScopedFDCloseTraits::Free(int fd) {
23 | PCHECK(IGNORE_EINTR(close(fd)) == 0);
24 | }
25 | #endif // BUILDFLAG(IS_POSIX)
26 |
27 | void ScopedFILECloser::operator()(FILE* file) const {
28 | if (file) {
29 | if (fclose(file) < 0) {
30 | PLOG(ERROR) << "fclose";
31 | }
32 | }
33 | }
34 |
35 | } // namespace internal
36 | } // namespace base
37 |
--------------------------------------------------------------------------------
/base/files/scoped_file.h:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_FILES_SCOPED_FILE_H_
6 | #define MINI_CHROMIUM_BASE_FILES_SCOPED_FILE_H_
7 |
8 | #include
9 |
10 | #include
11 |
12 | #include "base/scoped_generic.h"
13 | #include "build/build_config.h"
14 |
15 | namespace base {
16 |
17 | namespace internal {
18 |
19 | #if BUILDFLAG(IS_POSIX)
20 | struct ScopedFDCloseTraits {
21 | static int InvalidValue() {
22 | return -1;
23 | }
24 | static void Free(int fd);
25 | };
26 | #endif // BUILDFLAG(IS_POSIX)
27 |
28 | struct ScopedFILECloser {
29 | void operator()(FILE* file) const;
30 | };
31 |
32 | } // namespace internal
33 |
34 | #if BUILDFLAG(IS_POSIX)
35 | typedef ScopedGeneric ScopedFD;
36 | #endif // BUILDFLAG(IS_POSIX)
37 | typedef std::unique_ptr ScopedFILE;
38 |
39 | } // namespace base
40 |
41 | #endif // MINI_CHROMIUM_BASE_FILES_SCOPED_FILE_H_
42 |
--------------------------------------------------------------------------------
/base/format_macros.h:
--------------------------------------------------------------------------------
1 | // Copyright 2009 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef BASE_FORMAT_MACROS_H_
6 | #define BASE_FORMAT_MACROS_H_
7 |
8 | // This file defines the format macros for some integer types.
9 |
10 | // To print a 64-bit value in a portable way:
11 | // int64_t value;
12 | // printf("xyz:%" PRId64, value);
13 | // The "d" in the macro corresponds to %d; you can also use PRIu64 etc.
14 | //
15 | // For wide strings, prepend "Wide" to the macro:
16 | // int64_t value;
17 | // StringPrintf(L"xyz: %" WidePRId64, value);
18 | //
19 | // To print a size_t value in a portable way:
20 | // size_t size;
21 | // printf("xyz: %" PRIuS, size);
22 | // The "u" in the macro corresponds to %u, and S is for "size".
23 |
24 | #include "build/build_config.h"
25 |
26 | #if BUILDFLAG(IS_POSIX) && \
27 | (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
28 | #error "inttypes.h has already been included before this header file, but "
29 | #error "without __STDC_FORMAT_MACROS defined."
30 | #endif
31 |
32 | #if BUILDFLAG(IS_POSIX) && !defined(__STDC_FORMAT_MACROS)
33 | #define __STDC_FORMAT_MACROS
34 | #endif
35 |
36 | #include
37 |
38 | #if !defined(PRIuS)
39 | #define PRIuS "zu"
40 | #endif
41 |
42 | // The size of NSInteger and NSUInteger varies between 32-bit and 64-bit
43 | // architectures and Apple does not provides standard format macros and
44 | // recommends casting. This has many drawbacks, so instead define macros
45 | // for formatting those types.
46 | #if BUILDFLAG(IS_APPLE)
47 | #if defined(ARCH_CPU_64_BITS)
48 | #if !defined(PRIdNS)
49 | #define PRIdNS "ld"
50 | #endif
51 | #if !defined(PRIuNS)
52 | #define PRIuNS "lu"
53 | #endif
54 | #if !defined(PRIxNS)
55 | #define PRIxNS "lx"
56 | #endif
57 | #else // defined(ARCH_CPU_64_BITS)
58 | #if !defined(PRIdNS)
59 | #define PRIdNS "d"
60 | #endif
61 | #if !defined(PRIuNS)
62 | #define PRIuNS "u"
63 | #endif
64 | #if !defined(PRIxNS)
65 | #define PRIxNS "x"
66 | #endif
67 | #endif
68 | #endif // BUILDFLAG(IS_APPLE)
69 |
70 | #endif // BASE_FORMAT_MACROS_H_
71 |
--------------------------------------------------------------------------------
/base/fuchsia/fuchsia_logging.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2017 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/fuchsia/fuchsia_logging.h"
6 |
7 | #include
8 |
9 | #include
10 |
11 | #include "base/immediate_crash.h"
12 |
13 | namespace logging {
14 |
15 | ZxLogMessage::ZxLogMessage(const char* function,
16 | const char* file_path,
17 | int line,
18 | LogSeverity severity,
19 | zx_status_t zx_err)
20 | : LogMessage(function, file_path, line, severity), zx_err_(zx_err) {}
21 |
22 | ZxLogMessage::~ZxLogMessage() {
23 | AppendError();
24 | }
25 |
26 | void ZxLogMessage::AppendError() {
27 | // zx_status_t error values are negative, so log the numeric version as
28 | // decimal rather than hex. This is also useful to match zircon/errors.h for
29 | // grepping.
30 | stream() << ": " << zx_status_get_string(zx_err_) << " (" << zx_err_ << ")";
31 | }
32 |
33 | ZxLogMessageFatal::~ZxLogMessageFatal() {
34 | AppendError();
35 | Flush();
36 | base::ImmediateCrash();
37 | }
38 |
39 | } // namespace logging
40 |
--------------------------------------------------------------------------------
/base/fuchsia/fuchsia_logging.h:
--------------------------------------------------------------------------------
1 | // Copyright 2017 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_FUCHSIA_FUCHSIA_LOGGING_H_
6 | #define MINI_CHROMIUM_BASE_FUCHSIA_FUCHSIA_LOGGING_H_
7 |
8 | #include
9 |
10 | #include "base/logging.h"
11 |
12 | // Use the ZX_LOG family of macros along with a zx_status_t containing a Zircon
13 | // error. The error value will be decoded so that logged messages explain the
14 | // error.
15 |
16 | namespace logging {
17 |
18 | class ZxLogMessage : public logging::LogMessage {
19 | public:
20 | ZxLogMessage(const char* function,
21 | const char* file_path,
22 | int line,
23 | LogSeverity severity,
24 | zx_status_t zx_err);
25 |
26 | ZxLogMessage(const ZxLogMessage&) = delete;
27 | ZxLogMessage& operator=(const ZxLogMessage&) = delete;
28 |
29 | ~ZxLogMessage();
30 |
31 | protected:
32 | void AppendError();
33 |
34 | private:
35 | zx_status_t zx_err_;
36 | };
37 |
38 | class ZxLogMessageFatal final : public ZxLogMessage {
39 | public:
40 | using ZxLogMessage::ZxLogMessage;
41 | [[noreturn]] ~ZxLogMessageFatal() override;
42 | };
43 |
44 | } // namespace logging
45 |
46 | #define ZX_LOG_STREAM(severity, zx_err) \
47 | COMPACT_GOOGLE_LOG_EX_##severity(ZxLogMessage, zx_err).stream()
48 |
49 | #define ZX_LOG(severity, zx_err) \
50 | LAZY_STREAM(ZX_LOG_STREAM(severity, zx_err), LOG_IS_ON(severity))
51 | #define ZX_LOG_IF(severity, condition, zx_err) \
52 | LAZY_STREAM(ZX_LOG_STREAM(severity, zx_err), \
53 | LOG_IS_ON(severity) && (condition))
54 |
55 | #define ZX_CHECK(condition, zx_err) \
56 | LAZY_STREAM(ZX_LOG_STREAM(FATAL, zx_err), !(condition)) \
57 | << "Check failed: " #condition << ". "
58 |
59 | #define ZX_DLOG(severity, zx_err) \
60 | LAZY_STREAM(ZX_LOG_STREAM(severity, zx_err), DLOG_IS_ON(severity))
61 | #define ZX_DLOG_IF(severity, condition, zx_err) \
62 | LAZY_STREAM(ZX_LOG_STREAM(severity, zx_err), \
63 | DLOG_IS_ON(severity) && (condition))
64 |
65 | #define ZX_DCHECK(condition, zx_err) \
66 | LAZY_STREAM(ZX_LOG_STREAM(FATAL, zx_err), DCHECK_IS_ON() && !(condition)) \
67 | << "Check failed: " #condition << ". "
68 |
69 | #endif // MINI_CHROMIUM_BASE_FUCHSIA_FUCHSIA_LOGGING_H_
70 |
--------------------------------------------------------------------------------
/base/immediate_crash.h:
--------------------------------------------------------------------------------
1 | // Copyright 2006-2008 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_IMMEDIATE_CRASH_H_
6 | #define MINI_CHROMIUM_BASE_IMMEDIATE_CRASH_H_
7 |
8 | #include "build/build_config.h"
9 |
10 | #if BUILDFLAG(IS_WIN)
11 | #include
12 | #endif // BUILDFLAG(IS_WIN)
13 |
14 | #if defined(COMPILER_GCC)
15 | #define IMMEDIATE_CRASH_ALWAYS_INLINE inline __attribute__((__always_inline__))
16 | #elif defined(COMPILER_MSVC)
17 | #define IMMEDIATE_CRASH_ALWAYS_INLINE __forceinline
18 | #else
19 | #define IMMEDIATE_CRASH_ALWAYS_INLINE inline
20 | #endif
21 |
22 | namespace base {
23 |
24 | [[noreturn]] IMMEDIATE_CRASH_ALWAYS_INLINE void ImmediateCrash() {
25 | #if defined(COMPILER_MSVC)
26 | __debugbreak();
27 | #if defined(ARCH_CPU_X86_FAMILY)
28 | __ud2();
29 | #elif defined(ARCH_CPU_ARM64)
30 | __hlt(0);
31 | #else
32 | #error Unsupported Windows Arch
33 | #endif
34 | #elif defined(ARCH_CPU_X86_FAMILY)
35 | asm("int3; ud2;");
36 | #elif defined(ARCH_CPU_ARMEL)
37 | asm("bkpt #0; udf #0;");
38 | #elif defined(ARCH_CPU_ARM64)
39 | asm("brk #0; hlt #0;");
40 | #else
41 | __builtin_trap();
42 | #endif
43 | #if defined(__clang__) || defined(COMPILER_GCC)
44 | __builtin_unreachable();
45 | #endif // defined(__clang__) || defined(COMPILER_GCC)
46 | }
47 |
48 | } // namespace base
49 |
50 | #endif // MINI_CHROMIUM_BASE_IMMEDIATE_CRASH_H_
51 |
--------------------------------------------------------------------------------
/base/mac/close_nocancel.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2013 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | // http://crbug.com/269623
6 | // http://openradar.appspot.com/14999594
7 | //
8 | // When the default version of close used on Mac OS X fails with EINTR, the
9 | // file descriptor is not in a deterministic state. It may have been closed,
10 | // or it may not have been. This makes it impossible to gracefully recover
11 | // from the error. If the close is retried after the FD has been closed, the
12 | // subsequent close can report EBADF, or worse, it can close an unrelated FD
13 | // opened by another thread. If the close is not retried after the FD has been
14 | // left open, the FD is leaked. Neither of these are good options.
15 | //
16 | // Mac OS X provides an alternate version of close, close$NOCANCEL. This
17 | // version will never fail with EINTR before the FD is actually closed. With
18 | // this version, it is thus safe to call close without checking for EINTR (as
19 | // the HANDLE_EINTR macro does) and not risk leaking the FD. In fact, mixing
20 | // this verison of close with HANDLE_EINTR is hazardous.
21 | //
22 | // The $NOCANCEL variants of various system calls are activated by compiling
23 | // with __DARWIN_NON_CANCELABLE, which prevents them from being pthread
24 | // cancellation points. Rather than taking such a heavy-handed approach, this
25 | // file implements an alternative: to use the $NOCANCEL variant of close (thus
26 | // preventing it from being a pthread cancellation point) without affecting
27 | // any other system calls.
28 | //
29 | // This file operates by providing a close function with the non-$NOCANCEL
30 | // symbol name expected for the compilation environment as set by
31 | // and (the DARWIN_ALIAS_C macro). That name is set by an asm
32 | // label on the declaration of the close function, so the definition of that
33 | // function receives that name. The function calls the $NOCANCEL variant, which
34 | // is resolved from libsyscall. By linking with this version of close prior to
35 | // the libsyscall version, close's implementation is overridden.
36 |
37 | #include
38 | #include
39 |
40 | // If the non-cancelable variants of all system calls have already been
41 | // chosen, do nothing.
42 | #if !__DARWIN_NON_CANCELABLE
43 |
44 | extern "C" {
45 |
46 | #if !__DARWIN_ONLY_UNIX_CONFORMANCE
47 |
48 | // When there's a choice between UNIX2003 and pre-UNIX2003. There's no
49 | // close$NOCANCEL symbol in this case, so use close$NOCANCEL$UNIX2003 as the
50 | // implementation. It does the same thing that close$NOCANCEL would do.
51 | #define close_implementation close$NOCANCEL$UNIX2003
52 |
53 | #else // __DARWIN_ONLY_UNIX_CONFORMANCE
54 |
55 | // When only UNIX2003 is supported:
56 | #define close_implementation close$NOCANCEL
57 |
58 | #endif
59 |
60 | int close_implementation(int fd);
61 |
62 | int close(int fd) {
63 | return close_implementation(fd);
64 | }
65 |
66 | #undef close_implementation
67 |
68 | } // extern "C"
69 |
70 | #endif // !__DARWIN_NON_CANCELABLE
71 |
--------------------------------------------------------------------------------
/base/mac/scoped_ioobject.h:
--------------------------------------------------------------------------------
1 | // Copyright 2010 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_MAC_SCOPED_IOOBJECT_H_
6 | #define MINI_CHROMIUM_BASE_MAC_SCOPED_IOOBJECT_H_
7 |
8 | #include
9 |
10 | #include "base/apple/scoped_typeref.h"
11 |
12 | namespace base {
13 | namespace mac {
14 |
15 | namespace internal {
16 |
17 | template
18 | struct ScopedIOObjectTraits {
19 | static IOT InvalidValue() { return IO_OBJECT_NULL; }
20 | static IOT Retain(IOT iot) {
21 | IOObjectRetain(iot);
22 | return iot;
23 | }
24 | static void Release(IOT iot) { IOObjectRelease(iot); }
25 | };
26 |
27 | } // namespce internal
28 |
29 | template
30 | using ScopedIOObject =
31 | apple::ScopedTypeRef>;
32 |
33 | } // namespace mac
34 | } // namespace base
35 |
36 | #endif // MINI_CHROMIUM_BASE_MAC_SCOPED_IOOBJECT_H_
37 |
--------------------------------------------------------------------------------
/base/mac/scoped_launch_data.h:
--------------------------------------------------------------------------------
1 | // Copyright 2011 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_MAC_SCOPED_LAUNCH_DATA_H_
6 | #define MINI_CHROMIUM_BASE_MAC_SCOPED_LAUNCH_DATA_H_
7 |
8 | #include
9 |
10 | #include "base/scoped_generic.h"
11 |
12 | namespace base {
13 | namespace mac {
14 |
15 | namespace internal {
16 |
17 | struct ScopedLaunchDataTraits {
18 | static launch_data_t InvalidValue() { return nullptr; }
19 |
20 | static void Free(launch_data_t ldt) {
21 | #pragma clang diagnostic push
22 | #pragma clang diagnostic ignored "-Wdeprecated-declarations"
23 | launch_data_free(ldt);
24 | #pragma clang diagnostic pop
25 | }
26 | };
27 |
28 | } // namespace internal
29 |
30 | using ScopedLaunchData =
31 | ScopedGeneric;
32 |
33 | } // namespace mac
34 | } // namespace base
35 |
36 | #endif // MINI_CHROMIUM_BASE_MAC_SCOPED_LAUNCH_DATA_H_
37 |
--------------------------------------------------------------------------------
/base/memory/free_deleter.h:
--------------------------------------------------------------------------------
1 | // Copyright 2016 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_MEMORY_FREE_DELETER_H_
6 | #define MINI_CHROMIUM_BASE_MEMORY_FREE_DELETER_H_
7 |
8 | #include
9 |
10 | namespace base {
11 |
12 | // Function object which invokes 'free' on its parameter, which must be
13 | // a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
14 | //
15 | // std::unique_ptr foo_ptr(
16 | // static_cast(malloc(sizeof(int))));
17 | struct FreeDeleter {
18 | inline void operator()(void* ptr) const {
19 | free(ptr);
20 | }
21 | };
22 |
23 | } // namespace base
24 |
25 | #endif // MINI_CHROMIUM_BASE_MEMORY_FREE_DELETER_H_
26 |
--------------------------------------------------------------------------------
/base/memory/page_size.h:
--------------------------------------------------------------------------------
1 | // Copyright 2013 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_PROCESS_PROCESS_METRICS_H_
6 | #define MINI_CHROMIUM_BASE_PROCESS_PROCESS_METRICS_H_
7 |
8 | #include
9 |
10 | namespace base {
11 |
12 | size_t GetPageSize();
13 |
14 | } // namespace base
15 |
16 | #endif // MINI_CHROMIUM_BASE_PROCESS_PROCESS_METRICS_H_
17 |
--------------------------------------------------------------------------------
/base/memory/page_size_posix.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2013 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/memory/page_size.h"
6 |
7 | #include
8 |
9 | namespace base {
10 |
11 | size_t GetPageSize() {
12 | return getpagesize();
13 | }
14 |
15 | } // namespace base
16 |
--------------------------------------------------------------------------------
/base/memory/page_size_win.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2013 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/memory/page_size.h"
6 |
7 | namespace base {
8 |
9 | size_t GetPageSize() {
10 | return 4 * 1024;
11 | }
12 |
13 | } // namespace base
14 |
--------------------------------------------------------------------------------
/base/memory/raw_ptr_exclusion.h:
--------------------------------------------------------------------------------
1 | // Copyright 2023 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_MEMORY_RAW_PTR_EXCLUSION_H_
6 | #define MINI_CHROMIUM_BASE_MEMORY_RAW_PTR_EXCLUSION_H_
7 |
8 | // Not provided in mini_chromuim, as it's part of PartitionAlloc.
9 | #ifndef RAW_PTR_EXCLUSION
10 | #define RAW_PTR_EXCLUSION
11 | #endif
12 |
13 | #endif // MINI_CHROMIUM_BASE_MEMORY_RAW_PTR_EXCLUSION_H_
14 |
--------------------------------------------------------------------------------
/base/memory/scoped_policy.h:
--------------------------------------------------------------------------------
1 | // Copyright 2012 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_MEMORY_SCOPED_POLICY_H_
6 | #define MINI_CHROMIUM_BASE_MEMORY_SCOPED_POLICY_H_
7 |
8 | namespace base {
9 | namespace scoped_policy {
10 |
11 | // Defines the ownership policy for a scoped object.
12 | enum OwnershipPolicy {
13 | // The scoped object takes ownership of an object by taking over an existing
14 | // ownership claim.
15 | ASSUME,
16 |
17 | // The scoped object will retain the the object and any initial ownership is
18 | // not changed.
19 | RETAIN
20 | };
21 |
22 | } // namespace scoped_policy
23 | } // namespace base
24 |
25 | #endif // MINI_CHROMIUM_BASE_MEMORY_SCOPED_POLICY_H_
26 |
--------------------------------------------------------------------------------
/base/metrics/histogram_functions.h:
--------------------------------------------------------------------------------
1 | // Copyright 2016 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_METRICS_HISTOGRAM_FUNCTIONS_H_
6 | #define MINI_CHROMIUM_BASE_METRICS_HISTOGRAM_FUNCTIONS_H_
7 |
8 | #include
9 |
10 | // These are no-op stub versions of a subset of the functions from Chromium's
11 | // base/metrics/histogram_functions.h. This allows us to instrument the Crashpad
12 | // code as necessary, while not affecting out-of-Chromium builds.
13 | namespace base {
14 |
15 | void UmaHistogramSparse(const std::string& name, int sample) {}
16 |
17 | } // namespace base
18 |
19 | #endif // MINI_CHROMIUM_BASE_METRICS_HISTOGRAM_FUNCTIONS_H_
20 |
--------------------------------------------------------------------------------
/base/metrics/histogram_macros.h:
--------------------------------------------------------------------------------
1 | // Copyright 2016 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_METRICS_HISTOGRAM_MACROS_H_
6 | #define MINI_CHROMIUM_BASE_METRICS_HISTOGRAM_MACROS_H_
7 |
8 | // These are no-op stub versions of a subset of the macros from Chromium's
9 | // base/metrics/histogram_macros.h. This allows us to instrument the Crashpad
10 | // code as necessary, while not affecting out-of-Chromium builds.
11 |
12 | #define UMA_HISTOGRAM_UNUSED(x) (void)(x)
13 |
14 | #define UMA_HISTOGRAM_TIMES(name, sample) \
15 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
16 | #define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) \
17 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
18 | #define UMA_HISTOGRAM_LONG_TIMES(name, sample) \
19 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
20 | #define UMA_HISTOGRAM_LONG_TIMES_100(name, sample) \
21 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
22 | #define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
23 | UMA_HISTOGRAM_UNUSED(name), \
24 | UMA_HISTOGRAM_UNUSED(sample), \
25 | UMA_HISTOGRAM_UNUSED(min), \
26 | UMA_HISTOGRAM_UNUSED(max), \
27 | UMA_HISTOGRAM_UNUSED(bucket_count)
28 |
29 | #define UMA_HISTOGRAM_COUNTS(name, sample) \
30 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
31 | #define UMA_HISTOGRAM_COUNTS_100(name, sample) \
32 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
33 | #define UMA_HISTOGRAM_COUNTS_1000(name, sample) \
34 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
35 | #define UMA_HISTOGRAM_COUNTS_10000(name, sample) \
36 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
37 | #define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
38 | UMA_HISTOGRAM_UNUSED(name), \
39 | UMA_HISTOGRAM_UNUSED(sample), \
40 | UMA_HISTOGRAM_UNUSED(min), \
41 | UMA_HISTOGRAM_UNUSED(max), \
42 | UMA_HISTOGRAM_UNUSED(bucket_count)
43 |
44 | #define UMA_HISTOGRAM_MEMORY_KB(name, sample) \
45 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
46 | #define UMA_HISTOGRAM_MEMORY_MB(name, sample) \
47 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
48 | #define UMA_HISTOGRAM_MEMORY_LARGE_MB(name, sample) \
49 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
50 |
51 | #define UMA_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
52 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(under_one_hundred)
53 |
54 | #define UMA_HISTOGRAM_BOOLEAN(name, sample) \
55 | UMA_HISTOGRAM_UNUSED(name), UMA_HISTOGRAM_UNUSED(sample)
56 |
57 | #define UMA_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
58 | UMA_HISTOGRAM_UNUSED(name), \
59 | UMA_HISTOGRAM_UNUSED(sample), \
60 | UMA_HISTOGRAM_UNUSED(boundary_value)
61 | #define UMA_STABILITY_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
62 | UMA_HISTOGRAM_UNUSED(name), \
63 | UMA_HISTOGRAM_UNUSED(sample), \
64 | UMA_HISTOGRAM_UNUSED(boundary_value)
65 | #define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \
66 | UMA_HISTOGRAM_UNUSED(name), \
67 | UMA_HISTOGRAM_UNUSED(sample), \
68 | UMA_HISTOGRAM_UNUSED(custom_ranges)
69 |
70 | #endif // MINI_CHROMIUM_BASE_METRICS_HISTOGRAM_MACROS_H_
71 |
--------------------------------------------------------------------------------
/base/metrics/persistent_histogram_allocator.h:
--------------------------------------------------------------------------------
1 | // Copyright 2016 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_METRICS_PERSISTENT_HISTOGRAM_ALLOCATOR_H_
6 | #define MINI_CHROMIUM_BASE_METRICS_PERSISTENT_HISTOGRAM_ALLOCATOR_H_
7 |
8 | #include
9 | #include
10 |
11 | #include "base/files/file_path.h"
12 | #include "base/strings/string_piece.h"
13 |
14 | // This file is a non-functional stub of the Chromium base interface to allow
15 | // Crashpad to set up and tear down histogram storage when built against
16 | // Chromium. When Crashpad is built standalone these stubs are used which
17 | // silently do nothing.
18 | namespace base {
19 |
20 | class GlobalHistogramAllocator {
21 | public:
22 | GlobalHistogramAllocator(const GlobalHistogramAllocator&) = delete;
23 | GlobalHistogramAllocator& operator=(const GlobalHistogramAllocator&) = delete;
24 |
25 | static bool CreateWithActiveFileInDir(const base::FilePath&,
26 | size_t,
27 | uint64_t,
28 | base::StringPiece sp) {
29 | return false;
30 | }
31 |
32 | void CreateTrackingHistograms(base::StringPiece) {}
33 | void DeletePersistentLocation() {}
34 |
35 | static GlobalHistogramAllocator* Get() { return nullptr; }
36 | };
37 |
38 | } // namespace base
39 |
40 | #endif // MINI_CHROMIUM_BASE_METRICS_PERSISTENT_HISTOGRAM_ALLOCATOR_H_
41 |
--------------------------------------------------------------------------------
/base/notreached.h:
--------------------------------------------------------------------------------
1 | // Copyright 2020 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_NOTREACHED_H_
6 | #define MINI_CHROMIUM_BASE_NOTREACHED_H_
7 |
8 | #include "base/check.h"
9 | #include "base/logging.h"
10 |
11 | #define NOTREACHED() LOG(FATAL) << "NOTREACHED hit. "
12 |
13 | #endif // MINI_CHROMIUM_BASE_NOTREACHED_H_
14 |
--------------------------------------------------------------------------------
/base/numerics/basic_ops_impl.h:
--------------------------------------------------------------------------------
1 | // Copyright 2024 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_NUMERICS_BASIC_OPS_IMPL_H_
6 | #define MINI_CHROMIUM_BASE_NUMERICS_BASIC_OPS_IMPL_H_
7 |
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | namespace base::internal {
16 |
17 | // The correct type to perform math operations on given values of type `T`. This
18 | // may be a larger type than `T` to avoid promotion to `int` which involves sign
19 | // conversion!
20 | template
21 | requires(std::is_integral_v)
22 | using MathType = std::conditional_t<
23 | sizeof(T) >= sizeof(int),
24 | T,
25 | std::conditional_t, int, unsigned int>>;
26 |
27 | // Reverses the byte order of the integer.
28 | template
29 | requires(std::is_unsigned_v && std::is_integral_v)
30 | inline constexpr T SwapBytes(T value) {
31 | // MSVC intrinsics are not constexpr, so we provide our own constexpr
32 | // implementation. We provide it unconditionally so we can test it on all
33 | // platforms for correctness.
34 | if (std::is_constant_evaluated()) {
35 | if constexpr (sizeof(T) == 1u) {
36 | return value;
37 | } else if constexpr (sizeof(T) == 2u) {
38 | MathType a = (MathType(value) >> 0) & MathType{0xff};
39 | MathType b = (MathType(value) >> 8) & MathType{0xff};
40 | return static_cast((a << 8) | (b << 0));
41 | } else if constexpr (sizeof(T) == 4u) {
42 | T a = (value >> 0) & T{0xff};
43 | T b = (value >> 8) & T{0xff};
44 | T c = (value >> 16) & T{0xff};
45 | T d = (value >> 24) & T{0xff};
46 | return (a << 24) | (b << 16) | (c << 8) | (d << 0);
47 | } else {
48 | static_assert(sizeof(T) == 8u);
49 | T a = (value >> 0) & T{0xff};
50 | T b = (value >> 8) & T{0xff};
51 | T c = (value >> 16) & T{0xff};
52 | T d = (value >> 24) & T{0xff};
53 | T e = (value >> 32) & T{0xff};
54 | T f = (value >> 40) & T{0xff};
55 | T g = (value >> 48) & T{0xff};
56 | T h = (value >> 56) & T{0xff};
57 | return (a << 56) | (b << 48) | (c << 40) | (d << 32) | //
58 | (e << 24) | (f << 16) | (g << 8) | (h << 0);
59 | }
60 | }
61 |
62 | #if _MSC_VER
63 | if constexpr (sizeof(T) == 1u) {
64 | return value;
65 | } else if constexpr (sizeof(T) == sizeof(unsigned short)) {
66 | using U = unsigned short;
67 | return _byteswap_ushort(U{value});
68 | } else if constexpr (sizeof(T) == sizeof(unsigned long)) {
69 | using U = unsigned long;
70 | return _byteswap_ulong(U{value});
71 | } else {
72 | static_assert(sizeof(T) == 8u);
73 | return _byteswap_uint64(value);
74 | }
75 | #else
76 | if constexpr (sizeof(T) == 1u) {
77 | return value;
78 | } else if constexpr (sizeof(T) == 2u) {
79 | return __builtin_bswap16(uint16_t{value});
80 | } else if constexpr (sizeof(T) == 4u) {
81 | return __builtin_bswap32(value);
82 | } else {
83 | static_assert(sizeof(T) == 8u);
84 | return __builtin_bswap64(value);
85 | }
86 | #endif
87 | }
88 |
89 | // Signed values are byte-swapped as unsigned values.
90 | template
91 | requires(std::is_signed_v && std::is_integral_v)
92 | inline constexpr T SwapBytes(T value) {
93 | return static_cast(SwapBytes(static_cast>(value)));
94 | }
95 |
96 | // Converts from a byte array to an integer.
97 | template
98 | requires(std::is_unsigned_v && std::is_integral_v)
99 | inline constexpr T FromLittleEndian(std::span bytes) {
100 | T val;
101 | if (std::is_constant_evaluated()) {
102 | val = T{0};
103 | for (size_t i = 0u; i < sizeof(T); i += 1u) {
104 | // SAFETY: `i < sizeof(T)` (the number of bytes in T), so `(8 * i)` is
105 | // less than the number of bits in T.
106 | val |= MathType(bytes[i]) << (8u * i);
107 | }
108 | } else {
109 | // SAFETY: `bytes` has sizeof(T) bytes, and `val` is of type `T` so has
110 | // sizeof(T) bytes, and the two can not alias as `val` is a stack variable.
111 | memcpy(&val, bytes.data(), sizeof(T));
112 | }
113 | return val;
114 | }
115 |
116 | // Converts to a byte array from an integer.
117 | template
118 | requires(std::is_unsigned_v && std::is_integral_v)
119 | inline constexpr std::array ToLittleEndian(T val) {
120 | auto bytes = std::array();
121 | if (std::is_constant_evaluated()) {
122 | for (size_t i = 0u; i < sizeof(T); i += 1u) {
123 | const auto last_byte = static_cast(val & 0xff);
124 | // The low bytes go to the front of the array in little endian.
125 | bytes[i] = last_byte;
126 | // If `val` is one byte, this shift would be UB. But it's also not needed
127 | // since the loop will not run again.
128 | if constexpr (sizeof(T) > 1u) {
129 | val >>= 8u;
130 | }
131 | }
132 | } else {
133 | // SAFETY: `bytes` has sizeof(T) bytes, and `val` is of type `T` so has
134 | // sizeof(T) bytes, and the two can not alias as `val` is a stack variable.
135 | memcpy(bytes.data(), &val, sizeof(T));
136 | }
137 | return bytes;
138 | }
139 |
140 | } // namespace base::internal
141 |
142 | #endif // MINI_CHROMIUM_BASE_NUMERICS_BASIC_OPS_IMPL_H_
143 |
--------------------------------------------------------------------------------
/base/numerics/safe_conversions_arm_impl.h:
--------------------------------------------------------------------------------
1 | // Copyright 2017 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_NUMERICS_SAFE_CONVERSIONS_ARM_IMPL_H_
6 | #define MINI_CHROMIUM_BASE_NUMERICS_SAFE_CONVERSIONS_ARM_IMPL_H_
7 |
8 | #include
9 | #include
10 |
11 | #include "base/numerics/safe_conversions_impl.h"
12 |
13 | namespace base {
14 | namespace internal {
15 |
16 | // Fast saturation to a destination type.
17 | template
18 | struct SaturateFastAsmOp {
19 | static constexpr bool is_supported =
20 | std::is_signed::value && std::is_integral::value &&
21 | std::is_integral::value &&
22 | IntegerBitsPlusSign::value <= IntegerBitsPlusSign::value &&
23 | IntegerBitsPlusSign::value <= IntegerBitsPlusSign::value &&
24 | !IsTypeInRangeForNumericType::value;
25 |
26 | __attribute__((always_inline)) static Dst Do(Src value) {
27 | int32_t src = value;
28 | typename std::conditional::value, int32_t,
29 | uint32_t>::type result;
30 | if (std::is_signed::value) {
31 | asm("ssat %[dst], %[shift], %[src]"
32 | : [dst] "=r"(result)
33 | : [src] "r"(src), [shift] "n"(IntegerBitsPlusSign::value <= 32
34 | ? IntegerBitsPlusSign::value
35 | : 32));
36 | } else {
37 | asm("usat %[dst], %[shift], %[src]"
38 | : [dst] "=r"(result)
39 | : [src] "r"(src), [shift] "n"(IntegerBitsPlusSign::value < 32
40 | ? IntegerBitsPlusSign::value
41 | : 31));
42 | }
43 | return static_cast(result);
44 | }
45 | };
46 |
47 | } // namespace internal
48 | } // namespace base
49 |
50 | #endif // MINI_CHROMIUM_BASE_NUMERICS_SAFE_CONVERSIONS_ARM_IMPL_H_
51 |
--------------------------------------------------------------------------------
/base/numerics/safe_math.h:
--------------------------------------------------------------------------------
1 | // Copyright 2014 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_H_
6 | #define MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_H_
7 |
8 | #include "base/numerics/checked_math.h"
9 | #include "base/numerics/clamped_math.h"
10 | #include "base/numerics/safe_conversions.h"
11 |
12 | #endif // MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_H_
13 |
--------------------------------------------------------------------------------
/base/numerics/safe_math_arm_impl.h:
--------------------------------------------------------------------------------
1 | // Copyright 2017 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_ARM_IMPL_H_
6 | #define MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_ARM_IMPL_H_
7 |
8 | #include
9 |
10 | #include
11 | #include
12 |
13 | #include "base/numerics/safe_conversions.h"
14 |
15 | namespace base {
16 | namespace internal {
17 |
18 | template
19 | struct CheckedMulFastAsmOp {
20 | static const bool is_supported =
21 | FastIntegerArithmeticPromotion::is_contained;
22 |
23 | // The following is much more efficient than the Clang and GCC builtins for
24 | // performing overflow-checked multiplication when a twice wider type is
25 | // available. The below compiles down to 2-3 instructions, depending on the
26 | // width of the types in use.
27 | // As an example, an int32_t multiply compiles to:
28 | // smull r0, r1, r0, r1
29 | // cmp r1, r1, asr #31
30 | // And an int16_t multiply compiles to:
31 | // smulbb r1, r1, r0
32 | // asr r2, r1, #16
33 | // cmp r2, r1, asr #15
34 | template
35 | __attribute__((always_inline)) static bool Do(T x, U y, V* result) {
36 | using Promotion = typename FastIntegerArithmeticPromotion::type;
37 | Promotion presult;
38 |
39 | presult = static_cast(x) * static_cast(y);
40 | *result = static_cast(presult);
41 | return IsValueInRangeForNumericType(presult);
42 | }
43 | };
44 |
45 | template
46 | struct ClampedAddFastAsmOp {
47 | static const bool is_supported =
48 | BigEnoughPromotion::is_contained &&
49 | IsTypeInRangeForNumericType<
50 | int32_t,
51 | typename BigEnoughPromotion::type>::value;
52 |
53 | template
54 | __attribute__((always_inline)) static V Do(T x, U y) {
55 | // This will get promoted to an int, so let the compiler do whatever is
56 | // clever and rely on the saturated cast to bounds check.
57 | if (IsIntegerArithmeticSafe::value)
58 | return saturated_cast(x + y);
59 |
60 | int32_t result;
61 | int32_t x_i32 = x;
62 | int32_t y_i32 = y;
63 |
64 | asm("qadd %[result], %[first], %[second]"
65 | : [result] "=r"(result)
66 | : [first] "r"(x_i32), [second] "r"(y_i32));
67 | return saturated_cast(result);
68 | }
69 | };
70 |
71 | template
72 | struct ClampedSubFastAsmOp {
73 | static const bool is_supported =
74 | BigEnoughPromotion::is_contained &&
75 | IsTypeInRangeForNumericType<
76 | int32_t,
77 | typename BigEnoughPromotion::type>::value;
78 |
79 | template
80 | __attribute__((always_inline)) static V Do(T x, U y) {
81 | // This will get promoted to an int, so let the compiler do whatever is
82 | // clever and rely on the saturated cast to bounds check.
83 | if (IsIntegerArithmeticSafe::value)
84 | return saturated_cast(x - y);
85 |
86 | int32_t result;
87 | int32_t x_i32 = x;
88 | int32_t y_i32 = y;
89 |
90 | asm("qsub %[result], %[first], %[second]"
91 | : [result] "=r"(result)
92 | : [first] "r"(x_i32), [second] "r"(y_i32));
93 | return saturated_cast(result);
94 | }
95 | };
96 |
97 | template
98 | struct ClampedMulFastAsmOp {
99 | static const bool is_supported = CheckedMulFastAsmOp::is_supported;
100 |
101 | template
102 | __attribute__((always_inline)) static V Do(T x, U y) {
103 | // Use the CheckedMulFastAsmOp for full-width 32-bit values, because
104 | // it's fewer instructions than promoting and then saturating.
105 | if (!IsIntegerArithmeticSafe::value &&
106 | !IsIntegerArithmeticSafe::value) {
107 | V result;
108 | if (CheckedMulFastAsmOp::Do(x, y, &result))
109 | return result;
110 | return CommonMaxOrMin(IsValueNegative(x) ^ IsValueNegative(y));
111 | }
112 |
113 | assert((FastIntegerArithmeticPromotion::is_contained));
114 | using Promotion = typename FastIntegerArithmeticPromotion::type;
115 | return saturated_cast(static_cast(x) *
116 | static_cast(y));
117 | }
118 | };
119 |
120 | } // namespace internal
121 | } // namespace base
122 |
123 | #endif // MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_ARM_IMPL_H_
124 |
--------------------------------------------------------------------------------
/base/numerics/safe_math_clang_gcc_impl.h:
--------------------------------------------------------------------------------
1 | // Copyright 2017 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #ifndef MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_CLANG_GCC_IMPL_H_
6 | #define MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_CLANG_GCC_IMPL_H_
7 |
8 | #include
9 | #include
10 |
11 | #include "base/numerics/safe_conversions.h"
12 |
13 | #if defined(__ARMEL__) || defined(__arch64__)
14 | #include "base/numerics/safe_math_arm_impl.h"
15 | #define BASE_HAS_ASSEMBLER_SAFE_MATH (1)
16 | #else
17 | #define BASE_HAS_ASSEMBLER_SAFE_MATH (0)
18 | #endif
19 |
20 | namespace base {
21 | namespace internal {
22 |
23 | // These are the non-functioning boilerplate implementations of the optimized
24 | // safe math routines.
25 | #if !BASE_HAS_ASSEMBLER_SAFE_MATH
26 | template
27 | struct CheckedMulFastAsmOp {
28 | static const bool is_supported = false;
29 | template
30 | static constexpr bool Do(T, U, V*) {
31 | // Force a compile failure if instantiated.
32 | return CheckOnFailure::template HandleFailure();
33 | }
34 | };
35 |
36 | template
37 | struct ClampedAddFastAsmOp {
38 | static const bool is_supported = false;
39 | template
40 | static constexpr V Do(T, U) {
41 | // Force a compile failure if instantiated.
42 | return CheckOnFailure::template HandleFailure();
43 | }
44 | };
45 |
46 | template
47 | struct ClampedSubFastAsmOp {
48 | static const bool is_supported = false;
49 | template
50 | static constexpr V Do(T, U) {
51 | // Force a compile failure if instantiated.
52 | return CheckOnFailure::template HandleFailure();
53 | }
54 | };
55 |
56 | template
57 | struct ClampedMulFastAsmOp {
58 | static const bool is_supported = false;
59 | template
60 | static constexpr V Do(T, U) {
61 | // Force a compile failure if instantiated.
62 | return CheckOnFailure::template HandleFailure();
63 | }
64 | };
65 | #endif // BASE_HAS_ASSEMBLER_SAFE_MATH
66 | #undef BASE_HAS_ASSEMBLER_SAFE_MATH
67 |
68 | template
69 | struct CheckedAddFastOp {
70 | static const bool is_supported = true;
71 | template
72 | __attribute__((always_inline)) static constexpr bool Do(T x, U y, V* result) {
73 | return !__builtin_add_overflow(x, y, result);
74 | }
75 | };
76 |
77 | template
78 | struct CheckedSubFastOp {
79 | static const bool is_supported = true;
80 | template
81 | __attribute__((always_inline)) static constexpr bool Do(T x, U y, V* result) {
82 | return !__builtin_sub_overflow(x, y, result);
83 | }
84 | };
85 |
86 | template
87 | struct CheckedMulFastOp {
88 | #if defined(__clang__)
89 | // TODO(jschuh): Get the Clang runtime library issues sorted out so we can
90 | // support full-width, mixed-sign multiply builtins.
91 | // https://crbug.com/613003
92 | // We can support intptr_t, uintptr_t, or a smaller common type.
93 | static const bool is_supported =
94 | (IsTypeInRangeForNumericType::value &&
95 | IsTypeInRangeForNumericType::value) ||
96 | (IsTypeInRangeForNumericType::value &&
97 | IsTypeInRangeForNumericType::value);
98 | #else
99 | static const bool is_supported = true;
100 | #endif
101 | template
102 | __attribute__((always_inline)) static constexpr bool Do(T x, U y, V* result) {
103 | return CheckedMulFastAsmOp::is_supported
104 | ? CheckedMulFastAsmOp::Do(x, y, result)
105 | : !__builtin_mul_overflow(x, y, result);
106 | }
107 | };
108 |
109 | template
110 | struct ClampedAddFastOp {
111 | static const bool is_supported = ClampedAddFastAsmOp::is_supported;
112 | template
113 | __attribute__((always_inline)) static V Do(T x, U y) {
114 | return ClampedAddFastAsmOp::template Do(x, y);
115 | }
116 | };
117 |
118 | template
119 | struct ClampedSubFastOp {
120 | static const bool is_supported = ClampedSubFastAsmOp::is_supported;
121 | template
122 | __attribute__((always_inline)) static V Do(T x, U y) {
123 | return ClampedSubFastAsmOp::template Do(x, y);
124 | }
125 | };
126 |
127 | template
128 | struct ClampedMulFastOp {
129 | static const bool is_supported = ClampedMulFastAsmOp::is_supported;
130 | template
131 | __attribute__((always_inline)) static V Do(T x, U y) {
132 | return ClampedMulFastAsmOp::template Do(x, y);
133 | }
134 | };
135 |
136 | template
137 | struct ClampedNegFastOp {
138 | static const bool is_supported = std::is_signed::value;
139 | __attribute__((always_inline)) static T Do(T value) {
140 | // Use this when there is no assembler path available.
141 | if (!ClampedSubFastAsmOp::is_supported) {
142 | T result;
143 | return !__builtin_sub_overflow(T(0), value, &result)
144 | ? result
145 | : std::numeric_limits::max();
146 | }
147 |
148 | // Fallback to the normal subtraction path.
149 | return ClampedSubFastOp::template Do(T(0), value);
150 | }
151 | };
152 |
153 | } // namespace internal
154 | } // namespace base
155 |
156 | #endif // MINI_CHROMIUM_BASE_NUMERICS_SAFE_MATH_CLANG_GCC_IMPL_H_
157 |
--------------------------------------------------------------------------------
/base/posix/eintr_wrapper.h:
--------------------------------------------------------------------------------
1 | // Copyright 2009 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | // This provides a wrapper around system calls which may be interrupted by a
6 | // signal and return EINTR. See man 7 signal.
7 | //
8 | // On Windows, this wrapper macro does nothing.
9 |
10 | #ifndef MINI_CHROMIUM_BASE_POSIX_EINTR_WRAPPER_H_
11 | #define MINI_CHROMIUM_BASE_POSIX_EINTR_WRAPPER_H_
12 |
13 | #include "build/build_config.h"
14 |
15 | // On Fuchsia, these wrapper macros do nothing because there are no signals.
16 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_FUCHSIA)
17 |
18 | #include
19 |
20 | #define HANDLE_EINTR(x) ({ \
21 | decltype(x) eintr_wrapper_result; \
22 | do { \
23 | eintr_wrapper_result = (x); \
24 | } while (eintr_wrapper_result == -1 && errno == EINTR); \
25 | eintr_wrapper_result; \
26 | })
27 |
28 | #define IGNORE_EINTR(x) ({ \
29 | decltype(x) eintr_wrapper_result; \
30 | do { \
31 | eintr_wrapper_result = (x); \
32 | if (eintr_wrapper_result == -1 && errno == EINTR) { \
33 | eintr_wrapper_result = 0; \
34 | } \
35 | } while (0); \
36 | eintr_wrapper_result; \
37 | })
38 |
39 | #else
40 |
41 | #define HANDLE_EINTR(x) (x)
42 | #define IGNORE_EINTR(x) (x)
43 |
44 | #endif // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_FUCHSIA)
45 |
46 | #endif // MINI_CHROMIUM_BASE_POSIX_EINTR_WRAPPER_H_
47 |
--------------------------------------------------------------------------------
/base/posix/safe_strerror.cc:
--------------------------------------------------------------------------------
1 | // Copyright 2009 The Chromium Authors
2 | // Use of this source code is governed by a BSD-style license that can be
3 | // found in the LICENSE file.
4 |
5 | #include "base/posix/safe_strerror.h"
6 |
7 | #include
8 | #include
9 | #include