| 1 |
#ifndef AWS_COMMON_RING_BUFFER_INL |
| 2 |
#define AWS_COMMON_RING_BUFFER_INL |
| 3 |
/** |
| 4 |
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 |
* SPDX-License-Identifier: Apache-2.0. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <aws/common/ring_buffer.h> |
| 9 |
|
| 10 |
AWS_EXTERN_C_BEGIN |
| 11 |
/* |
| 12 |
* Checks whether atomic_ptr correctly points to a memory location within the bounds of the aws_ring_buffer |
| 13 |
*/ |
| 14 |
AWS_STATIC_IMPL bool aws_ring_buffer_check_atomic_ptr( |
| 15 |
const struct aws_ring_buffer *ring_buf, |
| 16 |
const uint8_t *atomic_ptr) { |
| 17 |
return ((atomic_ptr != NULL) && (atomic_ptr >= ring_buf->allocation && atomic_ptr <= ring_buf->allocation_end)); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Checks whether the ring buffer is empty |
| 22 |
*/ |
| 23 |
AWS_STATIC_IMPL bool aws_ring_buffer_is_empty(const struct aws_ring_buffer *ring_buf) { |
| 24 |
uint8_t *head = (uint8_t *)aws_atomic_load_ptr(&ring_buf->head); |
| 25 |
uint8_t *tail = (uint8_t *)aws_atomic_load_ptr(&ring_buf->tail); |
| 26 |
return head == tail; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Evaluates the set of properties that define the shape of all valid aws_ring_buffer structures. |
| 31 |
* It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion). |
| 32 |
*/ |
| 33 |
AWS_STATIC_IMPL bool aws_ring_buffer_is_valid(const struct aws_ring_buffer *ring_buf) { |
| 34 |
if (AWS_UNLIKELY(ring_buf == NULL)) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
uint8_t *head = (uint8_t *)aws_atomic_load_ptr(&ring_buf->head); |
| 38 |
uint8_t *tail = (uint8_t *)aws_atomic_load_ptr(&ring_buf->tail); |
| 39 |
bool head_in_range = aws_ring_buffer_check_atomic_ptr(ring_buf, head); |
| 40 |
bool tail_in_range = aws_ring_buffer_check_atomic_ptr(ring_buf, tail); |
| 41 |
/* if head points-to the first element of the buffer then tail must too */ |
| 42 |
bool valid_head_tail = (head != ring_buf->allocation) || (tail == ring_buf->allocation); |
| 43 |
return (ring_buf->allocation != NULL) && head_in_range && tail_in_range && valid_head_tail && |
| 44 |
(ring_buf->allocator != NULL); |
| 45 |
} |
| 46 |
AWS_EXTERN_C_END |
| 47 |
#endif /* AWS_COMMON_RING_BUFFER_INL */ |