mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-08 18:26:48 +00:00
parent
196b75dc17
commit
701589559f
@ -254,433 +254,3 @@
|
||||
| 不满 | 0 |
|
||||
|
||||
------------------
|
||||
|
||||
## ID Allocation
|
||||
|
||||
   ida的主要作用是分配+管理id. 它能分配一个最小的, 未被分配出去的id. 当您需要管理某个数据结构时, 可能需要使用id来区分不同的目标. 这个时候, ida将会是很好的选择. 因为ida的十分高效, 运行常数相对数组更小, 而且提供了基本管理id需要用到的功能, 值得您试一试.
|
||||
|
||||
  IDA定义于`idr.h`文件中. 您通过`DECLARE_IDA(my_ida)`来创建一个ida对象, 或者`struct ida my_ida; ida_init(&my_ida);`来初始化一个ida.
|
||||
|
||||
### ida_init
|
||||
`void ida_init(struct ida *ida_p)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  通初始化IDA, 你需要保证调用函数之前, ida的free_list为空, 否则会导致内存泄漏.
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
  无返回值
|
||||
|
||||
### ida_preload
|
||||
`int ida_preload(struct ida *ida_p, gfp_t gfp_mask)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  为ida预分配空间.您可以不自行调用, 因为当ida需要空间的时候, 内部会自行使用`kmalloc`函数获取空间. 当然, 设计这个函数的目的是为了让您有更多的选择. 当您提前调用这个函数, 可以避免之后在开辟空间上的时间开销.
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
**gfp_mask**
|
||||
|
||||
   保留参数, 目前尚未使用.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  如果分配成功,将返回0; 否则返回负数错误码, 有可能是内存空间不够.
