]> git.ipfire.org Git - thirdparty/mdadm.git/blob - dlink.c
Correctly abort level change when reshape_array fails.
[thirdparty/mdadm.git] / dlink.c
1
2 /* doubly linked lists */
3 /* This is free software. No strings attached. No copyright claimed */
4
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #ifdef __dietlibc__
9 char *strncpy(char *dest, const char *src, size_t n) __THROW;
10 #endif
11 #include "dlink.h"
12
13
14 void *dl_head()
15 {
16 void *h;
17 h = dl_alloc(0);
18 dl_next(h) = h;
19 dl_prev(h) = h;
20 return h;
21 }
22
23 void dl_free(void *v)
24 {
25 struct __dl_head *vv = v;
26 free(vv-1);
27 }
28
29 void dl_init(void *v)
30 {
31 dl_next(v) = v;
32 dl_prev(v) = v;
33 }
34
35 void dl_insert(void *head, void *val)
36 {
37 dl_next(val) = dl_next(head);
38 dl_prev(val) = head;
39 dl_next(dl_prev(val)) = val;
40 dl_prev(dl_next(val)) = val;
41 }
42
43 void dl_add(void *head, void *val)
44 {
45 dl_prev(val) = dl_prev(head);
46 dl_next(val) = head;
47 dl_next(dl_prev(val)) = val;
48 dl_prev(dl_next(val)) = val;
49 }
50
51 void dl_del(void *val)
52 {
53 if (dl_prev(val) == 0 || dl_next(val) == 0)
54 return;
55 dl_prev(dl_next(val)) = dl_prev(val);
56 dl_next(dl_prev(val)) = dl_next(val);
57 dl_prev(val) = dl_next(val) = 0;
58 }
59
60 char *dl_strndup(char *s, int l)
61 {
62 char *n;
63 if (s == NULL)
64 return NULL;
65 n = dl_newv(char, l+1);
66 if (n == NULL)
67 return NULL;
68 else
69 {
70 strncpy(n, s, l);
71 n[l] = 0;
72 return n;
73 }
74 }
75
76 char *dl_strdup(char *s)
77 {
78 return dl_strndup(s, (int)strlen(s));
79 }