1/****************************************************************************** 2 * 3 * Copyright 2015 Google Inc. 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 23typedef 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|. 33ringbuffer_t* ringbuffer_init(const size_t size); 34 35// Frees the ringbuffer structure and buffer 36// Save to call with NULL. 37void ringbuffer_free(ringbuffer_t* rb); 38 39// Returns remaining buffer size 40size_t ringbuffer_available(const ringbuffer_t* rb); 41 42// Returns size of data in buffer 43size_t ringbuffer_size(const ringbuffer_t* rb); 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. 48size_t ringbuffer_insert(ringbuffer_t* rb, const uint8_t* p, size_t length); 49 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. 54size_t ringbuffer_peek(const ringbuffer_t* rb, off_t offset, uint8_t* p, 55 size_t length); 56 57// Does the same as |ringbuffer_peek|, but also advances the ring buffer head 58size_t ringbuffer_pop(ringbuffer_t* rb, uint8_t* p, size_t length); 59 60// Deletes |length| bytes from the ringbuffer starting from the head 61// Return actual number of bytes deleted. 62size_t ringbuffer_delete(ringbuffer_t* rb, size_t length); 63