Blame shared/nm-glib-aux/nm-c-list.h

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2014 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#ifndef __NM_C_LIST_H__
Packit 5756e2
#define __NM_C_LIST_H__
Packit 5756e2
Packit 5756e2
#include "c-list/src/c-list.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
#define nm_c_list_contains_entry(list, what, member)   \
Packit Service a1bd4f
    ({                                                 \
Packit Service a1bd4f
        typeof(what) _what = (what);                   \
Packit Service a1bd4f
                                                       \
Packit Service a1bd4f
        _what &&c_list_contains(list, &_what->member); \
Packit Service a1bd4f
    })
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    CList lst;
Packit Service a1bd4f
    void *data;
Packit 5756e2
} NMCListElem;
Packit 5756e2
Packit 5756e2
static inline NMCListElem *
Packit Service a1bd4f
nm_c_list_elem_new_stale(void *data)
Packit 5756e2
{
Packit Service a1bd4f
    NMCListElem *elem;
Packit 5756e2
Packit Service a1bd4f
    elem       = g_slice_new(NMCListElem);
Packit Service a1bd4f
    elem->data = data;
Packit Service a1bd4f
    return elem;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_c_list_elem_free_full(NMCListElem *elem, GDestroyNotify free_fcn)
Packit 5756e2
{
Packit Service a1bd4f
    if (!elem)
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    c_list_unlink_stale(&elem->lst);
Packit Service a1bd4f
    if (free_fcn)
Packit Service a1bd4f
        free_fcn(elem->data);
Packit Service a1bd4f
    g_slice_free(NMCListElem, elem);
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_c_list_elem_free(NMCListElem *elem)
Packit 5756e2
{
Packit Service a1bd4f
    return nm_c_list_elem_free_full(elem, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline void *
Packit Service a1bd4f
nm_c_list_elem_free_steal(NMCListElem *elem)
Packit 5756e2
{
Packit Service a1bd4f
    gpointer data;
Packit 5756e2
Packit Service a1bd4f
    if (!elem)
Packit Service a1bd4f
        return NULL;
Packit Service a1bd4f
    data = elem->data;
Packit Service a1bd4f
    nm_c_list_elem_free_full(elem, NULL);
Packit Service a1bd4f
    return data;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline void
Packit Service a1bd4f
nm_c_list_elem_free_all(CList *head, GDestroyNotify free_fcn)
Packit 5756e2
{
Packit Service a1bd4f
    NMCListElem *elem;
Packit 5756e2
Packit Service a1bd4f
    while ((elem = c_list_first_entry(head, NMCListElem, lst)))
Packit Service a1bd4f
        nm_c_list_elem_free_full(elem, free_fcn);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
#define nm_c_list_elem_find_first(head, arg, predicate) \
Packit Service a1bd4f
    ({                                                  \
Packit Service a1bd4f
        CList *const _head   = (head);                  \
Packit Service a1bd4f
        NMCListElem *_result = NULL;                    \
Packit Service a1bd4f
        NMCListElem *_elem;                             \
Packit Service a1bd4f
                                                        \
Packit Service a1bd4f
        c_list_for_each_entry (_elem, _head, lst) {     \
Packit Service a1bd4f
            void *const arg = _elem->data;              \
Packit Service a1bd4f
                                                        \
Packit Service a1bd4f
            if (predicate) {                            \
Packit Service a1bd4f
                _result = _elem;                        \
Packit Service a1bd4f
                break;                                  \
Packit Service a1bd4f
            }                                           \
Packit Service a1bd4f
        }                                               \
Packit Service a1bd4f
        _result;                                        \
Packit Service a1bd4f
    })
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_c_list_elem_find_first_ptr:
Packit 5756e2
 * @head: the @CList head of a list containing #NMCListElem elements.
Packit 5756e2
 *   Note that the head is not itself part of the list.
Packit 5756e2
 * @needle: the needle pointer.
Packit 5756e2
 *
Packit 5756e2
 * Iterates the list and returns the first #NMCListElem with the matching @needle,
Packit 5756e2
 * using pointer equality.
Packit 5756e2
 *
Packit 5756e2
 * Returns: the found list element or %NULL if not found.
Packit 5756e2
 */
Packit 5756e2
static inline NMCListElem *
Packit Service a1bd4f
nm_c_list_elem_find_first_ptr(CList *head, gconstpointer needle)
Packit 5756e2
{
Packit Service a1bd4f
    return nm_c_list_elem_find_first(head, x, x == needle);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_c_list_move_before:
Packit 5756e2
 * @lst: the list element to which @elem will be prepended.
Packit 5756e2
 * @elem: the list element to move.
Packit 5756e2
 *
Packit 5756e2
 * This unlinks @elem from the current list and linkes it before
Packit 5756e2
 * @lst. This is like c_list_link_before(), except that @elem must
Packit 5756e2
 * be initialized and linked. Note that @elem may be linked in @lst
Packit 5756e2
 * or in another list. In both cases it gets moved.
Packit 5756e2
 *
Packit 5756e2
 * Returns: %TRUE if there were any changes. %FALSE if elem was already
Packit 5756e2
 *   linked at the right place.
Packit 5756e2
 */
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_c_list_move_before(CList *lst, CList *elem)
Packit 5756e2
{
Packit Service a1bd4f
    nm_assert(lst);
Packit Service a1bd4f
    nm_assert(elem);
Packit Service a1bd4f
Packit Service a1bd4f
    if (lst != elem && lst->prev != elem) {
Packit Service a1bd4f
        c_list_unlink_stale(elem);
Packit Service a1bd4f
        c_list_link_before(lst, elem);
Packit Service a1bd4f
        return TRUE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
    return FALSE;
Packit 5756e2
}
Packit Service a1bd4f
#define nm_c_list_move_tail(lst, elem) nm_c_list_move_before(lst, elem)
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nm_c_list_move_after:
Packit 5756e2
 * @lst: the list element to which @elem will be prepended.
Packit 5756e2
 * @elem: the list element to move.
Packit 5756e2
 *
Packit 5756e2
 * This unlinks @elem from the current list and linkes it after
Packit 5756e2
 * @lst. This is like c_list_link_after(), except that @elem must
Packit 5756e2
 * be initialized and linked. Note that @elem may be linked in @lst
Packit 5756e2
 * or in another list. In both cases it gets moved.
Packit 5756e2
 *
Packit 5756e2
 * Returns: %TRUE if there were any changes. %FALSE if elem was already
Packit 5756e2
 *   linked at the right place.
Packit 5756e2
 */
Packit 5756e2
static inline gboolean
Packit Service a1bd4f
nm_c_list_move_after(CList *lst, CList *elem)
Packit 5756e2
{
Packit Service a1bd4f
    nm_assert(lst);
Packit Service a1bd4f
    nm_assert(elem);
Packit Service a1bd4f
Packit Service a1bd4f
    if (lst != elem && lst->next != elem) {
Packit Service a1bd4f
        c_list_unlink_stale(elem);
Packit Service a1bd4f
        c_list_link_after(lst, elem);
Packit Service a1bd4f
        return TRUE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
    return FALSE;
Packit 5756e2
}
Packit Service a1bd4f
#define nm_c_list_move_front(lst, elem) nm_c_list_move_after(lst, elem)
Packit Service a1bd4f
Packit Service a1bd4f
#define nm_c_list_free_all(lst, type, member, destroy_fcn)         \
Packit Service a1bd4f
    G_STMT_START                                                   \
Packit Service a1bd4f
    {                                                              \
Packit Service a1bd4f
        CList *const _lst = (lst);                                 \
Packit Service a1bd4f
        type *       _elem;                                        \
Packit Service a1bd4f
                                                                   \
Packit Service a1bd4f
        while ((_elem = c_list_first_entry(_lst, type, member))) { \
Packit Service a1bd4f
            destroy_fcn(_elem);                                    \
Packit Service a1bd4f
        }                                                          \
Packit Service a1bd4f
    }                                                              \
Packit Service a1bd4f
    G_STMT_END
Packit 5756e2
Packit 5756e2
#endif /* __NM_C_LIST_H__ */