root/tools/virtio/linux/kernel.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. kmalloc
  2. kmalloc_array
  3. kzalloc
  4. alloc_pages_exact
  5. kfree
  6. free_pages_exact
  7. krealloc
  8. __get_free_page
  9. free_page

   1 /* SPDX-License-Identifier: GPL-2.0 */
   2 #ifndef KERNEL_H
   3 #define KERNEL_H
   4 #include <stdbool.h>
   5 #include <stdlib.h>
   6 #include <stddef.h>
   7 #include <stdio.h>
   8 #include <string.h>
   9 #include <assert.h>
  10 #include <stdarg.h>
  11 
  12 #include <linux/compiler.h>
  13 #include <linux/types.h>
  14 #include <linux/printk.h>
  15 #include <linux/bug.h>
  16 #include <errno.h>
  17 #include <unistd.h>
  18 #include <asm/barrier.h>
  19 
  20 #define CONFIG_SMP
  21 
  22 #define PAGE_SIZE getpagesize()
  23 #define PAGE_MASK (~(PAGE_SIZE-1))
  24 #define PAGE_ALIGN(x) ((x + PAGE_SIZE - 1) & PAGE_MASK)
  25 
  26 /* generic data direction definitions */
  27 #define READ                    0
  28 #define WRITE                   1
  29 
  30 typedef unsigned long long phys_addr_t;
  31 typedef unsigned long long dma_addr_t;
  32 typedef size_t __kernel_size_t;
  33 typedef unsigned int __wsum;
  34 
  35 struct page {
  36         unsigned long long dummy;
  37 };
  38 
  39 /* Physical == Virtual */
  40 #define virt_to_phys(p) ((unsigned long)p)
  41 #define phys_to_virt(a) ((void *)(unsigned long)(a))
  42 /* Page address: Virtual / 4K */
  43 #define page_to_phys(p) ((dma_addr_t)(unsigned long)(p))
  44 #define virt_to_page(p) ((struct page *)((unsigned long)p & PAGE_MASK))
  45 
  46 #define offset_in_page(p) (((unsigned long)p) % PAGE_SIZE)
  47 
  48 #define __printf(a,b) __attribute__((format(printf,a,b)))
  49 
  50 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  51 
  52 extern void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end;
  53 static inline void *kmalloc(size_t s, gfp_t gfp)
  54 {
  55         if (__kmalloc_fake)
  56                 return __kmalloc_fake;
  57         return malloc(s);
  58 }
  59 static inline void *kmalloc_array(unsigned n, size_t s, gfp_t gfp)
  60 {
  61         return kmalloc(n * s, gfp);
  62 }
  63 
  64 static inline void *kzalloc(size_t s, gfp_t gfp)
  65 {
  66         void *p = kmalloc(s, gfp);
  67 
  68         memset(p, 0, s);
  69         return p;
  70 }
  71 
  72 static inline void *alloc_pages_exact(size_t s, gfp_t gfp)
  73 {
  74         return kmalloc(s, gfp);
  75 }
  76 
  77 static inline void kfree(void *p)
  78 {
  79         if (p >= __kfree_ignore_start && p < __kfree_ignore_end)
  80                 return;
  81         free(p);
  82 }
  83 
  84 static inline void free_pages_exact(void *p, size_t s)
  85 {
  86         kfree(p);
  87 }
  88 
  89 static inline void *krealloc(void *p, size_t s, gfp_t gfp)
  90 {
  91         return realloc(p, s);
  92 }
  93 
  94 
  95 static inline unsigned long __get_free_page(gfp_t gfp)
  96 {
  97         void *p;
  98 
  99         posix_memalign(&p, PAGE_SIZE, PAGE_SIZE);
 100         return (unsigned long)p;
 101 }
 102 
 103 static inline void free_page(unsigned long addr)
 104 {
 105         free((void *)addr);
 106 }
 107 
 108 #define container_of(ptr, type, member) ({                      \
 109         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
 110         (type *)( (char *)__mptr - offsetof(type,member) );})
 111 
 112 #define uninitialized_var(x) x = x
 113 
 114 # ifndef likely
 115 #  define likely(x)     (__builtin_expect(!!(x), 1))
 116 # endif
 117 # ifndef unlikely
 118 #  define unlikely(x)   (__builtin_expect(!!(x), 0))
 119 # endif
 120 
 121 #define pr_err(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
 122 #ifdef DEBUG
 123 #define pr_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
 124 #else
 125 #define pr_debug(format, ...) do {} while (0)
 126 #endif
 127 #define dev_err(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
 128 #define dev_warn(dev, format, ...) fprintf (stderr, format, ## __VA_ARGS__)
 129 
 130 #define WARN_ON_ONCE(cond) (unlikely(cond) ? fprintf (stderr, "WARNING\n") : 0)
 131 
 132 #define min(x, y) ({                            \
 133         typeof(x) _min1 = (x);                  \
 134         typeof(y) _min2 = (y);                  \
 135         (void) (&_min1 == &_min2);              \
 136         _min1 < _min2 ? _min1 : _min2; })
 137 
 138 /* TODO: empty stubs for now. Broken but enough for virtio_ring.c */
 139 #define list_add_tail(a, b) do {} while (0)
 140 #define list_del(a) do {} while (0)
 141 #define list_for_each_entry(a, b, c) while (0)
 142 /* end of stubs */
 143 
 144 #endif /* KERNEL_H */

/* [<][>][^][v][top][bottom][index][help] */