kmem.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /*
  24. * Process flags handling
  25. */
  26. #define PFLAGS_TEST_NOIO() (current->flags & PF_NOIO)
  27. #define PFLAGS_TEST_FSTRANS() (current->flags & PF_FSTRANS)
  28. #define PFLAGS_SET_NOIO() do { \
  29. current->flags |= PF_NOIO; \
  30. } while (0)
  31. #define PFLAGS_CLEAR_NOIO() do { \
  32. current->flags &= ~PF_NOIO; \
  33. } while (0)
  34. /* these could be nested, so we save state */
  35. #define PFLAGS_SET_FSTRANS(STATEP) do { \
  36. *(STATEP) = current->flags; \
  37. current->flags |= PF_FSTRANS; \
  38. } while (0)
  39. #define PFLAGS_CLEAR_FSTRANS(STATEP) do { \
  40. *(STATEP) = current->flags; \
  41. current->flags &= ~PF_FSTRANS; \
  42. } while (0)
  43. /* Restore the PF_FSTRANS state to what was saved in STATEP */
  44. #define PFLAGS_RESTORE_FSTRANS(STATEP) do { \
  45. current->flags = ((current->flags & ~PF_FSTRANS) | \
  46. (*(STATEP) & PF_FSTRANS)); \
  47. } while (0)
  48. #define PFLAGS_DUP(OSTATEP, NSTATEP) do { \
  49. *(NSTATEP) = *(OSTATEP); \
  50. } while (0)
  51. /*
  52. * General memory allocation interfaces
  53. */
  54. #define KM_SLEEP 0x0001u
  55. #define KM_NOSLEEP 0x0002u
  56. #define KM_NOFS 0x0004u
  57. #define KM_MAYFAIL 0x0008u
  58. /*
  59. * We use a special process flag to avoid recursive callbacks into
  60. * the filesystem during transactions. We will also issue our own
  61. * warnings, so we explicitly skip any generic ones (silly of us).
  62. */
  63. static inline gfp_t
  64. kmem_flags_convert(unsigned int __nocast flags)
  65. {
  66. gfp_t lflags;
  67. BUG_ON(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL));
  68. if (flags & KM_NOSLEEP) {
  69. lflags = GFP_ATOMIC | __GFP_NOWARN;
  70. } else {
  71. lflags = GFP_KERNEL | __GFP_NOWARN;
  72. if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS))
  73. lflags &= ~__GFP_FS;
  74. }
  75. return lflags;
  76. }
  77. extern void *kmem_alloc(size_t, unsigned int __nocast);
  78. extern void *kmem_realloc(void *, size_t, size_t, unsigned int __nocast);
  79. extern void *kmem_zalloc(size_t, unsigned int __nocast);
  80. extern void kmem_free(void *, size_t);
  81. /*
  82. * Zone interfaces
  83. */
  84. #define KM_ZONE_HWALIGN SLAB_HWCACHE_ALIGN
  85. #define KM_ZONE_RECLAIM SLAB_RECLAIM_ACCOUNT
  86. #define KM_ZONE_SPREAD SLAB_MEM_SPREAD
  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, NULL);
  93. }
  94. static inline kmem_zone_t *
  95. kmem_zone_init_flags(int size, char *zone_name, unsigned long flags,
  96. void (*construct)(void *, kmem_zone_t *, unsigned long))
  97. {
  98. return kmem_cache_create(zone_name, size, 0, flags, construct, NULL);
  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. if (zone && kmem_cache_destroy(zone))
  109. BUG();
  110. }
  111. extern void *kmem_zone_alloc(kmem_zone_t *, unsigned int __nocast);
  112. extern void *kmem_zone_zalloc(kmem_zone_t *, unsigned int __nocast);
  113. /*
  114. * Low memory cache shrinkers
  115. */
  116. typedef struct shrinker *kmem_shaker_t;
  117. typedef int (*kmem_shake_func_t)(int, gfp_t);
  118. static inline kmem_shaker_t
  119. kmem_shake_register(kmem_shake_func_t sfunc)
  120. {
  121. return set_shrinker(DEFAULT_SEEKS, sfunc);
  122. }
  123. static inline void
  124. kmem_shake_deregister(kmem_shaker_t shrinker)
  125. {
  126. remove_shrinker(shrinker);
  127. }
  128. static inline int
  129. kmem_shake_allow(gfp_t gfp_mask)
  130. {
  131. return (gfp_mask & __GFP_WAIT);
  132. }
  133. #endif /* __XFS_SUPPORT_KMEM_H__ */