KernelNewbies
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Kernel Hacking

  • Frontpage

  • Kernel Hacking

  • Kernel Documentation

  • Kernel Glossary

  • FAQ

  • Found a bug?

  • Kernel Changelog

  • Upstream Merge Guide

Projects

  • KernelJanitors

  • KernelMentors

  • KernelProjects

Community

  • Why a community?

  • Regional Kernelnewbies

  • Personal Pages

  • Upcoming Events

References

  • Mailing Lists

  • Related Sites

  • Programming Links

Wiki

  • Recent Changes

  • Site Editors

  • Side Bar

  • Tips for Editors

  • Hosted by WikiWall

Navigation

  • RecentChanges
  • FindPage
  • HelpContents

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

KernelNewbies:
  • MagicMacros

This page is intended to describe some magic macros used in kernel.

container_of

The container_of macro is defined in include/linux/kernel.h as following:

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:        the pointer to the member.
 * @type:       the type of the container struct this is embedded in.
 * @member:     the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

GregKH described the inner working of this macro in an article on linuxjournal.com [1] and in his weblog [2].

[1] http://www.linuxjournal.com/article/5783

[2] http://www.kroah.com/log/linux/container_of.html

Another example of how to use this macro: http://bec-systems.com/site/115/the-linux-kernel-container_of-macro

ARRAY_SIZE

Also defined in include/linux/kernel.h ARRAY_SIZE(x) is used to get the number of elements in an array.

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

Therefor it divides the size of the array by the size of the first array element.

  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01