diff options
author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2021-12-27 00:00:00 +0000 |
---|---|---|
committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2022-03-18 15:35:13 +0500 |
commit | f19f813537c7aea1c20749c914e756b54a9c3cf5 (patch) | |
tree | 816ba62ca7c0fa19f2eb46d9e9d6f7dd7c3a744d /CPP/Common/MyBuffer2.h | |
parent | 98e06a519b63b81986abe76d28887f6984a7732b (diff) | |
download | 7zip-21.07.tar.gz 7zip-21.07.tar.bz2 7zip-21.07.zip |
'21.07'21.07
Diffstat (limited to 'CPP/Common/MyBuffer2.h')
-rw-r--r-- | CPP/Common/MyBuffer2.h | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/CPP/Common/MyBuffer2.h b/CPP/Common/MyBuffer2.h new file mode 100644 index 0000000..372d478 --- /dev/null +++ b/CPP/Common/MyBuffer2.h | |||
@@ -0,0 +1,139 @@ | |||
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 | |||
15 | CLASS_NO_COPY(CMidBuffer) | ||
16 | |||
17 | public: | ||
18 | CMidBuffer(): _data(NULL), _size(0) {} | ||
19 | ~CMidBuffer() { ::MidFree(_data); } | ||
20 | |||
21 | void Free() { ::MidFree(_data); _data = NULL; _size = 0; } | ||
22 | |||
23 | bool IsAllocated() const { return _data != NULL; } | ||
24 | operator Byte *() { return _data; } | ||
25 | operator const Byte *() const { return _data; } | ||
26 | size_t Size() const { return _size; } | ||
27 | |||
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 | |||
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 | |||
64 | CLASS_NO_COPY(CAlignedBuffer) | ||
65 | |||
66 | public: | ||
67 | CAlignedBuffer(): _data(NULL), _size(0) {} | ||
68 | ~CAlignedBuffer() | ||
69 | { | ||
70 | ISzAlloc_Free(&g_AlignedAlloc, _data); | ||
71 | } | ||
72 | |||
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 | |||
82 | void Free() | ||
83 | { | ||
84 | ISzAlloc_Free(&g_AlignedAlloc, _data); | ||
85 | _data = NULL; | ||
86 | _size = 0; | ||
87 | } | ||
88 | |||
89 | bool IsAllocated() const { return _data != NULL; } | ||
90 | operator Byte *() { return _data; } | ||
91 | operator const Byte *() const { return _data; } | ||
92 | size_t Size() const { return _size; } | ||
93 | |||
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 | |||
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 | ||