diff options
author | Derek Schuff <dschuff@chromium.org> | 2016-05-12 14:55:47 -0700 |
---|---|---|
committer | Derek Schuff <dschuff@chromium.org> | 2016-05-12 14:55:47 -0700 |
commit | d84fd5be60d4ead6bc9fbb3f27a710bef0c688c8 (patch) | |
tree | 54bb55ccb64d775b12f9b97bf14019506c607050 /src/wasm.h | |
parent | 87c583fb258bd373219fdfae3fa181c2d10b56b7 (diff) | |
download | binaryen-d84fd5be60d4ead6bc9fbb3f27a710bef0c688c8.tar.gz binaryen-d84fd5be60d4ead6bc9fbb3f27a710bef0c688c8.tar.bz2 binaryen-d84fd5be60d4ead6bc9fbb3f27a710bef0c688c8.zip |
Use a class with implicit overflow checks for Address (#486)
It includes implicit conversion from u64 and implicit conversion to address_t. This makes it easier to use without ugly casting and but still gets the overflow checks.
Diffstat (limited to 'src/wasm.h')
-rw-r--r-- | src/wasm.h | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/src/wasm.h b/src/wasm.h index 9f5f6820c..327be9c7e 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -92,7 +92,21 @@ struct Name : public cashew::IString { typedef uint32_t Index; // An address in linear memory. For now only wasm32 -typedef uint32_t Address; +struct Address { + typedef uint32_t address_t; + address_t addr; + Address() : addr(0) {} + Address(uint64_t a) : addr(static_cast<address_t>(a)) { + assert(a <= std::numeric_limits<address_t>::max()); + } + Address& operator=(uint64_t a) { + assert(a <= std::numeric_limits<address_t>::max()); + addr = static_cast<address_t>(a); + return *this; + } + operator address_t() const { return addr; } + Address& operator++() { ++addr; return *this; } +}; // Types @@ -1266,8 +1280,8 @@ public: class Memory { public: - static const Address kPageSize = 64 * 1024; - static const Address kPageMask = ~(kPageSize - 1); + static const Address::address_t kPageSize = 64 * 1024; + static const Address::address_t kPageMask = ~(kPageSize - 1); struct Segment { Address offset; std::vector<char> data; // TODO: optimize @@ -1285,7 +1299,7 @@ public: std::vector<Segment> segments; Name exportName; - Memory() : initial(0), max((Address)-1) {} + Memory() : initial(0), max(-1U) {} }; class Module { @@ -1387,4 +1401,12 @@ private: } // namespace wasm +namespace std { +template<> struct hash<wasm::Address> { + size_t operator()(const wasm::Address a) const { + return std::hash<uint32_t>()(a.addr); + } +}; +} + #endif // wasm_wasm_h |