Skip to content

Commit

Permalink
Linked lists
Browse files Browse the repository at this point in the history
  • Loading branch information
LizaTretyakova committed Sep 21, 2016
1 parent ba3b32d commit 1f899f8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
57 changes: 54 additions & 3 deletions tasks/linked_lists/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@
#include "stack.h"
#include "assert.h"

void clear_process_list(struct list_head *stack)
{
stack_entry_t* cur;

while (!stack_empty(stack))
{
cur = stack_pop(stack);
kfree(STACK_ENTRY_DATA(cur, char*));
delete_stack_entry(cur);
}
}

#define CHECK_NOT_NULL(ptr, stack_top, ret) \
if (!ptr) \
{ \
clear_process_list(&stack_top); \
ret = -ENOMEM; \
break; \
}


static int __init test_stack(void)
{
int ret = 0;
Expand All @@ -32,7 +53,7 @@ static int __init test_stack(void)
tos = stack_pop(&data_stack);
tos_data = STACK_ENTRY_DATA(tos, const char*);
delete_stack_entry(tos);
printk(KERN_ALERT "%s == %s\n", tos_data, test_data[i]);
printk(KERN_ALERT "!%s! == !%s!\n", tos_data, test_data[i]);
assert(!strcmp(tos_data, test_data[i]));
}

Expand All @@ -45,8 +66,38 @@ static int __init test_stack(void)

static int __init print_processes_backwards(void)
{
// TODO
return -ENODEV;
int ret = 0;
LIST_HEAD(data_stack);
stack_entry_t *cur = NULL;
struct task_struct *p;
char* name;

for_each_process(p)
{
// Get the name.
char* data = kmalloc(sizeof(p->comm), GFP_KERNEL);
CHECK_NOT_NULL(data, data_stack, ret)

CHECK_NOT_NULL(get_task_comm(data, p), data_stack, ret)

// Put to stack.
cur = create_stack_entry(data);
CHECK_NOT_NULL(cur, data_stack, ret)

stack_push(&data_stack, cur);
}

printk(KERN_ALERT "Hopefully now here appears the list of files in reversed order");
while (!stack_empty(&data_stack))
{
cur = stack_pop(&data_stack);
name = STACK_ENTRY_DATA(cur, char*);
delete_stack_entry(cur);
printk(KERN_ALERT "Yet another file: %s\n", name);
kfree(name);
}

return ret;
}

static int __init ll_init(void)
Expand Down
32 changes: 21 additions & 11 deletions tasks/linked_lists/stack.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
#include "stack.h"

#include "assert.h"
#include <linux/slab.h>
#include <linux/gfp.h>

stack_entry_t* create_stack_entry(void *data)
{
// TODO
(void)data;
return NULL;
stack_entry_t* result = kmalloc(sizeof(stack_entry_t), GFP_KERNEL);
if (!result) {
printk(KERN_ALERT "FAILED TO ALLOCATE MEMORY");
return result;
}
INIT_LIST_HEAD(&(result->lh));
result->data = data;
printk(KERN_ALERT "created node: %p\n", result);
return result;
}

void delete_stack_entry(stack_entry_t *entry)
{
// TODO
(void)entry;
// if I understand correctly, taking care of *data
// is not our responsibility, since we didn't create it
printk(KERN_ALERT "deleting node: %p\n", entry);
kfree(entry);
}

void stack_push(struct list_head *stack, stack_entry_t *entry)
{
// TODO
(void)stack;
(void)entry;
list_add(&(entry->lh), stack);
}

stack_entry_t* stack_pop(struct list_head *stack)
{
// TODO
(void)stack;
return NULL;
stack_entry_t* result = list_entry(stack->next, stack_entry_t, lh);
list_del(stack->next);
return result;
}
2 changes: 1 addition & 1 deletion tasks/linked_lists/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ stack_entry_t* stack_pop(struct list_head *stack);
#define STACK_ENTRY_DATA_RESET(stack_entry, new_data) \
do { \
(stack_entry)->data = new_data; \
} while(0)
} while(0)

#endif //_LL_STACK_H

0 comments on commit 1f899f8

Please sign in to comment.