포럼 회원으로 등록하신분만 다운로드가 가능합니다. 최대 업로드 가능한 용량은 20MB 입니다.
Double Linked-llist
리눅스 커널 Q&A 게시판 펌
(http://linux.flyduck.com/cgi-bin/webboard/wwwboard.cgi?db=lkqna&mode=read&num=1587&page=9&ftype=6&fval=&backdepth=1)
>옛날 커널에서는 prev , next를 사용했었는데
>요즘에는 list_head 를 사용하는것같습니다.
>list_head를 사용하는 이유는 무엇인지 궁금합니다.
>prev,next를 쓰는것이 더 직관적이지 않을까요?
2.4
커널에서부터 linked list 구현이 아주 일반화되어서 쉽게 linked list를 만들 수 있는데, list_head는 이
linked list head를 정의하는 자료구조라고 이해하면 될 것 같습니다. list_head만 있으면 linked
list를 간단하게 만들 수 있다... 이게 이유라고... :-)
>하나더 물어볼게 있는데요....
>list_for_each_safe 라는 게 list.h에 있던데 주석문을 보니 삭제에 대해서 >보호한다고 되어있습니다.
>그런데 이해가 안되는것이 ... 현재 루프를 돌고있는 item에 대해서만
>삭제에 대해서 안전한것 아닌가요?
>예를들어서, A-B-C-D-E 이런식으로 연결되어있을때
>list_for_each를 쓴다면 C를 순회하는도중에 C가 삭제되면 C->next를 얻을수없게되는데......
>list_for_each_safe를 쓰면 C를 순회하는 도중에 C가 삭제되더라도 C->next에대한 복사본을 유지하고 있으니까 이상이 없다고 이해했는데...맞게 이해한것인가요?
>
>하지만, C를 순회하는 도중에 C와 D가 모두 삭제되었다면 속수무책 아닌지요.
>차라리 깔금하게 스핀록 같은걸로 보호하고 list_for_each를 사용하는게 낫지 않을까요?
list_for_each_safe()
함수가 동작하는 방식은 잘 이해하셨습니다. 그렇다면 이를 사용하는 이유는... 코드에서 "linked list를 검색해서 원하는
것을 발견하면 지운다" 같은 일을 하게 되면 list item을 차례로 보면서 해당하는 것이 있으면 현재 item을 지우게 될
것입니다. 이 경우 다음 item을 찾는데 문제가 없게 만드는 것입니다. list_for_each_safe()는
synchronization을 위한 것이 아닙니다.
list를 traverse할 때 현재 item을 지워도 문제가 없게 하기 위한 것입니다.
리눅스 커널에서 쓰는 linked list 의 해더 파일이다.
범용으로 쓰기위해 양방향 연결리스트로 선언했고.
더미로 연리스리스트 head 를 선언하였다.
연결리스트 add 를 하다보면 header와 함께 환영 양방향 연결리스트로 구성이되고
prev , next 로 접근함에 따라 stack 방식/ que 방식의 메모리 접근가능.
for루프로 한쪽방향으로 계속 따라 들어가서 접근방식.
malloc으로 메모리 할당은 안했으므로..
단순 stack 메모리에 잡힐수도, malloc으로 사용자 메모리에 쓸수도 있는것이다.?
del 로 링크리스트 끊는 역할만 하므로 메모리는 약간의 낭비가 있을듯하다.?
list_entry 역할..
list 연결리스트의 주소만 갖고 해당 스트럭쳐의 주소를 반환받을수있다.
(어느 형태의 스트럭쳐든 간단히 연결리스트 구성가능)
리눅스 2.4 버젼의 list.h 의 파일을
윈도우용으로 포팅한 소스파일
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name)
struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do {
(ptr)->next = (ptr); (ptr)->prev = (ptr);
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static int list_empty(struct list_head *head)
{
return head->next == head;
}
static void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each(pos, head)
for (pos = (head)->next; pos != (head);
pos = pos->next)
/**
* list_for_each_prev - iterate over a list backwards
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define list_for_each_prev(pos, head)
for (pos = (head)->prev; pos != (head);
pos = pos->prev)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head)
for (pos = (head)->next, n = pos->next; pos != (head);
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop counter.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member)
for (pos = list_entry((head)->next, typeof(*pos), member);
&pos->member != (head);
pos = list_entry(pos->member.next, typeof(*pos), member);
#endif