File: circular_buffer.c

package info (click to toggle)
aircrack-ng 1%3A1.6%2Bgit20210130.91820bc-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,056 kB
  • sloc: ansic: 67,045; cs: 5,392; sh: 3,773; python: 2,565; pascal: 1,074; asm: 570; makefile: 253; cpp: 46
file content (177 lines) | stat: -rw-r--r-- 4,631 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * Copyright (C) 2018 Joseph Benden <joe@benden.us>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

#include "aircrack-ng/defs.h"
#include "aircrack-ng/adt/circular_buffer.h"

#ifndef NDEBUG
static inline bool is_power_of_two(size_t n)
{
	REQUIRE(n > 0);

	while ((n % 2) == 0)
	{
		n /= 2;
	}

	if (n == 1) return true;

	return false;
}
#endif

// The definition of our circular buffer is hidden from the API user.
struct circular_buffer_t
{
	uint8_t * buffer; /// Circular buffer's memory location.
	size_t read_pos; /// Current read position, as element index.
	size_t write_pos; /// Current write position, as element index.
	size_t max; /// Number of bytes allocated for whole ring buffer.
	size_t size; /// Number of bytes required for a single element.
};

/*
 * A circular buffer uses the "Virtual Streams" approach, as described on
 * the Ryg blog:
 *
 * https://fgiesen.wordpress.com/2010/12/14/ring-buffers-and-queues/
 */

#define CBUF_BUFFER_POS(cbuf, which)                                           \
	(cbuf->buffer + ((cbuf->which % (cbuf->max / cbuf->size)) * cbuf->size))

static inline void check_invariants(cbuf_handle_t cbuf)
{
#ifdef NDEBUG
	(void) cbuf;
#endif

	// All writes to structure are always ahead of the reads, unless empty.
	INVARIANT(cbuf->write_pos >= cbuf->read_pos);

	// All writes are restricted to the inside of our buffer's region.
	INVARIANT(cbuf->write_pos - cbuf->read_pos <= (cbuf->max / cbuf->size));
}

API_EXPORT cbuf_handle_t circular_buffer_init(uint8_t * buffer,
											  size_t bufferSize,
											  size_t elementSize)
{
	REQUIRE(buffer && bufferSize && elementSize);
	REQUIRE(bufferSize % elementSize == 0);
	REQUIRE(is_power_of_two(bufferSize));

	cbuf_handle_t cbuf = calloc(1, sizeof(circular_buffer_t));
	ALLEGE(cbuf);

	cbuf->buffer = buffer;
	cbuf->max = bufferSize;
	cbuf->size = elementSize;
	circular_buffer_reset(cbuf);

	ENSURE(circular_buffer_is_empty(cbuf));

	return cbuf;
}

API_EXPORT void circular_buffer_free(cbuf_handle_t cbuf)
{
	REQUIRE(cbuf);
	cbuf->buffer = NULL;
	free(cbuf);
}

API_EXPORT void circular_buffer_reset(cbuf_handle_t cbuf)
{
	REQUIRE(cbuf);

	cbuf->read_pos = 0;
	cbuf->write_pos = 0;
}

API_EXPORT bool circular_buffer_is_empty(cbuf_handle_t cbuf)
{
	REQUIRE(cbuf);
	return cbuf->read_pos == cbuf->write_pos;
}

API_EXPORT bool circular_buffer_is_full(cbuf_handle_t cbuf)
{
	REQUIRE(cbuf);
	return cbuf->write_pos == (cbuf->read_pos + (cbuf->max / cbuf->size));
}

API_EXPORT size_t circular_buffer_capacity(cbuf_handle_t cbuf)
{
	REQUIRE(cbuf);
	return cbuf->max / cbuf->size;
}

API_EXPORT size_t circular_buffer_size(cbuf_handle_t cbuf)
{
	REQUIRE(cbuf);
	return cbuf->write_pos - cbuf->read_pos;
}

API_EXPORT void
circular_buffer_put(cbuf_handle_t cbuf, void const * const data, size_t size)
{
	REQUIRE(cbuf && data && size > 0);
	REQUIRE(size <= cbuf->size);

	memcpy(CBUF_BUFFER_POS(cbuf, write_pos), data, size); // cannot overlap

	if (size < cbuf->size)
	{
		// zero extra buffer bytes
		memset(CBUF_BUFFER_POS(cbuf, write_pos) + size, 0, cbuf->size - size);
	}

	++cbuf->write_pos;

	check_invariants(cbuf);
}

API_EXPORT void
circular_buffer_get(cbuf_handle_t cbuf, void * const * data, size_t size)
{
	REQUIRE(cbuf && data && size > 0);
	REQUIRE(size <= cbuf->size);

	memcpy(*data, CBUF_BUFFER_POS(cbuf, read_pos), size); // cannot overlap

	++cbuf->read_pos;

	check_invariants(cbuf);
}