Re: list_head and struct_task

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello, Shyam!

Last year (actually 2 days ago) when you posted your question I was
thinking about 'container_of' macro, but I was not sure because the
question looked like a schedulers-related question. But it's just
a question about 'container_of'.

On 13:52 Fri 02 Jan     , Shyam Burkule wrote:
> 
> Actually my question is that if we have pointer to list_head(embedded in
> task_struct), then how kernel gets address of corresponding task_struct.
> Here I just given example of scheduler, where the kernel need this
> operation.
> 
> I traverse kernel code and for such operation kernel always comes to
> *container_of* micro, now I am trying to understand how this function works?

Please, distinguish functions and macros, container_of is a macro.


		Usage of 'container_of' with lists:
If we want to add objects of a structure (say, 'struct abc') to a list,
we can just add a 'struct list_head list' as a member of 'struct abc',
then we will be able to link the structures 'struct list_head' into a
list.

How we can walk around the list of 'struct list_head's (we know the next
and the previous node for each node in the list). But remember that
those 'struct list_head's are inside our 'struct abc'. If an instance of
'struct abc' (say, 'struct abc myabc') has memory address X, then the
address in memory of its member 'list' equals (X + [offset of 'list' in
our 'struct abc']).

The "offset of 'list' in our 'struct abc'" is contant for any instance of
the structure 'abc', so we can calculate it at compile time. And
'container_of' is useful when we want to get the pointer (pointer ~=
address) to myabc (&myabc) while we only have a pointer to myabc.list
(&myabc.list):
	struct list_head *p_list;
	struct abc *p_myabc;

	p_list = (...something...);
	p_myabc = container_of (p_list, struct abc, list);


This topic (container_of) has already been discussed here, you can find
it using Google "container_of" -> "I'm feeling lucky" button... ;)

The GregKH's "answer" (I'd like to say "article") is here:
	http://www.kroah.com/log/linux/container_of.html

					Alexander

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux