[PLUG] Re: wow. learned something new!

Randal L. Schwartz merlyn at stonehenge.com
Sun Mar 26 22:44:15 UTC 2006


>>>>> "Russell" == Russell Senior <seniorr at aracnet.com> writes:

Russell> I am not a kernel hacker, so when reading kernel code I've always been
Russell> a bit curious about struct list_head, the generic doubly linked list
Russell> in the kernel.  The mystery to me was always: how (expletive deleted)
Russell> do you get to the associated data!  I finally tracked down the answer
Russell> tonight.  The kernel source file include/linux/kernel.h contains a
Russell> macro, container_of() that magically returns a pointer to the
Russell> enclosing structure.  So, you plunk the list_head in your structure,
Russell> and then pointers to the embedded list_head are de-offset to the start
Russell> of the enclosing structure, and there you have your data.  

Russell> Clever.

And the wonderful thing is that with this being open source, you can *see* how
it works, and *how* it is used, in a large enterprise-ready system, and all of
us become better programmers for it.

One of the cool things about the original Unix Source licenses available to
universities was being able to study the V6 and V7 (One True Unix) kernels
line by line.  The V6 kernel was small enough that you could read it in a long
weekend from start to end, including the commentary from Lyons University.

Having good examples of large systems is a great way to see what works in
practice, not just theory.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!




More information about the PLUG mailing list