Rusty Russell | 61d0b5a4 | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 1 | #ifndef KERNEL_H |
| 2 | #define KERNEL_H |
| 3 | #include <stdbool.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <assert.h> |
| 9 | #include <stdarg.h> |
| 10 | |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/printk.h> |
| 13 | #include <linux/bug.h> |
| 14 | #include <errno.h> |
| 15 | #include <unistd.h> |
| 16 | #include <asm/barrier.h> |
| 17 | |
| 18 | #define CONFIG_SMP |
| 19 | |
| 20 | #define PAGE_SIZE getpagesize() |
| 21 | #define PAGE_MASK (~(PAGE_SIZE-1)) |
| 22 | |
| 23 | typedef unsigned long long dma_addr_t; |
| 24 | typedef size_t __kernel_size_t; |
| 25 | |
| 26 | struct page { |
| 27 | unsigned long long dummy; |
| 28 | }; |
| 29 | |
| 30 | /* Physical == Virtual */ |
| 31 | #define virt_to_phys(p) ((unsigned long)p) |
| 32 | #define phys_to_virt(a) ((void *)(unsigned long)(a)) |
| 33 | /* Page address: Virtual / 4K */ |
| 34 | #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p)) |
| 35 | #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK)) |
| 36 | |
| 37 | #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE) |
| 38 | |
| 39 | #define __printf(a,b) __attribute__((format(printf,a,b))) |
| 40 | |
Rusty Russell | 61d0b5a4 | 2013-03-18 13:22:19 +1030 | [diff] [blame] | 41 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) |
| 42 | |
| 43 | extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end; |
| 44 | static inline void *kmalloc(size_t s, gfp_t gfp) |
| 45 | { |
| 46 | if (__kmalloc_fake) |
| 47 | return __kmalloc_fake; |
| 48 | return malloc(s); |
| 49 | } |
| 50 | |
| 51 | static inline void kfree(void *p) |
| 52 | { |
| 53 | if (p >= __kfree_ignore_start && p < __kfree_ignore_end) |
| 54 | return; |
| 55 | free(p); |
| 56 | } |
| 57 | |
| 58 | static inline void *krealloc(void *p, size_t s, gfp_t gfp) |
| 59 | { |
| 60 | return realloc(p, s); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | static inline unsigned long __get_free_page(gfp_t gfp) |
| 65 | { |
| 66 | void *p; |
| 67 | |
| 68 | posix_memalign(&p, PAGE_SIZE, PAGE_SIZE); |
| 69 | return (unsigned long)p; |
| 70 | } |
| 71 | |
| 72 | static inline void free_page(unsigned long addr) |
| 73 | { |
| 74 | free((void *)addr); |
| 75 | } |
| 76 | |
| 77 | #define container_of(ptr, type, member) ({ \ |
| 78 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 79 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 80 | |
| 81 | #define uninitialized_var(x) x = x |
| 82 | |
| 83 | # ifndef likely |
| 84 | # define likely(x) (__builtin_expect(!!(x), 1)) |
| 85 | # endif |
| 86 | # ifndef unlikely |
| 87 | # define unlikely(x) (__builtin_expect(!!(x), 0)) |
| 88 | # endif |
| 89 | |
| 90 | #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 91 | #ifdef DEBUG |
| 92 | #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 93 | #else |
| 94 | #define pr_debug(format, ...) do {} while (0) |
| 95 | #endif |
| 96 | #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 97 | #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__) |
| 98 | |
| 99 | #define min(x, y) ({ \ |
| 100 | typeof(x) _min1 = (x); \ |
| 101 | typeof(y) _min2 = (y); \ |
| 102 | (void) (&_min1 == &_min2); \ |
| 103 | _min1 < _min2 ? _min1 : _min2; }) |
| 104 | |
| 105 | #endif /* KERNEL_H */ |