1 // Common/MyBuffer2.h
2 
3 #ifndef __COMMON_MY_BUFFER2_H
4 #define __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 
CLASS_NO_COPY(CMidBuffer)15   CLASS_NO_COPY(CMidBuffer)
16 
17 public:
18   CMidBuffer(): _data(NULL), _size(0) {}
~CMidBuffer()19   ~CMidBuffer() { ::MidFree(_data); }
20 
Free()21   void Free() { ::MidFree(_data); _data = NULL; _size = 0; }
22 
IsAllocated()23   bool IsAllocated() const { return _data != NULL; }
24   operator       Byte *()       { return _data; }
25   operator const Byte *() const { return _data; }
Size()26   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 CAlignedBuffer
60 {
61   Byte *_data;
62   size_t _size;
63 
CLASS_NO_COPY(CAlignedBuffer)64   CLASS_NO_COPY(CAlignedBuffer)
65 
66 public:
67   CAlignedBuffer(): _data(NULL), _size(0) {}
~CAlignedBuffer()68   ~CAlignedBuffer()
69   {
70     ISzAlloc_Free(&g_AlignedAlloc, _data);
71   }
72 
CAlignedBuffer(size_t size)73   CAlignedBuffer(size_t size): _size(0)
74   {
75     _data = NULL;
76     _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
77     if (!_data)
78       throw 1;
79     _size = size;
80   }
81 
Free()82   void Free()
83   {
84     ISzAlloc_Free(&g_AlignedAlloc, _data);
85     _data = NULL;
86     _size = 0;
87   }
88 
IsAllocated()89   bool IsAllocated() const { return _data != NULL; }
90   operator       Byte *()       { return _data; }
91   operator const Byte *() const { return _data; }
Size()92   size_t Size() const { return _size; }
93 
Alloc(size_t size)94   void Alloc(size_t size)
95   {
96     if (!_data || size != _size)
97     {
98       ISzAlloc_Free(&g_AlignedAlloc, _data);
99       _size = 0;
100       _data = NULL;
101       _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
102       if (_data)
103         _size = size;
104     }
105   }
106 
AllocAtLeast(size_t size)107   void AllocAtLeast(size_t size)
108   {
109     if (!_data || size > _size)
110     {
111       ISzAlloc_Free(&g_AlignedAlloc, _data);
112       _size = 0;
113       _data = NULL;
114       _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
115       if (_data)
116         _size = size;
117     }
118   }
119 };
120 
121 /*
122   CMidAlignedBuffer must return aligned pointer.
123    - in Windows it uses CMidBuffer(): MidAlloc() : VirtualAlloc()
124        VirtualAlloc(): Memory allocated is automatically initialized to zero.
125        MidAlloc(0) returns NULL
126    - in non-Windows systems it uses g_AlignedAlloc.
127      g_AlignedAlloc::Alloc(size = 0) can return non NULL.
128 */
129 
130 typedef
131 #ifdef _WIN32
132   CMidBuffer
133 #else
134   CAlignedBuffer
135 #endif
136   CMidAlignedBuffer;
137 
138 
139 #endif
140