5

I want to play with kernel linked list before I use it in some part of the kernel code. But if I just include list.h, it doesn't work due to dependencies.

How can I write code using list in a single.c file e.g. test.c so that I can test my code just by compiling test.c? Looking forward to hearing from you soon.

Also, how can I use nested linked list?

2 Answers 2

6

You can get a userspace port from http://www.mcs.anl.gov/~kazutomo/list/list.h.
It says:

Here is a recipe to cook list.h for user space program

  1. copy list.h from linux/include/list.h
  2. remove
    • #ifdef KERNE and its #endif
    • all #include line
    • prefetch() and rcu related functions
  3. add macro offsetof() and container_of
Sign up to request clarification or add additional context in comments.

Comments

4

It is not meant to use the list in Userspace since its made for inside Kernel use and has several dependencies of kernel types and so on. You can see this by compiling your code with correct include paths:

gcc -I path-to-kernel-src/include/ test.c

When test.c contains this code:

#include <stdio.h>
#include <stdlib.h>

#include <linux/list.h>

int main(int argc, char **argv) { }

It fails to compile since there are includes in list.h which conflicts the userspace include (stdlib.h).

Nevertheless, the dependencies of such a data structures like list are pretty small. You need to sort them out in order to break the list.h dependencies from other kernel. In a short test, I removed the includes to and from list.h and added the data types struct list_head/hlist_head and hlist_node.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.