Memory.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /******************************************************************************
  2. Use 'MemStats' to access information about system memory usage.
  3. Use 'Cipher' to handle data encryption.
  4. Use memory functions to handle:
  5. -Allocate
  6. -Free
  7. -Reallocate
  8. -Zero
  9. -Set
  10. -Copy
  11. -Swap
  12. -Compare
  13. -New
  14. -Delete
  15. /******************************************************************************/
  16. // MEMORY STATUS
  17. /******************************************************************************/
  18. struct MemStats // System Memory Status
  19. {
  20. Byte usage ; // percentage memory usage, 0..100, 0=no memory usage, 100=full memory usage
  21. Long avail_phys, total_phys, // available and total physical memory (in bytes)
  22. avail_page, total_page, // available and total paged memory (in bytes)
  23. avail_virt, total_virt; // available and total virtual memory (in bytes)
  24. Bool get(); // get values from system information, false on fail
  25. };
  26. /******************************************************************************/
  27. // ALLOCATE RAW MEMORY
  28. /******************************************************************************/
  29. Ptr Alloc ( IntPtr size ); // allocate memory , Exit on fail, !! combine only with 'Free' !!, sample usage: " Alloc (100);" will allocate raw memory of 100 bytes
  30. Ptr AllocZero( IntPtr size ); // allocate and zero memory , Exit on fail, !! combine only with 'Free' !!, sample usage: " AllocZero (100);" will allocate raw memory of 100 zeroed bytes
  31. T1(TYPE) TYPE* Alloc ( Int elms=1) {return (TYPE*)Alloc (elms*SIZE(TYPE) );} // allocate 'elms' elements of 'type', Exit on fail, !! combine only with 'Free' !!, sample usage: " Alloc <Int> (100);" will allocate raw memory of 100 Int elements
  32. T1(TYPE) TYPE* AllocZero( Int elms=1) {return (TYPE*)AllocZero (elms*SIZE(TYPE) );} // allocate and zero 'elms' elements of 'type', Exit on fail, !! combine only with 'Free' !!, sample usage: " AllocZero<Int> (100);" will allocate raw memory of 100 zeroed Int elements
  33. T1(TYPE) TYPE*& Alloc (TYPE* &data, Int elms=1) { data=Alloc <TYPE>(elms); return data;} // allocate memory for 'elms' TYPE's , Exit on fail, !! combine only with 'Free' !!, sample usage: "Int *data; Alloc (data, 100);" will allocate raw memory of 100 Int elements
  34. T1(TYPE) TYPE*& AllocZero(TYPE* &data, Int elms=1) { data=AllocZero<TYPE>(elms); return data;} // allocate and zero memory for 'elms' TYPE's , Exit on fail, !! combine only with 'Free' !!, sample usage: "Int *data; AllocZero(data, 100);" will allocate raw memory of 100 zeroed Int elements
  35. /******************************************************************************/
  36. // FREE RAW MEMORY
  37. /******************************************************************************/
  38. void Free(Ptr &data); // free memory and set pointer to null !! combine only with 'Alloc' !!
  39. T1(TYPE) TYPE*& Free(TYPE* &data) {Free(*(Ptr*)&data); return data;} // free memory and set pointer to null !! combine only with 'Alloc' !!
  40. /******************************************************************************/
  41. // REALLOCATE MEMORY
  42. /******************************************************************************/
  43. T1(TYPE) void Realloc (TYPE* &data, Int elms_new, Int elms_old); // reallocate memory without losing data with 'elms_new' and 'elms_old' , Exit on fail !! combine only with 'Alloc' !!
  44. T1(TYPE) void ReallocZero(TYPE* &data, Int elms_new, Int elms_old); // reallocate memory without losing data with 'elms_new' and 'elms_old', and zero new elements, Exit on fail !! combine only with 'Alloc' !!
  45. /******************************************************************************/
  46. // ZERO MEMORY
  47. /******************************************************************************/
  48. void Zero (Ptr data, IntPtr size); // zero memory
  49. T1(TYPE) TYPE& Zero (TYPE &elm ) {Zero(&elm , SIZE(TYPE)); return elm;} // zero element memory
  50. T1(TYPE) void ZeroN(TYPE *data, Int elms) {Zero( data, elms*SIZE(TYPE)); } // zero 'elms' elements memory
  51. /******************************************************************************/
  52. // SET MEMORY
  53. /******************************************************************************/
  54. void SetMem (Ptr data, Byte value, IntPtr size); // set memory to Byte value
  55. T1(TYPE) TYPE& SetMem (TYPE &elm , Byte value ) {SetMem(&elm , value, SIZE(TYPE)); return elm;} // set 'elm' memory to Byte value
  56. T1(TYPE) void SetMemN(TYPE *data, Byte value, Int elms) {SetMem( data, value, elms*SIZE(TYPE)); } // set 'elms' elements memory to Byte value
  57. /******************************************************************************/
  58. // COPY MEMORY
  59. /******************************************************************************/
  60. #if EE_PRIVATE
  61. inline void MoveFast (Ptr dest, CPtr src, IntPtr size) {memmove( dest, src, size );} // !! this does not check for pointers!=null, size>=0 , this is more safe than 'CopyFast'
  62. T1(TYPE) void MoveFastN(TYPE *dest, C TYPE *src, Int elms) {memmove( dest, src, elms*SIZE(TYPE));} // !! this does not check for pointers!=null, elms>=0 , this is more safe than 'CopyFast'
  63. inline void CopyFast (Ptr dest, CPtr src, IntPtr size) {memcpy ( dest, src, size );} // !! this does not check for pointers!=null, size>=0 and if memory overlaps, this is less safe than 'MoveFast'
  64. T1(TYPE) void CopyFast (TYPE &dest, C TYPE &src ) {memcpy (&dest, &src, SIZE(TYPE));} // !! this does not check for pointers!=null and if memory overlaps, this is less safe than 'MoveFast'
  65. T1(TYPE) void CopyFastN(TYPE *dest, C TYPE *src, Int elms) {memcpy ( dest, src, elms*SIZE(TYPE));} // !! this does not check for pointers!=null, elms>=0 and if memory overlaps, this is less safe than 'MoveFast'
  66. void Copy (Ptr dest, CPtr src, IntPtr dest_size, IntPtr src_size); // copy 'src' memory to 'dest', if 'src' is null then 'dest' will be set to zero, if 'src_size' is smaller than 'dest_size' then remaining bytes will be zeroed
  67. #endif
  68. void Copy (Ptr dest, CPtr src, IntPtr size); // copy 'src' memory to 'dest', if 'src' is null then 'dest' will be set to zero
  69. T1(TYPE) void Copy (TYPE &dest, C TYPE &src ) {Copy(&dest, &src, SIZE(TYPE));} // copy 'src' element to 'dest'
  70. T1(TYPE) void CopyN(TYPE *dest, C TYPE *src, Int elms) {Copy( dest, src, elms*SIZE(TYPE));} // copy 'elms' elements
  71. /******************************************************************************/
  72. // SWAP MEMORY
  73. /******************************************************************************/
  74. void SwapFast(Ptr a, Ptr b, IntPtr size); // swap memories (this function is slightly faster, because it doesn't check for pointers being not null and size>0, however it will crash if you specify null pointers or size<0)
  75. void Swap (Ptr a, Ptr b, IntPtr size); // swap memories
  76. inline void Swap (Char &a, Char &b ) {Char temp=a; a=b; b=temp;} // swap elements
  77. inline void Swap (Char8 &a, Char8 &b ) {Char8 temp=a; a=b; b=temp;} // swap elements
  78. inline void Swap (SByte &a, SByte &b ) {SByte temp=a; a=b; b=temp;} // swap elements
  79. inline void Swap (Short &a, Short &b ) {Short temp=a; a=b; b=temp;} // swap elements
  80. inline void Swap (Int &a, Int &b ) {Int temp=a; a=b; b=temp;} // swap elements
  81. inline void Swap (Long &a, Long &b ) {Long temp=a; a=b; b=temp;} // swap elements
  82. inline void Swap (Byte &a, Byte &b ) {Byte temp=a; a=b; b=temp;} // swap elements
  83. inline void Swap (UShort &a, UShort &b ) {UShort temp=a; a=b; b=temp;} // swap elements
  84. inline void Swap (UInt &a, UInt &b ) {UInt temp=a; a=b; b=temp;} // swap elements
  85. inline void Swap (ULong &a, ULong &b ) {ULong temp=a; a=b; b=temp;} // swap elements
  86. inline void Swap (Flt &a, Flt &b ) {Flt temp=a; a=b; b=temp;} // swap elements
  87. inline void Swap (Dbl &a, Dbl &b ) {Dbl temp=a; a=b; b=temp;} // swap elements
  88. inline void Swap (Ptr &a, Ptr &b ) {Ptr temp=a; a=b; b=temp;} // swap elements
  89. T1(TYPE) void Swap (TYPE* &a, TYPE* &b ) {TYPE *temp=a; a=b; b=temp;} // swap elements
  90. T1(TYPE) void Swap (TYPE &a, TYPE &b ) {SwapFast(&a, &b, SIZE(TYPE));} // swap elements
  91. #if EE_PRIVATE
  92. inline void SwapEndian( Short &i) {Byte *b=(Byte*)&i; Swap(b[0], b[1]);}
  93. inline void SwapEndian(UShort &i) {Byte *b=(Byte*)&i; Swap(b[0], b[1]);}
  94. inline void SwapEndian( Int &i) {Byte *b=(Byte*)&i; Swap(b[0], b[3]); Swap(b[1], b[2]);}
  95. inline void SwapEndian(UInt &i) {Byte *b=(Byte*)&i; Swap(b[0], b[3]); Swap(b[1], b[2]);}
  96. inline void SwapEndian( Long &i) {Byte *b=(Byte*)&i; Swap(b[0], b[7]); Swap(b[1], b[6]); Swap(b[2], b[5]); Swap(b[3], b[4]);}
  97. inline void SwapEndian(ULong &i) {Byte *b=(Byte*)&i; Swap(b[0], b[7]); Swap(b[1], b[6]); Swap(b[2], b[5]); Swap(b[3], b[4]);}
  98. #endif
  99. /******************************************************************************/
  100. // CHANGE ORDER OF ELEMENTS IN MEMORY
  101. /******************************************************************************/
  102. T1(TYPE) void ReverseOrder(TYPE *data, Int elms ); // reverse order of elements (first<->last)
  103. T1(TYPE) void RotateOrder(TYPE *data, Int elms, Int offset ); // rotate order of elements, changes the order of elements so "new_index=old_index+offset", 'offset'=offset of moving the original indexes into target indexes (-Inf..Inf)
  104. T1(TYPE) void RandomizeOrder(TYPE *data, Int elms, Randomizer &random=Random); // randomize order of elements
  105. /******************************************************************************/
  106. // COMPARE MEMORY
  107. /******************************************************************************/
  108. Bool EqualMem( CPtr a, CPtr b, IntPtr size); // if equal memories
  109. T1(TYPE) Bool EqualMem(C TYPE &a, C TYPE &b ) {return EqualMem((CPtr)&a, (CPtr)&b, SIZE(TYPE));} // if equal elements (in memory)
  110. /******************************************************************************/
  111. } // close EE namespace to define following functions in global namespace
  112. /******************************************************************************/
  113. // OVERRIDE NEW AND DELETE
  114. /******************************************************************************/
  115. Ptr operator new (size_t size) ; // override default new to use custom allocation with detection of memory leaks
  116. void operator delete(Ptr data)noexcept; // override default delete to use custom freeing with detection of memory leaks
  117. /******************************************************************************/
  118. namespace EE // restore EE namespace
  119. {
  120. /******************************************************************************/
  121. // ALLOCATE MEMORY AND CALL CONSTRUCTOR
  122. /******************************************************************************/
  123. T1(TYPE) TYPE*& New(TYPE* &data ) {data= new TYPE ; return data;} // create object !! combine only with 'Delete' or 'delete' !!
  124. T1(TYPE) TYPE*& New(TYPE* &data, Int elms) {data=((elms>0) ? new TYPE[elms] : null); return data;} // create 'elms' objects !! combine only with 'DeleteN' or 'delete[]' !!
  125. /******************************************************************************/
  126. // CALL DESTRUCTOR AND FREE MEMORY
  127. /******************************************************************************/
  128. T1(TYPE) TYPE*& Delete (TYPE* &data) {delete data; data=null; return data;} // delete object and set pointer to null !! combine only with "New(data )" or 'new' !!
  129. T1(TYPE) TYPE*& DeleteN(TYPE* &data) {delete[] data; data=null; return data;} // delete objects and set pointer to null !! combine only with "New(data, elms)" or 'new[]' !!
  130. /******************************************************************************/
  131. Str8 Base64(CPtr data, Int size); // calculate Base64 for given memory
  132. /******************************************************************************/
  133. #if EE_PRIVATE
  134. Long MemUsage();
  135. Long MemPeakUsage();
  136. void ClearMemPeakUsage();
  137. void ListMemLeaks();
  138. Ptr AlignedAlloc(IntPtr size);
  139. void AlignedFree (Ptr &ptr);
  140. void _CopyIs (Ptr dest, CPtr src, C MemPtr<Bool> &is , UInt elm_size); // copy only elements with "is[]=true"
  141. void _CopyList(Ptr dest, CPtr src, C MemPtr<Int > &list, UInt elm_size); // copy only elements mentioned on the list
  142. T1(TYPE) void CopyIs (TYPE *dest, C TYPE *src, C MemPtr<Bool> &is ) {_CopyIs (dest, (CPtr)src, is , SIZE(TYPE));} // copy only elements with "is[]=true"
  143. T1(TYPE) void CopyList(TYPE *dest, C TYPE *src, C MemPtr<Int > &list ) {_CopyList(dest, (CPtr)src, list, SIZE(TYPE));} // copy only elements mentioned on the list
  144. // copy different sized data
  145. void Copy8To16 (Ptr dest16, CPtr src8 , Int elms); // copy 8->16 bit
  146. void Copy8To32 (Ptr dest32, CPtr src8 , Int elms); // copy 8->32 bit
  147. void Copy16To8 (Ptr dest8 , CPtr src16, Int elms); // copy 16-> 8 bit
  148. void Copy16To32(Ptr dest32, CPtr src16, Int elms); // copy 16->32 bit
  149. void Copy24To32(Ptr dest32, CPtr src24, Int elms); // copy 24->32 bit
  150. void Copy32To8 (Ptr dest8 , CPtr src32, Int elms); // copy 32-> 8 bit
  151. void Copy32To16(Ptr dest16, CPtr src32, Int elms); // copy 32->16 bit
  152. void Copy32To24(Ptr dest24, CPtr src32, Int elms); // copy 32->24 bit
  153. inline void Copy32To32(Ptr dest32, CPtr src32, Int elms) {return Copy(dest32, src32, elms*4);}
  154. #endif
  155. /******************************************************************************/