Skip to content

Commit 7548be5

Browse files
committed
Removing unncessary dynamic memory allocation during property decoding
1 parent 57c374c commit 7548be5

File tree

6 files changed

+83
-102
lines changed

6 files changed

+83
-102
lines changed

src/cbor/CBORDecoder.cpp

+39-54
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@
3333

3434
void CBORDecoder::decode(PropertyContainer & property_container, uint8_t const * const payload, size_t const length, bool isSyncMessage)
3535
{
36+
CborValue array_iter, map_iter,value_iter;
3637
CborParser parser;
37-
CborValue array_iter, map_iter,value_iter;
38-
/* List of map data that will hold all the attributes of a property */
39-
std::list<CborMapData *> map_data_list;
40-
/* Current property name during decoding: use to look for a new property in the senml value array */
41-
String current_property_name;
38+
CborMapData map_data;
39+
std::list<CborMapData> map_data_list; /* List of map data that will hold all the attributes of a property */
40+
String current_property_name; /* Current property name during decoding: use to look for a new property in the senml value array */
4241
unsigned long current_property_base_time{0}, current_property_time{0};
4342

4443
if (cbor_parser_init(payload, length, 0, &parser, &array_iter) != CborNoError)
@@ -50,15 +49,13 @@ void CBORDecoder::decode(PropertyContainer & property_container, uint8_t const *
5049
if (cbor_value_enter_container(&array_iter, &map_iter) != CborNoError)
5150
return;
5251

53-
CborMapData * map_data = nullptr;
54-
5552
MapParserState current_state = MapParserState::EnterMap,
5653
next_state = MapParserState::Error;
5754

5855
while (current_state != MapParserState::Complete) {
5956

6057
switch (current_state) {
61-
case MapParserState::EnterMap : next_state = handle_EnterMap(&map_iter, &value_iter, &map_data); break;
58+
case MapParserState::EnterMap : next_state = handle_EnterMap(&map_iter, &value_iter); break;
6259
case MapParserState::MapKey : next_state = handle_MapKey(&value_iter); break;
6360
case MapParserState::UndefinedKey : next_state = handle_UndefinedKey(&value_iter); break;
6461
case MapParserState::BaseVersion : next_state = handle_BaseVersion(&value_iter, map_data); break;
@@ -82,12 +79,11 @@ void CBORDecoder::decode(PropertyContainer & property_container, uint8_t const *
8279
PRIVATE MEMBER FUNCTIONS
8380
******************************************************************************/
8481

85-
CBORDecoder::MapParserState CBORDecoder::handle_EnterMap(CborValue * map_iter, CborValue * value_iter, CborMapData **map_data) {
82+
CBORDecoder::MapParserState CBORDecoder::handle_EnterMap(CborValue * map_iter, CborValue * value_iter) {
8683
MapParserState next_state = MapParserState::Error;
8784

8885
if (cbor_value_get_type(map_iter) == CborMapType) {
8986
if (cbor_value_enter_container(map_iter, value_iter) == CborNoError) {
90-
*map_data = new CborMapData();
9187
next_state = MapParserState::MapKey;
9288
}
9389
}
@@ -144,13 +140,13 @@ CBORDecoder::MapParserState CBORDecoder::handle_UndefinedKey(CborValue * value_i
144140
return next_state;
145141
}
146142

147-
CBORDecoder::MapParserState CBORDecoder::handle_BaseVersion(CborValue * value_iter, CborMapData * map_data) {
143+
CBORDecoder::MapParserState CBORDecoder::handle_BaseVersion(CborValue * value_iter, CborMapData & map_data) {
148144
MapParserState next_state = MapParserState::Error;
149145

150146
if (cbor_value_is_integer(value_iter)) {
151147
int val = 0;
152148
if (cbor_value_get_int(value_iter, &val) == CborNoError) {
153-
map_data->base_version.set(val);
149+
map_data.base_version.set(val);
154150

155151
if (cbor_value_advance(value_iter) == CborNoError) {
156152
next_state = MapParserState::MapKey;
@@ -161,14 +157,14 @@ CBORDecoder::MapParserState CBORDecoder::handle_BaseVersion(CborValue * value_it
161157
return next_state;
162158
}
163159

164-
CBORDecoder::MapParserState CBORDecoder::handle_BaseName(CborValue * value_iter, CborMapData * map_data) {
160+
CBORDecoder::MapParserState CBORDecoder::handle_BaseName(CborValue * value_iter, CborMapData & map_data) {
165161
MapParserState next_state = MapParserState::Error;
166162

167163
if (cbor_value_is_text_string(value_iter)) {
168164
char * val = 0;
169165
size_t val_size = 0;
170166
if (cbor_value_dup_text_string(value_iter, &val, &val_size, value_iter) == CborNoError) {
171-
map_data->base_name.set(String(val));
167+
map_data.base_name.set(String(val));
172168
free(val);
173169
next_state = MapParserState::MapKey;
174170
}
@@ -177,12 +173,12 @@ CBORDecoder::MapParserState CBORDecoder::handle_BaseName(CborValue * value_iter,
177173
return next_state;
178174
}
179175

180-
CBORDecoder::MapParserState CBORDecoder::handle_BaseTime(CborValue * value_iter, CborMapData * map_data) {
176+
CBORDecoder::MapParserState CBORDecoder::handle_BaseTime(CborValue * value_iter, CborMapData & map_data) {
181177
MapParserState next_state = MapParserState::Error;
182178

183179
double val = 0.0;
184180
if (ifNumericConvertToDouble(value_iter, &val)) {
185-
map_data->base_time.set(val);
181+
map_data.base_time.set(val);
186182

187183
if (cbor_value_advance(value_iter) == CborNoError) {
188184
next_state = MapParserState::MapKey;
@@ -192,7 +188,7 @@ CBORDecoder::MapParserState CBORDecoder::handle_BaseTime(CborValue * value_iter,
192188
return next_state;
193189
}
194190

195-
CBORDecoder::MapParserState CBORDecoder::handle_Name(CborValue * value_iter, CborMapData * map_data, PropertyContainer & property_container) {
191+
CBORDecoder::MapParserState CBORDecoder::handle_Name(CborValue * value_iter, CborMapData & map_data, PropertyContainer & property_container) {
196192
MapParserState next_state = MapParserState::Error;
197193

198194
if (cbor_value_is_text_string(value_iter)) {
@@ -202,25 +198,25 @@ CBORDecoder::MapParserState CBORDecoder::handle_Name(CborValue * value_iter, Cbo
202198
if (cbor_value_dup_text_string(value_iter, &val, &val_size, value_iter) == CborNoError) {
203199
String name = val;
204200
free(val);
205-
map_data->name.set(name);
201+
map_data.name.set(name);
206202
int colonPos = name.indexOf(":");
207203
String attribute_name = "";
208204
if (colonPos != -1) {
209205
attribute_name = name.substring(colonPos + 1);
210206
}
211-
map_data->attribute_name.set(attribute_name);
207+
map_data.attribute_name.set(attribute_name);
212208
next_state = MapParserState::MapKey;
213209
}
214210
} else if (cbor_value_is_integer(value_iter)) {
215211
// if the value in the cbor message is an integer, a light payload has been used and an integer identifier should be decode in order to retrieve the corresponding property and attribute name to be updated
216212
int val = 0;
217213
if (cbor_value_get_int(value_iter, &val) == CborNoError) {
218-
map_data->light_payload.set(true);
219-
map_data->name_identifier.set(val & 255);
220-
map_data->attribute_identifier.set(val >> 8);
221-
map_data->light_payload.set(true);
214+
map_data.light_payload.set(true);
215+
map_data.name_identifier.set(val & 255);
216+
map_data.attribute_identifier.set(val >> 8);
217+
map_data.light_payload.set(true);
222218
String name = getPropertyNameByIdentifier(property_container, val);
223-
map_data->name.set(name);
219+
map_data.name.set(name);
224220

225221

226222
if (cbor_value_advance(value_iter) == CborNoError) {
@@ -234,12 +230,12 @@ CBORDecoder::MapParserState CBORDecoder::handle_Name(CborValue * value_iter, Cbo
234230
return next_state;
235231
}
236232

237-
CBORDecoder::MapParserState CBORDecoder::handle_Value(CborValue * value_iter, CborMapData * map_data) {
233+
CBORDecoder::MapParserState CBORDecoder::handle_Value(CborValue * value_iter, CborMapData & map_data) {
238234
MapParserState next_state = MapParserState::Error;
239235

240236
double val = 0.0;
241237
if (ifNumericConvertToDouble(value_iter, &val)) {
242-
map_data->val.set(val);
238+
map_data.val.set(val);
243239

244240
if (cbor_value_advance(value_iter) == CborNoError) {
245241
next_state = MapParserState::MapKey;
@@ -249,14 +245,14 @@ CBORDecoder::MapParserState CBORDecoder::handle_Value(CborValue * value_iter, Cb
249245
return next_state;
250246
}
251247

252-
CBORDecoder::MapParserState CBORDecoder::handle_StringValue(CborValue * value_iter, CborMapData * map_data) {
248+
CBORDecoder::MapParserState CBORDecoder::handle_StringValue(CborValue * value_iter, CborMapData & map_data) {
253249
MapParserState next_state = MapParserState::Error;
254250

255251
if (cbor_value_is_text_string(value_iter)) {
256252
char * val = 0;
257253
size_t val_size = 0;
258254
if (cbor_value_dup_text_string(value_iter, &val, &val_size, value_iter) == CborNoError) {
259-
map_data->str_val.set(String(val));
255+
map_data.str_val.set(String(val));
260256
free(val);
261257
next_state = MapParserState::MapKey;
262258
}
@@ -265,12 +261,12 @@ CBORDecoder::MapParserState CBORDecoder::handle_StringValue(CborValue * value_it
265261
return next_state;
266262
}
267263

268-
CBORDecoder::MapParserState CBORDecoder::handle_BooleanValue(CborValue * value_iter, CborMapData * map_data) {
264+
CBORDecoder::MapParserState CBORDecoder::handle_BooleanValue(CborValue * value_iter, CborMapData & map_data) {
269265
MapParserState next_state = MapParserState::Error;
270266

271267
bool val = false;
272268
if (cbor_value_get_boolean(value_iter, &val) == CborNoError) {
273-
map_data->bool_val.set(val);
269+
map_data.bool_val.set(val);
274270

275271
if (cbor_value_advance(value_iter) == CborNoError) {
276272
next_state = MapParserState::MapKey;
@@ -280,12 +276,12 @@ CBORDecoder::MapParserState CBORDecoder::handle_BooleanValue(CborValue * value_i
280276
return next_state;
281277
}
282278

283-
CBORDecoder::MapParserState CBORDecoder::handle_Time(CborValue * value_iter, CborMapData * map_data) {
279+
CBORDecoder::MapParserState CBORDecoder::handle_Time(CborValue * value_iter, CborMapData & map_data) {
284280
MapParserState next_state = MapParserState::Error;
285281

286282
double val = 0.0;
287283
if (ifNumericConvertToDouble(value_iter, &val)) {
288-
map_data->time.set(val);
284+
map_data.time.set(val);
289285

290286
if (cbor_value_advance(value_iter) == CborNoError) {
291287
next_state = MapParserState::MapKey;
@@ -295,31 +291,31 @@ CBORDecoder::MapParserState CBORDecoder::handle_Time(CborValue * value_iter, Cbo
295291
return next_state;
296292
}
297293

298-
CBORDecoder::MapParserState CBORDecoder::handle_LeaveMap(CborValue * map_iter, CborValue * value_iter, CborMapData * map_data, PropertyContainer & property_container, String & current_property_name, unsigned long & current_property_base_time, unsigned long & current_property_time, bool const is_sync_message, std::list<CborMapData *> & map_data_list) {
294+
CBORDecoder::MapParserState CBORDecoder::handle_LeaveMap(CborValue * map_iter, CborValue * value_iter, CborMapData & map_data, PropertyContainer & property_container, String & current_property_name, unsigned long & current_property_base_time, unsigned long & current_property_time, bool const is_sync_message, std::list<CborMapData> & map_data_list) {
299295
MapParserState next_state = MapParserState::Error;
300-
if (map_data->name.isSet()) {
296+
if (map_data.name.isSet()) {
301297
String propertyName;
302-
int colonPos = map_data->name.get().indexOf(":");
298+
int colonPos = map_data.name.get().indexOf(":");
303299
if (colonPos != -1) {
304-
propertyName = map_data->name.get().substring(0, colonPos);
300+
propertyName = map_data.name.get().substring(0, colonPos);
305301
} else {
306-
propertyName = map_data->name.get();
302+
propertyName = map_data.name.get();
307303
}
308304

309305
if (current_property_name != "" && propertyName != current_property_name) {
310306
/* Update the property containers depending on the parsed data */
311307
updateProperty(property_container, current_property_name, current_property_base_time + current_property_time, is_sync_message, &map_data_list);
312308
/* Reset current property data */
313-
freeMapDataList(&map_data_list);
309+
map_data_list.clear();
314310
current_property_base_time = 0;
315311
current_property_time = 0;
316312
}
317313
/* Compute the cloud change event baseTime and Time */
318-
if (map_data->base_time.isSet()) {
319-
current_property_base_time = (unsigned long)(map_data->base_time.get());
314+
if (map_data.base_time.isSet()) {
315+
current_property_base_time = (unsigned long)(map_data.base_time.get());
320316
}
321-
if (map_data->time.isSet() && (map_data->time.get() > current_property_time)) {
322-
current_property_time = (unsigned long)map_data->time.get();
317+
if (map_data.time.isSet() && (map_data.time.get() > current_property_time)) {
318+
current_property_time = (unsigned long)map_data.time.get();
323319
}
324320
map_data_list.push_back(map_data);
325321
current_property_name = propertyName;
@@ -333,25 +329,14 @@ CBORDecoder::MapParserState CBORDecoder::handle_LeaveMap(CborValue * map_iter, C
333329
/* Update the property containers depending on the parsed data */
334330
updateProperty(property_container, current_property_name, current_property_base_time + current_property_time, is_sync_message, &map_data_list);
335331
/* Reset last property data */
336-
freeMapDataList(&map_data_list);
332+
map_data_list.clear();
337333
next_state = MapParserState::Complete;
338334
}
339335
}
340336

341337
return next_state;
342338
}
343339

344-
void CBORDecoder::freeMapDataList(std::list<CborMapData *> * map_data_list)
345-
{
346-
std::for_each(map_data_list->begin(),
347-
map_data_list->end(),
348-
[](CborMapData * map_data)
349-
{
350-
delete map_data;
351-
});
352-
map_data_list->clear();
353-
}
354-
355340
bool CBORDecoder::ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val) {
356341

357342
if (cbor_value_is_integer(value_iter)) {

src/cbor/CBORDecoder.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,21 @@ class CBORDecoder
6363
Error
6464
};
6565

66-
static MapParserState handle_EnterMap(CborValue * map_iter, CborValue * value_iter, CborMapData **map_data);
66+
static MapParserState handle_EnterMap(CborValue * map_iter, CborValue * value_iter);
6767
static MapParserState handle_MapKey(CborValue * value_iter);
6868
static MapParserState handle_UndefinedKey(CborValue * value_iter);
69-
static MapParserState handle_BaseVersion(CborValue * value_iter, CborMapData * map_data);
70-
static MapParserState handle_BaseName(CborValue * value_iter, CborMapData * map_data);
71-
static MapParserState handle_BaseTime(CborValue * value_iter, CborMapData * map_data);
72-
static MapParserState handle_Name(CborValue * value_iter, CborMapData * map_data, PropertyContainer & property_container);
73-
static MapParserState handle_Value(CborValue * value_iter, CborMapData * map_data);
74-
static MapParserState handle_StringValue(CborValue * value_iter, CborMapData * map_data);
75-
static MapParserState handle_BooleanValue(CborValue * value_iter, CborMapData * map_data);
76-
static MapParserState handle_Time(CborValue * value_iter, CborMapData * map_data);
77-
static MapParserState handle_LeaveMap(CborValue * map_iter, CborValue * value_iter, CborMapData * map_data, PropertyContainer & property_container, String & current_property_name, unsigned long & current_property_base_time, unsigned long & current_property_time, bool const is_sync_message, std::list<CborMapData *> & map_data_list);
69+
static MapParserState handle_BaseVersion(CborValue * value_iter, CborMapData & map_data);
70+
static MapParserState handle_BaseName(CborValue * value_iter, CborMapData & map_data);
71+
static MapParserState handle_BaseTime(CborValue * value_iter, CborMapData & map_data);
72+
static MapParserState handle_Name(CborValue * value_iter, CborMapData & map_data, PropertyContainer & property_container);
73+
static MapParserState handle_Value(CborValue * value_iter, CborMapData & map_data);
74+
static MapParserState handle_StringValue(CborValue * value_iter, CborMapData & map_data);
75+
static MapParserState handle_BooleanValue(CborValue * value_iter, CborMapData & map_data);
76+
static MapParserState handle_Time(CborValue * value_iter, CborMapData & map_data);
77+
static MapParserState handle_LeaveMap(CborValue * map_iter, CborValue * value_iter, CborMapData & map_data, PropertyContainer & property_container, String & current_property_name, unsigned long & current_property_base_time, unsigned long & current_property_time, bool const is_sync_message, std::list<CborMapData> & map_data_list);
7878

7979
static bool ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val);
8080
static double convertCborHalfFloatToDouble(uint16_t const half_val);
81-
static void freeMapDataList(std::list<CborMapData *> * map_data_list);
8281

8382
};
8483

0 commit comments

Comments
 (0)