]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/vmalloc.h
New upstream snapshot
[bcachefs-tools-debian] / include / linux / vmalloc.h
1 #ifndef __TOOLS_LINUX_VMALLOC_H
2 #define __TOOLS_LINUX_VMALLOC_H
3
4 #include <stdlib.h>
5 #include <sys/mman.h>
6
7 #include "linux/slab.h"
8 #include "tools-util.h"
9
10 #define PAGE_KERNEL             0
11 #define PAGE_KERNEL_EXEC        1
12
13 #define vfree(p)                free(p)
14
15 static inline void *__vmalloc(unsigned long size, gfp_t gfp_mask)
16 {
17         void *p;
18
19         size = round_up(size, PAGE_SIZE);
20
21         run_shrinkers();
22
23         p = aligned_alloc(PAGE_SIZE, size);
24         if (!p)
25                 return NULL;
26
27         if (gfp_mask & __GFP_ZERO)
28                 memset(p, 0, size);
29
30         return p;
31 }
32
33 static inline void *vmalloc_exec(unsigned long size, gfp_t gfp_mask)
34 {
35         void *p;
36
37         p = __vmalloc(size, gfp_mask);
38         if (!p)
39                 return NULL;
40
41         if (mprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC)) {
42                 vfree(p);
43                 return NULL;
44         }
45
46         return p;
47 }
48
49 static inline void *vmalloc(unsigned long size)
50 {
51         return __vmalloc(size, GFP_KERNEL);
52 }
53
54 static inline void *vzalloc(unsigned long size)
55 {
56         return __vmalloc(size, GFP_KERNEL|__GFP_ZERO);
57 }
58
59 #endif /* __TOOLS_LINUX_VMALLOC_H */