1370b324cSopenharmony_ci// Common/MyBuffer2.h
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#ifndef ZIP7_INC_COMMON_MY_BUFFER2_H
4370b324cSopenharmony_ci#define ZIP7_INC_COMMON_MY_BUFFER2_H
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include "../../C/Alloc.h"
7370b324cSopenharmony_ci
8370b324cSopenharmony_ci#include "MyTypes.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ciclass CMidBuffer
11370b324cSopenharmony_ci{
12370b324cSopenharmony_ci  Byte *_data;
13370b324cSopenharmony_ci  size_t _size;
14370b324cSopenharmony_ci
15370b324cSopenharmony_ci  Z7_CLASS_NO_COPY(CMidBuffer)
16370b324cSopenharmony_ci
17370b324cSopenharmony_cipublic:
18370b324cSopenharmony_ci  CMidBuffer(): _data(NULL), _size(0) {}
19370b324cSopenharmony_ci  ~CMidBuffer() { ::MidFree(_data); }
20370b324cSopenharmony_ci
21370b324cSopenharmony_ci  void Free() { ::MidFree(_data); _data = NULL; _size = 0; }
22370b324cSopenharmony_ci
23370b324cSopenharmony_ci  bool IsAllocated() const { return _data != NULL; }
24370b324cSopenharmony_ci  operator       Byte *()       { return _data; }
25370b324cSopenharmony_ci  operator const Byte *() const { return _data; }
26370b324cSopenharmony_ci  size_t Size() const { return _size; }
27370b324cSopenharmony_ci
28370b324cSopenharmony_ci  void Alloc(size_t size)
29370b324cSopenharmony_ci  {
30370b324cSopenharmony_ci    if (!_data || size != _size)
31370b324cSopenharmony_ci    {
32370b324cSopenharmony_ci      ::MidFree(_data);
33370b324cSopenharmony_ci      _size = 0;
34370b324cSopenharmony_ci      _data = NULL;
35370b324cSopenharmony_ci      _data = (Byte *)::MidAlloc(size);
36370b324cSopenharmony_ci      if (_data)
37370b324cSopenharmony_ci        _size = size;
38370b324cSopenharmony_ci    }
39370b324cSopenharmony_ci  }
40370b324cSopenharmony_ci
41370b324cSopenharmony_ci  void AllocAtLeast(size_t size)
42370b324cSopenharmony_ci  {
43370b324cSopenharmony_ci    if (!_data || size > _size)
44370b324cSopenharmony_ci    {
45370b324cSopenharmony_ci      ::MidFree(_data);
46370b324cSopenharmony_ci      const size_t kMinSize = (size_t)1 << 16;
47370b324cSopenharmony_ci      if (size < kMinSize)
48370b324cSopenharmony_ci        size = kMinSize;
49370b324cSopenharmony_ci      _size = 0;
50370b324cSopenharmony_ci      _data = NULL;
51370b324cSopenharmony_ci      _data = (Byte *)::MidAlloc(size);
52370b324cSopenharmony_ci      if (_data)
53370b324cSopenharmony_ci        _size = size;
54370b324cSopenharmony_ci    }
55370b324cSopenharmony_ci  }
56370b324cSopenharmony_ci};
57370b324cSopenharmony_ci
58370b324cSopenharmony_ci
59370b324cSopenharmony_ciclass CAlignedBuffer1
60370b324cSopenharmony_ci{
61370b324cSopenharmony_ci  Byte *_data;
62370b324cSopenharmony_ci
63370b324cSopenharmony_ci  Z7_CLASS_NO_COPY(CAlignedBuffer1)
64370b324cSopenharmony_ci
65370b324cSopenharmony_cipublic:
66370b324cSopenharmony_ci  ~CAlignedBuffer1()
67370b324cSopenharmony_ci  {
68370b324cSopenharmony_ci    ISzAlloc_Free(&g_AlignedAlloc, _data);
69370b324cSopenharmony_ci  }
70370b324cSopenharmony_ci
71370b324cSopenharmony_ci  CAlignedBuffer1(size_t size)
72370b324cSopenharmony_ci  {
73370b324cSopenharmony_ci    _data = NULL;
74370b324cSopenharmony_ci    _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
75370b324cSopenharmony_ci    if (!_data)
76370b324cSopenharmony_ci      throw 1;
77370b324cSopenharmony_ci  }
78370b324cSopenharmony_ci
79370b324cSopenharmony_ci  operator       Byte *()       { return _data; }
80370b324cSopenharmony_ci  operator const Byte *() const { return _data; }
81370b324cSopenharmony_ci};
82370b324cSopenharmony_ci
83370b324cSopenharmony_ci
84370b324cSopenharmony_ciclass CAlignedBuffer
85370b324cSopenharmony_ci{
86370b324cSopenharmony_ci  Byte *_data;
87370b324cSopenharmony_ci  size_t _size;
88370b324cSopenharmony_ci
89370b324cSopenharmony_ci  Z7_CLASS_NO_COPY(CAlignedBuffer)
90370b324cSopenharmony_ci
91370b324cSopenharmony_cipublic:
92370b324cSopenharmony_ci  CAlignedBuffer(): _data(NULL), _size(0) {}
93370b324cSopenharmony_ci  ~CAlignedBuffer()
94370b324cSopenharmony_ci  {
95370b324cSopenharmony_ci    ISzAlloc_Free(&g_AlignedAlloc, _data);
96370b324cSopenharmony_ci  }
97370b324cSopenharmony_ci
98370b324cSopenharmony_ci  CAlignedBuffer(size_t size): _size(0)
99370b324cSopenharmony_ci  {
100370b324cSopenharmony_ci    _data = NULL;
101370b324cSopenharmony_ci    _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
102370b324cSopenharmony_ci    if (!_data)
103370b324cSopenharmony_ci      throw 1;
104370b324cSopenharmony_ci    _size = size;
105370b324cSopenharmony_ci  }
106370b324cSopenharmony_ci
107370b324cSopenharmony_ci  void Free()
108370b324cSopenharmony_ci  {
109370b324cSopenharmony_ci    ISzAlloc_Free(&g_AlignedAlloc, _data);
110370b324cSopenharmony_ci    _data = NULL;
111370b324cSopenharmony_ci    _size = 0;
112370b324cSopenharmony_ci  }
113370b324cSopenharmony_ci
114370b324cSopenharmony_ci  bool IsAllocated() const { return _data != NULL; }
115370b324cSopenharmony_ci  operator       Byte *()       { return _data; }
116370b324cSopenharmony_ci  operator const Byte *() const { return _data; }
117370b324cSopenharmony_ci  size_t Size() const { return _size; }
118370b324cSopenharmony_ci
119370b324cSopenharmony_ci  void Alloc(size_t size)
120370b324cSopenharmony_ci  {
121370b324cSopenharmony_ci    if (!_data || size != _size)
122370b324cSopenharmony_ci    {
123370b324cSopenharmony_ci      ISzAlloc_Free(&g_AlignedAlloc, _data);
124370b324cSopenharmony_ci      _size = 0;
125370b324cSopenharmony_ci      _data = NULL;
126370b324cSopenharmony_ci      _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
127370b324cSopenharmony_ci      if (_data)
128370b324cSopenharmony_ci        _size = size;
129370b324cSopenharmony_ci    }
130370b324cSopenharmony_ci  }
131370b324cSopenharmony_ci
132370b324cSopenharmony_ci  void AllocAtLeast(size_t size)
133370b324cSopenharmony_ci  {
134370b324cSopenharmony_ci    if (!_data || size > _size)
135370b324cSopenharmony_ci    {
136370b324cSopenharmony_ci      ISzAlloc_Free(&g_AlignedAlloc, _data);
137370b324cSopenharmony_ci      _size = 0;
138370b324cSopenharmony_ci      _data = NULL;
139370b324cSopenharmony_ci      _data = (Byte *)ISzAlloc_Alloc(&g_AlignedAlloc, size);
140370b324cSopenharmony_ci      if (_data)
141370b324cSopenharmony_ci        _size = size;
142370b324cSopenharmony_ci    }
143370b324cSopenharmony_ci  }
144370b324cSopenharmony_ci};
145370b324cSopenharmony_ci
146370b324cSopenharmony_ci/*
147370b324cSopenharmony_ci  CMidAlignedBuffer must return aligned pointer.
148370b324cSopenharmony_ci   - in Windows it uses CMidBuffer(): MidAlloc() : VirtualAlloc()
149370b324cSopenharmony_ci       VirtualAlloc(): Memory allocated is automatically initialized to zero.
150370b324cSopenharmony_ci       MidAlloc(0) returns NULL
151370b324cSopenharmony_ci   - in non-Windows systems it uses g_AlignedAlloc.
152370b324cSopenharmony_ci     g_AlignedAlloc::Alloc(size = 0) can return non NULL.
153370b324cSopenharmony_ci*/
154370b324cSopenharmony_ci
155370b324cSopenharmony_citypedef
156370b324cSopenharmony_ci#ifdef _WIN32
157370b324cSopenharmony_ci  CMidBuffer
158370b324cSopenharmony_ci#else
159370b324cSopenharmony_ci  CAlignedBuffer
160370b324cSopenharmony_ci#endif
161370b324cSopenharmony_ci  CMidAlignedBuffer;
162370b324cSopenharmony_ci
163370b324cSopenharmony_ci
164370b324cSopenharmony_ci#endif
165