内核中的radix-tree简单使用。整数与指针相关联。
向radix-tree中存入、取出数据。
系统环境:Linux ubuntu 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
代码:
myRadixtree.c
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/kernel.h>
-
-
#include <linux/radix-tree.h>
-
-
MODULE_LICENSE("GPL");
-
MODULE_AUTHOR("zl");
-
MODULE_DESCRIPTION("radixtree test");
-
-
char *test[] = {"abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz0",
-
"123", "456", "789", "zyx", "wvu", "tsr", "qpo", "nml", "kji"};
-
-
static int __init Radixtree_init(void)
-
{
-
int i = 0;
-
char *ptr;
-
int num = ARRAY_SIZE(test);
-
-
printk("num : %d\n", num);
-
-
printk("**********************\n");
-
RADIX_TREE(root, GFP_ATOMIC);
-
for(i = 0; i < num; i++) {
-
radix_tree_insert(&root, i, test[i]);
-
}
-
for(i = 0; i < num; i++) {
-
printk("---[%d]---%s\n", i, (char*)radix_tree_lookup(&root, i));
-
}
-
printk("**********************\n");
-
radix_tree_delete(&root, 13);
-
for(i = 0; i < num; i++) {
-
printk("---[%d]---%s\n", i, (char*)radix_tree_lookup(&root, i));
-
}
-
-
printk("**********************\n");
-
for(i = 0; i < num; i++) {
-
radix_tree_delete(&root, i);
-
}
-
-
return 0;
-
}
-
-
static void __exit Radixtree_exit(void)
-
{
-
printk("exit !\n");
-
}
-
-
-
module_init(Radixtree_init);
-
module_exit(Radixtree_exit);
Makefile文件内容如下:
-
obj-m = myRadixtree.o
-
#KERNELS = /home/zl/my2440-2.6.36
-
KERNELS = /lib/modules/$(shell uname -r)/build/
-
default:
-
make -C $(KERNELS) M=$(shell pwd) modules
-
.PHONY:clean
-
clean:
-
make -C $(KERNELS) M=$(shell pwd) clean
make clean;make;sudo insmod myRadixtree.ko; dmesg后,输出如下:
阅读(1626) | 评论(0) | 转发(0) |