Skip to content

Commit c98a341

Browse files
authored
Add Reference Count to Profile Collection (#23)
1 parent 8e341f2 commit c98a341

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

include/aws/sdkutils/aws_profile.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,22 @@ AWS_EXTERN_C_BEGIN
4242
*************************/
4343

4444
/**
45-
* Clean up everything associated with a profile collection
45+
* Increments the reference count on the profile collection, allowing the caller to take a reference to it.
46+
*
47+
* Returns the same profile collection passed in.
48+
*/
49+
AWS_SDKUTILS_API
50+
struct aws_profile_collection *aws_profile_collection_acquire(struct aws_profile_collection *collection);
51+
52+
/**
53+
* Decrements a profile collection's ref count. When the ref count drops to zero, the collection will be destroyed.
54+
* Returns NULL.
55+
*/
56+
AWS_SDKUTILS_API
57+
struct aws_profile_collection *aws_profile_collection_release(struct aws_profile_collection *collection);
58+
59+
/**
60+
* @Deprecated This is equivalent to aws_profile_collection_release.
4661
*/
4762
AWS_SDKUTILS_API
4863
void aws_profile_collection_destroy(struct aws_profile_collection *profile_collection);

source/aws_profile.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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

594596
void 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

783786
cleanup:
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

12401248
cleanup:
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

Comments
 (0)