Chromium Code Reviews| OLD | NEW |
|---|---|
| (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> | |
|
scottmg
2015/12/04 19:58:51
_Xbad_alloc is only there in VS2012+ I think, but
| |
| 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 | |
| OLD | NEW |