nlib
ExiAllocator.h
Go to the documentation of this file.
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 : prv_(rhs.prv_) {}
41  prv_ = rhs.prv_;
42  return *this;
43  }
44  NLIB_DEFMOVE_PIMPL(ExiAllocatorEx);
45 
46  errno_t Init(void* p, size_t size) NLIB_NOEXCEPT;
47  void* Finalize() NLIB_NOEXCEPT;
48  void* Alloc(size_t nbytes) NLIB_NOEXCEPT;
49  void* Realloc(void* ptr, size_t from_nbytes, size_t nbytes) NLIB_NOEXCEPT;
50  void Dealloc(void* p) NLIB_NOEXCEPT;
51  void Reset() NLIB_NOEXCEPT;
52  bool Test(size_t nbytes) NLIB_NOEXCEPT;
53  size_t GetPos() NLIB_NOEXCEPT;
54  bool IsOutOfMemory() NLIB_NOEXCEPT;
55 
56  bool IsValid() const NLIB_NOEXCEPT { return prv_ != nullptr; }
57  NLIB_SAFE_BOOL(ExiAllocatorEx, IsValid())
58 
59  private:
60  explicit ExiAllocatorEx(ExiAllocatorInfo* p) NLIB_NOEXCEPT : prv_(p) {}
61  ExiAllocatorInfo* prv_;
62  friend class ExiAllocator;
63 };
64 
66  public:
67  static errno_t Init(void* p, size_t size) NLIB_NOEXCEPT;
68  static void* Finalize() NLIB_NOEXCEPT;
69  static void* Alloc(size_t nbytes) NLIB_NOEXCEPT;
70  static void* Realloc(void* ptr, size_t from_nbytes, size_t nbytes) NLIB_NOEXCEPT;
71  static void Dealloc(void* p) NLIB_NOEXCEPT;
72  static void Reset() NLIB_NOEXCEPT;
73  static bool Test(size_t nbytes) NLIB_NOEXCEPT;
74  static size_t GetPos() NLIB_NOEXCEPT;
75  static bool IsOutOfMemory() NLIB_NOEXCEPT;
76  static ExiAllocatorEx GetAllocator() NLIB_NOEXCEPT;
77  static void Swap(ExiAllocatorEx& rhs) NLIB_NOEXCEPT;
78 
79  private:
80  ExiAllocator();
82 };
83 
84 class NLIB_VIS_PUBLIC ExiAllocatorSwapper NLIB_FINAL {
85  public:
86  explicit ExiAllocatorSwapper(ExiAllocatorEx& rhs) NLIB_NOEXCEPT : m_Al(rhs), m_Swapped(rhs) {
87  if (m_Al) ExiAllocator::Swap(rhs);
88  }
89  ~ExiAllocatorSwapper() NLIB_NOEXCEPT {
90  if (m_Swapped) ExiAllocator::Swap(m_Al);
91  }
92 
93  private:
94  ExiAllocatorEx& m_Al;
95  bool m_Swapped;
96  NLIB_DISALLOW_COPY_AND_ASSIGN(ExiAllocatorSwapper);
97 };
98 
99 #ifdef NN_PLATFORM_CTR
100 // NOTE: ARMCC does not support placement delete
101 #define NEXI_DEF_NEW_DELETE(typeName) \
102  static void* operator new(size_t size) throw() { \
103  NLIB_ASSERT(size == sizeof(typeName)); \
104  return ExiAllocator::Alloc(size); \
105  } \
106  static void* operator new(size_t size, const std::nothrow_t&) throw() { \
107  NLIB_ASSERT(size == sizeof(typeName)); \
108  return ExiAllocator::Alloc(size); \
109  } \
110  static void* operator new(size_t size, void* ptr) throw() { \
111  return ::operator new(size, ptr); \
112  } \
113  static void operator delete(void* p) throw() { ExiAllocator::Dealloc(p); } \
114  static void* operator new[](size_t size, void* p) throw(); \
115  static void operator delete[](void* p) throw()
116 
117 #define NEXI_DISALLOW_NEW_DELETE(typeName) \
118  public: \
119  static void* operator new(size_t size, void* ptr) throw() { \
120  return ::operator new(size, ptr); \
121  } \
122  \
123  private: \
124  static void* operator new(size_t size) throw(); \
125  static void* operator new(size_t size, const std::nothrow_t&) throw(); \
126  static void operator delete(void* p) throw(); \
127  static void* operator new[](size_t size, void* p) throw(); \
128  static void* operator new[](size_t size, const std::nothrow_t&) throw(); \
129  static void operator delete[](void* p) throw()
130 #else
131 #define NEXI_DEF_NEW_DELETE(typeName) \
132  private: \
133  static void* operator new[](size_t size); \
134  static void* operator new[](size_t size, void* p) NLIB_NOEXCEPT; \
135  static void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
136  static void operator delete[](void* p) NLIB_NOEXCEPT; \
137  static void operator delete[](void* p, void* ptr) NLIB_NOEXCEPT; \
138  static void operator delete[](void* p, const std::nothrow_t&) NLIB_NOEXCEPT; \
139  \
140  public: \
141  static void* operator new(size_t size) { \
142  NLIB_ASSERT(size == sizeof(typeName)); \
143  return ExiAllocator::Alloc(size); \
144  } \
145  static void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT { \
146  NLIB_ASSERT(size == sizeof(typeName)); \
147  return ExiAllocator::Alloc(size); \
148  } \
149  static void* operator new(size_t size, void* ptr) NLIB_NOEXCEPT { \
150  return ::operator new(size, ptr); \
151  } \
152  static void operator delete(void* p)NLIB_NOEXCEPT { ExiAllocator::Dealloc(p); } \
153  static void operator delete(void* p, const std::nothrow_t&)NLIB_NOEXCEPT { \
154  ExiAllocator::Dealloc(p); \
155  } \
156  static void operator delete(void* p, void* ptr)NLIB_NOEXCEPT { \
157  return ::operator delete(p, ptr); \
158  }
159 
160 #ifndef CAFE
161 #define NEXI_DISALLOW_NEW_DELETE(typeName) \
162  public: \
163  static void* operator new(size_t size, void* ptr) NLIB_NOEXCEPT { \
164  return ::operator new(size, ptr); \
165  } \
166  static void operator delete(void* p, void* ptr)NLIB_NOEXCEPT { \
167  return ::operator delete(p, ptr); \
168  } \
169  static void operator delete(void* p)NLIB_NOEXCEPT { \
170  NLIB_UNUSED(p); \
171  NLIB_ASSUME(0); \
172  } \
173  \
174  private: \
175  static void* operator new(size_t size); \
176  static void* operator new(size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
177  static void operator delete(void* p, const std::nothrow_t&)NLIB_NOEXCEPT; \
178  static void* operator new[](size_t size); \
179  static void* operator new[](size_t size, void* p) NLIB_NOEXCEPT; \
180  static void* operator new[](size_t size, const std::nothrow_t&) NLIB_NOEXCEPT; \
181  static void operator delete[](void* p) NLIB_NOEXCEPT; \
182  static void operator delete[](void* p, void* ptr) NLIB_NOEXCEPT; \
183  static void operator delete[](void* p, const std::nothrow_t&) NLIB_NOEXCEPT
184 #else
185 #define NEXI_DISALLOW_NEW_DELETE(typeName) NLIB_STATIC_ASSERT(true)
186 #endif
187 #endif
188 
189 } // namespace exi
190 NLIB_NAMESPACE_END
191 
192 #if defined(_MSC_VER) && defined(nx_exi_EXPORTS)
193 #undef NLIB_VIS_PUBLIC
194 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
195 #endif
196 
197 #endif // INCLUDE_NN_NLIB_EXI_EXIALLOCATOR_H_
Allocator used by the XML parser. The user can also use it.
Definition: ExiAllocator.h:65
Defines constructs such as string-type typedef statements and utility macros.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:183
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:199
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
constexpr ExiAllocatorEx() noexcept
Instantiates the object with default parameters (default constructor).
Definition: ExiAllocator.h:37
Allocator that can be set for each instance of XmlStreamReader and XmlStreamWriter.
Definition: ExiAllocator.h:35
ExiAllocatorEx & operator=(const ExiAllocatorEx &rhs) noexcept
Assignment operator.
Definition: ExiAllocator.h:40
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:111
A file that contains the configuration information for each development environment.
~ExiAllocatorEx() noexcept
Destructor.
Definition: ExiAllocator.h:38
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
ExiAllocatorEx(const ExiAllocatorEx &rhs) noexcept
Copy constructor.
Definition: ExiAllocator.h:39
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37