From 2cff15d35e4ff862e6a0811ae9e509c3d3352514 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Mon, 18 May 2015 11:37:03 -0700 Subject: [PATCH] Updates from code review comments. --- tests/test_table.cc | 3 +-- upb/refcounted.c | 12 ++++++++++++ upb/table.int.h | 8 ++++++-- upb/upb.h | 6 ++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/test_table.cc b/tests/test_table.cc index 30c5c941af..c70ef08a87 100644 --- a/tests/test_table.cc +++ b/tests/test_table.cc @@ -62,8 +62,7 @@ void test_strtable(const vector& keys, uint32_t num_to_insert) { upb_strtable_next(&iter)) { const char *key = upb_strtable_iter_key(&iter); std::string tmp(key, strlen(key)); - std::string tmp2(key, upb_strtable_iter_keylength(&iter)); - ASSERT(tmp == tmp2); + ASSERT(strlen(key) == upb_strtable_iter_keylength(&iter)); std::set::iterator i = all.find(tmp); ASSERT(i != all.end()); all.erase(i); diff --git a/upb/refcounted.c b/upb/refcounted.c index 40e6e89ae4..fa775ab118 100644 --- a/upb/refcounted.c +++ b/upb/refcounted.c @@ -726,6 +726,18 @@ static void freeobj(upb_refcounted *o) { bool upb_refcounted_init(upb_refcounted *r, const struct upb_refcounted_vtbl *vtbl, const void *owner) { +#ifndef NDEBUG + // Endianness check. This is unrelated to upb_refcounted, it's just a + // convenient place to put the check that we can be assured will run for + // basically every program using upb. + const int x = 1; +#ifdef UPB_BIG_ENDIAN + assert(*(char*)&x != 1); +#else + assert(*(char*)&x == 1); +#endif +#endif + r->next = r; r->vtbl = vtbl; r->individual_count = 0; diff --git a/upb/table.int.h b/upb/table.int.h index 0a246d5555..b6e8eb70a3 100644 --- a/upb/table.int.h +++ b/upb/table.int.h @@ -158,10 +158,14 @@ FUNCS(fptr, fptr, upb_func*, UPB_CTYPE_FPTR); // length into a byte-wise string representation, so code generation needs to // help it along. // -// "len1" is the low byte and len4 is the high byte. For big endian we'll need -// to define a version of this that flips it around. +// "len1" is the low byte and len4 is the high byte. +#ifdef UPB_BIG_ENDIAN +#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ + (uintptr_t)(len4 len3 len2 len1 strval) +#else #define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ (uintptr_t)(len1 len2 len3 len4 strval) +#endif // Either: // 1. an actual integer key, or diff --git a/upb/upb.h b/upb/upb.h index 13efaede15..b62ac365c4 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -25,6 +25,12 @@ #define UPB_INLINE static inline #endif +// Define this manually if you're on big endian and your compiler doesn't +// provide these preprocessor symbols. +#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#define UPB_BIG_ENDIAN +#endif + // For use in C/C++ source files (not headers), forces inlining within the file. #ifdef __GNUC__ #define UPB_FORCEINLINE inline __attribute__((always_inline))