2012-11-19 3 views
5

의 정의 :대신으로 container_of을 정의 container_of 매크로

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

왜이 단순히 작동하지 않습니다

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

정의의 첫 번째 줄의 사용은 무엇입니까?

답변

7

몇 가지 수준의 유형 안전성을 추가합니다. 두 번째 버전에서는 ptr을 위해 아무 것도 전달할 수 없으며 제대로 컴파일됩니다. 커널 버전에서는 ptr에 대한 포인터가 type.member과 일치하지 않으면 경고 메시지가 표시됩니다.