chd 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +30 -0
- data/chd.gemspec +29 -0
- data/ext/chd.c +1008 -0
- data/ext/extconf.rb +60 -0
- data/lib/chd/cd.rb +272 -0
- data/lib/chd/metadata.rb +196 -0
- data/lib/chd/version.rb +4 -0
- data/lib/chd.rb +21 -0
- data/libchdr/CMakeLists.txt +104 -0
- data/libchdr/LICENSE.txt +24 -0
- data/libchdr/README.md +7 -0
- data/libchdr/deps/lzma-19.00/CMakeLists.txt +33 -0
- data/libchdr/deps/lzma-19.00/LICENSE +3 -0
- data/libchdr/deps/lzma-19.00/include/7zTypes.h +375 -0
- data/libchdr/deps/lzma-19.00/include/Alloc.h +51 -0
- data/libchdr/deps/lzma-19.00/include/Bra.h +64 -0
- data/libchdr/deps/lzma-19.00/include/Compiler.h +33 -0
- data/libchdr/deps/lzma-19.00/include/CpuArch.h +336 -0
- data/libchdr/deps/lzma-19.00/include/Delta.h +19 -0
- data/libchdr/deps/lzma-19.00/include/LzFind.h +121 -0
- data/libchdr/deps/lzma-19.00/include/LzHash.h +57 -0
- data/libchdr/deps/lzma-19.00/include/Lzma86.h +111 -0
- data/libchdr/deps/lzma-19.00/include/LzmaDec.h +234 -0
- data/libchdr/deps/lzma-19.00/include/LzmaEnc.h +76 -0
- data/libchdr/deps/lzma-19.00/include/LzmaLib.h +131 -0
- data/libchdr/deps/lzma-19.00/include/Precomp.h +10 -0
- data/libchdr/deps/lzma-19.00/include/Sort.h +18 -0
- data/libchdr/deps/lzma-19.00/lzma-history.txt +446 -0
- data/libchdr/deps/lzma-19.00/lzma.txt +328 -0
- data/libchdr/deps/lzma-19.00/lzma.vcxproj +543 -0
- data/libchdr/deps/lzma-19.00/lzma.vcxproj.filters +17 -0
- data/libchdr/deps/lzma-19.00/src/Alloc.c +455 -0
- data/libchdr/deps/lzma-19.00/src/Bra86.c +82 -0
- data/libchdr/deps/lzma-19.00/src/BraIA64.c +53 -0
- data/libchdr/deps/lzma-19.00/src/CpuArch.c +218 -0
- data/libchdr/deps/lzma-19.00/src/Delta.c +64 -0
- data/libchdr/deps/lzma-19.00/src/LzFind.c +1127 -0
- data/libchdr/deps/lzma-19.00/src/Lzma86Dec.c +54 -0
- data/libchdr/deps/lzma-19.00/src/LzmaDec.c +1185 -0
- data/libchdr/deps/lzma-19.00/src/LzmaEnc.c +1330 -0
- data/libchdr/deps/lzma-19.00/src/Sort.c +141 -0
- data/libchdr/deps/zlib-1.2.11/CMakeLists.txt +29 -0
- data/libchdr/deps/zlib-1.2.11/ChangeLog +1515 -0
- data/libchdr/deps/zlib-1.2.11/FAQ +368 -0
- data/libchdr/deps/zlib-1.2.11/INDEX +68 -0
- data/libchdr/deps/zlib-1.2.11/Makefile +5 -0
- data/libchdr/deps/zlib-1.2.11/Makefile.in +410 -0
- data/libchdr/deps/zlib-1.2.11/README +115 -0
- data/libchdr/deps/zlib-1.2.11/adler32.c +186 -0
- data/libchdr/deps/zlib-1.2.11/compress.c +86 -0
- data/libchdr/deps/zlib-1.2.11/configure +921 -0
- data/libchdr/deps/zlib-1.2.11/crc32.c +442 -0
- data/libchdr/deps/zlib-1.2.11/crc32.h +441 -0
- data/libchdr/deps/zlib-1.2.11/deflate.c +2163 -0
- data/libchdr/deps/zlib-1.2.11/deflate.h +349 -0
- data/libchdr/deps/zlib-1.2.11/doc/algorithm.txt +209 -0
- data/libchdr/deps/zlib-1.2.11/doc/rfc1950.txt +619 -0
- data/libchdr/deps/zlib-1.2.11/doc/rfc1951.txt +955 -0
- data/libchdr/deps/zlib-1.2.11/doc/rfc1952.txt +675 -0
- data/libchdr/deps/zlib-1.2.11/doc/txtvsbin.txt +107 -0
- data/libchdr/deps/zlib-1.2.11/gzclose.c +25 -0
- data/libchdr/deps/zlib-1.2.11/gzguts.h +218 -0
- data/libchdr/deps/zlib-1.2.11/gzlib.c +637 -0
- data/libchdr/deps/zlib-1.2.11/gzread.c +654 -0
- data/libchdr/deps/zlib-1.2.11/gzwrite.c +665 -0
- data/libchdr/deps/zlib-1.2.11/infback.c +640 -0
- data/libchdr/deps/zlib-1.2.11/inffast.c +323 -0
- data/libchdr/deps/zlib-1.2.11/inffast.h +11 -0
- data/libchdr/deps/zlib-1.2.11/inffixed.h +94 -0
- data/libchdr/deps/zlib-1.2.11/inflate.c +1561 -0
- data/libchdr/deps/zlib-1.2.11/inflate.h +125 -0
- data/libchdr/deps/zlib-1.2.11/inftrees.c +304 -0
- data/libchdr/deps/zlib-1.2.11/inftrees.h +62 -0
- data/libchdr/deps/zlib-1.2.11/make_vms.com +867 -0
- data/libchdr/deps/zlib-1.2.11/treebuild.xml +116 -0
- data/libchdr/deps/zlib-1.2.11/trees.c +1203 -0
- data/libchdr/deps/zlib-1.2.11/trees.h +128 -0
- data/libchdr/deps/zlib-1.2.11/uncompr.c +93 -0
- data/libchdr/deps/zlib-1.2.11/zconf.h +534 -0
- data/libchdr/deps/zlib-1.2.11/zconf.h.cmakein +536 -0
- data/libchdr/deps/zlib-1.2.11/zconf.h.in +534 -0
- data/libchdr/deps/zlib-1.2.11/zlib.3 +149 -0
- data/libchdr/deps/zlib-1.2.11/zlib.3.pdf +0 -0
- data/libchdr/deps/zlib-1.2.11/zlib.h +1912 -0
- data/libchdr/deps/zlib-1.2.11/zlib.map +94 -0
- data/libchdr/deps/zlib-1.2.11/zlib.pc.cmakein +13 -0
- data/libchdr/deps/zlib-1.2.11/zlib.pc.in +13 -0
- data/libchdr/deps/zlib-1.2.11/zlib2ansi +152 -0
- data/libchdr/deps/zlib-1.2.11/zutil.c +325 -0
- data/libchdr/deps/zlib-1.2.11/zutil.h +271 -0
- data/libchdr/include/dr_libs/dr_flac.h +12280 -0
- data/libchdr/include/libchdr/bitstream.h +43 -0
- data/libchdr/include/libchdr/cdrom.h +110 -0
- data/libchdr/include/libchdr/chd.h +427 -0
- data/libchdr/include/libchdr/chdconfig.h +10 -0
- data/libchdr/include/libchdr/coretypes.h +60 -0
- data/libchdr/include/libchdr/flac.h +50 -0
- data/libchdr/include/libchdr/huffman.h +90 -0
- data/libchdr/pkg-config.pc.in +10 -0
- data/libchdr/src/libchdr_bitstream.c +125 -0
- data/libchdr/src/libchdr_cdrom.c +415 -0
- data/libchdr/src/libchdr_chd.c +2744 -0
- data/libchdr/src/libchdr_flac.c +302 -0
- data/libchdr/src/libchdr_huffman.c +545 -0
- data/libchdr/src/link.T +5 -0
- data/libchdr/tests/CMakeLists.txt +2 -0
- data/libchdr/tests/benchmark.c +52 -0
- metadata +183 -0
@@ -0,0 +1,455 @@
|
|
1
|
+
/* Alloc.c -- Memory allocation functions
|
2
|
+
2018-04-27 : Igor Pavlov : Public domain */
|
3
|
+
|
4
|
+
#include "Precomp.h"
|
5
|
+
|
6
|
+
#include <stdio.h>
|
7
|
+
|
8
|
+
#ifdef _WIN32
|
9
|
+
#include <windows.h>
|
10
|
+
#endif
|
11
|
+
#include <stdlib.h>
|
12
|
+
|
13
|
+
#include "Alloc.h"
|
14
|
+
|
15
|
+
/* #define _SZ_ALLOC_DEBUG */
|
16
|
+
|
17
|
+
/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */
|
18
|
+
#ifdef _SZ_ALLOC_DEBUG
|
19
|
+
|
20
|
+
#include <stdio.h>
|
21
|
+
int g_allocCount = 0;
|
22
|
+
int g_allocCountMid = 0;
|
23
|
+
int g_allocCountBig = 0;
|
24
|
+
|
25
|
+
|
26
|
+
#define CONVERT_INT_TO_STR(charType, tempSize) \
|
27
|
+
unsigned char temp[tempSize]; unsigned i = 0; \
|
28
|
+
while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \
|
29
|
+
*s++ = (charType)('0' + (unsigned)val); \
|
30
|
+
while (i != 0) { i--; *s++ = temp[i]; } \
|
31
|
+
*s = 0;
|
32
|
+
|
33
|
+
static void ConvertUInt64ToString(UInt64 val, char *s)
|
34
|
+
{
|
35
|
+
CONVERT_INT_TO_STR(char, 24);
|
36
|
+
}
|
37
|
+
|
38
|
+
#define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10)))))
|
39
|
+
|
40
|
+
static void ConvertUInt64ToHex(UInt64 val, char *s)
|
41
|
+
{
|
42
|
+
UInt64 v = val;
|
43
|
+
unsigned i;
|
44
|
+
for (i = 1;; i++)
|
45
|
+
{
|
46
|
+
v >>= 4;
|
47
|
+
if (v == 0)
|
48
|
+
break;
|
49
|
+
}
|
50
|
+
s[i] = 0;
|
51
|
+
do
|
52
|
+
{
|
53
|
+
unsigned t = (unsigned)(val & 0xF);
|
54
|
+
val >>= 4;
|
55
|
+
s[--i] = GET_HEX_CHAR(t);
|
56
|
+
}
|
57
|
+
while (i);
|
58
|
+
}
|
59
|
+
|
60
|
+
#define DEBUG_OUT_STREAM stderr
|
61
|
+
|
62
|
+
static void Print(const char *s)
|
63
|
+
{
|
64
|
+
fputs(s, DEBUG_OUT_STREAM);
|
65
|
+
}
|
66
|
+
|
67
|
+
static void PrintAligned(const char *s, size_t align)
|
68
|
+
{
|
69
|
+
size_t len = strlen(s);
|
70
|
+
for(;;)
|
71
|
+
{
|
72
|
+
fputc(' ', DEBUG_OUT_STREAM);
|
73
|
+
if (len >= align)
|
74
|
+
break;
|
75
|
+
++len;
|
76
|
+
}
|
77
|
+
Print(s);
|
78
|
+
}
|
79
|
+
|
80
|
+
static void PrintLn()
|
81
|
+
{
|
82
|
+
Print("\n");
|
83
|
+
}
|
84
|
+
|
85
|
+
static void PrintHex(UInt64 v, size_t align)
|
86
|
+
{
|
87
|
+
char s[32];
|
88
|
+
ConvertUInt64ToHex(v, s);
|
89
|
+
PrintAligned(s, align);
|
90
|
+
}
|
91
|
+
|
92
|
+
static void PrintDec(UInt64 v, size_t align)
|
93
|
+
{
|
94
|
+
char s[32];
|
95
|
+
ConvertUInt64ToString(v, s);
|
96
|
+
PrintAligned(s, align);
|
97
|
+
}
|
98
|
+
|
99
|
+
static void PrintAddr(void *p)
|
100
|
+
{
|
101
|
+
PrintHex((UInt64)(size_t)(ptrdiff_t)p, 12);
|
102
|
+
}
|
103
|
+
|
104
|
+
|
105
|
+
#define PRINT_ALLOC(name, cnt, size, ptr) \
|
106
|
+
Print(name " "); \
|
107
|
+
PrintDec(cnt++, 10); \
|
108
|
+
PrintHex(size, 10); \
|
109
|
+
PrintAddr(ptr); \
|
110
|
+
PrintLn();
|
111
|
+
|
112
|
+
#define PRINT_FREE(name, cnt, ptr) if (ptr) { \
|
113
|
+
Print(name " "); \
|
114
|
+
PrintDec(--cnt, 10); \
|
115
|
+
PrintAddr(ptr); \
|
116
|
+
PrintLn(); }
|
117
|
+
|
118
|
+
#else
|
119
|
+
|
120
|
+
#define PRINT_ALLOC(name, cnt, size, ptr)
|
121
|
+
#define PRINT_FREE(name, cnt, ptr)
|
122
|
+
#define Print(s)
|
123
|
+
#define PrintLn()
|
124
|
+
#define PrintHex(v, align)
|
125
|
+
#define PrintDec(v, align)
|
126
|
+
#define PrintAddr(p)
|
127
|
+
|
128
|
+
#endif
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
void *MyAlloc(size_t size)
|
133
|
+
{
|
134
|
+
if (size == 0)
|
135
|
+
return NULL;
|
136
|
+
#ifdef _SZ_ALLOC_DEBUG
|
137
|
+
{
|
138
|
+
void *p = malloc(size);
|
139
|
+
PRINT_ALLOC("Alloc ", g_allocCount, size, p);
|
140
|
+
return p;
|
141
|
+
}
|
142
|
+
#else
|
143
|
+
return malloc(size);
|
144
|
+
#endif
|
145
|
+
}
|
146
|
+
|
147
|
+
void MyFree(void *address)
|
148
|
+
{
|
149
|
+
PRINT_FREE("Free ", g_allocCount, address);
|
150
|
+
|
151
|
+
free(address);
|
152
|
+
}
|
153
|
+
|
154
|
+
#ifdef _WIN32
|
155
|
+
|
156
|
+
void *MidAlloc(size_t size)
|
157
|
+
{
|
158
|
+
if (size == 0)
|
159
|
+
return NULL;
|
160
|
+
|
161
|
+
PRINT_ALLOC("Alloc-Mid", g_allocCountMid, size, NULL);
|
162
|
+
|
163
|
+
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
|
164
|
+
}
|
165
|
+
|
166
|
+
void MidFree(void *address)
|
167
|
+
{
|
168
|
+
PRINT_FREE("Free-Mid", g_allocCountMid, address);
|
169
|
+
|
170
|
+
if (!address)
|
171
|
+
return;
|
172
|
+
VirtualFree(address, 0, MEM_RELEASE);
|
173
|
+
}
|
174
|
+
|
175
|
+
#ifndef MEM_LARGE_PAGES
|
176
|
+
#undef _7ZIP_LARGE_PAGES
|
177
|
+
#endif
|
178
|
+
|
179
|
+
#ifdef _7ZIP_LARGE_PAGES
|
180
|
+
SIZE_T g_LargePageSize = 0;
|
181
|
+
typedef SIZE_T (WINAPI *GetLargePageMinimumP)();
|
182
|
+
#endif
|
183
|
+
|
184
|
+
void SetLargePageSize()
|
185
|
+
{
|
186
|
+
#ifdef _7ZIP_LARGE_PAGES
|
187
|
+
SIZE_T size;
|
188
|
+
GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP)
|
189
|
+
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum");
|
190
|
+
if (!largePageMinimum)
|
191
|
+
return;
|
192
|
+
size = largePageMinimum();
|
193
|
+
if (size == 0 || (size & (size - 1)) != 0)
|
194
|
+
return;
|
195
|
+
g_LargePageSize = size;
|
196
|
+
#endif
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
void *BigAlloc(size_t size)
|
201
|
+
{
|
202
|
+
if (size == 0)
|
203
|
+
return NULL;
|
204
|
+
|
205
|
+
PRINT_ALLOC("Alloc-Big", g_allocCountBig, size, NULL);
|
206
|
+
|
207
|
+
#ifdef _7ZIP_LARGE_PAGES
|
208
|
+
{
|
209
|
+
SIZE_T ps = g_LargePageSize;
|
210
|
+
if (ps != 0 && ps <= (1 << 30) && size > (ps / 2))
|
211
|
+
{
|
212
|
+
size_t size2;
|
213
|
+
ps--;
|
214
|
+
size2 = (size + ps) & ~ps;
|
215
|
+
if (size2 >= size)
|
216
|
+
{
|
217
|
+
void *res = VirtualAlloc(NULL, size2, MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
|
218
|
+
if (res)
|
219
|
+
return res;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
#endif
|
224
|
+
|
225
|
+
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
|
226
|
+
}
|
227
|
+
|
228
|
+
void BigFree(void *address)
|
229
|
+
{
|
230
|
+
PRINT_FREE("Free-Big", g_allocCountBig, address);
|
231
|
+
|
232
|
+
if (!address)
|
233
|
+
return;
|
234
|
+
VirtualFree(address, 0, MEM_RELEASE);
|
235
|
+
}
|
236
|
+
|
237
|
+
#endif
|
238
|
+
|
239
|
+
|
240
|
+
static void *SzAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return MyAlloc(size); }
|
241
|
+
static void SzFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p); MyFree(address); }
|
242
|
+
const ISzAlloc g_Alloc = { SzAlloc, SzFree };
|
243
|
+
|
244
|
+
static void *SzMidAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return MidAlloc(size); }
|
245
|
+
static void SzMidFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p); MidFree(address); }
|
246
|
+
const ISzAlloc g_MidAlloc = { SzMidAlloc, SzMidFree };
|
247
|
+
|
248
|
+
static void *SzBigAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return BigAlloc(size); }
|
249
|
+
static void SzBigFree(ISzAllocPtr p, void *address) { UNUSED_VAR(p); BigFree(address); }
|
250
|
+
const ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree };
|
251
|
+
|
252
|
+
|
253
|
+
/*
|
254
|
+
uintptr_t : <stdint.h> C99 (optional)
|
255
|
+
: unsupported in VS6
|
256
|
+
*/
|
257
|
+
|
258
|
+
#ifdef _WIN32
|
259
|
+
typedef UINT_PTR UIntPtr;
|
260
|
+
#else
|
261
|
+
/*
|
262
|
+
typedef uintptr_t UIntPtr;
|
263
|
+
*/
|
264
|
+
typedef ptrdiff_t UIntPtr;
|
265
|
+
#endif
|
266
|
+
|
267
|
+
|
268
|
+
#define ADJUST_ALLOC_SIZE 0
|
269
|
+
/*
|
270
|
+
#define ADJUST_ALLOC_SIZE (sizeof(void *) - 1)
|
271
|
+
*/
|
272
|
+
/*
|
273
|
+
Use (ADJUST_ALLOC_SIZE = (sizeof(void *) - 1)), if
|
274
|
+
MyAlloc() can return address that is NOT multiple of sizeof(void *).
|
275
|
+
*/
|
276
|
+
|
277
|
+
|
278
|
+
/*
|
279
|
+
#define MY_ALIGN_PTR_DOWN(p, align) ((void *)((char *)(p) - ((size_t)(UIntPtr)(p) & ((align) - 1))))
|
280
|
+
*/
|
281
|
+
#define MY_ALIGN_PTR_DOWN(p, align) ((void *)((((UIntPtr)(p)) & ~((UIntPtr)(align) - 1))))
|
282
|
+
|
283
|
+
#define MY_ALIGN_PTR_UP_PLUS(p, align) MY_ALIGN_PTR_DOWN(((char *)(p) + (align) + ADJUST_ALLOC_SIZE), align)
|
284
|
+
|
285
|
+
|
286
|
+
#if (_POSIX_C_SOURCE >= 200112L) && !defined(_WIN32)
|
287
|
+
#define USE_posix_memalign
|
288
|
+
#endif
|
289
|
+
|
290
|
+
/*
|
291
|
+
This posix_memalign() is for test purposes only.
|
292
|
+
We also need special Free() function instead of free(),
|
293
|
+
if this posix_memalign() is used.
|
294
|
+
*/
|
295
|
+
|
296
|
+
/*
|
297
|
+
static int posix_memalign(void **ptr, size_t align, size_t size)
|
298
|
+
{
|
299
|
+
size_t newSize = size + align;
|
300
|
+
void *p;
|
301
|
+
void *pAligned;
|
302
|
+
*ptr = NULL;
|
303
|
+
if (newSize < size)
|
304
|
+
return 12; // ENOMEM
|
305
|
+
p = MyAlloc(newSize);
|
306
|
+
if (!p)
|
307
|
+
return 12; // ENOMEM
|
308
|
+
pAligned = MY_ALIGN_PTR_UP_PLUS(p, align);
|
309
|
+
((void **)pAligned)[-1] = p;
|
310
|
+
*ptr = pAligned;
|
311
|
+
return 0;
|
312
|
+
}
|
313
|
+
*/
|
314
|
+
|
315
|
+
/*
|
316
|
+
ALLOC_ALIGN_SIZE >= sizeof(void *)
|
317
|
+
ALLOC_ALIGN_SIZE >= cache_line_size
|
318
|
+
*/
|
319
|
+
|
320
|
+
#define ALLOC_ALIGN_SIZE ((size_t)1 << 7)
|
321
|
+
|
322
|
+
static void *SzAlignedAlloc(ISzAllocPtr pp, size_t size)
|
323
|
+
{
|
324
|
+
#ifndef USE_posix_memalign
|
325
|
+
|
326
|
+
void *p;
|
327
|
+
void *pAligned;
|
328
|
+
size_t newSize;
|
329
|
+
UNUSED_VAR(pp);
|
330
|
+
|
331
|
+
/* also we can allocate additional dummy ALLOC_ALIGN_SIZE bytes after aligned
|
332
|
+
block to prevent cache line sharing with another allocated blocks */
|
333
|
+
|
334
|
+
newSize = size + ALLOC_ALIGN_SIZE * 1 + ADJUST_ALLOC_SIZE;
|
335
|
+
if (newSize < size)
|
336
|
+
return NULL;
|
337
|
+
|
338
|
+
p = MyAlloc(newSize);
|
339
|
+
|
340
|
+
if (!p)
|
341
|
+
return NULL;
|
342
|
+
pAligned = MY_ALIGN_PTR_UP_PLUS(p, ALLOC_ALIGN_SIZE);
|
343
|
+
|
344
|
+
Print(" size="); PrintHex(size, 8);
|
345
|
+
Print(" a_size="); PrintHex(newSize, 8);
|
346
|
+
Print(" ptr="); PrintAddr(p);
|
347
|
+
Print(" a_ptr="); PrintAddr(pAligned);
|
348
|
+
PrintLn();
|
349
|
+
|
350
|
+
((void **)pAligned)[-1] = p;
|
351
|
+
|
352
|
+
return pAligned;
|
353
|
+
|
354
|
+
#else
|
355
|
+
|
356
|
+
void *p;
|
357
|
+
UNUSED_VAR(pp);
|
358
|
+
if (posix_memalign(&p, ALLOC_ALIGN_SIZE, size))
|
359
|
+
return NULL;
|
360
|
+
|
361
|
+
Print(" posix_memalign="); PrintAddr(p);
|
362
|
+
PrintLn();
|
363
|
+
|
364
|
+
return p;
|
365
|
+
|
366
|
+
#endif
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
static void SzAlignedFree(ISzAllocPtr pp, void *address)
|
371
|
+
{
|
372
|
+
UNUSED_VAR(pp);
|
373
|
+
#ifndef USE_posix_memalign
|
374
|
+
if (address)
|
375
|
+
MyFree(((void **)address)[-1]);
|
376
|
+
#else
|
377
|
+
free(address);
|
378
|
+
#endif
|
379
|
+
}
|
380
|
+
|
381
|
+
|
382
|
+
const ISzAlloc g_AlignedAlloc = { SzAlignedAlloc, SzAlignedFree };
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
#define MY_ALIGN_PTR_DOWN_1(p) MY_ALIGN_PTR_DOWN(p, sizeof(void *))
|
387
|
+
|
388
|
+
/* we align ptr to support cases where CAlignOffsetAlloc::offset is not multiply of sizeof(void *) */
|
389
|
+
#define REAL_BLOCK_PTR_VAR(p) ((void **)MY_ALIGN_PTR_DOWN_1(p))[-1]
|
390
|
+
/*
|
391
|
+
#define REAL_BLOCK_PTR_VAR(p) ((void **)(p))[-1]
|
392
|
+
*/
|
393
|
+
|
394
|
+
static void *AlignOffsetAlloc_Alloc(ISzAllocPtr pp, size_t size)
|
395
|
+
{
|
396
|
+
CAlignOffsetAlloc *p = CONTAINER_FROM_VTBL(pp, CAlignOffsetAlloc, vt);
|
397
|
+
void *adr;
|
398
|
+
void *pAligned;
|
399
|
+
size_t newSize;
|
400
|
+
size_t extra;
|
401
|
+
size_t alignSize = (size_t)1 << p->numAlignBits;
|
402
|
+
|
403
|
+
if (alignSize < sizeof(void *))
|
404
|
+
alignSize = sizeof(void *);
|
405
|
+
|
406
|
+
if (p->offset >= alignSize)
|
407
|
+
return NULL;
|
408
|
+
|
409
|
+
/* also we can allocate additional dummy ALLOC_ALIGN_SIZE bytes after aligned
|
410
|
+
block to prevent cache line sharing with another allocated blocks */
|
411
|
+
extra = p->offset & (sizeof(void *) - 1);
|
412
|
+
newSize = size + alignSize + extra + ADJUST_ALLOC_SIZE;
|
413
|
+
if (newSize < size)
|
414
|
+
return NULL;
|
415
|
+
|
416
|
+
adr = ISzAlloc_Alloc(p->baseAlloc, newSize);
|
417
|
+
|
418
|
+
if (!adr)
|
419
|
+
return NULL;
|
420
|
+
|
421
|
+
pAligned = (char *)MY_ALIGN_PTR_DOWN((char *)adr +
|
422
|
+
alignSize - p->offset + extra + ADJUST_ALLOC_SIZE, alignSize) + p->offset;
|
423
|
+
|
424
|
+
PrintLn();
|
425
|
+
Print("- Aligned: ");
|
426
|
+
Print(" size="); PrintHex(size, 8);
|
427
|
+
Print(" a_size="); PrintHex(newSize, 8);
|
428
|
+
Print(" ptr="); PrintAddr(adr);
|
429
|
+
Print(" a_ptr="); PrintAddr(pAligned);
|
430
|
+
PrintLn();
|
431
|
+
|
432
|
+
REAL_BLOCK_PTR_VAR(pAligned) = adr;
|
433
|
+
|
434
|
+
return pAligned;
|
435
|
+
}
|
436
|
+
|
437
|
+
|
438
|
+
static void AlignOffsetAlloc_Free(ISzAllocPtr pp, void *address)
|
439
|
+
{
|
440
|
+
if (address)
|
441
|
+
{
|
442
|
+
CAlignOffsetAlloc *p = CONTAINER_FROM_VTBL(pp, CAlignOffsetAlloc, vt);
|
443
|
+
PrintLn();
|
444
|
+
Print("- Aligned Free: ");
|
445
|
+
PrintLn();
|
446
|
+
ISzAlloc_Free(p->baseAlloc, REAL_BLOCK_PTR_VAR(address));
|
447
|
+
}
|
448
|
+
}
|
449
|
+
|
450
|
+
|
451
|
+
void AlignOffsetAlloc_CreateVTable(CAlignOffsetAlloc *p)
|
452
|
+
{
|
453
|
+
p->vt.Alloc = AlignOffsetAlloc_Alloc;
|
454
|
+
p->vt.Free = AlignOffsetAlloc_Free;
|
455
|
+
}
|
@@ -0,0 +1,82 @@
|
|
1
|
+
/* Bra86.c -- Converter for x86 code (BCJ)
|
2
|
+
2017-04-03 : Igor Pavlov : Public domain */
|
3
|
+
|
4
|
+
#include "Precomp.h"
|
5
|
+
|
6
|
+
#include "Bra.h"
|
7
|
+
|
8
|
+
#define Test86MSByte(b) ((((b) + 1) & 0xFE) == 0)
|
9
|
+
|
10
|
+
SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
|
11
|
+
{
|
12
|
+
SizeT pos = 0;
|
13
|
+
UInt32 mask = *state & 7;
|
14
|
+
if (size < 5)
|
15
|
+
return 0;
|
16
|
+
size -= 4;
|
17
|
+
ip += 5;
|
18
|
+
|
19
|
+
for (;;)
|
20
|
+
{
|
21
|
+
Byte *p = data + pos;
|
22
|
+
const Byte *limit = data + size;
|
23
|
+
for (; p < limit; p++)
|
24
|
+
if ((*p & 0xFE) == 0xE8)
|
25
|
+
break;
|
26
|
+
|
27
|
+
{
|
28
|
+
SizeT d = (SizeT)(p - data - pos);
|
29
|
+
pos = (SizeT)(p - data);
|
30
|
+
if (p >= limit)
|
31
|
+
{
|
32
|
+
*state = (d > 2 ? 0 : mask >> (unsigned)d);
|
33
|
+
return pos;
|
34
|
+
}
|
35
|
+
if (d > 2)
|
36
|
+
mask = 0;
|
37
|
+
else
|
38
|
+
{
|
39
|
+
mask >>= (unsigned)d;
|
40
|
+
if (mask != 0 && (mask > 4 || mask == 3 || Test86MSByte(p[(size_t)(mask >> 1) + 1])))
|
41
|
+
{
|
42
|
+
mask = (mask >> 1) | 4;
|
43
|
+
pos++;
|
44
|
+
continue;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
if (Test86MSByte(p[4]))
|
50
|
+
{
|
51
|
+
UInt32 v = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
|
52
|
+
UInt32 cur = ip + (UInt32)pos;
|
53
|
+
pos += 5;
|
54
|
+
if (encoding)
|
55
|
+
v += cur;
|
56
|
+
else
|
57
|
+
v -= cur;
|
58
|
+
if (mask != 0)
|
59
|
+
{
|
60
|
+
unsigned sh = (mask & 6) << 2;
|
61
|
+
if (Test86MSByte((Byte)(v >> sh)))
|
62
|
+
{
|
63
|
+
v ^= (((UInt32)0x100 << sh) - 1);
|
64
|
+
if (encoding)
|
65
|
+
v += cur;
|
66
|
+
else
|
67
|
+
v -= cur;
|
68
|
+
}
|
69
|
+
mask = 0;
|
70
|
+
}
|
71
|
+
p[1] = (Byte)v;
|
72
|
+
p[2] = (Byte)(v >> 8);
|
73
|
+
p[3] = (Byte)(v >> 16);
|
74
|
+
p[4] = (Byte)(0 - ((v >> 24) & 1));
|
75
|
+
}
|
76
|
+
else
|
77
|
+
{
|
78
|
+
mask = (mask >> 1) | 4;
|
79
|
+
pos++;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
/* BraIA64.c -- Converter for IA-64 code
|
2
|
+
2017-01-26 : Igor Pavlov : Public domain */
|
3
|
+
|
4
|
+
#include "Precomp.h"
|
5
|
+
|
6
|
+
#include "CpuArch.h"
|
7
|
+
#include "Bra.h"
|
8
|
+
|
9
|
+
SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
|
10
|
+
{
|
11
|
+
SizeT i;
|
12
|
+
if (size < 16)
|
13
|
+
return 0;
|
14
|
+
size -= 16;
|
15
|
+
i = 0;
|
16
|
+
do
|
17
|
+
{
|
18
|
+
unsigned m = ((UInt32)0x334B0000 >> (data[i] & 0x1E)) & 3;
|
19
|
+
if (m)
|
20
|
+
{
|
21
|
+
m++;
|
22
|
+
do
|
23
|
+
{
|
24
|
+
Byte *p = data + (i + (size_t)m * 5 - 8);
|
25
|
+
if (((p[3] >> m) & 15) == 5
|
26
|
+
&& (((p[-1] | ((UInt32)p[0] << 8)) >> m) & 0x70) == 0)
|
27
|
+
{
|
28
|
+
unsigned raw = GetUi32(p);
|
29
|
+
unsigned v = raw >> m;
|
30
|
+
v = (v & 0xFFFFF) | ((v & (1 << 23)) >> 3);
|
31
|
+
|
32
|
+
v <<= 4;
|
33
|
+
if (encoding)
|
34
|
+
v += ip + (UInt32)i;
|
35
|
+
else
|
36
|
+
v -= ip + (UInt32)i;
|
37
|
+
v >>= 4;
|
38
|
+
|
39
|
+
v &= 0x1FFFFF;
|
40
|
+
v += 0x700000;
|
41
|
+
v &= 0x8FFFFF;
|
42
|
+
raw &= ~((UInt32)0x8FFFFF << m);
|
43
|
+
raw |= (v << m);
|
44
|
+
SetUi32(p, raw);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
while (++m <= 4);
|
48
|
+
}
|
49
|
+
i += 16;
|
50
|
+
}
|
51
|
+
while (i <= size);
|
52
|
+
return i;
|
53
|
+
}
|