kmem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef __XFS_SUPPORT_KMEM_H__
  19. #define __XFS_SUPPORT_KMEM_H__
  20. #include <linux/slab.h>
  21. #include <linux/sched.h>
  22. #include <linux/mm.h>
  23. #include <linux/vmalloc.h>
  24. /*
  25. * General memory allocation interfaces
  26. */
  27. typedef unsigned __bitwise xfs_km_flags_t;
  28. #define KM_SLEEP ((__force xfs_km_flags_t)0x0001u)
  29. #define KM_NOSLEEP ((__force xfs_km_flags_t)0x0002u)
  30. #define KM_NOFS ((__force xfs_km_flags_t)0x0004u)
  31. #define KM_MAYFAIL ((__force xfs_km_flags_t)0x0008u)
  32. #define KM_ZERO ((__force xfs_km_flags_t)0x0010u)
  33. /*
  34. * We use a special process flag to avoid recursive callbacks into
  35. * the filesystem during transactions. We will also issue our own
  36. * warnings, so we explicitly skip any generic ones (silly of us).
  37. */
  38. static inline gfp_t
  39. kmem_flags_convert(xfs_km_flags_t flags)
  40. {
  41. gfp_t lflags;
  42. BUG_ON(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL|KM_ZERO));
  43. if (flags & KM_NOSLEEP) {
  44. lflags = GFP_ATOMIC | __GFP_NOWARN;
  45. } else {
  46. lflags = GFP_KERNEL | __GFP_NOWARN;
  47. if (flags & KM_NOFS)
  48. lflags &= ~__GFP_FS;
  49. }
  50. /*
  51. * Default page/slab allocator behavior is to retry for ever
  52. * for small allocations. We can override this behavior by using
  53. * __GFP_RETRY_MAYFAIL which will tell the allocator to retry as long
  54. * as it is feasible but rather fail than retry forever for all
  55. * request sizes.
  56. */
  57. if (flags & KM_MAYFAIL)
  58. lflags |= __GFP_RETRY_MAYFAIL;
  59. if (flags & KM_ZERO)
  60. lflags |= __GFP_ZERO;
  61. return lflags;
  62. }
  63. extern void *kmem_alloc(size_t, xfs_km_flags_t);
  64. extern void *kmem_alloc_large(size_t size, xfs_km_flags_t);
  65. extern void *kmem_realloc(const void *, size_t, xfs_km_flags_t);
  66. static inline void kmem_free(const void *ptr)
  67. {
  68. kvfree(ptr);
  69. }
  70. static inline void *
  71. kmem_zalloc(size_t size, xfs_km_flags_t flags)
  72. {
  73. return kmem_alloc(size, flags | KM_ZERO);
  74. }
  75. static inline void *
  76. kmem_zalloc_large(size_t size, xfs_km_flags_t flags)
  77. {
  78. return kmem_alloc_large(size, flags | KM_ZERO);
  79. }
  80. /*
  81. * Zone interfaces
  82. */
  83. #define KM_ZONE_HWALIGN SLAB_HWCACHE_ALIGN
  84. #define KM_ZONE_RECLAIM SLAB_RECLAIM_ACCOUNT
  85. #define KM_ZONE_SPREAD SLAB_MEM_SPREAD
  86. #define KM_ZONE_ACCOUNT SLAB_ACCOUNT
  87. #define kmem_zone kmem_cache
  88. #define kmem_zone_t struct kmem_cache
  89. static inline kmem_zone_t *
  90. kmem_zone_init(int size, char *zone_name)
  91. {
  92. return kmem_cache_create(zone_name, size, 0, 0, NULL);
  93. }
  94. static inline kmem_zone_t *
  95. kmem_zone_init_flags(int size, char *zone_name, slab_flags_t flags,
  96. void (*construct)(void *))
  97. {
  98. return kmem_cache_create(zone_name, size, 0, flags, construct);
  99. }
  100. static inline void
  101. kmem_zone_free(kmem_zone_t *zone, void *ptr)
  102. {
  103. kmem_cache_free(zone, ptr);
  104. }
  105. static inline void
  106. kmem_zone_destroy(kmem_zone_t *zone)
  107. {
  108. kmem_cache_destroy(zone);
  109. }
  110. extern void *kmem_zone_alloc(kmem_zone_t *, xfs_km_flags_t);
  111. static inline void *
  112. kmem_zone_zalloc(kmem_zone_t *zone, xfs_km_flags_t flags)
  113. {
  114. return kmem_zone_alloc(zone, flags | KM_ZERO);
  115. }
  116. #endif /* __XFS_SUPPORT_KMEM_H__ */