From b67dc01d1d52a9d12a1b9c807f6f89dd8490131a Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 22 Apr 2021 09:48:54 -0700 Subject: [PATCH] Replace int -> int32_t Summary: This diff replaces all usages of int by int32_t. This is to ensure we always use a fixed size for int that matches what's expected on Java. changelog: [internal] internal Reviewed By: sammy-SC Differential Revision: D27915608 fbshipit-source-id: 634c45796dda1d4434c3ad6ff3e199931c22940b --- .../common/mapbuffer/ReadableMapBuffer.h | 2 +- .../react/renderer/mapbuffer/MapBuffer.cpp | 22 +++++------ .../react/renderer/mapbuffer/MapBuffer.h | 10 ++--- .../renderer/mapbuffer/MapBufferBuilder.cpp | 37 ++++++++++--------- .../renderer/mapbuffer/MapBufferBuilder.h | 14 +++---- .../react/renderer/mapbuffer/primitives.h | 37 +++++++++---------- 6 files changed, 62 insertions(+), 60 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/jni/react/common/mapbuffer/ReadableMapBuffer.h b/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/jni/react/common/mapbuffer/ReadableMapBuffer.h index 6a231bd29be..b3f753a67b1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/jni/react/common/mapbuffer/ReadableMapBuffer.h +++ b/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/jni/react/common/mapbuffer/ReadableMapBuffer.h @@ -34,7 +34,7 @@ class ReadableMapBuffer : public jni::HybridClass { private: uint8_t *serializedData_ = nullptr; - int serializedDataSize_ = 0; + int32_t serializedDataSize_ = 0; friend HybridBase; diff --git a/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp b/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp index d0cf0c5812b..80e76dfb4f2 100644 --- a/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp +++ b/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp @@ -14,7 +14,7 @@ namespace react { // TODO T83483191: Extend MapBuffer C++ implementation to support basic random // access -MapBuffer::MapBuffer(uint8_t *const data, int dataSize) { +MapBuffer::MapBuffer(uint8_t *const data, int32_t dataSize) { react_native_assert( (data != nullptr) && "Error trying to build an invalid MapBuffer"); @@ -42,8 +42,8 @@ MapBuffer::MapBuffer(uint8_t *const data, int dataSize) { } } -int MapBuffer::getInt(Key key) const { - int value = 0; +int32_t MapBuffer::getInt(Key key) const { + int32_t value = 0; memcpy( reinterpret_cast(&value), reinterpret_cast(data_ + getValueOffset(key)), @@ -66,7 +66,7 @@ double MapBuffer::getDouble(Key key) const { return value; } -int MapBuffer::getDynamicDataOffset() const { +int32_t MapBuffer::getDynamicDataOffset() const { // The begininig of dynamic data can be calculated as the offset of the next // key in the map return getKeyOffset(count_); @@ -75,9 +75,9 @@ int MapBuffer::getDynamicDataOffset() const { std::string MapBuffer::getString(Key key) const { // TODO T83483191:Add checks to verify that offsets are under the boundaries // of the map buffer - int dynamicDataOffset = getDynamicDataOffset(); - int stringLength = 0; - int offset = getInt(key); + int32_t dynamicDataOffset = getDynamicDataOffset(); + int32_t stringLength = 0; + int32_t offset = getInt(key); memcpy( reinterpret_cast(&stringLength), reinterpret_cast(data_ + dynamicDataOffset + offset), @@ -97,10 +97,10 @@ std::string MapBuffer::getString(Key key) const { MapBuffer MapBuffer::getMapBuffer(Key key) const { // TODO T83483191: Add checks to verify that offsets are under the boundaries // of the map buffer - int dynamicDataOffset = getDynamicDataOffset(); + int32_t dynamicDataOffset = getDynamicDataOffset(); - int mapBufferLength = 0; - int offset = getInt(key); + int32_t mapBufferLength = 0; + int32_t offset = getInt(key); memcpy( reinterpret_cast(&mapBufferLength), reinterpret_cast(data_ + dynamicDataOffset + offset), @@ -121,7 +121,7 @@ bool MapBuffer::isNull(Key key) const { return getInt(key) == NULL_VALUE; } -int MapBuffer::getBufferSize() const { +int32_t MapBuffer::getBufferSize() const { return dataSize_; } diff --git a/ReactCommon/react/renderer/mapbuffer/MapBuffer.h b/ReactCommon/react/renderer/mapbuffer/MapBuffer.h index fdbe85f8957..b3f0c3051f1 100644 --- a/ReactCommon/react/renderer/mapbuffer/MapBuffer.h +++ b/ReactCommon/react/renderer/mapbuffer/MapBuffer.h @@ -37,20 +37,20 @@ class MapBuffer { const uint8_t *data_ = nullptr; // amount of bytes in the MapBuffer - int dataSize_ = 0; + int32_t dataSize_ = 0; // amount of items in the MapBuffer uint16_t count_ = 0; // returns the relative offset of the first byte of dynamic data - int getDynamicDataOffset() const; + int32_t getDynamicDataOffset() const; public: - MapBuffer(uint8_t *const data, int dataSize); + MapBuffer(uint8_t *const data, int32_t dataSize); ~MapBuffer(); - int getInt(Key key) const; + int32_t getInt(Key key) const; bool getBool(Key key) const; @@ -61,7 +61,7 @@ class MapBuffer { // TODO T83483191: review this declaration MapBuffer getMapBuffer(Key key) const; - int getBufferSize() const; + int32_t getBufferSize() const; // TODO T83483191: review parameters of copy method void copy(uint8_t *output) const; diff --git a/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp b/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp index 16b6cf80d1a..54a7fa03bf8 100644 --- a/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp +++ b/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp @@ -33,7 +33,7 @@ MapBufferBuilder::MapBufferBuilder(uint16_t initialSize) { } void MapBufferBuilder::ensureKeyValueSpace() { - int oldKeyValuesSize = keyValuesSize_; + int32_t oldKeyValuesSize = keyValuesSize_; react_native_assert( (keyValuesSize_ < std::numeric_limits::max() / 2) && "Error trying to assign a value beyond the capacity of uint16_t: "); @@ -45,7 +45,10 @@ void MapBufferBuilder::ensureKeyValueSpace() { delete[] oldKeyValues; } -void MapBufferBuilder::storeKeyValue(Key key, uint8_t *value, int valueSize) { +void MapBufferBuilder::storeKeyValue( + Key key, + uint8_t *value, + int32_t valueSize) { if (key < minKeyToStore_) { LOG(ERROR) << "Error: key out of order - key: " << key; abort(); @@ -58,10 +61,10 @@ void MapBufferBuilder::storeKeyValue(Key key, uint8_t *value, int valueSize) { // TODO T83483191: header.count points to the next index // TODO T83483191: add test to verify storage of sparse keys - int keyOffset = getKeyOffset(_header.count); - int valueOffset = keyOffset + KEY_SIZE; + int32_t keyOffset = getKeyOffset(_header.count); + int32_t valueOffset = keyOffset + KEY_SIZE; - int nextKeyValueOffset = keyOffset + BUCKET_SIZE; + int32_t nextKeyValueOffset = keyOffset + BUCKET_SIZE; if (nextKeyValueOffset >= keyValuesSize_) { ensureKeyValueSpace(); } @@ -89,12 +92,12 @@ void MapBufferBuilder::putNull(Key key) { putInt(key, NULL_VALUE); } -void MapBufferBuilder::putInt(Key key, int value) { +void MapBufferBuilder::putInt(Key key, int32_t value) { uint8_t *bytePointer = reinterpret_cast(&(value)); storeKeyValue(key, bytePointer, INT_SIZE); } -void MapBufferBuilder::ensureDynamicDataSpace(int size) { +void MapBufferBuilder::ensureDynamicDataSpace(int32_t size) { if (dynamicDataValues_ == nullptr) { dynamicDataSize_ = size; dynamicDataValues_ = new Byte[dynamicDataSize_]; @@ -103,14 +106,14 @@ void MapBufferBuilder::ensureDynamicDataSpace(int size) { } if (dynamicDataOffset_ + size >= dynamicDataSize_) { - int oldDynamicDataSize = dynamicDataSize_; + int32_t oldDynamicDataSize = dynamicDataSize_; react_native_assert( - (dynamicDataSize_ < std::numeric_limits::max() / 2) && + (dynamicDataSize_ < std::numeric_limits::max() / 2) && "Error: trying to assign a value beyond the capacity of int"); dynamicDataSize_ *= dynamicDataSize_; react_native_assert( - (dynamicDataSize_ < std::numeric_limits::max() - size) && + (dynamicDataSize_ < std::numeric_limits::max() - size) && "Error: trying to assign a value beyond the capacity of int"); // sum size to ensure that the size always fit into newDynamicDataValues @@ -124,15 +127,15 @@ void MapBufferBuilder::ensureDynamicDataSpace(int size) { } void MapBufferBuilder::putString(Key key, std::string value) { - int strLength = value.length(); + int32_t strLength = value.length(); const char *cstring = getCstring(&value); // format [lenght of string (int)] + [Array of Characters in the string] - int sizeOfLength = INT_SIZE; - // TODO T83483191: review if map.getBufferSize() should be an int or long + int32_t sizeOfLength = INT_SIZE; + // TODO T83483191: review if map.getBufferSize() should be an int32_t or long // instead of an int16 (because strings can be longer than int16); - int sizeOfDynamicData = sizeOfLength + strLength; + int32_t sizeOfDynamicData = sizeOfLength + strLength; ensureDynamicDataSpace(sizeOfDynamicData); memcpy(dynamicDataValues_ + dynamicDataOffset_, &strLength, sizeOfLength); memcpy( @@ -147,10 +150,10 @@ void MapBufferBuilder::putString(Key key, std::string value) { } void MapBufferBuilder::putMapBuffer(Key key, MapBuffer &map) { - int mapBufferSize = map.getBufferSize(); + int32_t mapBufferSize = map.getBufferSize(); // format [lenght of buffer (int)] + [bytes of MapBuffer] - int sizeOfDynamicData = mapBufferSize + INT_SIZE; + int32_t sizeOfDynamicData = mapBufferSize + INT_SIZE; // format [Array of bytes of the mapBuffer] ensureDynamicDataSpace(sizeOfDynamicData); @@ -171,7 +174,7 @@ MapBuffer MapBufferBuilder::build() { "Error when building mapbuffer with invalid datastructures."); // Create buffer: [header] + [key, values] + [dynamic data] - int bufferSize = keyValuesOffset_ + dynamicDataOffset_; + int32_t bufferSize = keyValuesOffset_ + dynamicDataOffset_; _header.bufferSize = bufferSize; diff --git a/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.h b/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.h index 5c7e918631d..d46dd8fbea0 100644 --- a/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.h +++ b/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.h @@ -20,7 +20,7 @@ namespace react { constexpr uint16_t INITIAL_KEY_VALUE_SIZE = 108; // Default initial size for _dynamicDataValues array -constexpr int INITIAL_DYNAMIC_DATA_SIZE = 200; +constexpr int32_t INITIAL_DYNAMIC_DATA_SIZE = 200; /** * MapBufferBuilder is a builder class for MapBuffer @@ -31,9 +31,9 @@ class MapBufferBuilder { void ensureKeyValueSpace(); - void ensureDynamicDataSpace(int size); + void ensureDynamicDataSpace(int32_t size); - void storeKeyValue(Key key, uint8_t *value, int valueSize); + void storeKeyValue(Key key, uint8_t *value, int32_t valueSize); // Array of [key,value] map entries: // - Key is represented in 2 bytes @@ -47,19 +47,19 @@ class MapBufferBuilder { // Relative offset on the _keyValues array. // This represents the first byte that can be written in _keyValues array - int keyValuesOffset_ = 0; + int32_t keyValuesOffset_ = 0; // This array contains data for dynamic values in the MapBuffer. // A dynamic value is a String or another MapBuffer. uint8_t *dynamicDataValues_ = nullptr; // Amount of bytes allocated on _dynamicDataValues - int dynamicDataSize_ = 0; + int32_t dynamicDataSize_ = 0; // Relative offset on the _dynamicDataValues array. // This represents the first byte that can be written in _dynamicDataValues // array - int dynamicDataOffset_ = 0; + int32_t dynamicDataOffset_ = 0; // Minimmum key to store in the MapBuffer (this is used to guarantee // consistency) @@ -74,7 +74,7 @@ class MapBufferBuilder { static MapBuffer EMPTY(); - void putInt(Key key, int value); + void putInt(Key key, int32_t value); void putBool(Key key, bool value); diff --git a/ReactCommon/react/renderer/mapbuffer/primitives.h b/ReactCommon/react/renderer/mapbuffer/primitives.h index 0ddb415501e..456e0b9e12d 100644 --- a/ReactCommon/react/renderer/mapbuffer/primitives.h +++ b/ReactCommon/react/renderer/mapbuffer/primitives.h @@ -9,11 +9,10 @@ #include -// TODO: Enable CHECK_CONSISTENCY only in debug mode or test environments (or -// just in demand) -// #define CHECK_CONSISTENCY 1 +// TODO T83483191: Enable CHECK_CONSISTENCY only in debug mode or test +// environments (or just in demand) #define CHECK_CONSISTENCY 1 -constexpr static int NULL_VALUE = 0; +constexpr static int32_t NULL_VALUE = 0; // Value used to verify if the data is serialized with LittleEndian order constexpr static uint16_t ALIGNMENT = 0xFE; @@ -28,36 +27,36 @@ namespace react { struct Header { uint16_t alignment; // alignment of serialization uint16_t count; // amount of items in the map - int bufferSize; // Amount of bytes used to store the map in memory + int32_t bufferSize; // Amount of bytes used to store the map in memory }; -constexpr static int KEY_SIZE = sizeof(Key); -constexpr static int HEADER_SIZE = sizeof(Header); -constexpr static int INT_SIZE = sizeof(int); -constexpr static int DOUBLE_SIZE = sizeof(double); -constexpr static int UINT8_SIZE = sizeof(uint8_t); -constexpr static int UINT16_SIZE = sizeof(uint16_t); -constexpr static int UINT64_SIZE = sizeof(uint64_t); -constexpr static int HEADER_ALIGNMENT_OFFSET = 0; -constexpr static int HEADER_COUNT_OFFSET = UINT16_SIZE; -constexpr static int HEADER_BUFFER_SIZE_OFFSET = UINT16_SIZE * 2; +constexpr static int32_t KEY_SIZE = sizeof(Key); +constexpr static int32_t HEADER_SIZE = sizeof(Header); +constexpr static int32_t INT_SIZE = sizeof(int32_t); +constexpr static int32_t DOUBLE_SIZE = sizeof(double); +constexpr static int32_t UINT8_SIZE = sizeof(uint8_t); +constexpr static int32_t UINT16_SIZE = sizeof(uint16_t); +constexpr static int32_t UINT64_SIZE = sizeof(uint64_t); +constexpr static int32_t HEADER_ALIGNMENT_OFFSET = 0; +constexpr static int32_t HEADER_COUNT_OFFSET = UINT16_SIZE; +constexpr static int32_t HEADER_BUFFER_SIZE_OFFSET = UINT16_SIZE * 2; -constexpr static int MAX_VALUE_SIZE = UINT64_SIZE; +constexpr static int32_t MAX_VALUE_SIZE = UINT64_SIZE; // 10 bytes : 2 key + 8 value -constexpr static int BUCKET_SIZE = KEY_SIZE + UINT64_SIZE; +constexpr static int32_t BUCKET_SIZE = KEY_SIZE + UINT64_SIZE; /** * Returns the offset of the key received by parameter. */ -inline int getKeyOffset(Key key) { +inline int32_t getKeyOffset(Key key) { return HEADER_SIZE + BUCKET_SIZE * key; } /** * Returns the offset of the value associated to the key received by parameter. */ -inline int getValueOffset(Key key) { +inline int32_t getValueOffset(Key key) { return getKeyOffset(key) + KEY_SIZE; }