FBX C++ API Reference
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fbxalloc.h
Go to the documentation of this file.
1 /****************************************************************************************
2 
3  Copyright (C) 2015 Autodesk, Inc.
4  All rights reserved.
5 
6  Use of this software is subject to the terms of the Autodesk license agreement
7  provided at the time of installation or download, or which otherwise accompanies
8  this software in either electronic or hard copy form.
9 
10 ****************************************************************************************/
11 
20 #ifndef _FBXSDK_CORE_ARCH_ALLOC_H_
21 #define _FBXSDK_CORE_ARCH_ALLOC_H_
22 
23 #include <fbxsdk/fbxsdk_def.h>
24 
25 #if defined(_DEBUG) && defined(FBXSDK_ENV_WIN)
26  #include <crtdbg.h>
27 #endif
28 
29 #if defined(FBXSDK_ENV_MAC)
30  #include <malloc/malloc.h>
31 #else
32  #include <malloc.h>
33 #endif
34 
35 #include <fbxsdk/fbxsdk_nsbegin.h>
36 
37 #if defined(FBXSDK_CPU_32) && !defined(FBXSDK_ENV_IOS)
38  #define FBXSDK_MEMORY_ALIGNMENT ((size_t)8U)
39 #else
40  #define FBXSDK_MEMORY_ALIGNMENT ((size_t)16U)
41 #endif
42 
43 #define FBXSDK_MEMORY_COPY(dst, src, size) {memcpy(dst,src,size);}
44 
45 typedef void* (*FbxMallocProc)(size_t);
46 typedef void* (*FbxCallocProc)(size_t, size_t);
47 typedef void* (*FbxReallocProc)(void*, size_t);
48 typedef void (*FbxFreeProc)(void*);
49 
53 
57 
61 
65 
69 
73 
77 
81 
85 
89 
93 
97 
98 /*****************************************************************************************************************************
99 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
100 *****************************************************************************************************************************/
101 #ifndef DOXYGEN_SHOULD_SKIP_THIS
102  FBXSDK_DLL void* FbxMalloc(size_t pSize);
103  FBXSDK_DLL void* FbxCalloc(size_t pCount, size_t pSize);
104  FBXSDK_DLL void* FbxRealloc(void* pData, size_t pSize);
105  FBXSDK_DLL void FbxFree(void* pData);
106  FBXSDK_DLL char* FbxStrDup(const char* pString);
107  FBXSDK_DLL wchar_t* FbxStrDupWC(const wchar_t* pString);
108 
109  //These versions of allocators use the default system mallocs, and on Windows we also pass the debugging parameters.
110  //If you define FBXSDK_ALLOC_DEBUG in your project, the FBX SDK will use these debug versions everywhere.
111  FBXSDK_DLL void* FbxMallocDebug(size_t pSize, int pBlock, const char* pFile, int pLine);
112  FBXSDK_DLL void* FbxCallocDebug(size_t pCount, size_t pSize, int pBlock, const char* pFile, int pLine);
113  FBXSDK_DLL void* FbxReallocDebug(void* pData, size_t pSize, int pBlock, const char* pFile, int pLine);
114  FBXSDK_DLL void FbxFreeDebug(void* pData, int pBlock);
115 
116  //When FBXSDK_ALLOC_DEBUG is defined, redirect allocation calls to the debug version.
117  #if defined(FBXSDK_ALLOC_DEBUG)
118  #define FbxMalloc(s) FbxMallocDebug(s, _NORMAL_BLOCK, __FILE__, __LINE__)
119  #define FbxCalloc(c, s) FbxCallocDebug(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
120  #define FbxRealloc(p, s) FbxReallocDebug(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
121  #define FbxFree(p) FbxFreeDebug(p, _NORMAL_BLOCK)
122  #endif
123 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
124 
126 template <class Type> class FbxDeletionPolicyDefault
127 {
128 public:
130  static inline void DeleteIt(Type** pPtr)
131  {
132  if( *pPtr )
133  {
134  delete *pPtr;
135  *pPtr = NULL;
136  }
137  }
138 };
139 
141 template<typename T> void FbxDelete(T* p);
142 template<typename T> void FbxDelete(const T* p);
143 template <class Type> class FbxDeletionPolicyDelete
144 {
145 public:
147  static inline void DeleteIt(Type** mPtr)
148  {
149  if( *mPtr )
150  {
151  FbxDelete(*mPtr);
152  *mPtr = NULL;
153  }
154  }
155 };
156 
158 template <class Type> class FbxDeletionPolicyFree
159 {
160 public:
162  static inline void DeleteIt(Type** pPtr)
163  {
164  if( *pPtr )
165  {
166  FbxFree(*pPtr);
167  *pPtr = NULL;
168  }
169  }
170 };
171 
173 template <class Type> class FbxDeletionPolicyObject
174 {
175 public:
177  static inline void DeleteIt(Type** pPtr)
178  {
179  if( *pPtr )
180  {
181  (*pPtr)->Destroy();
182  *pPtr = NULL;
183  }
184  }
185 };
186 
190 template<class Type, class Policy=FbxDeletionPolicyDefault<Type> > class FbxAutoPtr
191 {
192 public:
194  explicit FbxAutoPtr(Type* pPtr=0) : mPtr(pPtr){}
195 
197  ~FbxAutoPtr() { Policy::DeleteIt(&mPtr); }
198 
200  inline Type* Get() const { return mPtr; }
201 
203  inline Type* operator->() const { return mPtr; }
204 
206  inline operator Type* () const { return mPtr; }
207 
209  inline Type& operator*() const { return *mPtr; }
210 
212  inline bool operator!() const { return mPtr == 0; }
213 
215  inline operator bool () const { return mPtr != 0; }
216 
218  inline void Reset(Type* pPtr=0)
219  {
220  FBX_ASSERT(pPtr == 0 || pPtr != mPtr); //Catch self-reset errors
221  FbxAutoPtr<Type, Policy>(pPtr).Swap(*this);
222  }
223 
225  inline void Swap(FbxAutoPtr& pOther)
226  {
227  Type* TmpPtr = pOther.mPtr;
228  pOther.mPtr = mPtr;
229  mPtr = TmpPtr;
230  }
231 
233  inline Type* Release()
234  {
235  Type* TmpPtr = mPtr;
236  mPtr = NULL;
237  return TmpPtr;
238  }
239 
240 /*****************************************************************************************************************************
241 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
242 *****************************************************************************************************************************/
243 #ifndef DOXYGEN_SHOULD_SKIP_THIS
244 private:
245  FbxAutoPtr(const FbxAutoPtr&);
246  FbxAutoPtr& operator=(const FbxAutoPtr&);
247 
248  Type* mPtr;
249 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
250 };
251 
253 template <class Type> class FbxAutoFreePtr : public FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >
254 {
255 public:
257  explicit FbxAutoFreePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >(pPtr){}
258 };
259 
261 template <class Type> class FbxAutoDeletePtr : public FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >
262 {
263 public:
265  explicit FbxAutoDeletePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >(pPtr){}
266 };
267 
269 template <class Type> class FbxAutoDestroyPtr : public FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >
270 {
271 public:
273  explicit FbxAutoDestroyPtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >(pPtr){}
274 };
275 
276 
280 class RefCount
281 {
282 public:
283  RefCount() { Init(); };
284  ~RefCount() { Init(); };
285 
286  void Init() { count = 0; }
287  void IncRef() { count++; }
288  int DecRef() { count--; if (count < 0) count = 0; return count; }
289 
290 private:
291  int count;
292 };
293 
294 template<class Type, class Policy=FbxDeletionPolicyDefault<Type> > class FbxSharedPtr
295 {
296 public:
297  // Default constructor.
299  mPtr(0),
300  mRef(0)
301  {}
302 
304  explicit FbxSharedPtr(Type* pPtr) :
305  mPtr(pPtr),
306  mRef(0)
307  {
308  if (pPtr != 0)
309  {
310  mRef = (RefCount*)FbxMalloc(sizeof(RefCount));
311  mRef->Init();
312  mRef->IncRef();
313  }
314  }
315 
317  FbxSharedPtr(const FbxSharedPtr& pSPtr) :
318  mPtr(pSPtr.mPtr),
319  mRef(pSPtr.mRef)
320  {
321  if (pSPtr.mPtr != 0 && mRef != 0)
322  mRef->IncRef();
323  }
324 
325  // Assignment operator
327  {
328  if (this != &pSPtr) // avoid self assignment
329  {
330  Reset();
331 
332  if (pSPtr.mPtr)
333  {
334  mPtr = pSPtr.mPtr;
335  mRef = pSPtr.mRef;
336  FBX_ASSERT(mRef != NULL);
337  mRef->IncRef();
338  }
339  }
340  return *this;
341  }
342 
344  ~FbxSharedPtr() { Destroy(); }
345 
346  void Destroy() { Reset(); }
347 
349  inline Type* Get() const { return mPtr; }
350 
352  inline Type* operator->() const { return mPtr; }
353 
355  inline operator Type* () const { return mPtr; }
356 
358  inline Type& operator*() const { return *mPtr; }
359 
361  inline bool operator!() const { return mPtr == 0; }
362 
364  inline operator bool () const { return mPtr != 0; }
365 
366 
367 /*****************************************************************************************************************************
368 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
369 *****************************************************************************************************************************/
370 #ifndef DOXYGEN_SHOULD_SKIP_THIS
371 private:
372  void Reset()
373  {
374  if (mRef)
375  {
376  FBX_ASSERT(mPtr != 0);
377  if (mRef->DecRef() == 0)
378  {
379  Policy::DeleteIt(&mPtr);
380  FbxFree(mRef);
381  mRef = NULL;
382  }
383  }
384  }
385 
386  Type* mPtr;
387  RefCount* mRef;
388 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
389 };
390 
392 template <class Type> class FbxSharedFreePtr : public FbxSharedPtr<Type, FbxDeletionPolicyFree<Type> >
393 {
394 public:
396  explicit FbxSharedFreePtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyFree<Type> >(pPtr){}
397 };
398 
400 template <class Type> class FbxSharedDeletePtr : public FbxSharedPtr<Type, FbxDeletionPolicyDelete<Type> >
401 {
402 public:
404  explicit FbxSharedDeletePtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyDelete<Type> >(pPtr){}
405 };
406 
408 template <class Type> class FbxSharedDestroyPtr : public FbxSharedPtr<Type, FbxDeletionPolicyObject<Type> >
409 {
410 public:
412  explicit FbxSharedDestroyPtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyObject<Type> >(pPtr){}
413 };
414 
415 
416 
417 #include <fbxsdk/fbxsdk_nsend.h>
418 
419 #endif /* _FBXSDK_CORE_ARCH_ALLOC_H_ */
void Reset(Type *pPtr=0)
Reset the scoped pointer by swapping with another pointer.
Definition: fbxalloc.h:218
FbxAutoPtr mimics the auto_ptr class template implementation available in the C++ Standard Library...
Definition: fbxalloc.h:190
FbxSharedDestroyPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:412
FBX SDK environment definition.
void Destroy()
Definition: fbxalloc.h:346
FbxSharedPtr(Type *pPtr)
Construct from a pointer.
Definition: fbxalloc.h:304
void FbxSetFreeHandler(FbxFreeProc pHandler)
Set the global memory freeing function used internally by the FBX SDK.
FbxReallocProc FbxGetDefaultReallocHandler()
Get the default global memory re-allocation function used internally by the FBX SDK.
Deletion policy for pointer template classes that uses the FbxFree() function.
Definition: fbxalloc.h:158
FbxReallocProc FbxGetReallocHandler()
Get the global memory re-allocation function used internally by the FBX SDK.
FbxSharedPtr class describes an object that stores a pointer to a single allocated object of type Typ...
Definition: fbxalloc.h:280
#define NULL
Definition: fbxarch.h:210
FbxCallocProc FbxGetDefaultCallocHandler()
Get the default global zero&#39;d memory allocation function used internally by the FBX SDK...
FbxSharedPtr(const FbxSharedPtr &pSPtr)
Copy constructor.
Definition: fbxalloc.h:317
FbxMallocProc FbxGetMallocHandler()
Get the global memory allocation function used internally by the FBX SDK.
~FbxSharedPtr()
Destructor.
Definition: fbxalloc.h:344
Type * operator->() const
Member access operator.
Definition: fbxalloc.h:352
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:177
FbxAutoDeletePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:265
Deletion policy for pointer template classes that uses the Destroy() function.
Definition: fbxalloc.h:173
Type * Get() const
Retrieve the pointer it holds.
Definition: fbxalloc.h:200
FbxAutoDestroyPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:273
void FbxDelete(T *p)
Deletion policy for pointer template classes that uses the FbxDelete() function.
Definition: fbxnew.h:341
FbxSharedDeletePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:404
bool operator!() const
Logical not operator.
Definition: fbxalloc.h:212
int DecRef()
Definition: fbxalloc.h:288
Type & operator*() const
Dereference operator.
Definition: fbxalloc.h:209
void *(* FbxReallocProc)(void *, size_t)
Function pointer signature used to replace "calloc".
Definition: fbxalloc.h:47
FbxCallocProc FbxGetCallocHandler()
Get the global zero&#39;d memory allocation function used internally by the FBX SDK.
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:130
FbxFreeProc FbxGetDefaultFreeHandler()
Get the default global memory freeing function used internally by the FBX SDK.
void *(* FbxMallocProc)(size_t)
Definition: fbxalloc.h:45
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:162
Type & operator*() const
Dereference operator.
Definition: fbxalloc.h:358
void *(* FbxCallocProc)(size_t, size_t)
Function pointer signature used to replace "malloc".
Definition: fbxalloc.h:46
Scoped pointer for FbxNew allocations, which call FbxDelete() to deallocate.
Definition: fbxalloc.h:261
void FbxSetMallocHandler(FbxMallocProc pHandler)
Function pointer signature used to replace "free".
~RefCount()
Definition: fbxalloc.h:284
FbxFreeProc FbxGetFreeHandler()
Get the global memory freeing function used internally by the FBX SDK.
void FbxSetReallocHandler(FbxReallocProc pHandler)
Set the global memory re-allocation function used internally by the FBX SDK.
Scoped pointer for FbxMalloc allocations, which call FbxFree() to deallocate.
Definition: fbxalloc.h:253
void FbxSetCallocHandler(FbxCallocProc pHandler)
Set the global zero&#39;d memory allocation function used internally by the FBX SDK.
Scoped pointer for FbxObject derived classes, which call Destroy() to deallocate. ...
Definition: fbxalloc.h:269
Type * Get() const
Retrieve the pointer it holds.
Definition: fbxalloc.h:349
bool operator!() const
Logical not operator.
Definition: fbxalloc.h:361
~FbxAutoPtr()
Destructor.
Definition: fbxalloc.h:197
RefCount()
Definition: fbxalloc.h:283
Deletion policy for pointer template classes that uses the delete operator.
Definition: fbxalloc.h:126
#define FBXSDK_DLL
Definition: fbxarch.h:173
void IncRef()
Definition: fbxalloc.h:287
void Init()
Definition: fbxalloc.h:286
Scoped pointer for FbxNew allocations, which call FbxDelete() to deallocate.
Definition: fbxalloc.h:400
FbxAutoFreePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:257
FbxMallocProc FbxGetDefaultMallocHandler()
Get the default global memory allocation function used internally by the FBX SDK. ...
void(* FbxFreeProc)(void *)
Function pointer signature used to replace "realloc".
Definition: fbxalloc.h:48
Scoped pointer for FbxMalloc allocations, which call FbxFree() to deallocate.
Definition: fbxalloc.h:392
FbxAutoPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:194
FbxSharedPtr & operator=(const FbxSharedPtr &pSPtr)
Definition: fbxalloc.h:326
Scoped pointer for FbxObject derived classes, which call Destroy() to deallocate. ...
Definition: fbxalloc.h:408
void Swap(FbxAutoPtr &pOther)
Swap with another pointer.
Definition: fbxalloc.h:225
FbxSharedFreePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:396
Type * operator->() const
Member access operator.
Definition: fbxalloc.h:203
static void DeleteIt(Type **mPtr)
Destruction policy implementation.
Definition: fbxalloc.h:147
Type * Release()
Release the pointer, so that it won&#39;t perform deletion in its destruction.
Definition: fbxalloc.h:233