aboutsummaryrefslogtreecommitdiff
path: root/kmap/kmap.c
blob: 85577962a57bd2bf83ef61c5cb9569b4e9394518 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 *   kmap - (Linux) kernel map implementation
 *
 *   Copyright (c) 2011 Guillermo Ramos <0xwille@gmail.com>
 *
 * This file is part of kmap
 *
 * kmap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * kmap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kmap.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/errno.h>
#include <linux/list.h>
#include <linux/slab.h>
#include "kmap.h"

/*
 * Returns the node with the value passed, or NULL in case it does not exist
 */
static inline struct kmap *kmap_search(struct kmap *head, int value)
{
    struct list_head *list;
    struct kmap *node;

    list_for_each(list, &head->l) {
        node = list_entry(list, struct kmap, l);
        if (node->value == value)
            return node;
    }

    return NULL;
}

/*
 * Prints in klog a representation of all the nodes in the map
 */
void kmap_display(struct kmap *head)
{
    struct kmap *node;
    struct list_head *list;

    printk(KERN_ALERT "Displaying...");
    list_for_each(list, &head->l) {
        node = list_entry(list, struct kmap, l);
        printk(KERN_ALERT "  %p: v=%d d=%s\n",
               node, node->value, (char*)node->data);
    }
}

/*
 * Creates and returns a new head node
 */
inline struct kmap *kmap_create(void)
{
    struct kmap *head;

    head = kmalloc(sizeof(struct kmap), GFP_KERNEL);
    if (unlikely(!head))
        return NULL;

    INIT_LIST_HEAD(&head->l);

    return head;
}

/*
 * Deletes the map and frees the memory used by its nodes
 */
void kmap_destroy(struct kmap *head)
{
    struct kmap *node;
    struct list_head *list = head->l.next;

    while (list != &head->l) {
        node = list_entry(list, struct kmap, l);
        list = list->next;
        kfree(node);
    }

    kfree(head);
}

/*
 * Creates a new node with the given value and data, and adds it to the map head
 */
int kmap_insert(struct kmap *head, int value, void *data)
{
    struct kmap *new;

    // Key already exists
    if (kmap_search(head, value))
        return -EINVAL;

    new = kmap_create();
    if (unlikely(!new))
        return -ENOMEM;

    new->value = value;
    new->data = data;
    list_add_tail(&new->l, &head->l);

    return 0;
}

/*
 * Removes from the map the node with the given value.
 */
int kmap_delete(struct kmap *head, int value)
{
    struct kmap *node = kmap_search(head, value);

    if (node) {
        list_del(&node->l);
        kfree(node);
        return 0;
    } else {
        return -EINVAL;
    }
}

/*
 * Returns the data mapped to the given value, or NULL if that value has no data
 * associated to it
 */
void *kmap_get(struct kmap *head, int value)
{
    struct kmap *node = kmap_search(head, value);

    if (node)
        return node->data;
    else
        return NULL;
}

/*
 * Associates the given value with new data, if the value was already in the map
 */
void kmap_set(struct kmap *head, int value, void *data)
{
    struct kmap *node = kmap_search(head, value);

    if (node)
        node->data = data;
}