xref: /aosp_15_r20/external/lzma/CPP/Common/MyBuffer2.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
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 
Z7_CLASS_NO_COPY(CMidBuffer)15   Z7_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 CAlignedBuffer1
60 {
61   Byte *_data;
62 
Z7_CLASS_NO_COPY(CAlignedBuffer1)63   Z7_CLASS_NO_COPY(CAlignedBuffer1)
64 
65 public:
66   ~CAlignedBuffer1()
67   {
68     z7_AlignedFree(_data);
69   }
70 
CAlignedBuffer1(size_t size)71   CAlignedBuffer1(size_t size)
72   {
73     _data = NULL;
74     _data = (Byte *)z7_AlignedAlloc(size);
75     if (!_data)
76       throw 1;
77   }
78 
79   operator       Byte *()       { return _data; }
80   operator const Byte *() const { return _data; }
81 };
82 
83 
84 class CAlignedBuffer
85 {
86   Byte *_data;
87   size_t _size;
88 
Z7_CLASS_NO_COPY(CAlignedBuffer)89   Z7_CLASS_NO_COPY(CAlignedBuffer)
90 
91 public:
92   CAlignedBuffer(): _data(NULL), _size(0) {}
~CAlignedBuffer()93   ~CAlignedBuffer()
94   {
95     z7_AlignedFree(_data);
96   }
97 
98   /*
99   CAlignedBuffer(size_t size): _size(0)
100   {
101     _data = NULL;
102     _data = (Byte *)z7_AlignedAlloc(size);
103     if (!_data)
104       throw 1;
105     _size = size;
106   }
107   */
108 
Free()109   void Free()
110   {
111     z7_AlignedFree(_data);
112     _data = NULL;
113     _size = 0;
114   }
115 
IsAllocated()116   bool IsAllocated() const { return _data != NULL; }
117   operator       Byte *()       { return _data; }
118   operator const Byte *() const { return _data; }
Size()119   size_t Size() const { return _size; }
120 
Alloc(size_t size)121   void Alloc(size_t size)
122   {
123     if (!_data || size != _size)
124     {
125       z7_AlignedFree(_data);
126       _size = 0;
127       _data = NULL;
128       _data = (Byte *)z7_AlignedAlloc(size);
129       if (_data)
130         _size = size;
131     }
132   }
133 
AllocAtLeast(size_t size)134   void AllocAtLeast(size_t size)
135   {
136     if (!_data || size > _size)
137     {
138       z7_AlignedFree(_data);
139       _size = 0;
140       _data = NULL;
141       _data = (Byte *)z7_AlignedAlloc(size);
142       if (_data)
143         _size = size;
144     }
145   }
146 
147   // (size <= size_max)
AllocAtLeast_max(size_t size,size_t size_max)148   void AllocAtLeast_max(size_t size, size_t size_max)
149   {
150     if (!_data || size > _size)
151     {
152       z7_AlignedFree(_data);
153       _size = 0;
154       _data = NULL;
155       if (size_max < size) size_max = size; // optional check
156       const size_t delta = size / 2;
157       size += delta;
158       if (size < delta || size > size_max)
159         size = size_max;
160       _data = (Byte *)z7_AlignedAlloc(size);
161       if (_data)
162         _size = size;
163     }
164   }
165 };
166 
167 /*
168   CMidAlignedBuffer must return aligned pointer.
169    - in Windows it uses CMidBuffer(): MidAlloc() : VirtualAlloc()
170        VirtualAlloc(): Memory allocated is automatically initialized to zero.
171        MidAlloc(0) returns NULL
172    - in non-Windows systems it uses g_AlignedAlloc.
173      g_AlignedAlloc::Alloc(size = 0) can return non NULL.
174 */
175 
176 typedef
177 #ifdef _WIN32
178   CMidBuffer
179 #else
180   CAlignedBuffer
181 #endif
182   CMidAlignedBuffer;
183 
184 
185 #endif
186