statically initialize

pull/431/head
Vladislav 2017-03-08 21:16:50 +05:00 committed by GitHub
parent 6f6e11f85f
commit 667f35578b
1 changed files with 29 additions and 5 deletions

View File

@ -3680,7 +3680,7 @@ stbi_inline static int stbi__bit_reverse(int v, int bits)
return stbi__bitreverse16(v) >> (16-bits); return stbi__bitreverse16(v) >> (16-bits);
} }
static int stbi__zbuild_huffman(stbi__zhuffman *z, stbi_uc *sizelist, int num) static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num)
{ {
int i,k=0; int i,k=0;
int code, next_code[16], sizes[17]; int code, next_code[16], sizes[17];
@ -3970,9 +3970,33 @@ static int stbi__parse_zlib_header(stbi__zbuf *a)
return 1; return 1;
} }
// @TODO: should statically initialize these for optimal thread safety static const stbi_uc stbi__zdefault_length[288] = {
static stbi_uc stbi__zdefault_length[288], stbi__zdefault_distance[32]; 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
static void stbi__init_zdefaults(void) 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8
};
static const stbi_uc stbi__zdefault_distance[32] = {
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
};
/*
Init algorithm:
{ {
int i; // use <= to match clearly with spec int i; // use <= to match clearly with spec
for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8; for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
@ -3982,6 +4006,7 @@ static void stbi__init_zdefaults(void)
for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5; for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
} }
*/
static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
{ {
@ -4000,7 +4025,6 @@ static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
} else { } else {
if (type == 1) { if (type == 1) {
// use fixed code lengths // use fixed code lengths
if (!stbi__zdefault_distance[31]) stbi__init_zdefaults();
if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0; if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0;
if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0; if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
} else { } else {