@@ -13,21 +13,17 @@ namespace facebook {
13
13
namespace react {
14
14
15
15
MapBuffer MapBufferBuilder::EMPTY () {
16
- return MapBufferBuilder ().build ();
16
+ return MapBufferBuilder (0 ).build ();
17
17
}
18
18
19
19
MapBufferBuilder::MapBufferBuilder (uint32_t initialSize) {
20
20
buckets_.reserve (initialSize);
21
-
22
- dynamicDataSize_ = 0 ;
23
- dynamicDataValues_ = nullptr ;
24
- dynamicDataOffset_ = 0 ;
25
21
}
26
22
27
23
void MapBufferBuilder::storeKeyValue (
28
24
Key key,
29
- uint8_t *value,
30
- int32_t valueSize) {
25
+ uint8_t const *value,
26
+ uint32_t valueSize) {
31
27
if (key < minKeyToStore_) {
32
28
LOG (ERROR) << " Error: key out of order - key: " << key;
33
29
abort ();
@@ -54,7 +50,7 @@ void MapBufferBuilder::putBool(Key key, bool value) {
54
50
}
55
51
56
52
void MapBufferBuilder::putDouble (Key key, double value) {
57
- uint8_t *bytePointer = reinterpret_cast <uint8_t *>(&value);
53
+ auto const *bytePointer = reinterpret_cast <uint8_t *>(&value);
58
54
storeKeyValue (key, bytePointer, DOUBLE_SIZE);
59
55
}
60
56
@@ -63,107 +59,58 @@ void MapBufferBuilder::putNull(Key key) {
63
59
}
64
60
65
61
void MapBufferBuilder::putInt (Key key, int32_t value) {
66
- uint8_t *bytePointer = reinterpret_cast <uint8_t *>(&(value));
62
+ auto const *bytePointer = reinterpret_cast <uint8_t *>(&(value));
67
63
storeKeyValue (key, bytePointer, INT_SIZE);
68
64
}
69
65
70
- void MapBufferBuilder::ensureDynamicDataSpace (int32_t size) {
71
- if (dynamicDataValues_ == nullptr ) {
72
- dynamicDataSize_ = size;
73
- dynamicDataValues_ = new Byte[dynamicDataSize_];
74
- dynamicDataOffset_ = 0 ;
75
- return ;
76
- }
77
-
78
- if (dynamicDataOffset_ + size >= dynamicDataSize_) {
79
- int32_t oldDynamicDataSize = dynamicDataSize_;
80
- react_native_assert (
81
- (dynamicDataSize_ < std::numeric_limits<int32_t >::max () / 2 ) &&
82
- " Error: trying to assign a value beyond the capacity of int" );
83
- dynamicDataSize_ *= dynamicDataSize_;
84
-
85
- react_native_assert (
86
- (dynamicDataSize_ < std::numeric_limits<int32_t >::max () - size) &&
87
- " Error: trying to assign a value beyond the capacity of int" );
88
-
89
- // sum size to ensure that the size always fit into newDynamicDataValues
90
- dynamicDataSize_ += size;
91
- uint8_t *newDynamicDataValues = new Byte[dynamicDataSize_];
92
- uint8_t *oldDynamicDataValues = dynamicDataValues_;
93
- memcpy (newDynamicDataValues, dynamicDataValues_, oldDynamicDataSize);
94
- dynamicDataValues_ = newDynamicDataValues;
95
- delete[] oldDynamicDataValues;
96
- }
97
- }
98
-
99
66
void MapBufferBuilder::putString (Key key, std::string const &value) {
100
- int32_t strLength = static_cast < int32_t >( value.length () );
101
- const char *cstring = getCstring (& value);
67
+ int32_t strSize = value.size ( );
68
+ const char *strData = value. data ( );
102
69
103
- // format [lenght of string (int)] + [Array of Characters in the string]
104
- int32_t sizeOfLength = INT_SIZE;
105
- // TODO T83483191: review if map.getBufferSize () should be an int32_t or long
70
+ auto offset = dynamicData_. size ();
71
+ // format [length of string (int)] + [Array of Characters in the string]
72
+ // TODO T83483191: review if map.size () should be an int32_t or long
106
73
// instead of an int16 (because strings can be longer than int16);
107
-
108
- int32_t sizeOfDynamicData = sizeOfLength + strLength;
109
- ensureDynamicDataSpace (sizeOfDynamicData);
110
- memcpy (dynamicDataValues_ + dynamicDataOffset_, &strLength, sizeOfLength);
111
- memcpy (
112
- dynamicDataValues_ + dynamicDataOffset_ + sizeOfLength,
113
- cstring,
114
- strLength);
74
+ dynamicData_.resize (offset + INT_SIZE + strSize, 0 );
75
+ memcpy (dynamicData_.data () + offset, &strSize, INT_SIZE);
76
+ memcpy (dynamicData_.data () + offset + INT_SIZE, strData, strSize);
115
77
116
78
// Store Key and pointer to the string
117
- putInt (key, dynamicDataOffset_);
118
-
119
- dynamicDataOffset_ += sizeOfDynamicData;
79
+ putInt (key, offset);
120
80
}
121
81
122
82
void MapBufferBuilder::putMapBuffer (Key key, MapBuffer const &map) {
123
- int32_t mapBufferSize = map.getBufferSize ();
83
+ int32_t mapBufferSize = map.size ();
124
84
125
- // format [lenght of buffer (int)] + [bytes of MapBuffer]
126
- int32_t sizeOfDynamicData = mapBufferSize + INT_SIZE;
85
+ auto offset = dynamicData_.size ();
127
86
128
- // format [Array of bytes of the mapBuffer]
129
- ensureDynamicDataSpace (sizeOfDynamicData);
130
-
131
- memcpy (dynamicDataValues_ + dynamicDataOffset_, &mapBufferSize, INT_SIZE);
132
- // Copy the content of the map into dynamicDataValues_
133
- map.copy (dynamicDataValues_ + dynamicDataOffset_ + INT_SIZE);
87
+ // format [length of buffer (int)] + [bytes of MapBuffer]
88
+ dynamicData_.resize (offset + INT_SIZE + mapBufferSize, 0 );
89
+ memcpy (dynamicData_.data () + offset, &mapBufferSize, INT_SIZE);
90
+ // Copy the content of the map into dynamicData_
91
+ memcpy (dynamicData_.data () + offset + INT_SIZE, map.data (), mapBufferSize);
134
92
135
93
// Store Key and pointer to the string
136
- putInt (key, dynamicDataOffset_);
137
-
138
- dynamicDataOffset_ += sizeOfDynamicData;
94
+ putInt (key, offset);
139
95
}
140
96
141
97
MapBuffer MapBufferBuilder::build () {
142
98
// Create buffer: [header] + [key, values] + [dynamic data]
143
99
auto bucketSize = buckets_.size () * BUCKET_SIZE;
144
- int32_t bufferSize = HEADER_SIZE + bucketSize + dynamicDataOffset_ ;
100
+ uint32_t bufferSize = HEADER_SIZE + bucketSize + dynamicData_. size () ;
145
101
146
102
_header.bufferSize = bufferSize;
147
103
148
104
std::vector<uint8_t > buffer (bufferSize);
149
105
memcpy (buffer.data (), &_header, HEADER_SIZE);
150
106
memcpy (buffer.data () + HEADER_SIZE, buckets_.data (), bucketSize);
151
-
152
- if (dynamicDataValues_ != nullptr ) {
153
- memcpy (
154
- buffer.data () + HEADER_SIZE + bucketSize,
155
- dynamicDataValues_,
156
- dynamicDataOffset_);
157
- }
107
+ memcpy (
108
+ buffer.data () + HEADER_SIZE + bucketSize,
109
+ dynamicData_.data (),
110
+ dynamicData_.size ());
158
111
159
112
return MapBuffer (std::move (buffer));
160
113
}
161
114
162
- MapBufferBuilder::~MapBufferBuilder () {
163
- if (dynamicDataValues_ != nullptr ) {
164
- delete[] dynamicDataValues_;
165
- }
166
- }
167
-
168
115
} // namespace react
169
116
} // namespace facebook
0 commit comments