⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fdi_que.c

📁 Flash file system
💻 C
📖 第 1 页 / 共 5 页
字号:

      /* lock the sem_cntrl_mutex field in queue_id */
      SEM_MTX_WAIT(queue_ptr->sem_cntrl_mutex);
      /* look in the first priority header for the first data info structure */
      /*
       * assume there is a data item to get since the semaphore was unlocked.
       * assign the data_ptr parameter to the first item element address
       */
      *data_ptr = queue_ptr->first_header_ptr->first_item_ptr;

      /* assign the data_size_ptr value to the sizeof the first item data */
      *data_size_ptr = ((Q_ITEM_PTR) * data_ptr)->item_size;

      /* we must look below the Q_ITEM_INFO for the data_ptr return value */
      *data_ptr = (BYTE_PTR) * data_ptr + sizeof(Q_ITEM_INFO);
      /*
       * deassert the sem_cntrl_sync field in queue_id because we did not take
       * the item out
       */
      SEM_POST(queue_ptr->sem_cntrl_sync);
      /*
       * unlock the sem_cntrl_mutex field in queue_id to give others access to
       * the queue
       */
      SEM_MTX_POST(queue_ptr->sem_cntrl_mutex);
   }                                   /* priority == GET_FIRST_ITEM */
   else
   {
      /*
       * IF the priority parameter is not GET_FIRST_ITEM the routine finds the
       * first item if data_ptr parameter is NULL or finds the next item if
       * not.
       */

      /* lock the sem_cntrl_mutex field in queue_id */
      SEM_MTX_WAIT(queue_ptr->sem_cntrl_mutex);
      /*
       * IF the input parameter data_ptr is NULL THEN find the first item of
       * the priority parameter
       */
      if (*data_ptr == NULL)
      {
         /* Q_VISIT_EACH means to match the priority */

         status = FindItem(queue_ptr, data_ptr, data_size_ptr, &header_ptr,
                           priority, Q_VISIT_EACH);

      }
      else
      {                                /* look for the next data item */
         /*
          * assign this item to the beginning of the Q_ITEM_INFO structure by
          * subtracting the address of data_ptr by the sizeof the Q_ITEM_INFO
          * structure.
          */
         *data_ptr = (BYTE_PTR) * data_ptr - sizeof(Q_ITEM_INFO);
         if ((((Q_ITEM_PTR) (*data_ptr))->next_item_ptr) != NULL)
         {
            /*
             * assign item_size field of this item to value of parameter
             * data_size_ptr
             */
            *data_size_ptr = ((Q_ITEM_PTR)*data_ptr)->next_item_ptr->item_size;
            /*
             * assign parameter data_ptr to address of sum of this item with
             * an offset of the size of Q_ITEM_INFO structure.
             */
            *data_ptr = (BYTE_PTR) (((Q_ITEM_PTR) * data_ptr)->next_item_ptr) +
             sizeof(Q_ITEM_INFO);
            status = Q_ERR_NONE;
         }
         else
         {                             /* next_item_ptr == NULL */
            status = Q_ERR_NO_ITEM;
         }

      }                                /* ENDIF data_ptr == NULL */
      /*
       * unlock the sem_cntrl_mutex field in queue_id to give others access to
       * the queue
       */
      SEM_MTX_POST(queue_ptr->sem_cntrl_mutex);
   }                                   /* ENDIF priority != BYTEMAX */

   return status;
}                                      /* END Q_Peek */


/*############################################################################
 *### Q_Delete
 *###
 *### DESCRIPTION:
 *###    This function frees any memory allocated by the queue code for the
 *###    specified queue.
 *###
 *### PARAMETERS:
 *###    IN:
 *###       queue_id   -  handle to the queue to be deleted
 *###    OUT:
 *###
 *### RETURNS:
 *###    Returns the following errors codes:
 *###       Q_ERR_NONE
 *###       Q_ERR_INVALID_HANDLE
 *###*/
