| 1 | #ifndef AWS_COMMON_RING_BUFFER_H |
| 2 | #define AWS_COMMON_RING_BUFFER_H |
| 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/atomics.h> |
| 9 | |
| 10 | AWS_PUSH_SANE_WARNING_LEVEL |
| 11 | |
| 12 | /** |
| 13 | * Lockless ring buffer implementation that is thread safe assuming a single thread acquires and a single thread |
| 14 | * releases. For any other use case (other than the single-threaded use-case), you must manage thread-safety manually. |
| 15 | * |
| 16 | * Also, a very important note: release must happen in the same order as acquire. If you do not your application, and |
| 17 | * possibly computers within a thousand mile radius, may die terrible deaths, and the local drinking water will be |
| 18 | * poisoned for generations with fragments of what is left of your radioactive corrupted memory. |
| 19 | */ |
| 20 | struct aws_ring_buffer { |
| 21 | struct aws_allocator *allocator; |
| 22 | uint8_t *allocation; |
| 23 | struct aws_atomic_var head; |
| 24 | struct aws_atomic_var tail; |
| 25 | uint8_t *allocation_end; |
| 26 | }; |
| 27 | |
| 28 | struct aws_byte_buf; |
| 29 | |
| 30 | AWS_EXTERN_C_BEGIN |
| 31 | |
| 32 | /** |
| 33 | * Initializes a ring buffer with an allocation of size `size`. Returns AWS_OP_SUCCESS on a successful initialization, |
| 34 | * AWS_OP_ERR otherwise. |
| 35 | */ |
| 36 | AWS_COMMON_API int aws_ring_buffer_init(struct aws_ring_buffer *ring_buf, struct aws_allocator *allocator, size_t size); |
| 37 | |
| 38 | /* |
| 39 | * Checks whether atomic_ptr correctly points to a memory location within the bounds of the aws_ring_buffer |
| 40 | */ |
| 41 | AWS_STATIC_IMPL bool aws_ring_buffer_check_atomic_ptr( |
| 42 | const struct aws_ring_buffer *ring_buf, |
| 43 | const uint8_t *atomic_ptr); |
| 44 | /** |
| 45 | * Checks whether the ring buffer is empty |
| 46 | */ |
| 47 | AWS_STATIC_IMPL bool aws_ring_buffer_is_empty(const struct aws_ring_buffer *ring_buf); |
| 48 | |
| 49 | /** |
| 50 | * Evaluates the set of properties that define the shape of all valid aws_ring_buffer structures. |
| 51 | * It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion). |
| 52 | */ |
| 53 | AWS_STATIC_IMPL bool aws_ring_buffer_is_valid(const struct aws_ring_buffer *ring_buf); |
| 54 | |
| 55 | /** |
| 56 | * Cleans up the ring buffer's resources. |
| 57 | */ |
| 58 | AWS_COMMON_API void aws_ring_buffer_clean_up(struct aws_ring_buffer *ring_buf); |
| 59 | |
| 60 | /** |
| 61 | * Attempts to acquire `requested_size` buffer and stores the result in `dest` if successful. Returns AWS_OP_SUCCESS if |
| 62 | * the requested size was available for use, AWS_OP_ERR otherwise. |
| 63 | */ |
| 64 | AWS_COMMON_API int aws_ring_buffer_acquire( |
| 65 | struct aws_ring_buffer *ring_buf, |
| 66 | size_t requested_size, |
| 67 | struct aws_byte_buf *dest); |
| 68 | |
| 69 | /** |
| 70 | * Attempts to acquire `requested_size` buffer and stores the result in `dest` if successful. If not available, it will |
| 71 | * attempt to acquire anywhere from 1 byte to `requested_size`. Returns AWS_OP_SUCCESS if some buffer space is available |
| 72 | * for use, AWS_OP_ERR otherwise. |
| 73 | */ |
| 74 | AWS_COMMON_API int aws_ring_buffer_acquire_up_to( |
| 75 | struct aws_ring_buffer *ring_buf, |
| 76 | size_t minimum_size, |
| 77 | size_t requested_size, |
| 78 | struct aws_byte_buf *dest); |
| 79 | |
| 80 | /** |
| 81 | * Releases `buf` back to the ring buffer for further use. RELEASE MUST HAPPEN in the SAME ORDER AS ACQUIRE. |
| 82 | * If you do not, your application, and possibly computers within a thousand mile radius, may die terrible deaths, |
| 83 | * and the local drinking water will be poisoned for generations |
| 84 | * with fragments of what is left of your radioactive corrupted memory. |
| 85 | */ |
| 86 | AWS_COMMON_API void aws_ring_buffer_release(struct aws_ring_buffer *ring_buffer, struct aws_byte_buf *buf); |
| 87 | |
| 88 | /** |
| 89 | * Returns true if the memory in `buf` was vended by this ring buffer, false otherwise. |
| 90 | * Make sure `buf->buffer` and `ring_buffer->allocation` refer to the same memory region. |
| 91 | */ |
| 92 | AWS_COMMON_API bool aws_ring_buffer_buf_belongs_to_pool( |
| 93 | const struct aws_ring_buffer *ring_buffer, |
| 94 | const struct aws_byte_buf *buf); |
| 95 | |
| 96 | AWS_EXTERN_C_END |
| 97 | |
| 98 | #ifndef AWS_NO_STATIC_IMPL |
| 99 | # include <aws/common/ring_buffer.inl> |
| 100 | #endif /* AWS_NO_STATIC_IMPL */ |
| 101 | |
| 102 | AWS_POP_SANE_WARNING_LEVEL |
| 103 | |
| 104 | #endif /* AWS_COMMON_RING_BUFFER_H */ |