summaryrefslogtreecommitdiff
path: root/main/data_buffer.c
blob: ccbffd22dae62528ee7ffdc350bd1cf8ad50f213 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2018, Digium, Inc.
 *
 * Joshua Colp <jcolp@digium.com>
 * Ben Ford <bford@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Data Buffer API
 *
 * \author Joshua Colp <jcolp@digium.com>
 * \author Ben Ford <bford@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/logger.h"
#include "asterisk/strings.h"
#include "asterisk/data_buffer.h"
#include "asterisk/linkedlists.h"

/*!
 * \brief The number of payloads to increment the cache by
 */
#define CACHED_PAYLOAD_MAX 5

/*!
 * \brief Payload entry placed inside of the data buffer list
 */
struct data_buffer_payload_entry {
	/*! \brief The payload for this position */
	void *payload;
	/*! \brief The provided position for this */
	size_t pos;
	/*! \brief Linked list information */
	AST_LIST_ENTRY(data_buffer_payload_entry) list;
};

/*!
 * \brief Data buffer containing fixed number of data payloads
 */
struct ast_data_buffer {
	/*! \brief Callback function to free a data payload */
	ast_data_buffer_free_callback free_fn;
	/*! \brief A linked list of data payloads */
	AST_LIST_HEAD_NOLOCK(, data_buffer_payload_entry) payloads;
	/*! \brief A linked list of unused cached data payloads */
	AST_LIST_HEAD_NOLOCK(, data_buffer_payload_entry) cached_payloads;
	/*! \brief The current number of data payloads in the buffer */
	size_t count;
	/*! \brief Maximum number of data payloads in the buffer */
	size_t max;
	/*! \brief The current number of data payloads in the cache */
	size_t cache_count;
};

static void free_fn_do_nothing(void *data)
{
	return;
}

/*!
 * \brief Helper function to allocate a data payload
 */
static struct data_buffer_payload_entry *data_buffer_payload_alloc(void *payload, size_t pos)
{
	struct data_buffer_payload_entry *data_payload;

	data_payload = ast_calloc(1, sizeof(*data_payload));
	if (!data_payload) {
		return NULL;
	}

	data_payload->payload = payload;
	data_payload->pos = pos;

	return data_payload;
}

/*!
 * \brief Helper function that sets the cache to its maximum number of payloads
 */
static void ast_data_buffer_cache_adjust(struct ast_data_buffer *buffer)
{
	int buffer_space;

	ast_assert(buffer != NULL);

	buffer_space = buffer->max - buffer->count;

	if (buffer->cache_count == buffer_space) {
		return;
	}

	if (buffer->cache_count < buffer_space) {
		/* Add payloads to the cache, if able */
		while (buffer->cache_count < CACHED_PAYLOAD_MAX && buffer->cache_count < buffer_space) {
			struct data_buffer_payload_entry *buffer_payload;

			buffer_payload = data_buffer_payload_alloc(NULL, -1);
			if (buffer_payload) {
				AST_LIST_INSERT_TAIL(&buffer->cached_payloads, buffer_payload, list);
				buffer->cache_count++;
				continue;
			}

			ast_log(LOG_ERROR, "Failed to allocate memory to the cache.");
			break;
		}
	} else if (buffer->cache_count > buffer_space) {
		/* Remove payloads from the cache */
		while (buffer->cache_count > buffer_space) {
			struct data_buffer_payload_entry *buffer_payload;

			buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->cached_payloads, list);
			if (buffer_payload) {
				ast_free(buffer_payload);
				buffer->cache_count--;
				continue;
			}

			ast_log(LOG_ERROR, "Failed to remove memory from the cache.");
			break;
		}
	}
}

struct ast_data_buffer *ast_data_buffer_alloc(ast_data_buffer_free_callback free_fn, size_t size)
{
	struct ast_data_buffer *buffer;

	ast_assert(size != 0);

	buffer = ast_calloc(1, sizeof(*buffer));
	if (!buffer) {
		return NULL;
	}

	AST_LIST_HEAD_INIT_NOLOCK(&buffer->payloads);
	AST_LIST_HEAD_INIT_NOLOCK(&buffer->cached_payloads);

	/* If free_fn is NULL, just use free_fn_do_nothing as a default */
	buffer->free_fn = free_fn ? free_fn : free_fn_do_nothing;
	buffer->max = size;

