Skip to content

Commit 67dda26

Browse files
committed
Merge branch 'esp_matter/simplification_core_cpp' into 'main'
Removed Redundant if Statements for Code Simplification See merge request app-frameworks/esp-matter!873
2 parents a19d521 + c315ae4 commit 67dda26

File tree

2 files changed

+29
-81
lines changed

2 files changed

+29
-81
lines changed

components/esp_matter/esp_matter_core.cpp

+29-77
Original file line numberDiff line numberDiff line change
@@ -350,21 +350,13 @@ static esp_err_t free_default_value(attribute_t *attribute)
350350
/* Free value if data is more than 2 bytes or if it is min max attribute */
351351
if (current_attribute->flags & ATTRIBUTE_FLAG_MIN_MAX) {
352352
if (current_attribute->default_value_size > 2) {
353-
if (current_attribute->default_value.ptrToMinMaxValue->defaultValue.ptrToDefaultValue) {
354-
esp_matter_mem_free((void *)current_attribute->default_value.ptrToMinMaxValue->defaultValue.ptrToDefaultValue);
355-
}
356-
if (current_attribute->default_value.ptrToMinMaxValue->minValue.ptrToDefaultValue) {
357-
esp_matter_mem_free((void *)current_attribute->default_value.ptrToMinMaxValue->minValue.ptrToDefaultValue);
358-
}
359-
if (current_attribute->default_value.ptrToMinMaxValue->maxValue.ptrToDefaultValue) {
360-
esp_matter_mem_free((void *)current_attribute->default_value.ptrToMinMaxValue->maxValue.ptrToDefaultValue);
361-
}
353+
esp_matter_mem_free((void *)current_attribute->default_value.ptrToMinMaxValue->defaultValue.ptrToDefaultValue);
354+
esp_matter_mem_free((void *)current_attribute->default_value.ptrToMinMaxValue->minValue.ptrToDefaultValue);
355+
esp_matter_mem_free((void *)current_attribute->default_value.ptrToMinMaxValue->maxValue.ptrToDefaultValue);
362356
}
363357
esp_matter_mem_free((void *)current_attribute->default_value.ptrToMinMaxValue);
364358
} else if (current_attribute->default_value_size > 2) {
365-
if (current_attribute->default_value.ptrToDefaultValue) {
366-
esp_matter_mem_free((void *)current_attribute->default_value.ptrToDefaultValue);
367-
}
359+
esp_matter_mem_free((void *)current_attribute->default_value.ptrToDefaultValue);
368360
}
369361
return ESP_OK;
370362
}
@@ -493,30 +485,20 @@ static esp_err_t disable(endpoint_t *endpoint)
493485
/* Free attributes */
494486
esp_matter_mem_free((void *)endpoint_type->cluster[cluster_index].attributes);
495487
/* Free commands */
496-
if (endpoint_type->cluster[cluster_index].acceptedCommandList) {
497-
esp_matter_mem_free((void *)endpoint_type->cluster[cluster_index].acceptedCommandList);
498-
}
499-
if (endpoint_type->cluster[cluster_index].generatedCommandList) {
500-
esp_matter_mem_free((void *)endpoint_type->cluster[cluster_index].generatedCommandList);
501-
}
488+
esp_matter_mem_free((void *)endpoint_type->cluster[cluster_index].acceptedCommandList);
489+
esp_matter_mem_free((void *)endpoint_type->cluster[cluster_index].generatedCommandList);
502490
/* Free events */
503-
if (endpoint_type->cluster[cluster_index].eventList) {
504-
esp_matter_mem_free((void *)endpoint_type->cluster[cluster_index].eventList);
505-
}
491+
esp_matter_mem_free((void *)endpoint_type->cluster[cluster_index].eventList);
506492
}
507493
esp_matter_mem_free((void *)endpoint_type->cluster);
508494

509495
/* Free data versions */
510-
if (current_endpoint->data_versions_ptr) {
511-
esp_matter_mem_free(current_endpoint->data_versions_ptr);
512-
current_endpoint->data_versions_ptr = NULL;
513-
}
496+
esp_matter_mem_free(current_endpoint->data_versions_ptr);
497+
current_endpoint->data_versions_ptr = NULL;
514498

515499
/* Free device types */
516-
if (current_endpoint->device_types_ptr) {
517-
esp_matter_mem_free(current_endpoint->device_types_ptr);
518-
current_endpoint->device_types_ptr = NULL;
519-
}
500+
esp_matter_mem_free(current_endpoint->device_types_ptr);
501+
current_endpoint->device_types_ptr = NULL;
520502

