CIRCLEQ_LOOP_PREV - Linux


Overview

CIRCLEQ_LOOP_PREV iterates through a circular queue in reverse order, beginning from a specified element. It’s commonly used in data structures and kernel code for managing and traversing circular queues.

Syntax

#include <sys/queue.h>

void *CIRCLEQ_LOOP_PREV(void *element, offsetof(struct circle_entry, next));

Parameters

  • element: The current element in the circular queue where the reverse iteration starts.
  • offsetof(struct circle_entry, next): The offset of the next pointer within the struct circle_entry.

Options/Flags

None

Examples

struct circle_entry *prev_element;

for (prev_element = CIRCLEQ_LOOP_PREV(current_element, offsetof(struct circle_entry, next));
     prev_element != current_element;
     prev_element = CIRCLEQ_LOOP_PREV(prev_element, offsetof(struct circle_entry, next))) {
    // Perform some operation on the previous element
}

Common Issues

  • Ensure that current_element is a valid element within the circular queue. Iterating from an invalid element may lead to undefined behavior.
  • Be cautious when modifying the circular queue while iterating in reverse. Changes may affect the iteration order or cause inconsistencies.

Integration

CIRCLEQ_LOOP_PREV is often used in conjunction with other circular queue operations such as CIRCLEQ_INSERT, CIRCLEQ_REMOVE, and CIRCLEQ_FIRST.

Related Commands

  • CIRCLEQ_FIRST: Retrieves the first element in the circular queue.
  • CIRCLEQ_INSERT: Inserts an element into a circular queue.
  • CIRCLEQ_LOOP: Iterates through a circular queue in the forward direction.