blob: db0d674b2b47b985c7d20af1172ddf15131a15ec [file] [log] [blame]
Andre Eisenbach89f5e412014-12-05 09:40:20 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2015 Google Inc.
Andre Eisenbach89f5e412014-12-05 09:40:20 -08004 *
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.
Myles Watsonb55040c2016-10-19 13:15:34 -070037void ringbuffer_free(ringbuffer_t* rb);
Andre Eisenbach89f5e412014-12-05 09:40:20 -080038
39// Returns remaining buffer size
Myles Watsonb55040c2016-10-19 13:15:34 -070040size_t ringbuffer_available(const ringbuffer_t* rb);
Andre Eisenbach89f5e412014-12-05 09:40:20 -080041
42// Returns size of data in buffer
Myles Watsonb55040c2016-10-19 13:15:34 -070043size_t ringbuffer_size(const ringbuffer_t* rb);
Andre Eisenbach89f5e412014-12-05 09:40:20 -080044
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 Watsonb55040c2016-10-19 13:15:34 -070048size_t ringbuffer_insert(ringbuffer_t* rb, const uint8_t* p, size_t length);
Andre Eisenbach89f5e412014-12-05 09:40:20 -080049
Sharvil Nanavatib35c2342016-02-29 15:50:49 -080050// 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 Watsonb55040c2016-10-19 13:15:34 -070054size_t ringbuffer_peek(const ringbuffer_t* rb, off_t offset, uint8_t* p,
55 size_t length);
Andre Eisenbach89f5e412014-12-05 09:40:20 -080056
57// Does the same as |ringbuffer_peek|, but also advances the ring buffer head
Myles Watsonb55040c2016-10-19 13:15:34 -070058size_t ringbuffer_pop(ringbuffer_t* rb, uint8_t* p, size_t length);
Andre Eisenbach89f5e412014-12-05 09:40:20 -080059
60// Deletes |length| bytes from the ringbuffer starting from the head
61// Return actual number of bytes deleted.
Myles Watsonb55040c2016-10-19 13:15:34 -070062size_t ringbuffer_delete(ringbuffer_t* rb, size_t length);