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