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