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