162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * malloc.h - NTFS kernel memory handling. Part of the Linux-NTFS project.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2001-2005 Anton Altaparmakov
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef _LINUX_NTFS_MALLOC_H
962306a36Sopenharmony_ci#define _LINUX_NTFS_MALLOC_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/vmalloc.h>
1262306a36Sopenharmony_ci#include <linux/slab.h>
1362306a36Sopenharmony_ci#include <linux/highmem.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/**
1662306a36Sopenharmony_ci * __ntfs_malloc - allocate memory in multiples of pages
1762306a36Sopenharmony_ci * @size:	number of bytes to allocate
1862306a36Sopenharmony_ci * @gfp_mask:	extra flags for the allocator
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * Internal function.  You probably want ntfs_malloc_nofs()...
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
2362306a36Sopenharmony_ci * returns a pointer to the allocated memory.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * If there was insufficient memory to complete the request, return NULL.
2662306a36Sopenharmony_ci * Depending on @gfp_mask the allocation may be guaranteed to succeed.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_cistatic inline void *__ntfs_malloc(unsigned long size, gfp_t gfp_mask)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	if (likely(size <= PAGE_SIZE)) {
3162306a36Sopenharmony_ci		BUG_ON(!size);
3262306a36Sopenharmony_ci		/* kmalloc() has per-CPU caches so is faster for now. */
3362306a36Sopenharmony_ci		return kmalloc(PAGE_SIZE, gfp_mask & ~__GFP_HIGHMEM);
3462306a36Sopenharmony_ci		/* return (void *)__get_free_page(gfp_mask); */
3562306a36Sopenharmony_ci	}
3662306a36Sopenharmony_ci	if (likely((size >> PAGE_SHIFT) < totalram_pages()))
3762306a36Sopenharmony_ci		return __vmalloc(size, gfp_mask);
3862306a36Sopenharmony_ci	return NULL;
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/**
4262306a36Sopenharmony_ci * ntfs_malloc_nofs - allocate memory in multiples of pages
4362306a36Sopenharmony_ci * @size:	number of bytes to allocate
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
4662306a36Sopenharmony_ci * returns a pointer to the allocated memory.
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * If there was insufficient memory to complete the request, return NULL.
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_cistatic inline void *ntfs_malloc_nofs(unsigned long size)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM);
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/**
5662306a36Sopenharmony_ci * ntfs_malloc_nofs_nofail - allocate memory in multiples of pages
5762306a36Sopenharmony_ci * @size:	number of bytes to allocate
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
6062306a36Sopenharmony_ci * returns a pointer to the allocated memory.
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * This function guarantees that the allocation will succeed.  It will sleep
6362306a36Sopenharmony_ci * for as long as it takes to complete the allocation.
6462306a36Sopenharmony_ci *
6562306a36Sopenharmony_ci * If there was insufficient memory to complete the request, return NULL.
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_cistatic inline void *ntfs_malloc_nofs_nofail(unsigned long size)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM | __GFP_NOFAIL);
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistatic inline void ntfs_free(void *addr)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	kvfree(addr);
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#endif /* _LINUX_NTFS_MALLOC_H */
78