FBX C++ API Reference
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 size_t FbxAllocSize(size_t pNbItems, size_t pItemSize);
103  FBXSDK_DLL void* FbxMalloc(size_t pSize);
104  FBXSDK_DLL void* FbxCalloc(size_t pCount, size_t pSize);
105  FBXSDK_DLL void* FbxRealloc(void* pData, size_t pSize);
106  FBXSDK_DLL void FbxFree(void* pData);
107  FBXSDK_DLL char* FbxStrDup(const char* pString);
108  FBXSDK_DLL wchar_t* FbxStrDupWC(const wchar_t* pString);
109 
110  //These versions of allocators use the default system mallocs, and on Windows we also pass the debugging parameters.
111  //If you define FBXSDK_ALLOC_DEBUG in your project, the FBX SDK will use these debug versions everywhere.
112  FBXSDK_DLL void* FbxMallocDebug(size_t pSize, int pBlock, const char* pFile, int pLine);
113  FBXSDK_DLL void* FbxCallocDebug(size_t pCount, size_t pSize, int pBlock, const char* pFile, int pLine);
114  FBXSDK_DLL void* FbxReallocDebug(void* pData, size_t pSize, int pBlock, const char* pFile, int pLine);
115  FBXSDK_DLL void FbxFreeDebug(void* pData, int pBlock);
116 
117  //When FBXSDK_ALLOC_DEBUG is defined, redirect allocation calls to the debug version.
118  #if defined(FBXSDK_ALLOC_DEBUG)
119  #define FbxMalloc(s) FbxMallocDebug(s, _NORMAL_BLOCK, __FILE__, __LINE__)
120  #define FbxCalloc(c, s) FbxCallocDebug(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
121  #define FbxRealloc(p, s) FbxReallocDebug(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
122  #define FbxFree(p) FbxFreeDebug(p, _NORMAL_BLOCK)
123  #endif
124 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
125 
127 template <class Type> class FbxDeletionPolicyDefault
128 {
129 public:
131  static inline void DeleteIt(Type** pPtr)
132  {
133  if( *pPtr )
134  {
135  delete *pPtr;
136  *pPtr = NULL;
137  }
138  }
139 };
140 
142 template <class Type> class FbxDeletionPolicyArray
143 {
144 public:
146  static inline void DeleteIt(Type** pPtr)
147  {
148  if (*pPtr)
149  {
150  delete[] *pPtr;
151  *pPtr = NULL;
152  }
153  }
154 };
155 
157 template<typename T> void FbxDelete(T* p);
158 template<typename T> void FbxDelete(const T* p);
159 template <class Type> class FbxDeletionPolicyDelete
160 {
161 public:
163  static inline void DeleteIt(Type** mPtr)
164  {
165  if( *mPtr )
166  {
167  FbxDelete(*mPtr);
168  *mPtr = NULL;
169  }
170  }
171 };
172 
174 template <class Type> class FbxDeletionPolicyFree
175 {
176 public:
178  static inline void DeleteIt(Type** pPtr)
179  {
180  if( *pPtr )
181  {
182  FbxFree(*pPtr);
183  *pPtr = NULL;
184  }
185  }
186 };
187 
189 template <class Type> class FbxDeletionPolicyObject
190 {
191 public:
193  static inline void DeleteIt(Type** pPtr)
194  {
195  if( *pPtr )
196  {
197  (*pPtr)->Destroy();
198  *pPtr = NULL;
199  }
200  }
201 };
202 
206 template<class Type, class Policy=FbxDeletionPolicyDefault<Type> > class FbxAutoPtr
207 {
208 public:
210  explicit FbxAutoPtr(Type* pPtr=0) : mPtr(pPtr){}
211 
213  ~FbxAutoPtr() { Policy::DeleteIt(&mPtr); }
214 
216  inline Type* Get() const { return mPtr; }
217 
219  inline Type* operator->() const { return mPtr; }
220 
222  inline operator Type* () const { return mPtr; }
223 
225  inline Type& operator*() const { return *mPtr; }
226 
228  inline bool operator!() const { return mPtr == 0; }
229 
231  inline operator bool () const { return mPtr != 0; }
232 
234  inline void Reset(Type* pPtr=0)
235  {
236  FBX_ASSERT(pPtr == 0 || pPtr != mPtr); //Catch self-reset errors
237  FbxAutoPtr<Type, Policy>(pPtr).Swap(*this);
238  }
239 
241  inline void Swap(FbxAutoPtr& pOther)
242  {
243  Type* TmpPtr = pOther.mPtr;
244  pOther.mPtr = mPtr;
245  mPtr = TmpPtr;
246  }
247 
249  inline Type* Release()
250  {
251  Type* TmpPtr = mPtr;
252  mPtr = NULL;
253  return TmpPtr;
254  }
255 
256 /*****************************************************************************************************************************
257 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
258 *****************************************************************************************************************************/
259 #ifndef DOXYGEN_SHOULD_SKIP_THIS
260 private:
261  FbxAutoPtr(const FbxAutoPtr&);
262  FbxAutoPtr& operator=(const FbxAutoPtr&);
263 
264  Type* mPtr;
265 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
266 };
267 
269 template <class Type> class FbxAutoFreePtr : public FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >
270 {
271 public:
273  explicit FbxAutoFreePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >(pPtr){}
274 };
275 
277 template <class Type> class FbxAutoDeletePtr : public FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >
278 {
279 public:
281  explicit FbxAutoDeletePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >(pPtr){}
282 };
283 
285 template <class Type> class FbxAutoDestroyPtr : public FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >
286 {
287 public:
289  explicit FbxAutoDestroyPtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >(pPtr){}
290 };
291 
292 
296 class RefCount
297 {
298 public:
299  RefCount() { Init(); };
300  ~RefCount() { Init(); };
301 
302  void Init() { count = 0; }
303  void IncRef() { count++; }
304  int DecRef() { count--; if (count < 0) count = 0; return count; }
305 
306 private:
307  int count;
308 };
309 
310 template<class Type, class Policy=FbxDeletionPolicyDefault<Type> > class FbxSharedPtr
311 {
312 public:
313  // Default constructor.
315  mPtr(0),
316  mRef(0)
317  {}
318 
320  explicit FbxSharedPtr(Type* pPtr) :
321  mPtr(pPtr),
322  mRef(0)
323  {
324  if (pPtr != 0)
325  {
326  mRef = (RefCount*)FbxMalloc(sizeof(RefCount));
327  mRef->Init();
328  mRef->IncRef();
329  }
330  }
331 
333  FbxSharedPtr(const FbxSharedPtr& pSPtr) :
334  mPtr(pSPtr.mPtr),
335  mRef(pSPtr.mRef)
336  {
337  if (pSPtr.mPtr != 0 && mRef != 0)
338  mRef->IncRef();
339  }
340 
341  // Assignment operator
343  {
344  if (this != &pSPtr) // avoid self assignment
345  {
346  Reset();
347 
348  if (pSPtr.mPtr)
349  {
350  mPtr = pSPtr.mPtr;
351  mRef = pSPtr.mRef;
352  FBX_ASSERT(mRef != NULL);
353  mRef->IncRef();
354  }
355  }
356  return *this;
357  }
358 
361 
362  void Destroy() { Reset(); }
363 
365  inline Type* Get() const { return mPtr; }
366 
368  inline Type* operator->() const { return mPtr; }
369 
371  inline operator Type* () const { return mPtr; }
372 
374  inline Type& operator*() const { return *mPtr; }
375 
377  inline bool operator!() const { return mPtr == 0; }
378 
380  inline operator bool () const { return mPtr != 0; }
381 
382 
383 /*****************************************************************************************************************************
384 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
385 *****************************************************************************************************************************/
386 #ifndef DOXYGEN_SHOULD_SKIP_THIS
387 private:
388  void Reset()
389  {
390  if (mRef)
391  {
392  FBX_ASSERT(mPtr != 0);
393  if (mRef->DecRef() == 0)
394  {
395  Policy::DeleteIt(&mPtr);
396  FbxFree(mRef);
397  mRef = NULL;
398  }
399  }
400  }
401 
402  Type* mPtr;
403  RefCount* mRef;
404 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
405 };
406 
408 template <class Type> class FbxSharedFreePtr : public FbxSharedPtr<Type, FbxDeletionPolicyFree<Type> >
409 {
410 public:
412  explicit FbxSharedFreePtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyFree<Type> >(pPtr){}
413 };
414 
416 template <class Type> class FbxSharedDeletePtr : public FbxSharedPtr<Type, FbxDeletionPolicyDelete<Type> >
417 {
418 public:
420  explicit FbxSharedDeletePtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyDelete<Type> >(pPtr){}
421 };
422 
424 template <class Type> class FbxSharedDestroyPtr : public FbxSharedPtr<Type, FbxDeletionPolicyObject<Type> >
425 {
426 public:
428  explicit FbxSharedDestroyPtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyObject<Type> >(pPtr){}
429 };
430 
431 
432 
433 #include <fbxsdk/fbxsdk_nsend.h>
434 
435 #endif /* _FBXSDK_CORE_ARCH_ALLOC_H_ */
void Reset(Type *pPtr=0)
Reset the scoped pointer by swapping with another pointer.
Definition: fbxalloc.h:234
FbxAutoPtr mimics the auto_ptr class template implementation available in the C++ Standard Library...
Definition: fbxalloc.h:206
FbxSharedDestroyPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:428
FBX SDK environment definition.
void Destroy()
Definition: fbxalloc.h:362
FbxSharedPtr(Type *pPtr)
Construct from a pointer.
Definition: fbxalloc.h:320
void FbxSetFreeHandler(FbxFreeProc pHandler)
Set the global memory freeing function used internally by the FBX SDK.
Type * Get() const
Retrieve the pointer it holds.
Definition: fbxalloc.h:365
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:174
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:296
#define NULL
Definition: fbxarch.h:213
FbxCallocProc FbxGetDefaultCallocHandler()
Get the default global zero'd memory allocation function used internally by the FBX SDK...
FbxSharedPtr(const FbxSharedPtr &pSPtr)
Copy constructor.
Definition: fbxalloc.h:333
FbxMallocProc FbxGetMallocHandler()
Get the global memory allocation function used internally by the FBX SDK.
~FbxSharedPtr()
Destructor.
Definition: fbxalloc.h:360
Type * operator->() const
Member access operator.
Definition: fbxalloc.h:368
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:193
FbxAutoDeletePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:281
Deletion policy for pointer template classes that uses the Destroy() function.
Definition: fbxalloc.h:189
FbxAutoDestroyPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:289
void FbxDelete(T *p)
Deletion policy for pointer template classes that uses the FbxDelete() function.
Definition: fbxnew.h:341
Type * Get() const
Retrieve the pointer it holds.
Definition: fbxalloc.h:216
FbxSharedDeletePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:420
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:146
int DecRef()
Definition: fbxalloc.h:304
void *(* FbxReallocProc)(void *, size_t)
Function pointer signature used to replace "calloc".
Definition: fbxalloc.h:47
FbxCallocProc FbxGetCallocHandler()
Get the global zero'd memory allocation function used internally by the FBX SDK.
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:131
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:178
void *(* FbxCallocProc)(size_t, size_t)
Function pointer signature used to replace "malloc".
Definition: fbxalloc.h:46
bool operator!() const
Logical not operator.
Definition: fbxalloc.h:228
Deletion policy for pointer template classes that uses the delete[] operator.
Definition: fbxalloc.h:142
Scoped pointer for FbxNew allocations, which call FbxDelete() to deallocate.
Definition: fbxalloc.h:277
void FbxSetMallocHandler(FbxMallocProc pHandler)
Function pointer signature used to replace "free".
~RefCount()
Definition: fbxalloc.h:300
FbxFreeProc FbxGetFreeHandler()
Get the global memory freeing function used internally by the FBX SDK.
Type & operator*() const
Dereference operator.
Definition: fbxalloc.h:225
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:269
void FbxSetCallocHandler(FbxCallocProc pHandler)
Set the global zero'd memory allocation function used internally by the FBX SDK.
Scoped pointer for FbxObject derived classes, which call Destroy() to deallocate. ...
Definition: fbxalloc.h:285
Type & operator*() const
Dereference operator.
Definition: fbxalloc.h:374
bool operator!() const
Logical not operator.
Definition: fbxalloc.h:377
~FbxAutoPtr()
Destructor.
Definition: fbxalloc.h:213
RefCount()
Definition: fbxalloc.h:299
Deletion policy for pointer template classes that uses the delete operator.
Definition: fbxalloc.h:127
#define FBXSDK_DLL
Definition: fbxarch.h:176
void IncRef()
Definition: fbxalloc.h:303
void Init()
Definition: fbxalloc.h:302
Scoped pointer for FbxNew allocations, which call FbxDelete() to deallocate.
Definition: fbxalloc.h:416
FbxAutoFreePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:273
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:408
Type * operator->() const
Member access operator.
Definition: fbxalloc.h:219
FbxAutoPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:210
FbxSharedPtr & operator=(const FbxSharedPtr &pSPtr)
Definition: fbxalloc.h:342
Scoped pointer for FbxObject derived classes, which call Destroy() to deallocate. ...
Definition: fbxalloc.h:424
void Swap(FbxAutoPtr &pOther)
Swap with another pointer.
Definition: fbxalloc.h:241
FbxSharedFreePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:412
static void DeleteIt(Type **mPtr)
Destruction policy implementation.
Definition: fbxalloc.h:163
Type * Release()
Release the pointer, so that it won't perform deletion in its destruction.
Definition: fbxalloc.h:249