tty: tty_buffer: unify tty_insert_flip_string_{fixed_flag,flags}()
authorJiri Slaby (SUSE) <jirislaby@kernel.org>
Wed, 16 Aug 2023 10:55:23 +0000 (12:55 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 22 Aug 2023 12:58:15 +0000 (14:58 +0200)
They both do the same except for flags. One mem-copies the flags from
the caller, the other mem-sets to one flag given by the caller. This can
be unified with a simple if in the unified function.

Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20230816105530.3335-4-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/driver-api/tty/tty_buffer.rst
drivers/tty/tty_buffer.c
include/linux/tty_flip.h

index a39d478..774dc11 100644 (file)
@@ -15,10 +15,12 @@ Flip Buffer Management
 ======================
 
 .. kernel-doc:: drivers/tty/tty_buffer.c
-   :identifiers: tty_prepare_flip_string tty_insert_flip_string_fixed_flag
-           tty_insert_flip_string_flags __tty_insert_flip_char
+   :identifiers: tty_prepare_flip_string __tty_insert_flip_char
            tty_flip_buffer_push tty_ldisc_receive_buf
 
+.. kernel-doc:: include/linux/tty_flip.h
+   :identifiers: tty_insert_flip_string_fixed_flag tty_insert_flip_string_flags
+
 ----
 
 Other Functions
index c94df1a..94a88dc 100644 (file)
@@ -303,82 +303,42 @@ int tty_buffer_request_room(struct tty_port *port, size_t size)
 }
 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
 
-/**
- * tty_insert_flip_string_fixed_flag - add characters to the tty buffer
- * @port: tty port
- * @chars: characters
- * @flag: flag value for each character
- * @size: size
- *
- * Queue a series of bytes to the tty buffering. All the characters passed are
- * marked with the supplied flag.
- *
- * Returns: the number added.
- */
-int tty_insert_flip_string_fixed_flag(struct tty_port *port, const u8 *chars,
-                                     u8 flag, size_t size)
+int __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars,
+                                  const u8 *flags, bool mutable_flags,
+                                  size_t size)
 {
+       bool need_flags = mutable_flags || flags[0] != TTY_NORMAL;
        int copied = 0;
-       bool flags = flag != TTY_NORMAL;
 
        do {
                int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
-               int space = __tty_buffer_request_room(port, goal, flags);
+               int space = __tty_buffer_request_room(port, goal, need_flags);
                struct tty_buffer *tb = port->buf.tail;
 
                if (unlikely(space == 0))
                        break;
-               memcpy(char_buf_ptr(tb, tb->used), chars, space);
-               if (tb->flags)
-                       memset(flag_buf_ptr(tb, tb->used), flag, space);
-               tb->used += space;
-               copied += space;
-               chars += space;
-               /* There is a small chance that we need to split the data over
-                * several buffers. If this is the case we must loop.
-                */
-       } while (unlikely(size > copied));
-       return copied;
-}
-EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
 
-/**
- * tty_insert_flip_string_flags        -       add characters to the tty buffer
- * @port: tty port
- * @chars: characters
- * @flags: flag bytes
- * @size: size
- *
- * Queue a series of bytes to the tty buffering. For each character the flags
- * array indicates the status of the character.
- *
- * Returns: the number added.
- */
-int tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars,
-                                const u8 *flags, size_t size)
-{
-       int copied = 0;
+               memcpy(char_buf_ptr(tb, tb->used), chars, space);
 
-       do {
-               int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
-               int space = tty_buffer_request_room(port, goal);
-               struct tty_buffer *tb = port->buf.tail;
+               if (mutable_flags) {
+                       memcpy(flag_buf_ptr(tb, tb->used), flags, space);
+                       flags += space;
+               } else if (tb->flags) {
+                       memset(flag_buf_ptr(tb, tb->used), flags[0], space);
+               }
 
-               if (unlikely(space == 0))
-                       break;
-               memcpy(char_buf_ptr(tb, tb->used), chars, space);
-               memcpy(flag_buf_ptr(tb, tb->used), flags, space);
                tb->used += space;
                copied += space;
                chars += space;
-               flags += space;
+
                /* There is a small chance that we need to split the data over
                 * several buffers. If this is the case we must loop.
                 */
        } while (unlikely(size > copied));
+
        return copied;
 }
-EXPORT_SYMBOL(tty_insert_flip_string_flags);
+EXPORT_SYMBOL(__tty_insert_flip_string_flags);
 
 /**
  * __tty_insert_flip_char   -  add one character to the tty buffer
index d33aed2..8b781f7 100644 (file)
@@ -10,14 +10,52 @@ struct tty_ldisc;
 int tty_buffer_set_limit(struct tty_port *port, int limit);
 unsigned int tty_buffer_space_avail(struct tty_port *port);
 int tty_buffer_request_room(struct tty_port *port, size_t size);
-int tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars,
-                                const u8 *flags, size_t size);
-int tty_insert_flip_string_fixed_flag(struct tty_port *port, const u8 *chars,
-                                     u8 flag, size_t size);
+int __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars,
+                                  const u8 *flags, bool mutable_flags,
+                                  size_t size);
 int tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size);
 void tty_flip_buffer_push(struct tty_port *port);
 int __tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag);
 
+/**
+ * tty_insert_flip_string_fixed_flag - add characters to the tty buffer
+ * @port: tty port
+ * @chars: characters
+ * @flag: flag value for each character
+ * @size: size
+ *
+ * Queue a series of bytes to the tty buffering. All the characters passed are
+ * marked with the supplied flag.
+ *
+ * Returns: the number added.
+ */
+static inline int tty_insert_flip_string_fixed_flag(struct tty_port *port,
+                                                   const u8 *chars, u8 flag,
+                                                   size_t size)
+{
+       return __tty_insert_flip_string_flags(port, chars, &flag, false, size);
+}
+
+/**
+ * tty_insert_flip_string_flags - add characters to the tty buffer
+ * @port: tty port
+ * @chars: characters
+ * @flags: flag bytes
+ * @size: size
+ *
+ * Queue a series of bytes to the tty buffering. For each character the flags
+ * array indicates the status of the character.
+ *
+ * Returns: the number added.
+ */
+static inline int tty_insert_flip_string_flags(struct tty_port *port,
+                                              const u8 *chars, const u8 *flags,
+                                              size_t size)
+{
+       return __tty_insert_flip_string_flags(port, chars, flags, true, size);
+}
+
+
 static inline int tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag)
 {
        struct tty_buffer *tb = port->buf.tail;