Have script/*.c global var names start with g_

This commit is contained in:
Nigel Tao committed 2020-03-26 21:55:44 +11:00
1 parent f909c69deb
commit c0bd9df978
3 files changed
+130 -128

No files matched your search

+71 -69
View File
@@ -75,17 +75,17 @@
// The order matters here. Clang also defines "__GNUC__".
#if defined(__clang__)
const char* cc = "clang";
const char* cc_version = __clang_version__;
const char* g_cc = "clang";
const char* g_cc_version = __clang_version__;
#elif defined(__GNUC__)
const char* cc = "gcc";
const char* cc_version = __VERSION__;
const char* g_cc = "gcc";
const char* g_cc_version = __VERSION__;
#elif defined(_MSC_VER)
const char* cc = "cl";
const char* cc_version = "???";
const char* g_cc = "cl";
const char* g_cc_version = "???";
#else
const char* cc = "cc";
const char* cc_version = "???";
const char* g_cc = "cc";
const char* g_cc_version = "???";
#endif
static inline uint32_t //
@@ -103,39 +103,40 @@ load_u32be(uint8_t* p) {
#define MAX_DIMENSION (16384)
#define MAX_IDAT_CHUNKS (1024)
uint8_t dst_buffer_array[DST_BUFFER_ARRAY_SIZE] = {0};
size_t dst_len = 0;
uint8_t src_buffer_array[SRC_BUFFER_ARRAY_SIZE] = {0};
size_t src_len = 0;
uint8_t idat_buffer_array[SRC_BUFFER_ARRAY_SIZE] = {0};
uint8_t g_dst_buffer_array[DST_BUFFER_ARRAY_SIZE] = {0};
size_t g_dst_len = 0;
uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE] = {0};
size_t g_src_len = 0;
uint8_t g_idat_buffer_array[SRC_BUFFER_ARRAY_SIZE] = {0};
// The n'th IDAT chunk data (where n is a zero-based count) is in
// idat_buffer_array[i:j], where i = idat_splits[n+0] and j = idat_splits[n+1].
size_t idat_splits[MAX_IDAT_CHUNKS + 1] = {0};
uint32_t num_idat_chunks = 0;
// g_idat_buffer_array[i:j], where i = g_idat_splits[n+0] and j =
// g_idat_splits[n+1].
size_t g_idat_splits[MAX_IDAT_CHUNKS + 1] = {0};
uint32_t g_num_idat_chunks = 0;
#define WORK_BUFFER_ARRAY_SIZE \
WUFFS_ZLIB__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
#if WORK_BUFFER_ARRAY_SIZE > 0
uint8_t work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
#else
// Not all C/C++ compilers support 0-length arrays.
uint8_t work_buffer_array[1];
uint8_t g_work_buffer_array[1];
#endif
uint32_t width = 0;
uint32_t height = 0;
uint64_t bytes_per_pixel = 0;
uint64_t bytes_per_row = 0;
uint64_t bytes_per_frame = 0;
uint32_t g_width = 0;
uint32_t g_height = 0;
uint64_t g_bytes_per_pixel = 0;
uint64_t g_bytes_per_row = 0;
uint64_t g_bytes_per_frame = 0;
const char* //
read_stdin() {
while (src_len < SRC_BUFFER_ARRAY_SIZE) {
while (g_src_len < SRC_BUFFER_ARRAY_SIZE) {
const int stdin_fd = 0;
ssize_t n = read(stdin_fd, src_buffer_array + src_len,
SRC_BUFFER_ARRAY_SIZE - src_len);
ssize_t n = read(stdin_fd, g_src_buffer_array + g_src_len,
SRC_BUFFER_ARRAY_SIZE - g_src_len);
if (n > 0) {
src_len += n;
g_src_len += n;
} else if (n == 0) {
return NULL;
} else if (errno == EINTR) {
@@ -168,36 +169,36 @@ process_png_chunks(uint8_t* p, size_t n) {
if (chunk_len != 13) {
return "invalid PNG IDAT chunk";
}
width = load_u32be(p + 0);
height = load_u32be(p + 4);
if ((width == 0) || (height == 0)) {
g_width = load_u32be(p + 0);
g_height = load_u32be(p + 4);
if ((g_width == 0) || (g_height == 0)) {
return "image dimensions are too small";
}
if ((width > MAX_DIMENSION) || (height > MAX_DIMENSION)) {
if ((g_width > MAX_DIMENSION) || (g_height > MAX_DIMENSION)) {
return "image dimensions are too large";
}
if (p[8] != 8) {
return "unsupported PNG bit depth";
}
if (bytes_per_pixel != 0) {
if (g_bytes_per_pixel != 0) {
return "duplicate PNG IHDR chunk";
}
// Process the color type, as per the PNG spec table 11.1.
switch (p[9]) {
case 0:
bytes_per_pixel = 1;
g_bytes_per_pixel = 1;
break;
case 2:
bytes_per_pixel = 3;
g_bytes_per_pixel = 3;
break;
case 3:
bytes_per_pixel = 1;
g_bytes_per_pixel = 1;
break;
case 4:
bytes_per_pixel = 2;
g_bytes_per_pixel = 2;
break;
case 6:
bytes_per_pixel = 4;
g_bytes_per_pixel = 4;
break;
default:
return "unsupported PNG color type";
@@ -208,13 +209,14 @@ process_png_chunks(uint8_t* p, size_t n) {
break;
case 0x49444154: // "IDAT"
if (num_idat_chunks == MAX_IDAT_CHUNKS - 1) {
if (g_num_idat_chunks == MAX_IDAT_CHUNKS - 1) {
return "too many IDAT chunks";
}
memcpy(idat_buffer_array + idat_splits[num_idat_chunks], p, chunk_len);
idat_splits[num_idat_chunks + 1] =
idat_splits[num_idat_chunks] + chunk_len;
num_idat_chunks++;
memcpy(g_idat_buffer_array + g_idat_splits[g_num_idat_chunks], p,
chunk_len);
g_idat_splits[g_num_idat_chunks + 1] =
g_idat_splits[g_num_idat_chunks] + chunk_len;
g_num_idat_chunks++;
break;
}
p += chunk_len;
@@ -241,17 +243,17 @@ decode_once(bool frag_dst, bool frag_idat) {
wuffs_base__io_buffer dst = ((wuffs_base__io_buffer){
.data = ((wuffs_base__slice_u8){
.ptr = dst_buffer_array,
.len = bytes_per_frame,
.ptr = g_dst_buffer_array,
.len = g_bytes_per_frame,
}),
});
wuffs_base__io_buffer idat = ((wuffs_base__io_buffer){
.data = ((wuffs_base__slice_u8){
.ptr = idat_buffer_array,
.ptr = g_idat_buffer_array,
.len = SRC_BUFFER_ARRAY_SIZE,
}),
.meta = ((wuffs_base__io_buffer_meta){
.wi = idat_splits[num_idat_chunks],
.wi = g_idat_splits[g_num_idat_chunks],
.ri = 0,
.pos = 0,
.closed = true,
@@ -260,20 +262,20 @@ decode_once(bool frag_dst, bool frag_idat) {
uint32_t i = 0; // Number of dst fragments processed, if frag_dst.
if (frag_dst) {
dst.data.len = bytes_per_row;
dst.data.len = g_bytes_per_row;
}
uint32_t j = 0; // Number of IDAT fragments processed, if frag_idat.
if (frag_idat) {
idat.meta.wi = idat_splits[1];
idat.meta.closed = (num_idat_chunks == 1);
idat.meta.wi = g_idat_splits[1];
idat.meta.closed = (g_num_idat_chunks == 1);
}
while (true) {
status =
wuffs_zlib__decoder__transform_io(&dec, &dst, &idat,
((wuffs_base__slice_u8){
.ptr = work_buffer_array,
.ptr = g_work_buffer_array,
.len = WORK_BUFFER_ARRAY_SIZE,
}));
@@ -281,22 +283,22 @@ decode_once(bool frag_dst, bool frag_idat) {
break;
}
if ((status.repr == wuffs_base__suspension__short_write) && frag_dst &&
(i < height - 1)) {
(i < g_height - 1)) {
i++;
dst.data.len = bytes_per_row * (i + 1);
dst.data.len = g_bytes_per_row * (i + 1);
continue;
}
if ((status.repr == wuffs_base__suspension__short_read) && frag_idat &&
(j < num_idat_chunks - 1)) {
(j < g_num_idat_chunks - 1)) {
j++;
idat.meta.wi = idat_splits[j + 1];
idat.meta.closed = (num_idat_chunks == j + 1);
idat.meta.wi = g_idat_splits[j + 1];
idat.meta.closed = (g_num_idat_chunks == j + 1);
continue;
}
return wuffs_base__status__message(&status);
}
if (dst.meta.wi != bytes_per_frame) {
if (dst.meta.wi != g_bytes_per_frame) {
return "unexpected number of bytes decoded";
}
return NULL;
@@ -305,11 +307,11 @@ decode_once(bool frag_dst, bool frag_idat) {
const char* //
decode(bool frag_dst, bool frag_idat) {
int reps;
if (bytes_per_frame < 100000) {
if (g_bytes_per_frame < 100000) {
reps = 1000;
} else if (bytes_per_frame < 1000000) {
} else if (g_bytes_per_frame < 1000000) {
reps = 100;
} else if (bytes_per_frame < 10000000) {
} else if (g_bytes_per_frame < 10000000) {
reps = 10;
} else {
reps = 1;
@@ -339,7 +341,7 @@ decode(bool frag_dst, bool frag_idat) {
printf("Benchmark%sDst%sIDAT/%s\t%8d\t%8" PRIu64 " ns/op\n",
frag_dst ? "Frag" : "Full", //
frag_idat ? "Frag" : "Full", //
cc, reps, nanos / reps);
g_cc, reps, nanos / reps);
return NULL;
}
@@ -358,28 +360,28 @@ main(int argc, char** argv) {
if (msg) {
return fail(msg);
}
if ((src_len < 8) ||
strncmp((const char*)(src_buffer_array), "\x89PNG\x0D\x0A\x1A\x0A", 8)) {
if ((g_src_len < 8) || strncmp((const char*)(g_src_buffer_array),
"\x89PNG\x0D\x0A\x1A\x0A", 8)) {
return fail("invalid PNG");
}
msg = process_png_chunks(src_buffer_array + 8, src_len - 8);
msg = process_png_chunks(g_src_buffer_array + 8, g_src_len - 8);
if (msg) {
return fail(msg);
}
if (bytes_per_pixel == 0) {
if (g_bytes_per_pixel == 0) {
return fail("missing PNG IHDR chunk");
}
if (num_idat_chunks == 0) {
if (g_num_idat_chunks == 0) {
return fail("missing PNG IDAT chunk");
}
// The +1 here is for the per-row filter byte.
bytes_per_row = (uint64_t)width * bytes_per_pixel + 1;
bytes_per_frame = (uint64_t)height * bytes_per_row;
if (bytes_per_frame > DST_BUFFER_ARRAY_SIZE) {
g_bytes_per_row = (uint64_t)g_width * g_bytes_per_pixel + 1;
g_bytes_per_frame = (uint64_t)g_height * g_bytes_per_row;
if (g_bytes_per_frame > DST_BUFFER_ARRAY_SIZE) {
return fail("decompressed data is too large");
}
printf("# %s version %s\n#\n", cc, cc_version);
printf("# %s version %s\n#\n", g_cc, g_cc_version);
printf(
"# The output format, including the \"Benchmark\" prefixes, is "
"compatible with the\n"