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