Fix MinGW-related portability issues.

Create and use FMT* macros that are equivalent to the PRI* macros that
inttypes.h defines.  This allows uniform use of the Unix-specific format
specifiers, e.g. "%zu", as well as avoiding Windows-specific definitions
of e.g. PRIu64.

Add ffs()/ffsl() support for compiling with gcc.

Extract compatibility definitions of ENOENT, EINVAL, EAGAIN, EPERM,
ENOMEM, and ENORANGE into include/msvc_compat/windows_extra.h and
use the file for tests as well as for core jemalloc code.
This commit is contained in:
Jason Evans
2015-07-23 13:56:25 -07:00
parent e475ff1600
commit 5fae7dc1b3
18 changed files with 224 additions and 494 deletions

View File

@@ -6,13 +6,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <string.h>
#ifdef _WIN32
# include "msvc_compat/strings.h"
#endif
#include <sys/time.h>
#ifdef _WIN32
# include <windows.h>
# include "msvc_compat/windows_extra.h"
#else
# include <pthread.h>
#endif

View File

@@ -133,82 +133,82 @@
<=, "ju", __VA_ARGS__)
#define assert_zd_eq(a, b, ...) assert_cmp(ssize_t, a, b, ==, \
!=, PRIzd, __VA_ARGS__)
!=, "zd", __VA_ARGS__)
#define assert_zd_ne(a, b, ...) assert_cmp(ssize_t, a, b, !=, \
==, PRIzd, __VA_ARGS__)
==, "zd", __VA_ARGS__)
#define assert_zd_lt(a, b, ...) assert_cmp(ssize_t, a, b, <, \
>=, PRIzd, __VA_ARGS__)
>=, "zd", __VA_ARGS__)
#define assert_zd_le(a, b, ...) assert_cmp(ssize_t, a, b, <=, \
>, PRIzd, __VA_ARGS__)
>, "zd", __VA_ARGS__)
#define assert_zd_ge(a, b, ...) assert_cmp(ssize_t, a, b, >=, \
<, PRIzd, __VA_ARGS__)
<, "zd", __VA_ARGS__)
#define assert_zd_gt(a, b, ...) assert_cmp(ssize_t, a, b, >, \
<=, PRIzd, __VA_ARGS__)
<=, "zd", __VA_ARGS__)
#define assert_zu_eq(a, b, ...) assert_cmp(size_t, a, b, ==, \
!=, PRIzu, __VA_ARGS__)
!=, "zu", __VA_ARGS__)
#define assert_zu_ne(a, b, ...) assert_cmp(size_t, a, b, !=, \
==, PRIzu, __VA_ARGS__)
==, "zu", __VA_ARGS__)
#define assert_zu_lt(a, b, ...) assert_cmp(size_t, a, b, <, \
>=, PRIzu, __VA_ARGS__)
>=, "zu", __VA_ARGS__)
#define assert_zu_le(a, b, ...) assert_cmp(size_t, a, b, <=, \
>, PRIzu, __VA_ARGS__)
>, "zu", __VA_ARGS__)
#define assert_zu_ge(a, b, ...) assert_cmp(size_t, a, b, >=, \
<, PRIzu, __VA_ARGS__)
<, "zu", __VA_ARGS__)
#define assert_zu_gt(a, b, ...) assert_cmp(size_t, a, b, >, \
<=, PRIzu, __VA_ARGS__)
<=, "zu", __VA_ARGS__)
#define assert_d32_eq(a, b, ...) assert_cmp(int32_t, a, b, ==, \
!=, PRId32, __VA_ARGS__)
!=, FMTd32, __VA_ARGS__)
#define assert_d32_ne(a, b, ...) assert_cmp(int32_t, a, b, !=, \
==, PRId32, __VA_ARGS__)
==, FMTd32, __VA_ARGS__)
#define assert_d32_lt(a, b, ...) assert_cmp(int32_t, a, b, <, \
>=, PRId32, __VA_ARGS__)
>=, FMTd32, __VA_ARGS__)
#define assert_d32_le(a, b, ...) assert_cmp(int32_t, a, b, <=, \
>, PRId32, __VA_ARGS__)
>, FMTd32, __VA_ARGS__)
#define assert_d32_ge(a, b, ...) assert_cmp(int32_t, a, b, >=, \
<, PRId32, __VA_ARGS__)
<, FMTd32, __VA_ARGS__)
#define assert_d32_gt(a, b, ...) assert_cmp(int32_t, a, b, >, \
<=, PRId32, __VA_ARGS__)
<=, FMTd32, __VA_ARGS__)
#define assert_u32_eq(a, b, ...) assert_cmp(uint32_t, a, b, ==, \
!=, PRIu32, __VA_ARGS__)
!=, FMTu32, __VA_ARGS__)
#define assert_u32_ne(a, b, ...) assert_cmp(uint32_t, a, b, !=, \
==, PRIu32, __VA_ARGS__)
==, FMTu32, __VA_ARGS__)
#define assert_u32_lt(a, b, ...) assert_cmp(uint32_t, a, b, <, \
>=, PRIu32, __VA_ARGS__)
>=, FMTu32, __VA_ARGS__)
#define assert_u32_le(a, b, ...) assert_cmp(uint32_t, a, b, <=, \
>, PRIu32, __VA_ARGS__)
>, FMTu32, __VA_ARGS__)
#define assert_u32_ge(a, b, ...) assert_cmp(uint32_t, a, b, >=, \
<, PRIu32, __VA_ARGS__)
<, FMTu32, __VA_ARGS__)
#define assert_u32_gt(a, b, ...) assert_cmp(uint32_t, a, b, >, \
<=, PRIu32, __VA_ARGS__)
<=, FMTu32, __VA_ARGS__)
#define assert_d64_eq(a, b, ...) assert_cmp(int64_t, a, b, ==, \
!=, PRId64, __VA_ARGS__)
!=, FMTd64, __VA_ARGS__)
#define assert_d64_ne(a, b, ...) assert_cmp(int64_t, a, b, !=, \
==, PRId64, __VA_ARGS__)
==, FMTd64, __VA_ARGS__)
#define assert_d64_lt(a, b, ...) assert_cmp(int64_t, a, b, <, \
>=, PRId64, __VA_ARGS__)
>=, FMTd64, __VA_ARGS__)
#define assert_d64_le(a, b, ...) assert_cmp(int64_t, a, b, <=, \
>, PRId64, __VA_ARGS__)
>, FMTd64, __VA_ARGS__)
#define assert_d64_ge(a, b, ...) assert_cmp(int64_t, a, b, >=, \
<, PRId64, __VA_ARGS__)
<, FMTd64, __VA_ARGS__)
#define assert_d64_gt(a, b, ...) assert_cmp(int64_t, a, b, >, \
<=, PRId64, __VA_ARGS__)
<=, FMTd64, __VA_ARGS__)
#define assert_u64_eq(a, b, ...) assert_cmp(uint64_t, a, b, ==, \
!=, PRIu64, __VA_ARGS__)
!=, FMTu64, __VA_ARGS__)
#define assert_u64_ne(a, b, ...) assert_cmp(uint64_t, a, b, !=, \
==, PRIu64, __VA_ARGS__)
==, FMTu64, __VA_ARGS__)
#define assert_u64_lt(a, b, ...) assert_cmp(uint64_t, a, b, <, \
>=, PRIu64, __VA_ARGS__)
>=, FMTu64, __VA_ARGS__)
#define assert_u64_le(a, b, ...) assert_cmp(uint64_t, a, b, <=, \
>, PRIu64, __VA_ARGS__)
>, FMTu64, __VA_ARGS__)
#define assert_u64_ge(a, b, ...) assert_cmp(uint64_t, a, b, >=, \
<, PRIu64, __VA_ARGS__)
<, FMTu64, __VA_ARGS__)
#define assert_u64_gt(a, b, ...) assert_cmp(uint64_t, a, b, >, \
<=, PRIu64, __VA_ARGS__)
<=, FMTu64, __VA_ARGS__)
#define assert_b_eq(a, b, ...) do { \
bool a_ = (a); \

