@@ -250,22 +250,21 @@ size_t aws_mqtt_iot_sdk_metrics_compute_storage_size(const struct aws_mqtt_iot_s
250250 return storage_size ;
251251}
252252
253- int aws_mqtt_iot_sdk_metrics_storage_init (
254- struct aws_mqtt_iot_sdk_metrics_storage * metrics_storage ,
253+ struct aws_mqtt_iot_sdk_metrics_storage * aws_mqtt_iot_sdk_metrics_storage_new (
255254 struct aws_allocator * allocator ,
256255 const struct aws_mqtt_iot_sdk_metrics * metrics_options ) {
257256
258- if (metrics_storage == NULL || allocator == NULL ) {
259- return aws_raise_error (AWS_ERROR_INVALID_ARGUMENT );
257+ if (allocator == NULL || metrics_options == NULL ) {
258+ aws_raise_error (AWS_ERROR_INVALID_ARGUMENT );
259+ return NULL ;
260260 }
261261
262- if (metrics_options == NULL ) {
263- return AWS_OP_SUCCESS ;
264- }
262+ struct aws_mqtt_iot_sdk_metrics_storage * metrics_storage =
263+ aws_mem_calloc (allocator , 1 , sizeof (struct aws_mqtt_iot_sdk_metrics_storage ));
265264
266265 size_t storage_capacity = aws_mqtt_iot_sdk_metrics_compute_storage_size (metrics_options );
267266 if (aws_byte_buf_init (& metrics_storage -> storage , allocator , storage_capacity )) {
268- return AWS_OP_ERR ;
267+ goto cleanup_storage ;
269268 }
270269
271270 metrics_storage -> allocator = allocator ;
@@ -304,25 +303,30 @@ int aws_mqtt_iot_sdk_metrics_storage_init(
304303 if (metrics_options -> library_name .len > 0 ) {
305304 metrics_storage -> library_name = metrics_options -> library_name ;
306305 if (aws_byte_buf_append_and_update (& metrics_storage -> storage , & metrics_storage -> library_name )) {
307- goto metrics_storage_error ;
306+ goto cleanup_storage ;
308307 }
309308 storage_view -> library_name = metrics_storage -> library_name ;
310309 }
311310
312- return AWS_OP_SUCCESS ;
311+ return metrics_storage ;
313312
314- metrics_storage_error :
313+ cleanup_storage :
315314 // TODO Future Work: add metadata entries once we implemented the metadata feature
316315 // if (aws_array_list_is_valid(&metrics_storage->metadata_entries)) {
317316 // aws_array_list_clean_up(&metrics_storage->metadata_entries);
318317 // }
319-
320- aws_byte_buf_clean_up (& metrics_storage -> storage );
321318
322- return AWS_OP_ERR ;
319+ if (aws_byte_buf_is_valid (& metrics_storage -> storage )) {
320+ aws_byte_buf_clean_up (& metrics_storage -> storage );
321+ }
322+ if (metrics_options != NULL ) {
323+ aws_mem_release (allocator , metrics_storage );
324+ }
325+
326+ return NULL ;
323327}
324328
325- void aws_mqtt_iot_sdk_metrics_storage_clean_up (struct aws_mqtt_iot_sdk_metrics_storage * metrics_storage ) {
329+ void aws_mqtt_iot_sdk_metrics_storage_destroy (struct aws_mqtt_iot_sdk_metrics_storage * metrics_storage ) {
326330 if (metrics_storage == NULL ) {
327331 return ;
328332 }
0 commit comments