]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/vmalloc.h
efcc1912765f8c81115ab4a0ade75ca8fe3bc2ac
[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         run_shrinkers();
20
21         p = aligned_alloc(PAGE_SIZE, size);
22         if (!p)
23                 return NULL;
24
25         if (gfp_mask & __GFP_ZERO)
26                 memset(p, 0, size);
27
28         return p;
29 }
30
31 static inline void *vmalloc_exec(unsigned long size, gfp_t gfp_mask)
32 {
33         void *p;
34
35         p = __vmalloc(size, gfp_mask);
36         if (!p)
37                 return NULL;
38
39         if (mprotect(p, size, PROT_READ|PROT_WRITE|PROT_EXEC)) {
40                 vfree(p);
41                 return NULL;
42         }
43
44         return p;
45 }
46
47 static inline void *vmalloc(unsigned long size)
48 {
49         return __vmalloc(size, GFP_KERNEL);
50 }
51
52 static inline void *vzalloc(unsigned long size)
53 {
54         return __vmalloc(size, GFP_KERNEL|__GFP_ZERO);
55 }
56
57 #endif /* __TOOLS_LINUX_VMALLOC_H */