IceMemoryMacros.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains all memory macros.
  4. * \file IceMemoryMacros.h
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICEMEMORYMACROS_H__
  12. #define __ICEMEMORYMACROS_H__
  13. #undef ZeroMemory
  14. #undef CopyMemory
  15. #undef MoveMemory
  16. #undef FillMemory
  17. //! Clears a buffer.
  18. //! \param addr [in] buffer address
  19. //! \param size [in] buffer length
  20. //! \see FillMemory
  21. //! \see StoreDwords
  22. //! \see CopyMemory
  23. //! \see MoveMemory
  24. inline_ void ZeroMemory(void* addr, udword size) { memset(addr, 0, size); }
  25. //! Fills a buffer with a given byte.
  26. //! \param addr [in] buffer address
  27. //! \param size [in] buffer length
  28. //! \param val [in] the byte value
  29. //! \see StoreDwords
  30. //! \see ZeroMemory
  31. //! \see CopyMemory
  32. //! \see MoveMemory
  33. inline_ void FillMemory(void* dest, udword size, ubyte val) { memset(dest, val, size); }
  34. //! Fills a buffer with a given dword.
  35. //! \param addr [in] buffer address
  36. //! \param nb [in] number of dwords to write
  37. //! \param value [in] the dword value
  38. //! \see FillMemory
  39. //! \see ZeroMemory
  40. //! \see CopyMemory
  41. //! \see MoveMemory
  42. //! \warning writes nb*4 bytes !
  43. inline_ void StoreDwords(udword* dest, udword nb, udword value)
  44. {
  45. #if defined( _WIN32 ) && !defined( _WIN64 )
  46. // The asm code below **SHOULD** be equivalent to one of those C versions
  47. // or the other if your compiled is good: (checked on VC++ 6.0)
  48. //
  49. // 1) while(nb--) *dest++ = value;
  50. //
  51. // 2) for(udword i=0;i<nb;i++) dest[i] = value;
  52. //
  53. _asm push eax
  54. _asm push ecx
  55. _asm push edi
  56. _asm mov edi, dest
  57. _asm mov ecx, nb
  58. _asm mov eax, value
  59. _asm rep stosd
  60. _asm pop edi
  61. _asm pop ecx
  62. _asm pop eax
  63. #else
  64. while(nb--)
  65. *dest++ = value;
  66. #endif
  67. }
  68. //! Copies a buffer.
  69. //! \param addr [in] destination buffer address
  70. //! \param addr [in] source buffer address
  71. //! \param size [in] buffer length
  72. //! \see ZeroMemory
  73. //! \see FillMemory
  74. //! \see StoreDwords
  75. //! \see MoveMemory
  76. inline_ void CopyMemory(void* dest, const void* src, udword size) { memcpy(dest, src, size); }
  77. //! Moves a buffer.
  78. //! \param addr [in] destination buffer address
  79. //! \param addr [in] source buffer address
  80. //! \param size [in] buffer length
  81. //! \see ZeroMemory
  82. //! \see FillMemory
  83. //! \see StoreDwords
  84. //! \see CopyMemory
  85. inline_ void MoveMemory(void* dest, const void* src, udword size) { memmove(dest, src, size); }
  86. #define SIZEOFOBJECT sizeof(*this) //!< Gives the size of current object. Avoid some mistakes (e.g. "sizeof(this)").
  87. //#define CLEAROBJECT { memset(this, 0, SIZEOFOBJECT); } //!< Clears current object. Laziness is my business. HANDLE WITH CARE.
  88. #define DELETESINGLE(x) if (x) { delete x; x = null; } //!< Deletes an instance of a class.
  89. #define DELETEARRAY(x) if (x) { delete []x; x = null; } //!< Deletes an array.
  90. #define SAFE_RELEASE(x) if (x) { (x)->Release(); (x) = null; } //!< Safe D3D-style release
  91. #define SAFE_DESTRUCT(x) if (x) { (x)->SelfDestruct(); (x) = null; } //!< Safe ICE-style release
  92. #ifdef __ICEERROR_H__
  93. #define CHECKALLOC(x) if(!x) return SetIceError("Out of memory.", EC_OUT_OF_MEMORY); //!< Standard alloc checking. HANDLE WITH CARE.
  94. #else
  95. #define CHECKALLOC(x) if(!x) return false;
  96. #endif
  97. //! Standard allocation cycle
  98. #define SAFE_ALLOC(ptr, type, count) DELETEARRAY(ptr); ptr = new type[count]; CHECKALLOC(ptr);
  99. #endif // __ICEMEMORYMACROS_H__