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
You are not allowed to do recoverpass on this page. Login and try again.
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