Line data Source code
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 80838 : const uint8_t *atomic_ptr) {
17 80838 : return (atomic_ptr >= ring_buf->allocation && atomic_ptr <= ring_buf->allocation_end);
18 80838 : }
19 :
20 : /**
21 : * Checks whether the ring buffer is empty
22 : */
23 0 : AWS_STATIC_IMPL bool aws_ring_buffer_is_empty(const struct aws_ring_buffer *ring_buf) {
24 0 : uint8_t *head = (uint8_t *)aws_atomic_load_ptr(&ring_buf->head);
25 0 : uint8_t *tail = (uint8_t *)aws_atomic_load_ptr(&ring_buf->tail);
26 0 : return head == tail;
27 0 : }
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 40419 : AWS_STATIC_IMPL bool aws_ring_buffer_is_valid(const struct aws_ring_buffer *ring_buf) {
34 40419 : uint8_t *head = (uint8_t *)aws_atomic_load_ptr(&ring_buf->head);
35 40419 : uint8_t *tail = (uint8_t *)aws_atomic_load_ptr(&ring_buf->tail);
36 40419 : bool head_in_range = aws_ring_buffer_check_atomic_ptr(ring_buf, head);
37 40419 : bool tail_in_range = aws_ring_buffer_check_atomic_ptr(ring_buf, tail);
38 40419 : /* if head points-to the first element of the buffer then tail must too */
39 40419 : bool valid_head_tail = (head != ring_buf->allocation) || (tail == ring_buf->allocation);
40 40419 : return ring_buf && AWS_MEM_IS_READABLE(ring_buf->allocation, ring_buf->allocation_end - ring_buf->allocation) &&
41 40419 : head_in_range && tail_in_range && valid_head_tail && (ring_buf->allocator != NULL);
42 40419 : }
43 : AWS_EXTERN_C_END
44 : #endif /* AWS_COMMON_RING_BUFFER_INL */
|