开发者

FreeBSD - how to create linked list(s) in the kernel?

I'm doing a project right now and the project is to manipulate linked list(s) in the kernel. The project will implement a "toy" locking mechanism, in which all the locks are in a linked list(s). Please help me out in the following questions:

1) How to create a linked list in the kernel? Can I use functions in ? Or simply malloc(),etc.?

2开发者_运维知识库) Locks are grouped by "lock group name" in this project, does this mean there should be multiple linked lists and each linked list represent a "lock group"? Thank you!


1) queue(3) man page documents some very useful macros in sys/queue.h, implementing lists and tail queues. This header also available in kernel.

Memory allocation in kernel documented in malloc(9) man page. Generally it's just like user-level malloc, but with additional type parameter useful for finding memory leaks. Your code should look like this:

MALLOC_DEFINE(M_MYMALLOC, "mydata", "Some data");
struct foo {
    SLIST_ENTRY(foo) chain;
};
struct foo *bar = malloc(1000, M_MYMALLOC, M_WAITOK);
SLIST_INSERT_HEAD(&head, bar, chain);
SLIST_REMOVE_HEAD(&head, chain);
free(var, M_MYMALLOC);

2) It's hard to answer this question without knowing what "grouping" mean in context of your project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