1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/memory/aligned_memory.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,114 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// AlignedMemory is a POD type that gives you a portable way to specify static 1.9 +// or local stack data of a given alignment and size. For example, if you need 1.10 +// static storage for a class, but you want manual control over when the object 1.11 +// is constructed and destructed (you don't want static initialization and 1.12 +// destruction), use AlignedMemory: 1.13 +// 1.14 +// static AlignedMemory<sizeof(MyClass), ALIGNOF(MyClass)> my_class; 1.15 +// 1.16 +// // ... at runtime: 1.17 +// new(my_class.void_data()) MyClass(); 1.18 +// 1.19 +// // ... use it: 1.20 +// MyClass* mc = my_class.data_as<MyClass>(); 1.21 +// 1.22 +// // ... later, to destruct my_class: 1.23 +// my_class.data_as<MyClass>()->MyClass::~MyClass(); 1.24 +// 1.25 +// Alternatively, a runtime sized aligned allocation can be created: 1.26 +// 1.27 +// float* my_array = static_cast<float*>(AlignedAlloc(size, alignment)); 1.28 +// 1.29 +// // ... later, to release the memory: 1.30 +// AlignedFree(my_array); 1.31 +// 1.32 +// Or using scoped_ptr_malloc: 1.33 +// 1.34 +// scoped_ptr_malloc<float, ScopedPtrAlignedFree> my_array( 1.35 +// static_cast<float*>(AlignedAlloc(size, alignment))); 1.36 + 1.37 +#ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ 1.38 +#define BASE_MEMORY_ALIGNED_MEMORY_H_ 1.39 + 1.40 +#include "base/base_export.h" 1.41 +#include "base/basictypes.h" 1.42 +#include "base/compiler_specific.h" 1.43 + 1.44 +#if defined(COMPILER_MSVC) 1.45 +#include <malloc.h> 1.46 +#else 1.47 +#include <stdlib.h> 1.48 +#endif 1.49 + 1.50 +namespace base { 1.51 + 1.52 +// AlignedMemory is specialized for all supported alignments. 1.53 +// Make sure we get a compiler error if someone uses an unsupported alignment. 1.54 +template <size_t Size, size_t ByteAlignment> 1.55 +struct AlignedMemory {}; 1.56 + 1.57 +#define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \ 1.58 + template <size_t Size> \ 1.59 + class AlignedMemory<Size, byte_alignment> { \ 1.60 + public: \ 1.61 + ALIGNAS(byte_alignment) uint8 data_[Size]; \ 1.62 + void* void_data() { return static_cast<void*>(data_); } \ 1.63 + const void* void_data() const { \ 1.64 + return static_cast<const void*>(data_); \ 1.65 + } \ 1.66 + template<typename Type> \ 1.67 + Type* data_as() { return static_cast<Type*>(void_data()); } \ 1.68 + template<typename Type> \ 1.69 + const Type* data_as() const { \ 1.70 + return static_cast<const Type*>(void_data()); \ 1.71 + } \ 1.72 + private: \ 1.73 + void* operator new(size_t); \ 1.74 + void operator delete(void*); \ 1.75 + } 1.76 + 1.77 +// Specialization for all alignments is required because MSVC (as of VS 2008) 1.78 +// does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). 1.79 +// Greater than 4096 alignment is not supported by some compilers, so 4096 is 1.80 +// the maximum specified here. 1.81 +BASE_DECL_ALIGNED_MEMORY(1); 1.82 +BASE_DECL_ALIGNED_MEMORY(2); 1.83 +BASE_DECL_ALIGNED_MEMORY(4); 1.84 +BASE_DECL_ALIGNED_MEMORY(8); 1.85 +BASE_DECL_ALIGNED_MEMORY(16); 1.86 +BASE_DECL_ALIGNED_MEMORY(32); 1.87 +BASE_DECL_ALIGNED_MEMORY(64); 1.88 +BASE_DECL_ALIGNED_MEMORY(128); 1.89 +BASE_DECL_ALIGNED_MEMORY(256); 1.90 +BASE_DECL_ALIGNED_MEMORY(512); 1.91 +BASE_DECL_ALIGNED_MEMORY(1024); 1.92 +BASE_DECL_ALIGNED_MEMORY(2048); 1.93 +BASE_DECL_ALIGNED_MEMORY(4096); 1.94 + 1.95 +#undef BASE_DECL_ALIGNED_MEMORY 1.96 + 1.97 +BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment); 1.98 + 1.99 +inline void AlignedFree(void* ptr) { 1.100 +#if defined(COMPILER_MSVC) 1.101 + _aligned_free(ptr); 1.102 +#else 1.103 + free(ptr); 1.104 +#endif 1.105 +} 1.106 + 1.107 +// Helper class for use with scoped_ptr_malloc. 1.108 +class BASE_EXPORT ScopedPtrAlignedFree { 1.109 + public: 1.110 + inline void operator()(void* ptr) const { 1.111 + AlignedFree(ptr); 1.112 + } 1.113 +}; 1.114 + 1.115 +} // namespace base 1.116 + 1.117 +#endif // BASE_MEMORY_ALIGNED_MEMORY_H_