~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/innobase/ut/ut0list.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "ut0list.h"
 
2
#ifdef UNIV_NONINL
 
3
#include "ut0list.ic"
 
4
#endif
 
5
 
 
6
/********************************************************************
 
7
Create a new list. */
 
8
 
 
9
ib_list_t*
 
10
ib_list_create(void)
 
11
/*=================*/
 
12
                        /* out: list */
 
13
{
 
14
        ib_list_t*      list = mem_alloc(sizeof(ib_list_t));
 
15
 
 
16
        list->first = NULL;
 
17
        list->last = NULL;
 
18
        list->is_heap_list = FALSE;
 
19
 
 
20
        return(list);
 
21
}
 
22
 
 
23
/********************************************************************
 
24
Create a new list using the given heap. ib_list_free MUST NOT BE CALLED for
 
25
lists created with this function. */
 
26
 
 
27
ib_list_t*
 
28
ib_list_create_heap(
 
29
/*================*/
 
30
                                /* out: list */
 
31
        mem_heap_t*     heap)   /* in: memory heap to use */
 
32
{
 
33
        ib_list_t*      list = mem_heap_alloc(heap, sizeof(ib_list_t));
 
34
 
 
35
        list->first = NULL;
 
36
        list->last = NULL;
 
37
        list->is_heap_list = TRUE;
 
38
 
 
39
        return(list);
 
40
}
 
41
 
 
42
/********************************************************************
 
43
Free a list. */
 
44
 
 
45
void
 
46
ib_list_free(
 
47
/*=========*/
 
48
        ib_list_t*      list)   /* in: list */
 
49
{
 
50
        ut_a(!list->is_heap_list);
 
51
 
 
52
        /* We don't check that the list is empty because it's entirely valid
 
53
        to e.g. have all the nodes allocated from a single heap that is then
 
54
        freed after the list itself is freed. */
 
55
 
 
56
        mem_free(list);
 
57
}
 
58
 
 
59
/********************************************************************
 
60
Add the data to the start of the list. */
 
61
 
 
62
ib_list_node_t*
 
63
ib_list_add_first(
 
64
/*==============*/
 
65
                                /* out: new list node*/
 
66
        ib_list_t*      list,   /* in: list */
 
67
        void*           data,   /* in: data */
 
68
        mem_heap_t*     heap)   /* in: memory heap to use */
 
69
{
 
70
        return(ib_list_add_after(list, ib_list_get_first(list), data, heap));
 
71
}
 
72
 
 
73
/********************************************************************
 
74
Add the data to the end of the list. */
 
75
 
 
76
ib_list_node_t*
 
77
ib_list_add_last(
 
78
/*=============*/
 
79
                                /* out: new list node*/
 
80
        ib_list_t*      list,   /* in: list */
 
81
        void*           data,   /* in: data */
 
82
        mem_heap_t*     heap)   /* in: memory heap to use */
 
83
{
 
84
        return(ib_list_add_after(list, ib_list_get_last(list), data, heap));
 
85
}
 
86
 
 
87
/********************************************************************
 
88
Add the data after the indicated node. */
 
89
 
 
90
ib_list_node_t*
 
91
ib_list_add_after(
 
92
/*==============*/
 
93
                                        /* out: new list node*/
 
94
        ib_list_t*      list,           /* in: list */
 
95
        ib_list_node_t* prev_node,      /* in: node preceding new node (can
 
96
                                        be NULL) */
 
97
        void*           data,           /* in: data */
 
98
        mem_heap_t*     heap)           /* in: memory heap to use */
 
99
{
 
100
        ib_list_node_t* node = mem_heap_alloc(heap, sizeof(ib_list_node_t));
 
101
 
 
102
        node->data = data;
 
103
 
 
104
        if (!list->first) {
 
105
                /* Empty list. */
 
106
 
 
107
                ut_a(!prev_node);
 
108
 
 
109
                node->prev = NULL;
 
110
                node->next = NULL;
 
111
 
 
112
                list->first = node;
 
113
                list->last = node;
 
114
        } else if (!prev_node) {
 
115
                /* Start of list. */
 
116
 
 
117
                node->prev = NULL;
 
118
                node->next = list->first;
 
119
 
 
120
                list->first->prev = node;
 
121
 
 
122
                list->first = node;
 
123
        } else {
 
124
                /* Middle or end of list. */
 
125
 
 
126
                node->prev = prev_node;
 
127
                node->next = prev_node->next;
 
128
 
 
129
                prev_node->next = node;
 
130
 
 
131
                if (node->next) {
 
132
                        node->next->prev = node;
 
133
                } else {
 
134
                        list->last = node;
 
135
                }
 
136
        }
 
137
 
 
138
        return(node);
 
139
}
 
140
 
 
141
/********************************************************************
 
142
Remove the node from the list. */
 
143
 
 
144
void
 
145
ib_list_remove(
 
146
/*===========*/
 
147
        ib_list_t*      list,   /* in: list */
 
148
        ib_list_node_t* node)   /* in: node to remove */
 
149
{
 
150
        if (node->prev) {
 
151
                node->prev->next = node->next;
 
152
        } else {
 
153
                /* First item in list. */
 
154
 
 
155
                ut_ad(list->first == node);
 
156
 
 
157
                list->first = node->next;
 
158
        }
 
159
 
 
160
        if (node->next) {
 
161
                node->next->prev = node->prev;
 
162
        } else {
 
163
                /* Last item in list. */
 
164
 
 
165
                ut_ad(list->last == node);
 
166
 
 
167
                list->last = node->prev;
 
168
        }
 
169
}