1 #ifndef __LINUX_PERCPU_H
2 #define __LINUX_PERCPU_H
3 #include <linux/spinlock.h> /* For preempt_disable() */
4 #include <linux/slab.h> /* For kmalloc() */
6 #include <linux/string.h> /* For memset() */
7 #include <asm/percpu.h>
9 /* Enough to cover all DEFINE_PER_CPUs in kernel, including modules. */
10 #ifndef PERCPU_ENOUGH_ROOM
11 #define PERCPU_ENOUGH_ROOM 32768
14 /* Must be an lvalue. */
15 #define get_cpu_var(var) (*({ preempt_disable(); &__get_cpu_var(var); }))
16 #define put_cpu_var(var) preempt_enable()
26 * Use this to get to a cpu's version of the per-cpu object allocated using
27 * alloc_percpu. Non-atomic access to the current CPU's version should
28 * probably be combined with get_cpu()/put_cpu().
30 #define per_cpu_ptr(ptr, cpu) \
32 struct percpu_data *__p = (struct percpu_data *)~(unsigned long)(ptr); \
33 (__typeof__(ptr))__p->ptrs[(cpu)]; \
36 extern void *__alloc_percpu(size_t size, size_t align);
37 extern void free_percpu(const void *);
39 #else /* CONFIG_SMP */
41 #define per_cpu_ptr(ptr, cpu) (ptr)
43 static inline void *__alloc_percpu(size_t size, size_t align)
45 void *ret = kmalloc(size, GFP_KERNEL);
50 static inline void free_percpu(const void *ptr)
55 #endif /* CONFIG_SMP */
57 /* Simple wrapper for the common case: zeros memory. */
58 #define alloc_percpu(type) \
59 ((type *)(__alloc_percpu(sizeof(type), __alignof__(type))))
61 #endif /* __LINUX_PERCPU_H */