* [PATCH v2] ringbuf: Add l_ringbuf_append function
@ 2020-01-22 11:56 =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
2020-01-24 12:59 ` =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
2020-01-24 20:39 ` Denis Kenzior
0 siblings, 2 replies; 3+ messages in thread
From: =?unknown-8bit?q?Przemys=C5=82aw?= Fierek @ 2020-01-22 11:56 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 3339 bytes --]
---
ell/ell.sym | 1 +
ell/ringbuf.c | 69 ++++++++++++++++++++++++++++++++++++---------------
ell/ringbuf.h | 2 ++
3 files changed, 52 insertions(+), 20 deletions(-)
diff --git a/ell/ell.sym b/ell/ell.sym
index d759de6..46df1bd 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -395,6 +395,7 @@ global:
l_ringbuf_printf;
l_ringbuf_vprintf;
l_ringbuf_read;
+ l_ringbuf_append;
/* settings */
l_settings_new;
l_settings_free;
diff --git a/ell/ringbuf.c b/ell/ringbuf.c
index 49f370c..7c9777c 100644
--- a/ell/ringbuf.c
+++ b/ell/ringbuf.c
@@ -321,7 +321,7 @@ LIB_EXPORT int l_ringbuf_printf(struct l_ringbuf *ringbuf,
LIB_EXPORT int l_ringbuf_vprintf(struct l_ringbuf *ringbuf,
const char *format, va_list ap)
{
- size_t avail, offset, end;
+ size_t avail;
char *str;
int len;
@@ -342,28 +342,10 @@ LIB_EXPORT int l_ringbuf_vprintf(struct l_ringbuf *ringbuf,
return -1;
}
- /* Determine possible length of string before wrapping */
- offset = ringbuf->in & (ringbuf->size - 1);
- end = minsize((size_t) len, ringbuf->size - offset);
- memcpy(ringbuf->buffer + offset, str, end);
-
- if (ringbuf->in_tracing)
- ringbuf->in_tracing(ringbuf->buffer + offset, end,
- ringbuf->in_data);
-
- if (len - end > 0) {
- /* Put the remainder of string at the beginning */
- memcpy(ringbuf->buffer, str + end, len - end);
-
- if (ringbuf->in_tracing)
- ringbuf->in_tracing(ringbuf->buffer, len - end,
- ringbuf->in_data);
- }
+ len = l_ringbuf_append(ringbuf, str, (size_t) len);
l_free(str);
- ringbuf->in += len;
-
return len;
}
@@ -420,3 +402,50 @@ LIB_EXPORT ssize_t l_ringbuf_read(struct l_ringbuf *ringbuf, int fd)
return consumed;
}
+
+/**
+ * l_ringbuf_append:
+ * @ringbuf: Ring Buffer object
+ * @data: data to be appended
+ * @len: data length
+ *
+ * Appends data to the ring buffer.
+ *
+ * Returns: Number of appended bytes or -1 if the append failed.
+ **/
+ssize_t l_ringbuf_append(struct l_ringbuf *ringbuf, void *data, size_t len)
+{
+ size_t avail, offset, end, left;
+
+ if (!ringbuf || data == NULL)
+ return -1;
+
+ /* Determine how much can actually be appended */
+ avail = ringbuf->size - ringbuf->in + ringbuf->out;
+
+ if (!avail)
+ return -1;
+
+ /* Determine how much to append before wrapping */
+ offset = ringbuf->in & (ringbuf->size - 1);
+ end = minsize(len, ringbuf->size - offset);
+ memcpy(ringbuf->buffer + offset, data, end);
+
+ if (ringbuf->in_tracing)
+ ringbuf->in_tracing(ringbuf->buffer + offset, end,
+ ringbuf->in_data);
+
+ left = minsize(avail - end, len - end);
+
+ if (left > 0) {
+ memcpy(ringbuf->buffer, data + end, left);
+
+ if (ringbuf->in_tracing)
+ ringbuf->in_tracing(ringbuf->buffer, left,
+ ringbuf->in_data);
+ }
+
+ ringbuf->in += end + left;
+
+ return (end + left);
+}
diff --git a/ell/ringbuf.h b/ell/ringbuf.h
index 846f7a5..ba41324 100644
--- a/ell/ringbuf.h
+++ b/ell/ringbuf.h
@@ -58,6 +58,8 @@ int l_ringbuf_vprintf(struct l_ringbuf *ringbuf, const char *format,
va_list ap);
ssize_t l_ringbuf_read(struct l_ringbuf *ringbuf, int fd);
+ssize_t l_ringbuf_append(struct l_ringbuf *ringbuf, void *data, size_t len);
+
#ifdef __cplusplus
}
#endif
--
2.17.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] ringbuf: Add l_ringbuf_append function
2020-01-22 11:56 [PATCH v2] ringbuf: Add l_ringbuf_append function =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
@ 2020-01-24 12:59 ` =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
2020-01-24 20:39 ` Denis Kenzior
1 sibling, 0 replies; 3+ messages in thread
From: =?unknown-8bit?q?Przemys=C5=82aw?= Fierek @ 2020-01-24 12:59 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 3797 bytes --]
On 22.01.2020 12:56, Przemysław Fierek wrote:
> ---
> ell/ell.sym | 1 +
> ell/ringbuf.c | 69 ++++++++++++++++++++++++++++++++++++---------------
> ell/ringbuf.h | 2 ++
> 3 files changed, 52 insertions(+), 20 deletions(-)
>
> diff --git a/ell/ell.sym b/ell/ell.sym
> index d759de6..46df1bd 100644
> --- a/ell/ell.sym
> +++ b/ell/ell.sym
> @@ -395,6 +395,7 @@ global:
> l_ringbuf_printf;
> l_ringbuf_vprintf;
> l_ringbuf_read;
> + l_ringbuf_append;
> /* settings */
> l_settings_new;
> l_settings_free;
> diff --git a/ell/ringbuf.c b/ell/ringbuf.c
> index 49f370c..7c9777c 100644
> --- a/ell/ringbuf.c
> +++ b/ell/ringbuf.c
> @@ -321,7 +321,7 @@ LIB_EXPORT int l_ringbuf_printf(struct l_ringbuf *ringbuf,
> LIB_EXPORT int l_ringbuf_vprintf(struct l_ringbuf *ringbuf,
> const char *format, va_list ap)
> {
> - size_t avail, offset, end;
> + size_t avail;
> char *str;
> int len;
>
> @@ -342,28 +342,10 @@ LIB_EXPORT int l_ringbuf_vprintf(struct l_ringbuf *ringbuf,
> return -1;
> }
>
> - /* Determine possible length of string before wrapping */
> - offset = ringbuf->in & (ringbuf->size - 1);
> - end = minsize((size_t) len, ringbuf->size - offset);
> - memcpy(ringbuf->buffer + offset, str, end);
> -
> - if (ringbuf->in_tracing)
> - ringbuf->in_tracing(ringbuf->buffer + offset, end,
> - ringbuf->in_data);
> -
> - if (len - end > 0) {
> - /* Put the remainder of string at the beginning */
> - memcpy(ringbuf->buffer, str + end, len - end);
> -
> - if (ringbuf->in_tracing)
> - ringbuf->in_tracing(ringbuf->buffer, len - end,
> - ringbuf->in_data);
> - }
> + len = l_ringbuf_append(ringbuf, str, (size_t) len);
>
> l_free(str);
>
> - ringbuf->in += len;
> -
> return len;
> }
>
> @@ -420,3 +402,50 @@ LIB_EXPORT ssize_t l_ringbuf_read(struct l_ringbuf *ringbuf, int fd)
>
> return consumed;
> }
> +
> +/**
> + * l_ringbuf_append:
> + * @ringbuf: Ring Buffer object
> + * @data: data to be appended
> + * @len: data length
> + *
> + * Appends data to the ring buffer.
> + *
> + * Returns: Number of appended bytes or -1 if the append failed.
> + **/
> +ssize_t l_ringbuf_append(struct l_ringbuf *ringbuf, void *data, size_t len)
> +{
> + size_t avail, offset, end, left;
> +
> + if (!ringbuf || data == NULL)
> + return -1;
> +
> + /* Determine how much can actually be appended */
> + avail = ringbuf->size - ringbuf->in + ringbuf->out;
> +
> + if (!avail)
> + return -1;
> +
> + /* Determine how much to append before wrapping */
> + offset = ringbuf->in & (ringbuf->size - 1);
> + end = minsize(len, ringbuf->size - offset);
> + memcpy(ringbuf->buffer + offset, data, end);
> +
> + if (ringbuf->in_tracing)
> + ringbuf->in_tracing(ringbuf->buffer + offset, end,
> + ringbuf->in_data);
> +
> + left = minsize(avail - end, len - end);
> +
> + if (left > 0) {
> + memcpy(ringbuf->buffer, data + end, left);
> +
> + if (ringbuf->in_tracing)
> + ringbuf->in_tracing(ringbuf->buffer, left,
> + ringbuf->in_data);
> + }
> +
> + ringbuf->in += end + left;
> +
> + return (end + left);
> +}
> diff --git a/ell/ringbuf.h b/ell/ringbuf.h
> index 846f7a5..ba41324 100644
> --- a/ell/ringbuf.h
> +++ b/ell/ringbuf.h
> @@ -58,6 +58,8 @@ int l_ringbuf_vprintf(struct l_ringbuf *ringbuf, const char *format,
> va_list ap);
> ssize_t l_ringbuf_read(struct l_ringbuf *ringbuf, int fd);
>
> +ssize_t l_ringbuf_append(struct l_ringbuf *ringbuf, void *data, size_t len);
> +
> #ifdef __cplusplus
> }
> #endif
Hi,
I would ask about status of this patch. Does it require any extra
changes or it is ready to merge?
Regards,
Przemek
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] ringbuf: Add l_ringbuf_append function
2020-01-22 11:56 [PATCH v2] ringbuf: Add l_ringbuf_append function =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
2020-01-24 12:59 ` =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
@ 2020-01-24 20:39 ` Denis Kenzior
1 sibling, 0 replies; 3+ messages in thread
From: Denis Kenzior @ 2020-01-24 20:39 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 779 bytes --]
Hi Przemysław,
On 1/22/20 5:56 AM, Przemysław Fierek wrote:
> ---
> ell/ell.sym | 1 +
> ell/ringbuf.c | 69 ++++++++++++++++++++++++++++++++++++---------------
> ell/ringbuf.h | 2 ++
> 3 files changed, 52 insertions(+), 20 deletions(-)
>
<snip>
> +ssize_t l_ringbuf_append(struct l_ringbuf *ringbuf, void *data, size_t len)
> +{
> + size_t avail, offset, end, left;
> +
Do note that Linux Kernel coding style wants one variable declaration
per line. I know the current code is a bit lax in doing that, but for
new code or changes this is what we prefer. Refer to:
https://www.kernel.org/doc/html/latest/process/coding-style.html,
Section 8, last paragraph.
I manually amended this patch to fix that and applied.
Regards,
-Denis
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-01-24 20:39 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-22 11:56 [PATCH v2] ringbuf: Add l_ringbuf_append function =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
2020-01-24 12:59 ` =?unknown-8bit?q?Przemys=C5=82aw?= Fierek
2020-01-24 20:39 ` Denis Kenzior
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.