1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#ifndef _DRM_MEM_UTIL_H_
28#define _DRM_MEM_UTIL_H_
29
30#include <linux/vmalloc.h>
31
32static __inline__ void *drm_calloc_large(size_t nmemb, size_t size)
33{
34 if (size != 0 && nmemb > SIZE_MAX / size)
35 return NULL;
36
37 if (size * nmemb <= PAGE_SIZE)
38 return kcalloc(nmemb, size, GFP_KERNEL);
39
40 return __vmalloc(size * nmemb,
41 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
42}
43
44
45static __inline__ void *drm_malloc_ab(size_t nmemb, size_t size)
46{
47 if (size != 0 && nmemb > SIZE_MAX / size)
48 return NULL;
49
50 if (size * nmemb <= PAGE_SIZE)
51 return kmalloc(nmemb * size, GFP_KERNEL);
52
53 return __vmalloc(size * nmemb,
54 GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
55}
56
57static __inline__ void *drm_malloc_gfp(size_t nmemb, size_t size, gfp_t gfp)
58{
59 if (size != 0 && nmemb > SIZE_MAX / size)
60 return NULL;
61
62 if (size * nmemb <= PAGE_SIZE)
63 return kmalloc(nmemb * size, gfp);
64
65 if (gfp & __GFP_RECLAIMABLE) {
66 void *ptr = kmalloc(nmemb * size,
67 gfp | __GFP_NOWARN | __GFP_NORETRY);
68 if (ptr)
69 return ptr;
70 }
71
72 return __vmalloc(size * nmemb,
73 gfp | __GFP_HIGHMEM, PAGE_KERNEL);
74}
75
76static __inline void drm_free_large(void *ptr)
77{
78 kvfree(ptr);
79}
80
81#endif
82