	ast_data_buffer_cache_adjust(buffer);

	return buffer;
}

void ast_data_buffer_resize(struct ast_data_buffer *buffer, size_t size)
{
	struct data_buffer_payload_entry *existing_payload;

	ast_assert(buffer != NULL);

	/* The buffer must have at least a size of 1 */
	ast_assert(size > 0);

	if (buffer->max == size) {
		return;
	}

	/* If the size is decreasing, some payloads will need to be freed */
	if (buffer->max > size) {
		int remove = buffer->max - size;

		AST_LIST_TRAVERSE_SAFE_BEGIN(&buffer->payloads, existing_payload, list) {
			if (remove) {
				AST_LIST_REMOVE_HEAD(&buffer->payloads, list);
				buffer->free_fn(existing_payload->payload);
				ast_free(existing_payload);
				buffer->count--;
				remove--;
				continue;
			}
			break;
		}
		AST_LIST_TRAVERSE_SAFE_END;
	}

	buffer->max = size;
	ast_data_buffer_cache_adjust(buffer);
}

int ast_data_buffer_put(struct ast_data_buffer *buffer, size_t pos, void *payload)
{
	struct data_buffer_payload_entry *buffer_payload = NULL;
	struct data_buffer_payload_entry *existing_payload;
	int inserted = 0;

	ast_assert(buffer != NULL);
	ast_assert(payload != NULL);

	/* If the data buffer has reached its maximum size then the head goes away and
	 * we will reuse its buffer payload
	 */
	if (buffer->count == buffer->max) {
		buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->payloads, list);
		buffer->free_fn(buffer_payload->payload);
		buffer->count--;

		/* Update this buffer payload with its new information */
		buffer_payload->payload = payload;
		buffer_payload->pos = pos;
	}
	if (!buffer_payload) {
		if (!buffer->cache_count) {
			ast_data_buffer_cache_adjust(buffer);
		}
		buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->cached_payloads, list);
		buffer->cache_count--;

		/* Update the payload from the cache with its new information */
		buffer_payload->payload = payload;
		buffer_payload->pos = pos;
	}
	if (!buffer_payload) {
		return -1;
	}

	/* Given the position find its ideal spot within the buffer */
	AST_LIST_TRAVERSE_SAFE_BEGIN(&buffer->payloads, existing_payload, list) {
		/* If it's already in the buffer, drop it */
		if (existing_payload->pos == pos) {
			ast_debug(3, "Packet with position %zu is already in buffer. Not inserting.\n", pos);
			inserted = -1;
			break;
		}

		if (existing_payload->pos > pos) {
			AST_LIST_INSERT_BEFORE_CURRENT(buffer_payload, list);
			inserted = 1;
			break;
		}
	}
	AST_LIST_TRAVERSE_SAFE_END;

	if (inserted == -1) {
		return 0;
	}

	if (!inserted) {
		AST_LIST_INSERT_TAIL(&buffer->payloads, buffer_payload, list);
	}

	buffer->count++;

	return 0;
}

void *ast_data_buffer_get(const struct ast_data_buffer *buffer, size_t pos)
{
	struct data_buffer_payload_entry *buffer_payload;

	ast_assert(buffer != NULL);

	AST_LIST_TRAVERSE(&buffer->payloads, buffer_payload, list) {
		if (buffer_payload->pos == pos) {
			return buffer_payload->payload;
		}
	}

	return NULL;
}

void ast_data_buffer_free(struct ast_data_buffer *buffer)
{
	struct data_buffer_payload_entry *buffer_payload;

	ast_assert(buffer != NULL);

	while ((buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->payloads, list))) {
		buffer->free_fn(buffer_payload->payload);
		ast_free(buffer_payload);
	}

	while ((buffer_payload = AST_LIST_REMOVE_HEAD(&buffer->cached_payloads, list))) {
		ast_free(buffer_payload);
	}

	ast_free(buffer);
}

size_t ast_data_buffer_count(const struct ast_data_buffer *buffer)
{
	ast_assert(buffer != NULL);

	return buffer->count;
}

size_t ast_data_buffer_max(const struct ast_data_buffer *buffer)
{
	ast_assert(buffer != NULL);

	return buffer->max;
}