Skip to content

Commit

Permalink
linked_list
Browse files Browse the repository at this point in the history
  • Loading branch information
kostya93 committed Mar 8, 2016
1 parent 6597f79 commit 93709e4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
44 changes: 42 additions & 2 deletions tasks/linked_lists/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,49 @@ static void __init test_stack(void)
assert(stack_empty(&data_stack));
}

static void __init print_processes_backwards(void)
static int __init print_processes_backwards(void)
{
// TODO
LIST_HEAD(stack_of_process);
stack_entry_t* entry_ps = NULL;
struct task_struct* task_ps;
char* str = NULL;
for_each_process(task_ps)
{
str = (char*)kmalloc(TASK_COMM_LEN, GFP_KERNEL);
if (!str)
{
goto error;
}

entry_ps = create_stack_entry(str);
if (!entry_ps)
{
goto error;

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

kfree(str)

}

get_task_comm(str, task_ps);
stack_push(&stack_of_process, entry_ps);
}

while(!stack_empty(&stack_of_process))
{
entry_ps = stack_pop(&stack_of_process);
str = STACK_ENTRY_DATA(entry_ps, char*);
printk(KERN_ALERT "%s\n", str);
delete_stack_entry(entry_ps);

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

kfree(str);

}
return -ENOMEM;

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

return 0; // success


error:
printk(KERN_ALERT "error");
while(!stack_empty(&stack_of_process))
{
entry_ps = stack_pop(&stack_of_process);
str = STACK_ENTRY_DATA(entry_ps, char*);

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

kfree(str)

delete_stack_entry(entry_ps);
}
return -ENOMEM;
}

static int __init ll_init(void)
Expand Down
24 changes: 13 additions & 11 deletions tasks/linked_lists/stack.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
#include <linux/slab.h>
#include <linux/gfp.h>
#include "stack.h"

stack_entry_t* create_stack_entry(void *data)
{
// TODO
(void)data;
return NULL;
stack_entry_t* entry_ptr = kmalloc(sizeof(stack_entry_t), GFP_KERNEL);
if (entry_ptr != NULL)
{
entry_ptr->data = data;
INIT_LIST_HEAD(&(entry_ptr->lh));
}
return entry_ptr;
}

void delete_stack_entry(stack_entry_t *entry)
{
// TODO
(void)entry;
list_del(&entry->lh);

This comment has been minimized.

Copy link
@eabatalov

eabatalov Mar 10, 2016

Collaborator

create_stack_entry should be symmetric to delete_stack_entry.
create_stack_entry doesn't add newly created entry to any list so delete thouldn't do it too.

It is responsibility of stack_pop function to delete stack entry from the list.

So delete_stack_entry and stack_pop have not right implementations.

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;
return list_entry(stack->next, stack_entry_t, lh);
}

0 comments on commit 93709e4

Please sign in to comment.