View File

@@ -61,7 +61,7 @@ timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen)
int n;
/* Whole. */
n = malloc_snprintf(&buf[i], buflen-i, "%"PRIu64, t0 / t1);
n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1);
i += n;
if (i >= buflen)
return;
@@ -78,7 +78,7 @@ timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen)
uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10
>= 5)) ? 1 : 0;
n = malloc_snprintf(&buf[i], buflen-i,
"%"PRIu64, (t0 * mult / t1) % 10 + round);
"%"FMTu64, (t0 * mult / t1) % 10 + round);
i += n;
mult *= 10;
}

View File

@@ -31,8 +31,8 @@ compare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,
time_func(&timer_b, nwarmup, niter, func_b);
timer_ratio(&timer_a, &timer_b, ratio_buf, sizeof(ratio_buf));
malloc_printf("%"PRIu64" iterations, %s=%"PRIu64"us, "
"%s=%"PRIu64"us, ratio=1:%s\n",
malloc_printf("%"FMTu64" iterations, %s=%"FMTu64"us, "
"%s=%"FMTu64"us, ratio=1:%s\n",
niter, name_a, timer_usec(&timer_a), name_b, timer_usec(&timer_b),
ratio_buf);

View File

@@ -1543,13 +1543,13 @@ TEST_BEGIN(test_gen_rand_64)
}
r = gen_rand64(ctx);
assert_u64_eq(r, array64[i],
"Mismatch at array64[%d]=%"PRIx64", gen=%"PRIx64, i,
"Mismatch at array64[%d]=%"FMTx64", gen=%"FMTx64, i,
array64[i], r);
}
for (i = 0; i < COUNT_2; i++) {
r = gen_rand64(ctx);
assert_u64_eq(r, array64_2[i],
"Mismatch at array64_2[%d]=%"PRIx64" gen=%"PRIx64"", i,
"Mismatch at array64_2[%d]=%"FMTx64" gen=%"FMTx64"", i,
array64_2[i], r);
}
fini_gen_rand(ctx);
@@ -1580,13 +1580,13 @@ TEST_BEGIN(test_by_array_64)
}
r = gen_rand64(ctx);
assert_u64_eq(r, array64[i],
"Mismatch at array64[%d]=%"PRIx64" gen=%"PRIx64, i,
"Mismatch at array64[%d]=%"FMTx64" gen=%"FMTx64, i,
array64[i], r);
}
for (i = 0; i < COUNT_2; i++) {
r = gen_rand64(ctx);
assert_u64_eq(r, array64_2[i],
"Mismatch at array64_2[%d]=%"PRIx64" gen=%"PRIx64, i,
"Mismatch at array64_2[%d]=%"FMTx64" gen=%"FMTx64, i,
array64_2[i], r);
}
fini_gen_rand(ctx);

