1 // Common/MyBuffer2.h 2 3 #ifndef ZIP7_INC_COMMON_MY_BUFFER2_H 4 #define ZIP7_INC_COMMON_MY_BUFFER2_H 5 6 #include "../../C/Alloc.h" 7 8 #include "MyTypes.h" 9 10 class CMidBuffer 11 { 12 Byte *_data; 13 size_t _size; 14 15 Z7_CLASS_NO_COPY(CMidBuffer) 16 17 public: CMidBuffer()18 CMidBuffer(): _data(NULL), _size(0) {} ~CMidBuffer()19 ~CMidBuffer() { ::MidFree(_data); } 20 Free()21 void Free() { ::MidFree(_data); _data = NULL; _size = 0; } 22 IsAllocated() const23 bool IsAllocated() const { return _data != NULL; } operator Byte *()24 operator Byte *() { return _data; } operator const Byte *() const25 operator const Byte *() const { return _data; } Size() const26 size_t Size() const { return _size; } 27 Alloc(size_t size)28 void Alloc(size_t size) 29 { 30 if (!_data || size != _size) 31 { 32 ::MidFree(_data); 33 _size = 0; 34 _data = NULL; 35 _data = (Byte *)::MidAlloc(size); 36 if (_data) 37 _size = size; 38 } 39 } 40 AllocAtLeast(size_t size)41 void AllocAtLeast(size_t size) 42 { 43 if (!_data || size > _size) 44 { 45 ::MidFree(_data); 46 const size_t kMinSize = (size_t)1 << 16; 47 if (size < kMinSize) 48 size = kMinSize; 49 _size = 0; 50 _data = NULL; 51 _data = (Byte *)::MidAlloc(size); 52 if (_data) 53 _size = size; 54 } 55 } 56 }; 57 58 59 class CAlignedBuffer1 60 { 61 Byte *_data; 62 63 Z7_CLASS_NO_COPY(CAlignedBuffer1) 64 65 public: ~CAlignedBuffer1()66 ~CAlignedBuffer1() 67 { 68 ISzAlloc_Free(&g_AlignedAlloc, _data); 69 } 70 CAlignedBuffer1(size_t size)71 CAlignedBuffer1(size_t size) 72 { 73 _data = NULL; 74 _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size); 75 if (!_data) 76 throw 1; 77 } 78 operator Byte *()79 operator Byte *() { return _data; } operator const Byte *() const80 operator const Byte *() const { return _data; } 81 }; 82 83 84 class CAlignedBuffer 85 { 86 Byte *_data; 87 size_t _size; 88 89 Z7_CLASS_NO_COPY(CAlignedBuffer) 90 91 public: CAlignedBuffer()92 CAlignedBuffer(): _data(NULL), _size(0) {} ~CAlignedBuffer()93 ~CAlignedBuffer() 94 { 95 ISzAlloc_Free(&g_AlignedAlloc, _data); 96 } 97 CAlignedBuffer(size_t size)98 CAlignedBuffer(size_t size): _size(0) 99 { 100 _data = NULL; 101 _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size); 102 if (!_data) 103 throw 1; 104 _size = size; 105 } 106 Free()107 void Free() 108 { 109 ISzAlloc_Free(&g_AlignedAlloc, _data); 110 _data = NULL; 111 _size = 0; 112 } 113 IsAllocated() const114 bool IsAllocated() const { return _data != NULL; } operator Byte *()115 operator Byte *() { return _data; } operator const Byte *() const116 operator const Byte *() const { return _data; } Size() const117 size_t Size() const { return _size; } 118 Alloc(size_t size)119 void Alloc(size_t size) 120 { 121 if (!_data || size != _size) 122 { 123 ISzAlloc_Free(&g_AlignedAlloc, _data); 124 _size = 0; 125 _data = NULL; 126 _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size); 127 if (_data) 128 _size = size; 129 } 130 } 131 AllocAtLeast(size_t size)132 void AllocAtLeast(size_t size) 133 { 134 if (!_data || size > _size) 135 { 136 ISzAlloc_Free(&g_AlignedAlloc, _data); 137 _size = 0; 138 _data = NULL; 139 _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size); 140 if (_data) 141 _size = size; 142 } 143 } 144 }; 145 146 /* 147 CMidAlignedBuffer must return aligned pointer. 148 - in Windows it uses CMidBuffer(): MidAlloc() : VirtualAlloc() 149 VirtualAlloc(): Memory allocated is automatically initialized to zero. 150 MidAlloc(0) returns NULL 151 - in non-Windows systems it uses g_AlignedAlloc. 152 g_AlignedAlloc::Alloc(size = 0) can return non NULL. 153 */ 154 155 typedef 156 #ifdef _WIN32 157 CMidBuffer 158 #else 159 CAlignedBuffer 160 #endif 161 CMidAlignedBuffer; 162 163 164 #endif 165