mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-08 10:15:03 +00:00
删除一些过时的C代码 (#565)
* 删除C版本的crc库 * 删除lockref * 删除过时的libc文档以及wait.c * 删除过时的C版本kfifo代码及文档 * 移除未用到的lz4库 * 删除内核的stdlib.c * 删除fabs.c * fmt * 使得put_string系统调用能够通过tty输出颜色 暂且这样改,这一部分应该是用户层面提供的功能,后续删除 --------- Co-authored-by: GnoCiYeH <heyicong@dragonos.org>
This commit is contained in:
parent
5eeefb8c80
commit
c3dc6f2ff9
26
.vscode/settings.json
vendored
26
.vscode/settings.json
vendored
@ -1,6 +1,5 @@
|
|||||||
{
|
{
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"stdlib.h": "c",
|
|
||||||
"stdbool.h": "c",
|
"stdbool.h": "c",
|
||||||
"printk.h": "c",
|
"printk.h": "c",
|
||||||
"stdarg.h": "c",
|
"stdarg.h": "c",
|
||||||
@ -12,11 +11,8 @@
|
|||||||
"memory.h": "c",
|
"memory.h": "c",
|
||||||
"multiboot2.h": "c",
|
"multiboot2.h": "c",
|
||||||
"kprint.h": "c",
|
"kprint.h": "c",
|
||||||
"8259a.h": "c",
|
|
||||||
"ptrace.h": "c",
|
"ptrace.h": "c",
|
||||||
"mouse.h": "c",
|
"mouse.h": "c",
|
||||||
"keyboard.h": "c",
|
|
||||||
"ps2_keyboard.h": "c",
|
|
||||||
"algorithm": "c",
|
"algorithm": "c",
|
||||||
"array": "c",
|
"array": "c",
|
||||||
"atomic": "c",
|
"atomic": "c",
|
||||||
@ -95,22 +91,17 @@
|
|||||||
"spinlock.h": "c",
|
"spinlock.h": "c",
|
||||||
"stat.h": "c",
|
"stat.h": "c",
|
||||||
"video.h": "c",
|
"video.h": "c",
|
||||||
"libm.h": "c",
|
|
||||||
"ahci.h": "c",
|
"ahci.h": "c",
|
||||||
"slab.h": "c",
|
"slab.h": "c",
|
||||||
"boot_info.h": "c",
|
"boot_info.h": "c",
|
||||||
"pci.h": "c",
|
"pci.h": "c",
|
||||||
"time.h": "c",
|
"time.h": "c",
|
||||||
"ia64_msi.h": "c",
|
|
||||||
"errno.h": "c",
|
"errno.h": "c",
|
||||||
"bug.h": "c",
|
"bug.h": "c",
|
||||||
"sched.h": "c",
|
"sched.h": "c",
|
||||||
"preempt.h": "c",
|
"preempt.h": "c",
|
||||||
"screen_manager.h": "c",
|
|
||||||
"textui.h": "c",
|
"textui.h": "c",
|
||||||
"atomic.h": "c",
|
"atomic.h": "c",
|
||||||
"uart.h": "c",
|
|
||||||
"fat_ent.h": "c",
|
|
||||||
"semaphore.h": "c",
|
"semaphore.h": "c",
|
||||||
"mm-types.h": "c",
|
"mm-types.h": "c",
|
||||||
"current.h": "c",
|
"current.h": "c",
|
||||||
@ -120,27 +111,12 @@
|
|||||||
"mutex.h": "c",
|
"mutex.h": "c",
|
||||||
"mount.h": "c",
|
"mount.h": "c",
|
||||||
"internal.h": "c",
|
"internal.h": "c",
|
||||||
"devfs.h": "c",
|
|
||||||
"devfs-types.h": "c",
|
|
||||||
"chardev.h": "c",
|
|
||||||
"rootfs.h": "c",
|
|
||||||
"tty.h": "c",
|
|
||||||
"idr.h": "c",
|
|
||||||
"ktest_utils.h": "c",
|
|
||||||
"kthread.h": "c",
|
|
||||||
"lockref.h": "c",
|
|
||||||
"compiler_attributes.h": "c",
|
"compiler_attributes.h": "c",
|
||||||
"timer.h": "c",
|
"timer.h": "c",
|
||||||
"hid.h": "c",
|
"hid.h": "c",
|
||||||
"proc.h": "c",
|
|
||||||
"compiler.h": "c",
|
"compiler.h": "c",
|
||||||
"hpet.h": "c",
|
|
||||||
"numa.h": "c",
|
|
||||||
"procfs.h": "c",
|
|
||||||
"err.h": "c",
|
"err.h": "c",
|
||||||
"rtc.h": "c",
|
|
||||||
"list.h": "c",
|
"list.h": "c",
|
||||||
"fat32.h": "c",
|
|
||||||
"irqflags.h": "c",
|
"irqflags.h": "c",
|
||||||
"dirent.h": "c",
|
"dirent.h": "c",
|
||||||
"cmd_help.h": "c",
|
"cmd_help.h": "c",
|
||||||
@ -155,7 +131,6 @@
|
|||||||
"cmd.h": "c",
|
"cmd.h": "c",
|
||||||
"sleep.h": "c",
|
"sleep.h": "c",
|
||||||
"net.h": "c",
|
"net.h": "c",
|
||||||
"lz4.h": "c",
|
|
||||||
"cmd_test.h": "c",
|
"cmd_test.h": "c",
|
||||||
"cmpxchg.h": "c",
|
"cmpxchg.h": "c",
|
||||||
"mman.h": "c",
|
"mman.h": "c",
|
||||||
@ -165,7 +140,6 @@
|
|||||||
"charconv": "c",
|
"charconv": "c",
|
||||||
"printf.h": "c",
|
"printf.h": "c",
|
||||||
"klog.h": "c",
|
"klog.h": "c",
|
||||||
"sqlite3ext.h": "c",
|
|
||||||
"malloc.h": "c",
|
"malloc.h": "c",
|
||||||
"*.o": "c",
|
"*.o": "c",
|
||||||
"k_log.h": "c"
|
"k_log.h": "c"
|
||||||
|
@ -38,7 +38,6 @@
|
|||||||
:caption: 应用层
|
:caption: 应用层
|
||||||
|
|
||||||
userland/appdev/index
|
userland/appdev/index
|
||||||
userland/libc/index
|
|
||||||
|
|
||||||
.. toctree::
|
.. toctree::
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
@ -73,16 +73,9 @@
|
|||||||
- [x] softirq 软中断
|
- [x] softirq 软中断
|
||||||
- [x] 内核栈traceback
|
- [x] 内核栈traceback
|
||||||
|
|
||||||
### 内核数据结构
|
|
||||||
|
|
||||||
- [x] 普通二叉树
|
|
||||||
- [x] kfifo缓冲区
|
|
||||||
- [x] 循环链表
|
|
||||||
- [x] IDR
|
|
||||||
|
|
||||||
### 内核实用库
|
### 内核实用库
|
||||||
|
|
||||||
- [x] LZ4压缩库(1.9.3)
|
|
||||||
- [x] 字符串操作库
|
- [x] 字符串操作库
|
||||||
- [x] ELF可执行文件支持
|
- [x] ELF可执行文件支持
|
||||||
- [x] printk
|
- [x] printk
|
||||||
|
@ -1,256 +0,0 @@
|
|||||||
# 内核数据结构
|
|
||||||
|
|
||||||
  内核中实现了常用的几种数据结构,这里是他们的api文档。
|
|
||||||
|
|
||||||
--------------
|
|
||||||
## kfifo先进先出缓冲区
|
|
||||||
|
|
||||||
  kfifo先进先出缓冲区定义于`common/kfifo.h`中。您可以使用它,创建指定大小的fifo缓冲区(最大大小为4GB)
|
|
||||||
|
|
||||||
### kfifo_alloc
|
|
||||||
|
|
||||||
`int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  通过动态方式初始化kfifo缓冲队列。fifo缓冲区的buffer将由该函数进行申请。
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
**size**
|
|
||||||
|
|
||||||
  缓冲区大小(单位:bytes)
|
|
||||||
|
|
||||||
**reserved**
|
|
||||||
|
|
||||||
  当前字段保留,请将其置为0
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
  当返回值为0时,表示正常初始化成功,否则返回对应的errno
|
|
||||||
|
|
||||||
### kfifo_init
|
|
||||||
|
|
||||||
`void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  使用指定的缓冲区来初始化kfifo缓冲队列
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
**buffer**
|
|
||||||
|
|
||||||
  缓冲区基地址指针
|
|
||||||
|
|
||||||
**size**
|
|
||||||
|
|
||||||
  缓冲区大小(单位:bytes)
|
|
||||||
|
|
||||||
### kfifo_free_alloc
|
|
||||||
|
|
||||||
`void kfifo_free_alloc(struct kfifo_t* fifo)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  释放通过kfifo_alloc创建的fifo缓冲区. 请勿通过该函数释放其他方式创建的kfifo缓冲区。
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
### kfifo_in
|
|
||||||
|
|
||||||
`uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  向kfifo缓冲区推入指定大小的数据。当队列中空间不足时,则不推入数据。
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
**from**
|
|
||||||
|
|
||||||
  源数据基地址指针
|
|
||||||
|
|
||||||
**size**
|
|
||||||
|
|
||||||
  数据大小(单位:bytes)
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
  返回成功被推入的数据的大小。
|
|
||||||
|
|
||||||
### kfifo_out
|
|
||||||
|
|
||||||
`uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  从kfifo缓冲区取出数据,并从队列中删除数据。当队列中数据量不足时,则不取出。
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
**to**
|
|
||||||
|
|
||||||
  目标缓冲区基地址指针
|
|
||||||
|
|
||||||
**size**
|
|
||||||
|
|
||||||
  数据大小(单位:bytes)
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
  返回成功被取出的数据的大小。
|
|
||||||
|
|
||||||
### kfifo_out_peek
|
|
||||||
|
|
||||||
`uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  从kfifo缓冲区取出数据,但是不从队列中删除数据。当队列中数据量不足时,则不取出。
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
**to**
|
|
||||||
|
|
||||||
  目标缓冲区基地址指针
|
|
||||||
|
|
||||||
**size**
|
|
||||||
|
|
||||||
  数据大小(单位:bytes)
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
  返回成功被取出的数据的大小。
|
|
||||||
|
|
||||||
### kfifo_reset
|
|
||||||
|
|
||||||
`kfifo_reset(fifo)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  忽略kfifo队列中的所有内容,并把输入和输出偏移量都归零
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
### kfifo_reset_out
|
|
||||||
|
|
||||||
`kfifo_reset_out(fifo)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  忽略kfifo队列中的所有内容,并将输入偏移量赋值给输出偏移量
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
### kfifo_total_size
|
|
||||||
|
|
||||||
`kfifo_total_size(fifo)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  获取kfifo缓冲区的最大大小
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
  缓冲区最大大小
|
|
||||||
|
|
||||||
### kfifo_size
|
|
||||||
|
|
||||||
`kfifo_size(fifo)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  获取kfifo缓冲区当前已使用的大小
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
  缓冲区当前已使用的大小
|
|
||||||
|
|
||||||
### kfifo_empty
|
|
||||||
|
|
||||||
`kfifo_empty(fifo)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  判断kfifo缓冲区当前是否为空
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
| 情况 | 返回值 |
|
|
||||||
| ----------------------- | --- |
|
|
||||||
| 空 | 1 |
|
|
||||||
| 非空 | 0 |
|
|
||||||
|
|
||||||
### kfifo_full
|
|
||||||
|
|
||||||
`kfifo_full(fifo)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  判断kfifo缓冲区当前是否为满
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**fifo**
|
|
||||||
|
|
||||||
  kfifo队列结构体的指针
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
| 情况 | 返回值 |
|
|
||||||
| ------| --- |
|
|
||||||
| 满 | 1 |
|
|
||||||
| 不满 | 0 |
|
|
||||||
|
|
||||||
------------------
|
|
@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
kernel_api
|
kernel_api
|
||||||
atomic
|
atomic
|
||||||
data_structures
|
|
||||||
casting
|
casting
|
||||||
notifier_chain
|
notifier_chain
|
||||||
softirq
|
softirq
|
||||||
|
@ -693,36 +693,3 @@
|
|||||||
**size**
|
**size**
|
||||||
|
|
||||||
  待拷贝的数据大小
|
  待拷贝的数据大小
|
||||||
|
|
||||||
## CRC函数
|
|
||||||
|
|
||||||
### 函数列表
|
|
||||||
|
|
||||||
**`uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len)`**
|
|
||||||
|
|
||||||
**`uint8_t crc8(uint8_t crc, const uint8_t *buffer, size_t len)`**
|
|
||||||
|
|
||||||
**`uint16_t crc16(uint16_t crc, uint8_t const *buffer, size_t len)`**
|
|
||||||
|
|
||||||
**`uint32_t crc32(uint32_t crc, uint8_t const *buffer, size_t len)`**
|
|
||||||
|
|
||||||
**`uint64_t crc64(uint64_t crc, uint8_t const *buffer, size_t len)`**
|
|
||||||
|
|
||||||
### 描述
|
|
||||||
|
|
||||||
  用于计算循环冗余校验码
|
|
||||||
|
|
||||||
### 参数说明
|
|
||||||
|
|
||||||
**crc**
|
|
||||||
|
|
||||||
  传入的CRC初始值
|
|
||||||
|
|
||||||
**buffer**
|
|
||||||
|
|
||||||
  待处理的数据缓冲区
|
|
||||||
|
|
||||||
**len**
|
|
||||||
|
|
||||||
  缓冲区大小(字节)
|
|
||||||
|
|
||||||
|
@ -17,4 +17,3 @@
|
|||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
:caption: 目录
|
:caption: 目录
|
||||||
|
|
||||||
ktest-framework
|
|
||||||
|
@ -1,114 +0,0 @@
|
|||||||
# 内核测试框架
|
|
||||||
|
|
||||||
  DragonOS提供了一个测试框架,旨在对内核的一些模块进行自动化测试。内核测试框架位于`ktest/`下。
|
|
||||||
|
|
||||||
  我们可以使用这个测试框架,按照规范编写测试代码,然后在合适的地方使用`ktest_start()`创建一个全新的内核线程并发起测试。
|
|
||||||
|
|
||||||
## 使用方法
|
|
||||||
|
|
||||||
### 创建自动测试程序
|
|
||||||
|
|
||||||
  假如您要对kfifo模块进行自动测试,您可以在`ktest/`下,创建一个名为`test-kfifo.c`的测试文件,并编写Makefile。
|
|
||||||
|
|
||||||
  在`test-kfifo.c`中,包含`ktest_utils.h`和`ktest.h`这两个头文件。
|
|
||||||
|
|
||||||
  您需要像下面这样,在`test-kfifo.c`中,创建一个测试用例函数表,并把测试用例函数填写到其中:
|
|
||||||
```c
|
|
||||||
static ktest_case_table kt_kfifo_func_table[] = {
|
|
||||||
ktest_kfifo_case0_1,
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
  然后创建一个函数,作为kfifo测试的主函数。请注意,您需要将它的声明添加到`ktest.h`中。
|
|
||||||
|
|
||||||
```c
|
|
||||||
uint64_t ktest_test_kfifo(uint64_t arg)
|
|
||||||
{
|
|
||||||
kTEST("Testing kfifo...");
|
|
||||||
for (int i = 0; i < sizeof(kt_kfifo_func_table) / sizeof(ktest_case_table); ++i)
|
|
||||||
{
|
|
||||||
kTEST("Testing case %d", i);
|
|
||||||
kt_kfifo_func_table[i](i, 0);
|
|
||||||
}
|
|
||||||
kTEST("kfifo Test done.");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### 编写测试用例
|
|
||||||
|
|
||||||
  您可以创建一个或多个测试用例,命名为:`ktest_kfifo_case_xxxxx`. 在这个例子中,我创建了一个测试用例,命名为:`ktest_kfifo_case0_1`.如下所示:
|
|
||||||
|
|
||||||
```c
|
|
||||||
static long ktest_kfifo_case0_1(uint64_t arg0, uint64_t arg1)
|
|
||||||
```
|
|
||||||
|
|
||||||
  这里最多允许我们传递两个参数到测试函数里面。
|
|
||||||
|
|
||||||
  那么,我们该如何编写测试用例呢?
|
|
||||||
|
|
||||||
  我们主要是需要设置一些情节,以便能测试到目标组件的每个情况。为了检验模块的行为是否符合预期,我们需要使用`assert(condition)`宏函数,对目标`condition`进行校验。若`condition`为1,则表明测试通过。否则,将会输出一行assert failed信息到屏幕上。
|
|
||||||
|
|
||||||
### 发起测试
|
|
||||||
|
|
||||||
  我们可以在pid≥1的内核线程中发起测试。由于DragonOS目前尚不完善,您可以在`process/process.c`中的`initial_kernel_thread()`函数内,发起内核自动测试。具体的代码如下:
|
|
||||||
|
|
||||||
```c
|
|
||||||
ktest_start(ktest_test_kfifo, 0);
|
|
||||||
```
|
|
||||||
|
|
||||||
  这样就发起了一个内核测试,它会创建一个新的内核线程进行自动测试,您不必担心第一个内核线程会被阻塞。
|
|
||||||
  
|
|
||||||
|
|
||||||
## API文档
|
|
||||||
|
|
||||||
### ktest_start
|
|
||||||
|
|
||||||
`pid_t ktest_start(uint64_t (*func)(uint64_t arg), uint64_t arg)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  开启一个新的内核线程以进行测试
|
|
||||||
|
|
||||||
#### 参数
|
|
||||||
|
|
||||||
**func**
|
|
||||||
|
|
||||||
  测试函数. 新的测试线程将会执行该函数,以进行测试。
|
|
||||||
|
|
||||||
**arg**
|
|
||||||
|
|
||||||
  传递给测试函数的参数
|
|
||||||
|
|
||||||
#### 返回值
|
|
||||||
|
|
||||||
  测试线程的pid
|
|
||||||
|
|
||||||
### assert
|
|
||||||
|
|
||||||
`#define assert(condition)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  判定condition是否为1,若不为1,则输出一行错误日志信息:
|
|
||||||
|
|
||||||
```
|
|
||||||
[ kTEST FAILED ] Ktest Assertion Failed, file:%s, Line:%d
|
|
||||||
```
|
|
||||||
|
|
||||||
### kTEST
|
|
||||||
|
|
||||||
```#define kTEST(...) ```
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
|
|
||||||
  格式化输出一行以`[ kTEST ] file:%s, Line:%d`开头的日志信息。
|
|
||||||
|
|
||||||
|
|
||||||
### ktest_case_table
|
|
||||||
|
|
||||||
`typedef long (*ktest_case_table)(uint64_t arg0, uint64_t arg1)`
|
|
||||||
|
|
||||||
#### 描述
|
|
||||||
  ktest用例函数的类型定义。
|
|
@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
locks
|
locks
|
||||||
spinlock
|
spinlock
|
||||||
lockref
|
|
||||||
mutex
|
mutex
|
||||||
rwlock
|
rwlock
|
||||||
|
|
||||||
|
@ -1,181 +0,0 @@
|
|||||||
(_lockref)=
|
|
||||||
# lockref
|
|
||||||
|
|
||||||
  lockref是将自旋锁与引用计数变量融合在连续、对齐的8字节内的一种技术。
|
|
||||||
|
|
||||||
  目前,DragonOS中,通过C、Rust各实现了一个版本的lockref。请注意,二者不兼容。对于新的功能模块,请使用Rust版本的lockref。随着代码重构工作的进行,我们将会删除C版本的lockref。
|
|
||||||
|
|
||||||
## 1. lockref结构
|
|
||||||
|
|
||||||
### 1.1. Rust版本
|
|
||||||
```rust
|
|
||||||
/// 仅在x86_64架构下使用cmpxchg
|
|
||||||
#[cfg(target_arch = "x86_64")]
|
|
||||||
/// 由于需要cmpxchg,所以整个lockref按照8字节对齐
|
|
||||||
#[repr(align(8))]
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct LockRef {
|
|
||||||
pub lock: RawSpinlock,
|
|
||||||
pub count: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 除了x86_64以外的架构,不使用cmpxchg进行优化
|
|
||||||
#[cfg(not(target_arch = "x86_64"))]
|
|
||||||
pub struct LockRef {
|
|
||||||
lock: RawSpinlock,
|
|
||||||
count: i32,
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 1.2. C版本
|
|
||||||
```c
|
|
||||||
struct lockref
|
|
||||||
{
|
|
||||||
union
|
|
||||||
{
|
|
||||||
#ifdef __LOCKREF_ENABLE_CMPXCHG__
|
|
||||||
aligned_u64 lock_count; // 通过该变量的声明,使得整个lockref的地址按照8字节对齐
|
|
||||||
#endif
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
spinlock_t lock;
|
|
||||||
int count;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
## 2. 特性描述
|
|
||||||
  由于在高负载的情况下,系统会频繁的执行“锁定-改变引用变量-解锁”的操作,这期间很可能出现spinlock和引用计数跨缓存行的情况,这将会大大降低性能。lockref通过强制对齐,尽可能的降低缓存行的占用数量,使得性能得到提升。
|
|
||||||
|
|
||||||
  并且,在x64体系结构下,还通过cmpxchg()指令,实现了无锁快速路径。不需要对自旋锁加锁即可更改引用计数的值,进一步提升性能。当快速路径不存在(对于未支持的体系结构)或者尝试超时后,将会退化成“锁定-改变引用变量-解锁”的操作。此时由于lockref强制对齐,只涉及到1个缓存行,因此性能比原先的spinlock+ref_count的模式要高。
|
|
||||||
|
|
||||||
## 3. 关于cmpxchg_loop
|
|
||||||
|
|
||||||
  在改变引用计数时,cmpxchg先确保没有别的线程持有锁,然后改变引用计数,同时通过`lock cmpxchg`指令验证在更改发生时,没有其他线程持有锁,并且当前的目标lockref的值与old变量中存储的一致,从而将新值存储到目标lockref。这种无锁操作能极大的提升性能。如果不符合上述条件,在多次尝试后,将退化成传统的加锁方式来更改引用计数。
|
|
||||||
|
|
||||||
## 4. Rust版本的API
|
|
||||||
|
|
||||||
### 4.1. 引用计数自增
|
|
||||||
|
|
||||||
- `pub fn inc(&mut self)`
|
|
||||||
- `pub fn inc_not_zero(&mut self) -> Result<i32, SystemError>`
|
|
||||||
- `pub fn inc_not_dead(&mut self) -> Result<i32, SystemError>`
|
|
||||||
|
|
||||||
#### 4.1.1. inc
|
|
||||||
|
|
||||||
##### 说明
|
|
||||||
|
|
||||||
  原子的将引用计数加1。
|
|
||||||
|
|
||||||
##### 返回值
|
|
||||||
|
|
||||||
  无
|
|
||||||
|
|
||||||
#### 4.1.2. inc_not_zero
|
|
||||||
|
|
||||||
##### 说明
|
|
||||||
|
|
||||||
  原子地将引用计数加1.如果原来的count≤0,则操作失败。
|
|
||||||
|
|
||||||
##### 返回值
|
|
||||||
|
|
||||||
| 返回值 | 说明 |
|
|
||||||
| :--- | :--- |
|
|
||||||
| Ok(self.count) | 成功,返回新的引用计数 |
|
|
||||||
| Err(SystemError::EPERM) | 失败,返回EPERM |
|
|
||||||
|
|
||||||
#### 4.1.3. inc_not_dead
|
|
||||||
|
|
||||||
##### 说明
|
|
||||||
|
|
||||||
  引用计数自增1。(除非该lockref已经被标记为死亡)
|
|
||||||
|
|
||||||
##### 返回值
|
|
||||||
|
|
||||||
| 返回值 | 说明 |
|
|
||||||
| :--- | :--- |
|
|
||||||
| Ok(self.count) | 成功,返回新的引用计数 |
|
|
||||||
| Err(SystemError::EPERM) | 失败,返回EPERM |
|
|
||||||
|
|
||||||
### 4.2. 引用计数自减
|
|
||||||
- `pub fn dec(&mut self) -> Result<i32, SystemError>`
|
|
||||||
- `pub fn dec_return(&mut self) -> Result<i32, SystemError>`
|
|
||||||
- `pub fn dec_not_zero(&mut self) -> Result<i32, SystemError>`
|
|
||||||
- `pub fn dec_or_lock_not_zero(&mut self) -> Result<i32, SystemError>`
|
|
||||||
|
|
||||||
#### 4.2.1. dec
|
|
||||||
|
|
||||||
##### 说明
|
|
||||||
|
|
||||||
  原子地将引用计数-1。如果已处于count≤0的状态,则返回Err(SystemError::EPERM)
|
|
||||||
|
|
||||||
  本函数与`lockref_dec_return()`的区别在于,当在`cmpxchg()`中检测到`count<=0`或已加锁,本函数会再次尝试通过加锁来执行操作,而`lockref_dec_return()`会直接返回错误
|
|
||||||
|
|
||||||
##### 返回值
|
|
||||||
|
|
||||||
| 返回值 | 说明 |
|
|
||||||
| :--- | :--- |
|
|
||||||
| Ok(self.count) | 成功,返回新的引用计数 |
|
|
||||||
| Err(SystemError::EPERM) | 失败,返回EPERM |
|
|
||||||
|
|
||||||
#### 4.2.2. dec_return
|
|
||||||
|
|
||||||
  原子地将引用计数减1。如果处于已加锁或count≤0的状态,则返回SystemError::EPERM
|
|
||||||
|
|
||||||
  本函数与`lockref_dec()`的区别在于,当在`cmpxchg()`中检测到`count<=0`或已加锁,本函数会直接返回错误,而`lockref_dec()`会再次尝试通过加锁来执行操作.
|
|
||||||
|
|
||||||
:::{note}
|
|
||||||
若当前处理器架构不支持cmpxchg,则退化为`self.dec()`
|
|
||||||
:::
|
|
||||||
|
|
||||||
##### 返回值
|
|
||||||
|
|
||||||
| 返回值 | 说明 |
|
|
||||||
| :--- | :--- |
|
|
||||||
| Ok(self.count) | 成功,返回新的引用计数 |
|
|
||||||
| Err(SystemError::EPERM) | 失败,返回EPERM |
|
|
||||||
|
|
||||||
#### 4.2.3. dec_not_zero
|
|
||||||
|
|
||||||
##### 说明
|
|
||||||
|
|
||||||
  原子地将引用计数减1。若当前的引用计数≤1,则操作失败.
|
|
||||||
|
|
||||||
  该函数与`lockref_dec_or_lock_not_zero()`的区别在于,当`cmpxchg()`时发现`old.count≤1`时,该函数会直接返回`Err(-1)`,而`lockref_dec_or_lock_not_zero()`在这种情况下,会尝试加锁来进行操作。
|
|
||||||
|
|
||||||
##### 返回值
|
|
||||||
|
|
||||||
| 返回值 | 说明 |
|
|
||||||
| :--- | :--- |
|
|
||||||
| Ok(self.count) | 成功,返回新的引用计数 |
|
|
||||||
| Err(SystemError::EPERM) | 失败,返回EPERM |
|
|
||||||
|
|
||||||
|
|
||||||
#### 4.2.4. dec_or_lock_not_zero
|
|
||||||
|
|
||||||
##### 说明
|
|
||||||
|
|
||||||
  原子地将引用计数减1。若当前的引用计数≤1,则操作失败.
|
|
||||||
|
|
||||||
  该函数与`lockref_dec_not_zero()`的区别在于,当cmpxchg()时发现`old.count≤1`时,该函数会尝试加锁来进行操作,而`lockref_dec_not_zero()`在这种情况下,会直接返回`Err(SystemError::EPERM)`.
|
|
||||||
|
|
||||||
##### 返回值
|
|
||||||
|
|
||||||
| 返回值 | 说明 |
|
|
||||||
| :--- | :--- |
|
|
||||||
| Ok(self.count) | 成功,返回新的引用计数 |
|
|
||||||
| Err(SystemError::EPERM) | 失败,返回EPERM |
|
|
||||||
|
|
||||||
### 4.3. 其他
|
|
||||||
- `pub fn mark_dead(&mut self)`
|
|
||||||
|
|
||||||
#### 4.3.1. mark_dead
|
|
||||||
|
|
||||||
##### 说明
|
|
||||||
|
|
||||||
  将引用计数原子地标记为死亡状态.
|
|
||||||
|
|
||||||
## 参考资料
|
|
||||||
|
|
||||||
  [Introducing lockrefs - LWN.net, Jonathan Corbet](https://lwn.net/Articles/565734/)
|
|
@ -1,38 +0,0 @@
|
|||||||
# ctype.h
|
|
||||||
## 函数列表(这里只列出已实现的函数):
|
|
||||||
|
|
||||||
``int isprint(int c)`` : 传入一个字符,判断是否可以被输出
|
|
||||||
|
|
||||||
``int islower(int c)`` : 传入一个字符,判断是否是小写字母
|
|
||||||
|
|
||||||
``int isupper(int c)`` : 传入一个字符,判断是否是大写字母
|
|
||||||
|
|
||||||
``int isalpha(int c)`` : 传入一个字符,判断是否是字母
|
|
||||||
|
|
||||||
``int isdigit(int c)`` : 传入一个字符,判断是否是数字
|
|
||||||
|
|
||||||
``int toupper(int c)`` : 传入一个小写字母字符,返回这个字母的大写形式
|
|
||||||
|
|
||||||
``int tolower(int c)`` : 传入一个大写字母字符,返回这个字母的小写形式
|
|
||||||
|
|
||||||
``int isspace(int c)`` : 传入一个字符,判断是否是空白字符
|
|
||||||
|
|
||||||
## 宏定义:
|
|
||||||
|
|
||||||
### 暂无用处
|
|
||||||
|
|
||||||
``#define _U 01``
|
|
||||||
|
|
||||||
``#define _L 02``
|
|
||||||
|
|
||||||
``#define _N 04``
|
|
||||||
|
|
||||||
``#define _S 010``
|
|
||||||
|
|
||||||
``#define _P 020``
|
|
||||||
|
|
||||||
``#define _C 040``
|
|
||||||
|
|
||||||
``#define _X 0100``
|
|
||||||
|
|
||||||
``#define _B 0200``
|
|
@ -1,59 +0,0 @@
|
|||||||
# dirent.h
|
|
||||||
## 简介
|
|
||||||
与文件夹有关的头文件。
|
|
||||||
|
|
||||||
## 结构体列表:
|
|
||||||
|
|
||||||
``struct DIR`` :
|
|
||||||
|
|
||||||
变量列表:
|
|
||||||
|
|
||||||
``int fd`` : 文件夹id(不推荐修改)
|
|
||||||
|
|
||||||
``int buf_pos`` : 文件夹缓冲区指针的位置
|
|
||||||
|
|
||||||
``int buf_len`` : 文件夹缓冲区的大小(默认为256)
|
|
||||||
|
|
||||||
``struct dirent`` :
|
|
||||||
|
|
||||||
变量列表:
|
|
||||||
|
|
||||||
``ino_t(see libc/sys/types.h) ino`` : 文件序列号(不推荐修改)
|
|
||||||
|
|
||||||
``off_t d_off`` : dir偏移量(不推荐修改)
|
|
||||||
|
|
||||||
``unsigned short d_reclen`` : 文件夹中的记录数
|
|
||||||
|
|
||||||
``unsigned char d_type`` : 目标的类型(有可能是文件,文件夹,磁盘)
|
|
||||||
|
|
||||||
``char d_name[]`` : 目标的名字
|
|
||||||
|
|
||||||
## 函数列表(这里只列出已实现的函数):
|
|
||||||
|
|
||||||
``DIR opendir(const char *path)``
|
|
||||||
|
|
||||||
传入文件夹的路径,返回文件夹结构体
|
|
||||||
|
|
||||||
``int closedir(DIR *dirp)``
|
|
||||||
|
|
||||||
传入文件夹结构体,关闭文件夹,释放内存
|
|
||||||
|
|
||||||
若失败,返回-1
|
|
||||||
|
|
||||||
``dirent readdir(DIR *dir)``
|
|
||||||
|
|
||||||
传入文件夹结构体,读入文件夹里的内容,并打包为dirent结构体返回
|
|
||||||
|
|
||||||
## 宏定义:
|
|
||||||
|
|
||||||
文件夹类型:
|
|
||||||
|
|
||||||
``#define VFS_IF_FILE (1UL << 0)``
|
|
||||||
|
|
||||||
``#define VFS_IF_DIR (1UL << 1)``
|
|
||||||
|
|
||||||
``#define VFS_IF_DEVICE (1UL << 2)``
|
|
||||||
|
|
||||||
缓冲区长度的默认值
|
|
||||||
|
|
||||||
``#define DIR_BUF_SIZE 256``
|
|
@ -1,185 +0,0 @@
|
|||||||
# errno.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
共享错误号码
|
|
||||||
## 属性:
|
|
||||||
|
|
||||||
``extern int errno`` : 通用错误代码
|
|
||||||
|
|
||||||
## 宏定义(复制自代码,了解即可):
|
|
||||||
|
|
||||||
#define E2BIG 1 /* 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.*/
|
|
||||||
|
|
||||||
#define EACCES 2 /* 访问被拒绝 Permission denied*/
|
|
||||||
|
|
||||||
#define EADDRINUSE 3 /* 地址正在被使用 Address in use.*/
|
|
||||||
|
|
||||||
#define EADDRNOTAVAIL 4 /* 地址不可用 Address not available.*/
|
|
||||||
|
|
||||||
#define EAFNOSUPPORT 5 /* 地址family不支持 Address family not supported.*/
|
|
||||||
|
|
||||||
#define EAGAIN 6 /* 资源不可用,请重试。 Resource unavailable, try again (may be the same value as [EWOULDBLOCK]).*/
|
|
||||||
|
|
||||||
#define EALREADY 7 /* 连接已经在处理 Connection already in progress.*/
|
|
||||||
|
|
||||||
#define EBADF 8 /* 错误的文件描述符 Bad file descriptor.*/
|
|
||||||
|
|
||||||
#define EBADMSG 9 /* 错误的消息 Bad message.*/
|
|
||||||
|
|
||||||
|
|
||||||
#define EBUSY 10 /* 设备或资源忙 Device or resource busy.*/
|
|
||||||
|
|
||||||
#define ECANCELED 11 /* 操作被取消 Operation canceled.*/
|
|
||||||
|
|
||||||
#define ECHILD 12 /* 没有子进程 No child processes.*/
|
|
||||||
|
|
||||||
#define ECONNABORTED 13 /* 连接已断开 Connection aborted.*/
|
|
||||||
|
|
||||||
#define ECONNREFUSED 14 /* 连接被拒绝 Connection refused.*/
|
|
||||||
|
|
||||||
#define ECONNRESET 15 /* 连接被重置 Connection reset.*/
|
|
||||||
|
|
||||||
#define EDEADLK 16 /* 资源死锁将要发生 Resource deadlock would occur.*/
|
|
||||||
|
|
||||||
#define EDESTADDRREQ 17 /* 需要目标地址 Destination address required.*/
|
|
||||||
|
|
||||||
#define EDOM 18 /* 数学参数超出作用域 Mathematics argument out of domain of function.*/
|
|
||||||
|
|
||||||
#define EDQUOT 19 /* 保留使用 Reserved*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define EEXIST 20 /* 文件已存在 File exists.*/
|
|
||||||
|
|
||||||
#define EFAULT 21 /* 错误的地址 Bad address*/
|
|
||||||
|
|
||||||
#define EFBIG 22 /* 文件太大 File too large.*/
|
|
||||||
|
|
||||||
#define EHOSTUNREACH 23 /* 主机不可达 Host is unreachable.*/
|
|
||||||
|
|
||||||
#define EIDRM 24 /* 标志符被移除 Identifier removed.*/
|
|
||||||
|
|
||||||
#define EILSEQ 25 /* 不合法的字符序列 Illegal byte sequence.*/
|
|
||||||
|
|
||||||
#define EINPROGRESS 26 /* 操作正在处理 Operation in progress.*/
|
|
||||||
|
|
||||||
#define EINTR 27 /* 被中断的函数 Interrupted function.*/
|
|
||||||
|
|
||||||
#define EINVAL 28 /* 不可用的参数 Invalid argument.*/
|
|
||||||
|
|
||||||
#define EIO 29 /* I/O错误 I/O error.*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define EISCONN 30 /* 套接字已连接 Socket is connected.*/
|
|
||||||
|
|
||||||
#define EISDIR 31 /* 是一个目录 Is a directory*/
|
|
||||||
|
|
||||||
#define ELOOP 32 /* 符号链接级别过多 Too many levels of symbolic links.*/
|
|
||||||
|
|
||||||
#define EMFILE 33 /* 文件描述符的值过大 File descriptor value too large.*/
|
|
||||||
|
|
||||||
#define EMLINK 34 /* 链接数过多 Too many links.*/
|
|
||||||
|
|
||||||
#define EMSGSIZE 35 /* 消息过大 Message too large.*/
|
|
||||||
|
|
||||||
#define EMULTIHOP 36 /* 保留使用 Reserved.*/
|
|
||||||
|
|
||||||
#define ENAMETOOLONG 37 /* 文件名过长 Filename too long.*/
|
|
||||||
|
|
||||||
#define ENETDOWN 38 /* 网络已关闭 Network is down.*/
|
|
||||||
|
|
||||||
#define ENETRESET 39 /* 网络连接已断开 Connection aborted by network.*/
|
|
||||||
|
|
||||||
#define ENETUNREACH 40 /* 网络不可达 Network unreachable.*/
|
|
||||||
|
|
||||||
#define ENFILE 41 /* 系统中打开的文件过多 Too many files open in system.*/
|
|
||||||
|
|
||||||
#define ENOBUFS 42 /* 缓冲区空间不足 No buffer space available.*/
|
|
||||||
|
|
||||||
#define ENODATA 43 /* 队列头没有可读取的消息 No message is available on the STREAM head read queue.*/
|
|
||||||
|
|
||||||
#define ENODEV 44 /* 没有指定的设备 No such device.*/
|
|
||||||
|
|
||||||
#define ENOENT 45 /* 没有指定的文件或目录 No such file or directory.*/
|
|
||||||
|
|
||||||
#define ENOEXEC 46 /* 可执行文件格式错误 Executable file format error.*/
|
|
||||||
|
|
||||||
#define ENOLCK 47 /* 没有可用的锁 No locks available.*/
|
|
||||||
|
|
||||||
#define ENOLINK 48 /* 保留 Reserved.*/
|
|
||||||
|
|
||||||
|
|
||||||
#define ENOMEM 49 /* 没有足够的空间 Not enough space.*/
|
|
||||||
|
|
||||||
|
|
||||||
#define ENOMSG 50 /* 没有期待类型的消息 No message of the desired type.*/
|
|
||||||
|
|
||||||
#define ENOPROTOOPT 51 /* 协议不可用 Protocol not available.*/
|
|
||||||
|
|
||||||
#define ENOSPC 52 /* 设备上没有空间 No space left on device.*/
|
|
||||||
|
|
||||||
#define ENOSR 53 /* 没有STREAM资源 No STREAM resources.*/
|
|
||||||
|
|
||||||
#define ENOSTR 54 /* 不是STREAM Not a STREAM*/
|
|
||||||
|
|
||||||
#define ENOSYS 55 /* 功能不支持 Function not supported.*/
|
|
||||||
|
|
||||||
#define ENOTCONN 56 /* 套接字未连接 The socket is not connected.*/
|
|
||||||
|
|
||||||
#define ENOTDIR 57 /* 不是目录 Not a directory.*/
|
|
||||||
|
|
||||||
#define ENOTEMPTY 58 /* 目录非空 Directory not empty.*/
|
|
||||||
|
|
||||||
#define ENOTRECOVERABLE 59 /* 状态不可覆盖 State not recoverable.*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define ENOTSOCK 60 /* 不是一个套接字 Not a socket.*/
|
|
||||||
|
|
||||||
#define ENOTSUP 61 /* 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).*/
|
|
||||||
|
|
||||||
#define ENOTTY 62 /* 不正确的I/O控制操作 Inappropriate I/O control operation.*/
|
|
||||||
|
|
||||||
#define ENXIO 63 /* 没有这样的设备或地址 No such device or address.*/
|
|
||||||
|
|
||||||
#define EOPNOTSUPP 64 /* 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).*/
|
|
||||||
|
|
||||||
#define EOVERFLOW 65 /* 数值过大,产生溢出 Value too large to be stored in data type.*/
|
|
||||||
|
|
||||||
#define EOWNERDEAD 66 /* 之前的拥有者挂了 Previous owner died.*/
|
|
||||||
|
|
||||||
#define EPERM 67 /* 操作不被允许 Operation not permitted.*/
|
|
||||||
|
|
||||||
#define EPIPE 68 /* 断开的管道 Broken pipe.*/
|
|
||||||
|
|
||||||
#define EPROTO 69 /* 协议错误 Protocol error.*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define EPROTONOSUPPORT 70 /* 协议不被支持 Protocol not supported.*/
|
|
||||||
|
|
||||||
#define EPROTOTYPE 71 /* 对于套接字而言,错误的协议 Protocol wrong type for socket.*/
|
|
||||||
|
|
||||||
#define ERANGE 72 /* 结果过大 Result too large.*/
|
|
||||||
|
|
||||||
#define EROFS 73 /* 只读的文件系统 Read-only file system.*/
|
|
||||||
|
|
||||||
#define ESPIPE 74 /* 错误的寻道 Invalid seek.*/
|
|
||||||
|
|
||||||
#define ESRCH 75 /* 没有这样的进程 No such process.*/
|
|
||||||
|
|
||||||
#define ESTALE 76 /* 保留 Reserved.*/
|
|
||||||
|
|
||||||
#define ETIME 77 /* 流式ioctl()超时 Stream ioctl() timeout*/
|
|
||||||
|
|
||||||
#define ETIMEDOUT 78 /* 连接超时 Connection timed out.*/
|
|
||||||
|
|
||||||
#define ETXTBSY 79 /* 文本文件忙 Text file busy.*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define EWOULDBLOCK 80 /* 操作将被禁止 Operation would block (may be the same value as [EAGAIN]).*/
|
|
||||||
|
|
||||||
#define EXDEV 81 /* 跨设备连接 Cross-device link.*/
|
|
@ -1,44 +0,0 @@
|
|||||||
# fcntl.h
|
|
||||||
|
|
||||||
## 简介
|
|
||||||
|
|
||||||
文件操作
|
|
||||||
## 函数列表:
|
|
||||||
|
|
||||||
``int open(const char * path,int options, ...)``
|
|
||||||
|
|
||||||
传入文件路径,和文件类型(详细请看下面的宏定义),将文件打开并返回文件id。
|
|
||||||
|
|
||||||
## 宏定义(粘贴自代码,了解即可):
|
|
||||||
|
|
||||||
#define O_RDONLY 00000000 // Open Read-only
|
|
||||||
|
|
||||||
#define O_WRONLY 00000001 // Open Write-only
|
|
||||||
|
|
||||||
#define O_RDWR 00000002 // Open read/write
|
|
||||||
|
|
||||||
#define O_ACCMODE 00000003 // Mask for file access modes
|
|
||||||
|
|
||||||
|
|
||||||
#define O_CREAT 00000100 // Create file if it does not exist
|
|
||||||
|
|
||||||
#define O_EXCL 00000200 // Fail if file already exists
|
|
||||||
|
|
||||||
#define O_NOCTTY 00000400 // Do not assign controlling terminal
|
|
||||||
|
|
||||||
#define O_TRUNC 00001000 // 文件存在且是普通文件,并以O_RDWR或O_WRONLY打开,则它会被清空
|
|
||||||
|
|
||||||
#define O_APPEND 00002000 // 文件指针会被移动到文件末尾
|
|
||||||
|
|
||||||
#define O_NONBLOCK 00004000 // 非阻塞式IO模式
|
|
||||||
|
|
||||||
|
|
||||||
#define O_EXEC 00010000 // 以仅执行的方式打开(非目录文件)
|
|
||||||
|
|
||||||
#define O_SEARCH 00020000 // Open the directory for search only
|
|
||||||
|
|
||||||
#define O_DIRECTORY 00040000 // 打开的必须是一个目录
|
|
||||||
|
|
||||||
#define O_NOFOLLOW 00100000 // Do not follow symbolic links
|
|
||||||
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
|||||||
# math.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
数学库
|
|
||||||
|
|
||||||
## 函数列表:
|
|
||||||
|
|
||||||
``double fabs(double x)`` : 返回 x 的绝对值
|
|
||||||
|
|
||||||
``float fabsf(float x)`` : 返回 x 的绝对值
|
|
||||||
|
|
||||||
``long double fabsl(long double x)``: 返回 x 的绝对值
|
|
||||||
|
|
||||||
``double round(double x)`` 四舍五入 x
|
|
||||||
|
|
||||||
``float roundf(float x)`` 四舍五入 x
|
|
||||||
|
|
||||||
``long double roundl(long double x)`` 四舍五入 x
|
|
||||||
|
|
||||||
``int64_t pow(int64_t x,int y)`` 返回 x 的 y 次方
|
|
@ -1,3 +0,0 @@
|
|||||||
# printf.h
|
|
||||||
|
|
||||||
不建议引用,需要 ``printf`` 函数请引用 ``stdio.h``
|
|
@ -1,11 +0,0 @@
|
|||||||
# stddef.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
定义了关于指针的常用类型
|
|
||||||
|
|
||||||
## 定义:
|
|
||||||
|
|
||||||
``typedef __PTDIFF_TYPE__ ptrdiff_t`` : 两个指针相减的结果类型
|
|
||||||
|
|
||||||
``NULL ((void *) 0)`` : 空指针
|
|
@ -1,52 +0,0 @@
|
|||||||
# stdio.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
向标准输入输出里操作
|
|
||||||
## 函数列表:
|
|
||||||
|
|
||||||
``int64_t put_string(char *str, uint64_t front_color, uint64_t bg_color)``
|
|
||||||
|
|
||||||
输出字符串(带有前景色,背景色)
|
|
||||||
|
|
||||||
``int printf(const char *fmt, ...)``
|
|
||||||
|
|
||||||
就是正常的 ``printf``
|
|
||||||
|
|
||||||
``int sprintf(char *buf,const char *fmt,...)```
|
|
||||||
|
|
||||||
就是正常的 ``sprintf``
|
|
||||||
|
|
||||||
``int vsprintf(char *buf,const char *fmt,va_list args)``
|
|
||||||
|
|
||||||
格式化,不建议调用,请用 printf 或 sprintf 替代。
|
|
||||||
## 宏定义
|
|
||||||
|
|
||||||
### 字体颜色的宏定义
|
|
||||||
|
|
||||||
``#define COLOR_WHITE 0x00ffffff //白``
|
|
||||||
|
|
||||||
``#define COLOR_BLACK 0x00000000 //黑``
|
|
||||||
|
|
||||||
``#define COLOR_RED 0x00ff0000 //红``
|
|
||||||
|
|
||||||
``#define COLOR_ORANGE 0x00ff8000 //橙``
|
|
||||||
|
|
||||||
``#define COLOR_YELLOW 0x00ffff00 //黄``
|
|
||||||
|
|
||||||
``#define COLOR_GREEN 0x0000ff00 //绿``
|
|
||||||
|
|
||||||
``#define COLOR_BLUE 0x000000ff //蓝``
|
|
||||||
|
|
||||||
``#define COLOR_INDIGO 0x0000ffff //靛``
|
|
||||||
|
|
||||||
``#define COLOR_PURPLE 0x008000ff //紫``
|
|
||||||
### 无需使用
|
|
||||||
|
|
||||||
``#define SEEK_SET 0 /* Seek relative to start-of-file */``
|
|
||||||
|
|
||||||
``#define SEEK_CUR 1 /* Seek relative to current position */``
|
|
||||||
|
|
||||||
``#define SEEK_END 2 /* Seek relative to end-of-file */``
|
|
||||||
|
|
||||||
``#define SEEK_MAX 3``
|
|
@ -1,20 +0,0 @@
|
|||||||
# stdlib.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
一些常用函数
|
|
||||||
## 函数列表:
|
|
||||||
|
|
||||||
``void *malloc(ssize_t size)`` : 普通的 ``malloc``
|
|
||||||
|
|
||||||
``void free(void *ptr)`` : 释放内存
|
|
||||||
|
|
||||||
``int abs(int x)`` : x 的绝对值
|
|
||||||
|
|
||||||
``long labs(long x)`` : x 的绝对值
|
|
||||||
|
|
||||||
``long long llabs(long long x)`` : x 的绝对值
|
|
||||||
|
|
||||||
``int atoi(const char *str)`` 字符串转数字
|
|
||||||
|
|
||||||
``void exit(int status)`` : 普通的 ``exit``
|
|
@ -1,38 +0,0 @@
|
|||||||
# string.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
字符串操作
|
|
||||||
|
|
||||||
## 函数列表:
|
|
||||||
|
|
||||||
``size_t strlen(const char *s)`` : 获取字符串的长度
|
|
||||||
|
|
||||||
``int strcmp(const char *a,const char *b)`` : 比较字符串的字典序
|
|
||||||
|
|
||||||
``char *strncpy(char *dst, const char *src, size_t count)``
|
|
||||||
|
|
||||||
拷贝制定字节数的字符串
|
|
||||||
dst: 目标地址
|
|
||||||
src: 原字符串
|
|
||||||
count: 字节数
|
|
||||||
|
|
||||||
``char *strcpy(char *dst,const char *src)`` : 复制整个字符串
|
|
||||||
|
|
||||||
``char *strcat(char *dest,const char* src)`` : 拼接两个字符串
|
|
||||||
|
|
||||||
``char *strtok(char *str, const char *delim)`` : 分割字符串
|
|
||||||
|
|
||||||
``char *strtok_r(char *str, const char *delim, char **saveptr)`` : 分割字符串
|
|
||||||
|
|
||||||
**以下函数没有经过检验,不确保正常工作**
|
|
||||||
|
|
||||||
``size_t strspn(const char *str1, const char *str2)`` : 检索字符串 str1 中第一个不在字符串 str2 中出现的字符下标
|
|
||||||
|
|
||||||
``size_t strcspn(const char *str1, const char *str2)`` : 检索字符串 str1 开头连续有几个字符都不含字符串 str2 中的字符
|
|
||||||
|
|
||||||
``char *strpbrk(const char *str1, const char *str2)`` : 检索字符串 str1 中第一个匹配字符串 str2 中字符的字符
|
|
||||||
|
|
||||||
``char *strchr(const char *str, int c)`` : 在字符串中查找第一次出现的字符
|
|
||||||
|
|
||||||
``char *strrchr(const char *str, int c)`` : 在字符串中查找最后一次出现的字符
|
|
@ -1,32 +0,0 @@
|
|||||||
# time.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
时间相关
|
|
||||||
|
|
||||||
时刻以纳秒为单位
|
|
||||||
|
|
||||||
## 结构体:
|
|
||||||
|
|
||||||
``struct timespec`` : 时间戳
|
|
||||||
|
|
||||||
### 变量列表:
|
|
||||||
|
|
||||||
``long int tv_sec`` : 秒
|
|
||||||
|
|
||||||
``long int tv_nsec`` : 纳秒
|
|
||||||
## 宏定义:
|
|
||||||
|
|
||||||
``#define CLOCKS_PER_SEC 1000000`` 每一秒有1000000个时刻(纳秒)
|
|
||||||
|
|
||||||
## 函数列表:
|
|
||||||
|
|
||||||
``int nanosleep(const struct timespec *rdtp,struct timespec *rmtp)``
|
|
||||||
|
|
||||||
休眠指定时间
|
|
||||||
|
|
||||||
rdtp : 指定休眠的时间
|
|
||||||
|
|
||||||
rmtp : 返回剩余时间
|
|
||||||
|
|
||||||
``clock_t clock()`` : 获得当前系统时间
|
|
@ -1,56 +0,0 @@
|
|||||||
# unistd.h
|
|
||||||
|
|
||||||
## 简介:
|
|
||||||
|
|
||||||
也是一些常用函数
|
|
||||||
|
|
||||||
## 函数列表:
|
|
||||||
|
|
||||||
``int close(int fd)`` : 关闭文件
|
|
||||||
|
|
||||||
``ssize_t read(int fd,void *buf,size_t count)`` : 从文件读取
|
|
||||||
|
|
||||||
传入文件id,缓冲区,以及字节数
|
|
||||||
|
|
||||||
返回成功读取的字节数
|
|
||||||
|
|
||||||
``ssize_t write(int fd,void const *buf,size_t count)`` : 写入文件
|
|
||||||
|
|
||||||
传入文件id,缓冲区,字节数
|
|
||||||
|
|
||||||
返回成功写入的字节数
|
|
||||||
|
|
||||||
``off_t lseek(int fd,off_t offset,int whence)`` : 调整文件访问位置
|
|
||||||
|
|
||||||
传入文件id,偏移量,调整模式
|
|
||||||
|
|
||||||
返回结束后的文件访问位置
|
|
||||||
|
|
||||||
``pid_t fork(void)`` : fork 当前进程
|
|
||||||
|
|
||||||
``pid_t vfork(void)`` : fork 当前进程,与父进程共享 VM,flags,fd
|
|
||||||
|
|
||||||
``uint64_t brk(uint64_t end_brk)`` : 将堆内存调整为end_brk
|
|
||||||
|
|
||||||
若end_brk 为 -1,返回堆区域的起始地址
|
|
||||||
|
|
||||||
若end_brk 为 -2,返回堆区域的结束地址
|
|
||||||
|
|
||||||
否则调整堆区的结束地址域,并返回错误码
|
|
||||||
|
|
||||||
``void *sbrk(int64_t increment)`` :
|
|
||||||
|
|
||||||
将堆内存空间加上offset(注意,该系统调用只应在普通进程中调用,而不能是内核线程)
|
|
||||||
|
|
||||||
increment : 偏移量
|
|
||||||
|
|
||||||
``int64_t chdir(char *dest_path)``
|
|
||||||
|
|
||||||
切换工作目录(传入目录路径)
|
|
||||||
|
|
||||||
``int execv(const char* path,char * const argv[])`` : 执行文件
|
|
||||||
path : 路径
|
|
||||||
argv : 执行参数列表
|
|
||||||
|
|
||||||
``extern int usleep(useconds_t usec)`` : 睡眠usec微秒
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
|||||||
API文档
|
|
||||||
====================================
|
|
||||||
.. toctree::
|
|
||||||
:maxdepth: 1
|
|
||||||
:caption: 目录
|
|
||||||
|
|
||||||
api-list/ctype
|
|
||||||
api-list/dirent
|
|
||||||
api-list/errno
|
|
||||||
api-list/fcntl
|
|
||||||
api-list/math
|
|
||||||
api-list/stdio
|
|
||||||
api-list/printf
|
|
||||||
api-list/stddef
|
|
||||||
api-list/stdlib
|
|
||||||
api-list/string
|
|
||||||
api-list/time
|
|
||||||
api-list/unistd
|
|
||||||
|
|
||||||
这里是所有libc头文件的集合,在代码里可以这样引用:
|
|
||||||
``#include<libc/src/xxx.h>``
|
|
@ -1,9 +0,0 @@
|
|||||||
设计文档
|
|
||||||
====================================
|
|
||||||
|
|
||||||
.. toctree::
|
|
||||||
:maxdepth: 1
|
|
||||||
:caption: 目录
|
|
||||||
|
|
||||||
|
|
||||||
[内容待完善]
|
|
@ -1,10 +0,0 @@
|
|||||||
LibC文档
|
|
||||||
====================================
|
|
||||||
|
|
||||||
.. toctree::
|
|
||||||
:maxdepth: 1
|
|
||||||
:caption: 目录
|
|
||||||
|
|
||||||
intro
|
|
||||||
apis/index
|
|
||||||
design/index
|
|
@ -1,4 +0,0 @@
|
|||||||
# 简介
|
|
||||||
|
|
||||||
LibC是连接用户程序和操作系统的纽带,LibC为应用程序提供了一系列标准库函数。应用程序可以通过DragonOS的LibC,快速地与操作系统进行交互。
|
|
||||||
DragonOS的LibC主要依照POSIX 2008规范实现,与Linux下的glibC具有相似之处。
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc16
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint16_t crc
|
|
||||||
*/
|
|
||||||
uint16_t crc16(uint16_t crc, const uint8_t *buffer, size_t len);
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc32
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint32_t crc
|
|
||||||
*/
|
|
||||||
uint32_t crc32(uint32_t crc, const uint8_t *buffer, size_t len);
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc64
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint64_t crc
|
|
||||||
*/
|
|
||||||
uint64_t crc64(uint64_t crc, const uint8_t *buffer, size_t len);
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc7
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint8_t crc
|
|
||||||
*/
|
|
||||||
uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len);
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc8
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint8_t crc
|
|
||||||
*/
|
|
||||||
uint8_t crc8(uint8_t crc, const uint8_t *buffer, size_t len);
|
|
@ -1,153 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <DragonOS/stdint.h>
|
|
||||||
#include <common/spinlock.h>
|
|
||||||
|
|
||||||
struct kfifo_t
|
|
||||||
{
|
|
||||||
uint32_t total_size; // 缓冲区总空间
|
|
||||||
uint32_t size; // 元素所占的字节数
|
|
||||||
uint32_t in_offset; // 入口偏移
|
|
||||||
uint32_t out_offset; // 出口偏移
|
|
||||||
void *buffer; // 缓冲区
|
|
||||||
} __attribute__((aligned(sizeof(long))));
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 忽略kfifo队列中的所有内容,并把输入和输出偏移量都归零
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#define kfifo_reset(fifo) (void)({ \
|
|
||||||
(fifo)->size = 0; \
|
|
||||||
(fifo)->in_offset = 0; \
|
|
||||||
(fifo)->out_offset = 0; \
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 忽略kfifo队列中的所有内容,并将输入偏移量赋值给输出偏移量
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#define kfifo_reset_out(fifo) (void)({ \
|
|
||||||
(fifo)->size = 0; \
|
|
||||||
(fifo)->out_offset = (fifo)->in_offset; \
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 获取kfifo缓冲区的最大大小
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @return uint32_t 缓冲区最大大小
|
|
||||||
*/
|
|
||||||
#define kfifo_total_size(fifo) ((fifo)->total_size)
|
|
||||||
/**
|
|
||||||
* @brief 获取kfifo缓冲区当前已使用的大小
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @return uint32_t 缓冲区当前已使用的大小
|
|
||||||
*/
|
|
||||||
#define kfifo_size(fifo) ((fifo)->size)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 判断kfifo缓冲区当前是否为空
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @return uint32_t 0->非空, 1->空
|
|
||||||
*/
|
|
||||||
#define kfifo_empty(fifo) (((fifo)->size == 0) ? 1 : 0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 判断kfifo缓冲区当前是否为满
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @return uint32_t 0->不满, 1->满
|
|
||||||
*/
|
|
||||||
#define kfifo_full(fifo) (((fifo)->size == (fifo)->total_size) ? 1 : 0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 通过动态方式初始化kfifo缓冲队列
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param size 缓冲区大小
|
|
||||||
* @param reserved 暂时保留,请置为0
|
|
||||||
* @return int 错误码:成功->0
|
|
||||||
*/
|
|
||||||
int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 释放通过kfifo_alloc创建的fifo缓冲区
|
|
||||||
*
|
|
||||||
* @param fifo fifo队列结构体
|
|
||||||
*/
|
|
||||||
void kfifo_free_alloc(struct kfifo_t *fifo);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 使用指定的缓冲区来初始化kfifo缓冲队列
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param buffer 缓冲区
|
|
||||||
* @param size 缓冲区大小
|
|
||||||
*/
|
|
||||||
void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 向kfifo缓冲区推入指定大小的数据
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param from 来源数据地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @return uint32_t 推入的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 从kfifo缓冲区取出数据,并从队列中删除数据
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param to 拷贝目标地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @return uint32_t 取出的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 从kfifo缓冲区取出数据,但是不从队列中删除数据
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param to 拷贝目标地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @return uint32_t 取出的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 向kfifo缓冲区推入指定大小的数据并在过程加锁
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param from 来源数据地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @param lock 自旋锁
|
|
||||||
* @return uint32_t 推入的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t __always_inline kfifo_in_locked(struct kfifo_t *fifo, const void *from, uint32_t size, spinlock_t *lock)
|
|
||||||
{
|
|
||||||
spin_lock(lock);
|
|
||||||
uint32_t retval = kfifo_in(fifo, from, size);
|
|
||||||
spin_unlock(lock);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 从kfifo缓冲区取出数据,并从队列中删除数据,并在过程加锁
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param to 拷贝目标地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @param lock 自旋锁
|
|
||||||
* @return uint32_t 取出的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t __always_inline kfifo_out_locked(struct kfifo_t *fifo, void *to, uint32_t size, spinlock_t *lock)
|
|
||||||
{
|
|
||||||
spin_lock(lock);
|
|
||||||
uint32_t retval = kfifo_out(fifo, to, size);
|
|
||||||
spin_unlock(lock);
|
|
||||||
return retval;
|
|
||||||
}
|
|
@ -1,104 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <common/sys/types.h>
|
|
||||||
#include <common/spinlock.h>
|
|
||||||
|
|
||||||
#if ARCH(X86_64)
|
|
||||||
// 仅在x64架构下启用cmpxchg
|
|
||||||
#define __LOCKREF_ENABLE_CMPXCHG__
|
|
||||||
#endif
|
|
||||||
struct lockref
|
|
||||||
{
|
|
||||||
union
|
|
||||||
{
|
|
||||||
#ifdef __LOCKREF_ENABLE_CMPXCHG__
|
|
||||||
aligned_u64 lock_count; // 通过该变量的声明,使得整个lockref按照8字节对齐
|
|
||||||
#endif
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
spinlock_t lock;
|
|
||||||
int count;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子的将引用计数加1
|
|
||||||
*
|
|
||||||
* @param lock_ref 要被操作的lockref变量
|
|
||||||
*/
|
|
||||||
void lockref_inc(struct lockref *lock_ref);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地将引用计数加1.如果原来的count≤0,则操作失败。
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return bool 操作成功=>true
|
|
||||||
* 操作失败=>false
|
|
||||||
*/
|
|
||||||
bool lockref_inc_not_zero(struct lockref *lock_ref);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。如果已处于count≤0的状态,则返回-1
|
|
||||||
*
|
|
||||||
* 本函数与lockref_dec_return()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会再次尝试通过加锁来执行操作
|
|
||||||
* 而后者会直接返回错误
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return int 操作成功 => 返回新的引用变量值
|
|
||||||
* lockref处于count≤0的状态 => 返回-1
|
|
||||||
*/
|
|
||||||
int lockref_dec(struct lockref *lock_ref);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。如果处于已加锁或count≤0的状态,则返回-1
|
|
||||||
*
|
|
||||||
* 本函数与lockref_dec()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会直接返回错误
|
|
||||||
* 而后者会再次尝试通过加锁来执行操作
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return int 操作成功 => 返回新的引用变量值
|
|
||||||
* lockref处于已加锁或count≤0的状态 => 返回-1
|
|
||||||
*/
|
|
||||||
int lockref_dec_return(struct lockref *lock_ref);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败
|
|
||||||
*
|
|
||||||
* 该函数与lockref_dec_or_lock_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会直接返回false.
|
|
||||||
* 而后者在这种情况下,会尝试加锁来进行操作。
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return true 成功将引用计数减1
|
|
||||||
* @return false 如果当前的引用计数≤1,操作失败
|
|
||||||
*/
|
|
||||||
bool lockref_dec_not_zero(struct lockref *lock_ref);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败
|
|
||||||
*
|
|
||||||
* 该函数与lockref_dec_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会尝试加锁来进行操作。
|
|
||||||
* 而后者在这种情况下,会直接返回false.
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return true 成功将引用计数减1
|
|
||||||
* @return false 如果当前的引用计数≤1,操作失败
|
|
||||||
*/
|
|
||||||
bool lockref_dec_or_lock_not_zero(struct lockref *lock_ref);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 将lockref变量标记为已经死亡(将count设置为负值)
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
*/
|
|
||||||
void lockref_mark_dead(struct lockref * lock_ref);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 自增引用计数。(除非该lockref已经死亡)
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return true 操作成功
|
|
||||||
* @return false 操作失败,lockref已死亡
|
|
||||||
*/
|
|
||||||
bool lockref_inc_not_dead(struct lockref *lock_ref);
|
|
@ -1,771 +0,0 @@
|
|||||||
/*
|
|
||||||
* LZ4 - Fast LZ compression algorithm
|
|
||||||
* Header File
|
|
||||||
* Copyright (C) 2011-present, Yann Collet.
|
|
||||||
|
|
||||||
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are
|
|
||||||
met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
* Redistributions in binary form must reproduce the above
|
|
||||||
copyright notice, this list of conditions and the following disclaimer
|
|
||||||
in the documentation and/or other materials provided with the
|
|
||||||
distribution.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
You can contact the author at :
|
|
||||||
- LZ4 homepage : http://www.lz4.org
|
|
||||||
- LZ4 source repository : https://github.com/lz4/lz4
|
|
||||||
*/
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef LZ4_H_2983827168210
|
|
||||||
#define LZ4_H_2983827168210
|
|
||||||
|
|
||||||
/* --- Dependency --- */
|
|
||||||
#include <common/stddef.h> /* size_t */
|
|
||||||
|
|
||||||
/**
|
|
||||||
Introduction
|
|
||||||
|
|
||||||
LZ4 is lossless compression algorithm, providing compression speed >500 MB/s per core,
|
|
||||||
scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
|
|
||||||
multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
|
|
||||||
|
|
||||||
The LZ4 compression library provides in-memory compression and decompression functions.
|
|
||||||
It gives full buffer control to user.
|
|
||||||
Compression can be done in:
|
|
||||||
- a single step (described as Simple Functions)
|
|
||||||
- a single step, reusing a context (described in Advanced Functions)
|
|
||||||
- unbounded multiple steps (described as Streaming compression)
|
|
||||||
|
|
||||||
lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md).
|
|
||||||
Decompressing such a compressed block requires additional metadata.
|
|
||||||
Exact metadata depends on exact decompression function.
|
|
||||||
For the typical case of LZ4_decompress_safe(),
|
|
||||||
metadata includes block's compressed size, and maximum bound of decompressed size.
|
|
||||||
Each application is free to encode and pass such metadata in whichever way it wants.
|
|
||||||
|
|
||||||
lz4.h only handle blocks, it can not generate Frames.
|
|
||||||
|
|
||||||
Blocks are different from Frames (doc/lz4_Frame_format.md).
|
|
||||||
Frames bundle both blocks and metadata in a specified manner.
|
|
||||||
Embedding metadata is required for compressed data to be self-contained and portable.
|
|
||||||
Frame format is delivered through a companion API, declared in lz4frame.h.
|
|
||||||
The `lz4` CLI can only manage frames.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*^***************************************************************
|
|
||||||
* Export parameters
|
|
||||||
*****************************************************************/
|
|
||||||
/*
|
|
||||||
* LZ4_DLL_EXPORT :
|
|
||||||
* Enable exporting of functions when building a Windows DLL
|
|
||||||
* LZ4LIB_VISIBILITY :
|
|
||||||
* Control library symbols visibility.
|
|
||||||
*/
|
|
||||||
#ifndef LZ4LIB_VISIBILITY
|
|
||||||
#if defined(__GNUC__) && (__GNUC__ >= 4)
|
|
||||||
#define LZ4LIB_VISIBILITY __attribute__((visibility("default")))
|
|
||||||
#else
|
|
||||||
#define LZ4LIB_VISIBILITY
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT == 1)
|
|
||||||
#define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY
|
|
||||||
#elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT == 1)
|
|
||||||
#define LZ4LIB_API __declspec(dllimport) LZ4LIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
|
|
||||||
#else
|
|
||||||
#define LZ4LIB_API LZ4LIB_VISIBILITY
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*------ Version ------*/
|
|
||||||
#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
|
|
||||||
#define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */
|
|
||||||
#define LZ4_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */
|
|
||||||
|
|
||||||
#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR * 100 * 100 + LZ4_VERSION_MINOR * 100 + LZ4_VERSION_RELEASE)
|
|
||||||
|
|
||||||
#define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
|
|
||||||
#define LZ4_QUOTE(str) #str
|
|
||||||
#define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
|
|
||||||
#define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION)
|
|
||||||
|
|
||||||
LZ4LIB_API int LZ4_versionNumber(void); /**< library version number; useful to check dll version */
|
|
||||||
LZ4LIB_API const char *LZ4_versionString(void); /**< library version string; useful to check dll version */
|
|
||||||
|
|
||||||
/*-************************************
|
|
||||||
* Tuning parameter
|
|
||||||
**************************************/
|
|
||||||
/*!
|
|
||||||
* LZ4_MEMORY_USAGE :
|
|
||||||
* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
|
|
||||||
* Increasing memory usage improves compression ratio.
|
|
||||||
* Reduced memory usage may improve speed, thanks to better cache locality.
|
|
||||||
* Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
|
|
||||||
*/
|
|
||||||
#ifndef LZ4_MEMORY_USAGE
|
|
||||||
#define LZ4_MEMORY_USAGE 14
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*-************************************
|
|
||||||
* Simple Functions
|
|
||||||
**************************************/
|
|
||||||
/*! LZ4_compress_default() :
|
|
||||||
* Compresses 'srcSize' bytes from buffer 'src'
|
|
||||||
* into already allocated 'dst' buffer of size 'dstCapacity'.
|
|
||||||
* Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize).
|
|
||||||
* It also runs faster, so it's a recommended setting.
|
|
||||||
* If the function cannot compress 'src' into a more limited 'dst' budget,
|
|
||||||
* compression stops *immediately*, and the function result is zero.
|
|
||||||
* In which case, 'dst' content is undefined (invalid).
|
|
||||||
* srcSize : max supported value is LZ4_MAX_INPUT_SIZE.
|
|
||||||
* dstCapacity : size of buffer 'dst' (which must be already allocated)
|
|
||||||
* @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity)
|
|
||||||
* or 0 if compression fails
|
|
||||||
* Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_compress_default(const char *src, char *dst, int srcSize, int dstCapacity);
|
|
||||||
|
|
||||||
/*! LZ4_decompress_safe() :
|
|
||||||
* compressedSize : is the exact complete size of the compressed block.
|
|
||||||
* dstCapacity : is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size.
|
|
||||||
* @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
|
|
||||||
* If destination buffer is not large enough, decoding will stop and output an error code (negative value).
|
|
||||||
* If the source stream is detected malformed, the function will stop decoding and return a negative result.
|
|
||||||
* Note 1 : This function is protected against malicious data packets :
|
|
||||||
* it will never writes outside 'dst' buffer, nor read outside 'source' buffer,
|
|
||||||
* even if the compressed block is maliciously modified to order the decoder to do these actions.
|
|
||||||
* In such case, the decoder stops immediately, and considers the compressed block malformed.
|
|
||||||
* Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them.
|
|
||||||
* The implementation is free to send / store / derive this information in whichever way is most beneficial.
|
|
||||||
* If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_decompress_safe(const char *src, char *dst, int compressedSize, int dstCapacity);
|
|
||||||
|
|
||||||
/*-************************************
|
|
||||||
* Advanced Functions
|
|
||||||
**************************************/
|
|
||||||
#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
|
|
||||||
#define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize) / 255) + 16)
|
|
||||||
|
|
||||||
/*! LZ4_compressBound() :
|
|
||||||
Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
|
|
||||||
This function is primarily useful for memory allocation purposes (destination buffer size).
|
|
||||||
Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
|
|
||||||
Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize)
|
|
||||||
inputSize : max supported value is LZ4_MAX_INPUT_SIZE
|
|
||||||
return : maximum output size in a "worst case" scenario
|
|
||||||
or 0, if input size is incorrect (too large or negative)
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_compressBound(int inputSize);
|
|
||||||
|
|
||||||
/*! LZ4_compress_fast() :
|
|
||||||
Same as LZ4_compress_default(), but allows selection of "acceleration" factor.
|
|
||||||
The larger the acceleration value, the faster the algorithm, but also the lesser the compression.
|
|
||||||
It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed.
|
|
||||||
An acceleration value of "1" is the same as regular LZ4_compress_default()
|
|
||||||
Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c).
|
|
||||||
Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_compress_fast(const char *src, char *dst, int srcSize, int dstCapacity, int acceleration);
|
|
||||||
|
|
||||||
/*! LZ4_compress_fast_extState() :
|
|
||||||
* Same as LZ4_compress_fast(), using an externally allocated memory space for its state.
|
|
||||||
* Use LZ4_sizeofState() to know how much memory must be allocated,
|
|
||||||
* and allocate it on 8-bytes boundaries (using `malloc()` typically).
|
|
||||||
* Then, provide this buffer as `void* state` to compression function.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_sizeofState(void);
|
|
||||||
LZ4LIB_API int LZ4_compress_fast_extState(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration);
|
|
||||||
|
|
||||||
/*! LZ4_compress_destSize() :
|
|
||||||
* Reverse the logic : compresses as much data as possible from 'src' buffer
|
|
||||||
* into already allocated buffer 'dst', of size >= 'targetDestSize'.
|
|
||||||
* This function either compresses the entire 'src' content into 'dst' if it's large enough,
|
|
||||||
* or fill 'dst' buffer completely with as much data as possible from 'src'.
|
|
||||||
* note: acceleration parameter is fixed to "default".
|
|
||||||
*
|
|
||||||
* *srcSizePtr : will be modified to indicate how many bytes where read from 'src' to fill 'dst'.
|
|
||||||
* New value is necessarily <= input value.
|
|
||||||
* @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
|
|
||||||
* or 0 if compression fails.
|
|
||||||
*
|
|
||||||
* Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+):
|
|
||||||
* the produced compressed content could, in specific circumstances,
|
|
||||||
* require to be decompressed into a destination buffer larger
|
|
||||||
* by at least 1 byte than the content to decompress.
|
|
||||||
* If an application uses `LZ4_compress_destSize()`,
|
|
||||||
* it's highly recommended to update liblz4 to v1.9.2 or better.
|
|
||||||
* If this can't be done or ensured,
|
|
||||||
* the receiving decompression function should provide
|
|
||||||
* a dstCapacity which is > decompressedSize, by at least 1 byte.
|
|
||||||
* See https://github.com/lz4/lz4/issues/859 for details
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize);
|
|
||||||
|
|
||||||
/*! LZ4_decompress_safe_partial() :
|
|
||||||
* Decompress an LZ4 compressed block, of size 'srcSize' at position 'src',
|
|
||||||
* into destination buffer 'dst' of size 'dstCapacity'.
|
|
||||||
* Up to 'targetOutputSize' bytes will be decoded.
|
|
||||||
* The function stops decoding on reaching this objective.
|
|
||||||
* This can be useful to boost performance
|
|
||||||
* whenever only the beginning of a block is required.
|
|
||||||
*
|
|
||||||
* @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize)
|
|
||||||
* If source stream is detected malformed, function returns a negative result.
|
|
||||||
*
|
|
||||||
* Note 1 : @return can be < targetOutputSize, if compressed block contains less data.
|
|
||||||
*
|
|
||||||
* Note 2 : targetOutputSize must be <= dstCapacity
|
|
||||||
*
|
|
||||||
* Note 3 : this function effectively stops decoding on reaching targetOutputSize,
|
|
||||||
* so dstCapacity is kind of redundant.
|
|
||||||
* This is because in older versions of this function,
|
|
||||||
* decoding operation would still write complete sequences.
|
|
||||||
* Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize,
|
|
||||||
* it could write more bytes, though only up to dstCapacity.
|
|
||||||
* Some "margin" used to be required for this operation to work properly.
|
|
||||||
* Thankfully, this is no longer necessary.
|
|
||||||
* The function nonetheless keeps the same signature, in an effort to preserve API compatibility.
|
|
||||||
*
|
|
||||||
* Note 4 : If srcSize is the exact size of the block,
|
|
||||||
* then targetOutputSize can be any value,
|
|
||||||
* including larger than the block's decompressed size.
|
|
||||||
* The function will, at most, generate block's decompressed size.
|
|
||||||
*
|
|
||||||
* Note 5 : If srcSize is _larger_ than block's compressed size,
|
|
||||||
* then targetOutputSize **MUST** be <= block's decompressed size.
|
|
||||||
* Otherwise, *silent corruption will occur*.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_decompress_safe_partial(const char *src, char *dst, int srcSize, int targetOutputSize, int dstCapacity);
|
|
||||||
|
|
||||||
/*-*********************************************
|
|
||||||
* Streaming Compression Functions
|
|
||||||
***********************************************/
|
|
||||||
typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
|
|
||||||
|
|
||||||
LZ4LIB_API LZ4_stream_t *LZ4_createStream(void);
|
|
||||||
LZ4LIB_API int LZ4_freeStream(LZ4_stream_t *streamPtr);
|
|
||||||
|
|
||||||
/*! LZ4_resetStream_fast() : v1.9.0+
|
|
||||||
* Use this to prepare an LZ4_stream_t for a new chain of dependent blocks
|
|
||||||
* (e.g., LZ4_compress_fast_continue()).
|
|
||||||
*
|
|
||||||
* An LZ4_stream_t must be initialized once before usage.
|
|
||||||
* This is automatically done when created by LZ4_createStream().
|
|
||||||
* However, should the LZ4_stream_t be simply declared on stack (for example),
|
|
||||||
* it's necessary to initialize it first, using LZ4_initStream().
|
|
||||||
*
|
|
||||||
* After init, start any new stream with LZ4_resetStream_fast().
|
|
||||||
* A same LZ4_stream_t can be re-used multiple times consecutively
|
|
||||||
* and compress multiple streams,
|
|
||||||
* provided that it starts each new stream with LZ4_resetStream_fast().
|
|
||||||
*
|
|
||||||
* LZ4_resetStream_fast() is much faster than LZ4_initStream(),
|
|
||||||
* but is not compatible with memory regions containing garbage data.
|
|
||||||
*
|
|
||||||
* Note: it's only useful to call LZ4_resetStream_fast()
|
|
||||||
* in the context of streaming compression.
|
|
||||||
* The *extState* functions perform their own resets.
|
|
||||||
* Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API void LZ4_resetStream_fast(LZ4_stream_t *streamPtr);
|
|
||||||
|
|
||||||
/*! LZ4_loadDict() :
|
|
||||||
* Use this function to reference a static dictionary into LZ4_stream_t.
|
|
||||||
* The dictionary must remain available during compression.
|
|
||||||
* LZ4_loadDict() triggers a reset, so any previous data will be forgotten.
|
|
||||||
* The same dictionary will have to be loaded on decompression side for successful decoding.
|
|
||||||
* Dictionary are useful for better compression of small data (KB range).
|
|
||||||
* While LZ4 accept any input as dictionary,
|
|
||||||
* results are generally better when using Zstandard's Dictionary Builder.
|
|
||||||
* Loading a size of 0 is allowed, and is the same as reset.
|
|
||||||
* @return : loaded dictionary size, in bytes (necessarily <= 64 KB)
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary, int dictSize);
|
|
||||||
|
|
||||||
/*! LZ4_compress_fast_continue() :
|
|
||||||
* Compress 'src' content using data from previously compressed blocks, for better compression ratio.
|
|
||||||
* 'dst' buffer must be already allocated.
|
|
||||||
* If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster.
|
|
||||||
*
|
|
||||||
* @return : size of compressed block
|
|
||||||
* or 0 if there is an error (typically, cannot fit into 'dst').
|
|
||||||
*
|
|
||||||
* Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block.
|
|
||||||
* Each block has precise boundaries.
|
|
||||||
* Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata.
|
|
||||||
* It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together.
|
|
||||||
*
|
|
||||||
* Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory !
|
|
||||||
*
|
|
||||||
* Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB.
|
|
||||||
* Make sure that buffers are separated, by at least one byte.
|
|
||||||
* This construction ensures that each block only depends on previous block.
|
|
||||||
*
|
|
||||||
* Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB.
|
|
||||||
*
|
|
||||||
* Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration);
|
|
||||||
|
|
||||||
/*! LZ4_saveDict() :
|
|
||||||
* If last 64KB data cannot be guaranteed to remain available at its current memory location,
|
|
||||||
* save it into a safer place (char* safeBuffer).
|
|
||||||
* This is schematically equivalent to a memcpy() followed by LZ4_loadDict(),
|
|
||||||
* but is much faster, because LZ4_saveDict() doesn't need to rebuild tables.
|
|
||||||
* @return : saved dictionary size in bytes (necessarily <= maxDictSize), or 0 if error.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int maxDictSize);
|
|
||||||
|
|
||||||
/*-**********************************************
|
|
||||||
* Streaming Decompression Functions
|
|
||||||
* Bufferless synchronous API
|
|
||||||
************************************************/
|
|
||||||
typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */
|
|
||||||
|
|
||||||
/*! LZ4_createStreamDecode() and LZ4_freeStreamDecode() :
|
|
||||||
* creation / destruction of streaming decompression tracking context.
|
|
||||||
* A tracking context can be re-used multiple times.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API LZ4_streamDecode_t *LZ4_createStreamDecode(void);
|
|
||||||
LZ4LIB_API int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream);
|
|
||||||
|
|
||||||
/*! LZ4_setStreamDecode() :
|
|
||||||
* An LZ4_streamDecode_t context can be allocated once and re-used multiple times.
|
|
||||||
* Use this function to start decompression of a new stream of blocks.
|
|
||||||
* A dictionary can optionally be set. Use NULL or size 0 for a reset order.
|
|
||||||
* Dictionary is presumed stable : it must remain accessible and unmodified during next decompression.
|
|
||||||
* @return : 1 if OK, 0 if error
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize);
|
|
||||||
|
|
||||||
/*! LZ4_decoderRingBufferSize() : v1.8.2+
|
|
||||||
* Note : in a ring buffer scenario (optional),
|
|
||||||
* blocks are presumed decompressed next to each other
|
|
||||||
* up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize),
|
|
||||||
* at which stage it resumes from beginning of ring buffer.
|
|
||||||
* When setting such a ring buffer for streaming decompression,
|
|
||||||
* provides the minimum size of this ring buffer
|
|
||||||
* to be compatible with any source respecting maxBlockSize condition.
|
|
||||||
* @return : minimum ring buffer size,
|
|
||||||
* or 0 if there is an error (invalid maxBlockSize).
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
|
|
||||||
#define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */
|
|
||||||
|
|
||||||
/*! LZ4_decompress_*_continue() :
|
|
||||||
* These decoding functions allow decompression of consecutive blocks in "streaming" mode.
|
|
||||||
* A block is an unsplittable entity, it must be presented entirely to a decompression function.
|
|
||||||
* Decompression functions only accepts one block at a time.
|
|
||||||
* The last 64KB of previously decoded data *must* remain available and unmodified at the memory position where they were decoded.
|
|
||||||
* If less than 64KB of data has been decoded, all the data must be present.
|
|
||||||
*
|
|
||||||
* Special : if decompression side sets a ring buffer, it must respect one of the following conditions :
|
|
||||||
* - Decompression buffer size is _at least_ LZ4_decoderRingBufferSize(maxBlockSize).
|
|
||||||
* maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes.
|
|
||||||
* In which case, encoding and decoding buffers do not need to be synchronized.
|
|
||||||
* Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize.
|
|
||||||
* - Synchronized mode :
|
|
||||||
* Decompression buffer size is _exactly_ the same as compression buffer size,
|
|
||||||
* and follows exactly same update rule (block boundaries at same positions),
|
|
||||||
* and decoding function is provided with exact decompressed size of each block (exception for last block of the stream),
|
|
||||||
* _then_ decoding & encoding ring buffer can have any size, including small ones ( < 64 KB).
|
|
||||||
* - Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes.
|
|
||||||
* In which case, encoding and decoding buffers do not need to be synchronized,
|
|
||||||
* and encoding ring buffer can have any size, including small ones ( < 64 KB).
|
|
||||||
*
|
|
||||||
* Whenever these conditions are not possible,
|
|
||||||
* save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression,
|
|
||||||
* then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int srcSize, int dstCapacity);
|
|
||||||
|
|
||||||
/*! LZ4_decompress_*_usingDict() :
|
|
||||||
* These decoding functions work the same as
|
|
||||||
* a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue()
|
|
||||||
* They are stand-alone, and don't need an LZ4_streamDecode_t structure.
|
|
||||||
* Dictionary is presumed stable : it must remain accessible and unmodified during decompression.
|
|
||||||
* Performance tip : Decompression speed can be substantially increased
|
|
||||||
* when dst == dictStart + dictSize.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API int LZ4_decompress_safe_usingDict(const char *src, char *dst, int srcSize, int dstCapcity, const char *dictStart, int dictSize);
|
|
||||||
|
|
||||||
#endif /* LZ4_H_2983827168210 */
|
|
||||||
|
|
||||||
/*^*************************************
|
|
||||||
* !!!!!! STATIC LINKING ONLY !!!!!!
|
|
||||||
***************************************/
|
|
||||||
|
|
||||||
/*-****************************************************************************
|
|
||||||
* Experimental section
|
|
||||||
*
|
|
||||||
* Symbols declared in this section must be considered unstable. Their
|
|
||||||
* signatures or semantics may change, or they may be removed altogether in the
|
|
||||||
* future. They are therefore only safe to depend on when the caller is
|
|
||||||
* statically linked against the library.
|
|
||||||
*
|
|
||||||
* To protect against unsafe usage, not only are the declarations guarded,
|
|
||||||
* the definitions are hidden by default
|
|
||||||
* when building LZ4 as a shared/dynamic library.
|
|
||||||
*
|
|
||||||
* In order to access these declarations,
|
|
||||||
* define LZ4_STATIC_LINKING_ONLY in your application
|
|
||||||
* before including LZ4's headers.
|
|
||||||
*
|
|
||||||
* In order to make their implementations accessible dynamically, you must
|
|
||||||
* define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#ifdef LZ4_STATIC_LINKING_ONLY
|
|
||||||
|
|
||||||
#ifndef LZ4_STATIC_3504398509
|
|
||||||
#define LZ4_STATIC_3504398509
|
|
||||||
|
|
||||||
#ifdef LZ4_PUBLISH_STATIC_FUNCTIONS
|
|
||||||
#define LZ4LIB_STATIC_API LZ4LIB_API
|
|
||||||
#else
|
|
||||||
#define LZ4LIB_STATIC_API
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*! LZ4_compress_fast_extState_fastReset() :
|
|
||||||
* A variant of LZ4_compress_fast_extState().
|
|
||||||
*
|
|
||||||
* Using this variant avoids an expensive initialization step.
|
|
||||||
* It is only safe to call if the state buffer is known to be correctly initialized already
|
|
||||||
* (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized").
|
|
||||||
* From a high level, the difference is that
|
|
||||||
* this function initializes the provided state with a call to something like LZ4_resetStream_fast()
|
|
||||||
* while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream().
|
|
||||||
*/
|
|
||||||
LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset(void *state, const char *src, char *dst, int srcSize, int dstCapacity, int acceleration);
|
|
||||||
|
|
||||||
/*! LZ4_attach_dictionary() :
|
|
||||||
* This is an experimental API that allows
|
|
||||||
* efficient use of a static dictionary many times.
|
|
||||||
*
|
|
||||||
* Rather than re-loading the dictionary buffer into a working context before
|
|
||||||
* each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a
|
|
||||||
* working LZ4_stream_t, this function introduces a no-copy setup mechanism,
|
|
||||||
* in which the working stream references the dictionary stream in-place.
|
|
||||||
*
|
|
||||||
* Several assumptions are made about the state of the dictionary stream.
|
|
||||||
* Currently, only streams which have been prepared by LZ4_loadDict() should
|
|
||||||
* be expected to work.
|
|
||||||
*
|
|
||||||
* Alternatively, the provided dictionaryStream may be NULL,
|
|
||||||
* in which case any existing dictionary stream is unset.
|
|
||||||
*
|
|
||||||
* If a dictionary is provided, it replaces any pre-existing stream history.
|
|
||||||
* The dictionary contents are the only history that can be referenced and
|
|
||||||
* logically immediately precede the data compressed in the first subsequent
|
|
||||||
* compression call.
|
|
||||||
*
|
|
||||||
* The dictionary will only remain attached to the working stream through the
|
|
||||||
* first compression call, at the end of which it is cleared. The dictionary
|
|
||||||
* stream (and source buffer) must remain in-place / accessible / unchanged
|
|
||||||
* through the completion of the first compression call on the stream.
|
|
||||||
*/
|
|
||||||
LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t *workingStream, const LZ4_stream_t *dictionaryStream);
|
|
||||||
|
|
||||||
/*! In-place compression and decompression
|
|
||||||
*
|
|
||||||
* It's possible to have input and output sharing the same buffer,
|
|
||||||
* for highly contrained memory environments.
|
|
||||||
* In both cases, it requires input to lay at the end of the buffer,
|
|
||||||
* and decompression to start at beginning of the buffer.
|
|
||||||
* Buffer size must feature some margin, hence be larger than final size.
|
|
||||||
*
|
|
||||||
* |<------------------------buffer--------------------------------->|
|
|
||||||
* |<-----------compressed data--------->|
|
|
||||||
* |<-----------decompressed size------------------>|
|
|
||||||
* |<----margin---->|
|
|
||||||
*
|
|
||||||
* This technique is more useful for decompression,
|
|
||||||
* since decompressed size is typically larger,
|
|
||||||
* and margin is short.
|
|
||||||
*
|
|
||||||
* In-place decompression will work inside any buffer
|
|
||||||
* which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize).
|
|
||||||
* This presumes that decompressedSize > compressedSize.
|
|
||||||
* Otherwise, it means compression actually expanded data,
|
|
||||||
* and it would be more efficient to store such data with a flag indicating it's not compressed.
|
|
||||||
* This can happen when data is not compressible (already compressed, or encrypted).
|
|
||||||
*
|
|
||||||
* For in-place compression, margin is larger, as it must be able to cope with both
|
|
||||||
* history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX,
|
|
||||||
* and data expansion, which can happen when input is not compressible.
|
|
||||||
* As a consequence, buffer size requirements are much higher,
|
|
||||||
* and memory savings offered by in-place compression are more limited.
|
|
||||||
*
|
|
||||||
* There are ways to limit this cost for compression :
|
|
||||||
* - Reduce history size, by modifying LZ4_DISTANCE_MAX.
|
|
||||||
* Note that it is a compile-time constant, so all compressions will apply this limit.
|
|
||||||
* Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX,
|
|
||||||
* so it's a reasonable trick when inputs are known to be small.
|
|
||||||
* - Require the compressor to deliver a "maximum compressed size".
|
|
||||||
* This is the `dstCapacity` parameter in `LZ4_compress*()`.
|
|
||||||
* When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail,
|
|
||||||
* in which case, the return code will be 0 (zero).
|
|
||||||
* The caller must be ready for these cases to happen,
|
|
||||||
* and typically design a backup scheme to send data uncompressed.
|
|
||||||
* The combination of both techniques can significantly reduce
|
|
||||||
* the amount of margin required for in-place compression.
|
|
||||||
*
|
|
||||||
* In-place compression can work in any buffer
|
|
||||||
* which size is >= (maxCompressedSize)
|
|
||||||
* with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success.
|
|
||||||
* LZ4_COMPRESS_INPLACE_BUFFER_SIZE() depends on both maxCompressedSize and LZ4_DISTANCE_MAX,
|
|
||||||
* so it's possible to reduce memory requirements by playing with them.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LZ4_DECOMPRESS_INPLACE_MARGIN(compressedSize) (((compressedSize) >> 8) + 32)
|
|
||||||
#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize)) /**< note: presumes that compressedSize < decompressedSize. note2: margin is overestimated a bit, since it could use compressedSize instead */
|
|
||||||
|
|
||||||
#ifndef LZ4_DISTANCE_MAX /* history window size; can be user-defined at compile time */
|
|
||||||
#define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32) /* LZ4_DISTANCE_MAX can be safely replaced by srcSize when it's smaller */
|
|
||||||
#define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN) /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
|
|
||||||
|
|
||||||
#endif /* LZ4_STATIC_3504398509 */
|
|
||||||
#endif /* LZ4_STATIC_LINKING_ONLY */
|
|
||||||
|
|
||||||
#ifndef LZ4_H_98237428734687
|
|
||||||
#define LZ4_H_98237428734687
|
|
||||||
|
|
||||||
/*-************************************************************
|
|
||||||
* Private Definitions
|
|
||||||
**************************************************************
|
|
||||||
* Do not use these definitions directly.
|
|
||||||
* They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
|
|
||||||
* Accessing members will expose user code to API and/or ABI break in future versions of the library.
|
|
||||||
**************************************************************/
|
|
||||||
#define LZ4_HASHLOG (LZ4_MEMORY_USAGE - 2)
|
|
||||||
#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
|
|
||||||
#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
|
|
||||||
|
|
||||||
#if defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
|
|
||||||
#include <DragonOS/stdint.h>
|
|
||||||
typedef int8_t LZ4_i8;
|
|
||||||
typedef uint8_t LZ4_byte;
|
|
||||||
typedef uint16_t LZ4_u16;
|
|
||||||
typedef uint32_t LZ4_u32;
|
|
||||||
#else
|
|
||||||
typedef signed char LZ4_i8;
|
|
||||||
typedef unsigned char LZ4_byte;
|
|
||||||
typedef unsigned short LZ4_u16;
|
|
||||||
typedef unsigned int LZ4_u32;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
|
|
||||||
struct LZ4_stream_t_internal
|
|
||||||
{
|
|
||||||
LZ4_u32 hashTable[LZ4_HASH_SIZE_U32];
|
|
||||||
LZ4_u32 currentOffset;
|
|
||||||
LZ4_u32 tableType;
|
|
||||||
const LZ4_byte *dictionary;
|
|
||||||
const LZ4_stream_t_internal *dictCtx;
|
|
||||||
LZ4_u32 dictSize;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
const LZ4_byte *externalDict;
|
|
||||||
size_t extDictSize;
|
|
||||||
const LZ4_byte *prefixEnd;
|
|
||||||
size_t prefixSize;
|
|
||||||
} LZ4_streamDecode_t_internal;
|
|
||||||
|
|
||||||
/*! LZ4_stream_t :
|
|
||||||
* Do not use below internal definitions directly !
|
|
||||||
* Declare or allocate an LZ4_stream_t instead.
|
|
||||||
* LZ4_stream_t can also be created using LZ4_createStream(), which is recommended.
|
|
||||||
* The structure definition can be convenient for static allocation
|
|
||||||
* (on stack, or as part of larger structure).
|
|
||||||
* Init this structure with LZ4_initStream() before first use.
|
|
||||||
* note : only use this definition in association with static linking !
|
|
||||||
* this definition is not API/ABI safe, and may change in future versions.
|
|
||||||
*/
|
|
||||||
#define LZ4_STREAMSIZE 16416 /* static size, for inter-version compatibility */
|
|
||||||
#define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void *))
|
|
||||||
union LZ4_stream_u
|
|
||||||
{
|
|
||||||
void *table[LZ4_STREAMSIZE_VOIDP];
|
|
||||||
LZ4_stream_t_internal internal_donotuse;
|
|
||||||
}; /* previously typedef'd to LZ4_stream_t */
|
|
||||||
|
|
||||||
/*! LZ4_initStream() : v1.9.0+
|
|
||||||
* An LZ4_stream_t structure must be initialized at least once.
|
|
||||||
* This is automatically done when invoking LZ4_createStream(),
|
|
||||||
* but it's not when the structure is simply declared on stack (for example).
|
|
||||||
*
|
|
||||||
* Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t.
|
|
||||||
* It can also initialize any arbitrary buffer of sufficient size,
|
|
||||||
* and will @return a pointer of proper type upon initialization.
|
|
||||||
*
|
|
||||||
* Note : initialization fails if size and alignment conditions are not respected.
|
|
||||||
* In which case, the function will @return NULL.
|
|
||||||
* Note2: An LZ4_stream_t structure guarantees correct alignment and size.
|
|
||||||
* Note3: Before v1.9.0, use LZ4_resetStream() instead
|
|
||||||
*/
|
|
||||||
LZ4LIB_API LZ4_stream_t *LZ4_initStream(void *buffer, size_t size);
|
|
||||||
|
|
||||||
/*! LZ4_streamDecode_t :
|
|
||||||
* information structure to track an LZ4 stream during decompression.
|
|
||||||
* init this structure using LZ4_setStreamDecode() before first use.
|
|
||||||
* note : only use in association with static linking !
|
|
||||||
* this definition is not API/ABI safe,
|
|
||||||
* and may change in a future version !
|
|
||||||
*/
|
|
||||||
#define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void *) == 16) ? 2 : 0) /*AS-400*/)
|
|
||||||
#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
|
|
||||||
union LZ4_streamDecode_u
|
|
||||||
{
|
|
||||||
unsigned long long table[LZ4_STREAMDECODESIZE_U64];
|
|
||||||
LZ4_streamDecode_t_internal internal_donotuse;
|
|
||||||
}; /* previously typedef'd to LZ4_streamDecode_t */
|
|
||||||
|
|
||||||
/*-************************************
|
|
||||||
* Obsolete Functions
|
|
||||||
**************************************/
|
|
||||||
|
|
||||||
/*! Deprecation warnings
|
|
||||||
*
|
|
||||||
* Deprecated functions make the compiler generate a warning when invoked.
|
|
||||||
* This is meant to invite users to update their source code.
|
|
||||||
* Should deprecation warnings be a problem, it is generally possible to disable them,
|
|
||||||
* typically with -Wno-deprecated-declarations for gcc
|
|
||||||
* or _CRT_SECURE_NO_WARNINGS in Visual.
|
|
||||||
*
|
|
||||||
* Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS
|
|
||||||
* before including the header file.
|
|
||||||
*/
|
|
||||||
#ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
|
|
||||||
#define LZ4_DEPRECATED(message) /* disable deprecation warnings */
|
|
||||||
#else
|
|
||||||
#if defined(__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
|
|
||||||
#define LZ4_DEPRECATED(message) [[deprecated(message)]]
|
|
||||||
#elif defined(_MSC_VER)
|
|
||||||
#define LZ4_DEPRECATED(message) __declspec(deprecated(message))
|
|
||||||
#elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45))
|
|
||||||
#define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
|
|
||||||
#elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31)
|
|
||||||
#define LZ4_DEPRECATED(message) __attribute__((deprecated))
|
|
||||||
#else
|
|
||||||
#pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler")
|
|
||||||
#define LZ4_DEPRECATED(message) /* disabled */
|
|
||||||
#endif
|
|
||||||
#endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
|
|
||||||
|
|
||||||
/*! Obsolete compression functions (since v1.7.3) */
|
|
||||||
LZ4_DEPRECATED("use LZ4_compress_default() instead")
|
|
||||||
LZ4LIB_API int LZ4_compress(const char *src, char *dest, int srcSize);
|
|
||||||
LZ4_DEPRECATED("use LZ4_compress_default() instead")
|
|
||||||
LZ4LIB_API int LZ4_compress_limitedOutput(const char *src, char *dest, int srcSize, int maxOutputSize);
|
|
||||||
LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead")
|
|
||||||
LZ4LIB_API int LZ4_compress_withState(void *state, const char *source, char *dest, int inputSize);
|
|
||||||
LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead")
|
|
||||||
LZ4LIB_API int LZ4_compress_limitedOutput_withState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize);
|
|
||||||
LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead")
|
|
||||||
LZ4LIB_API int LZ4_compress_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize);
|
|
||||||
LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead")
|
|
||||||
LZ4LIB_API int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize, int maxOutputSize);
|
|
||||||
|
|
||||||
/*! Obsolete decompression functions (since v1.8.0) */
|
|
||||||
LZ4_DEPRECATED("use LZ4_decompress_fast() instead")
|
|
||||||
LZ4LIB_API int LZ4_uncompress(const char *source, char *dest, int outputSize);
|
|
||||||
LZ4_DEPRECATED("use LZ4_decompress_safe() instead")
|
|
||||||
LZ4LIB_API int LZ4_uncompress_unknownOutputSize(const char *source, char *dest, int isize, int maxOutputSize);
|
|
||||||
|
|
||||||
/* Obsolete streaming functions (since v1.7.0)
|
|
||||||
* degraded functionality; do not use!
|
|
||||||
*
|
|
||||||
* In order to perform streaming compression, these functions depended on data
|
|
||||||
* that is no longer tracked in the state. They have been preserved as well as
|
|
||||||
* possible: using them will still produce a correct output. However, they don't
|
|
||||||
* actually retain any history between compression calls. The compression ratio
|
|
||||||
* achieved will therefore be no better than compressing each chunk
|
|
||||||
* independently.
|
|
||||||
*/
|
|
||||||
LZ4_DEPRECATED("Use LZ4_createStream() instead")
|
|
||||||
LZ4LIB_API void *LZ4_create(char *inputBuffer);
|
|
||||||
LZ4_DEPRECATED("Use LZ4_createStream() instead")
|
|
||||||
LZ4LIB_API int LZ4_sizeofStreamState(void);
|
|
||||||
LZ4_DEPRECATED("Use LZ4_resetStream() instead")
|
|
||||||
LZ4LIB_API int LZ4_resetStreamState(void *state, char *inputBuffer);
|
|
||||||
LZ4_DEPRECATED("Use LZ4_saveDict() instead")
|
|
||||||
LZ4LIB_API char *LZ4_slideInputBuffer(void *state);
|
|
||||||
|
|
||||||
/*! Obsolete streaming decoding functions (since v1.7.0) */
|
|
||||||
LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead")
|
|
||||||
LZ4LIB_API int LZ4_decompress_safe_withPrefix64k(const char *src, char *dst, int compressedSize, int maxDstSize);
|
|
||||||
LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead")
|
|
||||||
LZ4LIB_API int LZ4_decompress_fast_withPrefix64k(const char *src, char *dst, int originalSize);
|
|
||||||
|
|
||||||
/*! Obsolete LZ4_decompress_fast variants (since v1.9.0) :
|
|
||||||
* These functions used to be faster than LZ4_decompress_safe(),
|
|
||||||
* but this is no longer the case. They are now slower.
|
|
||||||
* This is because LZ4_decompress_fast() doesn't know the input size,
|
|
||||||
* and therefore must progress more cautiously into the input buffer to not read beyond the end of block.
|
|
||||||
* On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability.
|
|
||||||
* As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated.
|
|
||||||
*
|
|
||||||
* The last remaining LZ4_decompress_fast() specificity is that
|
|
||||||
* it can decompress a block without knowing its compressed size.
|
|
||||||
* Such functionality can be achieved in a more secure manner
|
|
||||||
* by employing LZ4_decompress_safe_partial().
|
|
||||||
*
|
|
||||||
* Parameters:
|
|
||||||
* originalSize : is the uncompressed size to regenerate.
|
|
||||||
* `dst` must be already allocated, its size must be >= 'originalSize' bytes.
|
|
||||||
* @return : number of bytes read from source buffer (== compressed size).
|
|
||||||
* The function expects to finish at block's end exactly.
|
|
||||||
* If the source stream is detected malformed, the function stops decoding and returns a negative result.
|
|
||||||
* note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer.
|
|
||||||
* However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds.
|
|
||||||
* Also, since match offsets are not validated, match reads from 'src' may underflow too.
|
|
||||||
* These issues never happen if input (compressed) data is correct.
|
|
||||||
* But they may happen if input data is invalid (error or intentional tampering).
|
|
||||||
* As a consequence, use these functions in trusted environments with trusted data **only**.
|
|
||||||
*/
|
|
||||||
LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead")
|
|
||||||
LZ4LIB_API int LZ4_decompress_fast(const char *src, char *dst, int originalSize);
|
|
||||||
LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead")
|
|
||||||
LZ4LIB_API int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *src, char *dst, int originalSize);
|
|
||||||
LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead")
|
|
||||||
LZ4LIB_API int LZ4_decompress_fast_usingDict(const char *src, char *dst, int originalSize, const char *dictStart, int dictSize);
|
|
||||||
|
|
||||||
/*! LZ4_resetStream() :
|
|
||||||
* An LZ4_stream_t structure must be initialized at least once.
|
|
||||||
* This is done with LZ4_initStream(), or LZ4_resetStream().
|
|
||||||
* Consider switching to LZ4_initStream(),
|
|
||||||
* invoking LZ4_resetStream() will trigger deprecation warnings in the future.
|
|
||||||
*/
|
|
||||||
LZ4LIB_API void LZ4_resetStream(LZ4_stream_t *streamPtr);
|
|
||||||
|
|
||||||
#endif /* LZ4_H_98237428734687 */
|
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -1,30 +0,0 @@
|
|||||||
#include <common/math.h>
|
|
||||||
#include <common/sys/types.h>
|
|
||||||
#include "libm.h"
|
|
||||||
|
|
||||||
double fabs(double x)
|
|
||||||
{
|
|
||||||
union
|
|
||||||
{
|
|
||||||
double f;
|
|
||||||
uint64_t i;
|
|
||||||
} u = {x};
|
|
||||||
u.i &= -1ULL / 2;
|
|
||||||
return u.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#if __LDBL_MANT_DIG__ == 53 && __LDBL_MAX_EXP__ == 1024
|
|
||||||
long double fabsl(long double x)
|
|
||||||
{
|
|
||||||
return fabs(x);
|
|
||||||
}
|
|
||||||
#elif (__LDBL_MANT_DIG__ == 64 || __LDBL_MANT_DIG__ == 113) && __LDBL_MAX_EXP__ == 16384
|
|
||||||
long double fabsl(long double x)
|
|
||||||
{
|
|
||||||
union ldshape u = {x};
|
|
||||||
|
|
||||||
u.i.se &= 0x7fff;
|
|
||||||
return u.f;
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -1,3 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#define NUMA_NO_NODE (-1)
|
|
@ -1,9 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 将长整型转换为字符串
|
|
||||||
*
|
|
||||||
* @param input 输入的数据
|
|
||||||
* @return const char* 结果字符串
|
|
||||||
*/
|
|
||||||
const char* ltoa(long input);
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 等待指定pid的子进程退出
|
|
||||||
*
|
|
||||||
* @param pid 子进程的pid
|
|
||||||
* @param stat_loc 返回的子进程结束状态
|
|
||||||
* @param options 额外的控制选项
|
|
||||||
* @return pid_t
|
|
||||||
*/
|
|
||||||
pid_t waitpid(pid_t pid, int *stat_loc, int options);
|
|
@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#if ARCH(I386) || ARCH(X86_64)
|
|
||||||
|
|
||||||
#include <common/atomic.h>
|
|
||||||
|
|
||||||
// 该结构体需要与libs/refcount.rs的保持一致,且以rust版本为准
|
|
||||||
typedef struct refcount_struct {
|
|
||||||
atomic_t refs;
|
|
||||||
} refcount_t;
|
|
||||||
|
|
||||||
#endif
|
|
@ -11,14 +11,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
#include <common/crc16.h>
|
|
||||||
#include <common/crc32.h>
|
|
||||||
#include <common/crc64.h>
|
|
||||||
#include <common/crc7.h>
|
|
||||||
#include <common/crc8.h>
|
|
||||||
#include <common/glib.h>
|
#include <common/glib.h>
|
||||||
#include <common/kfifo.h>
|
|
||||||
#include <common/lz4.h>
|
|
||||||
#include <common/printk.h>
|
#include <common/printk.h>
|
||||||
#include <common/spinlock.h>
|
#include <common/spinlock.h>
|
||||||
#include <common/stdio.h>
|
#include <common/stdio.h>
|
||||||
@ -26,7 +19,6 @@
|
|||||||
#include <common/time.h>
|
#include <common/time.h>
|
||||||
#include <common/unistd.h>
|
#include <common/unistd.h>
|
||||||
#include <driver/multiboot2/multiboot2.h>
|
#include <driver/multiboot2/multiboot2.h>
|
||||||
#include <include/DragonOS/refcount.h>
|
|
||||||
#include <libs/lib_ui/textui.h>
|
#include <libs/lib_ui/textui.h>
|
||||||
#include <mm/mm.h>
|
#include <mm/mm.h>
|
||||||
#include <mm/mmio.h>
|
#include <mm/mmio.h>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
CFLAGS += -I .
|
CFLAGS += -I .
|
||||||
|
|
||||||
kernel_lib_subdirs:= sys
|
kernel_lib_subdirs:=
|
||||||
|
|
||||||
kernel_lib_objs:= $(shell find ./*.c)
|
kernel_lib_objs:= $(shell find ./*.c)
|
||||||
|
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
// Polynomial=0x8005
|
|
||||||
// Initial Value=0x0
|
|
||||||
// Final Xor Value=0x0
|
|
||||||
|
|
||||||
/** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */
|
|
||||||
uint16_t const crc16_table[256] = {
|
|
||||||
0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011, 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
|
|
||||||
0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072, 0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
|
|
||||||
0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2, 0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
|
|
||||||
0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1, 0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
|
|
||||||
0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192, 0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
|
|
||||||
0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1, 0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
|
|
||||||
0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151, 0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
|
|
||||||
0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132, 0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101,
|
|
||||||
0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312, 0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
|
|
||||||
0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371, 0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
|
|
||||||
0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1, 0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
|
|
||||||
0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2, 0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
|
|
||||||
0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291, 0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
|
|
||||||
0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2, 0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
|
|
||||||
0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252, 0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
|
|
||||||
0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231, 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc16
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint16_t crc
|
|
||||||
*/
|
|
||||||
uint16_t crc16(uint16_t crc, uint8_t const *buffer, size_t len)
|
|
||||||
{
|
|
||||||
while (len--)
|
|
||||||
{
|
|
||||||
crc = (crc << 8) ^ crc16_table[((crc >> 8) ^ *buffer++) & 0xff];
|
|
||||||
}
|
|
||||||
return crc;
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
#include <common/sys/types.h>
|
|
||||||
// Polynomial=0x4C11DB7
|
|
||||||
// Initial Value=0x0
|
|
||||||
// Final Xor Value:0x0
|
|
||||||
|
|
||||||
/** CRC table for the CRC-32. The poly is 0x4C11DB7 */
|
|
||||||
uint32_t const crc32_table[256] = {
|
|
||||||
0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005,
|
|
||||||
0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61, 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD,
|
|
||||||
0x4C11DB70, 0x48D0C6C7, 0x4593E01E, 0x4152FDA9, 0x5F15ADAC, 0x5BD4B01B, 0x569796C2, 0x52568B75,
|
|
||||||
0x6A1936C8, 0x6ED82B7F, 0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3, 0x709F7B7A, 0x745E66CD,
|
|
||||||
0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039, 0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5,
|
|
||||||
0xBE2B5B58, 0xBAEA46EF, 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033, 0xA4AD16EA, 0xA06C0B5D,
|
|
||||||
0xD4326D90, 0xD0F37027, 0xDDB056FE, 0xD9714B49, 0xC7361B4C, 0xC3F706FB, 0xCEB42022, 0xCA753D95,
|
|
||||||
0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1, 0xE13EF6F4, 0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D,
|
|
||||||
0x34867077, 0x30476DC0, 0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5, 0x2AC12072,
|
|
||||||
0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16, 0x018AEB13, 0x054BF6A4, 0x0808D07D, 0x0CC9CDCA,
|
|
||||||
0x7897AB07, 0x7C56B6B0, 0x71159069, 0x75D48DDE, 0x6B93DDDB, 0x6F52C06C, 0x6211E6B5, 0x66D0FB02,
|
|
||||||
0x5E9F46BF, 0x5A5E5B08, 0x571D7DD1, 0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,
|
|
||||||
0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B, 0xBB60ADFC, 0xB6238B25, 0xB2E29692,
|
|
||||||
0x8AAD2B2F, 0x8E6C3698, 0x832F1041, 0x87EE0DF6, 0x99A95DF3, 0x9D684044, 0x902B669D, 0x94EA7B2A,
|
|
||||||
0xE0B41DE7, 0xE4750050, 0xE9362689, 0xEDF73B3E, 0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2,
|
|
||||||
0xC6BCF05F, 0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34, 0xDC3ABDED, 0xD8FBA05A,
|
|
||||||
0x690CE0EE, 0x6DCDFD59, 0x608EDB80, 0x644FC637, 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB,
|
|
||||||
0x4F040D56, 0x4BC510E1, 0x46863638, 0x42472B8F, 0x5C007B8A, 0x58C1663D, 0x558240E4, 0x51435D53,
|
|
||||||
0x251D3B9E, 0x21DC2629, 0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5, 0x3F9B762C, 0x3B5A6B9B,
|
|
||||||
0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF, 0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623,
|
|
||||||
0xF12F560E, 0xF5EE4BB9, 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65, 0xEBA91BBC, 0xEF68060B,
|
|
||||||
0xD727BBB6, 0xD3E6A601, 0xDEA580D8, 0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD, 0xCDA1F604, 0xC960EBB3,
|
|
||||||
0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7, 0xAE3AFBA2, 0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B,
|
|
||||||
0x9B3660C6, 0x9FF77D71, 0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74, 0x857130C3,
|
|
||||||
0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640, 0x4E8EE645, 0x4A4FFBF2, 0x470CDD2B, 0x43CDC09C,
|
|
||||||
0x7B827D21, 0x7F436096, 0x7200464F, 0x76C15BF8, 0x68860BFD, 0x6C47164A, 0x61043093, 0x65C52D24,
|
|
||||||
0x119B4BE9, 0x155A565E, 0x18197087, 0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,
|
|
||||||
0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D, 0x2056CD3A, 0x2D15EBE3, 0x29D4F654,
|
|
||||||
0xC5A92679, 0xC1683BCE, 0xCC2B1D17, 0xC8EA00A0, 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB, 0xDBEE767C,
|
|
||||||
0xE3A1CBC1, 0xE760D676, 0xEA23F0AF, 0xEEE2ED18, 0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4,
|
|
||||||
0x89B8FD09, 0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662, 0x933EB0BB, 0x97FFAD0C,
|
|
||||||
0xAFB010B1, 0xAB710D06, 0xA6322BDF, 0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc32
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint32_t crc
|
|
||||||
*/
|
|
||||||
uint32_t crc32(uint32_t crc, uint8_t const *buffer, size_t len)
|
|
||||||
{
|
|
||||||
while (len--)
|
|
||||||
{
|
|
||||||
crc = (crc << 8) ^ (crc32_table[((crc >> 24) ^ *buffer++) & 0xff]);
|
|
||||||
}
|
|
||||||
return crc;
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
// Polynomial =0x42F0E1EBA9EA3693
|
|
||||||
// Initial Value=0x0
|
|
||||||
// Final Xor Value=0x0
|
|
||||||
uint64_t const crc64_table[256] = {
|
|
||||||
0x0000000000000000, 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, 0xC711223CFA3E5BB5, 0x493366450E42ECDF, 0x0BC387AEA7A8DA4C, 0xCCD2A5925D9681F9, 0x8E224479F47CB76A,
|
|
||||||
0x9266CC8A1C85D9BE, 0xD0962D61B56FEF2D, 0x17870F5D4F51B498, 0x5577EEB6E6BB820B, 0xDB55AACF12C73561, 0x99A54B24BB2D03F2, 0x5EB4691841135847, 0x1C4488F3E8F96ED4,
|
|
||||||
0x663D78FF90E185EF, 0x24CD9914390BB37C, 0xE3DCBB28C335E8C9, 0xA12C5AC36ADFDE5A, 0x2F0E1EBA9EA36930, 0x6DFEFF5137495FA3, 0xAAEFDD6DCD770416, 0xE81F3C86649D3285,
|
|
||||||
0xF45BB4758C645C51, 0xB6AB559E258E6AC2, 0x71BA77A2DFB03177, 0x334A9649765A07E4, 0xBD68D2308226B08E, 0xFF9833DB2BCC861D, 0x388911E7D1F2DDA8, 0x7A79F00C7818EB3B,
|
|
||||||
0xCC7AF1FF21C30BDE, 0x8E8A101488293D4D, 0x499B3228721766F8, 0x0B6BD3C3DBFD506B, 0x854997BA2F81E701, 0xC7B97651866BD192, 0x00A8546D7C558A27, 0x4258B586D5BFBCB4,
|
|
||||||
0x5E1C3D753D46D260, 0x1CECDC9E94ACE4F3, 0xDBFDFEA26E92BF46, 0x990D1F49C77889D5, 0x172F5B3033043EBF, 0x55DFBADB9AEE082C, 0x92CE98E760D05399, 0xD03E790CC93A650A,
|
|
||||||
0xAA478900B1228E31, 0xE8B768EB18C8B8A2, 0x2FA64AD7E2F6E317, 0x6D56AB3C4B1CD584, 0xE374EF45BF6062EE, 0xA1840EAE168A547D, 0x66952C92ECB40FC8, 0x2465CD79455E395B,
|
|
||||||
0x3821458AADA7578F, 0x7AD1A461044D611C, 0xBDC0865DFE733AA9, 0xFF3067B657990C3A, 0x711223CFA3E5BB50, 0x33E2C2240A0F8DC3, 0xF4F3E018F031D676, 0xB60301F359DBE0E5,
|
|
||||||
0xDA050215EA6C212F, 0x98F5E3FE438617BC, 0x5FE4C1C2B9B84C09, 0x1D14202910527A9A, 0x93366450E42ECDF0, 0xD1C685BB4DC4FB63, 0x16D7A787B7FAA0D6, 0x5427466C1E109645,
|
|
||||||
0x4863CE9FF6E9F891, 0x0A932F745F03CE02, 0xCD820D48A53D95B7, 0x8F72ECA30CD7A324, 0x0150A8DAF8AB144E, 0x43A04931514122DD, 0x84B16B0DAB7F7968, 0xC6418AE602954FFB,
|
|
||||||
0xBC387AEA7A8DA4C0, 0xFEC89B01D3679253, 0x39D9B93D2959C9E6, 0x7B2958D680B3FF75, 0xF50B1CAF74CF481F, 0xB7FBFD44DD257E8C, 0x70EADF78271B2539, 0x321A3E938EF113AA,
|
|
||||||
0x2E5EB66066087D7E, 0x6CAE578BCFE24BED, 0xABBF75B735DC1058, 0xE94F945C9C3626CB, 0x676DD025684A91A1, 0x259D31CEC1A0A732, 0xE28C13F23B9EFC87, 0xA07CF2199274CA14,
|
|
||||||
0x167FF3EACBAF2AF1, 0x548F120162451C62, 0x939E303D987B47D7, 0xD16ED1D631917144, 0x5F4C95AFC5EDC62E, 0x1DBC74446C07F0BD, 0xDAAD56789639AB08, 0x985DB7933FD39D9B,
|
|
||||||
0x84193F60D72AF34F, 0xC6E9DE8B7EC0C5DC, 0x01F8FCB784FE9E69, 0x43081D5C2D14A8FA, 0xCD2A5925D9681F90, 0x8FDAB8CE70822903, 0x48CB9AF28ABC72B6, 0x0A3B7B1923564425,
|
|
||||||
0x70428B155B4EAF1E, 0x32B26AFEF2A4998D, 0xF5A348C2089AC238, 0xB753A929A170F4AB, 0x3971ED50550C43C1, 0x7B810CBBFCE67552, 0xBC902E8706D82EE7, 0xFE60CF6CAF321874,
|
|
||||||
0xE224479F47CB76A0, 0xA0D4A674EE214033, 0x67C58448141F1B86, 0x253565A3BDF52D15, 0xAB1721DA49899A7F, 0xE9E7C031E063ACEC, 0x2EF6E20D1A5DF759, 0x6C0603E6B3B7C1CA,
|
|
||||||
0xF6FAE5C07D3274CD, 0xB40A042BD4D8425E, 0x731B26172EE619EB, 0x31EBC7FC870C2F78, 0xBFC9838573709812, 0xFD39626EDA9AAE81, 0x3A28405220A4F534, 0x78D8A1B9894EC3A7,
|
|
||||||
0x649C294A61B7AD73, 0x266CC8A1C85D9BE0, 0xE17DEA9D3263C055, 0xA38D0B769B89F6C6, 0x2DAF4F0F6FF541AC, 0x6F5FAEE4C61F773F, 0xA84E8CD83C212C8A, 0xEABE6D3395CB1A19,
|
|
||||||
0x90C79D3FEDD3F122, 0xD2377CD44439C7B1, 0x15265EE8BE079C04, 0x57D6BF0317EDAA97, 0xD9F4FB7AE3911DFD, 0x9B041A914A7B2B6E, 0x5C1538ADB04570DB, 0x1EE5D94619AF4648,
|
|
||||||
0x02A151B5F156289C, 0x4051B05E58BC1E0F, 0x87409262A28245BA, 0xC5B073890B687329, 0x4B9237F0FF14C443, 0x0962D61B56FEF2D0, 0xCE73F427ACC0A965, 0x8C8315CC052A9FF6,
|
|
||||||
0x3A80143F5CF17F13, 0x7870F5D4F51B4980, 0xBF61D7E80F251235, 0xFD913603A6CF24A6, 0x73B3727A52B393CC, 0x31439391FB59A55F, 0xF652B1AD0167FEEA, 0xB4A25046A88DC879,
|
|
||||||
0xA8E6D8B54074A6AD, 0xEA16395EE99E903E, 0x2D071B6213A0CB8B, 0x6FF7FA89BA4AFD18, 0xE1D5BEF04E364A72, 0xA3255F1BE7DC7CE1, 0x64347D271DE22754, 0x26C49CCCB40811C7,
|
|
||||||
0x5CBD6CC0CC10FAFC, 0x1E4D8D2B65FACC6F, 0xD95CAF179FC497DA, 0x9BAC4EFC362EA149, 0x158E0A85C2521623, 0x577EEB6E6BB820B0, 0x906FC95291867B05, 0xD29F28B9386C4D96,
|
|
||||||
0xCEDBA04AD0952342, 0x8C2B41A1797F15D1, 0x4B3A639D83414E64, 0x09CA82762AAB78F7, 0x87E8C60FDED7CF9D, 0xC51827E4773DF90E, 0x020905D88D03A2BB, 0x40F9E43324E99428,
|
|
||||||
0x2CFFE7D5975E55E2, 0x6E0F063E3EB46371, 0xA91E2402C48A38C4, 0xEBEEC5E96D600E57, 0x65CC8190991CB93D, 0x273C607B30F68FAE, 0xE02D4247CAC8D41B, 0xA2DDA3AC6322E288,
|
|
||||||
0xBE992B5F8BDB8C5C, 0xFC69CAB42231BACF, 0x3B78E888D80FE17A, 0x7988096371E5D7E9, 0xF7AA4D1A85996083, 0xB55AACF12C735610, 0x724B8ECDD64D0DA5, 0x30BB6F267FA73B36,
|
|
||||||
0x4AC29F2A07BFD00D, 0x08327EC1AE55E69E, 0xCF235CFD546BBD2B, 0x8DD3BD16FD818BB8, 0x03F1F96F09FD3CD2, 0x41011884A0170A41, 0x86103AB85A2951F4, 0xC4E0DB53F3C36767,
|
|
||||||
0xD8A453A01B3A09B3, 0x9A54B24BB2D03F20, 0x5D45907748EE6495, 0x1FB5719CE1045206, 0x919735E51578E56C, 0xD367D40EBC92D3FF, 0x1476F63246AC884A, 0x568617D9EF46BED9,
|
|
||||||
0xE085162AB69D5E3C, 0xA275F7C11F7768AF, 0x6564D5FDE549331A, 0x279434164CA30589, 0xA9B6706FB8DFB2E3, 0xEB46918411358470, 0x2C57B3B8EB0BDFC5, 0x6EA7525342E1E956,
|
|
||||||
0x72E3DAA0AA188782, 0x30133B4B03F2B111, 0xF7021977F9CCEAA4, 0xB5F2F89C5026DC37, 0x3BD0BCE5A45A6B5D, 0x79205D0E0DB05DCE, 0xBE317F32F78E067B, 0xFCC19ED95E6430E8,
|
|
||||||
0x86B86ED5267CDBD3, 0xC4488F3E8F96ED40, 0x0359AD0275A8B6F5, 0x41A94CE9DC428066, 0xCF8B0890283E370C, 0x8D7BE97B81D4019F, 0x4A6ACB477BEA5A2A, 0x089A2AACD2006CB9,
|
|
||||||
0x14DEA25F3AF9026D, 0x562E43B4931334FE, 0x913F6188692D6F4B, 0xD3CF8063C0C759D8, 0x5DEDC41A34BBEEB2, 0x1F1D25F19D51D821, 0xD80C07CD676F8394, 0x9AFCE626CE85B507
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc64
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint64_t crc
|
|
||||||
*/
|
|
||||||
uint64_t crc64(uint64_t crc, uint8_t const *buffer, size_t len)
|
|
||||||
{
|
|
||||||
while (len--)
|
|
||||||
{
|
|
||||||
crc = (crc << 8) ^ (crc64_table[((crc >> 56) ^ *buffer++) & 0xff]);
|
|
||||||
}
|
|
||||||
return crc;
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
#include <common/sys/types.h>
|
|
||||||
|
|
||||||
/** CRC table for the CRC-7. The poly is 0X09 */
|
|
||||||
// crc7 generator polynomial : G(x)=x7+x3+1
|
|
||||||
const uint8_t crc7_table[256] = {
|
|
||||||
0X00, 0X09, 0X12, 0X1B, 0X24, 0X2D, 0X36, 0X3F, 0X48, 0X41, 0X5A, 0X53, 0X6C, 0X65, 0X7E, 0X77,
|
|
||||||
0X19, 0X10, 0X0B, 0X02, 0X3D, 0X34, 0X2F, 0X26, 0X51, 0X58, 0X43, 0X4A, 0X75, 0X7C, 0X67, 0X6E,
|
|
||||||
0X32, 0X3B, 0X20, 0X29, 0X16, 0X1F, 0X04, 0X0D, 0X7A, 0X73, 0X68, 0X61, 0X5E, 0X57, 0X4C, 0X45,
|
|
||||||
0X2B, 0X22, 0X39, 0X30, 0X0F, 0X06, 0X1D, 0X14, 0X63, 0X6A, 0X71, 0X78, 0X47, 0X4E, 0X55, 0X5C,
|
|
||||||
0X64, 0X6D, 0X76, 0X7F, 0X40, 0X49, 0X52, 0X5B, 0X2C, 0X25, 0X3E, 0X37, 0X08, 0X01, 0X1A, 0X13,
|
|
||||||
0X7D, 0X74, 0X6F, 0X66, 0X59, 0X50, 0X4B, 0X42, 0X35, 0X3C, 0X27, 0X2E, 0X11, 0X18, 0X03, 0X0A,
|
|
||||||
0X56, 0X5F, 0X44, 0X4D, 0X72, 0X7B, 0X60, 0X69, 0X1E, 0X17, 0X0C, 0X05, 0X3A, 0X33, 0X28, 0X21,
|
|
||||||
0X4F, 0X46, 0X5D, 0X54, 0X6B, 0X62, 0X79, 0X70, 0X07, 0X0E, 0X15, 0X1C, 0X23, 0X2A, 0X31, 0X38,
|
|
||||||
0X41, 0X48, 0X53, 0X5A, 0X65, 0X6C, 0X77, 0X7E, 0X09, 0X00, 0X1B, 0X12, 0X2D, 0X24, 0X3F, 0X36,
|
|
||||||
0X58, 0X51, 0X4A, 0X43, 0X7C, 0X75, 0X6E, 0X67, 0X10, 0X19, 0X02, 0X0B, 0X34, 0X3D, 0X26, 0X2F,
|
|
||||||
0X73, 0X7A, 0X61, 0X68, 0X57, 0X5E, 0X45, 0X4C, 0X3B, 0X32, 0X29, 0X20, 0X1F, 0X16, 0X0D, 0X04,
|
|
||||||
0X6A, 0X63, 0X78, 0X71, 0X4E, 0X47, 0X5C, 0X55, 0X22, 0X2B, 0X30, 0X39, 0X06, 0X0F, 0X14, 0X1D,
|
|
||||||
0X25, 0X2C, 0X37, 0X3E, 0X01, 0X08, 0X13, 0X1A, 0X6D, 0X64, 0X7F, 0X76, 0X49, 0X40, 0X5B, 0X52,
|
|
||||||
0X3C, 0X35, 0X2E, 0X27, 0X18, 0X11, 0X0A, 0X03, 0X74, 0X7D, 0X66, 0X6F, 0X50, 0X59, 0X42, 0X4B,
|
|
||||||
0X17, 0X1E, 0X05, 0X0C, 0X33, 0X3A, 0X21, 0X28, 0X5F, 0X56, 0X4D, 0X44, 0X7B, 0X72, 0X69, 0X60,
|
|
||||||
0X0E, 0X07, 0X1C, 0X15, 0X2A, 0X23, 0X38, 0X31, 0X46, 0X4F, 0X54, 0X5D, 0X62, 0X6B, 0X70, 0X79};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc7
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint8_t crc
|
|
||||||
*/
|
|
||||||
uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len)
|
|
||||||
{
|
|
||||||
while (len--)
|
|
||||||
{
|
|
||||||
crc = crc7_table[(crc << 1) ^ *buffer++];
|
|
||||||
}
|
|
||||||
return (crc);
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
#include <common/sys/types.h>
|
|
||||||
// Polynomial=0x31
|
|
||||||
// Initial Value=0x0
|
|
||||||
// Final Xor Value=0x0
|
|
||||||
|
|
||||||
/** CRC table for the CRC-8. The poly is 0x31 */
|
|
||||||
const uint8_t crc8_table[256] = {
|
|
||||||
0x00, 0x31, 0x62, 0x53, 0xc4, 0xf5, 0xa6, 0x97, 0xb9, 0x88, 0xdb, 0xea, 0x7d, 0x4c, 0x1f, 0x2e,
|
|
||||||
0x43, 0x72, 0x21, 0x10, 0x87, 0xb6, 0xe5, 0xd4, 0xfa, 0xcb, 0x98, 0xa9, 0x3e, 0x0f, 0x5c, 0x6d,
|
|
||||||
0x86, 0xb7, 0xe4, 0xd5, 0x42, 0x73, 0x20, 0x11, 0x3f, 0x0e, 0x5d, 0x6c, 0xfb, 0xca, 0x99, 0xa8,
|
|
||||||
0xc5, 0xf4, 0xa7, 0x96, 0x01, 0x30, 0x63, 0x52, 0x7c, 0x4d, 0x1e, 0x2f, 0xb8, 0x89, 0xda, 0xeb,
|
|
||||||
0x3d, 0x0c, 0x5f, 0x6e, 0xf9, 0xc8, 0x9b, 0xaa, 0x84, 0xb5, 0xe6, 0xd7, 0x40, 0x71, 0x22, 0x13,
|
|
||||||
0x7e, 0x4f, 0x1c, 0x2d, 0xba, 0x8b, 0xd8, 0xe9, 0xc7, 0xf6, 0xa5, 0x94, 0x03, 0x32, 0x61, 0x50,
|
|
||||||
0xbb, 0x8a, 0xd9, 0xe8, 0x7f, 0x4e, 0x1d, 0x2c, 0x02, 0x33, 0x60, 0x51, 0xc6, 0xf7, 0xa4, 0x95,
|
|
||||||
0xf8, 0xc9, 0x9a, 0xab, 0x3c, 0x0d, 0x5e, 0x6f, 0x41, 0x70, 0x23, 0x12, 0x85, 0xb4, 0xe7, 0xd6,
|
|
||||||
0x7a, 0x4b, 0x18, 0x29, 0xbe, 0x8f, 0xdc, 0xed, 0xc3, 0xf2, 0xa1, 0x90, 0x07, 0x36, 0x65, 0x54,
|
|
||||||
0x39, 0x08, 0x5b, 0x6a, 0xfd, 0xcc, 0x9f, 0xae, 0x80, 0xb1, 0xe2, 0xd3, 0x44, 0x75, 0x26, 0x17,
|
|
||||||
0xfc, 0xcd, 0x9e, 0xaf, 0x38, 0x09, 0x5a, 0x6b, 0x45, 0x74, 0x27, 0x16, 0x81, 0xb0, 0xe3, 0xd2,
|
|
||||||
0xbf, 0x8e, 0xdd, 0xec, 0x7b, 0x4a, 0x19, 0x28, 0x06, 0x37, 0x64, 0x55, 0xc2, 0xf3, 0xa0, 0x91,
|
|
||||||
0x47, 0x76, 0x25, 0x14, 0x83, 0xb2, 0xe1, 0xd0, 0xfe, 0xcf, 0x9c, 0xad, 0x3a, 0x0b, 0x58, 0x69,
|
|
||||||
0x04, 0x35, 0x66, 0x57, 0xc0, 0xf1, 0xa2, 0x93, 0xbd, 0x8c, 0xdf, 0xee, 0x79, 0x48, 0x1b, 0x2a,
|
|
||||||
0xc1, 0xf0, 0xa3, 0x92, 0x05, 0x34, 0x67, 0x56, 0x78, 0x49, 0x1a, 0x2b, 0xbc, 0x8d, 0xde, 0xef,
|
|
||||||
0x82, 0xb3, 0xe0, 0xd1, 0x46, 0x77, 0x24, 0x15, 0x3b, 0x0a, 0x59, 0x68, 0xff, 0xce, 0x9d, 0xac};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 计算crc8
|
|
||||||
*
|
|
||||||
* @param crc crc初始值
|
|
||||||
* @param buffer 输入缓冲区
|
|
||||||
* @param len buffer大小(bytes)
|
|
||||||
* @return uint8_t crc
|
|
||||||
*/
|
|
||||||
uint8_t crc8(uint8_t crc, uint8_t const *buffer, size_t len)
|
|
||||||
{
|
|
||||||
while (len--)
|
|
||||||
{
|
|
||||||
// printf("%04x\n",(crc ^ *buffer)&0xff);
|
|
||||||
crc = crc8_table[crc ^ *buffer++];
|
|
||||||
}
|
|
||||||
return (crc);
|
|
||||||
}
|
|
@ -1,151 +0,0 @@
|
|||||||
#include <common/kfifo.h>
|
|
||||||
#include <common/glib.h>
|
|
||||||
#include <common/errno.h>
|
|
||||||
#include <common/compiler.h>
|
|
||||||
#include <mm/slab.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 通过动态方式初始化kfifo缓冲队列
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param size 缓冲区大小
|
|
||||||
* @param reserved 暂时保留,请置为0
|
|
||||||
* @return int 错误码: NOMEM; 成功->0
|
|
||||||
*/
|
|
||||||
int kfifo_alloc(struct kfifo_t *fifo, uint32_t size, uint64_t reserved)
|
|
||||||
{
|
|
||||||
memset(fifo, 0, sizeof(struct kfifo_t));
|
|
||||||
fifo->buffer = kmalloc(size, 0);
|
|
||||||
if (fifo->buffer == NULL)
|
|
||||||
goto failed;
|
|
||||||
|
|
||||||
fifo->total_size = size;
|
|
||||||
return 0;
|
|
||||||
failed:;
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 使用指定的缓冲区来初始化kfifo缓冲队列
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param buffer 缓冲区
|
|
||||||
* @param size 缓冲区大小
|
|
||||||
*/
|
|
||||||
void kfifo_init(struct kfifo_t *fifo, void *buffer, uint32_t size)
|
|
||||||
{
|
|
||||||
memset(fifo, 0, sizeof(struct kfifo_t));
|
|
||||||
|
|
||||||
fifo->buffer = buffer;
|
|
||||||
fifo->total_size = size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 向kfifo缓冲区推入数据
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param from 来源数据地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @return uint32_t 推入的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t kfifo_in(struct kfifo_t *fifo, const void *from, uint32_t size)
|
|
||||||
{
|
|
||||||
// 判断空间是否够
|
|
||||||
if (unlikely(fifo->size + size > fifo->total_size))
|
|
||||||
return 0;
|
|
||||||
if (unlikely(from == NULL))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// 分两种情况,一种是要发生回环,另一种不发生回环
|
|
||||||
if (fifo->in_offset + size > fifo->total_size) // 发生回环
|
|
||||||
{
|
|
||||||
uint32_t tmp = fifo->total_size - fifo->in_offset;
|
|
||||||
memcpy(fifo->buffer + fifo->in_offset, from, tmp);
|
|
||||||
memcpy(fifo->buffer, from + tmp, size - tmp);
|
|
||||||
fifo->in_offset = size - tmp;
|
|
||||||
}
|
|
||||||
else // 不发生回环
|
|
||||||
{
|
|
||||||
memcpy(fifo->buffer + fifo->in_offset, from, size);
|
|
||||||
fifo->in_offset += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
fifo->size += size;
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 从kfifo缓冲区取出数据,并从队列中删除数据
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param to 拷贝目标地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @return uint32_t 取出的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t kfifo_out(struct kfifo_t *fifo, void *to, uint32_t size)
|
|
||||||
{
|
|
||||||
if (unlikely(to == NULL)) // 判断目标地址是否为空
|
|
||||||
return 0;
|
|
||||||
if (unlikely(size > fifo->size)) // 判断队列中是否有这么多数据
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// 判断是否会发生回环
|
|
||||||
if (fifo->out_offset + size > fifo->total_size) // 发生回环
|
|
||||||
{
|
|
||||||
uint32_t tmp = fifo->total_size - fifo->out_offset;
|
|
||||||
memcpy(to, fifo->buffer + fifo->out_offset, tmp);
|
|
||||||
memcpy(to + tmp, fifo->buffer, size - tmp);
|
|
||||||
fifo->out_offset = size - tmp;
|
|
||||||
}
|
|
||||||
else // 未发生回环
|
|
||||||
{
|
|
||||||
memcpy(to, fifo->buffer + fifo->out_offset, size);
|
|
||||||
fifo->out_offset += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
fifo->size -= size;
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 从kfifo缓冲区取出数据,但是不从队列中删除数据
|
|
||||||
*
|
|
||||||
* @param fifo 队列结构体
|
|
||||||
* @param to 拷贝目标地址
|
|
||||||
* @param size 数据大小(字节数)
|
|
||||||
* @return uint32_t 取出的数据大小
|
|
||||||
*/
|
|
||||||
uint32_t kfifo_out_peek(struct kfifo_t *fifo, void *to, uint32_t size)
|
|
||||||
{
|
|
||||||
if (unlikely(to == NULL)) // 判断目标地址是否为空
|
|
||||||
return 0;
|
|
||||||
if (unlikely(size > fifo->size)) // 判断队列中是否有这么多数据
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// 判断是否会发生回环
|
|
||||||
if (fifo->out_offset + size > fifo->total_size) // 发生回环
|
|
||||||
{
|
|
||||||
uint32_t tmp = fifo->total_size - fifo->out_offset;
|
|
||||||
memcpy(to, fifo->buffer + fifo->out_offset, tmp);
|
|
||||||
memcpy(to + tmp, fifo->buffer, size - tmp);
|
|
||||||
}
|
|
||||||
else // 未发生回环
|
|
||||||
{
|
|
||||||
memcpy(to, fifo->buffer + fifo->out_offset, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 释放通过kfifo_alloc创建的fifo缓冲区
|
|
||||||
*
|
|
||||||
* @param fifo fifo队列结构体
|
|
||||||
*/
|
|
||||||
void kfifo_free_alloc(struct kfifo_t *fifo)
|
|
||||||
{
|
|
||||||
kfree(fifo->buffer);
|
|
||||||
memset(fifo, 0, sizeof(struct kfifo_t));
|
|
||||||
}
|
|
@ -1,249 +0,0 @@
|
|||||||
#include <arch/arch.h>
|
|
||||||
|
|
||||||
#if ARCH(I386) || ARCH(X86_64)
|
|
||||||
|
|
||||||
#include <common/lockref.h>
|
|
||||||
#include <common/compiler.h>
|
|
||||||
|
|
||||||
#ifdef __LOCKREF_ENABLE_CMPXCHG__
|
|
||||||
#include <asm/cmpxchg.h>
|
|
||||||
|
|
||||||
#define CMPXCHG_LOOP(__lock_ref, CODE, SUCCESS) \
|
|
||||||
{ \
|
|
||||||
int retry = 100; \
|
|
||||||
struct lockref old; \
|
|
||||||
BUILD_BUG_ON(sizeof(old) != sizeof(uint64_t)); \
|
|
||||||
old.lock_count = READ_ONCE(__lock_ref->lock_count); \
|
|
||||||
while (likely(!spin_is_locked(&old.lock))) \
|
|
||||||
{ \
|
|
||||||
struct lockref new = old; \
|
|
||||||
CODE; \
|
|
||||||
if (likely(arch_try_cmpxchg(&__lock_ref->lock_count, &old.lock_count, new.lock_count))) \
|
|
||||||
{ \
|
|
||||||
SUCCESS; \
|
|
||||||
} \
|
|
||||||
if (!--retry) \
|
|
||||||
break; \
|
|
||||||
pause(); \
|
|
||||||
} \
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
|
|
||||||
#define CMPXCHG_LOOP(__lock_ref, CODE, SUCCESS) \
|
|
||||||
do \
|
|
||||||
{ \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子的将引用计数加1
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
*/
|
|
||||||
void lockref_inc(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
// 先尝试使用cmpxchg进行无锁操作,若成功则返回
|
|
||||||
CMPXCHG_LOOP(lock_ref, ++new.count;, return;);
|
|
||||||
|
|
||||||
// 无锁操作超时,或当前是上锁的状态,则退化为有锁操作
|
|
||||||
spin_lock(&lock_ref->lock);
|
|
||||||
++lock_ref->count;
|
|
||||||
spin_unlock(&lock_ref->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地将引用计数加1.如果原来的count≤0,则操作失败。
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return bool 操作成功=>true
|
|
||||||
* 操作失败=>false
|
|
||||||
*/
|
|
||||||
bool lockref_inc_not_zero(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
CMPXCHG_LOOP(
|
|
||||||
lock_ref,
|
|
||||||
{
|
|
||||||
if (old.count <= 0)
|
|
||||||
return false;
|
|
||||||
++new.count;
|
|
||||||
},
|
|
||||||
{ return true; })
|
|
||||||
|
|
||||||
bool retval;
|
|
||||||
spin_lock(&lock_ref->lock);
|
|
||||||
retval = false;
|
|
||||||
if (lock_ref->count > 0)
|
|
||||||
{
|
|
||||||
++lock_ref->count;
|
|
||||||
retval = true;
|
|
||||||
}
|
|
||||||
spin_unlock(&lock_ref->lock);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。如果已处于count≤0的状态,则返回-1
|
|
||||||
*
|
|
||||||
* 本函数与lockref_dec_return()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会再次尝试通过加锁来执行操作
|
|
||||||
* 而后者会直接返回错误
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return int 操作成功 => 返回新的引用变量值
|
|
||||||
* lockref处于count≤0的状态 => 返回-1
|
|
||||||
*/
|
|
||||||
int lockref_dec(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
CMPXCHG_LOOP(
|
|
||||||
lock_ref,
|
|
||||||
{
|
|
||||||
if (old.count <= 0)
|
|
||||||
break;
|
|
||||||
--new.count;
|
|
||||||
},
|
|
||||||
{ return new.count; })
|
|
||||||
|
|
||||||
// 如果xchg时,处于已加锁的状态或者检测到old.count <= 0,则采取加锁处理
|
|
||||||
int retval = -1;
|
|
||||||
spin_lock(&lock_ref->lock);
|
|
||||||
if (lock_ref->count > 0)
|
|
||||||
{
|
|
||||||
--lock_ref->count;
|
|
||||||
retval = lock_ref->count;
|
|
||||||
}
|
|
||||||
spin_unlock(&lock_ref->lock);
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。如果处于已加锁或count≤0的状态,则返回-1
|
|
||||||
*
|
|
||||||
* 本函数与lockref_dec()的区别在于,当在cmpxchg()中检测到count<=0或已加锁,本函数会直接返回错误
|
|
||||||
* 而后者会再次尝试通过加锁来执行操作
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return int 操作成功 => 返回新的引用变量值
|
|
||||||
* lockref处于已加锁或count≤0的状态 => 返回-1
|
|
||||||
*/
|
|
||||||
int lockref_dec_return(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
CMPXCHG_LOOP(
|
|
||||||
lock_ref,
|
|
||||||
{
|
|
||||||
if (old.count <= 0)
|
|
||||||
return -1;
|
|
||||||
--new.count;
|
|
||||||
},
|
|
||||||
{ return new.count; })
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败
|
|
||||||
*
|
|
||||||
* 该函数与lockref_dec_or_lock_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会直接返回false.
|
|
||||||
* 而后者在这种情况下,会尝试加锁来进行操作。
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return true 成功将引用计数减1
|
|
||||||
* @return false 如果当前的引用计数≤1,操作失败
|
|
||||||
*/
|
|
||||||
bool lockref_dec_not_zero(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
CMPXCHG_LOOP(
|
|
||||||
lock_ref,
|
|
||||||
{
|
|
||||||
if (old.count <= 1)
|
|
||||||
return false;
|
|
||||||
--new.count;
|
|
||||||
},
|
|
||||||
{ return true; })
|
|
||||||
|
|
||||||
bool retval = false;
|
|
||||||
spin_lock(&lock_ref->lock);
|
|
||||||
if (lock_ref->count > 1)
|
|
||||||
{
|
|
||||||
--lock_ref->count;
|
|
||||||
retval = true;
|
|
||||||
}
|
|
||||||
spin_unlock(&lock_ref->lock);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 原子地减少引用计数。若当前的引用计数≤1,则操作失败
|
|
||||||
*
|
|
||||||
* 该函数与lockref_dec_not_zero()的区别在于,当cmpxchg()时发现old.count≤1时,该函数会尝试加锁来进行操作。
|
|
||||||
* 而后者在这种情况下,会直接返回false.
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return true 成功将引用计数减1
|
|
||||||
* @return false 如果当前的引用计数≤1,操作失败
|
|
||||||
*/
|
|
||||||
bool lockref_dec_or_lock_not_zero(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
CMPXCHG_LOOP(
|
|
||||||
lock_ref,
|
|
||||||
{
|
|
||||||
if (old.count <= 1)
|
|
||||||
break;
|
|
||||||
--new.count;
|
|
||||||
},
|
|
||||||
{ return true; });
|
|
||||||
|
|
||||||
bool retval = false;
|
|
||||||
spin_lock(&lock_ref->lock);
|
|
||||||
if (lock_ref->count > 1)
|
|
||||||
{
|
|
||||||
--lock_ref->count;
|
|
||||||
retval = true;
|
|
||||||
}
|
|
||||||
spin_unlock(&lock_ref->lock);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 将lockref变量标记为已经死亡(将count设置为负值)
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
*/
|
|
||||||
void lockref_mark_dead(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
// 需要自旋锁先被加锁,若没有被加锁,则会抛出错误信息
|
|
||||||
assert_spin_locked(&lock_ref->lock);
|
|
||||||
lock_ref->count = -128;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 自增引用计数。(除非该lockref已经死亡)
|
|
||||||
*
|
|
||||||
* @param lock_ref 指向要被操作的lockref变量的指针
|
|
||||||
* @return true 操作成功
|
|
||||||
* @return false 操作失败,lockref已死亡
|
|
||||||
*/
|
|
||||||
bool lockref_inc_not_dead(struct lockref *lock_ref)
|
|
||||||
{
|
|
||||||
CMPXCHG_LOOP(
|
|
||||||
lock_ref,
|
|
||||||
{
|
|
||||||
if (old.count < 0)
|
|
||||||
return false;
|
|
||||||
++new.count;
|
|
||||||
},
|
|
||||||
{ return true; })
|
|
||||||
|
|
||||||
bool retval = false;
|
|
||||||
// 快捷路径操作失败,尝试加锁
|
|
||||||
spin_lock(&lock_ref->lock);
|
|
||||||
if (lock_ref->count >= 0)
|
|
||||||
{
|
|
||||||
++lock_ref->count;
|
|
||||||
retval = true;
|
|
||||||
}
|
|
||||||
spin_unlock(&lock_ref->lock);
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
File diff suppressed because it is too large
Load Diff
@ -32,18 +32,6 @@ macro_rules! println {
|
|||||||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 指定颜色,彩色输出
|
|
||||||
/// @param FRcolor 前景色
|
|
||||||
/// @param BKcolor 背景色
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! printk_color {
|
|
||||||
|
|
||||||
($FRcolor:expr, $BKcolor:expr, $($arg:tt)*) => {
|
|
||||||
use alloc;
|
|
||||||
$crate::libs::printk::PrintkWriter.__write_string_color($FRcolor, $BKcolor, alloc::fmt::format(format_args!($($arg)*)).as_str())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! kdebug {
|
macro_rules! kdebug {
|
||||||
($($arg:tt)*) => {
|
($($arg:tt)*) => {
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
#include <common/stdlib.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 将长整型转换为字符串
|
|
||||||
*
|
|
||||||
* @param input 输入的数据
|
|
||||||
* @return const char* 结果字符串
|
|
||||||
*/
|
|
||||||
const char *ltoa(long input)
|
|
||||||
{
|
|
||||||
/* large enough for -9223372036854775808 */
|
|
||||||
static char buffer[21] = {0};
|
|
||||||
char *pos = buffer + sizeof(buffer) - 1;
|
|
||||||
int neg = input < 0;
|
|
||||||
unsigned long n = neg ? -input : input;
|
|
||||||
|
|
||||||
*pos-- = '\0';
|
|
||||||
do
|
|
||||||
{
|
|
||||||
*pos-- = '0' + n % 10;
|
|
||||||
n /= 10;
|
|
||||||
if (pos < buffer)
|
|
||||||
return pos + 1;
|
|
||||||
} while (n);
|
|
||||||
|
|
||||||
if (neg)
|
|
||||||
*pos-- = '-';
|
|
||||||
return pos + 1;
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
|
|
||||||
CFLAGS += -I .
|
|
||||||
|
|
||||||
kernel_lib_sys_objs:= $(shell find ./*.c)
|
|
||||||
|
|
||||||
ECHO:
|
|
||||||
@echo "$@"
|
|
||||||
|
|
||||||
$(kernel_lib_sys_objs): ECHO
|
|
||||||
$(CC) $(CFLAGS) -c $@ -o $@.o
|
|
||||||
|
|
||||||
all: $(kernel_lib_sys_objs)
|
|
||||||
@echo $(kernel_lib_sys_objs)
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
|||||||
#include <syscall/syscall_num.h>
|
|
||||||
#include <syscall/syscall.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 等待指定pid的子进程退出
|
|
||||||
*
|
|
||||||
* @param pid 子进程的pid
|
|
||||||
* @param stat_loc 返回的子进程结束状态
|
|
||||||
* @param options 额外的控制选项
|
|
||||||
* @return pid_t
|
|
||||||
*/
|
|
||||||
pid_t waitpid(pid_t pid, int *stat_loc, int options)
|
|
||||||
{
|
|
||||||
return (pid_t)enter_syscall_int(SYS_WAIT4, (uint64_t)pid, (uint64_t)stat_loc, options, 0, 0, 0);
|
|
||||||
}
|
|
@ -14,6 +14,7 @@ use crate::{
|
|||||||
resource::{RLimit64, RUsage},
|
resource::{RLimit64, RUsage},
|
||||||
ProcessManager,
|
ProcessManager,
|
||||||
},
|
},
|
||||||
|
syscall::user_access::check_and_clone_cstr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
@ -58,10 +59,6 @@ pub const SYS_SCHED: usize = 100003;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Syscall;
|
pub struct Syscall;
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
fn do_put_string(s: *const u8, front_color: u32, back_color: u32) -> usize;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Syscall {
|
impl Syscall {
|
||||||
/// 初始化系统调用
|
/// 初始化系统调用
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
@ -1047,7 +1044,16 @@ impl Syscall {
|
|||||||
front_color: u32,
|
front_color: u32,
|
||||||
back_color: u32,
|
back_color: u32,
|
||||||
) -> Result<usize, SystemError> {
|
) -> Result<usize, SystemError> {
|
||||||
return Ok(unsafe { do_put_string(s, front_color, back_color) });
|
// todo: 删除这个系统调用
|
||||||
|
let s = check_and_clone_cstr(s, Some(4096))?;
|
||||||
|
let fr = (front_color & 0x00ff0000) >> 16;
|
||||||
|
let fg = (front_color & 0x0000ff00) >> 8;
|
||||||
|
let fb = front_color & 0x000000ff;
|
||||||
|
let br = (back_color & 0x00ff0000) >> 16;
|
||||||
|
let bg = (back_color & 0x0000ff00) >> 8;
|
||||||
|
let bb = back_color & 0x000000ff;
|
||||||
|
print!("\x1B[38;2;{fr};{fg};{fb};48;2;{br};{bg};{bb}m{s}\x1B[0m");
|
||||||
|
return Ok(s.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reboot() -> Result<usize, SystemError> {
|
pub fn reboot() -> Result<usize, SystemError> {
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#include "syscall.h"
|
#include "syscall.h"
|
||||||
|
#include <arch/arch.h>
|
||||||
#include <common/errno.h>
|
#include <common/errno.h>
|
||||||
#include <common/fcntl.h>
|
#include <common/fcntl.h>
|
||||||
#include <common/string.h>
|
#include <common/string.h>
|
||||||
#include <mm/slab.h>
|
#include <mm/slab.h>
|
||||||
#include <process/process.h>
|
#include <process/process.h>
|
||||||
#include <time/sleep.h>
|
#include <time/sleep.h>
|
||||||
#include <arch/arch.h>
|
|
||||||
|
|
||||||
#if ARCH(I386) || ARCH(X86_64)
|
#if ARCH(I386) || ARCH(X86_64)
|
||||||
// 导出系统调用入口函数,定义在entry.S中
|
// 导出系统调用入口函数,定义在entry.S中
|
||||||
@ -26,8 +26,8 @@ extern void syscall_int(void);
|
|||||||
* @return long
|
* @return long
|
||||||
*/
|
*/
|
||||||
|
|
||||||
long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5)
|
long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3,
|
||||||
{
|
ul arg4, ul arg5) {
|
||||||
long err_code;
|
long err_code;
|
||||||
__asm__ __volatile__("movq %2, %%rdi \n\t"
|
__asm__ __volatile__("movq %2, %%rdi \n\t"
|
||||||
"movq %3, %%rsi \n\t"
|
"movq %3, %%rsi \n\t"
|
||||||
@ -37,37 +37,19 @@ long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg
|
|||||||
"movq %7, %%r9 \n\t"
|
"movq %7, %%r9 \n\t"
|
||||||
"int $0x80 \n\t"
|
"int $0x80 \n\t"
|
||||||
: "=a"(err_code)
|
: "=a"(err_code)
|
||||||
: "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2), "m"(arg3), "m"(arg4), "m"(arg5)
|
: "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2),
|
||||||
|
"m"(arg3), "m"(arg4), "m"(arg5)
|
||||||
: "memory", "r8", "r9", "r10", "rdi", "rsi", "rdx");
|
: "memory", "r8", "r9", "r10", "rdi", "rsi", "rdx");
|
||||||
|
|
||||||
return err_code;
|
return err_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5)
|
long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3,
|
||||||
{
|
ul arg4, ul arg5) {
|
||||||
while (1)
|
while (1) {
|
||||||
{
|
|
||||||
/* code */
|
/* code */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 打印字符串的系统调用
|
|
||||||
*
|
|
||||||
* 当arg1和arg2均为0时,打印黑底白字,否则按照指定的前景色和背景色来打印
|
|
||||||
*
|
|
||||||
* @param regs 寄存器
|
|
||||||
* @param arg0 要打印的字符串
|
|
||||||
* @param arg1 前景色
|
|
||||||
* @param arg2 背景色
|
|
||||||
* @return ul 返回值
|
|
||||||
*/
|
|
||||||
ul do_put_string(char *s, uint32_t front_color, uint32_t background_color)
|
|
||||||
{
|
|
||||||
|
|
||||||
printk_color(front_color, background_color, s);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user