fbxsdk/core/arch/fbxalloc.h Source File

fbxalloc.h
Go to the documentation of this file.
1 /****************************************************************************************
2 
3  Copyright (C) 2014 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 <class Type> class FbxDeletionPolicyDelete
142 {
143 public:
145  static inline void DeleteIt(Type** mPtr)
146  {
147  if( *mPtr )
148  {
149  FbxDelete(*mPtr);
150  *mPtr = NULL;
151  }
152  }
153 };
154 
156 template <class Type> class FbxDeletionPolicyFree
157 {
158 public:
160  static inline void DeleteIt(Type** pPtr)
161  {
162  if( *pPtr )
163  {
164  FbxFree(*pPtr);
165  *pPtr = NULL;
166  }
167  }
168 };
169 
171 template <class Type> class FbxDeletionPolicyObject
172 {
173 public:
175  static inline void DeleteIt(Type** pPtr)
176  {
177  if( *pPtr )
178  {
179  (*pPtr)->Destroy();
180  *pPtr = NULL;
181  }
182  }
183 };
184 
188 template<class Type, class Policy=FbxDeletionPolicyDefault<Type> > class FbxAutoPtr
189 {
190 public:
192  explicit FbxAutoPtr(Type* pPtr=0) : mPtr(pPtr){}
193 
195  ~FbxAutoPtr() { Policy::DeleteIt(&mPtr); }
196 
198  inline Type* Get() const { return mPtr; }
199 
201  inline Type* operator->() const { return mPtr; }
202 
204  inline operator Type* () const { return mPtr; }
205 
207  inline Type& operator*() const { return *mPtr; }
208 
210  inline bool operator!() const { return mPtr == 0; }
211 
213  inline operator bool () const { return mPtr != 0; }
214 
216  inline void Reset(Type* pPtr=0)
217  {
218  FBX_ASSERT(pPtr == 0 || pPtr != mPtr); //Catch self-reset errors
219  FbxAutoPtr<Type, Policy>(pPtr).Swap(*this);
220  }
221 
223  inline void Swap(FbxAutoPtr& pOther)
224  {
225  Type* TmpPtr = pOther.mPtr;
226  pOther.mPtr = mPtr;
227  mPtr = TmpPtr;
228  }
229 
231  inline Type* Release()
232  {
233  Type* TmpPtr = mPtr;
234  mPtr = NULL;
235  return TmpPtr;
236  }
237 
238 /*****************************************************************************************************************************
239 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
240 *****************************************************************************************************************************/
241 #ifndef DOXYGEN_SHOULD_SKIP_THIS
242 private:
243  FbxAutoPtr(const FbxAutoPtr&);
244  FbxAutoPtr& operator=(const FbxAutoPtr&);
245 
246  Type* mPtr;
247 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
248 };
249 
251 template <class Type> class FbxAutoFreePtr : public FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >
252 {
253 public:
255  explicit FbxAutoFreePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyFree<Type> >(pPtr){}
256 };
257 
259 template <class Type> class FbxAutoDeletePtr : public FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >
260 {
261 public:
263  explicit FbxAutoDeletePtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyDelete<Type> >(pPtr){}
264 };
265 
267 template <class Type> class FbxAutoDestroyPtr : public FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >
268 {
269 public:
271  explicit FbxAutoDestroyPtr(Type* pPtr=0) : FbxAutoPtr<Type, FbxDeletionPolicyObject<Type> >(pPtr){}
272 };
273 
274 
278 class RefCount
279 {
280 public:
281  RefCount() { Init(); };
282  ~RefCount() { Init(); };
283 
284  void Init() { count = 0; }
285  void IncRef() { count++; }
286  int DecRef() { count--; if (count < 0) count = 0; return count; }
287 
288 private:
289  int count;
290 };
291 
292 template<class Type, class Policy=FbxDeletionPolicyDefault<Type> > class FbxSharedPtr
293 {
294 public:
295  // Default constructor.
297  mPtr(0),
298  mRef(0)
299  {}
300 
302  explicit FbxSharedPtr(Type* pPtr) :
303  mPtr(pPtr),
304  mRef(0)
305  {
306  if (pPtr != 0)
307  {
308  mRef = (RefCount*)FbxMalloc(sizeof(RefCount));
309  mRef->Init();
310  mRef->IncRef();
311  }
312  }
313 
315  FbxSharedPtr(const FbxSharedPtr& pSPtr) :
316  mPtr(pSPtr.mPtr),
317  mRef(pSPtr.mRef)
318  {
319  if (pSPtr.mPtr != 0 && mRef != 0)
320  mRef->IncRef();
321  }
322 
323  // Assignment operator
325  {
326  if (this != &pSPtr) // avoid self assignment
327  {
328  Reset();
329 
330  if (pSPtr.mPtr)
331  {
332  mPtr = pSPtr.mPtr;
333  mRef = pSPtr.mRef;
334  FBX_ASSERT(mRef != NULL);
335  mRef->IncRef();
336  }
337  }
338  return *this;
339  }
340 
343 
344  void Destroy() { Reset(); }
345 
347  inline Type* Get() const { return mPtr; }
348 
350  inline Type* operator->() const { return mPtr; }
351 
353  inline operator Type* () const { return mPtr; }
354 
356  inline Type& operator*() const { return *mPtr; }
357 
359  inline bool operator!() const { return mPtr == 0; }
360 
362  inline operator bool () const { return mPtr != 0; }
363 
364 
365 /*****************************************************************************************************************************
366 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
367 *****************************************************************************************************************************/
368 #ifndef DOXYGEN_SHOULD_SKIP_THIS
369 private:
370  void Reset()
371  {
372  if (mRef)
373  {
374  FBX_ASSERT(mPtr != 0);
375  if (mRef->DecRef() == 0)
376  {
377  Policy::DeleteIt(&mPtr);
378  FbxFree(mRef);
379  mRef = NULL;
380  }
381  }
382  }
383 
384  Type* mPtr;
385  RefCount* mRef;
386 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
387 };
388 
390 template <class Type> class FbxSharedFreePtr : public FbxSharedPtr<Type, FbxDeletionPolicyFree<Type> >
391 {
392 public:
394  explicit FbxSharedFreePtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyFree<Type> >(pPtr){}
395 };
396 
398 template <class Type> class FbxSharedDeletePtr : public FbxSharedPtr<Type, FbxDeletionPolicyDelete<Type> >
399 {
400 public:
402  explicit FbxSharedDeletePtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyDelete<Type> >(pPtr){}
403 };
404 
406 template <class Type> class FbxSharedDestroyPtr : public FbxSharedPtr<Type, FbxDeletionPolicyObject<Type> >
407 {
408 public:
410  explicit FbxSharedDestroyPtr(Type* pPtr=0) : FbxSharedPtr<Type, FbxDeletionPolicyObject<Type> >(pPtr){}
411 };
412 
413 
414 
415 #include <fbxsdk/fbxsdk_nsend.h>
416 
417 #endif /* _FBXSDK_CORE_ARCH_ALLOC_H_ */
void Reset(Type *pPtr=0)
Reset the scoped pointer by swapping with another pointer.
Definition: fbxalloc.h:216
FbxAutoPtr mimics the auto_ptr class template implementation available in the C++ Standard Library...
Definition: fbxalloc.h:188
FbxSharedDestroyPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:410
FBX SDK environment definition.
void Destroy()
Definition: fbxalloc.h:344
FbxSharedPtr(Type *pPtr)
Construct from a pointer.
Definition: fbxalloc.h:302
Type * Get() const
Retrieve the pointer it holds.
Definition: fbxalloc.h:347
Deletion policy for pointer template classes that uses the FbxDelete() function.
Definition: fbxalloc.h:141
Deletion policy for pointer template classes that uses the FbxFree() function.
Definition: fbxalloc.h:156
FBXSDK_DLL FbxFreeProc FbxGetFreeHandler()
Get the global memory freeing 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:278
#define NULL
Definition: fbxarch.h:207
FbxSharedPtr(const FbxSharedPtr &pSPtr)
Copy constructor.
Definition: fbxalloc.h:315
FBXSDK_DLL FbxReallocProc FbxGetDefaultReallocHandler()
Get the default global memory re-allocation function used internally by the FBX SDK.
FBXSDK_DLL FbxMallocProc FbxGetMallocHandler()
Get the global memory allocation function used internally by the FBX SDK.
~FbxSharedPtr()
Destructor.
Definition: fbxalloc.h:342
Type * operator->() const
Member access operator.
Definition: fbxalloc.h:350
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:175
FbxAutoDeletePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:263
void(* FbxFreeProc)(void *)
Function pointer signature used to replace "realloc".
Definition: fbxalloc.h:48
Deletion policy for pointer template classes that uses the Destroy() function.
Definition: fbxalloc.h:171
FbxAutoDestroyPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:271
Type * Get() const
Retrieve the pointer it holds.
Definition: fbxalloc.h:198
FbxSharedDeletePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:402
int DecRef()
Definition: fbxalloc.h:286
FBXSDK_DLL FbxReallocProc FbxGetReallocHandler()
Get the global memory re-allocation function used internally by the FBX SDK.
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:130
FBXSDK_DLL FbxCallocProc FbxGetDefaultCallocHandler()
Get the default global zero'd memory allocation function used internally by the FBX SDK...
static void DeleteIt(Type **pPtr)
Destruction policy implementation.
Definition: fbxalloc.h:160
FBXSDK_DLL void FbxSetReallocHandler(FbxReallocProc pHandler)
Set the global memory re-allocation function used internally by the FBX SDK.
FBXSDK_DLL FbxFreeProc FbxGetDefaultFreeHandler()
Get the default global memory freeing function used internally by the FBX SDK.
bool operator!() const
Logical not operator.
Definition: fbxalloc.h:210
FBXSDK_DLL FbxMallocProc FbxGetDefaultMallocHandler()
Get the default global memory allocation function used internally by the FBX SDK. ...
void *(* FbxReallocProc)(void *, size_t)
Function pointer signature used to replace "calloc".
Definition: fbxalloc.h:47
Scoped pointer for FbxNew allocations, which call FbxDelete() to deallocate.
Definition: fbxalloc.h:259
FBXSDK_DLL void FbxSetMallocHandler(FbxMallocProc pHandler)
Function pointer signature used to replace "free".
~RefCount()
Definition: fbxalloc.h:282
void *(* FbxCallocProc)(size_t, size_t)
Function pointer signature used to replace "malloc".
Definition: fbxalloc.h:46
Type & operator*() const
Dereference operator.
Definition: fbxalloc.h:207
Scoped pointer for FbxMalloc allocations, which call FbxFree() to deallocate.
Definition: fbxalloc.h:251
Scoped pointer for FbxObject derived classes, which call Destroy() to deallocate. ...
Definition: fbxalloc.h:267
Type & operator*() const
Dereference operator.
Definition: fbxalloc.h:356
bool operator!() const
Logical not operator.
Definition: fbxalloc.h:359
~FbxAutoPtr()
Destructor.
Definition: fbxalloc.h:195
RefCount()
Definition: fbxalloc.h:281
Deletion policy for pointer template classes that uses the delete operator.
Definition: fbxalloc.h:126
#define FBXSDK_DLL
Definition: fbxarch.h:170
void IncRef()
Definition: fbxalloc.h:285
void Init()
Definition: fbxalloc.h:284
Scoped pointer for FbxNew allocations, which call FbxDelete() to deallocate.
Definition: fbxalloc.h:398
void FbxDelete(T *p)
Definition: fbxnew.h:341
FbxAutoFreePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:255
void *(* FbxMallocProc)(size_t)
Definition: fbxalloc.h:45
FBXSDK_DLL FbxCallocProc FbxGetCallocHandler()
Get the global zero'd memory allocation function used internally by the FBX SDK.
Scoped pointer for FbxMalloc allocations, which call FbxFree() to deallocate.
Definition: fbxalloc.h:390
Type * operator->() const
Member access operator.
Definition: fbxalloc.h:201
FbxAutoPtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:192
FBXSDK_DLL void FbxSetCallocHandler(FbxCallocProc pHandler)
Set the global zero'd memory allocation function used internally by the FBX SDK.
FbxSharedPtr & operator=(const FbxSharedPtr &pSPtr)
Definition: fbxalloc.h:324
Scoped pointer for FbxObject derived classes, which call Destroy() to deallocate. ...
Definition: fbxalloc.h:406
void Swap(FbxAutoPtr &pOther)
Swap with another pointer.
Definition: fbxalloc.h:223
FBXSDK_DLL void FbxSetFreeHandler(FbxFreeProc pHandler)
Set the global memory freeing function used internally by the FBX SDK.
FbxSharedFreePtr(Type *pPtr=0)
Construct from a pointer.
Definition: fbxalloc.h:394
static void DeleteIt(Type **mPtr)
Destruction policy implementation.
Definition: fbxalloc.h:145
Type * Release()
Release the pointer, so that it won't perform deletion in its destruction.
Definition: fbxalloc.h:231