2

I have a struct like

struct my_struct {
     struct list_head queues[NB_QUEUES];
};

I would like to access at the first element of one of the queue (index is computed but I take 2 for the example, which is < NB_QUEUES) like the following (which works without arrays)

struct list_head t = list_first_entry(&foo->queues[2], struct my_struct, ???);

What am I supposed to do to obtain the first element of the queue at the index 2 ? I didn't find anything which compils

1 Answer 1

9

So, your code isn't very clear. Let's talk about a more generic example:

  1. I have NB_QUEUES queues. I'm just going to store these as a global variable:

    struct list_head queues[NB_QUEUES];
    
  2. We'll want to make sure that our lists are initialized. We can do that statically with LIST_HEAD_INIT, or at runtime with INIT_LIST_HEAD.

    int i;
    for (i=0; i<NB_QUEUES; ++i)
        INIT_LIST_HEAD(&queues[i]);
    
  3. We can then push an object of type element into one of these queues:

    struct element {
        struct list_head list;
        int data;
        float more_data;
    };
    
    struct element *v = kmalloc(sizeof(*v), GFP_KERNEL);
    v->data = 4;
    v->more_data = 7.2;
    
    list_add(&v.list, &queues[2]);
    
  4. Later, we can then access the first element in the list.

    struct element *q = list_first_entry(&queues[2], struct element, list);
    

    The list argument is from the name of the member of struct element that we used when we inserted this object into the linked list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.