View File

@@ -8,7 +8,7 @@ struct p##_test_s { \
}; \
typedef struct p##_test_s p##_test_t;
#define TEST_BODY(p, t, tc, ta, PRI) do { \
#define TEST_BODY(p, t, tc, ta, FMT) do { \
const p##_test_t tests[] = { \
{(t)-1, (t)-1, (t)-2}, \
{(t)-1, (t) 0, (t)-2}, \
@@ -38,7 +38,7 @@ typedef struct p##_test_s p##_test_t;
\
assert_##ta##_eq(atomic_add_##p(&accum, tests[i].x), \
(t)((tc)tests[i].accum0 + (tc)tests[i].x), \
"i=%u, accum=%"PRI", x=%"PRI, \
"i=%u, accum=%"FMT", x=%"FMT, \
i, tests[i].accum0, tests[i].x); \
assert_##ta##_eq(atomic_read_##p(&accum), accum, \
"Erroneous add, i=%u", i); \
@@ -46,7 +46,7 @@ typedef struct p##_test_s p##_test_t;
accum = tests[i].accum0; \
assert_##ta##_eq(atomic_sub_##p(&accum, tests[i].x), \
(t)((tc)tests[i].accum0 - (tc)tests[i].x), \
"i=%u, accum=%"PRI", x=%"PRI, \
"i=%u, accum=%"FMT", x=%"FMT, \
i, tests[i].accum0, tests[i].x); \
assert_##ta##_eq(atomic_read_##p(&accum), accum, \
"Erroneous sub, i=%u", i); \
@@ -72,7 +72,7 @@ TEST_BEGIN(test_atomic_uint64)
#if !(LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)
test_skip("64-bit atomic operations not supported");
#else
TEST_BODY(uint64, uint64_t, uint64_t, u64, PRIx64);
TEST_BODY(uint64, uint64_t, uint64_t, u64, FMTx64);
#endif
}
TEST_END
@@ -81,7 +81,7 @@ TEST_STRUCT(uint32, uint32_t)
TEST_BEGIN(test_atomic_uint32)
{
TEST_BODY(uint32, uint32_t, uint32_t, u32, "#"PRIx32);
TEST_BODY(uint32, uint32_t, uint32_t, u32, "#"FMTx32);
}
TEST_END
@@ -97,7 +97,7 @@ TEST_STRUCT(z, size_t)
TEST_BEGIN(test_atomic_z)
{
TEST_BODY(z, size_t, size_t, zu, "#"PRIzx);
TEST_BODY(z, size_t, size_t, zu, "#zx");
}
TEST_END

