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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/stdlib/aligned_allocator.h"
16
17 #include <algorithm>
18
19 #include "build/build_config.h"
20
21 #if defined(OS_POSIX)
22 #include <stdlib.h>
23 #elif defined(OS_WIN)
24 #include <malloc.h>
25 #include <xutility>
26 #endif // OS_POSIX
27
28 namespace {
29
30 // Throws std::bad_alloc() by calling an internal function provided by the C++
31 // library to do so. This works even if C++ exceptions are disabled, causing
32 // program termination if uncaught.
33 void ThrowBadAlloc() {
34 #if defined(OS_POSIX)
35 // This works with both libc++ and libstdc++.
36 std::__throw_bad_alloc();
37 #elif defined(OS_WIN)
38 std::_Xbad_alloc();
39 #endif // OS_POSIX
40 }
41
42 } // namespace
43
44 namespace crashpad {
45 namespace internal {
46
47 void* AlignedAllocate(size_t alignment, size_t size) {
48 #if defined(OS_POSIX)
49 // posix_memalign() requires that alignment be at least sizeof(void*), so the
50 // power-of-2 check needs to happen before potentially changing the alignment.
51 if (alignment == 0 || alignment & (alignment - 1)) {
52 ThrowBadAlloc();
53 }
54
55 void* pointer;
56 if (posix_memalign(&pointer, std::max(alignment, sizeof(void*)), size) != 0) {
57 ThrowBadAlloc();
58 }
59 #elif defined(OS_WIN)
60 void* pointer = _aligned_malloc(size, alignment);
61 if (pointer == nullptr) {
62 ThrowBadAlloc();
63 }
64 #endif // OS_POSIX
65
66 return pointer;
67 }
68
69 void AlignedFree(void* pointer) {
70 #if defined(OS_POSIX)
71 free(pointer);
72 #elif defined(OS_WIN)
73 _aligned_free(pointer);
74 #endif // OS_POSIX
75 }
76
77 } // namespace internal
78 } // namespace crashpad
OLDNEW
« 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