Skip to content

Commit 65e50dd

Browse files
authored
Ref (aws#171)
* Updates CRT to use new ref-counting APIs for a number of C types
1 parent 06d214f commit 65e50dd

35 files changed

+1035
-888
lines changed

aws-common-runtime/aws-c-common

Submodule aws-c-common updated 181 files

include/aws/crt/Api.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ namespace Aws
5959
aws_logger logger;
6060
};
6161

62+
/**
63+
* Method for tests to wait for all outstanding thread-based resources to fully destruct. Must be called
64+
* before a memory check. We don't want this to be a part of normal (ApiHandle) destruction.
65+
*/
66+
AWS_CRT_CPP_API void TestCleanupAndWait();
67+
6268
AWS_CRT_CPP_API const char *ErrorDebugString(int error) noexcept;
6369
/**
6470
* @return the value of the last aws error on the current thread. Return 0 if no aws-error raised before.

include/aws/crt/io/EventLoopGroup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace Aws
5454
aws_event_loop_group *GetUnderlyingHandle() noexcept;
5555

5656
private:
57-
aws_event_loop_group m_eventLoopGroup;
57+
aws_event_loop_group *m_eventLoopGroup;
5858
int m_lastError;
5959
};
6060
} // namespace Io

include/aws/crt/io/HostResolver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ namespace Aws
7878
bool ResolveHost(const String &host, const OnHostResolved &onResolved) noexcept override;
7979

8080
/// @private
81-
aws_host_resolver *GetUnderlyingHandle() noexcept override { return &m_resolver; }
81+
aws_host_resolver *GetUnderlyingHandle() noexcept override { return m_resolver; }
8282
/// @private
8383
aws_host_resolution_config *GetConfig() noexcept override { return &m_config; }
8484

8585
private:
86-
aws_host_resolver m_resolver;
86+
aws_host_resolver *m_resolver;
8787
aws_host_resolution_config m_config;
8888
Allocator *m_allocator;
8989
bool m_initialized;

source/Api.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,12 @@ namespace Aws
3535
cJSON_InitHooks(&hooks);
3636
}
3737

38-
static void s_cleanUpApi()
39-
{
40-
g_allocator = nullptr;
41-
aws_auth_library_clean_up();
42-
aws_mqtt_library_clean_up();
43-
aws_http_library_clean_up();
44-
}
45-
4638
ApiHandle::ApiHandle(Allocator *allocator) noexcept : logger() { s_initApi(allocator); }
4739

4840
ApiHandle::ApiHandle() noexcept : logger() { s_initApi(DefaultAllocator()); }
4941

5042
ApiHandle::~ApiHandle()
5143
{
52-
s_cleanUpApi();
53-
5444
if (aws_logger_get() == &logger)
5545
{
5646
aws_logger_set(NULL);

source/io/EventLoopGroup.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,31 @@ namespace Aws
1111
namespace Io
1212
{
1313
EventLoopGroup::EventLoopGroup(uint16_t threadCount, Allocator *allocator) noexcept
14-
: m_lastError(AWS_ERROR_SUCCESS)
14+
: m_eventLoopGroup(nullptr), m_lastError(AWS_ERROR_SUCCESS)
1515
{
16-
AWS_ZERO_STRUCT(m_eventLoopGroup);
17-
18-
if (aws_event_loop_group_default_init(&m_eventLoopGroup, allocator, threadCount))
16+
m_eventLoopGroup = aws_event_loop_group_new_default(allocator, threadCount, NULL);
17+
if (m_eventLoopGroup == nullptr)
1918
{
2019
m_lastError = aws_last_error();
2120
}
2221
}
2322

24-
EventLoopGroup::~EventLoopGroup()
25-
{
26-
if (!m_lastError)
27-
{
28-
aws_event_loop_group_clean_up(&m_eventLoopGroup);
29-
m_lastError = AWS_ERROR_SUCCESS;
30-
}
31-
AWS_ZERO_STRUCT(m_eventLoopGroup);
32-
}
23+
EventLoopGroup::~EventLoopGroup() { aws_event_loop_group_release(m_eventLoopGroup); }
3324

3425
EventLoopGroup::EventLoopGroup(EventLoopGroup &&toMove) noexcept
3526
: m_eventLoopGroup(toMove.m_eventLoopGroup), m_lastError(toMove.m_lastError)
3627
{
3728
toMove.m_lastError = AWS_ERROR_UNKNOWN;
38-
AWS_ZERO_STRUCT(toMove.m_eventLoopGroup);
29+
toMove.m_eventLoopGroup = nullptr;
3930
}
4031

4132
EventLoopGroup &EventLoopGroup::operator=(EventLoopGroup &&toMove) noexcept
4233
{
4334
m_eventLoopGroup = toMove.m_eventLoopGroup;
4435
m_lastError = toMove.m_lastError;
4536
toMove.m_lastError = AWS_ERROR_UNKNOWN;
46-
AWS_ZERO_STRUCT(toMove.m_eventLoopGroup);
37+
toMove.m_eventLoopGroup = nullptr;
38+
4739
return *this;
4840
}
4941

@@ -55,7 +47,7 @@ namespace Aws
5547
{
5648
if (*this)
5749
{
58-
return &m_eventLoopGroup;
50+
return m_eventLoopGroup;
5951
}
6052

6153
return nullptr;

source/io/HostResolver.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ namespace Aws
2121
size_t maxHosts,
2222
size_t maxTTL,
2323
Allocator *allocator) noexcept
24-
: m_allocator(allocator), m_initialized(true)
24+
: m_resolver(nullptr), m_allocator(allocator), m_initialized(false)
2525
{
26-
if (aws_host_resolver_init_default(&m_resolver, allocator, maxHosts, elGroup.GetUnderlyingHandle()))
26+
AWS_ZERO_STRUCT(m_config);
27+
28+
m_resolver = aws_host_resolver_new_default(allocator, maxHosts, elGroup.GetUnderlyingHandle(), NULL);
29+
if (m_resolver != nullptr)
2730
{
28-
m_initialized = false;
31+
m_initialized = true;
2932
}
3033

3134
m_config.impl = aws_default_dns_resolve;
@@ -35,11 +38,8 @@ namespace Aws
3538

3639
DefaultHostResolver::~DefaultHostResolver()
3740
{
38-
if (m_initialized)
39-
{
40-
aws_host_resolver_clean_up(&m_resolver);
41-
m_initialized = false;
42-
}
41+
aws_host_resolver_release(m_resolver);
42+
m_initialized = false;
4343
}
4444

4545
/// @private
@@ -91,7 +91,7 @@ namespace Aws
9191
args->allocator = m_allocator;
9292

9393
if (!args->host ||
94-
aws_host_resolver_resolve_host(&m_resolver, args->host, s_onHostResolved, &m_config, args))
94+
aws_host_resolver_resolve_host(m_resolver, args->host, s_onHostResolved, &m_config, args))
9595
{
9696
Delete(args, m_allocator);
9797
return false;

source/io/TlsOptions.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,17 @@ namespace Aws
261261
if (mode == TlsMode::CLIENT)
262262
{
263263
aws_tls_ctx *underlying_tls_ctx = aws_tls_client_ctx_new(allocator, &options.m_options);
264-
265-
if (underlying_tls_ctx != NULL)
264+
if (underlying_tls_ctx != nullptr)
266265
{
267-
m_ctx.reset(underlying_tls_ctx, aws_tls_ctx_destroy);
266+
m_ctx.reset(underlying_tls_ctx, aws_tls_ctx_release);
268267
}
269268
}
270269
else
271270
{
272271
aws_tls_ctx *underlying_tls_ctx = aws_tls_server_ctx_new(allocator, &options.m_options);
273-
274-
if (underlying_tls_ctx != NULL)
272+
if (underlying_tls_ctx != nullptr)
275273
{
276-
m_ctx.reset(underlying_tls_ctx, aws_tls_ctx_destroy);
274+
m_ctx.reset(underlying_tls_ctx, aws_tls_ctx_release);
277275
}
278276
}
279277

source/mqtt/MqttClient.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ namespace Aws
283283
{
284284
if (*this)
285285
{
286-
aws_mqtt_client_connection_destroy(m_underlyingConnection);
286+
aws_mqtt_client_connection_release(m_underlyingConnection);
287287

288288
if (m_onAnyCbData)
289289
{
@@ -650,31 +650,15 @@ namespace Aws
650650
return packetId;
651651
}
652652

653-
MqttClient::MqttClient(Io::ClientBootstrap &bootstrap, Allocator *allocator) noexcept : m_client(nullptr)
653+
MqttClient::MqttClient(Io::ClientBootstrap &bootstrap, Allocator *allocator) noexcept
654+
: m_client(aws_mqtt_client_new(allocator, bootstrap.GetUnderlyingHandle()))
654655
{
655-
m_client =
656-
reinterpret_cast<aws_mqtt_client *>(aws_mem_acquire(allocator, sizeof(struct aws_mqtt_client)));
657-
if (!m_client)
658-
{
659-
return;
660-
}
661-
662-
if (aws_mqtt_client_init(m_client, allocator, bootstrap.GetUnderlyingHandle()))
663-
{
664-
aws_mem_release(allocator, reinterpret_cast<void *>(m_client));
665-
m_client = nullptr;
666-
}
667656
}
668657

669658
MqttClient::~MqttClient()
670659
{
671-
if (m_client)
672-
{
673-
Allocator *allocator = m_client->allocator;
674-
aws_mqtt_client_clean_up(m_client);
675-
aws_mem_release(allocator, reinterpret_cast<void *>(m_client));
676-
m_client = nullptr;
677-
}
660+
aws_mqtt_client_release(m_client);
661+
m_client = nullptr;
678662
}
679663

680664
MqttClient::MqttClient(MqttClient &&toMove) noexcept : m_client(toMove.m_client)

tests/ChannelBootstrapTest.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,31 @@
1010

1111
static int s_TestClientBootstrapResourceSafety(struct aws_allocator *allocator, void *)
1212
{
13-
Aws::Crt::ApiHandle apiHandle(allocator);
14-
15-
Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator);
16-
ASSERT_TRUE(eventLoopGroup);
17-
ASSERT_NOT_NULL(eventLoopGroup.GetUnderlyingHandle());
18-
19-
Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30, allocator);
20-
ASSERT_TRUE(defaultHostResolver);
21-
ASSERT_NOT_NULL(defaultHostResolver.GetUnderlyingHandle());
22-
23-
std::promise<void> bootstrapShutdownPromise;
24-
std::future<void> bootstrapShutdownFuture = bootstrapShutdownPromise.get_future();
2513
{
26-
Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup, defaultHostResolver, allocator);
27-
ASSERT_TRUE(clientBootstrap);
28-
ASSERT_NOT_NULL(clientBootstrap.GetUnderlyingHandle());
29-
clientBootstrap.EnableBlockingShutdown();
30-
clientBootstrap.SetShutdownCompleteCallback([&]() { bootstrapShutdownPromise.set_value(); });
14+
Aws::Crt::ApiHandle apiHandle(allocator);
15+
16+
Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator);
17+
ASSERT_TRUE(eventLoopGroup);
18+
ASSERT_NOT_NULL(eventLoopGroup.GetUnderlyingHandle());
19+
20+
Aws::Crt::Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 8, 30, allocator);
21+
ASSERT_TRUE(defaultHostResolver);
22+
ASSERT_NOT_NULL(defaultHostResolver.GetUnderlyingHandle());
23+
24+
std::promise<void> bootstrapShutdownPromise;
25+
std::future<void> bootstrapShutdownFuture = bootstrapShutdownPromise.get_future();
26+
{
27+
Aws::Crt::Io::ClientBootstrap clientBootstrap(eventLoopGroup, defaultHostResolver, allocator);
28+
ASSERT_TRUE(clientBootstrap);
29+
ASSERT_NOT_NULL(clientBootstrap.GetUnderlyingHandle());
30+
clientBootstrap.EnableBlockingShutdown();
31+
clientBootstrap.SetShutdownCompleteCallback([&]() { bootstrapShutdownPromise.set_value(); });
32+
}
33+
34+
ASSERT_TRUE(std::future_status::ready == bootstrapShutdownFuture.wait_for(std::chrono::seconds(10)));
3135
}
3236

33-
ASSERT_TRUE(std::future_status::ready == bootstrapShutdownFuture.wait_for(std::chrono::seconds(10)));
37+
Aws::Crt::TestCleanupAndWait();
3438

3539
return AWS_ERROR_SUCCESS;
3640
}

tests/CredentialsTest.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class GetCredentialsWaiter
6868
static int s_TestProviderStaticGet(struct aws_allocator *allocator, void *ctx)
6969
{
7070
(void)ctx;
71-
ApiHandle apiHandle(allocator);
72-
7371
{
72+
ApiHandle apiHandle(allocator);
73+
7474
CredentialsProviderStaticConfig config;
7575
config.AccessKeyId = aws_byte_cursor_from_c_str(s_access_key_id);
7676
config.SecretAccessKey = aws_byte_cursor_from_c_str(s_secret_access_key);
@@ -82,6 +82,8 @@ static int s_TestProviderStaticGet(struct aws_allocator *allocator, void *ctx)
8282
auto creds = waiter.GetCredentials();
8383
}
8484

85+
Aws::Crt::TestCleanupAndWait();
86+
8587
return AWS_OP_SUCCESS;
8688
}
8789

@@ -90,15 +92,17 @@ AWS_TEST_CASE(TestProviderStaticGet, s_TestProviderStaticGet)
9092
static int s_TestProviderEnvironmentGet(struct aws_allocator *allocator, void *ctx)
9193
{
9294
(void)ctx;
93-
ApiHandle apiHandle(allocator);
94-
9595
{
96+
ApiHandle apiHandle(allocator);
97+
9698
auto provider = CredentialsProvider::CreateCredentialsProviderEnvironment(allocator);
9799
GetCredentialsWaiter waiter(provider);
98100

99101
auto creds = waiter.GetCredentials();
100102
}
101103

104+
Aws::Crt::TestCleanupAndWait();
105+
102106
return AWS_OP_SUCCESS;
103107
}
104108

@@ -107,9 +111,9 @@ AWS_TEST_CASE(TestProviderEnvironmentGet, s_TestProviderEnvironmentGet)
107111
static int s_TestProviderProfileGet(struct aws_allocator *allocator, void *ctx)
108112
{
109113
(void)ctx;
110-
ApiHandle apiHandle(allocator);
111-
112114
{
115+
ApiHandle apiHandle(allocator);
116+
113117
CredentialsProviderProfileConfig config;
114118

115119
auto provider = CredentialsProvider::CreateCredentialsProviderProfile(config, allocator);
@@ -122,6 +126,8 @@ static int s_TestProviderProfileGet(struct aws_allocator *allocator, void *ctx)
122126
}
123127
}
124128

129+
Aws::Crt::TestCleanupAndWait();
130+
125131
return AWS_OP_SUCCESS;
126132
}
127133

@@ -130,9 +136,9 @@ AWS_TEST_CASE(TestProviderProfileGet, s_TestProviderProfileGet)
130136
static int s_TestProviderImdsGet(struct aws_allocator *allocator, void *ctx)
131137
{
132138
(void)ctx;
133-
ApiHandle apiHandle(allocator);
134139

135140
{
141+
ApiHandle apiHandle(allocator);
136142
apiHandle.InitializeLogging(Aws::Crt::LogLevel::Trace, stderr);
137143

138144
Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator);
@@ -154,6 +160,8 @@ static int s_TestProviderImdsGet(struct aws_allocator *allocator, void *ctx)
154160
auto creds = waiter.GetCredentials();
155161
}
156162

163+
Aws::Crt::TestCleanupAndWait();
164+
157165
return AWS_OP_SUCCESS;
158166
}
159167

@@ -162,9 +170,9 @@ AWS_TEST_CASE(TestProviderImdsGet, s_TestProviderImdsGet)
162170
static int s_TestProviderDefaultChainGet(struct aws_allocator *allocator, void *ctx)
163171
{
164172
(void)ctx;
165-
ApiHandle apiHandle(allocator);
166-
167173
{
174+
ApiHandle apiHandle(allocator);
175+
168176
Aws::Crt::Io::EventLoopGroup eventLoopGroup(0, allocator);
169177
ASSERT_TRUE(eventLoopGroup);
170178

@@ -184,7 +192,9 @@ static int s_TestProviderDefaultChainGet(struct aws_allocator *allocator, void *
184192
auto creds = waiter.GetCredentials();
185193
}
186194

195+
Aws::Crt::TestCleanupAndWait();
196+
187197
return AWS_OP_SUCCESS;
188198
}
189199

190-
AWS_TEST_CASE(TestProviderDefaultChainGet, s_TestProviderDefaultChainGet)
200+
AWS_TEST_CASE(TestProviderDefaultChainGet, s_TestProviderDefaultChainGet)

0 commit comments

Comments
 (0)