CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
common_PtrUtil.h
1 /*--------------------------------------------------------------------------------*
2  Copyright (C)Nintendo All rights reserved.
3 
4  These coded instructions, statements, and computer programs contain proprietary
5  information of Nintendo and/or its licensed developers and are protected by
6  national and international copyright laws. They may not be disclosed to third
7  parties or copied or duplicated in any form, in whole or in part, without the
8  prior written consent of Nintendo.
9 
10  The content herein is highly confidential and should be handled accordingly.
11  *--------------------------------------------------------------------------------*/
12 
13 
14 #pragma once
15 
16 #include <stdint.h>
17 
18 namespace nn
19 {
20 namespace pia
21 {
22 namespace common
23 {
24 
25 /*!
26  @cond PRIVATE
27  @brief ポインタ演算ユーティリティです。
28  */
29 class PtrUtil
30 {
31 public:
32  /*!
33  @brief アライメントが合うように繰り上げる(2の累乗限定で高速バージョン)
34 
35  @param ptr アライメントさせたいポインタ
36  @param alignment アライメントの指定(2の累乗でなければならない。単位はByte)
37  @return アライメント後のポインタ
38  */
39  static void* roundUpPow2(const void* ptr, uint32_t alignment)
40  {
41  // 処理削減のためにASSERTもしない
42  return reinterpret_cast<void*>(
43  (reinterpret_cast<uintptr_t>(ptr) + (alignment - 1)) & ~(static_cast<uintptr_t>(alignment - 1)));
44  }
45 
46  /*!
47  @brief アライメントが合うように繰り上げる(2の累乗でなくてもよい)
48 
49  @param ptr アライメントさせたいポインタ
50  @param alignment アライメントの指定(2の累乗でなくてもよい。単位はByte)
51  @return アライメント後のポインタ
52  */
53  static void* roundUpN(const void* ptr, uint32_t alignment)
54  {
55  return reinterpret_cast<void*>(
56  ((reinterpret_cast<uintptr_t>(ptr) + alignment - 1) / alignment) * alignment);
57  }
58 
59  /*!
60  @brief アライメントが合うように繰り下げる(2の累乗限定で高速バージョン)
61 
62  @param ptr アライメントさせたいポインタ
63  @param alignment アライメントの指定(2の累乗でなければならない。単位はByte)
64  @return アライメント後のポインタ
65  */
66  static void* roundDownPow2(const void* ptr, uint32_t alignment)
67  {
68  // 処理削減のためにASSERTもしない
69  return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(ptr) & ~(static_cast<uintptr_t>(alignment - 1)));
70  }
71 
72  /*!
73  @brief アライメントが合うように繰り下げる(2の累乗でなくてもよい)
74 
75  @param ptr アライメントさせたいポインタ
76  @param alignment アライメントの指定(2の累乗でなくてもよい。単位はByte)
77  @return アライメント後のポインタ
78  */
79  static void* roundDownN(const void* ptr, uint32_t alignment)
80  {
81  return reinterpret_cast<void*>(
82  (reinterpret_cast<uintptr_t>(ptr) / alignment) * alignment);
83  }
84 
85  /*!
86  @brief アドレスにオフセットを足す
87 
88  @param ptr アドレス
89  @param offset 足す値
90  @return 計算後の値
91  */
92  static void* addOffset(const void* ptr, ptrdiff_t offset)
93  {
94  return reinterpret_cast<void*>(static_cast<uintptr_t>(reinterpret_cast<uintptr_t>(ptr) + offset));
95  }
96 
97  /*!
98  @brief アドレスとアドレスの差を計算する(a - b)
99 
100  @param a アドレス
101  @param b アドレス
102  @return 差
103  */
104  static ptrdiff_t diff(const void* a, const void* b)
105  {
106  return reinterpret_cast<ptrdiff_t>(a) - reinterpret_cast<ptrdiff_t>(b);
107  }
108 
109  /*!
110  @brief 範囲内かどうかを調べる
111 
112  @param ptr 調べるアドレス
113  @param begin 開始アドレス
114  @param end 終了アドレス
115  @return 範囲内ならtrue
116  */
117  static bool isInclude(const void* ptr, const void* begin, const void* end)
118  {
119  return (reinterpret_cast<uintptr_t>(ptr) >= reinterpret_cast<uintptr_t>(begin) && reinterpret_cast<uintptr_t>(ptr) < reinterpret_cast<uintptr_t>(end));
120  }
121 
122  /*!
123  @brief アライメントされているかどうかを調べる(2の累乗でなくてもよい)
124 
125  @details isAlignedN()と同等の機能です。
126  @param ptr 調べるアドレス
127  @param alignment アライメント
128  @return アライメントされていればtrue
129  */
130  static bool isAligned(const void* ptr, int alignment)
131  {
132  PIA_ASSERT(alignment != 0);
133  // マイナスのalignmentでも大丈夫なはず。
134  // 余りはC99以前は処理系依存だが、割り切れる場合には0が返されるしかないはず
135  return (reinterpret_cast<uintptr_t>(ptr) % alignment == 0);
136  }
137 
138  /*!
139  @brief アライメントされているかどうかを調べる(2の累乗限定で高速バージョン)
140 
141  @param ptr 調べるアドレス
142  @param alignment アライメント
143  @return アライメントされていればtrue
144  */
145  static bool isAlignedPow2(const void* ptr, uint32_t alignment)
146  {
147  // 処理削減のためにASSERTもしない
148  return ((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) == 0);
149  }
150 
151  /*!
152  @brief アライメントされているかどうかを調べる(2の累乗でなくてもよい)
153 
154  @param ptr 調べるアドレス
155  @param alignment アライメント
156  @return アライメントされていればtrue
157  */
158  static bool isAlignedN(const void* ptr, int alignment)
159  {
160  PIA_ASSERT(alignment != 0);
161  // マイナスのalignmentでも大丈夫なはず。
162  // 余りはC99以前は処理系依存だが、割り切れる場合には0が返されるしかないはず
163  return (reinterpret_cast<uintptr_t>(ptr) % alignment == 0);
164  }
165 };
166 ///@endcond
167 }
168 }
169 } // end of namespace nn::pia::common