@@ -14,30 +14,24 @@ namespace react {
1414
1515// TODO T83483191: Extend MapBuffer C++ implementation to support basic random
1616// access
17- MapBuffer::MapBuffer (uint8_t *const data, int32_t dataSize) {
18- react_native_assert (
19- (data != nullptr ) && " Error trying to build an invalid MapBuffer" );
20-
21- // Should we move the memory here or document it?
22- data_ = data;
23-
17+ MapBuffer::MapBuffer (std::vector<uint8_t > data) : bytes_(std::move(data)) {
2418 count_ = 0 ;
2519 memcpy (
2620 reinterpret_cast <uint8_t *>(&count_),
27- reinterpret_cast < const uint8_t *>(data_ + HEADER_COUNT_OFFSET) ,
21+ bytes_. data () + HEADER_COUNT_OFFSET,
2822 UINT16_SIZE);
2923
3024 // TODO T83483191: extract memcpy calls into an inline function to simplify
3125 // the code
32- dataSize_ = 0 ;
26+ int32_t dataSize ;
3327 memcpy (
34- reinterpret_cast <uint8_t *>(&dataSize_ ),
35- reinterpret_cast < const uint8_t *>(data_ + HEADER_BUFFER_SIZE_OFFSET) ,
28+ reinterpret_cast <uint8_t *>(&dataSize ),
29+ bytes_. data () + HEADER_BUFFER_SIZE_OFFSET,
3630 INT_SIZE);
3731
38- if (dataSize != dataSize_ ) {
32+ if (dataSize != bytes_. size () ) {
3933 LOG (ERROR) << " Error: Data size does not match, expected " << dataSize
40- << " found: " << dataSize_ ;
34+ << " found: " << bytes_. size () ;
4135 abort ();
4236 }
4337}
@@ -46,7 +40,7 @@ int32_t MapBuffer::getInt(Key key) const {
4640 int32_t value = 0 ;
4741 memcpy (
4842 reinterpret_cast <uint8_t *>(&value),
49- reinterpret_cast < const uint8_t *>(data_ + getValueOffset (key) ),
43+ bytes_. data () + getValueOffset (key),
5044 INT_SIZE);
5145 return value;
5246}
@@ -61,7 +55,7 @@ double MapBuffer::getDouble(Key key) const {
6155 double value = 0 ;
6256 memcpy (
6357 reinterpret_cast <uint8_t *>(&value),
64- reinterpret_cast < const uint8_t *>(data_ + getValueOffset (key) ),
58+ bytes_. data () + getValueOffset (key),
6559 DOUBLE_SIZE);
6660 return value;
6761}
@@ -80,15 +74,14 @@ std::string MapBuffer::getString(Key key) const {
8074 int32_t offset = getInt (key);
8175 memcpy (
8276 reinterpret_cast <uint8_t *>(&stringLength),
83- reinterpret_cast < const uint8_t *>(data_ + dynamicDataOffset + offset) ,
77+ bytes_. data () + dynamicDataOffset + offset,
8478 INT_SIZE);
8579
8680 char *value = new char [stringLength];
8781
8882 memcpy (
8983 reinterpret_cast <char *>(value),
90- reinterpret_cast <const char *>(
91- data_ + dynamicDataOffset + offset + INT_SIZE),
84+ bytes_.data () + dynamicDataOffset + offset + INT_SIZE,
9285 stringLength);
9386
9487 return std::string (value, 0 , stringLength);
@@ -103,47 +96,41 @@ MapBuffer MapBuffer::getMapBuffer(Key key) const {
10396 int32_t offset = getInt (key);
10497 memcpy (
10598 reinterpret_cast <uint8_t *>(&mapBufferLength),
106- reinterpret_cast < const uint8_t *>(data_ + dynamicDataOffset + offset) ,
99+ bytes_. data () + dynamicDataOffset + offset,
107100 INT_SIZE);
108101
109- uint8_t * value = new Byte[ mapBufferLength] ;
102+ std::vector< uint8_t > value ( mapBufferLength) ;
110103
111104 memcpy (
112- reinterpret_cast <uint8_t *>(value),
113- reinterpret_cast <const uint8_t *>(
114- data_ + dynamicDataOffset + offset + INT_SIZE),
105+ value.data (),
106+ bytes_.data () + dynamicDataOffset + offset + INT_SIZE,
115107 mapBufferLength);
116108
117- return MapBuffer (value, mapBufferLength );
109+ return MapBuffer (std::move ( value) );
118110}
119111
120112bool MapBuffer::isNull (Key key) const {
121113 return getInt (key) == NULL_VALUE;
122114}
123115
124116int32_t MapBuffer::getBufferSize () const {
125- return dataSize_ ;
117+ return bytes_. size () ;
126118}
127119
128120void MapBuffer::copy (uint8_t *output) const {
129- memcpy (output, data_, dataSize_ );
121+ memcpy (output, bytes_. data (), bytes_. size () );
130122}
131123
132124uint16_t MapBuffer::getCount () const {
133125 uint16_t size = 0 ;
134126
135127 memcpy (
136- reinterpret_cast <uint16_t *>(&size),
137- reinterpret_cast <const uint16_t *>(data_ + HEADER_COUNT_OFFSET),
138-
128+ reinterpret_cast <uint8_t *>(&size),
129+ bytes_.data () + HEADER_COUNT_OFFSET,
139130 UINT16_SIZE);
140131
141132 return size;
142133}
143134
144- MapBuffer::~MapBuffer () {
145- delete[] data_;
146- }
147-
148135} // namespace react
149136} // namespace facebook
0 commit comments