Linux下链表常用实现方式

struct _inode_table {
        pthread_mutex_t lock;
        size_t hashsize; 
        char *name; 
        struct list_head lru; 
        size_t lru_size;
};
struct _inode {
        inode_table_t *table; 
        uuid_t gfid;
        gf_lock_t lock;
        struct list_head list;
};

struct list_head {
    struct list_head *next;
    struct list_head *prev;
};

  
//初始化,头和尾都指向自己
#define INIT_LIST_HEAD(head) do {            \
        (head)->next = (head)->prev = head;    \
    } while (0)
//添加到双向列表
static inline void list_add (struct list_head *new, struct list_head *head)
{
    new->prev = head;
    new->next = head->next;
    new->prev->next = new;
    new->next->prev = new;
}
//初始化函数
_inode_table_t*
 inode_table_new {
 inode_table_t *new = (void *)GF_CALLOC(1, sizeof (*new), gf_common_mt_inode_table_t);
 INIT_LIST_HEAD (&new->lru);
}
//初始化函数,
inode_t *
inode_new (inode_table_t *table)
{
inode_t *newi = NULL;
newi->table = table;
INIT_LIST_HEAD (&newi->list);
list_add (&newi->list, &table->lru);
table->lru_size++;
}
每生产一个inode,inode自己维护inode->list,并把inode->list添加到table->lru里面去,由table维护整个inode列表。

使用:
posted @ 2012-02-20 21:44  mingziday  阅读(389)  评论(0编辑  收藏  举报