View File

@@ -35,15 +35,15 @@ TEST_BEGIN(test_count_insert_search_remove)
assert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash, ckh_string_keycomp),
"Unexpected ckh_new() error");
assert_zu_eq(ckh_count(&ckh), 0,
"ckh_count() should return %"PRIzu", but it returned %"PRIzu, ZU(0),
"ckh_count() should return %zu, but it returned %zu", ZU(0),
ckh_count(&ckh));
/* Insert. */
for (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {
ckh_insert(tsd, &ckh, strs[i], strs[i]);
assert_zu_eq(ckh_count(&ckh), i+1,
"ckh_count() should return %"PRIzu", but it returned "
"%"PRIzu, i+1, ckh_count(&ckh));
"ckh_count() should return %zu, but it returned %zu", i+1,
ckh_count(&ckh));
}
/* Search. */
@@ -64,10 +64,10 @@ TEST_BEGIN(test_count_insert_search_remove)
ks = (i & 1) ? strs[i] : (const char *)NULL;
vs = (i & 2) ? strs[i] : (const char *)NULL;
assert_ptr_eq((void *)ks, (void *)k.s,
"Key mismatch, i=%"PRIzu, i);
assert_ptr_eq((void *)vs, (void *)v.s,
"Value mismatch, i=%"PRIzu, i);
assert_ptr_eq((void *)ks, (void *)k.s, "Key mismatch, i=%zu",
i);
assert_ptr_eq((void *)vs, (void *)v.s, "Value mismatch, i=%zu",
i);
}
assert_true(ckh_search(&ckh, missing, NULL, NULL),
"Unexpected ckh_search() success");
@@ -90,14 +90,14 @@ TEST_BEGIN(test_count_insert_search_remove)
ks = (i & 1) ? strs[i] : (const char *)NULL;
vs = (i & 2) ? strs[i] : (const char *)NULL;
assert_ptr_eq((void *)ks, (void *)k.s,
"Key mismatch, i=%"PRIzu, i);
assert_ptr_eq((void *)vs, (void *)v.s,
"Value mismatch, i=%"PRIzu, i);
assert_ptr_eq((void *)ks, (void *)k.s, "Key mismatch, i=%zu",
i);
assert_ptr_eq((void *)vs, (void *)v.s, "Value mismatch, i=%zu",
i);
assert_zu_eq(ckh_count(&ckh),
sizeof(strs)/sizeof(const char *) - i - 1,
"ckh_count() should return %"PRIzu", but it returned "
"%"PRIzu, sizeof(strs)/sizeof(const char *) - i - 1,
"ckh_count() should return %zu, but it returned %zu",
sizeof(strs)/sizeof(const char *) - i - 1,
ckh_count(&ckh));
}
@@ -137,8 +137,8 @@ TEST_BEGIN(test_insert_iter_remove)
}
assert_zu_eq(ckh_count(&ckh), NITEMS,
"ckh_count() should return %"PRIzu", but it returned "
"%"PRIzu, NITEMS, ckh_count(&ckh));
"ckh_count() should return %zu, but it returned %zu",
NITEMS, ckh_count(&ckh));
for (j = i + 1; j < NITEMS; j++) {
assert_false(ckh_search(&ckh, p[j], NULL, NULL),
@@ -167,20 +167,17 @@ TEST_BEGIN(test_insert_iter_remove)
for (k = 0; k < NITEMS; k++) {
if (p[k] == q) {
assert_false(seen[k],
"Item %"PRIzu" already "
"seen", k);
"Item %zu already seen", k);
seen[k] = true;
break;
}
}
}
for (j = 0; j < i + 1; j++) {
assert_true(seen[j], "Item %"PRIzu" not seen",
j);
}
for (j = 0; j < i + 1; j++)
assert_true(seen[j], "Item %zu not seen", j);
for (; j < NITEMS; j++)
assert_false(seen[j], "Item %"PRIzu" seen", j);
assert_false(seen[j], "Item %zu seen", j);
}
}
@@ -199,7 +196,7 @@ TEST_BEGIN(test_insert_iter_remove)
}
assert_zu_eq(ckh_count(&ckh), 0,
"ckh_count() should return %"PRIzu", but it returned %"PRIzu,
"ckh_count() should return %zu, but it returned %zu",
ZU(0), ckh_count(&ckh));
ckh_delete(tsd, &ckh);
#undef NITEMS

