Cremulus

The horror... the horror...

Sep 28th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. // Add this transmitter to the pending data list, if it isn't already in it.
  2. static void vAddToPendingList(TTRANSMITTER *pTX)
  3.   {
  4.   // Make sure we aren't already in the list... relying on a flag, urgh.
  5.   if (!pTX->bPendingData)
  6.     {
  7.     // We're going on the end, so we don't point at anyone after us
  8.     pTX->pNextPending=NULL;
  9.     // Add us to the end of the list, if we have a pointer to the end
  10.     if (pLastPending != NULL)
  11.       {
  12.       pLastPending->pNextPending = pTX;
  13.       }
  14.     // Regardless, this one is now the end.
  15.     pLastPending = pTX;
  16.     // Add us to the start of the list, if we don't have one
  17.     if (pFirstPending == NULL)
  18.       {
  19.       pFirstPending = pTX;
  20.       // If there wasn't a list the existing count must be zero, we'll inc it later.
  21.       u32PendingTxCount = 0;
  22.       }
  23.     // Set the flag
  24.     pTX->bPendingData = true;
  25.     // And increment the pending count
  26.     u32PendingTxCount++;
  27.     }
  28.   }
  29.  
  30. // Remove a transmitter from the pending list. Hopefully it's at the front.
  31. static void vRemoveFromPendingList(TTRANSMITTER *pTX)
  32.   {
  33.   TTRANSMITTER *pTXInList;
  34.  
  35.   // Avoid duff pointers
  36.   if (pTX == NULL)
  37.     return;
  38.   // Make sure it's actually in the list...
  39.   if (pTX->bPendingData)
  40.     {
  41.     // So, we think we're in the list
  42.     // Decrement the count
  43.     if (u32PendingTxCount>0)
  44.       u32PendingTxCount--;
  45.     // Are we the first in the list?
  46.     if (pFirstPending == pTX)
  47.       {
  48.       // It was the first, so the next becomes first
  49.       pFirstPending = pTX->pNextPending;
  50.       // Was it also the last?
  51.       if (pLastPending == pTX)
  52.         {
  53.         // First and last, so clear the pLast
  54.         pLastPending = NULL;
  55.         // And we know there can't be any
  56.         u32PendingTxCount=0;
  57.         }
  58.       }
  59.     else
  60.       {
  61.       // Bugger, we're not the first in the list. So we need to chase down the list until we find this entry
  62.       pTXInList = pFirstPending;
  63.       // We know this first one cannot be us, we've checked for that.
  64.       while (pTXInList != NULL)
  65.         {
  66.         // Are we the next one in the pending list after this one?
  67.         if (pTXInList->pNextPending == pTX)
  68.           {
  69.           // Yes, so clip us out of the list
  70.           pTXInList->pNextPending = pTX->pNextPending;
  71.           // Now, we need to check if this makes this one the last one in the list and update pLast if it does
  72.           if (pTXInList->pNextPending == NULL)
  73.             pLastPending = pTXInList;
  74.           // We can't appear in the list twice, so we're done...
  75.           break;
  76.           }
  77.         // Look at the next pending transmitter
  78.         pTXInList = pTXInList->pNextPending;
  79.         }
  80.       }
  81.     }
  82.   // Now, clear our list maintainence variables against future checking
  83.   pTX->bPendingData = false;
  84.   pTX->pNextPending = NULL;
  85.   }
  86.  
Add Comment
Please, Sign In to add comment