리눅스커널(연결리스트)

Download Report

Transcript 리눅스커널(연결리스트)

보통 자료구조의 연결 리스트
struct linked
{
int data;
struct linked* prev;
struct linked* next;
};
리눅스의 연결 리스트 구현 방법
struct list_head
{
struct list_head *next, *prev;
}
연결 리스트로 항목들을 연결할 구조체는
struct list_head 형식으로 정의된 멤버를
가진다.
ex ) struct task_struct
간단한 예
struct list_head
{
struct list_head *next, *prev;
}
struct item
{
int number1;
int number2;
struct list_head list;
};
연결 리스트의 헤드
struct list_head
{
struct list_head *next, *prev;
}
struct list_head head = { &head, &head };
-> 2중 환형 연결 리스트 이므로 초기 값은
자기 자신을 가르킨다.
list.next?
struct list_head
{
struct list_head *next, *prev;
}
struct item
{
int number;
struct list_head list;
};
struct item s1, s2, s3;
s2 = s1.list.next; ????
No No No..!!
s2.list = s1.list.next;
그렇다면 s2 값은
어떻게 얻지요??
#define offsetof ( , )
•
어떻게 item 구조체의 특정 멤버를 통해서 자신이 속한 item 구조체를 통째로 얻지?
<stddef.h>
#define offsetof(TYPE,MEMBER) ((size_t) &((TYPE*)0)->MEMBER)
struct item
{
int number1;
int number2;
struct list_head list;
};
int offset;
offset = offsetof ( struct item, number2 );
// offset = 4;
구조체의 특정 멤버와 구조체 간의 거리를 알아낼 수 있다.
그렇다면?
<전제조건>
커널 컴파일 시에는 구조체의 순서가 재배열 되지 않는다고 가정!
암튼 리스트 멤버의 주소값으로 구조체의 시작 주소값을 얻었다..ㅋㅋㅋㅋ ^^
list_entry = container_of
struct item* item2 = list_entry ( item1->list.next, struct item, list );
struct item
{
int number1;
int number2;
struct list_head list;
};
list_for_each