DragonOS/docs/kernel/core_api/kernel_api.md

314 lines
5.2 KiB
Markdown
Raw Normal View History

2022-07-12 12:39:33 +00:00
# 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**
2022-07-26 10:05:17 +00:00
  已存在于循环链表中的一个结点
2022-07-12 12:39:33 +00:00
**node**
2022-07-26 10:05:17 +00:00
  待插入的结点
2022-07-12 12:39:33 +00:00
### `void list_del(struct List *entry)`
#### 描述
  从链表中删除结点entry
#### 参数
**entry**
  待删除的结点
### `bool list_empty(struct List *entry)`
#### 描述
  判断链表是否为空
#### 参数
**entry**
  链表中的一个结点
### `struct List *list_prev(struct List *entry)`
#### 描述
2022-07-26 10:05:17 +00:00
  获取entry的前一个结点
2022-07-12 12:39:33 +00:00
#### 参数
**entry**
2022-07-26 10:05:17 +00:00
  链表中的一个结点
2022-07-12 12:39:33 +00:00
### `struct List *list_next(struct List *entry)`
#### 描述
  获取entry的后一个结点
#### 参数
**entry**
  链表中的一个结点
---
## 基础C函数库
  内核编程与应用层编程不同你将无法使用LibC中的函数来进行编程。为此内核实现了一些常用的C语言函数并尽量使其与标准C库中的函数行为相近。值得注意的是这些函数的行为可能与标准C库函数不同请在使用时仔细阅读以下文档这将会为你带来帮助。
### 字符串操作
#### `int strlen(const char *s)`
##### 描述
2022-07-26 10:05:17 +00:00
  测量并返回字符串长度。
2022-07-12 12:39:33 +00:00
##### 参数
**src**
2022-07-26 10:05:17 +00:00
  源字符串
2022-07-12 12:39:33 +00:00
#### `long strnlen(const char *src, unsigned long maxlen)`
##### 描述
  测量并返回字符串长度。当字符串长度大于maxlen时返回maxlen
##### 参数
**src**
  源字符串
**maxlen**
  最大长度
#### `long strnlen_user(const char *src, unsigned long maxlen)`
##### 描述
2022-07-26 10:05:17 +00:00
  测量并返回字符串长度。当字符串长度大于maxlen时返回maxlen。
2022-07-12 12:39:33 +00:00
  该函数会进行地址空间校验要求src字符串必须来自用户空间。当源字符串来自内核空间时将返回0.
##### 参数
**src**
2022-07-26 10:05:17 +00:00
  源字符串,地址位于用户空间
2022-07-12 12:39:33 +00:00
**maxlen**
2022-07-26 10:05:17 +00:00
  最大长度
2022-07-12 12:39:33 +00:00
#### `char *strncpy(char *dst, const char *src, long count)`
##### 描述
2022-07-26 10:05:17 +00:00
  拷贝长度为count个字节的字符串返回dst字符串
2022-07-12 12:39:33 +00:00
##### 参数
**src**
2022-07-26 10:05:17 +00:00
  源字符串
2022-07-12 12:39:33 +00:00
**dst**
  目标字符串
**count**
2022-07-26 10:05:17 +00:00
  要拷贝的源字符串的长度
2022-07-12 12:39:33 +00:00
2022-08-03 07:13:01 +00:00
#### `char *strcpy(char *dst, const char *src)`
##### 描述
  拷贝源字符串返回dst字符串
##### 参数
**src**
  源字符串
**dst**
  目标字符串
2022-07-12 12:39:33 +00:00
#### `long strncpy_from_user(char *dst, const char *src, unsigned long size)`
##### 描述
  从用户空间拷贝长度为count个字节的字符串到内核空间返回拷贝的字符串的大小
  该函数会对字符串的地址空间进行校验,防止出现地址空间越界的问题。
##### 参数
**src**
  源字符串
**dst**
2022-07-26 10:05:17 +00:00
  目标字符串
2022-07-12 12:39:33 +00:00
**size**
  要拷贝的源字符串的长度
#### `int strcmp(char *FirstPart, char *SecondPart)`
##### 描述
  比较两个字符串的大小。
***返回值***
| 情况 | 返回值 |
| ----------------------- | --- |
| FirstPart == SecondPart | 0 |
| FirstPart > SecondPart | 1 |
| FirstPart < SecondPart | -1 |
##### 参数
**FirstPart**
2022-07-26 10:05:17 +00:00
&emsp;&emsp;第一个字符串
2022-07-12 12:39:33 +00:00
**SecondPart**
&emsp;&emsp;第二个字符串
2022-07-26 10:05:17 +00:00
#### `printk(const char* fmt, ...)`
##### 描述
&emsp;&emsp;该宏能够在控制台上以黑底白字格式化输出字符串.
##### 参数
**fmt**
&emsp;&emsp;源格式字符串
**...**
&emsp;&emsp;可变参数
#### `printk_color(unsigned int FRcolor, unsigned int BKcolor, const char* fmt, ...)`
##### 描述
&emsp;&emsp;在控制台上以指定前景色和背景色格式化输出字符串.
##### 参数
**FRcolor**
&emsp;&emsp;前景色
**BKcolor**
&emsp;&emsp;背景色
**fmt**
&emsp;&emsp;源格式字符串
**...**
&emsp;&emsp;可变参数
#### `int vsprintf(char *buf, const char *fmt, va_list args)`
##### 描述
&emsp;&emsp;按照fmt格式化字符串并将结果输出到buf中返回写入buf的字符数量。
##### 参数
**buf**
&emsp;&emsp;输出缓冲区
**fmt**
&emsp;&emsp;源格式字符串
**args**
&emsp;&emsp;可变参数列表
#### `int sprintk(char *buf, const char *fmt, ...)`
##### 描述
&emsp;&emsp;按照fmt格式化字符串并将结果输出到buf中返回写入buf的字符数量。
##### 参数
**buf**
&emsp;&emsp;输出缓冲区
**fmt**
2022-07-12 12:39:33 +00:00
2022-07-26 10:05:17 +00:00
&emsp;&emsp;源格式字符串
2022-07-12 12:39:33 +00:00
2022-07-26 10:05:17 +00:00
**...**
2022-07-12 12:39:33 +00:00
2022-07-26 10:05:17 +00:00
&emsp;&emsp;可变参数