Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

466 lines
15KB

  1. /*****************************************************************************
  2. *
  3. * Linux kernel header adapted for user-mode
  4. * The 2.6.17-rt1 version was used.
  5. *
  6. * Original copyright holders of this code are unknown, they were not
  7. * mentioned in the original file.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. *****************************************************************************/
  23. #ifndef LINUX_LIST_H_INCLUDED
  24. #define LINUX_LIST_H_INCLUDED
  25. /* This file is from Linux Kernel (include/linux/list.h)
  26. * and modified by removing hardware prefetching of list items
  27. * and added list_split_tail* functions.
  28. *
  29. * Here by copyright, credits attributed to wherever they belong.
  30. * Filipe Coelho (aka falkTX <falktx@falktx.com>)
  31. */
  32. #ifdef __cplusplus
  33. # include <cstddef>
  34. #else
  35. # include <stddef.h>
  36. #endif
  37. #define __THROW
  38. #ifndef offsetof
  39. # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  40. #endif
  41. /**
  42. * container_of - cast a member of a structure out to the containing structure
  43. * @ptr: the pointer to the member.
  44. * @type: the type of the container struct this is embedded in.
  45. * @member: the name of the member within the struct.
  46. *
  47. */
  48. #define container_of(ptr, type, member) ({ \
  49. typeof( ((type *)0)->member ) *__mptr = (ptr); \
  50. (type *)( (char *)__mptr - offsetof(type, member) );})
  51. #define container_of_const(ptr, type, member) ({ \
  52. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  53. (const type *)( (const char *)__mptr - offsetof(type, member) );})
  54. #define prefetch(x) (x = x)
  55. /*
  56. * These are non-NULL pointers that will result in page faults
  57. * under normal circumstances, used to verify that nobody uses
  58. * non-initialized list entries.
  59. */
  60. #define LIST_POISON1 ((void *) 0x00100100)
  61. #define LIST_POISON2 ((void *) 0x00200200)
  62. /*
  63. * Simple doubly linked list implementation.
  64. *
  65. * Some of the internal functions ("__xxx") are useful when
  66. * manipulating whole lists rather than single entries, as
  67. * sometimes we already know the next/prev entries and we can
  68. * generate better code by using them directly rather than
  69. * using the generic single-entry routines.
  70. */
  71. struct list_head {
  72. struct list_head *next, *prev;
  73. };
  74. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  75. #define LIST_HEAD(name) \
  76. struct list_head name = LIST_HEAD_INIT(name)
  77. static inline void INIT_LIST_HEAD(struct list_head *list) __THROW
  78. {
  79. list->next = list;
  80. list->prev = list;
  81. }
  82. /*
  83. * Insert a new entry between two known consecutive entries.
  84. *
  85. * This is only for internal list manipulation where we know
  86. * the prev/next entries already!
  87. */
  88. static inline void __list_add(struct list_head *new_, struct list_head *prev, struct list_head *next) __THROW
  89. {
  90. next->prev = new_;
  91. new_->next = next;
  92. new_->prev = prev;
  93. prev->next = new_;
  94. }
  95. /**
  96. * list_add - add a new entry
  97. * @new: new entry to be added
  98. * @head: list head to add it after
  99. *
  100. * Insert a new entry after the specified head.
  101. * This is good for implementing stacks.
  102. */
  103. static inline void list_add(struct list_head *new_, struct list_head *head) __THROW
  104. {
  105. __list_add(new_, head, head->next);
  106. }
  107. /**
  108. * list_add_tail - add a new entry
  109. * @new: new entry to be added
  110. * @head: list head to add it before
  111. *
  112. * Insert a new entry before the specified head.
  113. * This is useful for implementing queues.
  114. */
  115. static inline void list_add_tail(struct list_head *new_, struct list_head *head) __THROW
  116. {
  117. __list_add(new_, head->prev, head);
  118. }
  119. /*
  120. * Delete a list entry by making the prev/next entries
  121. * point to each other.
  122. *
  123. * This is only for internal list manipulation where we know
  124. * the prev/next entries already!
  125. */
  126. static inline void __list_del(struct list_head *prev, struct list_head *next) __THROW
  127. {
  128. next->prev = prev;
  129. prev->next = next;
  130. }
  131. /**
  132. * list_del - deletes entry from list.
  133. * @entry: the element to delete from the list.
  134. * Note: list_empty on entry does not return true after this, the entry is
  135. * in an undefined state.
  136. */
  137. static inline void list_del(struct list_head *entry) __THROW
  138. {
  139. __list_del(entry->prev, entry->next);
  140. entry->next = (struct list_head*)LIST_POISON1;
  141. entry->prev = (struct list_head*)LIST_POISON2;
  142. }
  143. /**
  144. * list_del_init - deletes entry from list and reinitialize it.
  145. * @entry: the element to delete from the list.
  146. */
  147. static inline void list_del_init(struct list_head *entry) __THROW
  148. {
  149. __list_del(entry->prev, entry->next);
  150. INIT_LIST_HEAD(entry);
  151. }
  152. /**
  153. * list_move - delete from one list and add as another's head
  154. * @list: the entry to move
  155. * @head: the head that will precede our entry
  156. */
  157. static inline void list_move(struct list_head *list, struct list_head *head) __THROW
  158. {
  159. __list_del(list->prev, list->next);
  160. list_add(list, head);
  161. }
  162. /**
  163. * list_move_tail - delete from one list and add as another's tail
  164. * @list: the entry to move
  165. * @head: the head that will follow our entry
  166. */
  167. static inline void list_move_tail(struct list_head *list, struct list_head *head) __THROW
  168. {
  169. __list_del(list->prev, list->next);
  170. list_add_tail(list, head);
  171. }
  172. /**
  173. * list_empty - tests whether a list is empty
  174. * @head: the list to test.
  175. */
  176. static inline int list_empty(const struct list_head *head) __THROW
  177. {
  178. return head->next == head;
  179. }
  180. /**
  181. * list_empty_careful - tests whether a list is
  182. * empty _and_ checks that no other CPU might be
  183. * in the process of still modifying either member
  184. *
  185. * NOTE: using list_empty_careful() without synchronization
  186. * can only be safe if the only activity that can happen
  187. * to the list entry is list_del_init(). Eg. it cannot be used
  188. * if another CPU could re-list_add() it.
  189. *
  190. * @head: the list to test.
  191. */
  192. static inline int list_empty_careful(const struct list_head *head) __THROW
  193. {
  194. struct list_head *next = head->next;
  195. return (next == head) && (next == head->prev);
  196. }
  197. static inline void __list_splice(struct list_head *list, struct list_head *head) __THROW
  198. {
  199. struct list_head *first = list->next;
  200. struct list_head *last = list->prev;
  201. struct list_head *at = head->next;
  202. first->prev = head;
  203. head->next = first;
  204. last->next = at;
  205. at->prev = last;
  206. }
  207. static inline void __list_splice_tail(struct list_head *list, struct list_head *head) __THROW
  208. {
  209. struct list_head *first = list->next;
  210. struct list_head *last = list->prev;
  211. struct list_head *at = head->prev;
  212. first->prev = at;
  213. at->next = first;
  214. last->next = head;
  215. head->prev = last;
  216. }
  217. /**
  218. * list_splice - join two lists
  219. * @list: the new list to add.
  220. * @head: the place to add it in the first list.
  221. */
  222. static inline void list_splice(struct list_head *list, struct list_head *head) __THROW
  223. {
  224. if (!list_empty(list))
  225. __list_splice(list, head);
  226. }
  227. /**
  228. * list_splice_tail - join two lists
  229. * @list: the new list to add.
  230. * @head: the place to add it in the first list.
  231. *
  232. * @list goes to the end (at head->prev)
  233. */
  234. static inline void list_splice_tail(struct list_head *list, struct list_head *head) __THROW
  235. {
  236. if (!list_empty(list))
  237. __list_splice_tail(list, head);
  238. }
  239. /**
  240. * list_splice_init - join two lists and reinitialise the emptied list.
  241. * @list: the new list to add.
  242. * @head: the place to add it in the first list.
  243. *
  244. * The list at @list is reinitialised
  245. */
  246. static inline void list_splice_init(struct list_head *list, struct list_head *head) __THROW
  247. {
  248. if (!list_empty(list)) {
  249. __list_splice(list, head);
  250. INIT_LIST_HEAD(list);
  251. }
  252. }
  253. /**
  254. * list_splice_tail_init - join two lists and reinitialise the emptied list.
  255. * @list: the new list to add.
  256. * @head: the place to add it in the first list.
  257. *
  258. * The list at @list is reinitialised
  259. * @list goes to the end (at head->prev)
  260. */
  261. static inline void list_splice_tail_init(struct list_head *list, struct list_head *head) __THROW
  262. {
  263. if (!list_empty(list)) {
  264. __list_splice_tail(list, head);
  265. INIT_LIST_HEAD(list);
  266. }
  267. }
  268. /**
  269. * list_entry - get the struct for this entry
  270. * @ptr: the &struct list_head pointer.
  271. * @type: the type of the struct this is embedded in.
  272. * @member: the name of the list_struct within the struct.
  273. */
  274. #if defined(__GNUC__) && ! defined(__STRICT_ANSI__)
  275. # define list_entry(ptr, type, member) \
  276. container_of(ptr, type, member)
  277. # define list_entry_const(ptr, type, member) \
  278. container_of_const(ptr, type, member)
  279. #else
  280. # define list_entry(ptr, type, member) \
  281. ((type *)((char *)(ptr)-offsetof(type, member)))
  282. # define list_entry_const(ptr, type, member) \
  283. ((const type *)((const char *)(ptr)-offsetof(type, member)))
  284. #endif
  285. /**
  286. * list_for_each - iterate over a list
  287. * @pos: the &struct list_head to use as a loop counter.
  288. * @head: the head for your list.
  289. */
  290. #define list_for_each(pos, head) \
  291. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  292. pos = pos->next)
  293. /**
  294. * __list_for_each - iterate over a list
  295. * @pos: the &struct list_head to use as a loop counter.
  296. * @head: the head for your list.
  297. *
  298. * This variant differs from list_for_each() in that it's the
  299. * simplest possible list iteration code, no prefetching is done.
  300. * Use this for code that knows the list to be very short (empty
  301. * or 1 entry) most of the time.
  302. */
  303. #define __list_for_each(pos, head) \
  304. for (pos = (head)->next; pos != (head); pos = pos->next)
  305. /**
  306. * list_for_each_prev - iterate over a list backwards
  307. * @pos: the &struct list_head to use as a loop counter.
  308. * @head: the head for your list.
  309. */
  310. #define list_for_each_prev(pos, head) \
  311. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  312. pos = pos->prev)
  313. /**
  314. * list_for_each_safe - iterate over a list safe against removal of list entry
  315. * @pos: the &struct list_head to use as a loop counter.
  316. * @n: another &struct list_head to use as temporary storage
  317. * @head: the head for your list.
  318. */
  319. #define list_for_each_safe(pos, n, head) \
  320. for (pos = (head)->next, n = pos->next; pos != (head); \
  321. pos = n, n = pos->next)
  322. /**
  323. * list_for_each_entry - iterate over list of given type
  324. * @pos: the type * to use as a loop counter.
  325. * @head: the head for your list.
  326. * @member: the name of the list_struct within the struct.
  327. */
  328. #define list_for_each_entry(pos, head, member) \
  329. for (pos = list_entry((head)->next, typeof(*pos), member); \
  330. prefetch(pos->member.next), &pos->member != (head); \
  331. pos = list_entry(pos->member.next, typeof(*pos), member))
  332. /**
  333. * list_for_each_entry_reverse - iterate backwards over list of given type.
  334. * @pos: the type * to use as a loop counter.
  335. * @head: the head for your list.
  336. * @member: the name of the list_struct within the struct.
  337. */
  338. #define list_for_each_entry_reverse(pos, head, member) \
  339. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  340. prefetch(pos->member.prev), &pos->member != (head); \
  341. pos = list_entry(pos->member.prev, typeof(*pos), member))
  342. /**
  343. * list_prepare_entry - prepare a pos entry for use as a start point in list_for_each_entry_continue
  344. * @pos: the type * to use as a start point
  345. * @head: the head of the list
  346. * @member: the name of the list_struct within the struct.
  347. */
  348. #define list_prepare_entry(pos, head, member) \
  349. ((pos) ? : list_entry(head, typeof(*pos), member))
  350. /**
  351. * list_for_each_entry_continue - iterate over list of given type continuing after existing point
  352. * @pos: the type * to use as a loop counter.
  353. * @head: the head for your list.
  354. * @member: the name of the list_struct within the struct.
  355. */
  356. #define list_for_each_entry_continue(pos, head, member) \
  357. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  358. prefetch(pos->member.next), &pos->member != (head); \
  359. pos = list_entry(pos->member.next, typeof(*pos), member))
  360. /**
  361. * list_for_each_entry_from - iterate over list of given type continuing from existing point
  362. * @pos: the type * to use as a loop counter.
  363. * @head: the head for your list.
  364. * @member: the name of the list_struct within the struct.
  365. */
  366. #define list_for_each_entry_from(pos, head, member) \
  367. for (; prefetch(pos->member.next), &pos->member != (head); \
  368. pos = list_entry(pos->member.next, typeof(*pos), member))
  369. /**
  370. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  371. * @pos: the type * to use as a loop counter.
  372. * @n: another type * to use as temporary storage
  373. * @head: the head for your list.
  374. * @member: the name of the list_struct within the struct.
  375. */
  376. #define list_for_each_entry_safe(pos, n, head, member) \
  377. for (pos = list_entry((head)->next, typeof(*pos), member), \
  378. n = list_entry(pos->member.next, typeof(*pos), member); \
  379. &pos->member != (head); \
  380. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  381. /**
  382. * list_for_each_entry_safe_continue - iterate over list of given type continuing after existing point safe against removal of list entry
  383. * @pos: the type * to use as a loop counter.
  384. * @n: another type * to use as temporary storage
  385. * @head: the head for your list.
  386. * @member: the name of the list_struct within the struct.
  387. */
  388. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  389. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  390. n = list_entry(pos->member.next, typeof(*pos), member); \
  391. &pos->member != (head); \
  392. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  393. /**
  394. * list_for_each_entry_safe_from - iterate over list of given type from existing point safe against removal of list entry
  395. * @pos: the type * to use as a loop counter.
  396. * @n: another type * to use as temporary storage
  397. * @head: the head for your list.
  398. * @member: the name of the list_struct within the struct.
  399. */
  400. #define list_for_each_entry_safe_from(pos, n, head, member) \
  401. for (n = list_entry(pos->member.next, typeof(*pos), member); \
  402. &pos->member != (head); \
  403. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  404. /**
  405. * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against removal of list entry
  406. * @pos: the type * to use as a loop counter.
  407. * @n: another type * to use as temporary storage
  408. * @head: the head for your list.
  409. * @member: the name of the list_struct within the struct.
  410. */
  411. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  412. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  413. n = list_entry(pos->member.prev, typeof(*pos), member); \
  414. &pos->member != (head); \
  415. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  416. #endif // LINUX_LIST_H_INCLUDED