From 0678da6352ac484d560086a7f08b533c171b3ef1 Mon Sep 17 00:00:00 2001
From: Giovanni Di Sirio <gdisirio@gmail.com>
Date: Fri, 6 Mar 2015 15:33:35 +0000
Subject: More MISRA.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@7718 35acf78f-673a-0410-8e92-d51de3d6d3f4
---
 os/rt/include/chmtx.h      |  4 +--
 os/rt/include/chschd.h     | 27 +++++++++++++++-
 os/rt/include/chsem.h      |  2 +-
 os/rt/include/chsystypes.h |  5 +++
 os/rt/include/chthreads.h  |  5 ---
 os/rt/src/chcond.c         |  2 +-
 os/rt/src/chevents.c       | 27 +++++++++-------
 os/rt/src/chmboxes.c       | 64 +++++++++++++++++++++-----------------
 os/rt/src/chmtx.c          | 18 +++++------
 os/rt/src/chqueues.c       | 77 +++++++++++++++++++++++++++++-----------------
 os/rt/src/chschd.c         |  4 +--
 os/rt/src/chsem.c          |  6 ++--
 os/rt/src/chthreads.c      |  4 +--
 13 files changed, 150 insertions(+), 95 deletions(-)

diff --git a/os/rt/include/chmtx.h b/os/rt/include/chmtx.h
index 4a4b643ff..caa0c8266 100644
--- a/os/rt/include/chmtx.h
+++ b/os/rt/include/chmtx.h
@@ -49,12 +49,12 @@
 /**
  * @brief   Type of a mutex structure.
  */
-typedef struct mutex mutex_t;
+typedef struct ch_mutex mutex_t;
 
 /**
  * @brief   Mutex structure.
  */
