nlib
ExiAllocator.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_EXI_EXIALLOCATOR_H_
17 #define INCLUDE_NN_NLIB_EXI_EXIALLOCATOR_H_
18 
19 #include <new>
20 
21 #include "nn/nlib/Config.h"
22 #include "nn/nlib/exi/Types.h"
23 #include "nn/nlib/Swap.h"
24 
25 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
26 #undef NLIB_VIS_PUBLIC
27 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
28 #endif
29 
30 NLIB_NAMESPACE_BEGIN
31 namespace exi {
32 
33 struct ExiAllocatorInfo;
34 class ExiAllocator;
36  public:
39  ExiAllocatorEx(const ExiAllocatorEx& rhs) NLIB_NOEXCEPT : m_Ptr(rhs.m_Ptr) {}
41  m_Ptr = rhs.m_Ptr;
42  return *this;
43  }
44  NLIB_MOVE_MEMBER_HELPER_1(ExiAllocatorEx, m_Ptr);
46  using std::swap;
47  swap(rhs.m_Ptr, m_Ptr);
48  }
49 
50  errno_t Init(void* p, size_t size) NLIB_NOEXCEPT;
51  void* Finalize() NLIB_NOEXCEPT;
52  void* Alloc(size_t nbytes) NLIB_NOEXCEPT;
53  void* Realloc(void* ptr, size_t from_nbytes, size_t nbytes) NLIB_NOEXCEPT;
54  void Dealloc(void* p) NLIB_NOEXCEPT;
55  void Reset() NLIB_NOEXCEPT;
56  bool Test(size_t nbytes) NLIB_NOEXCEPT;
57  size_t GetPos() NLIB_NOEXCEPT;
58  bool IsOutOfMemory() NLIB_NOEXCEPT;
59 
60  bool IsValid() const NLIB_NOEXCEPT { return m_Ptr != NULL; }
61  NLIB_SAFE_BOOL(ExiAllocatorEx, IsValid())
62 
63  private:
64  explicit ExiAllocatorEx(ExiAllocatorInfo* p) NLIB_NOEXCEPT : m_Ptr(p) {}
65  ExiAllocatorInfo* m_Ptr;
66  friend class ExiAllocator;
67 };
68 
70  public:
71  static errno_t Init(void* p, size_t size) NLIB_NOEXCEPT;
72  static void* Finalize() NLIB_NOEXCEPT;
73  static void* Alloc(size_t nbytes) NLIB_NOEXCEPT;
74  static void* Realloc(void* ptr, size_t from_nbytes, size_t nbytes) NLIB_NOEXCEPT;
75  static void Dealloc(void* p) NLIB_NOEXCEPT;
76  static void Reset() NLIB_NOEXCEPT;
77  static bool Test(size_t nbytes) NLIB_NOEXCEPT;
78  static size_t GetPos() NLIB_NOEXCEPT;
79  static bool IsOutOfMemory() NLIB_NOEXCEPT;
80  static ExiAllocatorEx GetAllocator() NLIB_NOEXCEPT;
81  static void Swap(ExiAllocatorEx& rhs) NLIB_NOEXCEPT;
82 
83  private:
84  ExiAllocator();
86 };
87 
88 class NLIB_VIS_PUBLIC ExiAllocatorSwapper NLIB_FINAL {
89  public:
90  explicit ExiAllocatorSwapper(ExiAllocatorEx& rhs) NLIB_NOEXCEPT // NOLINT
91  : m_Al(rhs),
92  m_Swapped(rhs) {
93  if (m_Al) ExiAllocator::Swap(rhs);
94  }
95  ~ExiAllocatorSwapper() NLIB_NOEXCEPT {
96  if (m_Swapped) ExiAllocator::Swap(m_Al);
97  }
98 
99  private:
100  ExiAllocatorEx& m_Al;
101  bool m_Swapped;
102  NLIB_DISALLOW_COPY_AND_ASSIGN(ExiAllocatorSwapper);
103 };
104 
105 #ifdef NN_PLATFORM_CTR
106 // NOTE: ARMCC does not support placement delete
107 #define NEXI_DEF_NEW_DELETE(typeName) \
108  static void* operator new(size_t size) throw() { \
109  NLIB_ASSERT(size == sizeof(typeName)); \
110  return ExiAllocator::Alloc(size); \
111  } \
112  static void* operator new(size_t size, const std::nothrow_t&) throw() { \
113  NLIB_ASSERT(size == sizeof(typeName)); \
114  return ExiAllocator::Alloc(size); \
115  } \
116  static void* operator new(size_t size, void* ptr) throw() { \
117  return ::operator new(size, ptr); \
118  } \
119  static void operator delete(void* p) throw() { ExiAllocator::Dealloc(p); } \
120  static void* operator new[](size_t size, void* p) throw(); \
121  static void operator delete[](void* p) throw()
122 
123 #define NEXI_DISALLOW_NEW_DELETE(typeName) \
124 public: \
125  static void* operator new(size_t size, void* ptr) throw() { \
126  return ::operator new(size, ptr); \
127  } \
128 private: \
129  static void* operator new(size_t size) throw(); \
130  static void* operator new(size_t size, const std::nothrow_t&) throw(); \
131  static void operator delete(void* p) throw(); \
132  static void* operator new[](size_t size, void* p) throw(); \
133  static void* operator new[](size_t size, const std::nothrow_t&) throw(); \
134  static void operator delete[](void* p) throw()
135 #else
136 #define NEXI_DEF_NEW_DELETE(typeName) \
137 private: \
138  static void* operator new[](size_t size); \
139  static void* operator new[](size_t size, void* p) NLIB_NOEXCEPT; \
140  static void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
141  static void operator delete[](void* p) NLIB_NOEXCEPT; \
142  static void operator delete[](void* p, void* ptr) NLIB_NOEXCEPT; \
143  static void operator delete[](void* p, const std::nothrow_t&) NLIB_NOEXCEPT; \
144 public: \
145  static void* operator new(size_t size) { \
146  NLIB_ASSERT(size == sizeof(typeName)); \
147  return ExiAllocator::Alloc(size); \
148  } \
149  static void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
150  NLIB_ASSERT(size == sizeof(typeName)); \
151  return ExiAllocator::Alloc(size); \
152  } \
153  static void* operator new(size_t size, void* ptr) NLIB_NOEXCEPT { \
154  return ::operator new(size, ptr); \
155  } \
156  static void operator delete(void* p) NLIB_NOEXCEPT { ExiAllocator::Dealloc(p); } \
157  static void operator delete(void* p, const std::nothrow_t&) NLIB_NOEXCEPT { \
158  ExiAllocator::Dealloc(p); \
159  } \
160  static void operator delete(void* p, void* ptr) NLIB_NOEXCEPT { \
161  return ::operator delete(p, ptr); \
162  }
163 
164 #ifndef CAFE
165 #define NEXI_DISALLOW_NEW_DELETE(typeName) \
166 public: \
167  static void* operator new(size_t size, void* ptr) NLIB_NOEXCEPT { \
168  return ::operator new(size, ptr); \
169  } \
170  static void operator delete(void* p, void* ptr) NLIB_NOEXCEPT { \
171  return ::operator delete(p, ptr); \
172  } \
173  static void operator delete(void* p) NLIB_NOEXCEPT { \
174  NLIB_UNUSED(p); \
175  NLIB_ASSUME(0); \
176  } \
177 private: \
178  static void* operator new(size_t size); \
179  static void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
180  static void operator delete(void* p, const std::nothrow_t&) NLIB_NOEXCEPT; \
181  static void* operator new[](size_t size); \
182  static void* operator new[](size_t size, void* p) NLIB_NOEXCEPT; \
183  static void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
184  static void operator delete[](void* p) NLIB_NOEXCEPT; \
185  static void operator delete[](void* p, void* ptr) NLIB_NOEXCEPT; \
186  static void operator delete[](void* p, const std::nothrow_t&) NLIB_NOEXCEPT
187 #else
188 #define NEXI_DISALLOW_NEW_DELETE(typeName) NLIB_STATIC_ASSERT(true)
189 #endif
190 #endif
191 
192 } // namespace exi
193 NLIB_NAMESPACE_END
194 
195 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
196 #undef NLIB_VIS_PUBLIC
197 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
198 #endif
199 
200 #endif // INCLUDE_NN_NLIB_EXI_EXIALLOCATOR_H_
XMLパーサーが内部で利用するアロケータです。ユーザーが利用することもできます。
Definition: ExiAllocator.h:69
文字列型のtypedefやユーティリティマクロ等が定義されています。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:163
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:178
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
bool IsValid() const noexcept
アロケータが初期化されていればtrueを返します。
Definition: ExiAllocator.h:60
void swap(ExiAllocatorEx &rhs) noexcept
オブジェクトの内容をスワップします。
Definition: ExiAllocator.h:45
constexpr ExiAllocatorEx() noexcept
デフォルトコンストラクタです。
Definition: ExiAllocator.h:37
XmlStreamReader, XmlStreamWriter の各インスタンス毎に設定できるアロケータです。
Definition: ExiAllocator.h:35
ExiAllocatorEx & operator=(const ExiAllocatorEx &rhs) noexcept
代入演算子です。
Definition: ExiAllocator.h:40
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:93
開発環境別の設定が書かれるファイルです。
~ExiAllocatorEx() noexcept
デストラクタです。
Definition: ExiAllocator.h:38
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
ExiAllocatorEx(const ExiAllocatorEx &rhs) noexcept
コピーコンストラクタです。
Definition: ExiAllocator.h:39
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37