-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bret Ambrose
committed
Dec 11, 2023
1 parent
b7de17b
commit f501fa0
Showing
3 changed files
with
127 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef AWS_MQTT_PRIVATE_REQUEST_RESPONSE_WEAK_REF_H | ||
#define AWS_MQTT_PRIVATE_REQUEST_RESPONSE_WEAK_REF_H | ||
|
||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
#include <aws/mqtt/exports.h> | ||
|
||
#include <aws/common/common.h> | ||
|
||
struct aws_weak_ref; | ||
|
||
AWS_EXTERN_C_BEGIN | ||
|
||
AWS_MQTT_API struct aws_weak_ref *aws_weak_ref_new(struct aws_allocator *allocator, void *referenced); | ||
|
||
AWS_MQTT_API struct aws_weak_ref *aws_weak_ref_acquire(struct aws_weak_ref *weak_ref); | ||
|
||
AWS_MQTT_API struct aws_weak_ref *aws_weak_ref_release(struct aws_weak_ref *weak_ref); | ||
|
||
AWS_MQTT_API void *aws_weak_ref_get_reference(struct aws_weak_ref *weak_ref); | ||
|
||
AWS_MQTT_API void aws_weak_ref_zero_reference(struct aws_weak_ref *weak_ref); | ||
|
||
AWS_EXTERN_C_END | ||
|
||
|
||
#endif /* AWS_MQTT_PRIVATE_REQUEST_RESPONSE_WEAK_REF_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
#include <aws/mqtt/private/request-response/weak_ref.h> | ||
|
||
#include <aws/common/ref_count.h> | ||
|
||
struct aws_weak_ref { | ||
struct aws_allocator *allocator; | ||
struct aws_ref_count refcount; | ||
void *referenced; | ||
}; | ||
|
||
static void s_destroy_weak_ref(void *value) { | ||
struct aws_weak_ref *weak_ref = value; | ||
|
||
aws_mem_release(weak_ref->allocator, weak_ref); | ||
} | ||
|
||
struct aws_weak_ref *aws_weak_ref_new(struct aws_allocator *allocator, void *referenced) { | ||
struct aws_weak_ref *weak_ref = aws_mem_calloc(allocator, 1, sizeof(struct aws_weak_ref)); | ||
|
||
aws_ref_count_init(&weak_ref->refcount, weak_ref, s_destroy_weak_ref); | ||
weak_ref->allocator = allocator; | ||
weak_ref->referenced = referenced; | ||
|
||
return weak_ref; | ||
} | ||
|
||
struct aws_weak_ref *aws_weak_ref_acquire(struct aws_weak_ref *weak_ref) { | ||
if (NULL != weak_ref) { | ||
aws_ref_count_acquire(&weak_ref->refcount); | ||
} | ||
|
||
return weak_ref; | ||
} | ||
|
||
struct aws_weak_ref *aws_weak_ref_release(struct aws_weak_ref *weak_ref) { | ||
if (NULL != weak_ref) { | ||
aws_ref_count_release(&weak_ref->refcount); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
void *aws_weak_ref_get_reference(struct aws_weak_ref *weak_ref) { | ||
return weak_ref->referenced; | ||
} | ||
|
||
void aws_weak_ref_zero_reference(struct aws_weak_ref *weak_ref) { | ||
weak_ref->referenced = NULL; | ||
} |