DTS

Github: 用户空间双链表

Email: BuddyZhang1 buddy.zhang@aliyun.com

目录


双链表部署方法

BiscuitOS 开源项目提供了一套用户空间使用的双链表,开发者只要按照使用步骤就可以 轻松将双链表部署到开发者自己的项目中。具体步骤如下:

获取双链表

开发者首先获得双链表的源码文件,可以使用如下命令:

wget https://github.com/BiscuitOS/HardStack/blob/master/Algorithem/list/bindirect-list/Basic/list.h

通过上面的命令可以获得双链表的源代码,其中 list.h 文件内包含了双链表的核心实现。


双链表使用方法

开发者在获得双链表的源码之后,参照如下命令将双链表编译到自己的项目中,例如:

CC=gcc

CFLAGS = -I./

SRC := list_run.c

all: list

list: $(SRC)
	@$(CC) $(SRC) $(CFLAGS) -o $@

例如在上面的 Makefile 脚本中,需要使用 -I./ 选项,将头文件搜索路径执行当前目录, 接着引用 list.h 并一同编译到项目中,这样保证了可以在项目中调用双链表的接口。接着 要在自己的源码中调用双链表的接。

实际例子

在下面的源文件中,引用了双链表的接口,开发者可以参照这个文件构建,如下:

/*
 * list Manual.
 *
 * (C) 2019.05.27 <buddy.zhang@aliyun.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <stdio.h>
#include <stdlib.h>

/*
 * bidirect-list
*
 * +-----------+<--o    +-----------+<--o    +-----------+<--o    +-----------+
 * |           |   |    |           |   |    |           |   |    |           |
 * |      prev |   o----| prev      |   o----| prev      |   o----| prev      |
 * | list_head |        | list_head |        | list_head |        | list_head |
 * |      next |---o    |      next |---o    |      next |---o    |      next |
 * |           |   |    |           |   |    |           |   |    |           |
 * +-----------+   o--->+-----------+   o--->+-----------+   o--->+-----------+
 *
 */
/* list */
#include <list.h>

/* private structure */
struct node {
	const char *name;
	struct list_head list;
};

/* Initialize a group node structure */
static struct node node0 = { .name = "BiscuitOS_node0", };
static struct node node1 = { .name = "BiscuitOS_node1", };
static struct node node2 = { .name = "BiscuitOS_node2", };
static struct node node3 = { .name = "BiscuitOS_node3", };
static struct node node4 = { .name = "BiscuitOS_node4", };
static struct node node5 = { .name = "BiscuitOS_node5", };
static struct node node6 = { .name = "BiscuitOS_node6", };

/* Declaration and implement a bindirect-list */
static LIST_HEAD(BiscuitOS_list);

int main()
{
	struct node *np;

	/* add a new entry on back */
	list_add_tail(&node0.list, &BiscuitOS_list);
	list_add_tail(&node1.list, &BiscuitOS_list);
	list_add_tail(&node2.list, &BiscuitOS_list);
	list_add_tail(&node3.list, &BiscuitOS_list);
	list_add_tail(&node4.list, &BiscuitOS_list);
	list_add_tail(&node5.list, &BiscuitOS_list);
	list_add_tail(&node6.list, &BiscuitOS_list);

	/* Traverser all node on bindirect-list */
	list_for_each_entry(np, &BiscuitOS_list, list)
		printf("%s\n", np->name);

	return 0;
}

完整实践例子可以查看下面教程:

用户空间双链表最小实践


附录

Data Structure Visualizations

Bidirect list

BiscuitOS Home

BiscuitOS Driver

BiscuitOS Kernel Build

Linux Kernel

Bootlin: Elixir Cross Referencer

搭建高效的 Linux 开发环境

赞赏一下吧 🙂

MMU