|
||||
|
||||
|
||||
### ida_alloc
|
||||
`int ida_alloc(struct ida *ida_p, int *p_id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  获取一个空闲ID. 您需要注意, 返回值是成功/错误码.
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
**p_id**
|
||||
|
||||
   您需要传入一个int变量的指针, 如果成功分配ID, ID将会存储在该指针所指向的地址.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  如果分配成功,将返回0; 否则返回负数错误码, 有可能是内存空间不够.
|
||||
|
||||
|
||||
### ida_count
|
||||
`bool ida_count(struct ida *ida_p, int id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  查询一个ID是否被分配.
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
**id**
|
||||
|
||||
   您查询该ID是否被分配.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  如果分配,将返回true; 否则返回false.
|
||||
|
||||
|
||||
|
||||
### ida_remove
|
||||
`void ida_remove(struct ida *ida_p, int id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  删除一个已经分配的ID. 如果该ID不存在, 该函数不会产生异常错误, 因为在检测到该ID不存在的时候, 函数将会自动退出.
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
**id**
|
||||
|
||||
   您要删除的id.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  无返回值.
|
||||
|
||||
### ida_destroy
|
||||
`void ida_destroy(struct ida *ida_p)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  释放一个IDA所有的空间, 同时删除ida的所有已经分配的id.(所以您不用担心删除id之后, ida还会占用大量空间.)
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
  无返回值
|
||||
|
||||
### ida_empty
|
||||
`void ida_empty(struct ida *ida_p)`
|
||||
|
||||
#### 描述
|
||||
|
||||
   查询一个ida是否为空
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
  ida为空则返回true,否则返回false。
|
||||
|
||||
|
||||
--------------------
|
||||
|
||||
|
||||
## IDR
|
||||
|
||||
   idr是一个基于radix-tree的ID-pointer的数据结构. 该数据结构提供了建id与数据指针绑定的功能, 它的主要功能有以下4个:
|
||||
1. 获取一个ID, 并且将该ID与一个指针绑定
|
||||
2. 删除一个已分配的ID
|
||||
3. 根据ID查找对应的指针
|
||||
4. 根据ID使用新的ptr替换旧的ptr
|
||||
   您可以使用`DECLARE_idr(my_idr)`来创建一个idr。或者您也可以使用`struct idr my_idr; idr_init(my_idr);`这两句话创建一个idr。
|
||||
   至于什么是radix-tree,您可以把他简单理解为一个向上生长的多叉树,在实现中,我们选取了64叉树。
|
||||
|
||||
### idr_init
|
||||
`void idr_init(struct idr *idp)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  通初始化IDR, 你需要保证调用函数之前, idr的free_list为空, 否则会导致内存泄漏.
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
  无返回值
|
||||
|
||||
### idr_preload
|
||||
`int idr_preload(struct idr *idp, gfp_t gfp_mask)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  为idr预分配空间.您可以不自行调用, 因为当idr需要空间的时候, 内部会自行使用`kmalloc`函数获取空间. 当然, 设计这个函数的目的是为了让您有更多的选择. 当您提前调用这个函数, 可以避免之后在开辟空间上的时间开销.
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**gfp_mask**
|
||||
|
||||
   保留参数, 目前尚未使用.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  如果分配成功,将返回0; 否则返回负数错误码, 有可能是内存空间不够.
|
||||
|
||||
|
||||
### idr_alloc
|
||||
`int idr_alloc(struct idr *idp, void *ptr, int *id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
   获取一个空闲ID. 您需要注意, 返回值是成功/错误码.
|
||||
   调用这个函数,需要您保证ptr是非空的,即: `ptr != NULL`, 否则将会影响 `idr_find/idr_find_next/idr_find_next_getid/...`等函数的使用。(具体请看这三个函数的说明,当然,只会影响到您的使用体验,并不会影响到idr内部函数的决策和逻辑)
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向ida的指针
|
||||
|
||||
**ptr**
|
||||
|
||||
   指向数据的指针
|
||||
|
||||
**id**
|
||||
|
||||
   您需要传入一个int变量的指针, 如果成功分配ID, ID将会存储在该指针所指向的地址.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  如果分配成功,将返回0; 否则返回负数错误码, 有可能是内存空间不够.
|
||||
|
||||
|
||||
### idr_remove
|
||||
`void* idr_remove(struct idr *idp, int id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  删除一个id, 但是不释放对应的ptr指向的空间, 同时返回这个被删除id所对应的ptr。
|
||||
   如果该ID不存在, 该函数不会产生异常错误, 因为在检测到该ID不存在的时候, 函数将会自动退出,并返回NULL。
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**id**
|
||||
|
||||
   您要删除的id.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  如果删除成功,就返回被删除id所对应的ptr;否则返回NULL。注意:如果这个id本来就和NULL绑定,那么也会返回NULL
|
||||
|
||||
|
||||
### idr_remove_all
|
||||
`void idr_remove_all(struct idr *idp)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  删除idr的所有已经分配的id.(所以您不用担心删除id之后, idr还会占用大量空间。)
|
||||
|
||||
   但是你需要注意的是,调用这个函数是不会释放数据指针指向的空间的。 所以您调用该函数之前, 确保IDR内部的数据指针被保存。否则当IDR删除所有ID之后, 将会造成内存泄漏。
|
||||
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
  无返回值
|
||||
|
||||
|
||||
### idr_destroy
|
||||
`void idr_destroy(struct idr *idp)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  释放一个IDR所有的空间, 同时删除idr的所有已经分配的id.(所以您不用担心删除id之后, ida还会占用大量空间.) - 和`idr_remove_all`的区别是, 释放掉所有的空间(包括free_list的预分配空间)。
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
  无返回值
|
||||
|
||||
|
||||
### idr_find
|
||||
`void *idr_find(struct idr *idp, int id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  查询一个ID所绑定的数据指针
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**id**
|
||||
|
||||
   您查询该ID的数据指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
   如果分配,将返回该ID对应的数据指针; 否则返回NULL.(注意, 返回NULL不一定代表这ID不存在,有可能该ID就是与空指针绑定。)
|
||||
   当然,我们也提供了`idr_count`函数来判断id是否被分配,具体请查看idr_count介绍。
|
||||
|
||||
### idr_find_next
|
||||
`void *idr_find_next(struct idr *idp, int start_id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  传进一个start_id,返回满足 "id大于start_id的最小id" 所对应的数据指针。
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**start_id**
|
||||
|
||||
  您提供的ID限制
|
||||
|
||||
#### 返回值
|
||||
|
||||
   如果分配,将返回该ID对应的数据指针; 否则返回NULL.(注意, 返回NULL不一定代表这ID不存在,有可能该ID就是与空指针绑定。)
|
||||
   当然,我们也提供了`idr_count`函数来判断id是否被分配,具体请查看idr_count介绍。
|
||||
|
||||
|
||||
### idr_find_next_getid
|
||||
`void *idr_find_next_getid(struct idr *idp, int start_id, int *nextid)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  传进一个start_id,返回满足 "id大于start_id的最小id" 所对应的数据指针。同时,你获取到这个满足条件的最小id, 即参数中的 *nextid。
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**start_id**
|
||||
|
||||
   您提供的ID限制
|
||||
|
||||
#### 返回值
|
||||
|
||||
   如果分配,将返回该ID对应的数据指针; 否则返回NULL.(注意, 返回NULL不一定代表这ID不存在,有可能该ID就是与空指针绑定。)
|
||||
   当然,我们也提供了`idr_count`函数来判断id是否被分配,具体请查看idr_count介绍。
|
||||
|
||||
|
||||
### idr_replace
|
||||
`int idr_replace(struct idr *idp, void *ptr, int id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  传进一个ptr,使用该ptr替换掉id所对应的Old_ptr。
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**ptr**
|
||||
|
||||
  您要替换原来的old_ptr的新指针
|
||||
|
||||
**id**
|
||||
|
||||
   您要替换的指针所对应的id
|
||||
|
||||
#### 返回值
|
||||
|
||||
   0代表成功,否则就是错误码 - 代表错误。
|
||||
|
||||
|
||||
### idr_replace_get_old
|
||||
`int idr_replace_get_old(struct idr *idp, void *ptr, int id, void **oldptr)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  传进一个ptr,使用该ptr替换掉id所对应的Old_ptr,同时你可以获取到old_ptr。
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**ptr**
|
||||
|
||||
  您要替换原来的old_ptr的新指针
|
||||
|
||||
**id**
|
||||
|
||||
   您要替换的指针所对应的id
|
||||
|
||||
|
||||
**old_ptr**
|
||||
|
||||
   您需要传进该(void**)指针,old_ptr将会存放在该指针所指向的地址。
|
||||
|
||||
|
||||
#### 返回值
|
||||
|
||||
   0代表成功,否则就是错误码 - 代表错误。
|
||||
|
||||
### idr_empty
|
||||
`void idr_empty(struct idr *idp)`
|
||||
|
||||
#### 描述
|
||||
|
||||
   查询一个idr是否为空
|
||||
#### 参数
|
||||
|
||||
**idp**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
#### 返回值
|
||||
|
||||
  idr为空则返回true,否则返回false。
|
||||
|
||||
### idr_count
|
||||
`bool idr_count(struct idr *idp, int id)`
|
||||
|
||||
#### 描述
|
||||
|
||||
  查询一个ID是否被分配.
|
||||
#### 参数
|
||||
|
||||
**ida_p**
|
||||
|
||||
   指向idr的指针
|
||||
|
||||
**id**
|
||||
|
||||
   您查询该ID是否被分配.
|
||||
|
||||
#### 返回值
|
||||
|
||||
  如果分配,将返回true; 否则返回false.
|
@ -36,7 +36,7 @@ export ASFLAGS := --64
|
||||
LD_LIST := ""
|
||||
|
||||
|
||||
kernel_subdirs := common driver debug exception smp syscall ktest libs time
|
||||
kernel_subdirs := common driver debug exception smp syscall libs time
|
||||
|
||||
|
||||
kernel_rust:
|
||||
|
@ -1,79 +0,0 @@
|
||||
#pragma once
|
||||
#include <common/glib.h>
|
||||
|
||||
struct bt_node_t
|
||||
{
|
||||
struct bt_node_t *left;
|
||||
struct bt_node_t *right;
|
||||
struct bt_node_t *parent;
|
||||
void *value; // 数据
|
||||
|
||||
} __attribute__((aligned(sizeof(long))));
|
||||
|
||||
struct bt_root_t
|
||||
{
|
||||
struct bt_node_t *bt_node;
|
||||
int32_t size; // 树中的元素个数
|
||||
int (*cmp)(void *a, void *b); // 比较函数 a>b 返回1, a==b返回0, a<b返回-1
|
||||
/**
|
||||
* @brief 释放结点的value的函数
|
||||
* @param value 结点的值
|
||||
*/
|
||||
int (*release)(void *value);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 创建二叉搜索树
|
||||
*
|
||||
* @param node 根节点
|
||||
* @param cmp 比较函数
|
||||
* @param release 用来释放结点的value的函数
|
||||
* @return struct bt_root_t* 树根结构体
|
||||
*/
|
||||
struct bt_root_t *bt_create_tree(struct bt_node_t *node, int (*cmp)(void *a, void *b), int (*release)(void *value));
|
||||
|
||||
/**
|
||||
* @brief 创建结点
|
||||
*
|
||||
* @param left 左子节点
|
||||
* @param right 右子节点
|
||||
* @param value 当前节点的值
|
||||
* @return struct bt_node_t*
|
||||
*/
|
||||
struct bt_node_t *bt_create_node(struct bt_node_t *left, struct bt_node_t *right, struct bt_node_t *parent, void *value);
|
||||
|
||||
/**
|
||||
* @brief 插入结点
|
||||
*
|
||||
* @param root 树根结点
|
||||
* @param value 待插入结点的值
|
||||
* @return int 返回码
|
||||
*/
|
||||
int bt_insert(struct bt_root_t *root, void *value);
|
||||
|
||||
/**
|
||||
* @brief 搜索值为value的结点
|
||||
*
|
||||
* @param root 树根结点
|
||||
* @param value 值
|
||||
* @param ret_addr 返回的结点基地址
|
||||
* @return int 错误码
|
||||
*/
|
||||
int bt_query(struct bt_root_t *root, void *value, uint64_t *ret_addr);
|
||||
|
||||
/**
|
||||
* @brief 删除结点
|
||||
*
|
||||
* @param root 树根
|
||||
* @param value 待删除结点的值
|
||||
* @return int 返回码
|
||||
*/
|
||||
int bt_delete(struct bt_root_t *root, void *value);
|
||||
|
||||
/**
|
||||
* @brief 释放整个二叉搜索树
|
||||
*
|
||||
* @param root
|
||||
* @return int
|
||||
*/
|
||||
int bt_destroy_tree(struct bt_root_t *root);
|
@ -1,179 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if ARCH(I386) || ARCH(X86_64)
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize("O1")
|
||||
|
||||
|
||||
#include <common/errno.h>
|
||||
#include <common/spinlock.h>
|
||||
|
||||
#if ARCH(I386) || ARCH(X86_64)
|
||||
#include <arch/x86_64/math/bitcount.h>
|
||||
#else
|
||||
#error Arch not supported.
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* idr: 基于radix-tree的ID-pointer的数据结构
|
||||
* 主要功能:
|
||||
* 1. 获取一个ID, 并且将该ID与一个指针绑定 - 需要外部加锁
|
||||
* 2. 删除一个已分配的ID - 需要外部加锁
|
||||
* 3. 根据ID查找对应的指针 (读操作,看情况加锁)
|
||||
* 4. 根据ID使用新的ptr替换旧的ptr - 需要外部加锁
|
||||
*
|
||||
* 附加功能:
|
||||
* 1. 给定starting_id, 查询下一个已分配的next_id (即:next_id>starting_id)
|
||||
* 2. 销毁整个idr
|
||||
*
|
||||
*
|
||||
* .... 待实现
|
||||
*/
|
||||
|
||||
// 默认64位机器
|
||||
#define IDR_BITS 6
|
||||
#define IDR_FULL 0xfffffffffffffffful
|
||||
|
||||
// size = 64
|
||||
#define IDR_SIZE (1 << IDR_BITS)
|
||||
#define IDR_MASK ((1 << IDR_BITS) - 1)
|
||||
|
||||
// 能管理的ID范围[0:1<<31]
|
||||
#define MAX_ID_SHIFT (sizeof(int) * 8 - 1)
|
||||
#define MAX_ID_BIT (1U << MAX_ID_SHIFT)
|
||||
#define MAX_ID_MASK (MAX_ID_BIT - 1)
|
||||
|
||||
// IDR可能最大的层次 以及 IDR预分配空间的最大限制
|
||||
#define MAX_LEVEL ((MAX_ID_SHIFT + IDR_BITS - 1) / IDR_BITS)
|
||||
#define IDR_FREE_MAX (MAX_LEVEL << 1)
|
||||
|
||||
// 给定layer, 计算完全64叉树的大小
|
||||
#define TREE_SIZE(layer) ((layer >= 0) ? (1ull << ((layer + 1) * IDR_BITS)) : 1)
|
||||
|
||||
// 计算最后(最低位)一个1的位置 (注意使用64位的版本)
|
||||
#define __lowbit_id(x) ((x) ? (__ctzll(x)) : -1)
|
||||
|
||||
// 计算最前(最高位)一个1的位置 (注意使用64位的版本)
|
||||
#define __mostbit_id(x) ((x) ? (63 - __clzll(x)) : -1)
|
||||
|
||||
// radix-tree 节点定义
|
||||
struct idr_layer
|
||||
{
|
||||
struct idr_layer *ary[IDR_SIZE]; // IDR_SIZE叉树
|
||||
unsigned long bitmap; // 每一位表示这个子树是否被使用
|
||||
unsigned long full; // 64个儿子子树, 每一位代表一个子树是否满了
|
||||
int layer; // 层数(从底向上)
|
||||
};
|
||||
|
||||
// idr: 将id与pointer绑定的数据结构
|
||||
struct idr
|
||||
{
|
||||
struct idr_layer *top;
|
||||
struct idr_layer *free_list;
|
||||
int id_free_cnt;
|
||||
spinlock_t lock;
|
||||
}__attribute__((aligned(8)));
|
||||
|
||||
#define DECLARE_IDR(name) \
|
||||
struct idr name = {0}; \
|
||||
idr_init(&(name));
|
||||
|
||||
#define DECLARE_IDR_LAYER(name) \
|
||||
struct idr_layer name = {0}; \
|
||||
memset(name, 0, sizeof(struct idr_layer));
|
||||
|
||||
/**
|
||||
* 对外函数声明
|
||||
**/
|
||||
int idr_preload(struct idr *idp, gfp_t gfp_mask);
|
||||
int idr_alloc(struct idr *idp, void *ptr, int *id);
|
||||
void *idr_remove(struct idr *idp, int id);
|
||||
void idr_remove_all(struct idr *idp);
|
||||
void idr_destroy(struct idr *idp);
|
||||
void *idr_find(struct idr *idp, int id);
|
||||
void *idr_find_next(struct idr *idp, int start_id);
|
||||
void *idr_find_next_getid(struct idr *idp, int64_t start_id, int *nextid);
|
||||
int idr_replace_get_old(struct idr *idp, void *ptr, int id, void **oldptr);
|
||||
int idr_replace(struct idr *idp, void *ptr, int id);
|
||||
void idr_init(struct idr *idp);
|
||||
bool idr_empty(struct idr *idp);
|
||||
bool idr_count(struct idr *idp, int id);
|
||||
|
||||
/**
|
||||
* 对外宏:遍历idr两种方式:
|
||||
* 1. 从第一个元素开始遍历
|
||||
* 2. 从某一个id开始遍历
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief 第一种遍历方式: 从第一个元素开始遍历
|
||||
* @param idp idr指针
|
||||
* @param id 遍历的id,你不需要初始化这个id,因为它每一次都是从最小已分配的id开始遍历
|
||||
* @param ptr 数据指针(entry),你不需要初始化这个指针
|
||||
*/
|
||||
#define for_each_idr_entry(idp, id, ptr) \
|
||||
for (id = -1, ptr = idr_find_next_getid(idp, id, &id); ptr != NULL || !idr_count(idp, id); ptr = idr_find_next_getid(idp, id, &id))
|
||||
|
||||
/**
|
||||
* @brief 第二种遍历方式: 从某一个id开始遍历
|
||||
* @param idp idr指针
|
||||
* @param id 遍历的id,你需要初始化这个id(请你设置为你要从哪一个id开始遍历,遍历过程将会包括这个id)
|
||||
* @param ptr 数据指针(entry),你不需要初始化这个指针
|
||||
*/
|
||||
#define for_each_idr_entry_continue(idp, id, ptr) \
|
||||
for (ptr = idr_find_next_getid(idp, id - 1, &id); ptr != NULL || !idr_count(idp, id); ptr = idr_find_next_getid(idp, id, &id))
|
||||
|
||||
/**
|
||||
* ida: 基于IDR实现的ID分配器
|
||||
* 主要功能:
|
||||
* 1. 获取一个未分配的ID
|
||||
* 2. 询问一个ID是否被分配
|
||||
* 3. 删除一个已分配ID
|
||||
*
|
||||
* 附加功能:
|
||||
* 1. 暂定
|
||||
*/
|
||||
|
||||
// 一个块的大小 - 即 sizeof(struct ida_bitmap)
|
||||
#define IDA_CHUNK_SIZE 128
|
||||
// ida_bitmap的长度
|
||||
#define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long) - 1)
|
||||
// 对应linux的IDA_BITMAP_BITS = 960 = 15 * 64
|
||||
#define IDA_FULL (IDA_BITMAP_LONGS * sizeof(long) * 8)
|
||||
#define IDA_BITMAP_BITS IDA_FULL
|
||||
#define IDA_BMP_SIZE (8 * sizeof(long))
|
||||
|
||||
// 自定义bitmap
|
||||
struct ida_bitmap
|
||||
{
|
||||
unsigned long count; // bitmap中已经分配的id数量
|
||||
unsigned long bitmap[IDA_BITMAP_LONGS]; // bitmap本身, 每一个bit代表一个ID
|
||||
};
|
||||
|
||||
// id-allocater 管理+分配ID的数据结构
|
||||
struct ida
|
||||
{
|
||||
struct idr idr;
|
||||
struct ida_bitmap *free_list; // 预分配的数据块
|
||||
};
|
||||
|
||||
#define DECLARE_IDA(name) \
|
||||
struct ida name = {0}; \
|
||||
idr_init(&name.idr); \
|
||||
name.free_list = (NULL);
|
||||
|
||||
/**
|
||||
* 对外函数声明
|
||||
*/
|
||||
void ida_init(struct ida *ida_p);
|
||||
bool ida_empty(struct ida *ida_p);
|
||||
int ida_preload(struct ida *ida_p, gfp_t gfp_mask);
|
||||
int ida_alloc(struct ida *ida_p, int *p_id);
|
||||
bool ida_count(struct ida *ida_p, int id);
|
||||
void ida_remove(struct ida *ida_p, int id);
|
||||
void ida_destroy(struct ida *ida_p);
|
||||
|
||||
#pragma GCC pop_options
|
||||
|
||||
#endif
|
@ -17,7 +17,6 @@
|
||||
#include <common/crc7.h>
|
||||
#include <common/crc8.h>
|
||||
#include <common/glib.h>
|
||||
#include <common/idr.h>
|
||||
#include <common/kfifo.h>
|
||||
#include <common/lz4.h>
|
||||
#include <common/printk.h>
|
||||
|
@ -1,10 +0,0 @@
|
||||
SRC = $(wildcard *.c)
|
||||
OBJ = $(SRC:.c=.o)
|
||||
CFLAGS += -I .
|
||||
|
||||
.PHONY: all
|
||||
|
||||
all: $(OBJ)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
@ -1,15 +0,0 @@
|
||||
#include "ktest.h"
|
||||
#include <process/process.h>
|
||||
|
||||
/**
|
||||
* @brief 开启一个新的内核线程以进行测试
|
||||
*
|
||||
* @param func 测试函数
|
||||
* @param arg 传递给测试函数的参数
|
||||
* @return pid_t 测试内核线程的pid
|
||||
*/
|
||||
pid_t ktest_start(int (*func)(void* arg), void* arg)
|
||||
{
|
||||
kerror("Fix me: ktest_start, to use new process management.");
|
||||
while(1);
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
#include <common/sys/types.h>
|
||||
|
||||
int ktest_test_bitree(void* arg);
|
||||
int ktest_test_kfifo(void* arg);
|
||||
int ktest_test_mutex(void* arg);
|
||||
int ktest_test_idr(void* arg);
|
||||
int ktest_test_kvm(void* arg);
|
||||
|
||||
/**
|
||||
* @brief 开启一个新的内核线程以进行测试
|
||||
*
|
||||
* @param func 测试函数
|
||||
* @param arg 传递给测试函数的参数
|
||||
* @return pid_t 测试内核线程的pid
|
||||
*/
|
||||
pid_t ktest_start(int (*func)(void* arg), void* arg);
|
@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <common/printk.h>
|
||||
#include <common/compiler.h>
|
||||
|
||||
#define assert(condition) ({ \
|
||||
int __condition = !!(condition); \
|
||||
if (unlikely(!(__condition))) \
|
||||
{ \
|
||||
printk("[ kTEST FAILED ] Ktest Assertion Failed, file:%s, Line:%d\n", __FILE__, __LINE__); \
|
||||
} \
|
||||
likely(__condition); \
|
||||
})
|
||||
|
||||
#define kTEST(...) \
|
||||
do \
|
||||
{ \
|
||||
printk("[ kTEST ] file:%s, Line:%d\t", __FILE__, __LINE__); \
|
||||
printk(__VA_ARGS__); \
|
||||
printk("\n"); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @brief 测试用例函数表
|
||||
*
|
||||
*/
|
||||
typedef long (*ktest_case_table)(uint64_t arg0, uint64_t arg1);
|
@ -1,133 +0,0 @@
|
||||
#include "ktest.h"
|
||||
#include <ktest/ktest_utils.h>
|
||||
|
||||
#include <common/unistd.h>
|
||||
#include <common/kprint.h>
|
||||
#include <common/bitree.h>
|
||||
#include <common/errno.h>
|
||||
|
||||
#include <mm/slab.h>
|
||||
|
||||
struct test_value_t
|
||||
{
|
||||
uint64_t tv;
|
||||
};
|
||||
static int compare(void *a, void *b)
|
||||
{
|
||||
if (((struct test_value_t *)a)->tv > ((struct test_value_t *)b)->tv)
|
||||
return 1;
|
||||
else if (((struct test_value_t *)a)->tv == ((struct test_value_t *)b)->tv)
|
||||
return 0;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int release(void *value)
|
||||
{
|
||||
// kdebug("release");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 测试创建二叉树
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
static long ktest_bitree_case1(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
int val;
|
||||
// ========== 测试创建树
|
||||
struct test_value_t *tv1 = (struct test_value_t *)kmalloc(sizeof(struct test_value_t), 0);
|
||||
tv1->tv = 20;
|
||||
struct bt_node_t *rn = bt_create_node(NULL, NULL, NULL, tv1);
|
||||
|
||||
assert(rn != NULL);
|
||||
assert((int64_t)rn != (-EINVAL));
|
||||
assert(rn->value == tv1);
|
||||
|
||||
struct bt_root_t *tree = bt_create_tree(rn, compare, release);
|
||||
assert(tree != NULL);
|
||||
assert(tree->bt_node == rn);
|
||||
assert(tree->cmp == compare);
|
||||
assert(tree->release == release);
|
||||
assert(tree->size == 1);
|
||||
|
||||
// ========= 向树中插入数据10、30
|
||||
struct test_value_t *tv2 = (struct test_value_t *)kmalloc(sizeof(struct test_value_t), 0);
|
||||
assert(tv2 != NULL);
|
||||
tv2->tv = 10;
|
||||
{
|
||||
int last_size = tree->size;
|
||||
val = bt_insert(tree, tv2);
|
||||
assert(val == 0);
|
||||
assert(last_size + 1 == tree->size);
|
||||
}
|
||||
struct test_value_t *tv3 = (struct test_value_t *)kmalloc(sizeof(struct test_value_t), 0);
|
||||
assert(tv3 != NULL);
|
||||
tv3->tv = 30;
|
||||
{
|
||||
int last_size = tree->size;
|
||||
val = bt_insert(tree, tv3);
|
||||
assert(val == 0);
|
||||
assert(last_size + 1 == tree->size);
|
||||
}
|
||||
|
||||
// 检测树的形状
|
||||
assert(((struct test_value_t *)tree->bt_node->left->value)->tv == tv2->tv);
|
||||
assert(((struct test_value_t *)tree->bt_node->right->value)->tv == tv3->tv);
|
||||
|
||||
// ========= 查询结点
|
||||
// 查询值为tv2的结点
|
||||
struct bt_node_t *node2;
|
||||
assert(bt_query(tree, tv2, (uint64_t*)(&node2)) == 0);
|
||||
assert(node2 != NULL);
|
||||
assert(node2->value == tv2);
|
||||
|
||||
// ========= 插入第4个结点:15
|
||||
struct test_value_t *tv4 = (struct test_value_t *)kmalloc(sizeof(struct test_value_t), 0);
|
||||
assert(tv4 != NULL);
|
||||
tv4->tv = 15;
|
||||
{
|
||||
int last_size = tree->size;
|
||||
val = bt_insert(tree, tv4);
|
||||
assert(val == 0);
|
||||
assert(last_size + 1 == tree->size);
|
||||
}
|
||||
|
||||
assert(((struct test_value_t *)node2->right->value)->tv == tv4->tv);
|
||||
|
||||
// ======= 查询不存在的值
|
||||
struct bt_node_t *node_not_exists;
|
||||
struct test_value_t *tv_not_exists = (struct test_value_t *)kmalloc(sizeof(struct test_value_t), 0);
|
||||
assert(tv_not_exists != NULL);
|
||||
tv_not_exists->tv = 100;
|
||||
assert(bt_query(tree, tv_not_exists, (uint64_t*)(&node_not_exists)) == -1);
|
||||
// kdebug("node_not_exists.val=%d", ((struct test_value_t*)node_not_exists->value)->tv);
|
||||
assert(node_not_exists == NULL);
|
||||
|
||||
// 删除根节点
|
||||
assert(bt_delete(tree, rn->value) == 0);
|
||||
assert(((struct test_value_t *)tree->bt_node->value)->tv != 20);
|
||||
assert(tree->bt_node->right == NULL);
|
||||
|
||||
// 删除树
|
||||
assert(bt_destroy_tree(tree) == 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ktest_case_table kt_bitree_func_table[] = {
|
||||
ktest_bitree_case1,
|
||||
};
|
||||
|
||||
int ktest_test_bitree(void* arg)
|
||||
{
|
||||
kTEST("Testing bitree...");
|
||||
for (int i = 0; i < sizeof(kt_bitree_func_table) / sizeof(ktest_case_table); ++i)
|
||||
{
|
||||
kTEST("Testing case %d", i);
|
||||
kt_bitree_func_table[i](0, 0);
|
||||
}
|
||||
kTEST("bitree Test done.");
|
||||
return 0;
|
||||
}
|
@ -1,599 +0,0 @@
|
||||
|
||||
#include <arch/arch.h>
|
||||
|
||||
#if ARCH(I386) || ARCH(X86_64)
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize("O1")
|
||||
#include "ktest.h"
|
||||
#include "ktest_utils.h"
|
||||
#include <common/idr.h>
|
||||
|
||||
/**
|
||||
* @brief 测试idr的构建,预获取空间是否成功
|
||||
*
|
||||
* 以下函数将被测试:
|
||||
* 1. idr_preload
|
||||
* 2. DECLARE_IDR
|
||||
* 3. idr_init
|
||||
* 4. idr_destroy
|
||||
*
|
||||
* 同时还会(间接)测试一些内部函数:
|
||||
* 1. move_to_free_list
|
||||
*
|
||||
* @param arg0
|
||||
* @param arg1
|
||||
*/
|
||||
static long ktest_idr_case0(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
unsigned long bitmap = -1;
|
||||
assert((int)(bitmap == IDR_FULL));
|
||||
|
||||
DECLARE_IDR(k_idr);
|
||||
assert(k_idr.top == NULL); // 刚被创建,必须是NULL
|
||||
assert(k_idr.id_free_cnt == 0); // 必须是0
|
||||
assert(k_idr.free_list == NULL);
|
||||
|
||||
k_idr.id_free_cnt = arg1;
|
||||
idr_init(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
|
||||
assert(idr_preload(&k_idr, 0) == 0);
|
||||
assert(k_idr.id_free_cnt == IDR_FREE_MAX);
|
||||
|
||||
for (uint64_t i = 1; i < 64; i++)
|
||||
{
|
||||
int id = __lowbit_id(i), chk_id = -1;
|
||||
for (int j = 0; j < 64; j++)
|
||||
if ((i >> j) & 1)
|
||||
{
|
||||
chk_id = j;
|
||||
break;
|
||||
}
|
||||
assert(id == chk_id);
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
assert(k_idr.top == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 测试id的获取,id的删除,id的全体删除, idr的find函数
|
||||
*
|
||||
* @param arg0
|
||||
* @param arg1
|
||||
*/
|
||||
static long ktest_idr_case1(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
DECLARE_IDR(k_idr);
|
||||
int a[128];
|
||||
|
||||
// 获取128个id
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
// 查询128个ptr
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, a[i]);
|
||||
assert(ptr == &a[i]);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == a[i]);
|
||||
}
|
||||
|
||||
// 倒序:删除64个id
|
||||
for (int i = 127; i >= 64; i--)
|
||||
{
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
// 正序:删除64个id
|
||||
for (int i = 0; i <= 63; i++)
|
||||
{
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_count(&k_idr, i) == 0);
|
||||
}
|
||||
|
||||
// 重新申请128个id, 值域范围应该仍然是[0,127]
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_count(&k_idr, i));
|
||||
}
|
||||
|
||||
// 正序:删除32个id
|
||||
for (int i = 0; i <= 31; i++)
|
||||
{
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
// 倒序:删除32个id
|
||||
for (int i = 127; i >= 96; i--)
|
||||
{
|
||||
int *id = idr_remove(&k_idr, a[i]);
|
||||
assert(id != NULL);
|
||||
assert(*id == i);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
// 整体删除
|
||||
idr_remove_all(&k_idr);
|
||||
assert(k_idr.top == NULL);
|
||||
|
||||
// 获取128个id
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
// 查询128个ptr
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, a[i]);
|
||||
assert(ptr == &a[i]);
|
||||
assert(*ptr == a[i]);
|
||||
}
|
||||
|
||||
// 正序:删除64个id
|
||||
for (int i = 0; i <= 63; i++)
|
||||
{
|
||||
idr_remove(&k_idr, a[i]);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
// 倒序:删除64个id
|
||||
for (int i = 127; i >= 64; i--)
|
||||
{
|
||||
idr_remove(&k_idr, a[i]);
|
||||
assert(idr_find(&k_idr, a[i]) == NULL);
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief case1 的大数据测试
|
||||
*
|
||||
* @param arg0
|
||||
* @param arg1
|
||||
*/
|
||||
static long ktest_idr_case2(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
DECLARE_IDR(k_idr);
|
||||
|
||||
// 获取 1000‘000 个ID
|
||||
const int N = 1048576;
|
||||
// const int N = 1048576;
|
||||
const int M = 2e5;
|
||||
|
||||
int tmp = 0;
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
barrier();
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
barrier();
|
||||
assert(tmp == i);
|
||||
|
||||
barrier();
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
barrier();
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == i);
|
||||
|
||||
barrier();
|
||||
// if (i >= 7255) kdebug("1e6 !!!!!!! : %d", i);
|
||||
assert(idr_count(&k_idr, i));
|
||||
barrier();
|
||||
}
|
||||
// kdebug("111111");
|
||||
// 正向: M 个ID
|
||||
for (int i = 0; i < M; i++)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == N - 1);
|
||||
idr_remove(&k_idr, i);
|
||||
assert(idr_find(&k_idr, i) == NULL);
|
||||
}
|
||||
// kdebug("22222");
|
||||
|
||||
// 倒序: N-M 个ID
|
||||
for (int i = (N)-1; i >= M; i--)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
assert(*ptr == N - 1);
|
||||
idr_remove(&k_idr, i);
|
||||
assert(idr_find(&k_idr, i) == NULL);
|
||||
}
|
||||
// kdebug("3333333");
|
||||
// 重新插入数据
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(tmp == i);
|
||||
assert(k_idr.top != NULL);
|
||||
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == i);
|
||||
}
|
||||
// kdebug("4444444444");
|
||||
assert(k_idr.top != NULL);
|
||||
|
||||
for (int i = 0; i < M; i++)
|
||||
{
|
||||
assert(idr_replace(&k_idr, NULL, i) == 0);
|
||||
}
|
||||
// kdebug("555555555555555555");
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
// kdebug("666666666666");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief case1 的大数据测试
|
||||
*
|
||||
* @param arg0
|
||||
* @param arg1
|
||||
*/
|
||||
static long ktest_idr_case3(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
DECLARE_IDR(k_idr);
|
||||
|
||||
const int N = 1949;
|
||||
int tmp;
|
||||
|
||||
// 获取ID
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(tmp == i);
|
||||
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == i);
|
||||
}
|
||||
|
||||
// 查询 nextid
|
||||
for (int i = 1; i <= N; i++)
|
||||
{
|
||||
int nextid;
|
||||
int *ptr = idr_find_next_getid(&k_idr, i - 1, &nextid);
|
||||
if (likely(i < N))
|
||||
{
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == N - 1);
|
||||
assert(nextid == i);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(ptr == NULL);
|
||||
assert(nextid == -1);
|
||||
}
|
||||
}
|
||||
|
||||
int sz = N;
|
||||
// 删掉某一段
|
||||
for (int i = N / 3, j = 2 * (N / 3), k = 0; i <= j; k++, i++)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, i);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == N - 1);
|
||||
idr_remove(&k_idr, i);
|
||||
|
||||
assert(idr_find(&k_idr, i) == NULL);
|
||||
sz--;
|
||||
assert(k_idr.top != NULL);
|
||||
}
|
||||
|
||||
// 查询 nextid
|
||||
for (int i = 1; i <= N; i++)
|
||||
{
|
||||
int nextid;
|
||||
int *ptr = idr_find_next_getid(&k_idr, i - 1, &nextid);
|
||||
if (likely(i < N))
|
||||
{
|
||||
int target = i < N / 3 ? i : max(i, 2 * (N / 3) + 1);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == N - 1);
|
||||
assert(nextid == target);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(ptr == NULL);
|
||||
assert(nextid == -1);
|
||||
}
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 更加全面覆盖所有函数 - 小数据测试
|
||||
*
|
||||
* @param arg0
|
||||
* @param arg1
|
||||
*/
|
||||
static long ktest_idr_case4(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
DECLARE_IDR(k_idr);
|
||||
idr_init(&k_idr);
|
||||
|
||||
const int N = 91173;
|
||||
static uint32_t tmp;
|
||||
|
||||
for (int i = 1; i <= 20; i++)
|
||||
{
|
||||
int M = N / i, T = M / 3, b = 2 * T;
|
||||
for (int j = 0; j < M; j++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &tmp, &tmp) == 0);
|
||||
assert(tmp == j);
|
||||
}
|
||||
|
||||
for (int j = b; j >= T; j--)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, j);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == M - 1);
|
||||
idr_remove(&k_idr, j);
|
||||
}
|
||||
|
||||
for (int j = b + 1; j < M; j++)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, j);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == M - 1);
|
||||
idr_remove(&k_idr, j);
|
||||
}
|
||||
|
||||
for (int j = T - 1; j >= 0; j--)
|
||||
{
|
||||
int *ptr = idr_find(&k_idr, j);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == M - 1);
|
||||
idr_remove(&k_idr, j);
|
||||
}
|
||||
|
||||
assert(k_idr.top == NULL);
|
||||
assert(idr_empty(&k_idr));
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
assert(idr_empty(&k_idr));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 测试id的获取,id的删除,id的全体删除, idr的find函数
|
||||
*
|
||||
* @param arg0
|
||||
* @param arg1
|
||||
*/
|
||||
static long ktest_idr_case5(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
DECLARE_IDR(k_idr);
|
||||
const int N = 128;
|
||||
int a[N];
|
||||
|
||||
// 获取128个id
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
// 把id指向的指针向后移动一个单位
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int *ptr;
|
||||
int flags = idr_replace_get_old(&k_idr, &a[(i + 1) % N], i, (void *)&ptr);
|
||||
assert(flags == 0); // 0 是成功
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == i);
|
||||
|
||||
// 测试是否替换成功
|
||||
ptr = idr_find(&k_idr, i);
|
||||
assert(ptr != NULL);
|
||||
assert(*ptr == (i + 1) % N);
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
|
||||
// destroy之后,再获取128个id
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(idr_alloc(&k_idr, &a[i], &a[i]) == 0);
|
||||
assert(a[i] == i);
|
||||
}
|
||||
|
||||
// 销毁
|
||||
idr_destroy(&k_idr);
|
||||
assert(idr_empty(&k_idr));
|
||||
assert(k_idr.id_free_cnt == 0);
|
||||
assert(k_idr.free_list == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 测试ida的插入/删除
|
||||
*
|
||||
* @param arg0
|
||||
* @param arg1
|
||||
* @return long
|
||||
*/
|
||||
static long ktest_idr_case6(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
assert(IDA_BITMAP_LONGS != 0);
|
||||
assert(IDA_BMP_SIZE != 0);
|
||||
assert(IDA_FULL != 0);
|
||||
assert(IDA_BITMAP_BITS != 0);
|
||||
|
||||
DECLARE_IDA(k_ida);
|
||||
ida_init(&k_ida);
|
||||
io_sfence();
|
||||
|
||||
const int N = IDA_FULL * IDR_SIZE + 1;
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int p_id;
|
||||
io_sfence();
|
||||
assert(ida_alloc(&k_ida, &p_id) == 0);
|
||||
io_sfence();
|
||||
assert(p_id == i);
|
||||
io_sfence();
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
assert(ida_count(&k_ida, i) == 1);
|
||||
io_sfence();
|
||||
}
|
||||
|
||||
for (int i = N - 1; i >= 0; i--)
|
||||
{
|
||||
ida_remove(&k_ida, i);
|
||||
io_sfence();
|
||||
assert(ida_count(&k_ida, i) == 0);
|
||||
io_sfence();
|
||||
}
|
||||
|
||||
assert(k_ida.idr.top == NULL);
|
||||
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int p_id;
|
||||
io_sfence();
|
||||
assert(ida_alloc(&k_ida, &p_id) == 0);
|
||||
io_sfence();
|
||||
assert(p_id == i);
|
||||
io_sfence();
|
||||
}
|
||||
|
||||
assert(k_ida.idr.top != NULL);
|
||||
io_sfence();
|
||||
ida_destroy(&k_ida);
|
||||
io_sfence();
|
||||
assert(k_ida.idr.top == NULL);
|
||||
io_sfence();
|
||||
assert(k_ida.free_list == NULL);
|
||||
io_sfence();
|
||||
assert(ida_empty(&k_ida));
|
||||
io_sfence();
|
||||
|
||||
// 测试destroy之后能否重新获取ID
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
int p_id;
|
||||
io_sfence();
|
||||
assert(ida_alloc(&k_ida, &p_id) == 0);
|
||||
io_sfence();
|
||||
assert(p_id == i);
|
||||
io_sfence();
|
||||
}
|
||||
|
||||
for (int i = 0; i < N / 3; i++)
|
||||
{
|
||||
ida_remove(&k_ida, i);
|
||||
io_sfence();
|
||||
assert(ida_count(&k_ida, i) == 0);
|
||||
io_sfence();
|
||||
}
|
||||
|
||||
for (int i = 2 * N / 3; i < N; i++)
|
||||
{
|
||||
ida_remove(&k_ida, i);
|
||||
io_sfence();
|
||||
assert(ida_count(&k_ida, i) == 0);
|
||||
io_sfence();
|
||||
}
|
||||
|
||||
assert(k_ida.idr.top != NULL);
|
||||
io_sfence();
|
||||
ida_destroy(&k_ida);
|
||||
io_sfence();
|
||||
assert(k_ida.idr.top == NULL);
|
||||
io_sfence();
|
||||
assert(k_ida.free_list == NULL);
|
||||
io_sfence();
|
||||
assert(ida_empty(&k_ida));
|
||||
io_sfence();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ktest_case_table kt_idr_func_table[] = {
|
||||
ktest_idr_case0,
|
||||
ktest_idr_case1,
|
||||
ktest_idr_case2, // 为了加快启动速度, 暂时注释掉这个测试
|
||||
ktest_idr_case3,
|
||||
ktest_idr_case4,
|
||||
ktest_idr_case5,
|
||||
ktest_idr_case6,
|
||||
};
|
||||
|
||||
int ktest_test_idr(void *arg)
|
||||
{
|
||||
kTEST("Testing idr...");
|
||||
unsigned int sz = sizeof(kt_idr_func_table) / sizeof(ktest_case_table);
|
||||
for (int i = 0; i < sz; ++i)
|
||||
{
|
||||
kTEST("Testing case %d", i);
|
||||
kt_idr_func_table[i](i, i + 1);
|
||||
}
|
||||
kTEST("idr Test done.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#pragma GCC pop_options
|
||||
|
||||
#endif
|
@ -1,166 +0,0 @@
|
||||
#include "ktest.h"
|
||||
#include "ktest_utils.h"
|
||||
#include <common/kfifo.h>
|
||||
#include <common/kprint.h>
|
||||
#include <mm/slab.h>
|
||||
|
||||
static long ktest_kfifo_case0_1(uint64_t arg0, uint64_t arg1)
|
||||
{
|
||||
const int fifo_size = 256;
|
||||
// 创建kfifo(由kfifo申请内存)
|
||||
struct kfifo_t fifo;
|
||||
if (arg0 == 0)
|
||||
assert(kfifo_alloc(&fifo, fifo_size, 0) == 0);
|
||||
else
|
||||
{
|
||||
void *buf = kmalloc(fifo_size, 0);
|
||||
kfifo_init(&fifo, buf, fifo_size);
|
||||
}
|
||||
|
||||
assert(fifo.buffer != NULL);
|
||||
assert(fifo.total_size == fifo_size);
|
||||
assert(kfifo_total_size(&fifo) == fifo_size);
|
||||
assert(fifo.size == 0);
|
||||
assert(kfifo_size(&fifo) == 0);
|
||||
assert(fifo.in_offset == 0);
|
||||
assert(fifo.out_offset == 0);
|
||||
assert(kfifo_empty(&fifo) == 1);
|
||||
assert(kfifo_full(&fifo) == 0);
|
||||
|
||||
// 循环增加10个uint64_t
|
||||
for (int i = 1; i <= 10; ++i)
|
||||
{
|
||||
uint64_t tmp = i;
|
||||
assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
|
||||
}
|
||||
assert(fifo.in_offset == 10 * sizeof(uint64_t));
|
||||
assert(fifo.out_offset == 0);
|
||||
assert(fifo.size == 10 * sizeof(uint64_t));
|
||||
assert(fifo.total_size == fifo_size);
|
||||
|
||||
// 循环删除这10个uint64_t
|
||||
for (int i = 1; i <= 10; ++i)
|
||||
{
|
||||
uint64_t tmp = 0;
|
||||
assert(kfifo_out(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
|
||||
assert(tmp == i);
|
||||
assert(fifo.size == (10 - i) * sizeof(uint64_t));
|
||||
assert(fifo.in_offset == 10 * sizeof(uint64_t));
|
||||
assert(fifo.out_offset == i * sizeof(uint64_t));
|
||||
}
|
||||
|
||||
assert(fifo.in_offset == 10 * sizeof(uint64_t));
|
||||
assert(fifo.out_offset == 10 * sizeof(uint64_t));
|
||||
assert(fifo.in_offset == fifo.out_offset);
|
||||
assert(kfifo_empty(&fifo) == 1);
|
||||
|
||||
// reset
|
||||
kfifo_reset(&fifo);
|
||||
assert(fifo.in_offset == 0);
|
||||
assert(fifo.out_offset == 0);
|
||||
assert(fifo.size == 0);
|
||||
|
||||
// 测试插入31个元素
|
||||
for (int i = 1; i <= 31; ++i)
|
||||
{
|
||||
uint64_t tmp = i;
|
||||
assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
|
||||
}
|
||||
|
||||
assert(fifo.size == 31 * sizeof(uint64_t));
|
||||
assert(fifo.in_offset == 31 * sizeof(uint64_t));
|
||||
assert(fifo.out_offset == 0);
|
||||
|
||||
// 然后再尝试插入一个大小为2*sizeof(uint64_t)的元素
|
||||
{
|
||||
__int128_t tmp = 100;
|
||||
assert(kfifo_in(&fifo, &tmp, sizeof(__int128_t)) == 0);
|
||||
assert(fifo.size == 31 * sizeof(uint64_t));
|
||||
assert(fifo.in_offset == 31 * sizeof(uint64_t));
|
||||
assert(fifo.out_offset == 0);
|
||||
}
|
||||
// 插入一个uint64, 队列满
|
||||
{
|
||||
uint64_t tmp = 32;
|
||||
assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
|
||||
assert(kfifo_full(&fifo));
|
||||
assert(kfifo_empty(&fifo) == 0);
|
||||
assert(fifo.size == fifo.total_size);
|
||||
assert(fifo.in_offset == fifo_size);
|
||||
assert(fifo.out_offset == 0);
|
||||
}
|
||||
|
||||
// 取出之前的20个元素
|
||||
for (int i = 1; i <= 20; ++i)
|
||||
{
|
||||
uint64_t tmp = 0;
|
||||
assert(kfifo_out(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
|
||||
}
|
||||
assert(fifo.size == (fifo.total_size - 20 * sizeof(uint64_t)));
|
||||
assert(fifo.in_offset == fifo_size);
|
||||
assert(fifo.out_offset == 20 * sizeof(uint64_t));
|
||||
|
||||
// 插入10个元素,剩余10个空位
|
||||
{
|
||||
uint64_t tmp = 99;
|
||||
|
||||
assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
|
||||
assert(fifo.in_offset == 1 * sizeof(uint64_t));
|
||||
|
||||
for (int i = 1; i <= 9; ++i)
|
||||
{
|
||||
assert(kfifo_in(&fifo, &tmp, sizeof(uint64_t)) == sizeof(uint64_t));
|
||||
}
|
||||
assert(fifo.in_offset == 10 * sizeof(uint64_t));
|
||||
assert(fifo.size == 22 * sizeof(uint64_t));
|
||||
}
|
||||
|
||||
{
|
||||
// 取出20个
|
||||
char tmp[20 * sizeof(uint64_t)];
|
||||
assert(kfifo_out(&fifo, &tmp, 20 * sizeof(uint64_t)) == 20 * sizeof(uint64_t));
|
||||
assert(fifo.out_offset == 8 * sizeof(uint64_t));
|
||||
assert(fifo.size == 2 * (sizeof(uint64_t)));
|
||||
}
|
||||
|
||||
{
|
||||
// 插入25个
|
||||
char tmp[25 * sizeof(uint64_t)];
|
||||
assert(kfifo_in(&fifo, &tmp, 25 * sizeof(uint64_t)) == 25 * sizeof(uint64_t));
|
||||
assert(fifo.out_offset == 8 * sizeof(uint64_t));
|
||||
assert(fifo.size == 27 * sizeof(uint64_t));
|
||||
assert(fifo.in_offset == 3 * sizeof(uint64_t));
|
||||
}
|
||||
|
||||
// 测试reset out
|
||||
uint32_t prev_in_offset = fifo.in_offset;
|
||||
kfifo_reset_out(&fifo);
|
||||
assert(fifo.size == 0);
|
||||
assert(fifo.total_size == fifo_size);
|
||||
assert(fifo.in_offset == prev_in_offset);
|
||||
assert(fifo.out_offset == prev_in_offset);
|
||||
|
||||
// 测试释放
|
||||
if (arg0 == 0)
|
||||
{
|
||||
kfifo_free_alloc(&fifo);
|
||||
assert(fifo.buffer == NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ktest_case_table kt_kfifo_func_table[] = {
|
||||
ktest_kfifo_case0_1,
|
||||
};
|
||||
|
||||
int ktest_test_kfifo(void* 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;
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
#include "ktest.h"
|
||||
#include "ktest_utils.h"
|
||||
|
||||
static long ktest_kvm_case0_1(uint64_t arg0, uint64_t arg1){
|
||||
kTEST("Testing /dev/kvm device...");
|
||||
|
||||
}
|
||||
|
||||
static ktest_case_table kt_kvm_func_table[] = {
|
||||
ktest_kvm_case0_1,
|
||||
};
|
||||
|
||||
int ktest_test_kvm(void* arg)
|
||||
{
|
||||
kTEST("Testing kvm...");
|
||||
for (int i = 0; i < sizeof(kt_kvm_func_table) / sizeof(ktest_case_table); ++i)
|
||||
{
|
||||
kTEST("Testing case %d", i);
|
||||
kt_kvm_func_table[i](i, 0);
|
||||
}
|
||||
kTEST("kvm Test done.");
|
||||
return 0;
|
||||
}
|
@ -1,234 +0,0 @@
|
||||
#include <common/bitree.h>
|
||||
#include <mm/slab.h>
|
||||
#include <common/errno.h>
|
||||
#include <common/kfifo.h>
|
||||
#include <common/string.h>
|
||||
#include <debug/bug.h>
|
||||
|
||||
#define smaller(root, a, b) (root->cmp((a)->value, (b)->value) == -1)
|
||||
#define equal(root, a, b) (root->cmp((a)->value, (b)->value) == 0)
|
||||
#define greater(root, a, b) (root->cmp((a)->value, (b)->value) == 1)
|
||||
|
||||
/**
|
||||
* @brief 创建二叉搜索树
|
||||
*
|
||||
* @param node 根节点
|
||||
* @param cmp 比较函数
|
||||
* @param release 用来释放结点的value的函数
|
||||
* @return struct bt_root_t* 树根结构体
|
||||
*/
|
||||
struct bt_root_t *bt_create_tree(struct bt_node_t *node, int (*cmp)(void *a, void *b), int (*release)(void *value))
|
||||
{
|
||||
if (node == NULL || cmp == NULL)
|
||||
return (void*)-EINVAL;
|
||||
|
||||
struct bt_root_t *root = (struct bt_root_t *)kmalloc(sizeof(struct bt_root_t), 0);
|
||||
memset((void *)root, 0, sizeof(struct bt_root_t));
|
||||
root->bt_node = node;
|
||||
root->cmp = cmp;
|
||||
root->release = release;
|
||||
root->size = (node == NULL) ? 0 : 1;
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 创建结点
|
||||
*
|
||||
* @param left 左子节点
|
||||
* @param right 右子节点
|
||||
* @param value 当前节点的值
|
||||
* @return struct bt_node_t*
|
||||
*/
|
||||
struct bt_node_t *bt_create_node(struct bt_node_t *left, struct bt_node_t *right, struct bt_node_t *parent, void *value)
|
||||
{
|
||||
struct bt_node_t *node = (struct bt_node_t *)kmalloc(sizeof(struct bt_node_t), 0);
|
||||
FAIL_ON_TO(node == NULL, nomem);
|
||||
memset((void *)node, 0, sizeof(struct bt_node_t));
|
||||
|
||||
node->left = left;
|
||||
node->right = right;
|
||||
node->value = value;
|
||||
node->parent = parent;
|
||||
|
||||
return node;
|
||||
nomem:;
|
||||
return (void*)-ENOMEM;
|
||||
}
|
||||
/**
|
||||
* @brief 插入结点
|
||||
*
|
||||
* @param root 树根结点
|
||||
* @param value 待插入结点的值
|
||||
* @return int 返回码
|
||||
*/
|
||||
int bt_insert(struct bt_root_t *root, void *value)
|
||||
{
|
||||
if (root == NULL)
|
||||
return -EINVAL;
|
||||
|
||||
struct bt_node_t *this_node = root->bt_node;
|
||||
struct bt_node_t *last_node = NULL;
|
||||
struct bt_node_t *insert_node = bt_create_node(NULL, NULL, NULL, value);
|
||||
FAIL_ON_TO((uint64_t)insert_node == (uint64_t)(-ENOMEM), failed);
|
||||
|
||||
while (this_node != NULL)
|
||||
{
|
||||
last_node = this_node;
|
||||
if (smaller(root, insert_node, this_node))
|
||||
this_node = this_node->left;
|
||||
else
|
||||
this_node = this_node->right;
|
||||
}
|
||||
|
||||
insert_node->parent = last_node;
|
||||
if (unlikely(last_node == NULL))
|
||||
root->bt_node = insert_node;
|
||||
else
|
||||
{
|
||||
if (smaller(root, insert_node, last_node))
|
||||
last_node->left = insert_node;
|
||||
else
|
||||
last_node->right = insert_node;
|
||||
}
|
||||
++root->size;
|
||||
return 0;
|
||||
|
||||
failed:;
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 搜索值为value的结点
|
||||
*
|
||||
* @param value 值
|
||||
* @param ret_addr 返回的结点基地址
|
||||
* @return int 错误码
|
||||
*/
|
||||
int bt_query(struct bt_root_t *root, void *value, uint64_t *ret_addr)
|
||||
{
|
||||
struct bt_node_t *this_node = root->bt_node;
|
||||
struct bt_node_t tmp_node = {0};
|
||||
tmp_node.value = value;
|
||||
|
||||
// 如果返回地址为0
|
||||
if (ret_addr == NULL)
|
||||
return -EINVAL;
|
||||
|
||||
while (this_node != NULL && !equal(root, this_node, &tmp_node))
|
||||
{
|
||||
if (smaller(root, &tmp_node, this_node))
|
||||
this_node = this_node->left;
|
||||
else
|
||||
this_node = this_node->right;
|
||||
}
|
||||
|
||||
if (this_node != NULL && equal(root, this_node, &tmp_node))
|
||||
{
|
||||
*ret_addr = (uint64_t)this_node;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 找不到则返回-1,且addr设为0
|
||||
*ret_addr = NULL;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static struct bt_node_t *bt_get_minimum(struct bt_node_t *this_node)
|
||||
{
|
||||
while (this_node->left != NULL)
|
||||
this_node = this_node->left;
|
||||
return this_node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 删除结点
|
||||
*
|
||||
* @param root 树根
|
||||
* @param value 待删除结点的值
|
||||
* @return int 返回码
|
||||
*/
|
||||
int bt_delete(struct bt_root_t *root, void *value)
|
||||
{
|
||||
uint64_t tmp_addr;
|
||||
int retval;
|
||||
|
||||
// 寻找待删除结点
|
||||
retval = bt_query(root, value, &tmp_addr);
|
||||
if (retval != 0 || tmp_addr == NULL)
|
||||
return retval;
|
||||
|
||||
struct bt_node_t *this_node = (struct bt_node_t *)tmp_addr;
|
||||
struct bt_node_t *to_delete = NULL, *to_delete_son = NULL;
|
||||
if (this_node->left == NULL || this_node->right == NULL)
|
||||
to_delete = this_node;
|
||||
else
|
||||
{
|
||||
to_delete = bt_get_minimum(this_node->right);
|
||||
// 释放要被删除的值,并把下一个结点的值替换上来
|
||||
root->release(this_node->value);
|
||||
this_node->value = to_delete->value;
|
||||
}
|
||||
|
||||
if (to_delete->left != NULL)
|
||||
to_delete_son = to_delete->left;
|
||||
else
|
||||
to_delete_son = to_delete->right;
|
||||
|
||||
if (to_delete_son != NULL)
|
||||
to_delete_son->parent = to_delete->parent;
|
||||
|
||||
if (to_delete->parent == NULL)
|
||||
root->bt_node = to_delete_son;
|
||||
else
|
||||
{
|
||||
if (to_delete->parent->left == to_delete)
|
||||
to_delete->parent->left = to_delete_son;
|
||||
else
|
||||
to_delete->parent->right = to_delete_son;
|
||||
}
|
||||
|
||||
--root->size;
|
||||
// 释放最终要删除的结点的对象
|
||||
kfree(to_delete);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 释放整个二叉搜索树
|
||||
*
|
||||
* @param root 树的根节点
|
||||
* @return int 错误码
|
||||
*/
|
||||
int bt_destroy_tree(struct bt_root_t *root)
|
||||
{
|
||||
// 新建一个kfifo缓冲区,将指向结点的指针存入fifo队列
|
||||
// 注:为了将指针指向的地址存入队列,我们需要对指针取地址
|
||||
struct kfifo_t fifo;
|
||||
kfifo_alloc(&fifo, ((root->size + 1) / 2) * sizeof(struct bt_node_t *), 0);
|
||||
kfifo_in(&fifo, (void *)&(root->bt_node), sizeof(struct bt_node_t *));
|
||||
|
||||
// bfs
|
||||
while (!kfifo_empty(&fifo))
|
||||
{
|
||||
// 取出队列头部的结点指针
|
||||
struct bt_node_t *nd;
|
||||
int count = kfifo_out(&fifo, &nd, sizeof(uint64_t));
|
||||
|
||||
// 将子节点加入队列
|
||||
if (nd->left != NULL)
|
||||
kfifo_in(&fifo, (void *)&(nd->left), sizeof(struct bt_node_t *));
|
||||
|
||||
if (nd->right != NULL)
|
||||
kfifo_in(&fifo, (void *)&(nd->right), sizeof(struct bt_node_t *));
|
||||
|
||||
// 销毁当前节点
|
||||
root->release(nd->value);
|
||||
kfree(nd);
|
||||
}
|
||||
|
||||
kfifo_free_alloc(&fifo);
|
||||
|
||||
return 0;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user