membuff: Add tests and update to support a flag for empty/full
Subsystems (3)
cover membuff: Add tests and update to support a flag for empty/full
The membuff implementation curently has no tests. It also assumes that head and tail can never correspond unless the buffer is empty.
This series provides a compile-time flag to support a 'full' flag. It also adds some tests of the main routines.
The data structure is also renamed to membuf which fits better with U-Boot.
There may be some cases in the code which could be optimised a little, but the implementation is functional.
Simon Glass (8):
membuff: Rename functions to have membuf_ prefix
membuff: Rename the files to membuf
membuf: Rename struct
membuf: Include stdbool
membuf: Correct implementation of membuf_dispose()
membuf: Add some tests
membuf: Minor code-style improvements
membuf: Support a flag for being full
MAINTAINERS | 7 +
arch/sandbox/include/asm/serial.h | 2 +-
boot/bootmeth_extlinux.c | 8 +-
common/console.c | 32 ++--
drivers/serial/sandbox.c | 10 +-
drivers/usb/emul/sandbox_keyb.c | 8 +-
include/asm-generic/global_data.h | 6 +-
include/{membuff.h => membuf.h} | 98 ++++++------
lib/Makefile | 2 +-
lib/{membuff.c => membuf.c} | 134 ++++++++++------
test/lib/Makefile | 1 +
test/lib/membuf.c | 244 ++++++++++++++++++++++++++++++
12 files changed, 427 insertions(+), 125 deletions(-)
rename include/{membuff.h => membuf.h} (67%)
rename lib/{membuff.c => membuf.c} (69%)
create mode 100644 test/lib/membuf.cSignature · 1 line
2.34.1
PATCH 1/8 membuff: Rename functions to have membuf_ prefix
The double 'f' is not necessary and is a bit annoying as elsewhere in U-Boot we use 'buf'. Rename all the functions before it is used more widely.
---
boot/bootmeth_extlinux.c | 6 +-- common/console.c | 32 ++++++------- drivers/serial/sandbox.c | 10 ++-- drivers/usb/emul/sandbox_keyb.c | 6 +-- include/membuff.h | 82 ++++++++++++++++----------------- lib/membuff.c | 66 +++++++++++++------------- 6 files changed, 101 insertions(+), 101 deletions(-)
boot/bootmeth_extlinux.c +3 -3
@@ -112,9 +112,9 @@ static int extlinux_fill_info(struct bootflow *bflow)int len;log_debug ("parsing bflow file size %x\n" , bflow->size); -membuff_init (&mb, bflow->buf, bflow->size); -membuff_putraw (&mb, bflow->size, true, &data); -while (len =membuff_readline (&mb, line,sizeof (line) -1 ,' ' , true), len) { +membuf_init (&mb, bflow->buf, bflow->size); +membuf_putraw (&mb, bflow->size, true, &data); +while (len =membuf_readline (&mb, line,sizeof (line) -1 ,' ' , true), len) {char *tok, *p = line; tok =strsep (&p," " );
common/console.c +16 -16
@@ -101,7 +101,7 @@ static void console_record_putc(const char c)if (!(gd->flags &GD_FLG_RECORD ))return ;if (gd->console_out.start && - !membuff_putbyte ((struct membuff *)&gd->console_out, c)) + !membuf_putbyte ((struct membuff *)&gd->console_out, c)) gd->flags |=GD_FLG_RECORD_OVF ; }
@@ -112,7 +112,7 @@ static void console_record_puts(const char *s)if (gd->console_out.start) {int len =strlen (s); -if (membuff_put ((struct membuff *)&gd->console_out, s, len) != +if (membuf_put ((struct membuff *)&gd->console_out, s, len) != len) gd->flags |=GD_FLG_RECORD_OVF ; }
@@ -125,7 +125,7 @@ static int console_record_getc(void)if (!gd->console_in.start)return -1 ; -return membuff_getbyte ((struct membuff *)&gd->console_in); +return membuf_getbyte ((struct membuff *)&gd->console_in); }static int console_record_tstc (void )
@@ -133,7 +133,7 @@ static int console_record_tstc(void)if (!(gd->flags &GD_FLG_RECORD ))return 0 ;if (gd->console_in.start) { -if (membuff_peekbyte ((struct membuff *)&gd->console_in) !=-1 ) +if (membuf_peekbyte ((struct membuff *)&gd->console_in) !=-1 )return 1 ; }return 0 ;
@@ -814,14 +814,14 @@ int console_record_init(void) {int ret; - ret =membuff_new ((struct membuff *)&gd->console_out, - gd->flags &GD_FLG_RELOC ? -CONFIG_CONSOLE_RECORD_OUT_SIZE : -CONFIG_CONSOLE_RECORD_OUT_SIZE_F ); + ret =membuf_new ((struct membuff *)&gd->console_out, + gd->flags &GD_FLG_RELOC ? +CONFIG_CONSOLE_RECORD_OUT_SIZE : +CONFIG_CONSOLE_RECORD_OUT_SIZE_F );if (ret)return ret; - ret =membuff_new ((struct membuff *)&gd->console_in, -CONFIG_CONSOLE_RECORD_IN_SIZE ); + ret =membuf_new ((struct membuff *)&gd->console_in, +CONFIG_CONSOLE_RECORD_IN_SIZE );/* Start recording from the beginning */ gd->flags |=GD_FLG_RECORD ;
@@ -831,8 +831,8 @@ int console_record_init(void)void console_record_reset (void ) { -membuff_purge ((struct membuff *)&gd->console_out); -membuff_purge ((struct membuff *)&gd->console_in); +membuf_purge ((struct membuff *)&gd->console_out); +membuf_purge ((struct membuff *)&gd->console_in); gd->flags &= ~GD_FLG_RECORD_OVF ; }
@@ -851,23 +851,23 @@ int console_record_readline(char *str, int maxlen)if (console_record_isempty ())return -ENOENT ; -return membuff_readline ((struct membuff *)&gd->console_out, str, +return membuf_readline ((struct membuff *)&gd->console_out, str, maxlen, '\0', false); }int console_record_avail (void ) { -return membuff_avail ((struct membuff *)&gd->console_out); +return membuf_avail ((struct membuff *)&gd->console_out); }bool console_record_isempty (void ) { -return membuff_isempty ((struct membuff *)&gd->console_out); +return membuf_isempty ((struct membuff *)&gd->console_out); }int console_in_puts (const char *str) { -return membuff_put ((struct membuff *)&gd->console_in, str,strlen (str)); +return membuf_put ((struct membuff *)&gd->console_in, str,strlen (str)); }#endif
drivers/serial/sandbox.c +5 -5
@@ -63,7 +63,7 @@ static int sandbox_serial_probe(struct udevice *dev)if (state->term_raw !=STATE_TERM_RAW )disable_ctrlc (1 ); -membuff_init (&priv->buf, priv->serial_buf,sizeof (priv->serial_buf)); +membuf_init (&priv->buf, priv->serial_buf,sizeof (priv->serial_buf));return 0 ; }
@@ -138,15 +138,15 @@ static int sandbox_serial_pending(struct udevice *dev, bool input)return 0 ;os_usleep (100 ); - avail =membuff_putraw (&priv->buf,100 , false, &data); + avail =membuf_putraw (&priv->buf,100 , false, &data);if (!avail)return 1 ;/* buffer full */ count =os_read (0 , data, avail);if (count >0 ) -membuff_putraw (&priv->buf, count, true, &data); +membuf_putraw (&priv->buf, count, true, &data); -return membuff_avail (&priv->buf); +return membuf_avail (&priv->buf); }static int sandbox_serial_getc (struct udevice *dev)
@@ -156,7 +156,7 @@ static int sandbox_serial_getc(struct udevice *dev)if (!sandbox_serial_pending (dev, true))return -EAGAIN ;/* buffer empty */ -return membuff_getbyte (&priv->buf); +return membuf_getbyte (&priv->buf); }#ifdef CONFIG_DEBUG_UART_SANDBOX
drivers/usb/emul/sandbox_keyb.c +3 -3
@@ -167,7 +167,7 @@ int sandbox_usb_keyb_add_string(struct udevice *dev,struct sandbox_keyb_priv *priv =dev_get_priv (dev);int ret; - ret =membuff_put (&priv->in, scancode,USB_KBD_BOOT_REPORT_SIZE ); + ret =membuf_put (&priv->in, scancode,USB_KBD_BOOT_REPORT_SIZE );if (ret !=USB_KBD_BOOT_REPORT_SIZE )return -ENOSPC ;
@@ -194,7 +194,7 @@ static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev,if (length <USB_KBD_BOOT_REPORT_SIZE )return 0 ; -membuff_get (&priv->in, buffer,USB_KBD_BOOT_REPORT_SIZE ); +membuf_get (&priv->in, buffer,USB_KBD_BOOT_REPORT_SIZE );return 0 ; }
@@ -220,7 +220,7 @@ static int sandbox_keyb_probe(struct udevice *dev)struct sandbox_keyb_priv *priv =dev_get_priv (dev);/* Provide an 80 character keyboard buffer */ -return membuff_new (&priv->in,80 *USB_KBD_BOOT_REPORT_SIZE ); +return membuf_new (&priv->in,80 *USB_KBD_BOOT_REPORT_SIZE ); }static const struct dm_usb_ops sandbox_usb_keyb_ops = {
include/membuff.h +41 -41
224 lines not on this page · raw message
lib/membuff.c +33 -33
202 lines not on this page · raw message
Signature · 1 line
2.34.1
Simon Glass
AppliedRe: [PATCH 1/8] membuff: Rename functions to have membuf_ prefix
The double 'f' is not necessary and is a bit annoying as elsewhere in U-Boot we use 'buf'. Rename all the functions before it is used more widely.
---
boot/bootmeth_extlinux.c | 6 +-- common/console.c | 32 ++++++------- drivers/serial/sandbox.c | 10 ++-- drivers/usb/emul/sandbox_keyb.c | 6 +-- include/membuff.h | 82 ++++++++++++++++----------------- lib/membuff.c | 66 +++++++++++++------------- 6 files changed, 101 insertions(+), 101 deletions(-)
Applied to sjg/master, thanks!
PATCH 2/8 membuff: Rename the files to membuf
In reply to Simon GlassRename the C and header files to use the membuf basename, to match the functions.
Add a MAINTAINERS entry while we are here.
---
MAINTAINERS | 7 +++++++
include/asm-generic/global_data.h | 2 +-
include/{membuff.h => membuf.h} | 0
lib/Makefile | 2 +-
lib/{membuff.c => membuf.c} | 2 +-
5 files changed, 10 insertions(+), 3 deletions(-)
rename include/{membuff.h => membuf.h} (100%)
rename lib/{membuff.c => membuf.c} (99%)MAINTAINERS +7 -0
@@ -1242,6 +1242,13 @@ T: git git://github.com/ARM-software/u-boot.git F: drivers/video/mali_dp.c F: drivers/i2c/i2c-versatile.c +MEMBUF +M: Simon Glass <[email protected]> +S: Maintained +T: git https://source.denx.de/u-boot/u-boot.git +F: include/membuf.h +F: lib/membuf.c + MICROBLAZE M: Michal Simek <[email protected]> S: Maintained
include/asm-generic/global_data.h +1 -1
@@ -24,7 +24,7 @@#include <cyclic.h> #include <event_internal.h> #include <fdtdec.h> -#include <membuff.h> +#include <membuf.h> #include <linux/list.h> #include <linux/build_bug.h> #include <asm-offsets.h>
include/membuf.h renamed from include/membuff.h +0 -0
lib/Makefile +1 -1
@@ -124,7 +124,7 @@ obj-y += hang.o obj-y += linux_compat.o obj-y += linux_string.o obj-$(CONFIG_$(PHASE_)LMB) += lmb.o -obj-y += membuff.o +obj-y += membuf.o obj-$(CONFIG_REGEX) += slre.o obj-y += string.o obj-y += tables_csum.o
lib/membuf.c renamed from lib/membuff.c +1 -1
@@ -9,7 +9,7 @@#include <errno.h> #include <log.h> #include <malloc.h> -#include "membuff.h" +#include "membuf.h" void membuf_purge (struct membuff *mb) {
Signature · 1 line
2.34.1
PATCH 3/8 membuf: Rename struct
In reply to Simon GlassRename the struct to match the function prefix and filenames.
---
arch/sandbox/include/asm/serial.h | 2 +- boot/bootmeth_extlinux.c | 2 +- common/console.c | 24 +++++++++--------- drivers/usb/emul/sandbox_keyb.c | 2 +- include/asm-generic/global_data.h | 4 +-- include/membuf.h | 42 +++++++++++++++---------------- lib/membuf.c | 42 +++++++++++++++---------------- 7 files changed, 59 insertions(+), 59 deletions(-)
arch/sandbox/include/asm/serial.h +1 -1
@@ -44,7 +44,7 @@ void sandbox_serial_endisable(bool enabled);* @buf: holds input characters available to be read by this driver */ struct sandbox_serial_priv { -struct membuff buf; +struct membuf buf;char serial_buf[16 ];bool start_of_line; };
boot/bootmeth_extlinux.c +1 -1
@@ -106,7 +106,7 @@ static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)*/ static int extlinux_fill_info (struct bootflow *bflow) { -struct membuff mb; +struct membuf mb;char line[200 ];char *data;int len;
common/console.c +12 -12
@@ -101,7 +101,7 @@ static void console_record_putc(const char c)if (!(gd->flags &GD_FLG_RECORD ))return ;if (gd->console_out.start && - !membuf_putbyte ((struct membuff *)&gd->console_out, c)) + !membuf_putbyte ((struct membuf *)&gd->console_out, c)) gd->flags |=GD_FLG_RECORD_OVF ; }
@@ -112,7 +112,7 @@ static void console_record_puts(const char *s)if (gd->console_out.start) {int len =strlen (s); -if (membuf_put ((struct membuff *)&gd->console_out, s, len) != +if (membuf_put ((struct membuf *)&gd->console_out, s, len) != len) gd->flags |=GD_FLG_RECORD_OVF ; }
@@ -125,7 +125,7 @@ static int console_record_getc(void)if (!gd->console_in.start)return -1 ; -return membuf_getbyte ((struct membuff *)&gd->console_in); +return membuf_getbyte ((struct membuf *)&gd->console_in); }static int console_record_tstc (void )
@@ -133,7 +133,7 @@ static int console_record_tstc(void)if (!(gd->flags &GD_FLG_RECORD ))return 0 ;if (gd->console_in.start) { -if (membuf_peekbyte ((struct membuff *)&gd->console_in) !=-1 ) +if (membuf_peekbyte ((struct membuf *)&gd->console_in) !=-1 )return 1 ; }return 0 ;
@@ -814,13 +814,13 @@ int console_record_init(void) {int ret; - ret =membuf_new ((struct membuff *)&gd->console_out, + ret =membuf_new ((struct membuf *)&gd->console_out, gd->flags &GD_FLG_RELOC ?CONFIG_CONSOLE_RECORD_OUT_SIZE :CONFIG_CONSOLE_RECORD_OUT_SIZE_F );if (ret)return ret; - ret =membuf_new ((struct membuff *)&gd->console_in, + ret =membuf_new ((struct membuf *)&gd->console_in,CONFIG_CONSOLE_RECORD_IN_SIZE );/* Start recording from the beginning */
@@ -831,8 +831,8 @@ int console_record_init(void)void console_record_reset (void ) { -membuf_purge ((struct membuff *)&gd->console_out); -membuf_purge ((struct membuff *)&gd->console_in); +membuf_purge ((struct membuf *)&gd->console_out); +membuf_purge ((struct membuf *)&gd->console_in); gd->flags &= ~GD_FLG_RECORD_OVF ; }
@@ -851,23 +851,23 @@ int console_record_readline(char *str, int maxlen)if (console_record_isempty ())return -ENOENT ; -return membuf_readline ((struct membuff *)&gd->console_out, str, +return membuf_readline ((struct membuf *)&gd->console_out, str, maxlen, '\0', false); }int console_record_avail (void ) { -return membuf_avail ((struct membuff *)&gd->console_out); +return membuf_avail ((struct membuf *)&gd->console_out); }bool console_record_isempty (void ) { -return membuf_isempty ((struct membuff *)&gd->console_out); +return membuf_isempty ((struct membuf *)&gd->console_out); }int console_in_puts (const char *str) { -return membuf_put ((struct membuff *)&gd->console_in, str,strlen (str)); +return membuf_put ((struct membuf *)&gd->console_in, str,strlen (str)); }#endif
drivers/usb/emul/sandbox_keyb.c +1 -1
@@ -38,7 +38,7 @@ enum {* */ struct sandbox_keyb_priv { -struct membuff in; +struct membuf in; };struct sandbox_keyb_plat {
include/asm-generic/global_data.h +2 -2
@@ -317,14 +317,14 @@ struct global_data {* * This buffer is used to collect output during console recording. */ -struct membuff console_out; +struct membuf console_out;/** * @console_in: input buffer for console recording * * If console recording is activated, this buffer can be used to * emulate input. */ -struct membuff console_in; +struct membuf console_in;#endif #if CONFIG_IS_ENABLED (VIDEO )/**
include/membuf.h +21 -21
167 lines not on this page · raw message
lib/membuf.c +21 -21
158 lines not on this page · raw message
Signature · 1 line
2.34.1
Simon Glass
AppliedRe: [PATCH 3/8] membuf: Rename struct
Rename the struct to match the function prefix and filenames.
---
arch/sandbox/include/asm/serial.h | 2 +- boot/bootmeth_extlinux.c | 2 +- common/console.c | 24 +++++++++--------- drivers/usb/emul/sandbox_keyb.c | 2 +- include/asm-generic/global_data.h | 4 +-- include/membuf.h | 42 +++++++++++++++---------------- lib/membuf.c | 42 +++++++++++++++---------------- 7 files changed, 59 insertions(+), 59 deletions(-)
Applied to sjg/master, thanks!
PATCH 4/8 membuf: Include stdbool
In reply to Simon GlassThis uses a bool type so include the required header.
---
include/membuf.h | 2 ++ 1 file changed, 2 insertions(+)
include/membuf.h +2 -0
@@ -9,6 +9,8 @@#ifndef _membuf_H#define _membuf_H +#include <stdbool.h> +/** * @struct membuf: holds the state of a membuff - it is used for input and * output buffers. The buffer extends from @start to (@start + @size - 1).
Signature · 1 line
2.34.1
Simon Glass
AppliedRe: [PATCH 4/8] membuf: Include stdbool
This uses a bool type so include the required header.
---
include/membuf.h | 2 ++ 1 file changed, 2 insertions(+)
Applied to sjg/master, thanks!
PATCH 5/8 membuf: Correct implementation of membuf_dispose()
In reply to Simon GlassThis should free the pointer, not the address of the pointer. Fix it.
---
lib/membuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/membuf.c +1 -1
@@ -384,6 +384,6 @@ void membuf_uninit(struct membuf *mb)void membuf_dispose (struct membuf *mb) { -free (&mb->start); +free (mb->start);membuf_uninit (mb); }
Signature · 1 line
2.34.1
Simon Glass
AppliedRe: [PATCH 5/8] membuf: Correct implementation of membuf_dispose()
This should free the pointer, not the address of the pointer. Fix it.
---
lib/membuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Applied to sjg/master, thanks!
PATCH 6/8 membuf: Add some tests
In reply to Simon GlassAdd tests for the membuf implementation.
---
test/lib/Makefile | 1 + test/lib/membuf.c | 239 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 test/lib/membuf.c
test/lib/Makefile +1 -0
@@ -12,6 +12,7 @@ obj-y += hexdump.o obj-$(CONFIG_SANDBOX) += kconfig.o obj-y += lmb.o obj-y += longjmp.o +obj-$(CONFIG_SANDBOX) += membuf.o obj-$(CONFIG_CONSOLE_RECORD) += test_print.o obj-$(CONFIG_SSCANF) += sscanf.o obj-y += string.o
test/lib/membuf.c new file +239 -0
239 lines not on this page · raw message
Signature · 1 line
2.34.1
Simon Glass
AppliedRe: [PATCH 6/8] membuf: Add some tests
Add tests for the membuf implementation.
---
test/lib/Makefile | 1 + test/lib/membuf.c | 239 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 test/lib/membuf.c
Applied to sjg/master, thanks!
PATCH 7/8 membuf: Minor code-style improvements
In reply to Simon GlassShow the start in end in the comment. Comment a missing variable in membuf_readline() and fix its line length.
---
include/membuf.h | 6 ++++-- lib/membuf.c | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)
include/membuf.h +4 -2
@@ -25,7 +25,7 @@* * .............xxxxxxxxxxxxxxxx......................... * ^ ^ -* tail head +* ^start tail head ^end * * xxxxxxxxxxxxx................xxxxxxxxxxxxxxxxxxxxxxxxx * ^ ^
@@ -194,11 +194,13 @@ int membuf_free(struct membuf *mb);* @mb: membuff to adjust * @str: Place to put the line * @maxlen: Maximum line length (excluding terminator) +* @minch: Minimum ASCII character to permit as part of the line (e.g. ' ') * @must_fit: If true then str is empty if line doesn't fit * Return: number of bytes read (including terminator) if a line has been * read, 0 if nothing was there or line didn't fit when must_fit is set */ -int membuf_readline (struct membuf *mb,char *str,int maxlen,int minch,bool must_fit); +int membuf_readline (struct membuf *mb,char *str,int maxlen,int minch, +bool must_fit);/** * membuf_extend_by() - expand a membuff
lib/membuf.c +2 -1
@@ -287,7 +287,8 @@ int membuf_free(struct membuf *mb) (mb->end - mb->start) -1 -membuf_avail (mb); } -int membuf_readline (struct membuf *mb,char *str,int maxlen,int minch,bool must_fit) +int membuf_readline (struct membuf *mb,char *str,int maxlen,int minch, +bool must_fit) {int len;/* number of bytes read (!= string length) */ char *s, *end;
Signature · 1 line
2.34.1
Simon Glass
AppliedRe: [PATCH 7/8] membuf: Minor code-style improvements
Show the start in end in the comment. Comment a missing variable in membuf_readline() and fix its line length.
---
include/membuf.h | 6 ++++-- lib/membuf.c | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)
Applied to sjg/master, thanks!
PATCH 8/8 membuf: Support a flag for being full
In reply to Simon GlassAt present the membuf implementation wastes a slot in the fifo so it can detect the difference between a full and an empty buffer.
Add the option of supporting a boolean flag, if desired. For now it is off.
The code-size penalty is non-zero, but the space penalty is small and could be reduced on 64-bit machines by using a u32 offset for head and tail.
---
include/membuf.h | 6 +++++ lib/membuf.c | 63 ++++++++++++++++++++++++++++++++++++++--------- test/lib/membuf.c | 11 ++++++--- 3 files changed, 65 insertions(+), 15 deletions(-)
include/membuf.h +6 -0
@@ -11,6 +11,9 @@#include <stdbool.h> +/* Set this to 1 to support a 'full' flag */ +#define MEMBUF_FULL 0 +/** * @struct membuf: holds the state of a membuff - it is used for input and * output buffers. The buffer extends from @start to (@start + @size - 1).
@@ -36,6 +39,9 @@ struct membuf {char *end;/** the end of the buffer (start + length) */ char *head;/** current buffer head */ char *tail;/** current buffer tail */ +#if MEMBUF_FULL +bool full;/** true if full (for when head == tail) */ +#endif };/**
lib/membuf.c +51 -12
162 lines not on this page · raw message
test/lib/membuf.c +8 -3
@@ -23,6 +23,11 @@ static void membuf_zero(struct membuf *mb)static int membuf_check (struct unit_test_state *uts,struct membuf *mb,int value) { +/* says it is full, but can't be */ +#if MEMBUF_FULL +ut_assert (!(mb->full && mb->head != mb->tail)); +#endif +/* head is out of range */ ut_assert (!(mb->head < mb->start || mb->head >= mb->end));
@@ -47,7 +52,7 @@ static int lib_test_membuf_one(struct unit_test_state *uts) in[i +TEST_SIZE ] = in[i]; } - test_size =TEST_SIZE ; + test_size =TEST_SIZE +MEMBUF_FULL ;for (i =1 ; i <TEST_COUNT ; i++) {membuf_zero (&mb);
@@ -92,7 +97,7 @@ static int lib_test_membuf_random(struct unit_test_state *uts) in[i +TEST_SIZE ] = in[i]; } - test_size =TEST_SIZE ; + test_size =TEST_SIZE +MEMBUF_FULL ; inptr = in; outptr = in;
@@ -145,7 +150,7 @@ static int lib_test_membuf_extend(struct unit_test_state *uts) in[i +TEST_SIZE ] = in[i]; } - test_size =TEST_SIZE -1 ; + test_size =TEST_SIZE -1 +MEMBUF_FULL ;for (cur =0 ; cur <= test_size; cur++) {ut_assertok (membuf_new (&mb,TEST_SIZE ));
Signature · 1 line
2.34.1
Simon Glass
AppliedRe: [PATCH 8/8] membuf: Support a flag for being full
At present the membuf implementation wastes a slot in the fifo so it can detect the difference between a full and an empty buffer.
Add the option of supporting a boolean flag, if desired. For now it is off.
The code-size penalty is non-zero, but the space penalty is small and could be reduced on 64-bit machines by using a u32 offset for head and tail.
---
include/membuf.h | 6 +++++ lib/membuf.c | 63 ++++++++++++++++++++++++++++++++++++++--------- test/lib/membuf.c | 11 ++++++--- 3 files changed, 65 insertions(+), 15 deletions(-)
Applied to sjg/master, thanks!
Quoted 3 lines · Simon Glass
membuf: Support a flag for being full
No, that is the worst of all worlds, especially with it being a build-time flag. The right implementation is the one where the head and tail indices are free-running, where you get such a "flag" for free, because you're not wasting the top bits of the indices.
https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/
If you want to do the churn of renaming anyway, I suggest doing it by adding an implementation using the proper scheme under the new name, switch users over, and dropping the old. IMO, this series as-is brings no value (except for the tests, of course).
Rasmus
Hi Rasmus,
Quoted 7 lines · Rasmus Villemoes
Quoted 3 lines · Simon Glass
membuf: Support a flag for being full
Quoted 6 lines
No, that is the worst of all worlds, especially with it being a build-time flag. The right implementation is the one where the head and tail indices are free-running, where you get such a "flag" for free, because you're not wasting the top bits of the indices.
https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/
Thanks for the link. It is an interesting read, including the comments. I've only written one ring buffer in my life and this is it :-)
Which option do you suggest? We don't currently restrict the size to a power of two and that does seem like a strange restriction. It will make it less useful, e.g. in extlinux_fill_info() and potentially replacing memgets().
We actually don't have to worry (today) about threading, so option two could work, I suppose. I don't like the 'length' field any more than the author, but we could have a simple function to turn it into (pointer plus an empty/full flag)
The power-of-two restriction could be lifted by use division instead of masking and making sure that the indexes keep to reasonable values (say no more than twice the buffer size). In fact with the limit, we could just subtract the length from the index when accessing it, if needed.
But overall I think I'm with Chris.
Quoted 5 lines
If you want to do the churn of renaming anyway, I suggest doing it by adding an implementation using the proper scheme under the new name, switch users over, and dropping the old. IMO, this series as-is brings no value (except for the tests, of course).
OK. Do you think this series gets us closer to that, or further away?
Regards,
Simon
Hi Rasmus,
Quoted 41 lines · Simon Glass
Hi Rasmus,
Quoted 7 lines · Rasmus Villemoes
Quoted 3 lines · Simon Glass
membuf: Support a flag for being full
Quoted 6 lines
No, that is the worst of all worlds, especially with it being a build-time flag. The right implementation is the one where the head and tail indices are free-running, where you get such a "flag" for free, because you're not wasting the top bits of the indices.
https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/
Thanks for the link. It is an interesting read, including the comments. I've only written one ring buffer in my life and this is it :-)
Which option do you suggest? We don't currently restrict the size to a power of two and that does seem like a strange restriction. It will make it less useful, e.g. in
extlinux_fill_info()and potentially replacingmemgets().We actually don't have to worry (today) about threading, so option two could work, I suppose. I don't like the 'length' field any more than the author, but we could have a simple function to turn it into (pointer plus an empty/full flag)
The power-of-two restriction could be lifted by use division instead of masking and making sure that the indexes keep to reasonable values (say no more than twice the buffer size). In fact with the limit, we could just subtract the length from the index when accessing it, if needed.
But overall I think I'm with Chris.
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
If you want to do the churn of renaming anyway, I suggest doing it by adding an implementation using the proper scheme under the new name, switch users over, and dropping the old. IMO, this series as-is brings no value (except for the tests, of course).
OK. Do you think this series gets us closer to that, or further away?
I didn't get a response to this (which is not a problem, I miss things all the time). Anyway I don't like the power-of-two restriction and you can see my other responses above. I've applied this to my tree as I want to have the tests in place.
Regards,
SImon
Quoted 27 lines · Simon Glass
Hi Rasmus,
Quoted 24 lines · Simon Glass
Hi Rasmus,
Quoted 19 lines · Rasmus Villemoes
Quoted 3 lines · Simon Glass
membuf: Support a flag for being full
No, that is the worst of all worlds, especially with it being a build-time flag. The right implementation is the one where the head and tail indices are free-running, where you get such a "flag" for free, because you're not wasting the top bits of the indices.
https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/
If you want to do the churn of renaming anyway, I suggest doing it by adding an implementation using the proper scheme under the new name, switch users over, and dropping the old. IMO, this series as-is brings no value (except for the tests, of course).
Quoted 6 lines
Quoted 1 line
OK. Do you think this series gets us closer to that, or further away?
I didn't get a response to this (which is not a problem, I miss things all the time). Anyway I don't like the power-of-two restriction and you can see my other responses above. I've applied this to my tree as I want to have the tests in place.
I stand by my earlier comments that this is the wrong way to implement a circular buffer. I hope Tom doesn't pull this.
Rasmus
Hi Rasmus,
Quoted 32 lines · Rasmus Villemoes
Quoted 29 lines · Simon Glass
Hi Rasmus,
Quoted 25 lines · Simon Glass
Hi Rasmus,
Quoted 12 lines · Rasmus Villemoes
Quoted 3 lines · Simon Glass
membuf: Support a flag for being full
No, that is the worst of all worlds, especially with it being a build-time flag. The right implementation is the one where the head and tail indices are free-running, where you get such a "flag" for free, because you're not wasting the top bits of the indices.
Quoted 7 lines
https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/
If you want to do the churn of renaming anyway, I suggest doing it by adding an implementation using the proper scheme under the new name, switch users over, and dropping the old. IMO, this series as-is brings no value (except for the tests, of course).
OK. Do you think this series gets us closer to that, or further away?
Quoted 7 lines
Quoted 4 lines
I didn't get a response to this (which is not a problem, I miss things all the time). Anyway I don't like the power-of-two restriction and you can see my other responses above. I've applied this to my tree as I want to have the tests in place.
I stand by my earlier comments that this is the wrong way to implement a circular buffer. I hope Tom doesn't pull this.
OK. Are you saying that you think it should only support power-of-two sizes, or something else? What specifically do you want?
This series:
- adds tests
- renames to membuf
- shows how we could switch to using an empty/full flag instead of leaving an empty slot, so we can see the code-size image
- does all this without requiring the size to be a power of two (which limits its usefulness IMO)
Regards,
Simon
Quoted 27 lines · Simon Glass
Hi Rasmus,
Quoted 24 lines · Rasmus Villemoes
Quoted 20 lines · Simon Glass
Hi Rasmus,
Quoted 6 lines · Simon Glass
Hi Rasmus,
Quoted 2 lines · Rasmus Villemoes
Quoted 6 lines
Quoted 4 lines
If you want to do the churn of renaming anyway, I suggest doing it by adding an implementation using the proper scheme under the new name, switch users over, and dropping the old. IMO, this series as-is brings no value (except for the tests, of course).
OK. Do you think this series gets us closer to that, or further away?
I didn't get a response to this (which is not a problem, I miss things all the time). Anyway I don't like the power-of-two restriction and you can see my other responses above. I've applied this to my tree as I want to have the tests in place.
Quoted 5 lines
Quoted 2 lines
I stand by my earlier comments that this is the wrong way to implement a circular buffer. I hope Tom doesn't pull this.
OK. Are you saying that you think it should only support power-of-two sizes,
Yes, because that's the natural way to implement such a simple data structure on real hardware, and I don't agree that it limits its usefulness in any way. See how the linux kernel implements unix pipes, and their kfifo helper.
Quoted 1 line
or something else? What specifically do you want?
I don't "want" anything in particular. I'm merely voicing my opinion that I consider this approach to implementing a circular buffer inferior.
Quoted 2 lines
This series:
- adds tests
Yes, always a good thing.
Quoted 1 line
- renames to membuf
While we can agree that is better than with two f's, I don't see it as a huge virtue.
Quoted 2 lines
- shows how we could switch to using an empty/full flag instead of leaving an empty slot, so we can see the code-size image
As I've said previously, I believe that actually makes the whole thing even worse. Not using the natural wrapping/masking is already error-prone enough, but needing to have two parallel implementations living in the same source files with #ifdeffery; please, no. Do one or the other, unconditionally.
Quoted 2 lines
- does all this without requiring the size to be a power of two (which limits its usefulness IMO)
On that, we'll just have to agree to disagree.
Rasmus
PS: Something like
https://gist.github.com/Villemoes/332ac7a5dfc983c58ad40773c7bc6385 is what I consider a simple and readable implementation (it may be buggy, of course).
Frankly, the triple-pointer use in membuff.c frightens me, and that the implementation of something as simple as membuff_avail() involves a struct copy and calling membuff_getraw() in a loop makes it really hard to reason about and verify.
Hi Rasmus,
Quoted 57 lines · Rasmus Villemoes
Quoted 27 lines · Simon Glass
Hi Rasmus,
Quoted 24 lines · Rasmus Villemoes
Quoted 20 lines · Simon Glass
Hi Rasmus,
Quoted 6 lines · Simon Glass
Hi Rasmus,
Quoted 2 lines · Rasmus Villemoes
Quoted 6 lines
Quoted 4 lines
If you want to do the churn of renaming anyway, I suggest doing it by adding an implementation using the proper scheme under the new name, switch users over, and dropping the old. IMO, this series as-is brings no value (except for the tests, of course).
OK. Do you think this series gets us closer to that, or further away?
I didn't get a response to this (which is not a problem, I miss things all the time). Anyway I don't like the power-of-two restriction and you can see my other responses above. I've applied this to my tree as I want to have the tests in place.
Quoted 5 lines
Quoted 2 lines
I stand by my earlier comments that this is the wrong way to implement a circular buffer. I hope Tom doesn't pull this.
OK. Are you saying that you think it should only support power-of-two sizes,
Yes, because that's the natural way to implement such a simple data structure on real hardware, and I don't agree that it limits its usefulness in any way. See how the linux kernel implements unix pipes, and their kfifo helper.
Quoted 1 line
or something else? What specifically do you want?
I don't "want" anything in particular. I'm merely voicing my opinion that I consider this approach to implementing a circular buffer inferior.
Quoted 2 lines
This series:
- adds testsYes, always a good thing.
Quoted 1 line
- renames to membuf
While we can agree that is better than with two f's, I don't see it as a huge virtue.
Quoted 8 lines
Quoted 2 lines
- shows how we could switch to using an empty/full flag instead of leaving an empty slot, so we can see the code-size image
As I've said previously, I believe that actually makes the whole thing even worse. Not using the natural wrapping/masking is already error-prone enough, but needing to have two parallel implementations living in the same source files with #ifdeffery; please, no. Do one or the other, unconditionally.
Sure, I can remove that, since it seems that the flag is not that useful. I'll send a patch.
Quoted 5 lines
Quoted 3 lines
- does all this without requiring the size to be a power of two (which limits its usefulness IMO)
On that, we'll just have to agree to disagree.
An example of this is parsing a text file. At present you can just:
membuf_init(size...)membuf_readline()...
whereas presumably with power-of-two you would need to
int newsize = ALIGN(size, next power of two....)
void *newptr = malloc(newsize)memcpy(newptr, data, size) membuf_init(newsize...)membuf_setup([so that only 'size' bytes are present]) membuf_readline()...
Quoted 7 lines
Rasmus
PS: Something like
https://gist.github.com/Villemoes/332ac7a5dfc983c58ad40773c7bc6385 is what I consider a simple and readable implementation (it may be buggy, of course).
There are no tests, so who knows? But also, it doesn't allow 'raw' access which is considerably more efficient in many situations, e.g. you can find out the amount of space, read directly into that space without a separate buffer, then confirm how much you read.
Certainly my code can be improved.
Quoted 5 lines
Frankly, the triple-pointer use in
membuff.cfrightens me, and that the implementation of something as simple asmembuff_avail()involves a struct copy and callingmembuff_getraw()in a loop makes it really hard to reason about and verify.
We have tests for verification. But how about you send a patch against the API in my tree? If we can come up with something we both like, then perhaps Tom might apply it.
Regards,
Simon