521503
/* Delete identify */
522504
if (current_endpoint->identify) {
@@ -572,9 +554,7 @@ esp_err_t enable(endpoint_t *endpoint)
572554
DataVersion *data_versions_ptr = (DataVersion *)esp_matter_mem_calloc(1, cluster_count * sizeof(DataVersion));
573555
if (!data_versions_ptr) {
574556
ESP_LOGE(TAG, "Couldn't allocate data_versions");
575-
esp_matter_mem_free(data_versions_ptr);
576557
esp_matter_mem_free(endpoint_type);
577-
current_endpoint->data_versions_ptr = NULL;
578558
current_endpoint->endpoint_type = NULL;
579559
/* goto cleanup is not used here to avoid 'crosses initialization' of data_versions below */
580560
return ESP_ERR_NO_MEM;
@@ -777,50 +757,28 @@ esp_err_t enable(endpoint_t *endpoint)
777757
return err;
778758

779759
cleanup:
780-
if (generated_command_ids) {
781-
esp_matter_mem_free(generated_command_ids);
782-
}
783-
if (accepted_command_ids) {
784-
esp_matter_mem_free(accepted_command_ids);
785-
}
786-
if (event_ids) {
787-
esp_matter_mem_free(event_ids);
788-
}
789-
if (matter_attributes) {
790-
esp_matter_mem_free(matter_attributes);
791-
}
760+
esp_matter_mem_free(generated_command_ids);
761+
esp_matter_mem_free(accepted_command_ids);
762+
esp_matter_mem_free(event_ids);
763+
esp_matter_mem_free(matter_attributes);
792764
if (matter_clusters) {
793765
for (int cluster_index = 0; cluster_index < cluster_count; cluster_index++) {
794766
/* Free attributes */
795-
if (matter_clusters[cluster_index].attributes) {
796-
esp_matter_mem_free((void *)matter_clusters[cluster_index].attributes);
797-
}
767+
esp_matter_mem_free((void *)matter_clusters[cluster_index].attributes);
798768
/* Free commands */
799-
if (matter_clusters[cluster_index].acceptedCommandList) {
800-
esp_matter_mem_free((void *)matter_clusters[cluster_index].acceptedCommandList);
801-
}
802-
if (matter_clusters[cluster_index].generatedCommandList) {
803-
esp_matter_mem_free((void *)matter_clusters[cluster_index].generatedCommandList);
804-
}
769+
esp_matter_mem_free((void *)matter_clusters[cluster_index].acceptedCommandList);
770+
esp_matter_mem_free((void *)matter_clusters[cluster_index].generatedCommandList);
805771
/* Free events */
806-
if (matter_clusters[cluster_index].eventList) {
807-
esp_matter_mem_free((void *)matter_clusters[cluster_index].eventList);
808-
}
772+
esp_matter_mem_free((void *)matter_clusters[cluster_index].eventList);
809773
}
810774
esp_matter_mem_free(matter_clusters);
811775
}
812-
if (data_versions_ptr) {
813-
esp_matter_mem_free(data_versions_ptr);
814-
current_endpoint->data_versions_ptr = NULL;
815-
}
816-
if (device_types_ptr) {
817-
esp_matter_mem_free(device_types_ptr);
818-
current_endpoint->device_types_ptr = NULL;
819-
}
820-
if (endpoint_type) {
821-
esp_matter_mem_free(endpoint_type);
822-
current_endpoint->endpoint_type = NULL;
823-
}
776+
esp_matter_mem_free(data_versions_ptr);
777+
current_endpoint->data_versions_ptr = NULL;
778+
esp_matter_mem_free(device_types_ptr);
779+
current_endpoint->device_types_ptr = NULL;
780+
esp_matter_mem_free(endpoint_type);
781+
current_endpoint->endpoint_type = NULL;
824782
return err;
825783
}
826784

@@ -1178,15 +1136,11 @@ static esp_err_t destroy(attribute_t *attribute)
11781136
current_attribute->val.type == ESP_MATTER_VAL_TYPE_LONG_OCTET_STRING ||
11791137
current_attribute->val.type == ESP_MATTER_VAL_TYPE_ARRAY) {
11801138
/* Free buf */
1181-
if (current_attribute->val.val.a.b) {
1182-
esp_matter_mem_free(current_attribute->val.val.a.b);
1183-
}
1139+
esp_matter_mem_free(current_attribute->val.val.a.b);
11841140
}
11851141

11861142
/* Free bounds */
1187-
if (current_attribute->bounds) {
1188-
esp_matter_mem_free(current_attribute->bounds);
1189-
}
1143+
esp_matter_mem_free(current_attribute->bounds);
11901144

11911145
/* Erase the persistent data */
11921146
if (current_attribute->flags & ATTRIBUTE_FLAG_NONVOLATILE) {
@@ -1273,10 +1227,8 @@ esp_err_t set_val(attribute_t *attribute, esp_matter_attr_val_t *val)
12731227
val->type == ESP_MATTER_VAL_TYPE_LONG_CHAR_STRING || val->type == ESP_MATTER_VAL_TYPE_LONG_OCTET_STRING ||
12741228
val->type == ESP_MATTER_VAL_TYPE_ARRAY) {
12751229
/* Free old buf */
1276-
if (current_attribute->val.val.a.b) {
1277-
esp_matter_mem_free(current_attribute->val.val.a.b);
1278-
current_attribute->val.val.a.b = NULL;
1279-
}
1230+
esp_matter_mem_free(current_attribute->val.val.a.b);
1231+
current_attribute->val.val.a.b = NULL;
12801232
if (val->val.a.s > 0) {
12811233
/* Alloc new buf */
12821234
uint8_t *new_buf = (uint8_t *)esp_matter_mem_calloc(1, val->val.a.s);

components/esp_matter/utils/esp_matter_mem.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,5 @@ IRAM_ATTR void *esp_matter_mem_realloc(void *ptr, size_t size)
4444

4545
IRAM_ATTR void esp_matter_mem_free(void *ptr)
4646
{
47-
#if CONFIG_ESP_MATTER_MEM_ALLOC_MODE_DEFAULT
4847
free(ptr);
49-
#else
50-
heap_caps_free(ptr);
51-
#endif
5248
}

0 commit comments

Comments
 (0)