I need to implement a linked list in kernel code (not sure if it matters too much). the code should identify each linked list with a number between 0 and 255. and each node also has an id inside the list and some other data fields. I'm having some trouble making it work and not sure what has gone wrong I've written it very clearly but it seems to have segmentation issues. I would be happy to understand what went wrong here.
Note: I'm quite sure the problems aren't regarding the fact it is using kernel functions.
#undef __KERNEL__
#define __KERNEL__
#undef MODULE
#define MODULE
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/string.h>
MODULE_LICENSE("GPL");
typedef struct Node {
long node_id;
char data[256];
int data_size;
struct Node next;
};
typedef struct List {
int list_id;
struct Node head;
};
/* array holds the Lists by their list_id number */
static struct List* lists = (List*) kmalloc(256 * sizeof(List), GFP_KERNEL);
static struct List* get_list(int list_id) {
return lists[list_id];
}
static struct Node* get_node(struct List* list, long node_id) {
struct Node* node = list->head;
while (node != NULL) {
if (node->node_id == node_id) {
return node;
}
node = node->next;
}
return NULL;
}
static void add_node(struct List* list, long node_id) {
struct Node* node;
node = kmalloc(sizeof(Node), GFP_KERNEL);
node->next = list->head;
list->head = node;
}
static void add_list(int list_id) {
struct List* list;
list = kmalloc(sizeof(List), GFP_KERNEL);
list->list_id = list_id;
lists[list->list_id] = list;
}
static void free_list(struct List* list) {
if (list == NULL) {
return;
}
while (list->head != NULL) {
struct Node* node = list->head;
list->head = node->next;
kfree(node);
}
kfree(list);
}
static void free_lists(void) {
int list_id;
for (list_id = 0 ; list_id < 256; list_id++) {
free_list(lists[list_id]);
}
}