88#include <aws/common/file.h>
99#include <aws/common/hash_table.h>
1010#include <aws/common/logging.h>
11+ #include <aws/common/ref_count.h>
1112#include <aws/common/string.h>
1213#include <aws/sdkutils/aws_profile.h>
1314
@@ -33,6 +34,7 @@ struct aws_profile_collection {
3334 struct aws_allocator * allocator ;
3435 enum aws_profile_source_type profile_source ;
3536 struct aws_hash_table profiles ;
37+ struct aws_ref_count ref_count ;
3638};
3739
3840/*
@@ -592,12 +594,11 @@ static void s_profile_hash_table_value_destroy(void *value) {
592594 */
593595
594596void aws_profile_collection_destroy (struct aws_profile_collection * profile_collection ) {
595- if (profile_collection == NULL ) {
596- return ;
597- }
597+ aws_profile_collection_release (profile_collection );
598+ }
598599
600+ static void s_aws_profile_collection_destroy_internal (struct aws_profile_collection * profile_collection ) {
599601 aws_hash_table_clean_up (& profile_collection -> profiles );
600-
601602 aws_mem_release (profile_collection -> allocator , profile_collection );
602603}
603604
@@ -741,6 +742,8 @@ struct aws_profile_collection *aws_profile_collection_new_from_merge(
741742 }
742743
743744 AWS_ZERO_STRUCT (* merged );
745+ aws_ref_count_init (
746+ & merged -> ref_count , merged , (aws_simple_completion_callback * )s_aws_profile_collection_destroy_internal );
744747
745748 size_t max_profiles = 0 ;
746749 if (config_profiles != NULL ) {
@@ -781,7 +784,7 @@ struct aws_profile_collection *aws_profile_collection_new_from_merge(
781784 return merged ;
782785
783786cleanup :
784- aws_profile_collection_destroy (merged );
787+ s_aws_profile_collection_destroy_internal (merged );
785788
786789 return NULL ;
787790}
@@ -1198,6 +1201,11 @@ static struct aws_profile_collection *s_aws_profile_collection_new_internal(
11981201 profile_collection -> profile_source = source ;
11991202 profile_collection -> allocator = allocator ;
12001203
1204+ aws_ref_count_init (
1205+ & profile_collection -> ref_count ,
1206+ profile_collection ,
1207+ (aws_simple_completion_callback * )s_aws_profile_collection_destroy_internal );
1208+
12011209 if (aws_hash_table_init (
12021210 & profile_collection -> profiles ,
12031211 allocator ,
@@ -1238,7 +1246,23 @@ static struct aws_profile_collection *s_aws_profile_collection_new_internal(
12381246 return profile_collection ;
12391247
12401248cleanup :
1241- aws_profile_collection_destroy (profile_collection );
1249+ s_aws_profile_collection_destroy_internal (profile_collection );
1250+
1251+ return NULL ;
1252+ }
1253+
1254+ struct aws_profile_collection * aws_profile_collection_acquire (struct aws_profile_collection * collection ) {
1255+ if (collection != NULL ) {
1256+ aws_ref_count_acquire (& collection -> ref_count );
1257+ }
1258+
1259+ return collection ;
1260+ }
1261+
1262+ struct aws_profile_collection * aws_profile_collection_release (struct aws_profile_collection * collection ) {
1263+ if (collection != NULL ) {
1264+ aws_ref_count_release (& collection -> ref_count );
1265+ }
12421266
12431267 return NULL ;
12441268}
0 commit comments