container_of 매크로
글쓴이: thisnome / 작성시간: 수, 2013/04/03 - 9:53오후
아시다시피.. 아래는 커널소스에서 종종 보게되는 container_of 매크로입니다.
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
저는 두번째 줄이 왜 필요한지가 궁금합니다.
__mptr 을 선언하여 ptr 값을 넣어주고, 다시 __mptr 을 사용하게 되는데요..
아래와 같이 바꿔도 될 것 같은데.. 위와같이 __mptr const 변수를 꼭 사용하는 심오한 이유가 있지 않을까 궁금하네요.
#define container_of(ptr, type, member) ({ \ (type *)( (char *)ptr - offsetof(type,member) );})
Forums:
http://stackoverflow.com/ques
http://stackoverflow.com/questions/6083734/rationale-behind-the-container-of-macro-in-linux-list-h
컴파일러 워닝을 제거하기 위한 것이라 합니다.
정확하게는..
kukyakya 님의 링크를 참고하여 좀더 생각해 보았습니다.
제가 이해하기로는 컴파일러 워닝을 제거하기 위해서 라기보다는,
잘못된 입력을 받았을 때, 컴파일러 워닝을 나오게 하기 위해서 인듯 합니다.
답변 감사합니다.
댓글 달기