malloc.h 2.3 KB

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