source: branches/stable/mondo/src/lib/mr_list.c @ 1364

Last change on this file since 1364 was 1364, checked in by Bruno Cornec, 13 years ago

Remove a useless strcture + fix some compile errors in mr_list

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/*
2 * $Id$
3 *
4 * List handling functions safe from a memory management point of view
5 *
6 */
7
8#include <stdio.h>
9#include <string.h>
10#include <time.h>
11
12#include "mr_mem.h"
13#include "mr_msg.h"
14#include "mr_list.h"
15
16/* List allocation and initialization */
17void mr_list_alloc(struct mr_list *list) {
18
19    list = mr_malloc(sizeof(struct mr_list));
20    list->first = NULL;
21    list->last = NULL;
22}
23
24/* Elt of list allocation and initialization */
25void mr_list_alloc_elt(struct mr_list_elt *elt, void *data, void (*mr_free_data)(void *data)) {
26
27    elt = mr_malloc(sizeof(struct mr_list_elt));
28    elt->mr_free_data = mr_free_data;
29    elt->data = (void *)data;
30    elt->prev = NULL;
31    elt->next = NULL;
32}
33
34void mr_list_free_elt(struct mr_list_elt *elt) {
35    elt->mr_free_data(elt->data);
36    mr_free(elt->data);
37    mr_free(elt);
38}
39
40/* List desallocation and removal */
41void mr_list_free(struct mr_list *list) {
42
43    struct mr_list_elt *elt = list->first;
44    struct mr_list_elt *elt2 = NULL;
45   
46    while (elt != NULL) {
47        /* preserve next elt */
48        elt2 = elt->next;
49        /* Purge elt */
50        mr_list_free_elt(elt);
51        /* next elt to consider */
52        elt = elt2;
53    }
54
55    mr_free(list);
56}
57
58/* Count the number of elements in the list */
59int mr_list_length(struct mr_list *list) {
60
61    int i = 0;
62    struct mr_list_elt *p = list->first;
63
64    while (p != NULL) {
65        i++;
66        p = p->next;
67    }
68    return(i);
69}
70
71/* Add an element first in the list */
72void mr_list_add_elt_first(struct mr_list *list, struct mr_list_elt *elt) {
73   
74    if (list->first != NULL) {
75        /* if there was a first elt shift it */
76        list->first->prev = elt;
77        elt->next = list->first;
78    } else {
79        /* if there was no first, there was no last as well, so set it up */
80        list->last = elt;
81    }
82    list->first = elt;
83}
84
85/* Add an element last in the list */
86void mr_list_add_elt_last(struct mr_list *list, struct mr_list_elt *elt) {
87   
88    if (list->last != NULL) {
89        /* if there was a last elt use it */
90        list->last->next = elt;
91        elt->prev = list->last;
92    } else {
93        /* if there was no last, there was no first as well, so set it up */
94        list->first = elt;
95    }
96    list->last = elt;
97}
98
99/* Add an element in the list after the ref elt given in parameter */
100void mr_list_add_elt_after(struct mr_list *list, struct mr_list_elt *ref, struct mr_list_elt *elt) {
101
102    if (ref->next != NULL) {
103        /* if there was a next elt, shit it */
104        ref->next->prev = elt;
105        elt->next = ref->next;
106    } else {
107        /* If not, it's the last elt */
108        list->last = elt;
109    }
110    ref->next = elt;
111    elt->prev = ref;
112}
113
114/* Add an element in the list before the ref elt given in parameter */
115void mr_list_add_elt_before(struct mr_list *list, struct mr_list_elt *ref, struct mr_list_elt *elt) {
116
117    if (ref->prev != NULL) {
118        /* if there was a prev elt, shit it */
119        ref->prev->next = elt;
120        elt->prev = ref->prev;
121    } else {
122        /* If not, it's the first elt */
123        list->first = elt;
124    }
125    ref->prev = elt;
126    elt->next = ref;
127}
128
129/* Delete the first element in the list */
130void mr_list_del_elt_first(struct mr_list *list) {
131
132    struct mr_list_elt *elt = list->first;
133
134    if (elt->next != NULL) {
135        /* There are other elts behind */
136        list->first = elt->next;
137    }
138    mr_list_free_elt(elt);
139}
140
141/* Delete the last element in the list */
142void mr_list_del_elt_last(struct mr_list *list) {
143
144    struct mr_list_elt *elt = list->last;
145
146    if (elt->prev != NULL) {
147        /* There are other elts before */
148        list->last = elt->prev;
149    }
150    mr_list_free_elt(elt);
151}
152
153/* Delete an element in the list after the ref elt given in parameter */
154void mr_list_del_elt_after(struct mr_list *list, struct mr_list_elt *ref) {
155
156    struct mr_list_elt *elt = ref->next;
157
158    if (elt != NULL) {
159        /* if there was a next elt, delete it */
160        if (elt->next != NULL) {
161            /* there is an elt behind, shift it */
162            elt->prev->next = elt->next;
163            elt->next->prev = elt->prev;
164            mr_list_free_elt(elt);
165        } else {
166            /* that elt is the last one */
167            mr_list_del_elt_last(list);
168        }
169    } else {
170        /* If not, ref is the last elt */
171        mr_msg(3, "No elt to delete available after the last one");
172    }
173}
174
175/* Delete an element in the list before the ref elt given in parameter */
176void mr_list_del_elt_before(struct mr_list *list, struct mr_list_elt *ref) {
177
178    struct mr_list_elt *elt = ref->prev;
179
180    if (elt != NULL) {
181        /* if there was a previous elt, delete it */
182        if (elt->prev != NULL) {
183            /* there is an elt before, shift it */
184            elt->prev->next = elt->next;
185            elt->next->prev = elt->prev;
186            mr_list_free_elt(elt);
187        } else {
188            /* that elt is the first one */
189            mr_list_del_elt_first(list);
190        }
191    } else {
192        /* If not, ref is the first elt */
193        mr_msg(3, "No elt to delete available before the first one");
194    }
195}
Note: See TracBrowser for help on using the repository browser.