kernel_api.md 5.0 KB

DragonOS内核核心API

循环链表管理函数

  循环链表是内核的重要的数据结构之一。包含在kernel/common/glib.h中。

void list_init(struct List *list)

描述

  初始化一个List结构体,使其prev和next指针指向自身

参数

list

  要被初始化的List结构体

void list_add(struct List *entry, struct List *node)

描述

  将node插入到entry的后方

参数

entry

  已存在于循环链表中的一个结点

node

  待插入的结点

void list_append(struct List *entry, struct List *node)

描述

  将node插入到entry的前方

参数

entry

  已存在于循环链表中的一个结点

node

  待插入的结点

void list_del(struct List *entry)

描述

  从链表中删除结点entry

参数

entry

  待删除的结点

bool list_empty(struct List *entry)

描述

  判断链表是否为空

参数

entry

  链表中的一个结点

struct List *list_prev(struct List *entry)

描述

  获取entry的前一个结点

参数

entry

  链表中的一个结点

struct List *list_next(struct List *entry)

描述

  获取entry的后一个结点

参数

entry

  链表中的一个结点


基础C函数库

  内核编程与应用层编程不同,你将无法使用LibC中的函数来进行编程。为此,内核实现了一些常用的C语言函数,并尽量使其与标准C库中的函数行为相近。值得注意的是,这些函数的行为可能与标准C库函数不同,请在使用时仔细阅读以下文档,这将会为你带来帮助。

字符串操作

int strlen(const char *s)

描述

  测量并返回字符串长度。

参数

src

  源字符串

long strnlen(const char *src, unsigned long maxlen)

描述

  测量并返回字符串长度。当字符串长度大于maxlen时,返回maxlen

参数

src

  源字符串

maxlen

  最大长度

long strnlen_user(const char *src, unsigned long maxlen)

描述

  测量并返回字符串长度。当字符串长度大于maxlen时,返回maxlen。

  该函数会进行地址空间校验,要求src字符串必须来自用户空间。当源字符串来自内核空间时,将返回0.

参数

src

  源字符串,地址位于用户空间

maxlen

  最大长度

char *strncpy(char *dst, const char *src, long count)

描述

  拷贝长度为count个字节的字符串,返回dst字符串

参数

src

  源字符串

dst

  目标字符串

count

  要拷贝的源字符串的长度

long strncpy_from_user(char *dst, const char *src, unsigned long size)

描述

  从用户空间拷贝长度为count个字节的字符串到内核空间,返回拷贝的字符串的大小

  该函数会对字符串的地址空间进行校验,防止出现地址空间越界的问题。

参数

src

  源字符串

dst

  目标字符串

size

  要拷贝的源字符串的长度

int strcmp(char *FirstPart, char *SecondPart)

描述

  比较两个字符串的大小。

返回值

情况 返回值
FirstPart == SecondPart 0
FirstPart > SecondPart 1
FirstPart < SecondPart -1
参数

FirstPart

  第一个字符串

SecondPart

  第二个字符串

printk(const char* fmt, ...)

描述

  该宏能够在控制台上以黑底白字格式化输出字符串.

参数

fmt

  源格式字符串

...

  可变参数

printk_color(unsigned int FRcolor, unsigned int BKcolor, const char* fmt, ...)

描述

  在控制台上以指定前景色和背景色格式化输出字符串.

参数

FRcolor

  前景色

BKcolor

  背景色

fmt

  源格式字符串

...

  可变参数

int vsprintf(char *buf, const char *fmt, va_list args)

描述

  按照fmt格式化字符串,并将结果输出到buf中,返回写入buf的字符数量。

参数

buf

  输出缓冲区

fmt

  源格式字符串

args

  可变参数列表

int sprintk(char *buf, const char *fmt, ...)

描述

  按照fmt格式化字符串,并将结果输出到buf中,返回写入buf的字符数量。

参数

buf

  输出缓冲区

fmt

  源格式字符串

...

  可变参数