Add offsetof to compiler.h
[urcu.git] / tests / api_gcc.h
1 #define _INCLUDE_API_H
2
3 /*
4 * common.h: Common Linux kernel-isms.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; but version 2 of the License only due
9 * to code included from the Linux kernel.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Copyright (c) 2006 Paul E. McKenney, IBM.
21 *
22 * Much code taken from the Linux kernel. For such code, the option
23 * to redistribute under later versions of GPL might not be available.
24 */
25
26 #ifndef __always_inline
27 #define __always_inline inline
28 #endif
29
30 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
31 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
32
33 #ifdef __ASSEMBLY__
34 # define stringify_in_c(...) __VA_ARGS__
35 # define ASM_CONST(x) x
36 #else
37 /* This version of stringify will deal with commas... */
38 # define __stringify_in_c(...) #__VA_ARGS__
39 # define stringify_in_c(...) __stringify_in_c(__VA_ARGS__) " "
40 # define __ASM_CONST(x) x##UL
41 # define ASM_CONST(x) __ASM_CONST(x)
42 #endif
43
44
45 /*
46 * arch-i386.h: Expose x86 atomic instructions. 80486 and better only.
47 *
48 * This program is free software; you can redistribute it and/or modify
49 * it under the terms of the GNU General Public License as published by
50 * the Free Software Foundation, but version 2 only due to inclusion
51 * of Linux-kernel code.
52 *
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * GNU General Public License for more details.
57 *
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
61 *
62 * Copyright (c) 2006 Paul E. McKenney, IBM.
63 *
64 * Much code taken from the Linux kernel. For such code, the option
65 * to redistribute under later versions of GPL might not be available.
66 */
67
68 /*
69 * Machine parameters.
70 */
71
72 #define CACHE_LINE_SIZE 64
73 #define ____cacheline_internodealigned_in_smp \
74 __attribute__((__aligned__(1 << 6)))
75
76 #define LOCK_PREFIX "lock ; "
77
78 /*
79 * Atomic data structure, initialization, and access.
80 */
81
82 typedef 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
101 static __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 */
113 static __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 */
127 static __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 */
138 static __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 */
149 static __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 */
162 static __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 */
175 static __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 */
189 static __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 */
201 static __inline__ int atomic_add_return(int i, atomic_t *v)
202 {
203 return __sync_add_and_fetch(&v->counter, i);
204 }
205
206 static __inline__ int atomic_sub_return(int i, atomic_t *v)
207 {
208 return atomic_add_return(-i,v);
209 }
210
211 static inline unsigned int
212 cmpxchg(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 <arch.h>
286 /* #include "atomic.h" */
287
288 /*
289 * Compiler magic.
290 */
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
307 typedef 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
312 static 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
320 static 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
328 static 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
343 typedef 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
349 thread_id_t __thread_id_map[NR_THREADS];
350 spinlock_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
360 pthread_key_t thread_id_key;
361
362 static 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
390 static 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
400 static 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
425 static 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
447 static 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
460 static 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
473 long 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
527 DEFINE_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
556 struct 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
614 static 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 container_of(ptr, type, member) ({ \
638 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
639 (type *)( (char *)__mptr - offsetof(type,member) );})
640
641 /*
642 * Simple doubly linked list implementation.
643 *
644 * Some of the internal functions ("__xxx") are useful when
645 * manipulating whole lists rather than single entries, as
646 * sometimes we already know the next/prev entries and we can
647 * generate better code by using them directly rather than
648 * using the generic single-entry routines.
649 */
650
651 struct list_head {
652 struct list_head *next, *prev;
653 };
654
655 #define LIST_HEAD_INIT(name) { &(name), &(name) }
656
657 #define LIST_HEAD(name) \
658 struct list_head name = LIST_HEAD_INIT(name)
659
660 static inline void INIT_LIST_HEAD(struct list_head *list)
661 {
662 list->next = list;
663 list->prev = list;
664 }
665
666 /*
667 * Insert a new entry between two known consecutive entries.
668 *
669 * This is only for internal list manipulation where we know
670 * the prev/next entries already!
671 */
672 #ifndef CONFIG_DEBUG_LIST
673 static inline void __list_add(struct list_head *new,
674 struct list_head *prev,
675 struct list_head *next)
676 {
677 next->prev = new;
678 new->next = next;
679 new->prev = prev;
680 prev->next = new;
681 }
682 #else
683 extern void __list_add(struct list_head *new,
684 struct list_head *prev,
685 struct list_head *next);
686 #endif
687
688 /**
689 * list_add - add a new entry
690 * @new: new entry to be added
691 * @head: list head to add it after
692 *
693 * Insert a new entry after the specified head.
694 * This is good for implementing stacks.
695 */
696 static inline void list_add(struct list_head *new, struct list_head *head)
697 {
698 __list_add(new, head, head->next);
699 }
700
701
702 /**
703 * list_add_tail - add a new entry
704 * @new: new entry to be added
705 * @head: list head to add it before
706 *
707 * Insert a new entry before the specified head.
708 * This is useful for implementing queues.
709 */
710 static inline void list_add_tail(struct list_head *new, struct list_head *head)
711 {
712 __list_add(new, head->prev, head);
713 }
714
715 /*
716 * Delete a list entry by making the prev/next entries
717 * point to each other.
718 *
719 * This is only for internal list manipulation where we know
720 * the prev/next entries already!
721 */
722 static inline void __list_del(struct list_head * prev, struct list_head * next)
723 {
724 next->prev = prev;
725 prev->next = next;
726 }
727
728 /**
729 * list_del - deletes entry from list.
730 * @entry: the element to delete from the list.
731 * Note: list_empty() on entry does not return true after this, the entry is
732 * in an undefined state.
733 */
734 #ifndef CONFIG_DEBUG_LIST
735 static inline void list_del(struct list_head *entry)
736 {
737 __list_del(entry->prev, entry->next);
738 entry->next = LIST_POISON1;
739 entry->prev = LIST_POISON2;
740 }
741 #else
742 extern void list_del(struct list_head *entry);
743 #endif
744
745 /**
746 * list_replace - replace old entry by new one
747 * @old : the element to be replaced
748 * @new : the new element to insert
749 *
750 * If @old was empty, it will be overwritten.
751 */
752 static inline void list_replace(struct list_head *old,
753 struct list_head *new)
754 {
755 new->next = old->next;
756 new->next->prev = new;
757 new->prev = old->prev;
758 new->prev->next = new;
759 }
760
761 static inline void list_replace_init(struct list_head *old,
762 struct list_head *new)
763 {
764 list_replace(old, new);
765 INIT_LIST_HEAD(old);
766 }
767
768 /**
769 * list_del_init - deletes entry from list and reinitialize it.
770 * @entry: the element to delete from the list.
771 */
772 static inline void list_del_init(struct list_head *entry)
773 {
774 __list_del(entry->prev, entry->next);
775 INIT_LIST_HEAD(entry);
776 }
777
778 /**
779 * list_move - delete from one list and add as another's head
780 * @list: the entry to move
781 * @head: the head that will precede our entry
782 */
783 static inline void list_move(struct list_head *list, struct list_head *head)
784 {
785 __list_del(list->prev, list->next);
786 list_add(list, head);
787 }
788
789 /**
790 * list_move_tail - delete from one list and add as another's tail
791 * @list: the entry to move
792 * @head: the head that will follow our entry
793 */
794 static inline void list_move_tail(struct list_head *list,
795 struct list_head *head)
796 {
797 __list_del(list->prev, list->next);
798 list_add_tail(list, head);
799 }
800
801 /**
802 * list_is_last - tests whether @list is the last entry in list @head
803 * @list: the entry to test
804 * @head: the head of the list
805 */
806 static inline int list_is_last(const struct list_head *list,
807 const struct list_head *head)
808 {
809 return list->next == head;
810 }
811
812 /**
813 * list_empty - tests whether a list is empty
814 * @head: the list to test.
815 */
816 static inline int list_empty(const struct list_head *head)
817 {
818 return head->next == head;
819 }
820
821 /**
822 * list_empty_careful - tests whether a list is empty and not being modified
823 * @head: the list to test
824 *
825 * Description:
826 * tests whether a list is empty _and_ checks that no other CPU might be
827 * in the process of modifying either member (next or prev)
828 *
829 * NOTE: using list_empty_careful() without synchronization
830 * can only be safe if the only activity that can happen
831 * to the list entry is list_del_init(). Eg. it cannot be used
832 * if another CPU could re-list_add() it.
833 */
834 static inline int list_empty_careful(const struct list_head *head)
835 {
836 struct list_head *next = head->next;
837 return (next == head) && (next == head->prev);
838 }
839
840 /**
841 * list_is_singular - tests whether a list has just one entry.
842 * @head: the list to test.
843 */
844 static inline int list_is_singular(const struct list_head *head)
845 {
846 return !list_empty(head) && (head->next == head->prev);
847 }
848
849 static inline void __list_cut_position(struct list_head *list,
850 struct list_head *head, struct list_head *entry)
851 {
852 struct list_head *new_first = entry->next;
853 list->next = head->next;
854 list->next->prev = list;
855 list->prev = entry;
856 entry->next = list;
857 head->next = new_first;
858 new_first->prev = head;
859 }
860
861 /**
862 * list_cut_position - cut a list into two
863 * @list: a new list to add all removed entries
864 * @head: a list with entries
865 * @entry: an entry within head, could be the head itself
866 * and if so we won't cut the list
867 *
868 * This helper moves the initial part of @head, up to and
869 * including @entry, from @head to @list. You should
870 * pass on @entry an element you know is on @head. @list
871 * should be an empty list or a list you do not care about
872 * losing its data.
873 *
874 */
875 static inline void list_cut_position(struct list_head *list,
876 struct list_head *head, struct list_head *entry)
877 {
878 if (list_empty(head))
879 return;
880 if (list_is_singular(head) &&
881 (head->next != entry && head != entry))
882 return;
883 if (entry == head)
884 INIT_LIST_HEAD(list);
885 else
886 __list_cut_position(list, head, entry);
887 }
888
889 static inline void __list_splice(const struct list_head *list,
890 struct list_head *prev,
891 struct list_head *next)
892 {
893 struct list_head *first = list->next;
894 struct list_head *last = list->prev;
895
896 first->prev = prev;
897 prev->next = first;
898
899 last->next = next;
900 next->prev = last;
901 }
902
903 /**
904 * list_splice - join two lists, this is designed for stacks
905 * @list: the new list to add.
906 * @head: the place to add it in the first list.
907 */
908 static inline void list_splice(const struct list_head *list,
909 struct list_head *head)
910 {
911 if (!list_empty(list))
912 __list_splice(list, head, head->next);
913 }
914
915 /**
916 * list_splice_tail - join two lists, each list being a queue
917 * @list: the new list to add.
918 * @head: the place to add it in the first list.
919 */
920 static inline void list_splice_tail(struct list_head *list,
921 struct list_head *head)
922 {
923 if (!list_empty(list))
924 __list_splice(list, head->prev, head);
925 }
926
927 /**
928 * list_splice_init - join two lists and reinitialise the emptied list.
929 * @list: the new list to add.
930 * @head: the place to add it in the first list.
931 *
932 * The list at @list is reinitialised
933 */
934 static inline void list_splice_init(struct list_head *list,
935 struct list_head *head)
936 {
937 if (!list_empty(list)) {
938 __list_splice(list, head, head->next);
939 INIT_LIST_HEAD(list);
940 }
941 }
942
943 /**
944 * list_splice_tail_init - join two lists and reinitialise the emptied list
945 * @list: the new list to add.
946 * @head: the place to add it in the first list.
947 *
948 * Each of the lists is a queue.
949 * The list at @list is reinitialised
950 */
951 static inline void list_splice_tail_init(struct list_head *list,
952 struct list_head *head)
953 {
954 if (!list_empty(list)) {
955 __list_splice(list, head->prev, head);
956 INIT_LIST_HEAD(list);
957 }
958 }
959
960 /**
961 * list_entry - get the struct for this entry
962 * @ptr: the &struct list_head pointer.
963 * @type: the type of the struct this is embedded in.
964 * @member: the name of the list_struct within the struct.
965 */
966 #define list_entry(ptr, type, member) \
967 container_of(ptr, type, member)
968
969 /**
970 * list_first_entry - get the first element from a list
971 * @ptr: the list head to take the element from.
972 * @type: the type of the struct this is embedded in.
973 * @member: the name of the list_struct within the struct.
974 *
975 * Note, that list is expected to be not empty.
976 */
977 #define list_first_entry(ptr, type, member) \
978 list_entry((ptr)->next, type, member)
979
980 /**
981 * list_for_each - iterate over a list
982 * @pos: the &struct list_head to use as a loop cursor.
983 * @head: the head for your list.
984 */
985 #define list_for_each(pos, head) \
986 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
987 pos = pos->next)
988
989 /**
990 * __list_for_each - iterate over a list
991 * @pos: the &struct list_head to use as a loop cursor.
992 * @head: the head for your list.
993 *
994 * This variant differs from list_for_each() in that it's the
995 * simplest possible list iteration code, no prefetching is done.
996 * Use this for code that knows the list to be very short (empty
997 * or 1 entry) most of the time.
998 */
999 #define __list_for_each(pos, head) \
1000 for (pos = (head)->next; pos != (head); pos = pos->next)
1001
1002 /**
1003 * list_for_each_prev - iterate over a list backwards
1004 * @pos: the &struct list_head to use as a loop cursor.
1005 * @head: the head for your list.
1006 */
1007 #define list_for_each_prev(pos, head) \
1008 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
1009 pos = pos->prev)
1010
1011 /**
1012 * list_for_each_safe - iterate over a list safe against removal of list entry
1013 * @pos: the &struct list_head to use as a loop cursor.
1014 * @n: another &struct list_head to use as temporary storage
1015 * @head: the head for your list.
1016 */
1017 #define list_for_each_safe(pos, n, head) \
1018 for (pos = (head)->next, n = pos->next; pos != (head); \
1019 pos = n, n = pos->next)
1020
1021 /**
1022 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
1023 * @pos: the &struct list_head to use as a loop cursor.
1024 * @n: another &struct list_head to use as temporary storage
1025 * @head: the head for your list.
1026 */
1027 #define list_for_each_prev_safe(pos, n, head) \
1028 for (pos = (head)->prev, n = pos->prev; \
1029 prefetch(pos->prev), pos != (head); \
1030 pos = n, n = pos->prev)
1031
1032 /**
1033 * list_for_each_entry - iterate over list of given type
1034 * @pos: the type * to use as a loop cursor.
1035 * @head: the head for your list.
1036 * @member: the name of the list_struct within the struct.
1037 */
1038 #define list_for_each_entry(pos, head, member) \
1039 for (pos = list_entry((head)->next, typeof(*pos), member); \
1040 prefetch(pos->member.next), &pos->member != (head); \
1041 pos = list_entry(pos->member.next, typeof(*pos), member))
1042
1043 /**
1044 * list_for_each_entry_reverse - iterate backwards over list of given type.
1045 * @pos: the type * to use as a loop cursor.
1046 * @head: the head for your list.
1047 * @member: the name of the list_struct within the struct.
1048 */
1049 #define list_for_each_entry_reverse(pos, head, member) \
1050 for (pos = list_entry((head)->prev, typeof(*pos), member); \
1051 prefetch(pos->member.prev), &pos->member != (head); \
1052 pos = list_entry(pos->member.prev, typeof(*pos), member))
1053
1054 /**
1055 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
1056 * @pos: the type * to use as a start point
1057 * @head: the head of the list
1058 * @member: the name of the list_struct within the struct.
1059 *
1060 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
1061 */
1062 #define list_prepare_entry(pos, head, member) \
1063 ((pos) ? : list_entry(head, typeof(*pos), member))
1064
1065 /**
1066 * list_for_each_entry_continue - continue iteration over list of given type
1067 * @pos: the type * to use as a loop cursor.
1068 * @head: the head for your list.
1069 * @member: the name of the list_struct within the struct.
1070 *
1071 * Continue to iterate over list of given type, continuing after
1072 * the current position.
1073 */
1074 #define list_for_each_entry_continue(pos, head, member) \
1075 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
1076 prefetch(pos->member.next), &pos->member != (head); \
1077 pos = list_entry(pos->member.next, typeof(*pos), member))
1078
1079 /**
1080 * list_for_each_entry_continue_reverse - iterate backwards from the given point
1081 * @pos: the type * to use as a loop cursor.
1082 * @head: the head for your list.
1083 * @member: the name of the list_struct within the struct.
1084 *
1085 * Start to iterate over list of given type backwards, continuing after
1086 * the current position.
1087 */
1088 #define list_for_each_entry_continue_reverse(pos, head, member) \
1089 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
1090 prefetch(pos->member.prev), &pos->member != (head); \
1091 pos = list_entry(pos->member.prev, typeof(*pos), member))
1092
1093 /**
1094 * list_for_each_entry_from - iterate over list of given type from the current point
1095 * @pos: the type * to use as a loop cursor.
1096 * @head: the head for your list.
1097 * @member: the name of the list_struct within the struct.
1098 *
1099 * Iterate over list of given type, continuing from current position.
1100 */
1101 #define list_for_each_entry_from(pos, head, member) \
1102 for (; prefetch(pos->member.next), &pos->member != (head); \
1103 pos = list_entry(pos->member.next, typeof(*pos), member))
1104
1105 /**
1106 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1107 * @pos: the type * to use as a loop cursor.
1108 * @n: another type * to use as temporary storage
1109 * @head: the head for your list.
1110 * @member: the name of the list_struct within the struct.
1111 */
1112 #define list_for_each_entry_safe(pos, n, head, member) \
1113 for (pos = list_entry((head)->next, typeof(*pos), member), \
1114 n = list_entry(pos->member.next, typeof(*pos), member); \
1115 &pos->member != (head); \
1116 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1117
1118 /**
1119 * list_for_each_entry_safe_continue
1120 * @pos: the type * to use as a loop cursor.
1121 * @n: another type * to use as temporary storage
1122 * @head: the head for your list.
1123 * @member: the name of the list_struct within the struct.
1124 *
1125 * Iterate over list of given type, continuing after current point,
1126 * safe against removal of list entry.
1127 */
1128 #define list_for_each_entry_safe_continue(pos, n, head, member) \
1129 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
1130 n = list_entry(pos->member.next, typeof(*pos), member); \
1131 &pos->member != (head); \
1132 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1133
1134 /**
1135 * list_for_each_entry_safe_from
1136 * @pos: the type * to use as a loop cursor.
1137 * @n: another type * to use as temporary storage
1138 * @head: the head for your list.
1139 * @member: the name of the list_struct within the struct.
1140 *
1141 * Iterate over list of given type from current point, safe against
1142 * removal of list entry.
1143 */
1144 #define list_for_each_entry_safe_from(pos, n, head, member) \
1145 for (n = list_entry(pos->member.next, typeof(*pos), member); \
1146 &pos->member != (head); \
1147 pos = n, n = list_entry(n->member.next, typeof(*n), member))
1148
1149 /**
1150 * list_for_each_entry_safe_reverse
1151 * @pos: the type * to use as a loop cursor.
1152 * @n: another type * to use as temporary storage
1153 * @head: the head for your list.
1154 * @member: the name of the list_struct within the struct.
1155 *
1156 * Iterate backwards over list of given type, safe against removal
1157 * of list entry.
1158 */
1159 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
1160 for (pos = list_entry((head)->prev, typeof(*pos), member), \
1161 n = list_entry(pos->member.prev, typeof(*pos), member); \
1162 &pos->member != (head); \
1163 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
1164
1165 /*
1166 * Double linked lists with a single pointer list head.
1167 * Mostly useful for hash tables where the two pointer list head is
1168 * too wasteful.
1169 * You lose the ability to access the tail in O(1).
1170 */
1171
1172 struct hlist_head {
1173 struct hlist_node *first;
1174 };
1175
1176 struct hlist_node {
1177 struct hlist_node *next, **pprev;
1178 };
1179
1180 #define HLIST_HEAD_INIT { .first = NULL }
1181 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
1182 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
1183 static inline void INIT_HLIST_NODE(struct hlist_node *h)
1184 {
1185 h->next = NULL;
1186 h->pprev = NULL;
1187 }
1188
1189 static inline int hlist_unhashed(const struct hlist_node *h)
1190 {
1191 return !h->pprev;
1192 }
1193
1194 static inline int hlist_empty(const struct hlist_head *h)
1195 {
1196 return !h->first;
1197 }
1198
1199 static inline void __hlist_del(struct hlist_node *n)
1200 {
1201 struct hlist_node *next = n->next;
1202 struct hlist_node **pprev = n->pprev;
1203 *pprev = next;
1204 if (next)
1205 next->pprev = pprev;
1206 }
1207
1208 static inline void hlist_del(struct hlist_node *n)
1209 {
1210 __hlist_del(n);
1211 n->next = LIST_POISON1;
1212 n->pprev = LIST_POISON2;
1213 }
1214
1215 static inline void hlist_del_init(struct hlist_node *n)
1216 {
1217 if (!hlist_unhashed(n)) {
1218 __hlist_del(n);
1219 INIT_HLIST_NODE(n);
1220 }
1221 }
1222
1223 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
1224 {
1225 struct hlist_node *first = h->first;
1226 n->next = first;
1227 if (first)
1228 first->pprev = &n->next;
1229 h->first = n;
1230 n->pprev = &h->first;
1231 }
1232
1233 /* next must be != NULL */
1234 static inline void hlist_add_before(struct hlist_node *n,
1235 struct hlist_node *next)
1236 {
1237 n->pprev = next->pprev;
1238 n->next = next;
1239 next->pprev = &n->next;
1240 *(n->pprev) = n;
1241 }
1242
1243 static inline void hlist_add_after(struct hlist_node *n,
1244 struct hlist_node *next)
1245 {
1246 next->next = n->next;
1247 n->next = next;
1248 next->pprev = &n->next;
1249
1250 if(next->next)
1251 next->next->pprev = &next->next;
1252 }
1253
1254 /*
1255 * Move a list from one list head to another. Fixup the pprev
1256 * reference of the first entry if it exists.
1257 */
1258 static inline void hlist_move_list(struct hlist_head *old,
1259 struct hlist_head *new)
1260 {
1261 new->first = old->first;
1262 if (new->first)
1263 new->first->pprev = &new->first;
1264 old->first = NULL;
1265 }
1266
1267 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1268
1269 #define hlist_for_each(pos, head) \
1270 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
1271 pos = pos->next)
1272
1273 #define hlist_for_each_safe(pos, n, head) \
1274 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1275 pos = n)
1276
1277 /**
1278 * hlist_for_each_entry - iterate over list of given type
1279 * @tpos: the type * to use as a loop cursor.
1280 * @pos: the &struct hlist_node to use as a loop cursor.
1281 * @head: the head for your list.
1282 * @member: the name of the hlist_node within the struct.
1283 */
1284 #define hlist_for_each_entry(tpos, pos, head, member) \
1285 for (pos = (head)->first; \
1286 pos && ({ prefetch(pos->next); 1;}) && \
1287 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1288 pos = pos->next)
1289
1290 /**
1291 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1292 * @tpos: the type * to use as a loop cursor.
1293 * @pos: the &struct hlist_node to use as a loop cursor.
1294 * @member: the name of the hlist_node within the struct.
1295 */
1296 #define hlist_for_each_entry_continue(tpos, pos, member) \
1297 for (pos = (pos)->next; \
1298 pos && ({ prefetch(pos->next); 1;}) && \
1299 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1300 pos = pos->next)
1301
1302 /**
1303 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1304 * @tpos: the type * to use as a loop cursor.
1305 * @pos: the &struct hlist_node to use as a loop cursor.
1306 * @member: the name of the hlist_node within the struct.
1307 */
1308 #define hlist_for_each_entry_from(tpos, pos, member) \
1309 for (; pos && ({ prefetch(pos->next); 1;}) && \
1310 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1311 pos = pos->next)
1312
1313 /**
1314 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1315 * @tpos: the type * to use as a loop cursor.
1316 * @pos: the &struct hlist_node to use as a loop cursor.
1317 * @n: another &struct hlist_node to use as temporary storage
1318 * @head: the head for your list.
1319 * @member: the name of the hlist_node within the struct.
1320 */
1321 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
1322 for (pos = (head)->first; \
1323 pos && ({ n = pos->next; 1; }) && \
1324 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
1325 pos = n)
1326
1327 #endif
This page took 0.060628 seconds and 4 git commands to generate.