staging: android: logger: simplify and optimize get_entry_len
authorTim Bird <tim.bird@am.sony.com>
Wed, 8 Feb 2012 18:37:57 +0000 (10:37 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 9 Feb 2012 00:51:45 +0000 (16:51 -0800)
Make this code slightly easier to read, and eliminate calls
to sub-routines.  Some of these were previously optimized away
by the compiler, but one memcpy was not.

In my testing, this makes the code about 20% smaller, and
has no sub-routine calls and no branches (on ARM).

v2 of this patch is, IMHO, easier to read than v1. Compared to
that patch it uses __u8 instead of unsigned char, for
consistency with the __u16 val data type, simplifies the
conditional expression, adds a another comment, and
moves a common statement out of the if.

Signed-off-by: Tim Bird <tim.bird@am.sony.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/android/logger.c

index 29ee137..1e9e638 100644 (file)
@@ -93,20 +93,24 @@ static inline struct logger_log *file_get_log(struct file *file)
  * get_entry_len - Grabs the length of the payload of the next entry starting
  * from 'off'.
  *
+ * An entry length is 2 bytes (16 bits) in host endian order.
+ * In the log, the length does not include the size of the log entry structure.
+ * This function returns the size including the log entry structure.
+ *
  * Caller needs to hold log->mutex.
  */
 static __u32 get_entry_len(struct logger_log *log, size_t off)
 {
        __u16 val;
 
-       switch (log->size - off) {
-       case 1:
-               memcpy(&val, log->buffer + off, 1);
-               memcpy(((char *) &val) + 1, log->buffer, 1);
-               break;
-       default:
-               memcpy(&val, log->buffer + off, 2);
-       }
+       /* copy 2 bytes from buffer, in memcpy order, */
+       /* handling possible wrap at end of buffer */
+
+       ((__u8 *)&val)[0] = log->buffer[off];
+       if (likely(off+1 < log->size))
+               ((__u8 *)&val)[1] = log->buffer[off+1];
+       else
+               ((__u8 *)&val)[1] = log->buffer[0];
 
        return sizeof(struct logger_entry) + val;
 }