删除无用的C版本bitree和ida/idr. (#526)

这些数据结构不再使用,将其删除.
This commit is contained in:
LoGin
2024-02-19 11:17:23 +08:00
committed by GitHub
parent 196b75dc17
commit 701589559f
15 changed files with 1 additions and 2972 deletions

View File

@ -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:

View File

@ -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);

View File

@ -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

View File

@ -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>

View File

@ -1,10 +0,0 @@
SRC = $(wildcard *.c)
OBJ = $(SRC:.c=.o)
CFLAGS += -I .
.PHONY: all
all: $(OBJ)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
// 获取 1000000 个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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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