1 /* SPDX-License-Identifier: GPL-2.0 */
12 #include <linux/compiler.h>
13 #include <linux/types.h>
14 #include <linux/overflow.h>
15 #include <linux/list.h>
16 #include <linux/printk.h>
17 #include <linux/bug.h>
20 #include <asm/barrier.h>
24 #define PAGE_SIZE getpagesize()
25 #define PAGE_MASK (~(PAGE_SIZE-1))
26 #define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
28 /* generic data direction definitions */
32 typedef unsigned long long dma_addr_t;
33 typedef size_t __kernel_size_t;
34 typedef unsigned int __wsum;
37 unsigned long long dummy;
40 /* Physical == Virtual */
41 #define virt_to_phys(p) ((unsigned long)p)
42 #define phys_to_virt(a) ((void *)(unsigned long)(a))
43 /* Page address: Virtual / 4K */
44 #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
45 #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
47 #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
49 #define __printf(a,b) __attribute__((format(printf,a,b)))
51 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
53 extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
54 static inline void *kmalloc(size_t s, gfp_t gfp)
57 return __kmalloc_fake;
60 static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
62 return kmalloc(n * s, gfp);
65 static inline void *kzalloc(size_t s, gfp_t gfp)
67 void *p = kmalloc(s, gfp);
73 static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
75 return kmalloc(s, gfp);
78 static inline void kfree(void *p)
80 if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
85 static inline void free_pages_exact(void *p, size_t s)
90 static inline void *krealloc(void *p, size_t s, gfp_t gfp)
96 static inline unsigned long __get_free_page(gfp_t gfp)
100 posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
101 return (unsigned long)p;
104 static inline void free_page(unsigned long addr)
109 #define container_of(ptr, type, member) ({ \
110 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
111 (type *)( (char *)__mptr - offsetof(type,member) );})
114 # define likely(x) (__builtin_expect(!!(x), 1))
117 # define unlikely(x) (__builtin_expect(!!(x), 0))
120 static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t gfp)
124 if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
127 return krealloc(p, bytes, gfp);
130 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
132 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
134 #define pr_debug(format, ...) do {} while (0)
136 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
137 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
138 #define dev_warn_once(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
140 #define min(x, y) ({ \
141 typeof(x) _min1 = (x); \
142 typeof(y) _min2 = (y); \
143 (void) (&_min1 == &_min2); \
144 _min1 < _min2 ? _min1 : _min2; })
146 #endif /* KERNEL_H */