Q_ERROR
Q_Delete(Q_ID queue_id)
{
   register Q_DESC_PTR queue_ptr = (Q_DESC_PTR) queue_id;
   VOID_PTR removed_ptr;

#if (Q_VERIFICATION == TRUE)
   if (VALID_QUEUE_PTR(queue_ptr) == FALSE)  /* IF call ValidQueuePtr fails */
      return Q_ERR_INVALID_HANDLE;
#endif

#if(SEM_CREATE_DESTROY == TRUE)
   /* destroy the sem_cntrl_mutex semaphore */
   SEM_MTX_DESTROY(queue_ptr->sem_cntrl_mutex);

   /* destroy the sem_cntrl_sync semaphore */
   SEM_DESTROY(queue_ptr->sem_cntrl_sync);
#endif /* SEM_CREATE_DESTROY */

#if (Q_VERIFICATION == TRUE)
   /* wipe out the queue's signature so others will see that it is corrupt */
   ((Q_DESC_PTR) queue_ptr)->queue_signature = WORD_LOW((DWORD) queue_ptr);
#endif

   /* DO WHILE there are priority headers */
   while (queue_ptr->first_header_ptr != NULL)
   {
      /* DO WHILE there are items */
      while (queue_ptr->first_header_ptr->first_item_ptr != NULL)
      {
         /* save the first item pointer  */
         removed_ptr = queue_ptr->first_header_ptr->first_item_ptr;

         /* reattach the next item pointer to the priority header */
         queue_ptr->first_header_ptr->first_item_ptr =
          queue_ptr->first_header_ptr->first_item_ptr->next_item_ptr;

         /* deallocate memory for the item pointer */

         FDI_FREE((BYTE_PTR) removed_ptr);
      }                                /* ENDDO WHILE there are items */
      /* save the first priority header pointer */
      removed_ptr = queue_ptr->first_header_ptr;

      /* reattach the next priority header pointer to the descriptor */
      queue_ptr->first_header_ptr =
       queue_ptr->first_header_ptr->next_header_ptr;

      /* deallocate memory for the priority header pointer */

      FDI_FREE((BYTE_PTR) removed_ptr);
   }                                   /* ENDDO WHILE there are headers */

   /* deallocate memory for the descriptor structure */

#if (Q_ALIGN_CHECKING == TRUE)
   queue_ptr -= ((Q_DESC_PTR) queue_ptr)->alignment_shift;
#endif


   FDI_FREE((BYTE_PTR) queue_ptr);

   return Q_ERR_NONE;
}                                      /* END Q_Delete */


/*############################################################################
 *### Q_Remove
 *###
 *### DESCRIPTION:
 *### Removes the passed "data_ptr" object and associated item information
 *### structure.  If "data_ptr" is the last object in the queue, the
 *### associated priority header is also removed.
 *###
 *### PARAMETERS:
 *###    IN:
 *###       queue_id       -  remove the item in the queue with this handle
 *###       data_ptr       -  address of the item to remove
 *###       append_to_replace - flag; if true indicates the append is within
 *###                           an existing granularity, similar to replace
 *###    OUT:
 *###
 *### RETURNS:
 *###    Returns the following errors codes:
 *###       Q_ERR_NONE
 *###       Q_ERR_INVALID_HANDLE
 *###*/
