aligned_memory.h 3.46 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// AlignedMemory is a POD type that gives you a portable way to specify static
// or local stack data of a given alignment and size. For example, if you need
// static storage for a class, but you want manual control over when the object
// is constructed and destructed (you don't want static initialization and
// destruction), use AlignedMemory:
//
//   static AlignedMemory<sizeof(MyClass), ALIGNOF(MyClass)> my_class;
//
//   // ... at runtime:
//   new(my_class.void_data()) MyClass();
//
//   // ... use it:
//   MyClass* mc = my_class.data_as<MyClass>();
//
//   // ... later, to destruct my_class:
//   my_class.data_as<MyClass>()->MyClass::~MyClass();
//
// Alternatively, a runtime sized aligned allocation can be created:
//
//   float* my_array = static_cast<float*>(AlignedAlloc(size, alignment));
//
//   // ... later, to release the memory:
//   AlignedFree(my_array);
//
// Or using scoped_ptr:
//
//   scoped_ptr<float, AlignedFreeDeleter> my_array(
//       static_cast<float*>(AlignedAlloc(size, alignment)));

34 35
#ifndef BUTIL_MEMORY_ALIGNED_MEMORY_H_
#define BUTIL_MEMORY_ALIGNED_MEMORY_H_
gejun's avatar
gejun committed
36

37 38 39
#include "butil/base_export.h"
#include "butil/basictypes.h"
#include "butil/compiler_specific.h"
gejun's avatar
gejun committed
40 41 42 43 44 45 46

#if defined(COMPILER_MSVC)
#include <malloc.h>
#else
#include <stdlib.h>
#endif

47
namespace butil {
gejun's avatar
gejun committed
48 49 50 51 52 53

// AlignedMemory is specialized for all supported alignments.
// Make sure we get a compiler error if someone uses an unsupported alignment.
template <size_t Size, size_t ByteAlignment>
struct AlignedMemory {};

54
#define BUTIL_DECL_ALIGNED_MEMORY(byte_alignment) \
gejun's avatar
gejun committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    template <size_t Size> \
    class AlignedMemory<Size, byte_alignment> { \
     public: \
      ALIGNAS(byte_alignment) uint8_t data_[Size]; \
      void* void_data() { return static_cast<void*>(data_); } \
      const void* void_data() const { \
        return static_cast<const void*>(data_); \
      } \
      template<typename Type> \
      Type* data_as() { return static_cast<Type*>(void_data()); } \
      template<typename Type> \
      const Type* data_as() const { \
        return static_cast<const Type*>(void_data()); \
      } \
     private: \
      void* operator new(size_t); \
      void operator delete(void*); \
    }

// Specialization for all alignments is required because MSVC (as of VS 2008)
// does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param).
// Greater than 4096 alignment is not supported by some compilers, so 4096 is
// the maximum specified here.
78 79 80 81 82 83 84 85 86 87 88 89 90
BUTIL_DECL_ALIGNED_MEMORY(1);
BUTIL_DECL_ALIGNED_MEMORY(2);
BUTIL_DECL_ALIGNED_MEMORY(4);
BUTIL_DECL_ALIGNED_MEMORY(8);
BUTIL_DECL_ALIGNED_MEMORY(16);
BUTIL_DECL_ALIGNED_MEMORY(32);
BUTIL_DECL_ALIGNED_MEMORY(64);
BUTIL_DECL_ALIGNED_MEMORY(128);
BUTIL_DECL_ALIGNED_MEMORY(256);
BUTIL_DECL_ALIGNED_MEMORY(512);
BUTIL_DECL_ALIGNED_MEMORY(1024);
BUTIL_DECL_ALIGNED_MEMORY(2048);
BUTIL_DECL_ALIGNED_MEMORY(4096);
gejun's avatar
gejun committed
91

92
#undef BUTIL_DECL_ALIGNED_MEMORY
gejun's avatar
gejun committed
93

94
BUTIL_EXPORT void* AlignedAlloc(size_t size, size_t alignment);
gejun's avatar
gejun committed
95 96 97 98 99 100 101 102 103 104

inline void AlignedFree(void* ptr) {
#if defined(COMPILER_MSVC)
  _aligned_free(ptr);
#else
  free(ptr);
#endif
}

// Deleter for use with scoped_ptr. E.g., use as
105
//   scoped_ptr<Foo, butil::AlignedFreeDeleter> foo;
gejun's avatar
gejun committed
106 107 108 109 110 111
struct AlignedFreeDeleter {
  inline void operator()(void* ptr) const {
    AlignedFree(ptr);
  }
};

112
}  // namespace butil
gejun's avatar
gejun committed
113

114
#endif  // BUTIL_MEMORY_ALIGNED_MEMORY_H_