]> git.ipfire.org Git - thirdparty/mdadm.git/blame - dlink.c
Release mdadm-3.3.4
[thirdparty/mdadm.git] / dlink.c
CommitLineData
82b27616
NB
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>
98c6faba
NB
8#ifdef __dietlibc__
9char *strncpy(char *dest, const char *src, size_t n) __THROW;
10#endif
503975b9 11void *xcalloc(size_t num, size_t size);
82b27616 12#include "dlink.h"
82b27616
NB
13
14void *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
23void dl_free(void *v)
24{
25 struct __dl_head *vv = v;
26 free(vv-1);
27}
28
29void dl_init(void *v)
30{
31 dl_next(v) = v;
32 dl_prev(v) = v;
33}
34
35void 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
43void 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
51void 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
60char *dl_strndup(char *s, int l)
61{
62 char *n;
63 if (s == NULL)
64 return NULL;
65 n = dl_newv(char, l+1);
503975b9
N
66 strncpy(n, s, l);
67 n[l] = 0;
68 return n;
82b27616
NB
69}
70
71char *dl_strdup(char *s)
72{
73 return dl_strndup(s, (int)strlen(s));
74}