File: llist.c

package info (click to toggle)
389-ds-base 2.3.1%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,536 kB
  • sloc: ansic: 306,972; python: 96,937; cpp: 10,257; perl: 2,854; makefile: 2,046; sh: 925; yacc: 806; xml: 379; lex: 366; javascript: 148; java: 50
file content (338 lines) | stat: -rw-r--r-- 7,860 bytes parent folder | download | duplicates (2)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * License: GPL (version 3 or any later version).
 * See LICENSE for details.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* llist.c - single link list implementation */

#include <string.h>
#include "slapi-plugin.h"
#include "slapi-private.h"
#include "llist.h"
#include "repl_shared.h"

/* data structures */

/* link list node */
typedef struct lnode
{
    char *key;
    void *data;
    struct lnode *next;
} LNode;

/* This structure defines a one-way linked list with head and tail pointers.
   The list contains a "dummy" head node which makes sure that every node
   has a previous node. This allows to remove a node during iteration without
   breaking the list */
struct llist
{
    LNode *head;
    LNode *tail;
};

/* forward declarations */
static LNode *_llistNewNode(const char *key, void *data);
static void _llistDestroyNode(LNode **node, FNFree fnFree);

LList *
llistNew()
{
    LList *list = (LList *)slapi_ch_calloc(1, sizeof(LList));

    /* allocate a special head node - it contains no data but just
       fulfills the requirement that every node has a previous one.
       This is used during iteration with removal */
    if (list) {
        list->head = (LNode *)slapi_ch_calloc(1, sizeof(LNode));
        if (list->head == NULL) {
            slapi_ch_free((void **)&list);
        }
    }

    return list;
}

void
llistDestroy(LList **list, FNFree fnFree)
{
    LNode *node = NULL, *prev_node;

    if (list == NULL || *list == NULL)
        return;

    if ((*list)->head)
        node = (*list)->head->next;

    while (node) {
        prev_node = node;
        node = node->next;
        _llistDestroyNode(&prev_node, fnFree);
    }

    slapi_ch_free((void **)&((*list)->head));
    slapi_ch_free((void **)list);
}

void *
llistGetFirst(LList *list, void **iterator)
{
    if (list == NULL || iterator == NULL || list->head == NULL || list->head->next == NULL) {
        /* empty list or error */
        return NULL;
    }

    /* Iterator points to the previous element (so that we can remove current element
       and still keep the list in tact. In case of the first element, iterator points
       to the dummy head element */
    (*iterator) = list->head;
    return list->head->next->data;
}

void *
llistGetNext(LList *list, void **iterator)
{
    LNode *node;

    if (list == NULL || list->head == NULL || iterator == NULL || *iterator == NULL) {
        /* end of the list or error */
        return NULL;
    }

    /* Iterator points to the previous element (so that we can
       remove current element and still keep list in tact. */
    node = *(LNode **)iterator;
    node = node->next;

    (*iterator) = node;

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

void *
llistRemoveCurrentAndGetNext(LList *list, void **iterator)
{
    LNode *prevNode, *node;

    /* end of the list is reached or error occured */
    if (list == NULL || iterator == NULL || *iterator == NULL)
        return NULL;

    /* Iterator points to the previous element (so that we can
       remove current element and still keep list in tact. */
    prevNode = *(LNode **)iterator;
    node = prevNode->next;
    if (node) {
        prevNode->next = node->next;
        if (list->tail == node) {
            list->tail = prevNode;
        }
        _llistDestroyNode(&node, NULL);
        node = prevNode->next;
        if (node) {
            return node->data;
        } else {
            return NULL;
        }
    } else
        return NULL;
}

void *
llistGetHead(LList *list)
{
    if (list == NULL || list->head == NULL || list->head->next == NULL) {
        /* empty list or error */
        return NULL;
    }

    return list->head->next->data;
}

void *
llistGetTail(LList *list)
{
    if (list == NULL || list->tail == NULL) {
        /* empty list or error */
        return NULL;
    }

    return list->tail->data;
}

void *
llistGet(LList *list, const char *key)
{
    LNode *node;

    /* empty list or invalid input */
    if (list == NULL || list->head == NULL || list->head->next == NULL || key == NULL)
        return NULL;

    node = list->head->next;
    while (node) {
        if (node->key && strcmp(key, node->key) == 0) {
            return node->data;
        }

        node = node->next;
    }

    /* node with specified key is not found */
    return NULL;
}

int
llistInsertHead(LList *list, const char *key, void *data)
{
    LNode *node;
    if (list == NULL || list->head == NULL || data == NULL) {
        slapi_log_err(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: invalid argument\n");
        return -1;
    }

    node = _llistNewNode(key, data);
    if (node == NULL) {
        slapi_log_err(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: failed to allocate list node\n");
        return -1;
    }

    if (list->head->next == NULL) /* empty list */
    {
        list->head->next = node;
        list->tail = node;
    } else {
        node->next = list->head->next;
        list->head->next = node;
    }

    return 0;
}

int
llistInsertTail(LList *list, const char *key, void *data)
{
    LNode *node;
    if (list == NULL || list->head == NULL || data == NULL) {
        slapi_log_err(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: invalid argument\n");
        return -1;
    }

    node = _llistNewNode(key, data);
    if (node == NULL) {
        slapi_log_err(SLAPI_LOG_PLUGIN, repl_plugin_name, "llistInsertHead: failed to allocate list node\n");
        return -1;
    }

    if (list->head->next == NULL) /* empty list */
    {
        list->head->next = node;
        /* coverity[copy_paste_error] */
        list->tail = node;
    } else {
        list->tail->next = node;
        list->tail = node;
    }

    return 0;
}

void *
llistRemoveHead(LList *list)
{
    LNode *node;
    void *data;

    if (list == NULL || list->head == NULL || list->head->next == NULL)
        return NULL;

    node = list->head->next;
    data = node->data;

    list->head->next = node->next;

    /* last element removed */
    if (list->head->next == NULL)
        list->tail = NULL;

    _llistDestroyNode(&node, NULL);

    return data;
}

void *
llistRemove(LList *list, const char *key)
{
    LNode *node, *prev_node;
    void *data;

    if (list == NULL || list->head == NULL || list->head->next == NULL || key == NULL)
        return NULL;

    node = list->head->next;
    prev_node = list->head;
    while (node) {
        if (node->key && strcmp(key, node->key) == 0) {
            prev_node->next = node->next;
            /* last element removed */
            if (node->next == NULL) {
                /* no more elements in the list */
                if (list->head->next == NULL) {
                    list->tail = NULL;
                } else {
                    list->tail = prev_node;
                }
            }

            data = node->data;
            _llistDestroyNode(&node, NULL);
            return data;
        }

        prev_node = node;
        node = node->next;
    }

    /* node with specified key is not found */
    return NULL;
}

static LNode *
_llistNewNode(const char *key, void *data)
{
    LNode *node = (LNode *)slapi_ch_malloc(sizeof(LNode));
    if (node == NULL)
        return NULL;

    if (key)
        node->key = slapi_ch_strdup(key);
    else
        node->key = NULL;

    node->data = data;
    node->next = NULL;

    return node;
}

static void
_llistDestroyNode(LNode **node, FNFree fnFree)
{
    if ((*node)->data && fnFree)
        fnFree(&(*node)->data);
    if ((*node)->key)
        slapi_ch_free((void **)&((*node)->key));

    slapi_ch_free((void **)node);
}