security/sandbox/chromium/base/memory/aligned_memory.h

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // AlignedMemory is a POD type that gives you a portable way to specify static
michael@0 6 // or local stack data of a given alignment and size. For example, if you need
michael@0 7 // static storage for a class, but you want manual control over when the object
michael@0 8 // is constructed and destructed (you don't want static initialization and
michael@0 9 // destruction), use AlignedMemory:
michael@0 10 //
michael@0 11 // static AlignedMemory<sizeof(MyClass), ALIGNOF(MyClass)> my_class;
michael@0 12 //
michael@0 13 // // ... at runtime:
michael@0 14 // new(my_class.void_data()) MyClass();
michael@0 15 //
michael@0 16 // // ... use it:
michael@0 17 // MyClass* mc = my_class.data_as<MyClass>();
michael@0 18 //
michael@0 19 // // ... later, to destruct my_class:
michael@0 20 // my_class.data_as<MyClass>()->MyClass::~MyClass();
michael@0 21 //
michael@0 22 // Alternatively, a runtime sized aligned allocation can be created:
michael@0 23 //
michael@0 24 // float* my_array = static_cast<float*>(AlignedAlloc(size, alignment));
michael@0 25 //
michael@0 26 // // ... later, to release the memory:
michael@0 27 // AlignedFree(my_array);
michael@0 28 //
michael@0 29 // Or using scoped_ptr_malloc:
michael@0 30 //
michael@0 31 // scoped_ptr_malloc<float, ScopedPtrAlignedFree> my_array(
michael@0 32 // static_cast<float*>(AlignedAlloc(size, alignment)));
michael@0 33
michael@0 34 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_
michael@0 35 #define BASE_MEMORY_ALIGNED_MEMORY_H_
michael@0 36
michael@0 37 #include "base/base_export.h"
michael@0 38 #include "base/basictypes.h"
michael@0 39 #include "base/compiler_specific.h"
michael@0 40
michael@0 41 #if defined(COMPILER_MSVC)
michael@0 42 #include <malloc.h>
michael@0 43 #else
michael@0 44 #include <stdlib.h>
michael@0 45 #endif
michael@0 46
michael@0 47 namespace base {
michael@0 48
michael@0 49 // AlignedMemory is specialized for all supported alignments.
michael@0 50 // Make sure we get a compiler error if someone uses an unsupported alignment.
michael@0 51 template <size_t Size, size_t ByteAlignment>
michael@0 52 struct AlignedMemory {};
michael@0 53
michael@0 54 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \
michael@0 55 template <size_t Size> \
michael@0 56 class AlignedMemory<Size, byte_alignment> { \
michael@0 57 public: \
michael@0 58 ALIGNAS(byte_alignment) uint8 data_[Size]; \
michael@0 59 void* void_data() { return static_cast<void*>(data_); } \
michael@0 60 const void* void_data() const { \
michael@0 61 return static_cast<const void*>(data_); \
michael@0 62 } \
michael@0 63 template<typename Type> \
michael@0 64 Type* data_as() { return static_cast<Type*>(void_data()); } \
michael@0 65 template<typename Type> \
michael@0 66 const Type* data_as() const { \
michael@0 67 return static_cast<const Type*>(void_data()); \
michael@0 68 } \
michael@0 69 private: \
michael@0 70 void* operator new(size_t); \
michael@0 71 void operator delete(void*); \
michael@0 72 }
michael@0 73
michael@0 74 // Specialization for all alignments is required because MSVC (as of VS 2008)
michael@0 75 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param).
michael@0 76 // Greater than 4096 alignment is not supported by some compilers, so 4096 is
michael@0 77 // the maximum specified here.
michael@0 78 BASE_DECL_ALIGNED_MEMORY(1);
michael@0 79 BASE_DECL_ALIGNED_MEMORY(2);
michael@0 80 BASE_DECL_ALIGNED_MEMORY(4);
michael@0 81 BASE_DECL_ALIGNED_MEMORY(8);
michael@0 82 BASE_DECL_ALIGNED_MEMORY(16);
michael@0 83 BASE_DECL_ALIGNED_MEMORY(32);
michael@0 84 BASE_DECL_ALIGNED_MEMORY(64);
michael@0 85 BASE_DECL_ALIGNED_MEMORY(128);
michael@0 86 BASE_DECL_ALIGNED_MEMORY(256);
michael@0 87 BASE_DECL_ALIGNED_MEMORY(512);
michael@0 88 BASE_DECL_ALIGNED_MEMORY(1024);
michael@0 89 BASE_DECL_ALIGNED_MEMORY(2048);
michael@0 90 BASE_DECL_ALIGNED_MEMORY(4096);
michael@0 91
michael@0 92 #undef BASE_DECL_ALIGNED_MEMORY
michael@0 93
michael@0 94 BASE_EXPORT void* AlignedAlloc(size_t size, size_t alignment);
michael@0 95
michael@0 96 inline void AlignedFree(void* ptr) {
michael@0 97 #if defined(COMPILER_MSVC)
michael@0 98 _aligned_free(ptr);
michael@0 99 #else
michael@0 100 free(ptr);
michael@0 101 #endif
michael@0 102 }
michael@0 103
michael@0 104 // Helper class for use with scoped_ptr_malloc.
michael@0 105 class BASE_EXPORT ScopedPtrAlignedFree {
michael@0 106 public:
michael@0 107 inline void operator()(void* ptr) const {
michael@0 108 AlignedFree(ptr);
michael@0 109 }
michael@0 110 };
michael@0 111
michael@0 112 } // namespace base
michael@0 113
michael@0 114 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_

mercurial