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