View File

@@ -30,8 +30,8 @@ arena_dalloc_junk_small_intercept(void *ptr, arena_bin_info_t *bin_info)
arena_dalloc_junk_small_orig(ptr, bin_info);
for (i = 0; i < bin_info->reg_size; i++) {
assert_c_eq(((char *)ptr)[i], 0x5a,
"Missing junk fill for byte %"PRIzu"/%"PRIzu" of "
"deallocated region", i, bin_info->reg_size);
"Missing junk fill for byte %zu/%zu of deallocated region",
i, bin_info->reg_size);
}
if (ptr == watch_for_junking)
saw_junking = true;
@@ -45,8 +45,8 @@ arena_dalloc_junk_large_intercept(void *ptr, size_t usize)
arena_dalloc_junk_large_orig(ptr, usize);
for (i = 0; i < usize; i++) {
assert_c_eq(((char *)ptr)[i], 0x5a,
"Missing junk fill for byte %"PRIzu"/%"PRIzu" of "
"deallocated region", i, usize);
"Missing junk fill for byte %zu/%zu of deallocated region",
i, usize);
}
if (ptr == watch_for_junking)
saw_junking = true;
@@ -89,18 +89,18 @@ test_junk(size_t sz_min, size_t sz_max)
sz_prev = sz, sz = sallocx(s, 0)) {
if (sz_prev > 0) {
assert_c_eq(s[0], 'a',
"Previously allocated byte %"PRIzu"/%"PRIzu" is "
"corrupted", ZU(0), sz_prev);
"Previously allocated byte %zu/%zu is corrupted",
ZU(0), sz_prev);
assert_c_eq(s[sz_prev-1], 'a',
"Previously allocated byte %"PRIzu"/%"PRIzu" is "
"corrupted", sz_prev-1, sz_prev);
"Previously allocated byte %zu/%zu is corrupted",
sz_prev-1, sz_prev);
}
for (i = sz_prev; i < sz; i++) {
if (opt_junk_alloc) {
assert_c_eq(s[i], 0xa5,
"Newly allocated byte %"PRIzu"/%"PRIzu
" isn't junk-filled", i, sz);
"Newly allocated byte %zu/%zu isn't "
"junk-filled", i, sz);
}
s[i] = 'a';
}
@@ -111,15 +111,15 @@ test_junk(size_t sz_min, size_t sz_max)
assert_ptr_not_null((void *)s,
"Unexpected rallocx() failure");
assert_true(!opt_junk_free || saw_junking,
"Expected region of size %"PRIzu" to be "
"junk-filled", sz);
"Expected region of size %zu to be junk-filled",
sz);
}
}
watch_junking(s);
dallocx(s, 0);
assert_true(!opt_junk_free || saw_junking,
"Expected region of size %"PRIzu" to be junk-filled", sz);
"Expected region of size %zu to be junk-filled", sz);
if (opt_junk_free) {
arena_dalloc_junk_small = arena_dalloc_junk_small_orig;

View File

@@ -72,7 +72,7 @@ TEST_BEGIN(test_rtree_bits)
&node, "rtree_get() should return "
"previously set value and ignore "
"insignificant key bits; i=%u, j=%u, k=%u, "
"set key=%#"PRIxPTR", get key=%#"PRIxPTR, i,
"set key=%#"FMTxPTR", get key=%#"FMTxPTR, i,
j, k, keys[j], keys[k]);
}
assert_ptr_null(rtree_get(&rtree,