Cleanup headers
[urcu.git] / tests / api_gcc.h
1 #define _INCLUDE_API_H
2
3 /*
4 * common.h: Common Linux kernel-isms.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; but version 2 of the License only due
9 * to code included from the Linux kernel.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Copyright (c) 2006 Paul E. McKenney, IBM.
21 *
22 * Much code taken from the Linux kernel. For such code, the option
23 * to redistribute under later versions of GPL might not be available.
24 */
25
26 #ifndef __always_inline
27 #define __always_inline inline
28 #endif
29
30 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
31 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
32
33 #ifdef __ASSEMBLY__
34 # define stringify_in_c(...) __VA_ARGS__
35 # define ASM_CONST(x) x
36 #else
37 /* This version of stringify will deal with commas... */
38 # define __stringify_in_c(...) #__VA_ARGS__
39 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
40 # define __ASM_CONST(x) x##UL
41 # define ASM_CONST(x) __ASM_CONST(x)
42 #endif
43
44
45 /*
46 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
47 *
48 * This program is free software; you can redistribute it and/or modify
49 * it under the terms of the GNU General Public License as published by
50 * the Free Software Foundation, but version 2 only due to inclusion
51 * of Linux-kernel code.
52 *
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * GNU General Public License for more details.
57 *
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
61 *
62 * Copyright (c) 2006 Paul E. McKenney, IBM.
63 *
64 * Much code taken from the Linux kernel. For such code, the option
65 * to redistribute under later versions of GPL might not be available.
66 */
67
68 /*
69 * Machine parameters.
70 */
71
72 #define CACHE_LINE_SIZE 64
73 #define ____cacheline_internodealigned_in_smp \
74 __attribute__((__aligned__(1 << 6)))
75
76 #define LOCK_PREFIX "lock ; "
77
78 #if 0 /* duplicate with arch_atomic.h */
79 /*
80 * Atomic data structure, initialization, and access.
81 */
82
83 typedef struct { volatile int counter; } atomic_t;
84
85 #define ATOMIC_INIT(i) { (i) }
86
87 #define atomic_read(v) ((v)->counter)
88 #define atomic_set(v, i) (((v)->counter) = (i))
89
90 /*
91 * Atomic operations.
92 */
93
94 /**
95 * atomic_add - add integer to atomic variable
96 * @i: integer value to add
97 * @v: pointer of type atomic_t
98 *
99 * Atomically adds @i to @v.
100 */
101
102 static __inline__ void atomic_add(int i, atomic_t *v)
103 {
104 (void)__sync_fetch_and_add(&v->counter, i);
105 }
106
107 /**
108 * atomic_sub - subtract the atomic variable
109 * @i: integer value to subtract
110 * @v: pointer of type atomic_t
111 *
112 * Atomically subtracts @i from @v.
113 */
114 static __inline__ void atomic_sub(int i, atomic_t *v)
115 {
116 (void)__sync_fetch_and_add(&v->counter, -i);
117 }
118
119 /**
120 * atomic_sub_and_test - subtract value from variable and test result
121 * @i: integer value to subtract
122 * @v: pointer of type atomic_t
123 *
124 * Atomically subtracts @i from @v and returns
125 * true if the result is zero, or false for all
126 * other cases.
127 */
128 static __inline__ int atomic_sub_and_test(int i, atomic_t *v)
129 {
130 return __sync_add_and_fetch(&v->counter, -i) == 0;
131 }
132
133 /**
134 * atomic_inc - increment atomic variable
135 * @v: pointer of type atomic_t
136 *
137 * Atomically increments @v by 1.
138 */
139 static __inline__ void atomic_inc(atomic_t *v)
140 {
141 (void)__sync_fetch_and_add(&v->counter, 1);
142 }
143
144 /**
145 * atomic_dec - decrement atomic variable
146 * @v: pointer of type atomic_t
147 *
148 * Atomically decrements @v by 1.
149 */
150 static __inline__ void atomic_dec(atomic_t *v)
151 {
152 (void)__sync_fetch_and_add(&v->counter, -1);
153 }
154
155 /**
156 * atomic_dec_and_test - decrement and test
157 * @v: pointer of type atomic_t
158 *
159 * Atomically decrements @v by 1 and
160 * returns true if the result is 0, or false for all other
161 * cases.
162 */
163 static __inline__ int atomic_dec_and_test(atomic_t *v)
164 {
165 return __sync_add_and_fetch(&v->counter, -1) == 0;
166 }
167
168 /**
169 * atomic_inc_and_test - increment and test
170 * @v: pointer of type atomic_t
171 *
172 * Atomically increments @v by 1
173 * and returns true if the result is zero, or false for all
174 * other cases.
175 */
176 static __inline__ int atomic_inc_and_test(atomic_t *v)
177 {
178 return __sync_add_and_fetch(&v->counter, 1) == 0;
179 }
180
181 /**
182 * atomic_add_negative - add and test if negative
183 * @v: pointer of type atomic_t
184 * @i: integer value to add
185 *
186 * Atomically adds @i to @v and returns true
187 * if the result is negative, or false when
188 * result is greater than or equal to zero.
189 */
190 static __inline__ int atomic_add_negative(int i, atomic_t *v)
191 {
192 return __sync_add_and_fetch(&v->counter, i) < 0;
193 }
194
195 /**
196 * atomic_add_return - add and return
197 * @v: pointer of type atomic_t
198 * @i: integer value to add
199 *
200 * Atomically adds @i to @v and returns @i + @v
201 */
202 static __inline__ int atomic_add_return(int i, atomic_t *v)
203 {
204 return __sync_add_and_fetch(&v->counter, i);
205 }
206
207 static __inline__ int atomic_sub_return(int i, atomic_t *v)
208 {
209 return atomic_add_return(-i,v);
210 }
211
212 static inline unsigned int
213 cmpxchg(volatile long *ptr, long oldval, long newval)
214 {
215 return __sync_val_compare_and_swap(ptr, oldval, newval);
216 }
217
218 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
219 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
220
221 /**
222 * atomic_add_unless - add unless the number is a given value
223 * @v: pointer of type atomic_t
224 * @a: the amount to add to v...
225 * @u: ...unless v is equal to u.
226 *
227 * Atomically adds @a to @v, so long as it was not @u.
228 * Returns non-zero if @v was not @u, and zero otherwise.
229 */
230 #define atomic_add_unless(v, a, u) \
231 ({ \
232 int c, old; \
233 c = atomic_read(v); \
234 for (;;) { \
235 if (unlikely(c == (u))) \
236 break; \
237 old = atomic_cmpxchg((v), c, c + (a)); \
238 if (likely(old == c)) \
239 break; \
240 c = old; \
241 } \
242 c != (u); \
243 })
244 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
245
246 #define atomic_inc_return(v) (atomic_add_return(1,v))
247 #define atomic_dec_return(v) (atomic_sub_return(1,v))
248
249 /* Atomic operations are already serializing on x86 */
250 #define smp_mb__before_atomic_dec() barrier()
251 #define smp_mb__after_atomic_dec() barrier()
252 #define smp_mb__before_atomic_inc() barrier()
253 #define smp_mb__after_atomic_inc() barrier()
254
255 #endif //0 /* duplicate with arch_atomic.h */
256
257 /*
258 * api_pthreads.h: API mapping to pthreads environment.
259 *
260 * This program is free software; you can redistribute it and/or modify
261 * it under the terms of the GNU General Public License as published by
262 * the Free Software Foundation; either version 2 of the License, or
263 * (at your option) any later version. However, please note that much
264 * of the code in this file derives from the Linux kernel, and that such
265 * code may not be available except under GPLv2.
266 *
267 * This program is distributed in the hope that it will be useful,
268 * but WITHOUT ANY WARRANTY; without even the implied warranty of
269 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
270 * GNU General Public License for more details.
271 *
272 * You should have received a copy of the GNU General Public License
273 * along with this program; if not, write to the Free Software
274 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
275 *
276 * Copyright (c) 2006 Paul E. McKenney, IBM.
277 */
278
279 #include <stdio.h>
280 #include <stdlib.h>
281 #include <errno.h>
282 #include <limits.h>
283 #include <sys/types.h>
284 #define __USE_GNU
285 #include <pthread.h>
286 #include <sched.h>
287 #include <sys/param.h>
288 /* #include "atomic.h" */
289
290 /*
291 * Compiler magic.
292 */
293 #define container_of(ptr, type, member) ({ \
294 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
295 (type *)( (char *)__mptr - offsetof(type,member) );})
296
297 /*
298 * Default machine parameters.
299 */
300
301 #ifndef CACHE_LINE_SIZE
302 #define CACHE_LINE_SIZE 128
303 #endif /* #ifndef CACHE_LINE_SIZE */
304
305 /*
306 * Exclusive locking primitives.
307 */
308
309 typedef pthread_mutex_t spinlock_t;
310
311 #define DEFINE_SPINLOCK(lock) spinlock_t lock = PTHREAD_MUTEX_INITIALIZER;
312 #define __SPIN_LOCK_UNLOCKED(lockp) PTHREAD_MUTEX_INITIALIZER
313
314 static void spin_lock_init(spinlock_t *sp)
315 {
316 if (pthread_mutex_init(sp, NULL) != 0) {
317 perror("spin_lock_init:pthread_mutex_init");
318 exit(-1);
319 }
320 }
321
322 static void spin_lock(spinlock_t *sp)
323 {
324 if (pthread_mutex_lock(sp) != 0) {
325 perror("spin_lock:pthread_mutex_lock");
326 exit(-1);
327 }
328 }
329
330 static void spin_unlock(spinlock_t *sp)
331 {
332 if (pthread_mutex_unlock(sp) != 0) {
333 perror("spin_unlock:pthread_mutex_unlock");
334 exit(-1);
335 }
336 }
337
338 #define spin_lock_irqsave(l, f) do { f = 1; spin_lock(l); } while (0)
339 #define spin_unlock_irqrestore(l, f) do { f = 0; spin_unlock(l); } while (0)
340
341 /*
342 * Thread creation/destruction primitives.
343 */
344
345 typedef pthread_t thread_id_t;
346
347 #define NR_THREADS 128
348
349 #define __THREAD_ID_MAP_EMPTY 0
350 #define __THREAD_ID_MAP_WAITING 1
351 thread_id_t __thread_id_map[NR_THREADS];
352 spinlock_t __thread_id_map_mutex;
353
354 #define for_each_thread(t) \
355 for (t = 0; t < NR_THREADS; t++)
356
357 #define for_each_running_thread(t) \
358 for (t = 0; t < NR_THREADS; t++) \
359 if ((__thread_id_map[t] != __THREAD_ID_MAP_EMPTY) && \
360 (__thread_id_map[t] != __THREAD_ID_MAP_WAITING))
361
362 pthread_key_t thread_id_key;
363
364 static int __smp_thread_id(void)
365 {
366 int i;
367 thread_id_t tid = pthread_self();
368
369 for (i = 0; i < NR_THREADS; i++) {
370 if (__thread_id_map[i] == tid) {
371 long v = i + 1; /* must be non-NULL. */
372
373 if (pthread_setspecific(thread_id_key, (void *)v) != 0) {
374 perror("pthread_setspecific");
375 exit(-1);
376 }
377 return i;
378 }
379 }
380 spin_lock(&__thread_id_map_mutex);
381 for (i = 0; i < NR_THREADS; i++) {
382 if (__thread_id_map[i] == tid)
383 spin_unlock(&__thread_id_map_mutex);
384 return i;
385 }
386 spin_unlock(&__thread_id_map_mutex);
387 fprintf(stderr, "smp_thread_id: Rogue thread, id: %d(%#x)\n",
388 (int)tid, (int)tid);
389 exit(-1);
390 }
391
392 static int smp_thread_id(void)
393 {
394 void *id;
395
396 id = pthread_getspecific(thread_id_key);
397 if (id == NULL)
398 return __smp_thread_id();
399 return (long)(id - 1);
400 }
401
402 static thread_id_t create_thread(void *(*func)(void *), void *arg)
403 {
404 thread_id_t tid;
405 int i;
406
407 spin_lock(&__thread_id_map_mutex);
408 for (i = 0; i < NR_THREADS; i++) {
409 if (__thread_id_map[i] == __THREAD_ID_MAP_EMPTY)
410 break;
411 }
412 if (i >= NR_THREADS) {
413 spin_unlock(&__thread_id_map_mutex);
414 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
415 exit(-1);
416 }
417 __thread_id_map[i] = __THREAD_ID_MAP_WAITING;
418 spin_unlock(&__thread_id_map_mutex);
419 if (pthread_create(&tid, NULL, func, arg) != 0) {
420 perror("create_thread:pthread_create");
421 exit(-1);
422 }
423 __thread_id_map[i] = tid;
424 return tid;
425 }
426
427 static void *wait_thread(thread_id_t tid)
428 {
429 int i;
430 void *vp;
431
432 for (i = 0; i < NR_THREADS; i++) {
433 if (__thread_id_map[i] == tid)
434 break;
435 }
436 if (i >= NR_THREADS){
437 fprintf(stderr, "wait_thread: bad tid = %d(%#x)\n",
438 (int)tid, (int)tid);
439 exit(-1);
440 }
441 if (pthread_join(tid, &vp) != 0) {
442 perror("wait_thread:pthread_join");
443 exit(-1);
444 }
445 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
446 return vp;
447 }
448
449 static void wait_all_threads(void)
450 {
451 int i;
452 thread_id_t tid;
453
454 for (i = 1; i < NR_THREADS; i++) {
455 tid = __thread_id_map[i];
456 if (tid != __THREAD_ID_MAP_EMPTY &&
457 tid != __THREAD_ID_MAP_WAITING)
458 (void)wait_thread(tid);
459 }
460 }
461
462 static void run_on(int cpu)
463 {
464 cpu_set_t mask;
465
466 CPU_ZERO(&mask);
467 CPU_SET(cpu, &mask);
468 sched_setaffinity(0, sizeof(mask), &mask);
469 }
470
471 /*
472 * timekeeping -- very crude -- should use MONOTONIC...
473 */
474
475 long long get_microseconds(void)
476 {
477 struct timeval tv;
478
479 if (gettimeofday(&tv, NULL) != 0)
480 abort();
481 return ((long long)tv.tv_sec) * 1000000LL + (long long)tv.tv_usec;
482 }
483
484 /*
485 * Per-thread variables.
486 */
487
488 #define DEFINE_PER_THREAD(type, name) \
489 struct { \
490 __typeof__(type) v \
491 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
492 } __per_thread_##name[NR_THREADS];
493 #define DECLARE_PER_THREAD(type, name) extern DEFINE_PER_THREAD(type, name)
494
495 #define per_thread(name, thread) __per_thread_##name[thread].v
496 #define __get_thread_var(name) per_thread(name, smp_thread_id())
497
498 #define init_per_thread(name, v) \
499 do { \
500 int __i_p_t_i; \
501 for (__i_p_t_i = 0; __i_p_t_i < NR_THREADS; __i_p_t_i++) \
502 per_thread(name, __i_p_t_i) = v; \
503 } while (0)
504
505 /*
506 * CPU traversal primitives.
507 */
508
509 #ifndef NR_CPUS
510 #define NR_CPUS 16
511 #endif /* #ifndef NR_CPUS */
512
513 #define for_each_possible_cpu(cpu) \
514 for (cpu = 0; cpu < NR_CPUS; cpu++)
515 #define for_each_online_cpu(cpu) \
516 for (cpu = 0; cpu < NR_CPUS; cpu++)
517
518 /*
519 * Per-CPU variables.
520 */
521
522 #define DEFINE_PER_CPU(type, name) \
523 struct { \
524 __typeof__(type) v \
525 __attribute__((__aligned__(CACHE_LINE_SIZE))); \
526 } __per_cpu_##name[NR_CPUS]
527 #define DECLARE_PER_CPU(type, name) extern DEFINE_PER_CPU(type, name)
528
529 DEFINE_PER_THREAD(int, smp_processor_id);
530
531 #define per_cpu(name, thread) __per_cpu_##name[thread].v
532 #define __get_cpu_var(name) per_cpu(name, smp_processor_id())
533
534 #define init_per_cpu(name, v) \
535 do { \
536 int __i_p_c_i; \
537 for (__i_p_c_i = 0; __i_p_c_i < NR_CPUS; __i_p_c_i++) \
538 per_cpu(name, __i_p_c_i) = v; \
539 } while (0)
540
541 /*
542 * CPU state checking (crowbarred).
543 */
544
545 #define idle_cpu(cpu) 0
546 #define in_softirq() 1
547 #define hardirq_count() 0
548 #define PREEMPT_SHIFT 0
549 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
550 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
551 #define PREEMPT_BITS 8
552 #define SOFTIRQ_BITS 8
553
554 /*
555 * CPU hotplug.
556 */
557
558 struct notifier_block {
559 int (*notifier_call)(struct notifier_block *, unsigned long, void *);
560 struct notifier_block *next;
561 int priority;
562 };
563
564 #define CPU_ONLINE 0x0002 /* CPU (unsigned)v is up */
565 #define CPU_UP_PREPARE 0x0003 /* CPU (unsigned)v coming up */
566 #define CPU_UP_CANCELED 0x0004 /* CPU (unsigned)v NOT coming up */
567 #define CPU_DOWN_PREPARE 0x0005 /* CPU (unsigned)v going down */
568 #define CPU_DOWN_FAILED 0x0006 /* CPU (unsigned)v NOT going down */
569 #define CPU_DEAD 0x0007 /* CPU (unsigned)v dead */
570 #define CPU_DYING 0x0008 /* CPU (unsigned)v not running any task,
571 * not handling interrupts, soon dead */
572 #define CPU_POST_DEAD 0x0009 /* CPU (unsigned)v dead, cpu_hotplug
573 * lock is dropped */
574
575 /* Used for CPU hotplug events occuring while tasks are frozen due to a suspend
576 * operation in progress
577 */
578 #define CPU_TASKS_FROZEN 0x0010
579
580 #define CPU_ONLINE_FROZEN (CPU_ONLINE | CPU_TASKS_FROZEN)
581 #define CPU_UP_PREPARE_FROZEN (CPU_UP_PREPARE | CPU_TASKS_FROZEN)
582 #define CPU_UP_CANCELED_FROZEN (CPU_UP_CANCELED | CPU_TASKS_FROZEN)
583 #define CPU_DOWN_PREPARE_FROZEN (CPU_DOWN_PREPARE | CPU_TASKS_FROZEN)
584 #define CPU_DOWN_FAILED_FROZEN (CPU_DOWN_FAILED | CPU_TASKS_FROZEN)
585 #define CPU_DEAD_FROZEN (CPU_DEAD | CPU_TASKS_FROZEN)
586 #define CPU_DYING_FROZEN (CPU_DYING | CPU_TASKS_FROZEN)
587
588 /* Hibernation and suspend events */
589 #define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
590 #define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
591 #define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
592 #define PM_POST_SUSPEND 0x0004 /* Suspend finished */
593 #define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
594 #define PM_POST_RESTORE 0x0006 /* Restore failed */
595
596 #define NOTIFY_DONE 0x0000 /* Don't care */
597 #define NOTIFY_OK 0x0001 /* Suits me */
598 #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */
599 #define NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002)
600 /* Bad/Veto action */
601 /*
602 * Clean way to return from the notifier and stop further calls.
603 */
604 #define NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK)
605
606 /*
607 * Bug checks.
608 */
609
610 #define BUG_ON(c) do { if (!(c)) abort(); } while (0)
611
612 /*
613 * Initialization -- Must be called before calling any primitives.
614 */
615
616 static void smp_init(void)
617 {
618 int i;
619
620 spin_lock_init(&__thread_id_map_mutex);
621 __thread_id_map[0] = pthread_self();
622 for (i = 1; i < NR_THREADS; i++)
623 __thread_id_map[i] = __THREAD_ID_MAP_EMPTY;
624 init_per_thread(smp_processor_id, 0);
625 if (pthread_key_create(&thread_id_key, NULL) != 0) {
626 perror("pthread_key_create");
627 exit(-1);
628 }
629 }
630
631 /* Taken from the Linux kernel source tree, so GPLv2-only!!! */
632
633 #ifndef _LINUX_LIST_H
634 #define _LINUX_LIST_H
635
636 #define LIST_POISON1 ((void *) 0x00100100)
637 #define LIST_POISON2 ((void *) 0x00200200)
638
639 #define container_of(ptr, type, member) ({ \
640 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
641 (type *)( (char *)__mptr - offsetof(type,member) );})
642
643 /*
644 * Simple doubly linked list implementation.
645 *
646 * Some of the internal functions ("__xxx") are useful when
647 * manipulating whole lists rather than single entries, as
648 * sometimes we already know the next/prev entries and we can
649 * generate better code by using them directly rather than
650 * using the generic single-entry routines.
651 */
652
653 struct list_head {
654 struct list_head *next, *prev;
655 };
656
657 #define LIST_HEAD_INIT(name) { &(name), &(name) }
658
659 #define LIST_HEAD(name) \
660 struct list_head name = LIST_HEAD_INIT(name)
661
662 static inline void INIT_LIST_HEAD(struct list_head *list)
663 {
664 list->next = list;
665 list->prev = list;
666 }
667
668 /*
669 * Insert a new entry between two known consecutive entries.
670 *
671 * This is only for internal list manipulation where we know
672 * the prev/next entries already!
673 */
674 #ifndef CONFIG_DEBUG_LIST
675 static inline void __list_add(struct list_head *new,
676 struct list_head *prev,
677 struct list_head *next)
678 {
679 next->prev = new;
680 new->next = next;
681 new->prev = prev;
682 prev->next = new;
683 }
684 #else
685 extern void __list_add(struct list_head *new,
686 struct list_head *prev,
687 struct list_head *next);
688 #endif
689
690 /**
691 * list_add - add a new entry
692 * @new: new entry to be added
693 * @head: list head to add it after
694 *
695 * Insert a new entry after the specified head.
696 * This is good for implementing stacks.
697 */
698 static inline void list_add(struct list_head *new, struct list_head *head)
699 {
700 __list_add(new, head, head->next);
701 }
702
703
704 /**
705 * list_add_tail - add a new entry
706 * @new: new entry to be added
707 * @head: list head to add it before
708 *
709 * Insert a new entry before the specified head.
710 * This is useful for implementing queues.
711 */
712 static inline void list_add_tail(struct list_head *new, struct list_head *head)
713 {
714 __list_add(new, head->prev, head);
715 }
716
717 /*
718 * Delete a list entry by making the prev/next entries
719 * point to each other.
720 *
721 * This is only for internal list manipulation where we know
722 * the prev/next entries already!
723 */
724 static inline void __list_del(struct list_head * prev, struct list_head * next)
725 {
726 next->prev = prev;
727 prev->next = next;
728 }
729
730 /**
731 * list_del - deletes entry from list.
732 * @entry: the element to delete from the list.
733 * Note: list_empty() on entry does not return true after this, the entry is
734 * in an undefined state.
735 */
736 #ifndef CONFIG_DEBUG_LIST
737 static inline void list_del(struct list_head *entry)
738 {
739 __list_del(entry->prev, entry->next);
740 entry->next = LIST_POISON1;
741 entry->prev = LIST_POISON2;
742 }
743 #else
744 extern void list_del(struct list_head *entry);
745 #endif
746
747 /**
748 * list_replace - replace old entry by new one
749 * @old : the element to be replaced
750 * @new : the new element to insert
751 *
752 * If @old was empty, it will be overwritten.
753 */
754 static inline void list_replace(struct list_head *old,
755 struct list_head *new)
756 {
757 new->next = old->next;
758 new->next->prev = new;
759 new->prev = old->prev;
760 new->prev->next = new;
761 }
762
763 static inline void list_replace_init(struct list_head *old,
764 struct list_head *new)
765 {
766 list_replace(old, new);
767 INIT_LIST_HEAD(old);
768 }
769
770 /**
771 * list_del_init - deletes entry from list and reinitialize it.
772 * @entry: the element to delete from the list.
773 */
774 static inline void list_del_init(struct list_head *entry)
775 {
776 __list_del(entry->prev, entry->next);
777 INIT_LIST_HEAD(entry);
778 }
779
780 /**
781 * list_move - delete from one list and add as another's head
782 * @list: the entry to move
783 * @head: the head that will precede our entry
784 */
785 static inline void list_move(struct list_head *list, struct list_head *head)
786 {
787 __list_del(list->prev, list->next);
788 list_add(list, head);
789 }
790
791 /**
792 * list_move_tail - delete from one list and add as another's tail
793 * @list: the entry to move
794 * @head: the head that will follow our entry
795 */
796 static inline void list_move_tail(struct list_head *list,
797 struct list_head *head)
798 {
799 __list_del(list->prev, list->next);
800 list_add_tail(list, head);
801 }
802
803 /**
804 * list_is_last - tests whether @list is the last entry in list @head
805 * @list: the entry to test
806 * @head: the head of the list
807 */
808 static inline int list_is_last(const struct list_head *list,
809 const struct list_head *head)
810 {
811 return list->next == head;
812 }
813
814 /**
815 * list_empty - tests whether a list is empty
816 * @head: the list to test.
817 */
818 static inline int list_empty(const struct list_head *head)
819 {
820 return head->next == head;
821 }
822
823 /**
824 * list_empty_careful - tests whether a list is empty and not being modified
825 * @head: the list to test
826 *
827 * Description:
828 * tests whether a list is empty _and_ checks that no other CPU might be
829 * in the process of modifying either member (next or prev)
830 *
831 * NOTE: using list_empty_careful() without synchronization
832 * can only be safe if the only activity that can happen
833 * to the list entry is list_del_init(). Eg. it cannot be used
834 * if another CPU could re-list_add() it.
835 */
836 static inline int list_empty_careful(const struct list_head *head)
837 {
838 struct list_head *next = head->next;
839 return (next == head) && (next == head->prev);
840 }
841
842 /**
843 * list_is_singular - tests whether a list has just one entry.
844 * @head: the list to test.
845 */
846 static inline int list_is_singular(const struct list_head *head)
847 {
848 return !list_empty(head) && (head->next == head->prev);
849 }
850
851 static inline void __list_cut_position(struct list_head *list,
852 struct list_head *head, struct list_head *entry)
853 {
854 struct list_head *new_first = entry->next;
855 list->next = head->next;
856 list->next->prev = list;
857 list->prev = entry;
858 entry->next = list;
859 head->next = new_first;
860 new_first->prev = head;
861 }
862
863 /**
864 * list_cut_position - cut a list into two
865 * @list: a new list to add all removed entries
866 * @head: a list with entries
867 * @entry: an entry within head, could be the head itself
868 * and if so we won't cut the list
869 *
870 * This helper moves the initial part of @head, up to and
871 * including @entry, from @head to @list. You should
872 * pass on @entry an element you know is on @head. @list
873 * should be an empty list or a list you do not care about
874 * losing its data.
875 *
876 */
877 static inline void list_cut_position(struct list_head *list,
878 struct list_head *head, struct list_head *entry)
879 {
880 if (list_empty(head))
881 return;
882 if (list_is_singular(head) &&
883 (head->next != entry && head != entry))
884 return;
885 if (entry == head)
886 INIT_LIST_HEAD(list);
887 else
888 __list_cut_position(list, head, entry);
889 }
890
891 static inline void __list_splice(const struct list_head *list,
892 struct list_head *prev,
893 struct list_head *next)
894 {
895 struct list_head *first = list->next;
896 struct list_head *last = list->prev;
897
898 first->prev = prev;
899 prev->next = first;
900
901 last->next = next;
902 next->prev = last;
903 }
904
905 /**
906 * list_splice - join two lists, this is designed for stacks
907 * @list: the new list to add.
908 * @head: the place to add it in the first list.
909 */
910 static inline void list_splice(const struct list_head *list,
911 struct list_head *head)
912 {
913 if (!list_empty(list))
914 __list_splice(list, head, head->next);
915 }
916
917 /**
918 * list_splice_tail - join two lists, each list being a queue
919 * @list: the new list to add.
920 * @head: the place to add it in the first list.
921 */
922 static inline void list_splice_tail(struct list_head *list,
923 struct list_head *head)
924 {
925 if (!list_empty(list))
926 __list_splice(list, head->prev, head);
927 }
928
929 /**
930 * list_splice_init - join two lists and reinitialise the emptied list.
931 * @list: the new list to add.
932 * @head: the place to add it in the first list.
933 *
934 * The list at @list is reinitialised
935 */
936 static inline void list_splice_init(struct list_head *list,
937 struct list_head *head)
938 {
939 if (!list_empty(list)) {
940 __list_splice(list, head, head->next);
941 INIT_LIST_HEAD(list);
942 }
943 }
944
945 /**
946 * list_splice_tail_init - join two lists and reinitialise the emptied list
947 * @list: the new list to add.
948 * @head: the place to add it in the first list.
949 *
950 * Each of the lists is a queue.
951 * The list at @list is reinitialised
952 */
953 static inline void list_splice_tail_init(struct list_head *list,
954 struct list_head *head)
955 {
956 if (!list_empty(list)) {
957 __list_splice(list, head->prev, head);
958 INIT_LIST_HEAD(list);
959 }
960 }
961
962 /**
963 * list_entry - get the struct for this entry
964 * @ptr: the &struct list_head pointer.
965 * @type: the type of the struct this is embedded in.
966 * @member: the name of the list_struct within the struct.
967 */
968 #define list_entry(ptr, type, member) \
969 container_of(ptr, type, member)
970
971 /**
972 * list_first_entry - get the first element from a list
973 * @ptr: the list head to take the element from.
974 * @type: the type of the struct this is embedded in.
975 * @member: the name of the list_struct within the struct.
976 *
977 * Note, that list is expected to be not empty.
978 */
979 #define list_first_entry(ptr, type, member) \
980 list_entry((ptr)->next, type, member)
981
982 /**
983 * list_for_each - iterate over a list
984 * @pos: the &struct list_head to use as a loop cursor.
985 * @head: the head for your list.
986 */
987 #define list_for_each(pos, head) \
988 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
989 pos = pos->next)
990
991 /**
992 * __list_for_each - iterate over a list
993 * @pos: the &struct list_head to use as a loop cursor.
994 * @head: the head for your list.
995 *
996 * This variant differs from list_for_each() in that it's the
997 * simplest possible list iteration code, no prefetching is done.
998 * Use this for code that knows the list to be very short (empty
999 * or 1 entry) most of the time.
1000 */
1001 #define __list_for_each(pos, head) \
1002 for (pos = (head)->next; pos != (head); pos = pos->next)
1003
1004 /**
1005 * list_for_each_prev - iterate over a list backwards
1006 * @pos: the &struct list_head to use as a loop cursor.
1007 * @head: the head for your list.
1008 */
1009 #define list_for_each_prev(pos, head) \
1010 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1011 pos = pos->prev)
1012
1013 /**
1014 * list_for_each_safe - iterate over a list safe against removal of list entry
1015 * @pos: the &struct list_head to use as a loop cursor.
1016 * @n: another &struct list_head to use as temporary storage
1017 * @head: the head for your list.
1018 */
1019 #define list_for_each_safe(pos, n, head) \
1020 for (pos = (head)->next, n = pos->next; pos != (head); \
1021 pos = n, n = pos->next)
1022
1023 /**
1024 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1025 * @pos: the &struct list_head to use as a loop cursor.
1026 * @n: another &struct list_head to use as temporary storage
1027 * @head: the head for your list.
1028 */
1029 #define list_for_each_prev_safe(pos, n, head) \
1030 for (pos = (head)->prev, n = pos->prev; \
1031 prefetch(pos->prev), pos != (head); \
1032 pos = n, n = pos->prev)
1033
1034 /**
1035 * list_for_each_entry - iterate over list of given type
1036 * @pos: the type * to use as a loop cursor.
1037 * @head: the head for your list.
1038 * @member: the name of the list_struct within the struct.
1039 */
1040 #define list_for_each_entry(pos, head, member) \
1041 for (pos = list_entry((head)->next, typeof(*pos), member); \
1042 prefetch(pos->member.next), &pos->member != (head); \
1043 pos = list_entry(pos->member.next, typeof(*pos), member))
1044
1045 /**
1046 * list_for_each_entry_reverse - iterate backwards over list of given type.
1047 * @pos: the type * to use as a loop cursor.
1048 * @head: the head for your list.
1049 * @member: the name of the list_struct within the struct.
1050 */
1051 #define list_for_each_entry_reverse(pos, head, member) \
1052 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1053 prefetch(pos->member.prev), &pos->member != (head); \
1054 pos = list_entry(pos->member.prev, typeof(*pos), member))
1055
1056 /**
1057 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1058 * @pos: the type * to use as a start point
1059 * @head: the head of the list
1060 * @member: the name of the list_struct within the struct.
1061 *
1062 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1063 */
1064 #define list_prepare_entry(pos, head, member) \
1065 ((pos) ? : list_entry(head, typeof(*pos), member))
1066
1067 /**
1068 * list_for_each_entry_continue - continue iteration over list of given type
1069 * @pos: the type * to use as a loop cursor.
1070 * @head: the head for your list.
1071 * @member: the name of the list_struct within the struct.
1072 *
1073 * Continue to iterate over list of given type, continuing after
1074 * the current position.
1075 */
1076 #define list_for_each_entry_continue(pos, head, member) \
1077 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1078 prefetch(pos->member.next), &pos->member != (head); \
1079 pos = list_entry(pos->member.next, typeof(*pos), member))
1080
1081 /**
1082 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1083 * @pos: the type * to use as a loop cursor.
1084 * @head: the head for your list.
1085 * @member: the name of the list_struct within the struct.
1086 *
1087 * Start to iterate over list of given type backwards, continuing after
1088 * the current position.
1089 */
1090 #define list_for_each_entry_continue_reverse(pos, head, member) \
1091 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1092 prefetch(pos->member.prev), &pos->member != (head); \
1093 pos = list_entry(pos->member.prev, typeof(*pos), member))
1094
1095 /**
1096 * list_for_each_entry_from - iterate over list of given type from the current point
1097 * @pos: the type * to use as a loop cursor.
1098 * @head: the head for your list.
1099 * @member: the name of the list_struct within the struct.
1100 *
1101 * Iterate over list of given type, continuing from current position.
1102 */
1103 #define list_for_each_entry_from(pos, head, member) \
1104 for (; prefetch(pos->member.next), &pos->member != (head); \
1105 pos = list_entry(pos->member.next, typeof(*pos), member))
1106
1107 /**
1108 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1109 * @pos: the type * to use as a loop cursor.
1110 * @n: another type * to use as temporary storage
1111 * @head: the head for your list.
1112 * @member: the name of the list_struct within the struct.
1113 */
1114 #define list_for_each_entry_safe(pos, n, head, member) \
1115 for (pos = list_entry((head)->next, typeof(*pos), member), \
1116 n = list_entry(pos->member.next, typeof(*pos), member); \
1117 &pos->member != (head); \
1118 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1119
1120 /**
1121 * list_for_each_entry_safe_continue
1122 * @pos: the type * to use as a loop cursor.
1123 * @n: another type * to use as temporary storage
1124 * @head: the head for your list.
1125 * @member: the name of the list_struct within the struct.
1126 *
1127 * Iterate over list of given type, continuing after current point,
1128 * safe against removal of list entry.
1129 */
1130 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1131 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1132 n = list_entry(pos->member.next, typeof(*pos), member); \
1133 &pos->member != (head); \
1134 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1135
1136 /**
1137 * list_for_each_entry_safe_from
1138 * @pos: the type * to use as a loop cursor.
1139 * @n: another type * to use as temporary storage
1140 * @head: the head for your list.
1141 * @member: the name of the list_struct within the struct.
1142 *
1143 * Iterate over list of given type from current point, safe against
1144 * removal of list entry.
1145 */
1146 #define list_for_each_entry_safe_from(pos, n, head, member) \
1147 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1148 &pos->member != (head); \
1149 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1150
1151 /**
1152 * list_for_each_entry_safe_reverse
1153 * @pos: the type * to use as a loop cursor.
1154 * @n: another type * to use as temporary storage
1155 * @head: the head for your list.
1156 * @member: the name of the list_struct within the struct.
1157 *
1158 * Iterate backwards over list of given type, safe against removal
1159 * of list entry.
1160 */
1161 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1162 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1163 n = list_entry(pos->member.prev, typeof(*pos), member); \
1164 &pos->member != (head); \
1165 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1166
1167 /*
1168 * Double linked lists with a single pointer list head.
1169 * Mostly useful for hash tables where the two pointer list head is
1170 * too wasteful.
1171 * You lose the ability to access the tail in O(1).
1172 */
1173
1174 struct hlist_head {
1175 struct hlist_node *first;
1176 };
1177
1178 struct hlist_node {
1179 struct hlist_node *next, **pprev;
1180 };
1181
1182 #define HLIST_HEAD_INIT { .first = NULL }
1183 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1184 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1185 static inline void INIT_HLIST_NODE(struct hlist_node *h)
1186 {
1187 h->next = NULL;
1188 h->pprev = NULL;
1189 }
1190
1191 static inline int hlist_unhashed(const struct hlist_node *h)
1192 {
1193 return !h->pprev;
1194 }
1195
1196 static inline int hlist_empty(const struct hlist_head *h)
1197 {
1198 return !h->first;
1199 }
1200
1201 static inline void __hlist_del(struct hlist_node *n)
1202 {
1203 struct hlist_node *next = n->next;
1204 struct hlist_node **pprev = n->pprev;
1205 *pprev = next;
1206 if (next)
1207 next->pprev = pprev;
1208 }
1209
1210 static inline void hlist_del(struct hlist_node *n)
1211 {
1212 __hlist_del(n);
1213 n->next = LIST_POISON1;
1214 n->pprev = LIST_POISON2;
1215 }
1216
1217 static inline void hlist_del_init(struct hlist_node *n)
1218 {
1219 if (!hlist_unhashed(n)) {
1220 __hlist_del(n);
1221 INIT_HLIST_NODE(n);
1222 }
1223 }
1224
1225 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
1226 {
1227 struct hlist_node *first = h->first;
1228 n->next = first;
1229 if (first)
1230 first->pprev = &n->next;
1231 h->first = n;
1232 n->pprev = &h->first;
1233 }
1234
1235 /* next must be != NULL */
1236 static inline void hlist_add_before(struct hlist_node *n,
1237 struct hlist_node *next)
1238 {
1239 n->pprev = next->pprev;
1240 n->next = next;
1241 next->pprev = &n->next;
1242 *(n->pprev) = n;
1243 }
1244
1245 static inline void hlist_add_after(struct hlist_node *n,
1246 struct hlist_node *next)
1247 {
1248 next->next = n->next;
1249 n->next = next;
1250 next->pprev = &n->next;
1251
1252 if(next->next)
1253 next->next->pprev = &next->next;
1254 }
1255
1256 /*
1257 * Move a list from one list head to another. Fixup the pprev
1258 * reference of the first entry if it exists.
1259 */
1260 static inline void hlist_move_list(struct hlist_head *old,
1261 struct hlist_head *new)
1262 {
1263 new->first = old->first;
1264 if (new->first)
1265 new->first->pprev = &new->first;
1266 old->first = NULL;
1267 }
1268
1269 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1270
1271 #define hlist_for_each(pos, head) \
1272 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1273 pos = pos->next)
1274
1275 #define hlist_for_each_safe(pos, n, head) \
1276 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1277 pos = n)
1278
1279 /**
1280 * hlist_for_each_entry - iterate over list of given type
1281 * @tpos: the type * to use as a loop cursor.
1282 * @pos: the &struct hlist_node to use as a loop cursor.
1283 * @head: the head for your list.
1284 * @member: the name of the hlist_node within the struct.
1285 */
1286 #define hlist_for_each_entry(tpos, pos, head, member) \
1287 for (pos = (head)->first; \
1288 pos && ({ prefetch(pos->next); 1;}) && \
1289 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1290 pos = pos->next)
1291
1292 /**
1293 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1294 * @tpos: the type * to use as a loop cursor.
1295 * @pos: the &struct hlist_node to use as a loop cursor.
1296 * @member: the name of the hlist_node within the struct.
1297 */
1298 #define hlist_for_each_entry_continue(tpos, pos, member) \
1299 for (pos = (pos)->next; \
1300 pos && ({ prefetch(pos->next); 1;}) && \
1301 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1302 pos = pos->next)
1303
1304 /**
1305 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1306 * @tpos: the type * to use as a loop cursor.
1307 * @pos: the &struct hlist_node to use as a loop cursor.
1308 * @member: the name of the hlist_node within the struct.
1309 */
1310 #define hlist_for_each_entry_from(tpos, pos, member) \
1311 for (; pos && ({ prefetch(pos->next); 1;}) && \
1312 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1313 pos = pos->next)
1314
1315 /**
1316 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1317 * @tpos: the type * to use as a loop cursor.
1318 * @pos: the &struct hlist_node to use as a loop cursor.
1319 * @n: another &struct hlist_node to use as temporary storage
1320 * @head: the head for your list.
1321 * @member: the name of the hlist_node within the struct.
1322 */
1323 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1324 for (pos = (head)->first; \
1325 pos && ({ n = pos->next; 1; }) && \
1326 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1327 pos = n)
1328
1329 #endif
This page took 0.05838 seconds and 4 git commands to generate.