11bd4fe43Sopenharmony_ci/*
21bd4fe43Sopenharmony_ci * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
31bd4fe43Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41bd4fe43Sopenharmony_ci * you may not use this file except in compliance with the License.
51bd4fe43Sopenharmony_ci * You may obtain a copy of the License at
61bd4fe43Sopenharmony_ci *
71bd4fe43Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81bd4fe43Sopenharmony_ci *
91bd4fe43Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101bd4fe43Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111bd4fe43Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121bd4fe43Sopenharmony_ci * See the License for the specific language governing permissions and
131bd4fe43Sopenharmony_ci * limitations under the License.
141bd4fe43Sopenharmony_ci */
151bd4fe43Sopenharmony_ci
161bd4fe43Sopenharmony_ci#ifndef __HI_LIST_H__
171bd4fe43Sopenharmony_ci#define __HI_LIST_H__
181bd4fe43Sopenharmony_ci
191bd4fe43Sopenharmony_ci#ifndef _LINUX_LIST_H
201bd4fe43Sopenharmony_ci
211bd4fe43Sopenharmony_ci#ifdef __KERNEL__
221bd4fe43Sopenharmony_ci
231bd4fe43Sopenharmony_ci#include <linux/types.h>
241bd4fe43Sopenharmony_ci#else
251bd4fe43Sopenharmony_ci
261bd4fe43Sopenharmony_ci#include <stdint.h>
271bd4fe43Sopenharmony_ci#endif
281bd4fe43Sopenharmony_ci
291bd4fe43Sopenharmony_ci#define INIT_LIST_HEAD(ptr) \
301bd4fe43Sopenharmony_cido {                        \
311bd4fe43Sopenharmony_ci    (ptr)->next = (ptr);    \
321bd4fe43Sopenharmony_ci    (ptr)->prev = (ptr);    \
331bd4fe43Sopenharmony_ci} while (0)
341bd4fe43Sopenharmony_ci
351bd4fe43Sopenharmony_ci#define LIST_HEAD_INIT(name) { &(name), &(name) }
361bd4fe43Sopenharmony_ci
371bd4fe43Sopenharmony_cistruct list_head {
381bd4fe43Sopenharmony_ci    struct list_head *next, *prev;
391bd4fe43Sopenharmony_ci};
401bd4fe43Sopenharmony_ci
411bd4fe43Sopenharmony_cistatic inline void __list_add(struct list_head *_new, struct list_head *prev, struct list_head *next)
421bd4fe43Sopenharmony_ci{
431bd4fe43Sopenharmony_ci    next->prev = _new;
441bd4fe43Sopenharmony_ci    _new->next = next;
451bd4fe43Sopenharmony_ci    _new->prev = prev;
461bd4fe43Sopenharmony_ci    prev->next = _new;
471bd4fe43Sopenharmony_ci}
481bd4fe43Sopenharmony_ci
491bd4fe43Sopenharmony_cistatic inline void list_add(struct list_head *_new, struct list_head *head)
501bd4fe43Sopenharmony_ci{
511bd4fe43Sopenharmony_ci    __list_add(_new, head, head->next);
521bd4fe43Sopenharmony_ci}
531bd4fe43Sopenharmony_ci
541bd4fe43Sopenharmony_cistatic inline void list_add_tail(struct list_head *_new, struct list_head *head)
551bd4fe43Sopenharmony_ci{
561bd4fe43Sopenharmony_ci    __list_add(_new, head->prev, head);
571bd4fe43Sopenharmony_ci}
581bd4fe43Sopenharmony_ci
591bd4fe43Sopenharmony_cistatic inline void __list_del(struct list_head *prev, struct list_head *next)
601bd4fe43Sopenharmony_ci{
611bd4fe43Sopenharmony_ci    next->prev = prev;
621bd4fe43Sopenharmony_ci    prev->next = next;
631bd4fe43Sopenharmony_ci}
641bd4fe43Sopenharmony_ci
651bd4fe43Sopenharmony_cistatic inline void list_del(struct list_head *entry)
661bd4fe43Sopenharmony_ci{
671bd4fe43Sopenharmony_ci    __list_del(entry->prev, entry->next);
681bd4fe43Sopenharmony_ci}
691bd4fe43Sopenharmony_ci
701bd4fe43Sopenharmony_cistatic inline void list_del_init(struct list_head *entry)
711bd4fe43Sopenharmony_ci{
721bd4fe43Sopenharmony_ci    __list_del(entry->prev, entry->next);
731bd4fe43Sopenharmony_ci    INIT_LIST_HEAD(entry);
741bd4fe43Sopenharmony_ci}
751bd4fe43Sopenharmony_ci
761bd4fe43Sopenharmony_cistatic inline void list_move(struct list_head *list, struct list_head *head)
771bd4fe43Sopenharmony_ci{
781bd4fe43Sopenharmony_ci    __list_del(list->prev, list->next);
791bd4fe43Sopenharmony_ci    list_add(list, head);
801bd4fe43Sopenharmony_ci}
811bd4fe43Sopenharmony_ci
821bd4fe43Sopenharmony_cistatic inline void list_move_tail(struct list_head *list,
831bd4fe43Sopenharmony_ci                                  struct list_head *head)
841bd4fe43Sopenharmony_ci{
851bd4fe43Sopenharmony_ci    __list_del(list->prev, list->next);
861bd4fe43Sopenharmony_ci    list_add_tail(list, head);
871bd4fe43Sopenharmony_ci}
881bd4fe43Sopenharmony_ci
891bd4fe43Sopenharmony_cistatic inline int list_empty(struct list_head *head)
901bd4fe43Sopenharmony_ci{
911bd4fe43Sopenharmony_ci    return head->next == head;
921bd4fe43Sopenharmony_ci}
931bd4fe43Sopenharmony_ci
941bd4fe43Sopenharmony_cistatic inline void __list_splice(struct list_head *list,
951bd4fe43Sopenharmony_ci                                 struct list_head *head)
961bd4fe43Sopenharmony_ci{
971bd4fe43Sopenharmony_ci    struct list_head *first = list->next;
981bd4fe43Sopenharmony_ci    struct list_head *last = list->prev;
991bd4fe43Sopenharmony_ci    struct list_head *at = head->next;
1001bd4fe43Sopenharmony_ci
1011bd4fe43Sopenharmony_ci    first->prev = head;
1021bd4fe43Sopenharmony_ci    head->next = first;
1031bd4fe43Sopenharmony_ci
1041bd4fe43Sopenharmony_ci    last->next = at;
1051bd4fe43Sopenharmony_ci    at->prev = last;
1061bd4fe43Sopenharmony_ci}
1071bd4fe43Sopenharmony_ci
1081bd4fe43Sopenharmony_cistatic inline void list_splice(struct list_head *list, struct list_head *head)
1091bd4fe43Sopenharmony_ci{
1101bd4fe43Sopenharmony_ci    if (!list_empty(list)) {
1111bd4fe43Sopenharmony_ci        __list_splice(list, head);
1121bd4fe43Sopenharmony_ci    }
1131bd4fe43Sopenharmony_ci}
1141bd4fe43Sopenharmony_ci
1151bd4fe43Sopenharmony_cistatic inline void list_splice_init(struct list_head *list, struct list_head *head)
1161bd4fe43Sopenharmony_ci{
1171bd4fe43Sopenharmony_ci    if (!list_empty(list)) {
1181bd4fe43Sopenharmony_ci        __list_splice(list, head);
1191bd4fe43Sopenharmony_ci        INIT_LIST_HEAD(list);
1201bd4fe43Sopenharmony_ci    }
1211bd4fe43Sopenharmony_ci}
1221bd4fe43Sopenharmony_ci
1231bd4fe43Sopenharmony_ci#define list_entry(ptr, type, member) \
1241bd4fe43Sopenharmony_ci    ((type *)((uintptr_t)(ptr) - ((unsigned long)(&((type *)1)->member) - 1)))
1251bd4fe43Sopenharmony_ci
1261bd4fe43Sopenharmony_ci#define list_for_each(pos, head) \
1271bd4fe43Sopenharmony_ci    for (pos = (head)->next; pos != (head); pos = pos->next)
1281bd4fe43Sopenharmony_ci
1291bd4fe43Sopenharmony_ci#define list_for_each_safe(pos, n, head) \
1301bd4fe43Sopenharmony_ci    for (pos = (head)->next, n = pos->next; pos != (head); \
1311bd4fe43Sopenharmony_ci         pos = n, n = pos->next)
1321bd4fe43Sopenharmony_ci
1331bd4fe43Sopenharmony_ci#define get_first_item(attached, type, member) \
1341bd4fe43Sopenharmony_ci    ((type *)((char *)((attached)->next) - (unsigned long)(&((type *)0)->member)))
1351bd4fe43Sopenharmony_ci
1361bd4fe43Sopenharmony_ci#endif
1371bd4fe43Sopenharmony_ci
1381bd4fe43Sopenharmony_ci#endif
139