malloc.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * malloc.h - NTFS kernel memory handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2005 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #ifndef _LINUX_NTFS_MALLOC_H
  22. #define _LINUX_NTFS_MALLOC_H
  23. #include <linux/vmalloc.h>
  24. #include <linux/slab.h>
  25. #include <linux/highmem.h>
  26. /**
  27. * __ntfs_malloc - allocate memory in multiples of pages
  28. * @size: number of bytes to allocate
  29. * @gfp_mask: extra flags for the allocator
  30. *
  31. * Internal function. You probably want ntfs_malloc_nofs()...
  32. *
  33. * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
  34. * returns a pointer to the allocated memory.
  35. *
  36. * If there was insufficient memory to complete the request, return NULL.
  37. * Depending on @gfp_mask the allocation may be guaranteed to succeed.
  38. */
  39. static inline void *__ntfs_malloc(unsigned long size, gfp_t gfp_mask)
  40. {
  41. if (likely(size <= PAGE_SIZE)) {
  42. BUG_ON(!size);
  43. /* kmalloc() has per-CPU caches so is faster for now. */
  44. return kmalloc(PAGE_SIZE, gfp_mask & ~__GFP_HIGHMEM);
  45. /* return (void *)__get_free_page(gfp_mask); */
  46. }
  47. if (likely((size >> PAGE_SHIFT) < totalram_pages))
  48. return __vmalloc(size, gfp_mask, PAGE_KERNEL);
  49. return NULL;
  50. }
  51. /**
  52. * ntfs_malloc_nofs - allocate memory in multiples of pages
  53. * @size: number of bytes to allocate
  54. *
  55. * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
  56. * returns a pointer to the allocated memory.
  57. *
  58. * If there was insufficient memory to complete the request, return NULL.
  59. */
  60. static inline void *ntfs_malloc_nofs(unsigned long size)
  61. {
  62. return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM);
  63. }
  64. /**
  65. * ntfs_malloc_nofs_nofail - allocate memory in multiples of pages
  66. * @size: number of bytes to allocate
  67. *
  68. * Allocates @size bytes of memory, rounded up to multiples of PAGE_SIZE and
  69. * returns a pointer to the allocated memory.
  70. *
  71. * This function guarantees that the allocation will succeed. It will sleep
  72. * for as long as it takes to complete the allocation.
  73. *
  74. * If there was insufficient memory to complete the request, return NULL.
  75. */
  76. static inline void *ntfs_malloc_nofs_nofail(unsigned long size)
  77. {
  78. return __ntfs_malloc(size, GFP_NOFS | __GFP_HIGHMEM | __GFP_NOFAIL);
  79. }
  80. static inline void ntfs_free(void *addr)
  81. {
  82. kvfree(addr);
  83. }
  84. #endif /* _LINUX_NTFS_MALLOC_H */