Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,116 @@
// Copyright 2015 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_STDLIB_ALIGNED_ALLOCATOR_H_
#define CRASHPAD_UTIL_STDLIB_ALIGNED_ALLOCATOR_H_
#include <stddef.h>
#include <limits>
#include <memory>
#include <new>
#include <utility>
#include <vector>
namespace crashpad {
//! \brief Allocates memory with the specified alignment constraint.
//!
//! This function wraps `posix_memalign()` or `_aligned_malloc()`. Memory
//! allocated by this function must be released by AlignFree().
void* AlignedAllocate(size_t alignment, size_t size);
//! \brief Frees memory allocated by AlignedAllocate().
//!
//! This function wraps `free()` or `_aligned_free()`.
void AlignedFree(void* pointer);
//! \brief A standard allocator that aligns its allocations as requested,
//! suitable for use as an allocator in standard containers.
//!
//! This is similar to `std::allocator<T>`, with the addition of an alignment
//! guarantee. \a Alignment must be a power of 2. If \a Alignment is not
//! specified, the default alignment for type \a T is used.
template <class T, size_t Alignment = alignof(T)>
struct AlignedAllocator {
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = size_t;
using difference_type = ptrdiff_t;
template <class U>
struct rebind {
using other = AlignedAllocator<U, Alignment>;
};
AlignedAllocator() noexcept {}
AlignedAllocator(const AlignedAllocator& other) noexcept {}
template <typename U>
AlignedAllocator(const AlignedAllocator<U, Alignment>& other) noexcept {}
~AlignedAllocator() {}
pointer address(reference x) const noexcept { return &x; }
const_pointer address(const_reference x) const noexcept { return &x; }
pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0) {
return reinterpret_cast<pointer>(
AlignedAllocate(Alignment, sizeof(value_type) * n));
}
void deallocate(pointer p, size_type n) { AlignedFree(p); }
size_type max_size() const noexcept {
return std::numeric_limits<size_type>::max() / sizeof(value_type);
}
template <class U, class... Args>
void construct(U* p, Args&&... args) {
new (reinterpret_cast<void*>(p)) U(std::forward<Args>(args)...);
}
template <class U>
void destroy(U* p) {
p->~U();
}
};
template <class T1, class T2, size_t Alignment>
bool operator==(const AlignedAllocator<T1, Alignment>& lhs,
const AlignedAllocator<T2, Alignment>& rhs) noexcept {
return true;
}
template <class T1, class T2, size_t Alignment>
bool operator!=(const AlignedAllocator<T1, Alignment>& lhs,
const AlignedAllocator<T2, Alignment>& rhs) noexcept {
return false;
}
//! \brief A `std::vector` using AlignedAllocator.
//!
//! This is similar to `std::vector<T>`, with the addition of an alignment
//! guarantee. \a Alignment must be a power of 2. If \a Alignment is not
//! specified, the default alignment for type \a T is used.
template <typename T, size_t Alignment = alignof(T)>
using AlignedVector = std::vector<T, AlignedAllocator<T, Alignment>>;
} // namespace crashpad
#endif // CRASHPAD_UTIL_STDLIB_ALIGNED_ALLOCATOR_H_
@@ -0,0 +1,56 @@
// Copyright 2015 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_STDLIB_MAP_INSERT_H_
#define CRASHPAD_UTIL_STDLIB_MAP_INSERT_H_
#include <map>
#include <utility>
namespace crashpad {
//! \brief Inserts a mapping from \a key to \a value into \a map, or replaces
//! an existing mapping so that \a key maps to \a value.
//!
//! This behaves similarly to `std::map<>::%insert_or_assign()` proposed for
//! C++17, except that the \a old_value parameter is added.
//!
//! \param[in,out] map The map to operate on.
//! \param[in] key The key that should be mapped to \a value.
//! \param[in] value The value that \a key should map to.
//! \param[out] old_value If \a key was previously present in \a map, this will
//! be set to its previous value. This parameter is optional and may be
//! `nullptr` if this information is not required.
//!
//! \return `false` if \a key was previously present in \a map. If \a old_value
//! is not `nullptr`, it will be set to the previous value. `true` if \a
//! key was not present in the map and was inserted.
template <typename T>
bool MapInsertOrReplace(T* map,
const typename T::key_type& key,
const typename T::mapped_type& value,
typename T::mapped_type* old_value) {
const auto result = map->insert(std::make_pair(key, value));
if (!result.second) {
if (old_value) {
*old_value = result.first->second;
}
result.first->second = value;
}
return result.second;
}
} // namespace crashpad
#endif // CRASHPAD_UTIL_STDLIB_MAP_INSERT_H_
+39
View File
@@ -0,0 +1,39 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_STDLIB_OBJC_H_
#define CRASHPAD_UTIL_STDLIB_OBJC_H_
#include <AvailabilityMacros.h>
#include <objc/objc.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_8
// In order for the @NO and @YES literals to work, NO and YES must be defined as
// __objc_no and __objc_yes. See
// https://clang.llvm.org/docs/ObjectiveCLiterals.html.
//
// NO and YES are defined properly for this purpose in the 10.8 SDK, but not in
// earlier SDKs. Because this code is never expected to be compiled with a
// compiler that does not understand the modern forms of these boolean
// constants, but it may be built with an older SDK, replace the outdated SDK
// definitions unconditionally.
#undef NO
#undef YES
#define NO __objc_no
#define YES __objc_yes
#endif
#endif // CRASHPAD_UTIL_STDLIB_OBJC_H_
@@ -0,0 +1,65 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_STDLIB_STRING_NUMBER_CONVERSION_H_
#define CRASHPAD_UTIL_STDLIB_STRING_NUMBER_CONVERSION_H_
#include "base/strings/string_piece.h"
namespace crashpad {
// Convert between strings and numbers.
//
// These functions will only set *number if a perfect conversion can be
// performed. A perfect conversion contains no leading or trailing characters
// (including whitespace) other than the number to convert, and does not
// overflow the targeted data type. If a perfect conversion is possible, *number
// is set and these functions return true. Otherwise, they return false.
//
// The interface in base/strings/string_number_conversions.h doesnt allow
// arbitrary bases based on whether the string begins with prefixes such as "0x"
// as strtol does with base = 0. The functions here are implemented on the
// strtol family with base = 0, and thus do accept such input.
//! \{
//! \brief Convert a string to a number.
//!
//! A conversion will only be performed if it can be done perfectly: if \a
//! string contains no leading or trailing characters (including whitespace)
//! other than the number to convert, and does not overflow the targeted data
//! type.
//!
//! \param[in] string The string to convert to a number. As in `strtol()` with a
//! `base` parameter of `0`, the string is treated as decimal unless it
//! begins with a `"0x"` or `"0X"` prefix, in which case it is treated as
//! hexadecimal, or a `"0"` prefix, in which case it is treated as octal.
//! \param[out] number The converted number. This will only be set if a perfect
//! conversion can be performed.
//!
//! \return `true` if a perfect conversion could be performed, with \a number
//! set appropriately. `false` if a perfect conversion was not possible.
//!
//! \note The interface in `base/strings/string_number_conversions.h` doesnt
//! allow arbitrary bases based on whether the string begins with a prefix
//! indicating its base. The functions here are provided for situations
//! where such prefix recognition is desirable.
bool StringToNumber(const base::StringPiece& string, int* number);
bool StringToNumber(const base::StringPiece& string, unsigned int* number);
bool StringToNumber(const base::StringPiece& string, int64_t* number);
bool StringToNumber(const base::StringPiece& string, uint64_t* number);
//! \}
} // namespace crashpad
#endif // CRASHPAD_UTIL_STDLIB_STRING_NUMBER_CONVERSION_H_
@@ -0,0 +1,54 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_STDLIB_STRLCPY_H_
#define CRASHPAD_UTIL_STDLIB_STRLCPY_H_
#include <sys/types.h>
#include "base/strings/string16.h"
namespace crashpad {
//! \brief Copy a `NUL`-terminated char16-based string to a fixed-size buffer.
//!
//! This function behaves identically to `strlcpy()`, but it operates on char16
//! data instead of `char` data. It copies the `NUL`-terminated string in the
//! buffer beginning at \a source to the buffer of size \a length at \a
//! destination, ensuring that the destination buffer is `NUL`-terminated. No
//! data will be written outside of the \a destination buffer, but if \a length
//! is smaller than the length of the string at \a source, the string will be
//! truncated.
//!
//! \param[out] destination A pointer to a buffer of at least size \a length
//! char16 units (not bytes). The string will be copied to this buffer,
//! possibly with truncation, and `NUL`-terminated. Nothing will be written
//! following the `NUL` terminator.
//! \param[in] source A pointer to a `NUL`-terminated string of char16 data. The
//! `NUL` terminator must be a `NUL` value in a char16 unit, not just a
//! single `NUL` byte.
//! \param[in] length The length of the \a destination buffer in char16 units,
//! not bytes. A maximum of \a `length - 1` char16 units from \a source will
//! be copied to \a destination.
//!
//! \return The length of the \a source string in char16 units, not including
//! its `NUL` terminator. When truncation occurs, the return value will be
//! equal to or greater than than the \a length parameter.
size_t c16lcpy(base::char16* destination,
const base::char16* source,
size_t length);
} // namespace crashpad
#endif // CRASHPAD_UTIL_STDLIB_STRLCPY_H_
@@ -0,0 +1,50 @@
// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_STDLIB_STRNLEN_H_
#define CRASHPAD_UTIL_STDLIB_STRNLEN_H_
#include <string.h>
#include <sys/types.h>
#include "build/build_config.h"
#if defined(OS_MACOSX)
#include <AvailabilityMacros.h>
#endif
namespace crashpad {
//! \brief Returns the length of a string, not to exceed a maximum.
//!
//! \param[in] string The string whose length is to be calculated.
//! \param[in] max_length The maximum length to return.
//!
//! \return The length of \a string, determined as the index of the first `NUL`
//! byte found, not exceeding \a max_length.
//!
//! \note This function is provided because it was introduced in POSIX.1-2008,
//! and not all systems standard libraries provide an implementation.
size_t strnlen(const char* string, size_t max_length);
#if !defined(OS_MACOSX) || \
MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
inline size_t strnlen(const char* string, size_t max_length) {
return ::strnlen(string, max_length);
}
#endif
} // namespace crashpad
#endif // CRASHPAD_UTIL_STDLIB_STRNLEN_H_
@@ -0,0 +1,63 @@
// Copyright 2017 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CRASHPAD_UTIL_STDLIB_THREAD_SAFE_VECTOR_H_
#define CRASHPAD_UTIL_STDLIB_THREAD_SAFE_VECTOR_H_
#include <utility>
#include <vector>
#include "base/macros.h"
#include "base/synchronization/lock.h"
namespace crashpad {
//! \brief A wrapper for a `std::vector<>` that can be accessed safely from
//! multiple threads.
//!
//! This is not a drop-in replacement for `std::vector<>`. Only necessary
//! operations are defined.
template <typename T>
class ThreadSafeVector {
public:
ThreadSafeVector() : vector_(), lock_() {}
~ThreadSafeVector() {}
//! \brief Wraps `std::vector<>::%push_back()`.
void PushBack(const T& element) {
base::AutoLock lock_owner(lock_);
vector_.push_back(element);
}
//! \brief Atomically clears the underlying vector and returns its previous
//! contents.
std::vector<T> Drain() {
std::vector<T> contents;
{
base::AutoLock lock_owner(lock_);
std::swap(vector_, contents);
}
return contents;
}
private:
std::vector<T> vector_;
base::Lock lock_;
DISALLOW_COPY_AND_ASSIGN(ThreadSafeVector);
};
} // namespace crashpad
#endif // CRASHPAD_UTIL_STDLIB_THREAD_SAFE_VECTOR_H_