프로그래밍/리눅스 드라이버

리눅스 커널에서 사용하는 Container_of 매크로

얼땅불땅 2013. 8. 14. 09:42












리눅스 커널을 보던 중 Container_of 라는 매크로라는 녀석을 자주 보게 된다.

container_of - cast a member of a structure out to the containing structure

  
SYNOPSIS
container_of  (ptr, type, member);

ARGUMENTS

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.

하는 역활은 한 구조체의 멤버만 알고 있을 경우 그 구조체의 시작 주소를 구할 때 사용한다. 예를 들어 다음과 같은 경우에 사용된다.

struct tagTEMP
{
   int X;
   int Y;

  struct list *next;
};

void calc(struct list *node )
{
   struct tagTEMP *pTEMP;

  pTEMP = container_of(next, struct tagTEMP, next );
}