nlib
DynamicAlignedStorage.h
[詳解]
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
17 #define INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
18 
19 #include "nn/nlib/Config.h"
20 
21 NLIB_NAMESPACE_BEGIN
22 
24  public:
25  DynamicAlignedStorage() NLIB_NOEXCEPT : storage_(NULL) {}
26  NLIB_MOVE_MEMBER_HELPER_1(DynamicAlignedStorage, storage_)
27  errno_t Init(size_t size, size_t align) NLIB_NOEXCEPT {
28  if (align == 0) return EINVAL;
29  if ((align & (align - 1)) != 0) return EINVAL;
30 #ifdef NN_PLATFORM_CTR
31  void* p = nlib_malloc(size + align);
32 #elif defined(_MSC_VER)
33  void* p = _aligned_malloc(size, align);
34 #else
35  void* p = nlib_memalign(align, size);
36 #endif
37  if (!p) return ENOMEM;
38  storage_ = p;
39 #ifdef NN_PLATFORM_CTR
40  align_ = align;
41 #endif
42  return 0;
43  }
45  void* tmp = rhs.storage_;
46  rhs.storage_ = storage_;
47  storage_ = tmp;
48  }
50 #ifdef _MSC_VER
51  _aligned_free(storage_);
52 #else
53  nlib_free(storage_);
54 #endif
55  }
56  void* Get() NLIB_NOEXCEPT {
57 #ifndef NN_PLATFORM_CTR
58  return storage_;
59 #else
60  uintptr_t pos = (reinterpret_cast<uintptr_t>(storage_) + (align_ - 1)) & ~(align_ - 1);
61  return reinterpret_cast<void*>(pos);
62 #endif
63  }
64 
65  private:
66  void* storage_;
67 #ifdef NN_PLATFORM_CTR
68  size_t align_;
69 #endif
71 };
72 
75  lhs.swap(rhs);
76 }
77 
78 NLIB_NAMESPACE_END
79 
80 #endif // INCLUDE_NN_NLIB_DYNAMICALIGNEDSTORAGE_H_
#define NLIB_ALWAYS_INLINE
コンパイラに関数をインライン展開するように強く示します。
Definition: Platform_unix.h:97
NLIB_CHECK_RESULT void * nlib_memalign(size_t alignment, size_t size)
memalign()を呼び出すweak関数です。nlibはこの関数を経由してmemalign()を呼び出します。 ...
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:163
~DynamicAlignedStorage() noexcept
デストラクタです。
アラインされたメモリを得るためのクラスです。
NLIB_CHECK_RESULT void * nlib_malloc(size_t size)
C標準関数のmalloc()を呼び出すweak関数です。nlibはこの関数を経由してmalloc()を呼び出します。 ...
void swap(DynamicAlignedStorage &rhs) noexcept
オブジェクトをスワップします。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
void * Get() noexcept
割り当てられた領域へのポインタを返します。
開発環境別の設定が書かれるファイルです。
DynamicAlignedStorage() noexcept
デフォルトコンストラクタです。
void nlib_free(void *ptr)
C標準関数のfree()を呼び出すweak関数です。nlibはこの関数を経由してfree()を呼び出します。 ...
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37