blob: 6bfb179f4b2200bf045cc8bdc6cf755651e5ddaa [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file strrb.c
2 * Ringbuffer implementation, tailored for logging.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +00003 * This is a lossy ringbuffer. It keeps up to N of the newest messages,
4 * overwriting the oldest as newer ones come in.
5 *
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02006 * Ringbuffer assumptions, invarients, and notes:
7 * - start is the index of the first used index slot in the ring buffer.
8 * - end is the index of the next index slot in the ring buffer.
9 * - start == end => buffer is empty
10 * - Consequence: the buffer can hold at most size - 1 messages
11 * (if this were not the case, full and empty buffers would be indistinguishable
12 * given the conventions in this implementation).
13 * - Whenever the ringbuffer is full, start is advanced. The second oldest
14 * message becomes unreachable by valid indexes (end is not a valid index)
15 * and the oldest message is overwritten (if there was a message there, which
16 * is the case unless this is the first time the ringbuffer becomes full).
17 */
18/*
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000019 * (C) 2012-2013, Katerina Barone-Adesi <kat.obsc@gmail.com>
20 * All Rights Reserved
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
Harald Welte33e940b2014-10-26 20:52:25 +010024 * the Free Software Foundation; either version 2 of the License, or
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000025 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License along
33 * with this program; if not, write to the Free Software Foundation, Inc.,
34 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 *
36 */
37
Harald Welte96e2a002017-06-12 21:44:18 +020038/*! \addtogroup utils
39 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020040 * \file strrb.c */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000041
42#include <stdio.h>
43#include <string.h>
Harald Welteb592ab72014-03-10 18:12:17 +010044#include <string.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000045
46#include <osmocom/core/strrb.h>
Harald Weltea5bb2b92017-05-15 12:50:58 +020047#include <osmocom/core/talloc.h>
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000048
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049/*! Create an empty, initialized osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000050 * \param[in] ctx The talloc memory context which should own this.
51 * \param[in] rb_size The number of message slots the osmo_strrb can hold.
52 * \returns A struct osmo_strrb* on success, NULL in case of error.
53 *
54 * This function creates and initializes a ringbuffer.
55 * Note that the ringbuffer stores at most rb_size - 1 messages.
56 */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000057struct 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020086/*! Check if an osmo_strrb is empty.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000087 * \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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! Return a pointer to the Nth string in the osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +000096 * \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
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! Count the number of log messages in an osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000130 * \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
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200142/*! Add a string to the osmo_strrb.
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000143 * \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}
Harald Welte96e2a002017-06-12 21:44:18 +0200173
174/*! @} */