Q_ERROR
Q_Remove(Q_ID queue_id, VOID_PTR data_ptr, BYTE append_to_replace)
{
   register Q_HDR_PTR removed_header_ptr = NULL;
   register Q_HDR_PTR header_ptr = NULL;
   register Q_DESC_PTR queue_ptr = (Q_DESC_PTR) queue_id;
   register Q_ITEM_PTR item_ptr = NULL;

#if (Q_VERIFICATION == TRUE)
   if (VALID_QUEUE_PTR(queue_ptr) == FALSE)  /* IF call ValidQueuePtr fails */
      return Q_ERR_INVALID_HANDLE;
#endif
   /*
    * assign the data item pointer to the beginning of the Q_ITEM_INFO
    * structure by subtracting the address of data_ptr by the sizeof the
    * Q_ITEM_INFO structure.
    */
   item_ptr = (Q_ITEM_PTR)((BYTE_PTR)data_ptr - sizeof(Q_ITEM_INFO));

   /* lock the sem_cntrl_mutex field in queue_id */
   SEM_MTX_WAIT(queue_ptr->sem_cntrl_mutex);
   /*
    * IF the first item in the first header is the item we what to remove.
    * THEN look from the Q_DESCRIPTOR structure into the first header
    */
   if (queue_ptr->first_header_ptr->first_item_ptr == item_ptr)
   {

      /*
       * decrement accum_dirty from for only WRITE_DELETE's
       */
      if (((COMMAND_PTR)(data_ptr))->sub_command == WRITE_DELETE)
      {
         queue_ptr->first_header_ptr->accum_dirty -= TOTALGRAN(
            ((COMMAND_PTR)(data_ptr))->gran_needed);

      }
      else
      {
         /*
          * decrement from accum_free for all but WRITE_MODIFY
          */
         if (((COMMAND_PTR)(data_ptr))->sub_command != WRITE_MODIFY)
         {
            queue_ptr->first_header_ptr->accum_free -= TOTALGRAN(
               ((COMMAND_PTR)(data_ptr))->gran_needed);

         }
         /*
          * decrement from accum_dirty for WRITE_REPLACE or appends that do
          * not grow in size, but just replace within existing granularity
          */
         if ((((COMMAND_PTR)(data_ptr))->sub_command == WRITE_REPLACE) ||
             (append_to_replace == TRUE))
         {
            queue_ptr->first_header_ptr->accum_dirty -= TOTALGRAN(
               ((COMMAND_PTR)(data_ptr))->gran_needed);
         }
         /* added this condition for recording accum_dirty */
         /*
          * Test for appending to an existing single instance object and 
          * decrement the accum_dirty count by the existing object's 
          * data_offset
          */
         else if (( (((COMMAND_PTR)data_ptr)->sub_command == WRITE_APPEND) ||
                    (((COMMAND_PTR)data_ptr)->sub_command == WRITE_RESERVED) ) &&
            (((COMMAND_PTR)data_ptr)->data_offset != 0) &&
            (((COMMAND_PTR)data_ptr)->data_offset < 
            TO_BYTES(MAX_NUM_UNITS_PER_FRAG)))
         {

            queue_ptr->first_header_ptr->accum_dirty -=
               TO_GRAN(((COMMAND_PTR)data_ptr)->data_offset);
         }                             /* ENDIF appending to single inst */

       }

      /*
       * IF there are no more items in this priority THEN remove the priority
       * header
       */
      if (item_ptr->next_item_ptr == NULL)
      {
         removed_header_ptr = queue_ptr->first_header_ptr;

         /*
          * IF the subcommand is WRITE_RESERVE then we did not add the
          * data onto the bottom of the COMMAND structure, therefore we
          * only count the sizeof(COMMAND) and not the size pointed to by
          * the Q_ITEM_INFO structure.
          */
#if (PACKET_DATA == TRUE)
         if ((((COMMAND_PTR)data_ptr)->sub_command == WRITE_RESERVED) ||
                (((COMMAND_PTR)data_ptr)->sub_command == WRITE_RSRVPCKT) )
#else /* PACKET_DATA */
         if (((COMMAND_PTR)data_ptr)->sub_command == WRITE_RESERVED)
#endif /* PACKET_DATA */
         {
            /* increment the free count of the queue descriptor pointer. */
            queue_ptr->free_count += (sizeof(Q_ITEM_INFO) +
                                      sizeof(Q_PRIORITY_HEADER) +
                                      sizeof(COMMAND));
         }
         else                          /* ELSE NOT WRITE_RESERVE */
         {
            /* increment the free count of the queue descriptor pointer. */
            queue_ptr->free_count += (sizeof(Q_ITEM_INFO) +
                                      sizeof(Q_PRIORITY_HEADER) +
               queue_ptr->first_header_ptr->first_item_ptr->item_size);
         }
         /* connect to the next header in line if available */
         queue_ptr->first_header_ptr =
          queue_ptr->first_header_ptr->next_header_ptr;

         /* delete the priority queue of this item */

         FDI_FREE((BYTE_PTR) removed_header_ptr);
      }
      else
      {
         /*
          * IF the subcommand is WRITE_RESERVE then we did not add the
          * data onto the bottom of the COMMAND structure, therefore we
          * only count the sizeof(COMMAND) and not the size pointed to by
          * the Q_ITEM_INFO structure.
          */
#if (PACKET_DATA == TRUE)
         if ((((COMMAND_PTR)data_ptr)->sub_command == WRITE_RESERVED) ||
             (((COMMAND_PTR)data_ptr)->sub_command == WRITE_RSRVPCKT) )
#else /* PACKET_DATA */
         if (((COMMAND_PTR)data_ptr)->sub_command == WRITE_RESERVED)
#endif /* PACKET_DATA */
         {
            /* increment the free count of the queue descriptor pointer. */
            queue_ptr->free_count += (sizeof(Q_ITEM_INFO) + sizeof(COMMAND));
         }
         else                          /* ELSE NOT WRITE_RESERVE */
         {
            /* increment the free count of the queue descriptor pointer. */
            queue_ptr->free_count += (sizeof(Q_ITEM_INFO) +
               queue_ptr->first_header_ptr->first_item_ptr->item_size);
         }
         /* connect to the next item in the priority */
         queue_ptr->first_header_ptr->first_item_ptr = item_ptr->next_item_ptr;
      }
   }
   else
   {
      /*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -