1 /******************************************************************************
2 *
3 * Copyright (C) 2017 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 #include <assert.h>
20 #include <stdlib.h>
21
22 #include "ringbuffer.h"
23
24 struct ringbuffer_t {
25 size_t total;
26 size_t available;
27 uint8_t* base;
28 uint8_t* head;
29 uint8_t* tail;
30 };
31
ringbuffer_init(const size_t size)32 ringbuffer_t* ringbuffer_init(const size_t size) {
33 if (size == 0) return nullptr;
34
35 ringbuffer_t* p = static_cast<ringbuffer_t*>(calloc(1, sizeof(ringbuffer_t)));
36
37 if (p == nullptr) return nullptr;
38
39 p->base = static_cast<uint8_t*>(calloc(size, sizeof(uint8_t)));
40 p->head = p->tail = p->base;
41 p->total = p->available = size;
42
43 return p;
44 }
45
ringbuffer_free(ringbuffer_t * rb)46 void ringbuffer_free(ringbuffer_t* rb) {
47 if (rb != nullptr) free(rb->base);
48 free(rb);
49 }
50
ringbuffer_available(const ringbuffer_t * rb)51 size_t ringbuffer_available(const ringbuffer_t* rb) {
52 assert(rb);
53 return rb->available;
54 }
55
ringbuffer_size(const ringbuffer_t * rb)56 size_t ringbuffer_size(const ringbuffer_t* rb) {
57 assert(rb);
58 return rb->total - rb->available;
59 }
60
ringbuffer_insert(ringbuffer_t * rb,const uint8_t * p,size_t length)61 size_t ringbuffer_insert(ringbuffer_t* rb, const uint8_t* p, size_t length) {
62 assert(rb);
63 assert(p);
64
65 if (length > ringbuffer_available(rb)) length = ringbuffer_available(rb);
66
67 for (size_t i = 0; i != length; ++i) {
68 *rb->tail++ = *p++;
69 if (rb->tail >= (rb->base + rb->total)) rb->tail = rb->base;
70 }
71
72 rb->available -= length;
73 return length;
74 }
75
ringbuffer_delete(ringbuffer_t * rb,size_t length)76 size_t ringbuffer_delete(ringbuffer_t* rb, size_t length) {
77 assert(rb);
78
79 if (length > ringbuffer_size(rb)) length = ringbuffer_size(rb);
80
81 rb->head += length;
82 if (rb->head >= (rb->base + rb->total)) rb->head -= rb->total;
83
84 rb->available += length;
85 return length;
86 }
87
ringbuffer_peek(const ringbuffer_t * rb,off_t offset,uint8_t * p,size_t length)88 size_t ringbuffer_peek(const ringbuffer_t* rb, off_t offset, uint8_t* p,
89 size_t length) {
90 assert(rb);
91 assert(p);
92 assert(offset >= 0);
93 assert((size_t)offset <= ringbuffer_size(rb));
94
95 uint8_t* b = ((rb->head - rb->base + offset) % rb->total) + rb->base;
96 const size_t bytes_to_copy = (offset + length > ringbuffer_size(rb))
97 ? ringbuffer_size(rb) - offset
98 : length;
99
100 for (size_t copied = 0; copied < bytes_to_copy; ++copied) {
101 *p++ = *b++;
102 if (b >= (rb->base + rb->total)) b = rb->base;
103 }
104
105 return bytes_to_copy;
106 }
107
ringbuffer_pop(ringbuffer_t * rb,uint8_t * p,size_t length)108 size_t ringbuffer_pop(ringbuffer_t* rb, uint8_t* p, size_t length) {
109 assert(rb);
110 assert(p);
111
112 const size_t copied = ringbuffer_peek(rb, 0, p, length);
113 rb->head += copied;
114 if (rb->head >= (rb->base + rb->total)) rb->head -= rb->total;
115
116 rb->available += copied;
117 return copied;
118 }
119