Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Linux kernel linked list outside kernel code?

Tags:

linux

list

kernel

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?

like image 504
Masum Avatar asked Nov 29 '12 07:11

Masum


2 Answers

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
like image 178
CL. Avatar answered Nov 02 '22 12:11

CL.


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.

like image 36
falstaff Avatar answered Nov 02 '22 13:11

falstaff