All of lore.kernel.org
 help / color / mirror / Atom feed
* container_of
@ 2008-12-19 17:56 Julia Lawall
  2008-12-20 11:54 ` container_of Darren Jenkins
                   ` (5 more replies)
  0 siblings, 6 replies; 13+ messages in thread
From: Julia Lawall @ 2008-12-19 17:56 UTC (permalink / raw)
  To: kernel-janitors

Considering code such as the following (drivers/acpi/osl.c):

 	struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
	if (!dpc) {
 		printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
 		return;
 	}

Is it very likely that container_of can return NULL?  Container_of 
computes an offset from a pointer, so I have the impression that if given 
a NULL value it would normally return a negative number (or a very large 
positive one, depending on how it is interpreted).

thanks,
julia

^ permalink raw reply	[flat|nested] 13+ messages in thread
* container_of
@ 2015-01-17 16:32 Simon Brand
  2015-01-17 16:58 ` container_of Manish Katiyar
  2015-01-17 19:13 ` container_of Anish Kumar
  0 siblings, 2 replies; 13+ messages in thread
From: Simon Brand @ 2015-01-17 16:32 UTC (permalink / raw)
  To: kernelnewbies

Good evening,

i read the article about the container_of function:
http://www.kroah.com/log/linux/container_of.html

 I understand what it does and how it works.

I thought, it could be simplified by replacing it through this:

#define container_of(ptr, type, member) ({ \
                (type *)( (char *)ptr - offsetof(type,member) );})

Original:
#define container_of(ptr, type, member) ({ \
                const typeof( ((type *)0)->member ) *__mptr = (ptr); 
                (type *)( (char *)__mptr - offsetof(type,member) );})


ptr has the type of a pointer to the member, which should be the same
as __mptr? The value should although be the same.

First I tried it in a self written script, then replaced it in
include/linux/kernel.h and compiled it as usermode linux -> working
well.

Then I compiled it and run it in a VM, but it is not working.

Can you please explain to me, why the original version is always working
and "mine" is not? 

Thank you for your time!

Regards,
Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-01-19 15:18 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-12-19 17:56 container_of Julia Lawall
2008-12-20 11:54 ` container_of Darren Jenkins
2008-12-20 18:19 ` container_of Julia Lawall
2008-12-23  5:37 ` container_of Greg KH
2008-12-23  6:01 ` container_of Julia Lawall
2008-12-23  6:09 ` container_of Greg KH
2008-12-23  7:00 ` container_of Julia Lawall
  -- strict thread matches above, loose matches on Subject: below --
2015-01-17 16:32 container_of Simon Brand
2015-01-17 16:58 ` container_of Manish Katiyar
2015-01-17 19:45   ` container_of Simon Brand
2015-01-17 19:53     ` container_of Mike Krinkin
2015-01-19 15:18       ` container_of Simon Brand
2015-01-17 19:13 ` container_of Anish Kumar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.