1d722e3fbSopenharmony_ci/************************************************************************** 2d722e3fbSopenharmony_ci * 3d722e3fbSopenharmony_ci * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA. 4d722e3fbSopenharmony_ci * All Rights Reserved. 5d722e3fbSopenharmony_ci * 6d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the 8d722e3fbSopenharmony_ci * "Software"), to deal in the Software without restriction, including 9d722e3fbSopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10d722e3fbSopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11d722e3fbSopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12d722e3fbSopenharmony_ci * the following conditions: 13d722e3fbSopenharmony_ci * 14d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 17d722e3fbSopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 18d722e3fbSopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19d722e3fbSopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20d722e3fbSopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 21d722e3fbSopenharmony_ci * 22d722e3fbSopenharmony_ci * The above copyright notice and this permission notice (including the 23d722e3fbSopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 24d722e3fbSopenharmony_ci * of the Software. 25d722e3fbSopenharmony_ci */ 26d722e3fbSopenharmony_ci 27d722e3fbSopenharmony_ci/* 28d722e3fbSopenharmony_ci * List macros heavily inspired by the Linux kernel 29d722e3fbSopenharmony_ci * list handling. No list looping yet. 30d722e3fbSopenharmony_ci */ 31d722e3fbSopenharmony_ci 32d722e3fbSopenharmony_ci#include <stddef.h> 33d722e3fbSopenharmony_ci 34d722e3fbSopenharmony_citypedef struct _drmMMListHead 35d722e3fbSopenharmony_ci{ 36d722e3fbSopenharmony_ci struct _drmMMListHead *prev; 37d722e3fbSopenharmony_ci struct _drmMMListHead *next; 38d722e3fbSopenharmony_ci} drmMMListHead; 39d722e3fbSopenharmony_ci 40d722e3fbSopenharmony_ci#define DRMINITLISTHEAD(__item) \ 41d722e3fbSopenharmony_ci do{ \ 42d722e3fbSopenharmony_ci (__item)->prev = (__item); \ 43d722e3fbSopenharmony_ci (__item)->next = (__item); \ 44d722e3fbSopenharmony_ci } while (0) 45d722e3fbSopenharmony_ci 46d722e3fbSopenharmony_ci#define DRMLISTADD(__item, __list) \ 47d722e3fbSopenharmony_ci do { \ 48d722e3fbSopenharmony_ci (__item)->prev = (__list); \ 49d722e3fbSopenharmony_ci (__item)->next = (__list)->next; \ 50d722e3fbSopenharmony_ci (__list)->next->prev = (__item); \ 51d722e3fbSopenharmony_ci (__list)->next = (__item); \ 52d722e3fbSopenharmony_ci } while (0) 53d722e3fbSopenharmony_ci 54d722e3fbSopenharmony_ci#define DRMLISTADDTAIL(__item, __list) \ 55d722e3fbSopenharmony_ci do { \ 56d722e3fbSopenharmony_ci (__item)->next = (__list); \ 57d722e3fbSopenharmony_ci (__item)->prev = (__list)->prev; \ 58d722e3fbSopenharmony_ci (__list)->prev->next = (__item); \ 59d722e3fbSopenharmony_ci (__list)->prev = (__item); \ 60d722e3fbSopenharmony_ci } while(0) 61d722e3fbSopenharmony_ci 62d722e3fbSopenharmony_ci#define DRMLISTDEL(__item) \ 63d722e3fbSopenharmony_ci do { \ 64d722e3fbSopenharmony_ci (__item)->prev->next = (__item)->next; \ 65d722e3fbSopenharmony_ci (__item)->next->prev = (__item)->prev; \ 66d722e3fbSopenharmony_ci } while(0) 67d722e3fbSopenharmony_ci 68d722e3fbSopenharmony_ci#define DRMLISTDELINIT(__item) \ 69d722e3fbSopenharmony_ci do { \ 70d722e3fbSopenharmony_ci (__item)->prev->next = (__item)->next; \ 71d722e3fbSopenharmony_ci (__item)->next->prev = (__item)->prev; \ 72d722e3fbSopenharmony_ci (__item)->next = (__item); \ 73d722e3fbSopenharmony_ci (__item)->prev = (__item); \ 74d722e3fbSopenharmony_ci } while(0) 75d722e3fbSopenharmony_ci 76d722e3fbSopenharmony_ci#define DRMLISTENTRY(__type, __item, __field) \ 77d722e3fbSopenharmony_ci ((__type *)(((char *) (__item)) - offsetof(__type, __field))) 78d722e3fbSopenharmony_ci 79d722e3fbSopenharmony_ci#define DRMLISTEMPTY(__item) ((__item)->next == (__item)) 80d722e3fbSopenharmony_ci 81d722e3fbSopenharmony_ci#define DRMLISTSINGLE(__list) \ 82d722e3fbSopenharmony_ci (!DRMLISTEMPTY(__list) && ((__list)->next == (__list)->prev)) 83d722e3fbSopenharmony_ci 84d722e3fbSopenharmony_ci#define DRMLISTFOREACH(__item, __list) \ 85d722e3fbSopenharmony_ci for ((__item) = (__list)->next; \ 86d722e3fbSopenharmony_ci (__item) != (__list); (__item) = (__item)->next) 87d722e3fbSopenharmony_ci 88d722e3fbSopenharmony_ci#define DRMLISTFOREACHSAFE(__item, __temp, __list) \ 89d722e3fbSopenharmony_ci for ((__item) = (__list)->next, (__temp) = (__item)->next; \ 90d722e3fbSopenharmony_ci (__item) != (__list); \ 91d722e3fbSopenharmony_ci (__item) = (__temp), (__temp) = (__item)->next) 92d722e3fbSopenharmony_ci 93d722e3fbSopenharmony_ci#define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list) \ 94d722e3fbSopenharmony_ci for ((__item) = (__list)->prev, (__temp) = (__item)->prev; \ 95d722e3fbSopenharmony_ci (__item) != (__list); \ 96d722e3fbSopenharmony_ci (__item) = (__temp), (__temp) = (__item)->prev) 97d722e3fbSopenharmony_ci 98d722e3fbSopenharmony_ci#define DRMLISTFOREACHENTRY(__item, __list, __head) \ 99d722e3fbSopenharmony_ci for ((__item) = DRMLISTENTRY(__typeof__(*__item), (__list)->next, __head); \ 100d722e3fbSopenharmony_ci &(__item)->__head != (__list); \ 101d722e3fbSopenharmony_ci (__item) = DRMLISTENTRY(__typeof__(*__item), \ 102d722e3fbSopenharmony_ci (__item)->__head.next, __head)) 103d722e3fbSopenharmony_ci 104d722e3fbSopenharmony_ci#define DRMLISTFOREACHENTRYSAFE(__item, __temp, __list, __head) \ 105d722e3fbSopenharmony_ci for ((__item) = DRMLISTENTRY(__typeof__(*__item), (__list)->next, __head), \ 106d722e3fbSopenharmony_ci (__temp) = DRMLISTENTRY(__typeof__(*__item), \ 107d722e3fbSopenharmony_ci (__item)->__head.next, __head); \ 108d722e3fbSopenharmony_ci &(__item)->__head != (__list); \ 109d722e3fbSopenharmony_ci (__item) = (__temp), \ 110d722e3fbSopenharmony_ci (__temp) = DRMLISTENTRY(__typeof__(*__item), \ 111d722e3fbSopenharmony_ci (__temp)->__head.next, __head)) 112d722e3fbSopenharmony_ci 113d722e3fbSopenharmony_ci#define DRMLISTJOIN(__list, __join) if (!DRMLISTEMPTY(__list)) { \ 114d722e3fbSopenharmony_ci (__list)->next->prev = (__join); \ 115d722e3fbSopenharmony_ci (__list)->prev->next = (__join)->next; \ 116d722e3fbSopenharmony_ci (__join)->next->prev = (__list)->prev; \ 117d722e3fbSopenharmony_ci (__join)->next = (__list)->next; \ 118d722e3fbSopenharmony_ci} 119