Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: util/stdlib/aligned_allocator.cc

Issue 1498133002: Add AlignedVector and use it for vector<MEMORY_BASIC_INFORMATION64> (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address review feedback Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « util/stdlib/aligned_allocator.h ('k') | util/stdlib/aligned_allocator_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: util/stdlib/aligned_allocator.cc
diff --git a/util/stdlib/aligned_allocator.cc b/util/stdlib/aligned_allocator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..363022c814b87e9bd63b9d977019e123a52a51e8
--- /dev/null
+++ b/util/stdlib/aligned_allocator.cc
@@ -0,0 +1,78 @@
+// 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.
+
+#include "util/stdlib/aligned_allocator.h"
+
+#include <algorithm>
+
+#include "build/build_config.h"
+
+#if defined(OS_POSIX)
+#include <stdlib.h>
+#elif defined(OS_WIN)
+#include <malloc.h>
+#include <xutility>
+#endif // OS_POSIX
+
+namespace {
+
+// Throws std::bad_alloc() by calling an internal function provided by the C++
+// library to do so. This works even if C++ exceptions are disabled, causing
+// program termination if uncaught.
+void ThrowBadAlloc() {
+#if defined(OS_POSIX)
+ // This works with both libc++ and libstdc++.
+ std::__throw_bad_alloc();
+#elif defined(OS_WIN)
+ std::_Xbad_alloc();
+#endif // OS_POSIX
+}
+
+} // namespace
+
+namespace crashpad {
+namespace internal {
+
+void* AlignedAllocate(size_t alignment, size_t size) {
+#if defined(OS_POSIX)
+ // posix_memalign() requires that alignment be at least sizeof(void*), so the
+ // power-of-2 check needs to happen before potentially changing the alignment.
+ if (alignment == 0 || alignment & (alignment - 1)) {
+ ThrowBadAlloc();
+ }
+
+ void* pointer;
+ if (posix_memalign(&pointer, std::max(alignment, sizeof(void*)), size) != 0) {
+ ThrowBadAlloc();
+ }
+#elif defined(OS_WIN)
+ void* pointer = _aligned_malloc(size, alignment);
+ if (pointer == nullptr) {
+ ThrowBadAlloc();
+ }
+#endif // OS_POSIX
+
+ return pointer;
+}
+
+void AlignedFree(void* pointer) {
+#if defined(OS_POSIX)
+ free(pointer);
+#elif defined(OS_WIN)
+ _aligned_free(pointer);
+#endif // OS_POSIX
+}
+
+} // namespace internal
+} // namespace crashpad
« no previous file with comments | « util/stdlib/aligned_allocator.h ('k') | util/stdlib/aligned_allocator_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698