blob: 0c56be2475b72524eff09d66870e63439945046d [file] [log] [blame]
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00001/* Ringbuffer implementation, tailored for logging.
2 * This is a lossy ringbuffer. It keeps up to N of the newest messages,
3 * overwriting the oldest as newer ones come in.
4 *
5 * (C) 2012-2013, Katerina Barone-Adesi <kat.obsc@gmail.com>
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
Harald Welte33e940b2014-10-26 20:52:25 +010010 * the Free Software Foundation; either version 2 of the License, or
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welte96e2a002017-06-12 21:44:18 +020024/*! \addtogroup utils
25 * @{
26 */
27
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000028/*! \file strrb.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029 * Lossy string ringbuffer for logging; keeps newest messages.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000030 */
31
32#include <stdio.h>
33#include <string.h>
Harald Welteb592ab72014-03-10 18:12:17 +010034#include <string.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000035
36#include <osmocom/core/strrb.h>
Harald Weltea5bb2b92017-05-15 12:50:58 +020037#include <osmocom/core/talloc.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000038
39/* Ringbuffer assumptions, invarients, and notes:
40 * - start is the index of the first used index slot in the ring buffer.
41 * - end is the index of the next index slot in the ring buffer.
42 * - start == end => buffer is empty
43 * - Consequence: the buffer can hold at most size - 1 messages
44 * (if this were not the case, full and empty buffers would be indistinguishable
45 * given the conventions in this implementation).
46 * - Whenever the ringbuffer is full, start is advanced. The second oldest
47 * message becomes unreachable by valid indexes (end is not a valid index)
48 * and the oldest message is overwritten (if there was a message there, which
49 * is the case unless this is the first time the ringbuffer becomes full).
50*/
51
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052/*! Create an empty, initialized osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000053 * \param[in] ctx The talloc memory context which should own this.
54 * \param[in] rb_size The number of message slots the osmo_strrb can hold.
55 * \returns A struct osmo_strrb* on success, NULL in case of error.
56 *
57 * This function creates and initializes a ringbuffer.
58 * Note that the ringbuffer stores at most rb_size - 1 messages.
59 */
60
61struct osmo_strrb *osmo_strrb_create(TALLOC_CTX * ctx, size_t rb_size)
62{
63 struct osmo_strrb *rb = NULL;
64 unsigned int i;
65
66 rb = talloc_zero(ctx, struct osmo_strrb);
67 if (!rb)
68 goto alloc_error;
69
70 /* start and end are zero already, which is correct */
71 rb->size = rb_size;
72
73 rb->buffer = talloc_array(rb, char *, rb->size);
74 if (!rb->buffer)
75 goto alloc_error;
76 for (i = 0; i < rb->size; i++) {
77 rb->buffer[i] =
78 talloc_zero_size(rb->buffer, RB_MAX_MESSAGE_SIZE);
79 if (!rb->buffer[i])
80 goto alloc_error;
81 }
82
83 return rb;
84
85alloc_error: /* talloc_free(NULL) is safe */
86 talloc_free(rb);
87 return NULL;
88}
89
Neels Hofmeyr87e45502017-06-20 00:17:59 +020090/*! Check if an osmo_strrb is empty.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000091 * \param[in] rb The osmo_strrb to check.
92 * \returns True if the osmo_strrb is empty, false otherwise.
93 */
94bool osmo_strrb_is_empty(const struct osmo_strrb *rb)
95{
96 return rb->end == rb->start;
97}
98
Neels Hofmeyr87e45502017-06-20 00:17:59 +020099/*! Return a pointer to the Nth string in the osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000100 * \param[in] rb The osmo_strrb to search.
101 * \param[in] string_index The index sought (N), zero-indexed.
102 *
103 * Return a pointer to the Nth string in the osmo_strrb.
104 * Return NULL if there is no Nth string.
105 * Note that N is zero-indexed.
106 * \returns A pointer to the target string on success, NULL in case of error.
107 */
108const char *osmo_strrb_get_nth(const struct osmo_strrb *rb,
109 unsigned int string_index)
110{
111 unsigned int offset = string_index + rb->start;
112
113 if ((offset >= rb->size) && (rb->start > rb->end))
114 offset -= rb->size;
115 if (_osmo_strrb_is_bufindex_valid(rb, offset))
116 return rb->buffer[offset];
117
118 return NULL;
119}
120
121bool _osmo_strrb_is_bufindex_valid(const struct osmo_strrb *rb,
122 unsigned int bufi)
123{
124 if (osmo_strrb_is_empty(rb))
125 return 0;
Holger Hans Peter Freyther476cf332013-07-03 09:30:02 +0200126 if (bufi >= rb->size)
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000127 return 0;
128 if (rb->start < rb->end)
129 return (bufi >= rb->start) && (bufi < rb->end);
130 return (bufi < rb->end) || (bufi >= rb->start);
131}
132
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200133/*! Count the number of log messages in an osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000134 * \param[in] rb The osmo_strrb to count the elements of.
135 *
136 * \returns The number of log messages in the osmo_strrb.
137 */
138size_t osmo_strrb_elements(const struct osmo_strrb *rb)
139{
140 if (rb->end < rb->start)
141 return rb->end + (rb->size - rb->start);
142
143 return rb->end - rb->start;
144}
145
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200146/*! Add a string to the osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000147 * \param[in] rb The osmo_strrb to add to.
148 * \param[in] data The string to add.
149 *
150 * Add a message to the osmo_strrb.
151 * Older messages will be overwritten as necessary.
152 * \returns 0 normally, 1 as a warning (ie, if data was truncated).
153 */
154int osmo_strrb_add(struct osmo_strrb *rb, const char *data)
155{
156 size_t len = strlen(data);
157 int ret = 0;
158
159 if (len >= RB_MAX_MESSAGE_SIZE) {
160 len = RB_MAX_MESSAGE_SIZE - 1;
161 ret = 1;
162 }
163
164 memcpy(rb->buffer[rb->end], data, len);
165 rb->buffer[rb->end][len] = '\0';
166
167 rb->end += 1;
168 rb->end %= rb->size;
169
170 /* The buffer is full; oldest message is forgotten - see notes above */
171 if (rb->end == rb->start) {
172 rb->start += 1;
173 rb->start %= rb->size;
174 }
175 return ret;
176}
Harald Welte96e2a002017-06-12 21:44:18 +0200177
178/*! @} */