libubox
C utility functions for OpenWrt.
safe_list.h File Reference
#include <stdbool.h>
#include "list.h"
#include "utils.h"

Go to the source code of this file.

Data Structures

struct  safe_list
 

Macros

#define INIT_SAFE_LIST(_head)
 
#define SAFE_LIST_INIT(_name)   { LIST_HEAD_INIT(_name.list), NULL }
 
#define SAFE_LIST(_name)   struct safe_list _name = SAFE_LIST_INIT(_name)
 

Functions

int safe_list_for_each (struct safe_list *list, int(*cb)(void *ctx, struct safe_list *list), void *ctx)
 
void safe_list_add (struct safe_list *list, struct safe_list *head)
 
void safe_list_add_first (struct safe_list *list, struct safe_list *head)
 
void safe_list_del (struct safe_list *list)
 
static bool safe_list_empty (struct safe_list *head)
 

Macro Definition Documentation

◆ INIT_SAFE_LIST

#define INIT_SAFE_LIST (   _head)
Value:
do { \
INIT_LIST_HEAD(_head.list); \
(_head)->i = NULL; \
} while (0)

Definition at line 48 of file safe_list.h.

◆ SAFE_LIST

#define SAFE_LIST (   _name)    struct safe_list _name = SAFE_LIST_INIT(_name)

Definition at line 55 of file safe_list.h.

◆ SAFE_LIST_INIT

#define SAFE_LIST_INIT (   _name)    { LIST_HEAD_INIT(_name.list), NULL }

Definition at line 54 of file safe_list.h.

Function Documentation

◆ safe_list_add()

void safe_list_add ( struct safe_list list,
struct safe_list head 
)

Definition at line 83 of file safe_list.c.

84 {
85  list->i = NULL;
86  list_add_tail(&list->list, &head->list);
87 }
static void list_add_tail(struct list_head *_new, struct list_head *head)
Definition: list.h:165
struct list_head list
Definition: safe_list.h:36
struct safe_list_iterator * i
Definition: safe_list.h:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_list_add_first()

void safe_list_add_first ( struct safe_list list,
struct safe_list head 
)

Definition at line 89 of file safe_list.c.

90 {
91  list->i = NULL;
92  list_add(&list->list, &head->list);
93 }
static void list_add(struct list_head *_new, struct list_head *head)
Definition: list.h:159
Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_list_del()

void safe_list_del ( struct safe_list list)

Definition at line 95 of file safe_list.c.

96 {
97  struct safe_list_iterator *i, *next_i, **tail;
98  struct safe_list *next;
99 
100  next = list_entry(list->list.next, struct safe_list, list);
101  list_del(&list->list);
102 
103  if (!list->i)
104  return;
105 
106  next_i = next->i;
107  tail = &next->i;
108 
109  for (i = list->i; i; i = i->next_i) {
110  tail = &i->next_i;
111  i->next = next;
112  }
113 
114  next->i = list->i;
115  list->i->head = &next->i;
116  *tail = next_i;
117  if (next_i)
118  next_i->head = tail;
119 
120  list->i = NULL;
121 }
static void list_del(struct list_head *entry)
Definition: list.h:96
#define list_entry(ptr, type, field)
Definition: list.h:120
struct list_head * next
Definition: list.h:54
struct safe_list_iterator ** head
Definition: safe_list.c:22
struct safe_list_iterator * next_i
Definition: safe_list.c:23
struct safe_list * next
Definition: safe_list.c:24
Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_list_empty()

static bool safe_list_empty ( struct safe_list head)
inlinestatic

Definition at line 57 of file safe_list.h.

58 {
59  return list_empty(&head->list);
60 }
static bool list_empty(const struct list_head *head)
Definition: list.h:69

◆ safe_list_for_each()

int safe_list_for_each ( struct safe_list list,
int(*)(void *ctx, struct safe_list *list)  cb,
void *  ctx 
)

Definition at line 62 of file safe_list.c.

65 {
66  struct safe_list_iterator i;
67  struct safe_list *cur;
68  int ret = 0;
69 
70  for (cur = list_entry(head->list.next, struct safe_list, list),
72  cur != head;
73  cur = i.next, __safe_list_move_iterator(cur, &i)) {
74  ret = cb(ctx, cur);
75  if (ret)
76  break;
77  }
78 
80  return ret;
81 }
static void __safe_list_del_iterator(struct safe_list_iterator *i)
Definition: safe_list.c:47
static void __safe_list_set_iterator(struct safe_list *list, struct safe_list_iterator *i)
Definition: safe_list.c:28
static void __safe_list_move_iterator(struct safe_list *list, struct safe_list_iterator *i)
Definition: safe_list.c:55
Here is the call graph for this function:
Here is the caller graph for this function: