xref: /DragonOS/docs/kernel/core_api/kernel_api.md (revision 39a09ffd72e8d536137c0190ce4636d9e930c6ec)
1# DragonOS内核核心API
2
3## 循环链表管理函数
4
5  循环链表是内核的重要的数据结构之一。包含在`kernel/common/glib.h`中。
6
7### `void list_init(struct List *list)`
8
9#### 描述
10
11  初始化一个List结构体,使其prev和next指针指向自身
12
13#### 参数
14
15**list**
16
17  要被初始化的List结构体
18
19### `void list_add(struct List *entry, struct List *node)`
20
21#### 描述
22
23  将node插入到entry的后方
24
25#### 参数
26
27**entry**
28
29  已存在于循环链表中的一个结点
30
31**node**
32
33  待插入的结点
34
35### `void list_append(struct List *entry, struct List *node)`
36
37#### 描述
38
39  将node插入到entry的前方
40
41#### 参数
42
43**entry**
44
45  已存在于循环链表中的一个结点
46
47**node**
48
49  待插入的结点
50
51### `void list_del(struct List *entry)`
52
53#### 描述
54
55  从链表中删除结点entry
56
57#### 参数
58
59**entry**
60
61  待删除的结点
62
63### `bool list_empty(struct List *entry)`
64
65#### 描述
66
67  判断链表是否为空
68
69#### 参数
70
71**entry**
72
73  链表中的一个结点
74
75### `struct List *list_prev(struct List *entry)`
76
77#### 描述
78
79  获取entry的前一个结点
80
81#### 参数
82
83**entry**
84
85  链表中的一个结点
86
87### `struct List *list_next(struct List *entry)`
88
89#### 描述
90
91  获取entry的后一个结点
92
93#### 参数
94
95**entry**
96
97  链表中的一个结点
98
99---
100
101## 基础C函数库
102
103  内核编程与应用层编程不同,你将无法使用LibC中的函数来进行编程。为此,内核实现了一些常用的C语言函数,并尽量使其与标准C库中的函数行为相近。值得注意的是,这些函数的行为可能与标准C库函数不同,请在使用时仔细阅读以下文档,这将会为你带来帮助。
104
105### 字符串操作
106
107#### `int strlen(const char *s)`
108
109##### 描述
110
111测量并返回字符串长度。
112
113##### 参数
114
115**src**
116
117源字符串
118
119#### `long strnlen(const char *src, unsigned long maxlen)`
120
121##### 描述
122
123  测量并返回字符串长度。当字符串长度大于maxlen时,返回maxlen
124
125##### 参数
126
127**src**
128
129  源字符串
130
131**maxlen**
132
133  最大长度
134
135#### `long strnlen_user(const char *src, unsigned long maxlen)`
136
137##### 描述
138
139  测量并返回字符串长度。当字符串长度大于maxlen时,返回maxlen。
140
141  该函数会进行地址空间校验,要求src字符串必须来自用户空间。当源字符串来自内核空间时,将返回0.
142
143##### 参数
144
145**src**
146
147  源字符串,地址位于用户空间
148
149**maxlen**
150
151  最大长度
152
153#### `char *strncpy(char *dst, const char *src, long count)`
154
155##### 描述
156
157  拷贝长度为count个字节的字符串,返回dst字符串
158
159##### 参数
160
161**src**
162
163  源字符串
164
165**dst**
166
167  目标字符串
168
169**count**
170
171  要拷贝的源字符串的长度
172
173
174
175#### `long strncpy_from_user(char *dst, const char *src, unsigned long size)`
176
177##### 描述
178
179  从用户空间拷贝长度为count个字节的字符串到内核空间,返回拷贝的字符串的大小
180
181  该函数会对字符串的地址空间进行校验,防止出现地址空间越界的问题。
182
183##### 参数
184
185**src**
186
187  源字符串
188
189**dst**
190
191  目标字符串
192
193**size**
194
195  要拷贝的源字符串的长度
196
197#### `int strcmp(char *FirstPart, char *SecondPart)`
198
199##### 描述
200
201  比较两个字符串的大小。
202
203***返回值***
204
205| 情况                      | 返回值 |
206| ----------------------- | --- |
207| FirstPart == SecondPart | 0   |
208| FirstPart > SecondPart  | 1   |
209| FirstPart < SecondPart  | -1  |
210
211##### 参数
212
213**FirstPart**
214
215  第一个字符串
216
217**SecondPart**
218
219&emsp;&emsp;第二个字符串
220
221
222
223
224