1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * malloc.h - NTFS kernel memory handling. Part of the Linux-NTFS project.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2001-2005 Anton Altaparmakov
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef _LINUX_NTFS_MALLOC_H
9*4882a593Smuzhiyun #define _LINUX_NTFS_MALLOC_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/vmalloc.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/highmem.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /**
16*4882a593Smuzhiyun * __ntfs_malloc - allocate memory in multiples of pages
17*4882a593Smuzhiyun * @size: number of bytes to allocate
18*4882a593Smuzhiyun * @gfp_mask: extra flags for the allocator
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Internal function. You probably want ntfs_malloc_nofs()...
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
23*4882a593Smuzhiyun * returns a pointer to the allocated memory.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * If there was insufficient memory to complete the request, return NULL.
26*4882a593Smuzhiyun * Depending on @gfp_mask the allocation may be guaranteed to succeed.
27*4882a593Smuzhiyun */
__ntfs_malloc(unsigned long size,gfp_t gfp_mask)28*4882a593Smuzhiyun static inline void *__ntfs_malloc(unsigned long size, gfp_t gfp_mask)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun if (likely(size <= PAGE_SIZE)) {
31*4882a593Smuzhiyun BUG_ON(!size);
32*4882a593Smuzhiyun /* kmalloc() has per-CPU caches so is faster for now. */
33*4882a593Smuzhiyun return kmalloc(PAGE_SIZE, gfp_mask & ~__GFP_HIGHMEM);
34*4882a593Smuzhiyun /* return (void *)__get_free_page(gfp_mask); */
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun if (likely((size >> PAGE_SHIFT) < totalram_pages()))
37*4882a593Smuzhiyun return __vmalloc(size, gfp_mask);
38*4882a593Smuzhiyun return NULL;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun * ntfs_malloc_nofs - allocate memory in multiples of pages
43*4882a593Smuzhiyun * @size: number of bytes to allocate
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
46*4882a593Smuzhiyun * returns a pointer to the allocated memory.
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * If there was insufficient memory to complete the request, return NULL.
49*4882a593Smuzhiyun */
ntfs_malloc_nofs(unsigned long size)50*4882a593Smuzhiyun static inline void *ntfs_malloc_nofs(unsigned long size)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun * ntfs_malloc_nofs_nofail - allocate memory in multiples of pages
57*4882a593Smuzhiyun * @size: number of bytes to allocate
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
60*4882a593Smuzhiyun * returns a pointer to the allocated memory.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * This function guarantees that the allocation will succeed. It will sleep
63*4882a593Smuzhiyun * for as long as it takes to complete the allocation.
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * If there was insufficient memory to complete the request, return NULL.
66*4882a593Smuzhiyun */
ntfs_malloc_nofs_nofail(unsigned long size)67*4882a593Smuzhiyun static inline void *ntfs_malloc_nofs_nofail(unsigned long size)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM | __GFP_NOFAIL);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
ntfs_free(void * addr)72*4882a593Smuzhiyun static inline void ntfs_free(void *addr)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun kvfree(addr);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #endif /* _LINUX_NTFS_MALLOC_H */
78