Update api gcc in tests
[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 #if 0
644 /*
645 * Simple doubly linked list implementation.
646 *
647 * Some of the internal functions ("__xxx") are useful when
648 * manipulating whole lists rather than single entries, as
649 * sometimes we already know the next/prev entries and we can
650 * generate better code by using them directly rather than
651 * using the generic single-entry routines.
652 */
653
654 struct list_head {
655 struct list_head *next, *prev;
656 };
657
658 #define LIST_HEAD_INIT(name) { &(name), &(name) }
659
660 #define LIST_HEAD(name) \
661 struct list_head name = LIST_HEAD_INIT(name)
662
663 static inline void INIT_LIST_HEAD(struct list_head *list)
664 {
665 list->next = list;
666 list->prev = list;
667 }
668
669 /*
670 * Insert a new entry between two known consecutive entries.
671 *
672 * This is only for internal list manipulation where we know
673 * the prev/next entries already!
674 */
675 #ifndef CONFIG_DEBUG_LIST
676 static inline void __list_add(struct list_head *new,
677 struct list_head *prev,
678 struct list_head *next)
679 {
680 next->prev = new;
681 new->next = next;
682 new->prev = prev;
683 prev->next = new;
684 }
685 #else
686 extern void __list_add(struct list_head *new,
687 struct list_head *prev,
688 struct list_head *next);
689 #endif
690
691 /**
692 * list_add - add a new entry
693 * @new: new entry to be added
694 * @head: list head to add it after
695 *
696 * Insert a new entry after the specified head.
697 * This is good for implementing stacks.
698 */
699 static inline void list_add(struct list_head *new, struct list_head *head)
700 {
701 __list_add(new, head, head->next);
702 }
703
704
705 /**
706 * list_add_tail - add a new entry
707 * @new: new entry to be added
708 * @head: list head to add it before
709 *
710 * Insert a new entry before the specified head.
711 * This is useful for implementing queues.
712 */
713 static inline void list_add_tail(struct list_head *new, struct list_head *head)
714 {
715 __list_add(new, head->prev, head);
716 }
717
718 /*
719 * Delete a list entry by making the prev/next entries
720 * point to each other.
721 *
722 * This is only for internal list manipulation where we know
723 * the prev/next entries already!
724 */
725 static inline void __list_del(struct list_head * prev, struct list_head * next)
726 {
727 next->prev = prev;
728 prev->next = next;
729 }
730
731 /**
732 * list_del - deletes entry from list.
733 * @entry: the element to delete from the list.
734 * Note: list_empty() on entry does not return true after this, the entry is
735 * in an undefined state.
736 */
737 #ifndef CONFIG_DEBUG_LIST
738 static inline void list_del(struct list_head *entry)
739 {
740 __list_del(entry->prev, entry->next);
741 entry->next = LIST_POISON1;
742 entry->prev = LIST_POISON2;
743 }
744 #else
745 extern void list_del(struct list_head *entry);
746 #endif
747
748 /**
749 * list_replace - replace old entry by new one
750 * @old : the element to be replaced
751 * @new : the new element to insert
752 *
753 * If @old was empty, it will be overwritten.
754 */
755 static inline void list_replace(struct list_head *old,
756 struct list_head *new)
757 {
758 new->next = old->next;
759 new->next->prev = new;
760 new->prev = old->prev;
761 new->prev->next = new;
762 }
763
764 static inline void list_replace_init(struct list_head *old,
765 struct list_head *new)
766 {
767 list_replace(old, new);
768 INIT_LIST_HEAD(old);
769 }
770
771 /**
772 * list_del_init - deletes entry from list and reinitialize it.
773 * @entry: the element to delete from the list.
774 */
775 static inline void list_del_init(struct list_head *entry)
776 {
777 __list_del(entry->prev, entry->next);
778 INIT_LIST_HEAD(entry);
779 }
780
781 /**
782 * list_move - delete from one list and add as another's head
783 * @list: the entry to move
784 * @head: the head that will precede our entry
785 */
786 static inline void list_move(struct list_head *list, struct list_head *head)
787 {
788 __list_del(list->prev, list->next);
789 list_add(list, head);
790 }
791
792 /**
793 * list_move_tail - delete from one list and add as another's tail
794 * @list: the entry to move
795 * @head: the head that will follow our entry
796 */
797 static inline void list_move_tail(struct list_head *list,
798 struct list_head *head)
799 {
800 __list_del(list->prev, list->next);
801 list_add_tail(list, head);
802 }
803
804 /**
805 * list_is_last - tests whether @list is the last entry in list @head
806 * @list: the entry to test
807 * @head: the head of the list
808 */
809 static inline int list_is_last(const struct list_head *list,
810 const struct list_head *head)
811 {
812 return list->next == head;
813 }
814
815 /**
816 * list_empty - tests whether a list is empty
817 * @head: the list to test.
818 */
819 static inline int list_empty(const struct list_head *head)
820 {
821 return head->next == head;
822 }
823
824 /**
825 * list_empty_careful - tests whether a list is empty and not being modified
826 * @head: the list to test
827 *
828 * Description:
829 * tests whether a list is empty _and_ checks that no other CPU might be
830 * in the process of modifying either member (next or prev)
831 *
832 * NOTE: using list_empty_careful() without synchronization
833 * can only be safe if the only activity that can happen
834 * to the list entry is list_del_init(). Eg. it cannot be used
835 * if another CPU could re-list_add() it.
836 */
837 static inline int list_empty_careful(const struct list_head *head)
838 {
839 struct list_head *next = head->next;
840 return (next == head) && (next == head->prev);
841 }
842
843 /**
844 * list_is_singular - tests whether a list has just one entry.
845 * @head: the list to test.
846 */
847 static inline int list_is_singular(const struct list_head *head)
848 {
849 return !list_empty(head) && (head->next == head->prev);
850 }
851
852 static inline void __list_cut_position(struct list_head *list,
853 struct list_head *head, struct list_head *entry)
854 {
855 struct list_head *new_first = entry->next;
856 list->next = head->next;
857 list->next->prev = list;
858 list->prev = entry;
859 entry->next = list;
860 head->next = new_first;
861 new_first->prev = head;
862 }
863
864 /**
865 * list_cut_position - cut a list into two
866 * @list: a new list to add all removed entries
867 * @head: a list with entries
868 * @entry: an entry within head, could be the head itself
869 * and if so we won't cut the list
870 *
871 * This helper moves the initial part of @head, up to and
872 * including @entry, from @head to @list. You should
873 * pass on @entry an element you know is on @head. @list
874 * should be an empty list or a list you do not care about
875 * losing its data.
876 *
877 */
878 static inline void list_cut_position(struct list_head *list,
879 struct list_head *head, struct list_head *entry)
880 {
881 if (list_empty(head))
882 return;
883 if (list_is_singular(head) &&
884 (head->next != entry && head != entry))
885 return;
886 if (entry == head)
887 INIT_LIST_HEAD(list);
888 else
889 __list_cut_position(list, head, entry);
890 }
891
892 static inline void __list_splice(const struct list_head *list,
893 struct list_head *prev,
894 struct list_head *next)
895 {
896 struct list_head *first = list->next;
897 struct list_head *last = list->prev;
898
899 first->prev = prev;
900 prev->next = first;
901
902 last->next = next;
903 next->prev = last;
904 }
905
906 /**
907 * list_splice - join two lists, this is designed for stacks
908 * @list: the new list to add.
909 * @head: the place to add it in the first list.
910 */
911 static inline void list_splice(const struct list_head *list,
912 struct list_head *head)
913 {
914 if (!list_empty(list))
915 __list_splice(list, head, head->next);
916 }
917
918 /**
919 * list_splice_tail - join two lists, each list being a queue
920 * @list: the new list to add.
921 * @head: the place to add it in the first list.
922 */
923 static inline void list_splice_tail(struct list_head *list,
924 struct list_head *head)
925 {
926 if (!list_empty(list))
927 __list_splice(list, head->prev, head);
928 }
929
930 /**
931 * list_splice_init - join two lists and reinitialise the emptied list.
932 * @list: the new list to add.
933 * @head: the place to add it in the first list.
934 *
935 * The list at @list is reinitialised
936 */
937 static inline void list_splice_init(struct list_head *list,
938 struct list_head *head)
939 {
940 if (!list_empty(list)) {
941 __list_splice(list, head, head->next);
942 INIT_LIST_HEAD(list);
943 }
944 }
945
946 /**
947 * list_splice_tail_init - join two lists and reinitialise the emptied list
948 * @list: the new list to add.
949 * @head: the place to add it in the first list.
950 *
951 * Each of the lists is a queue.
952 * The list at @list is reinitialised
953 */
954 static inline void list_splice_tail_init(struct list_head *list,
955 struct list_head *head)
956 {
957 if (!list_empty(list)) {
958 __list_splice(list, head->prev, head);
959 INIT_LIST_HEAD(list);
960 }
961 }
962
963 /**
964 * list_entry - get the struct for this entry
965 * @ptr: the &struct list_head pointer.
966 * @type: the type of the struct this is embedded in.
967 * @member: the name of the list_struct within the struct.
968 */
969 #define list_entry(ptr, type, member) \
970 container_of(ptr, type, member)
971
972 /**
973 * list_first_entry - get the first element from a list
974 * @ptr: the list head to take the element from.
975 * @type: the type of the struct this is embedded in.
976 * @member: the name of the list_struct within the struct.
977 *
978 * Note, that list is expected to be not empty.
979 */
980 #define list_first_entry(ptr, type, member) \
981 list_entry((ptr)->next, type, member)
982
983 /**
984 * list_for_each - iterate over a list
985 * @pos: the &struct list_head to use as a loop cursor.
986 * @head: the head for your list.
987 */
988 #define list_for_each(pos, head) \
989 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
990 pos = pos->next)
991
992 /**
993 * __list_for_each - iterate over a list
994 * @pos: the &struct list_head to use as a loop cursor.
995 * @head: the head for your list.
996 *
997 * This variant differs from list_for_each() in that it's the
998 * simplest possible list iteration code, no prefetching is done.
999 * Use this for code that knows the list to be very short (empty
1000 * or 1 entry) most of the time.
1001 */
1002 #define __list_for_each(pos, head) \
1003 for (pos = (head)->next; pos != (head); pos = pos->next)
1004
1005 /**
1006 * list_for_each_prev - iterate over a list backwards
1007 * @pos: the &struct list_head to use as a loop cursor.
1008 * @head: the head for your list.
1009 */
1010 #define list_for_each_prev(pos, head) \
1011 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1012 pos = pos->prev)
1013
1014 /**
1015 * list_for_each_safe - iterate over a list safe against removal of list entry
1016 * @pos: the &struct list_head to use as a loop cursor.
1017 * @n: another &struct list_head to use as temporary storage
1018 * @head: the head for your list.
1019 */
1020 #define list_for_each_safe(pos, n, head) \
1021 for (pos = (head)->next, n = pos->next; pos != (head); \
1022 pos = n, n = pos->next)
1023
1024 /**
1025 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1026 * @pos: the &struct list_head to use as a loop cursor.
1027 * @n: another &struct list_head to use as temporary storage
1028 * @head: the head for your list.
1029 */
1030 #define list_for_each_prev_safe(pos, n, head) \
1031 for (pos = (head)->prev, n = pos->prev; \
1032 prefetch(pos->prev), pos != (head); \
1033 pos = n, n = pos->prev)
1034
1035 /**
1036 * list_for_each_entry - iterate over list of given type
1037 * @pos: the type * to use as a loop cursor.
1038 * @head: the head for your list.
1039 * @member: the name of the list_struct within the struct.
1040 */
1041 #define list_for_each_entry(pos, head, member) \
1042 for (pos = list_entry((head)->next, typeof(*pos), member); \
1043 prefetch(pos->member.next), &pos->member != (head); \
1044 pos = list_entry(pos->member.next, typeof(*pos), member))
1045
1046 /**
1047 * list_for_each_entry_reverse - iterate backwards over list of given type.
1048 * @pos: the type * to use as a loop cursor.
1049 * @head: the head for your list.
1050 * @member: the name of the list_struct within the struct.
1051 */
1052 #define list_for_each_entry_reverse(pos, head, member) \
1053 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1054 prefetch(pos->member.prev), &pos->member != (head); \
1055 pos = list_entry(pos->member.prev, typeof(*pos), member))
1056
1057 /**
1058 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1059 * @pos: the type * to use as a start point
1060 * @head: the head of the list
1061 * @member: the name of the list_struct within the struct.
1062 *
1063 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1064 */
1065 #define list_prepare_entry(pos, head, member) \
1066 ((pos) ? : list_entry(head, typeof(*pos), member))
1067
1068 /**
1069 * list_for_each_entry_continue - continue iteration over list of given type
1070 * @pos: the type * to use as a loop cursor.
1071 * @head: the head for your list.
1072 * @member: the name of the list_struct within the struct.
1073 *
1074 * Continue to iterate over list of given type, continuing after
1075 * the current position.
1076 */
1077 #define list_for_each_entry_continue(pos, head, member) \
1078 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1079 prefetch(pos->member.next), &pos->member != (head); \
1080 pos = list_entry(pos->member.next, typeof(*pos), member))
1081
1082 /**
1083 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1084 * @pos: the type * to use as a loop cursor.
1085 * @head: the head for your list.
1086 * @member: the name of the list_struct within the struct.
1087 *
1088 * Start to iterate over list of given type backwards, continuing after
1089 * the current position.
1090 */
1091 #define list_for_each_entry_continue_reverse(pos, head, member) \
1092 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1093 prefetch(pos->member.prev), &pos->member != (head); \
1094 pos = list_entry(pos->member.prev, typeof(*pos), member))
1095
1096 /**
1097 * list_for_each_entry_from - iterate over list of given type from the current point
1098 * @pos: the type * to use as a loop cursor.
1099 * @head: the head for your list.
1100 * @member: the name of the list_struct within the struct.
1101 *
1102 * Iterate over list of given type, continuing from current position.
1103 */
1104 #define list_for_each_entry_from(pos, head, member) \
1105 for (; prefetch(pos->member.next), &pos->member != (head); \
1106 pos = list_entry(pos->member.next, typeof(*pos), member))
1107
1108 /**
1109 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1110 * @pos: the type * to use as a loop cursor.
1111 * @n: another type * to use as temporary storage
1112 * @head: the head for your list.
1113 * @member: the name of the list_struct within the struct.
1114 */
1115 #define list_for_each_entry_safe(pos, n, head, member) \
1116 for (pos = list_entry((head)->next, typeof(*pos), member), \
1117 n = list_entry(pos->member.next, typeof(*pos), member); \
1118 &pos->member != (head); \
1119 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1120
1121 /**
1122 * list_for_each_entry_safe_continue
1123 * @pos: the type * to use as a loop cursor.
1124 * @n: another type * to use as temporary storage
1125 * @head: the head for your list.
1126 * @member: the name of the list_struct within the struct.
1127 *
1128 * Iterate over list of given type, continuing after current point,
1129 * safe against removal of list entry.
1130 */
1131 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1132 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1133 n = list_entry(pos->member.next, typeof(*pos), member); \
1134 &pos->member != (head); \
1135 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1136
1137 /**
1138 * list_for_each_entry_safe_from
1139 * @pos: the type * to use as a loop cursor.
1140 * @n: another type * to use as temporary storage
1141 * @head: the head for your list.
1142 * @member: the name of the list_struct within the struct.
1143 *
1144 * Iterate over list of given type from current point, safe against
1145 * removal of list entry.
1146 */
1147 #define list_for_each_entry_safe_from(pos, n, head, member) \
1148 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1149 &pos->member != (head); \
1150 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1151
1152 /**
1153 * list_for_each_entry_safe_reverse
1154 * @pos: the type * to use as a loop cursor.
1155 * @n: another type * to use as temporary storage
1156 * @head: the head for your list.
1157 * @member: the name of the list_struct within the struct.
1158 *
1159 * Iterate backwards over list of given type, safe against removal
1160 * of list entry.
1161 */
1162 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1163 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1164 n = list_entry(pos->member.prev, typeof(*pos), member); \
1165 &pos->member != (head); \
1166 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1167
1168 #endif //0
1169
1170 /*
1171 * Double linked lists with a single pointer list head.
1172 * Mostly useful for hash tables where the two pointer list head is
1173 * too wasteful.
1174 * You lose the ability to access the tail in O(1).
1175 */
1176
1177 struct hlist_head {
1178 struct hlist_node *first;
1179 };
1180
1181 struct hlist_node {
1182 struct hlist_node *next, **pprev;
1183 };
1184
1185 #define HLIST_HEAD_INIT { .first = NULL }
1186 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1187 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1188 static inline void INIT_HLIST_NODE(struct hlist_node *h)
1189 {
1190 h->next = NULL;
1191 h->pprev = NULL;
1192 }
1193
1194 static inline int hlist_unhashed(const struct hlist_node *h)
1195 {
1196 return !h->pprev;
1197 }
1198
1199 static inline int hlist_empty(const struct hlist_head *h)
1200 {
1201 return !h->first;
1202 }
1203
1204 static inline void __hlist_del(struct hlist_node *n)
1205 {
1206 struct hlist_node *next = n->next;
1207 struct hlist_node **pprev = n->pprev;
1208 *pprev = next;
1209 if (next)
1210 next->pprev = pprev;
1211 }
1212
1213 static inline void hlist_del(struct hlist_node *n)
1214 {
1215 __hlist_del(n);
1216 n->next = LIST_POISON1;
1217 n->pprev = LIST_POISON2;
1218 }
1219
1220 static inline void hlist_del_init(struct hlist_node *n)
1221 {
1222 if (!hlist_unhashed(n)) {
1223 __hlist_del(n);
1224 INIT_HLIST_NODE(n);
1225 }
1226 }
1227
1228 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
1229 {
1230 struct hlist_node *first = h->first;
1231 n->next = first;
1232 if (first)
1233 first->pprev = &n->next;
1234 h->first = n;
1235 n->pprev = &h->first;
1236 }
1237
1238 /* next must be != NULL */
1239 static inline void hlist_add_before(struct hlist_node *n,
1240 struct hlist_node *next)
1241 {
1242 n->pprev = next->pprev;
1243 n->next = next;
1244 next->pprev = &n->next;
1245 *(n->pprev) = n;
1246 }
1247
1248 static inline void hlist_add_after(struct hlist_node *n,
1249 struct hlist_node *next)
1250 {
1251 next->next = n->next;
1252 n->next = next;
1253 next->pprev = &n->next;
1254
1255 if(next->next)
1256 next->next->pprev = &next->next;
1257 }
1258
1259 /*
1260 * Move a list from one list head to another. Fixup the pprev
1261 * reference of the first entry if it exists.
1262 */
1263 static inline void hlist_move_list(struct hlist_head *old,
1264 struct hlist_head *new)
1265 {
1266 new->first = old->first;
1267 if (new->first)
1268 new->first->pprev = &new->first;
1269 old->first = NULL;
1270 }
1271
1272 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1273
1274 #define hlist_for_each(pos, head) \
1275 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1276 pos = pos->next)
1277
1278 #define hlist_for_each_safe(pos, n, head) \
1279 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1280 pos = n)
1281
1282 /**
1283 * hlist_for_each_entry - iterate over list of given type
1284 * @tpos: the type * to use as a loop cursor.
1285 * @pos: the &struct hlist_node to use as a loop cursor.
1286 * @head: the head for your list.
1287 * @member: the name of the hlist_node within the struct.
1288 */
1289 #define hlist_for_each_entry(tpos, pos, head, member) \
1290 for (pos = (head)->first; \
1291 pos && ({ prefetch(pos->next); 1;}) && \
1292 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1293 pos = pos->next)
1294
1295 /**
1296 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1297 * @tpos: the type * to use as a loop cursor.
1298 * @pos: the &struct hlist_node to use as a loop cursor.
1299 * @member: the name of the hlist_node within the struct.
1300 */
1301 #define hlist_for_each_entry_continue(tpos, pos, member) \
1302 for (pos = (pos)->next; \
1303 pos && ({ prefetch(pos->next); 1;}) && \
1304 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1305 pos = pos->next)
1306
1307 /**
1308 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1309 * @tpos: the type * to use as a loop cursor.
1310 * @pos: the &struct hlist_node to use as a loop cursor.
1311 * @member: the name of the hlist_node within the struct.
1312 */
1313 #define hlist_for_each_entry_from(tpos, pos, member) \
1314 for (; pos && ({ prefetch(pos->next); 1;}) && \
1315 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1316 pos = pos->next)
1317
1318 /**
1319 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1320 * @tpos: the type * to use as a loop cursor.
1321 * @pos: the &struct hlist_node to use as a loop cursor.
1322 * @n: another &struct hlist_node to use as temporary storage
1323 * @head: the head for your list.
1324 * @member: the name of the hlist_node within the struct.
1325 */
1326 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1327 for (pos = (head)->first; \
1328 pos && ({ n = pos->next; 1; }) && \
1329 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1330 pos = n)
1331
1332 #endif
This page took 0.075798 seconds and 4 git commands to generate.