linux/list.h 에서 list_entry()

puresupe의 이미지

http://isis.poly.edu/kulesh/stuff/src/klist/

이 페이지에서, 리눅스 커널에서 사용하는 링크드 리스트에 대해서 읽었습니다.

Linux implementation of the linked list is different from the many linked list implementations you might have seen. Usually a linked list contains the items that are to be linked. For example:
 
struct my_list{
	void *myitem;
	struct my_list *next;
	struct my_list *prev;
	};
 
The kernel's implementation of linked list gives the illusion that the list is contained in the items it links! For example, if you were to create a linked list of struct my_cool_list you would do the following:
 
struct my_cool_list{
	struct list_head list; /* kernel's list structure */
	int my_cool_data;
	void* my_cool_void;
	};

위에 씌여있는것 처럼, 1) 다음구조체의 시작점을 포인팅하는 방법말고, 2)link정보를 담은 변수를 포인팅하게해서 offsetof()를 통해서, 구조체의 시작주소를 얻는 방법을 사용하고 있습니다.

http://kldp.org/node/109915

에서 clique 라는 분께서 답변도 달아주셨지만, 제 가 궁금한 부분은 왜 그런식의 링크드 리스트를 사용하는가 입니다.

1) 방법보다 2) 방법에, 어떤 잇점이 있는지 궁금하네요.

pastime의 이미지

여러 구조체에 대해서도 동일한 함수를 사용할 수 있다는 점입니다.
1) 방법이라면 my_list와 my_cool_list를 이용하기 위해
각각의 함수를 따로 작성해야 겠지요..