blob: 069a699375c555382533a40d5f952bbfbe7e9931 [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
24/*! \file strrb.c
25 * \brief Lossy string ringbuffer for logging; keeps newest messages.
26 */
27
28#include <stdio.h>
29#include <string.h>
Harald Welteb592ab72014-03-10 18:12:17 +010030#include <string.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000031
32#include <osmocom/core/strrb.h>
Harald Weltea5bb2b92017-05-15 12:50:58 +020033#include <osmocom/core/talloc.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000034
35/* Ringbuffer assumptions, invarients, and notes:
36 * - start is the index of the first used index slot in the ring buffer.
37 * - end is the index of the next index slot in the ring buffer.
38 * - start == end => buffer is empty
39 * - Consequence: the buffer can hold at most size - 1 messages
40 * (if this were not the case, full and empty buffers would be indistinguishable
41 * given the conventions in this implementation).
42 * - Whenever the ringbuffer is full, start is advanced. The second oldest
43 * message becomes unreachable by valid indexes (end is not a valid index)
44 * and the oldest message is overwritten (if there was a message there, which
45 * is the case unless this is the first time the ringbuffer becomes full).
46*/
47
48/*! \brief Create an empty, initialized osmo_strrb.
49 * \param[in] ctx The talloc memory context which should own this.
50 * \param[in] rb_size The number of message slots the osmo_strrb can hold.
51 * \returns A struct osmo_strrb* on success, NULL in case of error.
52 *
53 * This function creates and initializes a ringbuffer.
54 * Note that the ringbuffer stores at most rb_size - 1 messages.
55 */
56
57struct osmo_strrb *osmo_strrb_create(TALLOC_CTX * ctx, size_t rb_size)
58{
59 struct osmo_strrb *rb = NULL;
60 unsigned int i;
61
62 rb = talloc_zero(ctx, struct osmo_strrb);
63 if (!rb)
64 goto alloc_error;
65
66 /* start and end are zero already, which is correct */
67 rb->size = rb_size;
68
69 rb->buffer = talloc_array(rb, char *, rb->size);
70 if (!rb->buffer)
71 goto alloc_error;
72 for (i = 0; i < rb->size; i++) {
73 rb->buffer[i] =
74 talloc_zero_size(rb->buffer, RB_MAX_MESSAGE_SIZE);
75 if (!rb->buffer[i])
76 goto alloc_error;
77 }
78
79 return rb;
80
81alloc_error: /* talloc_free(NULL) is safe */
82 talloc_free(rb);
83 return NULL;
84}
85
86/*! \brief Check if an osmo_strrb is empty.
87 * \param[in] rb The osmo_strrb to check.
88 * \returns True if the osmo_strrb is empty, false otherwise.
89 */
90bool osmo_strrb_is_empty(const struct osmo_strrb *rb)
91{
92 return rb->end == rb->start;
93}
94
95/*! \brief Return a pointer to the Nth string in the osmo_strrb.
96 * \param[in] rb The osmo_strrb to search.
97 * \param[in] string_index The index sought (N), zero-indexed.
98 *
99 * Return a pointer to the Nth string in the osmo_strrb.
100 * Return NULL if there is no Nth string.
101 * Note that N is zero-indexed.
102 * \returns A pointer to the target string on success, NULL in case of error.
103 */
104const char *osmo_strrb_get_nth(const struct osmo_strrb *rb,
105 unsigned int string_index)
106{
107 unsigned int offset = string_index + rb->start;
108
109 if ((offset >= rb->size) && (rb->start > rb->end))
110 offset -= rb->size;
111 if (_osmo_strrb_is_bufindex_valid(rb, offset))
112 return rb->buffer[offset];
113
114 return NULL;
115}
116
117bool _osmo_strrb_is_bufindex_valid(const struct osmo_strrb *rb,
118 unsigned int bufi)
119{
120 if (osmo_strrb_is_empty(rb))
121 return 0;
Holger Hans Peter Freyther476cf332013-07-03 09:30:02 +0200122 if (bufi >= rb->size)
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000123 return 0;
124 if (rb->start < rb->end)
125 return (bufi >= rb->start) && (bufi < rb->end);
126 return (bufi < rb->end) || (bufi >= rb->start);
127}
128
129/*! \brief Count the number of log messages in an osmo_strrb.
130 * \param[in] rb The osmo_strrb to count the elements of.
131 *
132 * \returns The number of log messages in the osmo_strrb.
133 */
134size_t osmo_strrb_elements(const struct osmo_strrb *rb)
135{
136 if (rb->end < rb->start)
137 return rb->end + (rb->size - rb->start);
138
139 return rb->end - rb->start;
140}
141
142/*! \brief Add a string to the osmo_strrb.
143 * \param[in] rb The osmo_strrb to add to.
144 * \param[in] data The string to add.
145 *
146 * Add a message to the osmo_strrb.
147 * Older messages will be overwritten as necessary.
148 * \returns 0 normally, 1 as a warning (ie, if data was truncated).
149 */
150int osmo_strrb_add(struct osmo_strrb *rb, const char *data)
151{
152 size_t len = strlen(data);
153 int ret = 0;
154
155 if (len >= RB_MAX_MESSAGE_SIZE) {
156 len = RB_MAX_MESSAGE_SIZE - 1;
157 ret = 1;
158 }
159
160 memcpy(rb->buffer[rb->end], data, len);
161 rb->buffer[rb->end][len] = '\0';
162
163 rb->end += 1;
164 rb->end %= rb->size;
165
166 /* The buffer is full; oldest message is forgotten - see notes above */
167 if (rb->end == rb->start) {
168 rb->start += 1;
169 rb->start %= rb->size;
170 }
171 return ret;
172}