diff options
author | alon@honor <none@none> | 2010-09-07 19:23:00 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-09-07 19:23:00 -0700 |
commit | 49042c3b99fea528354d794a0383cbefed299c3e (patch) | |
tree | 0691946a6bf1d8becc74542c9468bf96240829c5 /src | |
parent | 883237a187d1097987b26c6ba10bbeea6d8c03e5 (diff) |
ES_SIZEOF: safe and portable sizeof
Diffstat (limited to 'src')
-rw-r--r-- | src/include/emscripten.h | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/include/emscripten.h b/src/include/emscripten.h new file mode 100644 index 00000000..99cd40e3 --- /dev/null +++ b/src/include/emscripten.h @@ -0,0 +1,32 @@ +/** + * This file contains a few useful things for compiling C/C++ code + * with Emscripten, an LLVM-to-JavaScript compiler. + * + * The code can be used permissively under the MIT license. + * + * http://emscripten.org + */ + +// ES_SIZEOF +// +// A 'safe' sizeof operator. Sadly llvm-gcc calculates sizeof's +// and stores them hardcoded as raw values, unlike say offsets +// within a structure which it nicely details using getelementptr. +// +// You should always use ES_SIZEOF instead of sizeof when using +// Emscripten. +// +// Note that this only works with types, not values. +// +// Sadly +// #define ES_SIZEOF(x) int(&((x*)(NULL))[1]) +// does not work, since the compiler parses and hard-codes the +// value. So we need to trick it with a function call. +#ifdef EMSCRIPTEN + template<class T> + int es_sizeof(T* x) { return int(&x[1]); } + #define ES_SIZEOF(T) es_sizeof((T*)NULL) +#else + #define ES_SIZEOF(T) sizeof(T) +#endif + |