-struct mutex {
+struct ch_mutex {
   threads_queue_t       m_queue;    /**< @brief Queue of the threads sleeping
                                                 on this mutex.              */
   thread_t              *m_owner;   /**< @brief Owner @p thread_t pointer or
diff --git a/os/rt/include/chschd.h b/os/rt/include/chschd.h
index c166bc034..f57972a88 100644
--- a/os/rt/include/chschd.h
+++ b/os/rt/include/chschd.h
@@ -265,6 +265,31 @@ struct ch_thread {
      *        states.
      */
     void                *wtobjp;
+    /**
+     * @brief Pointer to a generic thread reference object.
+     * @note  This field is used to get a pointer to a synchronization
+     *        object and is valid when the thread is in @p CH_STATE_SUSPENDED
+     *        state.
+     */
+    thread_reference_t  *wttrp;
+#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
+    /**
+     * @brief Pointer to a generic semaphore object.
+     * @note  This field is used to get a pointer to a synchronization
+     *        object and is valid when the thread is in @p CH_STATE_WTSEM
+     *        state.
+     */
+    struct ch_semaphore *wtsemp;
+#endif
+#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
+    /**
+     * @brief Pointer to a generic mutex object.
+     * @note  This field is used to get a pointer to a synchronization
+     *        object and is valid when the thread is in @p CH_STATE_WTMTX
+     *        state.
+     */
+    struct ch_mutex     *wtmtxp;
+#endif
 #if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
     /**
      * @brief Enabled events mask.
@@ -301,7 +326,7 @@ struct ch_thread {
    * @brief List of the mutexes owned by this thread.
    * @note  The list is terminated by a @p NULL in this field.
    */
-  struct mutex          *p_mtxlist;
+  struct ch_mutex       *p_mtxlist;
   /**
    * @brief Thread's own, non-inherited, priority.
    */
diff --git a/os/rt/include/chsem.h b/os/rt/include/chsem.h
index 51a29e0f2..efc1cea0b 100644
--- a/os/rt/include/chsem.h
+++ b/os/rt/include/chsem.h
@@ -49,7 +49,7 @@
 /**
  * @brief   Semaphore structure.
  */
-typedef struct semaphore {
+typedef struct ch_semaphore {
   threads_queue_t       s_queue;    /**< @brief Queue of the threads sleeping
                                                 on this semaphore.          */
   cnt_t                 s_cnt;      /**< @brief The semaphore counter.      */
diff --git a/os/rt/include/chsystypes.h b/os/rt/include/chsystypes.h
index cf101e0db..9a01a93c4 100644
--- a/os/rt/include/chsystypes.h
+++ b/os/rt/include/chsystypes.h
@@ -62,6 +62,11 @@ typedef uint16_t systime_t;
  */
 typedef struct ch_thread thread_t;
 
+/**
+ * @brief   Type of a thread reference.
+ */
+typedef thread_t * thread_reference_t;
+
 /**
  * @brief   Type of a generic threads single link list, it works like a stack.
  */
diff --git a/os/rt/include/chthreads.h b/os/rt/include/chthreads.h
index 44b9e2032..6632d8d62 100644
--- a/os/rt/include/chthreads.h
+++ b/os/rt/include/chthreads.h
@@ -44,11 +44,6 @@
 /* Module data structures and types.                                         */
 /*===========================================================================*/
 
-/**
- * @brief   Type of a thread reference.
- */
-typedef thread_t * thread_reference_t;
-
 /**
  * @brief   Thread function.
  */
diff --git a/os/rt/src/chcond.c b/os/rt/src/chcond.c
index b6d6219da..20b0cd47e 100644
--- a/os/rt/src/chcond.c
+++ b/os/rt/src/chcond.c
@@ -154,7 +154,7 @@ void chCondBroadcastI(condition_variable_t *cp) {
   /* Empties the condition variable queue and inserts all the threads into the
      ready list in FIFO order. The wakeup message is set to @p MSG_RESET in
      order to make a chCondBroadcast() detectable from a chCondSignal().*/
-  while (cp->c_queue.p_next != (void *)&cp->c_queue) {
+  while (queue_notempty(&cp->c_queue)) {
     chSchReadyI(queue_fifo_remove(&cp->c_queue))->p_u.rdymsg = MSG_RESET;
   }
 }
diff --git a/os/rt/src/chevents.c b/os/rt/src/chevents.c
index 61b93086c..e768457f5 100644
--- a/os/rt/src/chevents.c
+++ b/os/rt/src/chevents.c
@@ -182,7 +182,8 @@ eventmask_t chEvtGetAndClearEvents(eventmask_t events) {
 eventmask_t chEvtAddEvents(eventmask_t events) {
 
   chSysLock();
-  events = (currp->p_epending |= events);
+  currp->p_epending |= events;
+  events = currp->p_epending;
   chSysUnlock();
 
   return events;
@@ -218,7 +219,7 @@ void chEvtBroadcastFlagsI(event_source_t *esp, eventflags_t flags) {
     elp->el_flags |= flags;
     /* When flags == 0 the thread will always be signaled because the
        source does not emit any flag.*/
-    if ((flags == 0) || ((elp->el_flags & elp->el_wflags) != 0)) {
+    if ((flags == 0U) || ((elp->el_flags & elp->el_wflags) != 0U)) {
       chEvtSignalI(elp->el_listener, elp->el_events);
     }
     elp = elp->el_next;
@@ -285,7 +286,7 @@ void chEvtSignalI(thread_t *tp, eventmask_t events) {
   tp->p_epending |= events;
   /* Test on the AND/OR conditions wait states.*/
   if (((tp->p_state == CH_STATE_WTOREVT) &&
-       ((tp->p_epending & tp->p_u.ewmask) != 0)) ||
+       ((tp->p_epending & tp->p_u.ewmask) != 0U)) ||
       ((tp->p_state == CH_STATE_WTANDEVT) &&
        ((tp->p_epending & tp->p_u.ewmask) == tp->p_u.ewmask))) {
     tp->p_u.rdymsg = MSG_OK;
@@ -349,8 +350,8 @@ void chEvtDispatch(const evhandler_t *handlers, eventmask_t events) {
   chDbgCheck(handlers != NULL);
 
   eid = 0;
-  while (events) {
-    if (events & EVENT_MASK(eid)) {
+  while (events != 0U) {
+    if ((events & EVENT_MASK(eid)) != 0U) {
       chDbgAssert(handlers[eid] != NULL, "null handler");
       events &= ~EVENT_MASK(eid);
       handlers[eid](eid);
@@ -383,12 +384,13 @@ eventmask_t chEvtWaitOne(eventmask_t events) {
   eventmask_t m;
 
   chSysLock();
-  if ((m = (ctp->p_epending & events)) == 0) {
+  m = ctp->p_epending & events;
+  if (m == 0U) {
     ctp->p_u.ewmask = events;
     chSchGoSleepS(CH_STATE_WTOREVT);
     m = ctp->p_epending & events;
   }
-  m ^= m & (m - 1);
+  m ^= m & (m - 1U);
   ctp->p_epending &= ~m;
   chSysUnlock();
 
@@ -412,7 +414,8 @@ eventmask_t chEvtWaitAny(eventmask_t events) {
   eventmask_t m;
 
   chSysLock();
-  if ((m = (ctp->p_epending & events)) == 0) {
+  m = ctp->p_epending & events;
+  if (m == 0U) {
     ctp->p_u.ewmask = events;
     chSchGoSleepS(CH_STATE_WTOREVT);
     m = ctp->p_epending & events;
@@ -477,7 +480,8 @@ eventmask_t chEvtWaitOneTimeout(eventmask_t events, systime_t time) {
   eventmask_t m;
 
   chSysLock();
-  if ((m = (ctp->p_epending & events)) == 0) {
+  m = ctp->p_epending & events;
+  if (m == 0U) {
     if (TIME_IMMEDIATE == time) {
       chSysUnlock();
       return (eventmask_t)0;
@@ -489,7 +493,7 @@ eventmask_t chEvtWaitOneTimeout(eventmask_t events, systime_t time) {
     }
     m = ctp->p_epending & events;
   }
-  m ^= m & (m - 1);
+  m ^= m & (m - 1U);
   ctp->p_epending &= ~m;
   chSysUnlock();
 
@@ -519,7 +523,8 @@ eventmask_t chEvtWaitAnyTimeout(eventmask_t events, systime_t time) {
   eventmask_t m;
 
   chSysLock();
-  if ((m = (ctp->p_epending & events)) == 0) {
+  m = ctp->p_epending & events;
+  if (m == 0U) {
     if (TIME_IMMEDIATE == time) {
       chSysUnlock();
       return (eventmask_t)0;
diff --git a/os/rt/src/chmboxes.c b/os/rt/src/chmboxes.c
index 5b8bc3f64..9c0c3ae0d 100644
--- a/os/rt/src/chmboxes.c
+++ b/os/rt/src/chmboxes.c
@@ -87,7 +87,9 @@ void chMBObjectInit(mailbox_t *mbp, msg_t *buf, cnt_t n) {
 
   chDbgCheck((mbp != NULL) && (buf != NULL) && (n > 0));
 
-  mbp->mb_buffer = mbp->mb_wrptr = mbp->mb_rdptr = buf;
+  mbp->mb_buffer = buf;
+  mbp->mb_rdptr = buf;
+  mbp->mb_wrptr = buf;
   mbp->mb_top = &buf[n];
   chSemObjectInit(&mbp->mb_emptysem, n);
   chSemObjectInit(&mbp->mb_fullsem, 0);
@@ -140,7 +142,7 @@ void chMBResetI(mailbox_t *mbp) {
  *
  * @param[in] mbp       the pointer to an initialized @p mailbox_t object
  * @param[in] msg       the message to be posted on the mailbox
- * @param[in] time      the number of ticks before the operation timeouts,
+ * @param[in] timeout   the number of ticks before the operation timeouts,
  *                      the following special values are allowed:
  *                      - @a TIME_IMMEDIATE immediate timeout.
  *                      - @a TIME_INFINITE no timeout.
@@ -152,11 +154,11 @@ void chMBResetI(mailbox_t *mbp) {
  *
  * @api
  */
-msg_t chMBPost(mailbox_t *mbp, msg_t msg, systime_t time) {
+msg_t chMBPost(mailbox_t *mbp, msg_t msg, systime_t timeout) {
   msg_t rdymsg;
 
   chSysLock();
-  rdymsg = chMBPostS(mbp, msg, time);
+  rdymsg = chMBPostS(mbp, msg, timeout);
   chSysUnlock();
 
   return rdymsg;
@@ -169,7 +171,7 @@ msg_t chMBPost(mailbox_t *mbp, msg_t msg, systime_t time) {
  *
  * @param[in] mbp       the pointer to an initialized @p mailbox_t object
  * @param[in] msg       the message to be posted on the mailbox
- * @param[in] time      the number of ticks before the operation timeouts,
+ * @param[in] timeout   the number of ticks before the operation timeouts,
  *                      the following special values are allowed:
  *                      - @a TIME_IMMEDIATE immediate timeout.
  *                      - @a TIME_INFINITE no timeout.
@@ -181,16 +183,17 @@ msg_t chMBPost(mailbox_t *mbp, msg_t msg, systime_t time) {
  *
  * @sclass
  */
-msg_t chMBPostS(mailbox_t *mbp, msg_t msg, systime_t time) {
+msg_t chMBPostS(mailbox_t *mbp, msg_t msg, systime_t timeout) {
   msg_t rdymsg;
 
   chDbgCheckClassS();
   chDbgCheck(mbp != NULL);
 
-  rdymsg = chSemWaitTimeoutS(&mbp->mb_emptysem, time);
+  rdymsg = chSemWaitTimeoutS(&mbp->mb_emptysem, timeout);
   if (rdymsg == MSG_OK) {
-    *mbp->mb_wrptr++ = msg;
-    /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
+    *mbp->mb_wrptr = msg;
+    mbp->mb_wrptr++;
+    /*lint -save -e946 [18.2, 18.3] Normal pointers arithmetic, it is safe.*/
     if (mbp->mb_wrptr >= mbp->mb_top) {
     /*lint -restore*/
       mbp->mb_wrptr = mbp->mb_buffer;
@@ -226,8 +229,9 @@ msg_t chMBPostI(mailbox_t *mbp, msg_t msg) {
   }
 
   chSemFastWaitI(&mbp->mb_emptysem);
-  *mbp->mb_wrptr++ = msg;
-  /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
+  *mbp->mb_wrptr = msg;
+  mbp->mb_wrptr++;
+  /*lint -save -e946 [18.2, 18.3] Normal pointers arithmetic, it is safe.*/
   if (mbp->mb_wrptr >= mbp->mb_top) {
   /*lint -restore*/
     mbp->mb_wrptr = mbp->mb_buffer;
@@ -244,7 +248,7 @@ msg_t chMBPostI(mailbox_t *mbp, msg_t msg) {
  *
  * @param[in] mbp       the pointer to an initialized @p mailbox_t object
  * @param[in] msg       the message to be posted on the mailbox
- * @param[in] time      the number of ticks before the operation timeouts,
+ * @param[in] timeout   the number of ticks before the operation timeouts,
  *                      the following special values are allowed:
  *                      - @a TIME_IMMEDIATE immediate timeout.
  *                      - @a TIME_INFINITE no timeout.
@@ -256,11 +260,11 @@ msg_t chMBPostI(mailbox_t *mbp, msg_t msg) {
  *
  * @api
  */
-msg_t chMBPostAhead(mailbox_t *mbp, msg_t msg, systime_t time) {
+msg_t chMBPostAhead(mailbox_t *mbp, msg_t msg, systime_t timeout) {
   msg_t rdymsg;
 
   chSysLock();
-  rdymsg = chMBPostAheadS(mbp, msg, time);
+  rdymsg = chMBPostAheadS(mbp, msg, timeout);
   chSysUnlock();
 
   return rdymsg;
@@ -273,7 +277,7 @@ msg_t chMBPostAhead(mailbox_t *mbp, msg_t msg, systime_t time) {
  *
  * @param[in] mbp       the pointer to an initialized @p mailbox_t object
  * @param[in] msg       the message to be posted on the mailbox
- * @param[in] time      the number of ticks before the operation timeouts,
+ * @param[in] timeout   the number of ticks before the operation timeouts,
  *                      the following special values are allowed:
  *                      - @a TIME_IMMEDIATE immediate timeout.
  *                      - @a TIME_INFINITE no timeout.
@@ -285,15 +289,15 @@ msg_t chMBPostAhead(mailbox_t *mbp, msg_t msg, systime_t time) {
  *
  * @sclass
  */
-msg_t chMBPostAheadS(mailbox_t *mbp, msg_t msg, systime_t time) {
+msg_t chMBPostAheadS(mailbox_t *mbp, msg_t msg, systime_t timeout) {
   msg_t rdymsg;
 
   chDbgCheckClassS();
   chDbgCheck(mbp != NULL);
 
-  rdymsg = chSemWaitTimeoutS(&mbp->mb_emptysem, time);
+  rdymsg = chSemWaitTimeoutS(&mbp->mb_emptysem, timeout);
   if (rdymsg == MSG_OK) {
-    /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
+    /*lint -save -e946 [18.2, 18.3] Normal pointers arithmetic, it is safe.*/
     if (--mbp->mb_rdptr < mbp->mb_buffer) {
     /*lint -restore*/
       mbp->mb_rdptr = mbp->mb_top - 1;
@@ -329,7 +333,7 @@ msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg) {
     return MSG_TIMEOUT;
   }
   chSemFastWaitI(&mbp->mb_emptysem);
-  /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
+  /*lint -save -e946 [18.2, 18.3] Normal pointers arithmetic, it is safe.*/
   if (--mbp->mb_rdptr < mbp->mb_buffer) {
   /*lint -restore*/
     mbp->mb_rdptr = mbp->mb_top - 1;
@@ -347,7 +351,7 @@ msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg) {
  *
  * @param[in] mbp       the pointer to an initialized @p mailbox_t object
  * @param[out] msgp     pointer to a message variable for the received message
- * @param[in] time      the number of ticks before the operation timeouts,
+ * @param[in] timeout   the number of ticks before the operation timeouts,
  *                      the following special values are allowed:
  *                      - @a TIME_IMMEDIATE immediate timeout.
  *                      - @a TIME_INFINITE no timeout.
@@ -359,11 +363,11 @@ msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg) {
  *
  * @api
  */
-msg_t chMBFetch(mailbox_t *mbp, msg_t *msgp, systime_t time) {
+msg_t chMBFetch(mailbox_t *mbp, msg_t *msgp, systime_t timeout) {
   msg_t rdymsg;
 
   chSysLock();
-  rdymsg = chMBFetchS(mbp, msgp, time);
+  rdymsg = chMBFetchS(mbp, msgp, timeout);
   chSysUnlock();
 
   return rdymsg;
@@ -376,7 +380,7 @@ msg_t chMBFetch(mailbox_t *mbp, msg_t *msgp, systime_t time) {
  *
  * @param[in] mbp       the pointer to an initialized @p mailbox_t object
  * @param[out] msgp     pointer to a message variable for the received message
- * @param[in] time      the number of ticks before the operation timeouts,
+ * @param[in] timeout   the number of ticks before the operation timeouts,
  *                      the following special values are allowed:
  *                      - @a TIME_IMMEDIATE immediate timeout.
  *                      - @a TIME_INFINITE no timeout.
@@ -388,16 +392,17 @@ msg_t chMBFetch(mailbox_t *mbp, msg_t *msgp, systime_t time) {
  *
  * @sclass
  */
-msg_t chMBFetchS(mailbox_t *mbp, msg_t *msgp, systime_t time) {
+msg_t chMBFetchS(mailbox_t *mbp, msg_t *msgp, systime_t timeout) {
   msg_t rdymsg;
 
   chDbgCheckClassS();
   chDbgCheck((mbp != NULL) && (msgp != NULL));
 
-  rdymsg = chSemWaitTimeoutS(&mbp->mb_fullsem, time);
+  rdymsg = chSemWaitTimeoutS(&mbp->mb_fullsem, timeout);
   if (rdymsg == MSG_OK) {
-    *msgp = *mbp->mb_rdptr++;
-    /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
+    *msgp = *mbp->mb_rdptr;
+    mbp->mb_rdptr++;
+    /*lint -save -e946 [18.2, 18.3] Normal pointers arithmetic, it is safe.*/
     if (mbp->mb_rdptr >= mbp->mb_top) {
     /*lint -restore*/
       mbp->mb_rdptr = mbp->mb_buffer;
@@ -432,8 +437,9 @@ msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp) {
     return MSG_TIMEOUT;
   }
   chSemFastWaitI(&mbp->mb_fullsem);
-  *msgp = *mbp->mb_rdptr++;
-  /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
+  *msgp = *mbp->mb_rdptr;
+  mbp->mb_rdptr++;
+  /*lint -save -e946 [18.2, 18.3] Normal pointers arithmetic, it is safe.*/
   if (mbp->mb_rdptr >= mbp->mb_top) {
   /*lint -restore*/
     mbp->mb_rdptr = mbp->mb_buffer;
diff --git a/os/rt/src/chmtx.c b/os/rt/src/chmtx.c
index 13ae7e6fc..3243f6eee 100644
--- a/os/rt/src/chmtx.c
+++ b/os/rt/src/chmtx.c
@@ -170,14 +170,15 @@ void chMtxLockS(mutex_t *mp) {
         switch (tp->p_state) {
         case CH_STATE_WTMTX:
           /* Re-enqueues the mutex owner with its new priority.*/
-          queue_prio_insert(queue_dequeue(tp),
-                            (threads_queue_t *)tp->p_u.wtobjp);
-          tp = ((mutex_t *)tp->p_u.wtobjp)->m_owner;
+          queue_prio_insert(queue_dequeue(tp), &tp->p_u.wtmtxp->m_queue);
+          tp = tp->p_u.wtmtxp->m_owner;
+          /*lint -e{9042} [16.1] Continues the while.*/
           continue;
-#if (CH_CFG_USE_CONDVARS == TRUE) |                                         \
+#if (CH_CFG_USE_CONDVARS == TRUE) ||                                        \
     ((CH_CFG_USE_SEMAPHORES == TRUE) &&                                     \
-     (CH_CFG_USE_SEMAPHORES_PRIORITY) == TRUE) |                            \
-    ((CH_CFG_USE_MESSAGES == TRUE) && (CH_CFG_USE_MESSAGES_PRIORITY == TRUE))
+     (CH_CFG_USE_SEMAPHORES_PRIORITY == TRUE)) ||                           \
+    ((CH_CFG_USE_MESSAGES == TRUE) &&                                       \
+     (CH_CFG_USE_MESSAGES_PRIORITY == TRUE))
 #if CH_CFG_USE_CONDVARS == TRUE
         case CH_STATE_WTCOND:
 #endif
@@ -189,8 +190,7 @@ void chMtxLockS(mutex_t *mp) {
         case CH_STATE_SNDMSGQ:
 #endif
           /* Re-enqueues tp with its new priority on the queue.*/
-          queue_prio_insert(queue_dequeue(tp),
-                            (threads_queue_t *)tp->p_u.wtobjp);
+          queue_prio_insert(queue_dequeue(tp), &tp->p_u.wtmtxp->m_queue);
           break;
 #endif
         case CH_STATE_READY:
@@ -210,7 +210,7 @@ void chMtxLockS(mutex_t *mp) {
 
       /* Sleep on the mutex.*/
       queue_prio_insert(ctp, &mp->m_queue);
-      ctp->p_u.wtobjp = mp;
+      ctp->p_u.wtmtxp = mp;
       chSchGoSleepS(CH_STATE_WTMTX);
 
       /* It is assumed that the thread performing the unlock operation assigns
diff --git a/os/rt/src/chqueues.c b/os/rt/src/chqueues.c
index f02eb14a0..fbe05e129 100644
--- a/os/rt/src/chqueues.c
+++ b/os/rt/src/chqueues.c
@@ -89,10 +89,14 @@ void chIQObjectInit(input_queue_t *iqp, uint8_t *bp, size_t size,
 
   chThdQueueObjectInit(&iqp->q_waiting);
   iqp->q_counter = 0;
-  iqp->q_buffer = iqp->q_rdptr = iqp->q_wrptr = bp;
-  iqp->q_top = bp + size;
-  iqp->q_notify = infy;
-  iqp->q_link = link;
+  iqp->q_buffer  = bp;
+  iqp->q_rdptr   = bp;
+  iqp->q_wrptr   = bp;
+  /*lint -save -e9016 [18.4] Normal pointers arithmetic.*/
+  iqp->q_top     = bp + size;
+  /*lint -restore*/
+  iqp->q_notify  = infy;
+  iqp->q_link    = link;
 }
 
 /**
@@ -110,7 +114,8 @@ void chIQResetI(input_queue_t *iqp) {
 
   chDbgCheckClassI();
 
-  iqp->q_rdptr = iqp->q_wrptr = iqp->q_buffer;
+  iqp->q_rdptr = iqp->q_buffer;
+  iqp->q_wrptr = iqp->q_buffer;
   iqp->q_counter = 0;
   chThdDequeueAllI(&iqp->q_waiting, Q_RESET);
 }
@@ -136,8 +141,9 @@ msg_t chIQPutI(input_queue_t *iqp, uint8_t b) {
     return Q_FULL;
   }
 
+  *iqp->q_wrptr = b;
+  iqp->q_wrptr++;
   iqp->q_counter++;
-  *iqp->q_wrptr++ = b;
   /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
   if (iqp->q_wrptr >= iqp->q_top) {
   /*lint -restore*/
@@ -173,20 +179,21 @@ msg_t chIQGetTimeout(input_queue_t *iqp, systime_t time) {
   uint8_t b;
 
   chSysLock();
-  if (iqp->q_notify) {
+  if (iqp->q_notify != NULL) {
     iqp->q_notify(iqp);
   }
 
   while (chIQIsEmptyI(iqp)) {
-    msg_t msg;
-    if ((msg = chThdEnqueueTimeoutS(&iqp->q_waiting, time)) < Q_OK) {
+    msg_t msg = chThdEnqueueTimeoutS(&iqp->q_waiting, time);
+    if (msg < Q_OK) {
       chSysUnlock();
       return msg;
     }
   }
 
+  b = *iqp->q_rdptr;
+  iqp->q_rdptr++;
   iqp->q_counter--;
-  b = *iqp->q_rdptr++;
   /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
   if (iqp->q_rdptr >= iqp->q_top) {
   /*lint -restore*/
@@ -194,7 +201,7 @@ msg_t chIQGetTimeout(input_queue_t *iqp, systime_t time) {
   }
   chSysUnlock();
 
-  return b;
+  return (msg_t)b;
 }
 
 /**
@@ -230,7 +237,7 @@ size_t chIQReadTimeout(input_queue_t *iqp, uint8_t *bp,
 
   chSysLock();
   while (true) {
-    if (nfy) {
+    if (nfy != NULL) {
       nfy(iqp);
     }
 
@@ -241,8 +248,10 @@ size_t chIQReadTimeout(input_queue_t *iqp, uint8_t *bp,
       }
     }
 
+    *bp = *iqp->q_rdptr;
+    bp++;
+    iqp->q_rdptr++;
     iqp->q_counter--;
-    *bp++ = *iqp->q_rdptr++;
     /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
     if (iqp->q_rdptr >= iqp->q_top) {
     /*lint -restore*/
@@ -251,7 +260,8 @@ size_t chIQReadTimeout(input_queue_t *iqp, uint8_t *bp,
 
     chSysUnlock(); /* Gives a preemption chance in a controlled point.*/
     r++;
-    if (--n == 0) {
+    n--;
+    if (n == 0U) {
       return r;
     }
 
@@ -280,10 +290,14 @@ void chOQObjectInit(output_queue_t *oqp, uint8_t *bp, size_t size,
 
   chThdQueueObjectInit(&oqp->q_waiting);
   oqp->q_counter = size;
-  oqp->q_buffer = oqp->q_rdptr = oqp->q_wrptr = bp;
-  oqp->q_top = bp + size;
-  oqp->q_notify = onfy;
-  oqp->q_link = link;
+  oqp->q_buffer  = bp;
+  oqp->q_rdptr   = bp;
+  oqp->q_wrptr   = bp;
+  /*lint -save -e9016 [18.4] Normal pointers arithmetic.*/
+  oqp->q_top     = bp + size;
+  /*lint -restore*/
+  oqp->q_notify  = onfy;
+  oqp->q_link    = link;
 }
 
 /**
@@ -301,7 +315,8 @@ void chOQResetI(output_queue_t *oqp) {
 
   chDbgCheckClassI();
 
-  oqp->q_rdptr = oqp->q_wrptr = oqp->q_buffer;
+  oqp->q_rdptr = oqp->q_buffer;
+  oqp->q_wrptr = oqp->q_buffer;
   oqp->q_counter = chQSizeX(oqp);
   chThdDequeueAllI(&oqp->q_waiting, Q_RESET);
 }
@@ -332,23 +347,23 @@ msg_t chOQPutTimeout(output_queue_t *oqp, uint8_t b, systime_t time) {
 
   chSysLock();
   while (chOQIsFullI(oqp)) {
-    msg_t msg;
-
-    if ((msg = chThdEnqueueTimeoutS(&oqp->q_waiting, time)) < Q_OK) {
+    msg_t msg = chThdEnqueueTimeoutS(&oqp->q_waiting, time);
+    if (msg < Q_OK) {
       chSysUnlock();
       return msg;
     }
   }
 
+  *oqp->q_wrptr = b;
+  oqp->q_wrptr++;
   oqp->q_counter--;
-  *oqp->q_wrptr++ = b;
   /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
   if (oqp->q_wrptr >= oqp->q_top) {
   /*lint -restore*/
     oqp->q_wrptr = oqp->q_buffer;
   }
 
-  if (oqp->q_notify) {
+  if (oqp->q_notify != NULL) {
     oqp->q_notify(oqp);
   }
   chSysUnlock();
@@ -375,8 +390,9 @@ msg_t chOQGetI(output_queue_t *oqp) {
     return Q_EMPTY;
   }
 
+  b = *oqp->q_rdptr;
+  oqp->q_rdptr++;
   oqp->q_counter++;
-  b = *oqp->q_rdptr++;
   /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
   if (oqp->q_rdptr >= oqp->q_top) {
   /*lint -restore*/
@@ -385,7 +401,7 @@ msg_t chOQGetI(output_queue_t *oqp) {
 
   chThdDequeueNextI(&oqp->q_waiting, Q_OK);
 
-  return b;
+  return (msg_t)b;
 }
 
 /**
@@ -427,21 +443,24 @@ size_t chOQWriteTimeout(output_queue_t *oqp, const uint8_t *bp,
         return w;
       }
     }
+    *oqp->q_wrptr = *bp;
+    bp++;
+    oqp->q_wrptr++;
     oqp->q_counter--;
-    *oqp->q_wrptr++ = *bp++;
     /*lint -save -e946 [18.2] Normal pointers arithmetic, it is safe.*/
     if (oqp->q_wrptr >= oqp->q_top) {
     /*lint -restore*/
       oqp->q_wrptr = oqp->q_buffer;
     }
 
-    if (nfy) {
+    if (nfy != NULL) {
       nfy(oqp);
     }
 
     chSysUnlock(); /* Gives a preemption chance in a controlled point.*/
     w++;
-    if (--n == 0) {
+    n--;
+    if (n == 0U) {
       return w;
     }
     chSysLock();
diff --git a/os/rt/src/chschd.c b/os/rt/src/chschd.c
index 03d5259ed..3e7f84002 100644
--- a/os/rt/src/chschd.c
+++ b/os/rt/src/chschd.c
@@ -289,11 +289,11 @@ static void wakeup(void *p) {
     chSysUnlockFromISR();
     return;
   case CH_STATE_SUSPENDED:
-    *(thread_reference_t *)tp->p_u.wtobjp = NULL;
+    *tp->p_u.wttrp = NULL;
     break;
 #if CH_CFG_USE_SEMAPHORES == TRUE
   case CH_STATE_WTSEM:
-    chSemFastSignalI((semaphore_t *)tp->p_u.wtobjp);
+    chSemFastSignalI(tp->p_u.wtsemp);
     /* Falls into, intentional. */
 #endif
 #if (CH_CFG_USE_CONDVARS == TRUE) && (CH_CFG_USE_CONDVARS_TIMEOUT == TRUE)
diff --git a/os/rt/src/chsem.c b/os/rt/src/chsem.c
index 87a28eb28..30a68e035 100644
--- a/os/rt/src/chsem.c
+++ b/os/rt/src/chsem.c
@@ -203,7 +203,7 @@ msg_t chSemWaitS(semaphore_t *sp) {
               "inconsistent semaphore");
 
   if (--sp->s_cnt < 0) {
-    currp->p_u.wtobjp = sp;
+    currp->p_u.wtsemp = sp;
     sem_insert(currp, &sp->s_queue);
     chSchGoSleepS(CH_STATE_WTSEM);
 
@@ -275,7 +275,7 @@ msg_t chSemWaitTimeoutS(semaphore_t *sp, systime_t time) {
 
       return MSG_TIMEOUT;
     }
-    currp->p_u.wtobjp = sp;
+    currp->p_u.wtsemp = sp;
     sem_insert(currp, &sp->s_queue);
 
     return chSchGoSleepTimeoutS(CH_STATE_WTSEM, time);
@@ -393,7 +393,7 @@ msg_t chSemSignalWait(semaphore_t *sps, semaphore_t *spw) {
   if (--spw->s_cnt < 0) {
     thread_t *ctp = currp;
     sem_insert(ctp, &spw->s_queue);
-    ctp->p_u.wtobjp = spw;
+    ctp->p_u.wtsemp = spw;
     chSchGoSleepS(CH_STATE_WTSEM);
     msg = ctp->p_u.rdymsg;
   }
diff --git a/os/rt/src/chthreads.c b/os/rt/src/chthreads.c
index af672774e..3f5f35878 100644
--- a/os/rt/src/chthreads.c
+++ b/os/rt/src/chthreads.c
@@ -511,7 +511,7 @@ msg_t chThdSuspendS(thread_reference_t *trp) {
   chDbgAssert(*trp == NULL, "not NULL");
 
   *trp = tp;
-  tp->p_u.wtobjp = &trp;
+  tp->p_u.wttrp = trp;
   chSchGoSleepS(CH_STATE_SUSPENDED);
 
   return chThdGetSelfX()->p_u.rdymsg;
@@ -546,7 +546,7 @@ msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout) {
   }
 
   *trp = tp;
-  tp->p_u.wtobjp = &trp;
+  tp->p_u.wttrp = trp;
 
   return chSchGoSleepTimeoutS(CH_STATE_SUSPENDED, timeout);
 }
-- 
cgit v1.2.3