Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 3b10fdd | 2017-09-18 09:00:20 -0700 | [diff] [blame^] | 3 | * Copyright 2015 Google Inc. |
Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | typedef struct ringbuffer_t ringbuffer_t; |
| 24 | |
| 25 | // NOTE: |
| 26 | // None of the functions below are thread safe when it comes to accessing the |
| 27 | // *rb pointer. It is *NOT* possible to insert and pop/delete at the same time. |
| 28 | // Callers must protect the *rb pointer separately. |
| 29 | |
| 30 | // Create a ringbuffer with the specified size |
| 31 | // Returns NULL if memory allocation failed. Resulting pointer must be freed |
| 32 | // using |ringbuffer_free|. |
| 33 | ringbuffer_t* ringbuffer_init(const size_t size); |
| 34 | |
| 35 | // Frees the ringbuffer structure and buffer |
| 36 | // Save to call with NULL. |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 37 | void ringbuffer_free(ringbuffer_t* rb); |
Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 38 | |
| 39 | // Returns remaining buffer size |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 40 | size_t ringbuffer_available(const ringbuffer_t* rb); |
Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 41 | |
| 42 | // Returns size of data in buffer |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 43 | size_t ringbuffer_size(const ringbuffer_t* rb); |
Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 44 | |
| 45 | // Attempts to insert up to |length| bytes of data at |p| into the buffer |
| 46 | // Return actual number of bytes added. Can be less than |length| if buffer |
| 47 | // is full. |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 48 | size_t ringbuffer_insert(ringbuffer_t* rb, const uint8_t* p, size_t length); |
Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 49 | |
Sharvil Nanavati | 8a3fdd1 | 2016-02-29 15:50:49 -0800 | [diff] [blame] | 50 | // Peek |length| number of bytes from the ringbuffer, starting at |offset|, |
| 51 | // into the buffer |p|. Return the actual number of bytes peeked. Can be less |
| 52 | // than |length| if there is less than |length| data available. |offset| must |
| 53 | // be non-negative. |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 54 | size_t ringbuffer_peek(const ringbuffer_t* rb, off_t offset, uint8_t* p, |
| 55 | size_t length); |
Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 56 | |
| 57 | // Does the same as |ringbuffer_peek|, but also advances the ring buffer head |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 58 | size_t ringbuffer_pop(ringbuffer_t* rb, uint8_t* p, size_t length); |
Andre Eisenbach | 8f6c5be | 2014-12-05 09:40:20 -0800 | [diff] [blame] | 59 | |
| 60 | // Deletes |length| bytes from the ringbuffer starting from the head |
| 61 | // Return actual number of bytes deleted. |
Myles Watson | 914a9dc | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 62 | size_t ringbuffer_delete(ringbuffer_t* rb, size_t length); |