From e2840a4aa1b6a7a2ca1421d0d6da3e56992e5090 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sun, 17 May 2015 16:11:07 -0700 Subject: Restructure tables for C89 port and smaller size. Changes the data layout of tables slightly so that string keys are prefixed with their size, rather than the size being inline in the table itself. This has a few benefits: 1. inttables shrink a bit, because there is no longer a wasted and unused size field sitting in them. 2. This avoids the need to have a union in the table. This is important for an impending C89 port of upb, since C89 has literally no way of statically initializing a non-first union member. --- tests/test_table.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/test_table.cc b/tests/test_table.cc index 9cc98b1..30c5c94 100644 --- a/tests/test_table.cc +++ b/tests/test_table.cc @@ -62,6 +62,8 @@ 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); std::set::iterator i = all.find(tmp); ASSERT(i != all.end()); all.erase(i); -- cgit v1.2.3 From 2cff15d35e4ff862e6a0811ae9e509c3d3352514 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Mon, 18 May 2015 11:37:03 -0700 Subject: 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(-) (limited to 'tests') diff --git a/tests/test_table.cc b/tests/test_table.cc index 30c5c94..c70ef08 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 40e6e89..fa775ab 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 0a246d5..b6e8eb7 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 13efaed..b62ac36 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)) -- cgit v1.2.3