kmem.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * memory management routines
  25. */
  26. #define KM_SLEEP 0x0001u
  27. #define KM_NOSLEEP 0x0002u
  28. #define KM_NOFS 0x0004u
  29. #define KM_MAYFAIL 0x0008u
  30. #define kmem_zone kmem_cache
  31. #define kmem_zone_t struct kmem_cache
  32. typedef unsigned long xfs_pflags_t;
  33. #define PFLAGS_TEST_NOIO() (current->flags & PF_NOIO)
  34. #define PFLAGS_TEST_FSTRANS() (current->flags & PF_FSTRANS)
  35. #define PFLAGS_SET_NOIO() do { \
  36. current->flags |= PF_NOIO; \
  37. } while (0)
  38. #define PFLAGS_CLEAR_NOIO() do { \
  39. current->flags &= ~PF_NOIO; \
  40. } while (0)
  41. /* these could be nested, so we save state */
  42. #define PFLAGS_SET_FSTRANS(STATEP) do { \
  43. *(STATEP) = current->flags; \
  44. current->flags |= PF_FSTRANS; \
  45. } while (0)
  46. #define PFLAGS_CLEAR_FSTRANS(STATEP) do { \
  47. *(STATEP) = current->flags; \
  48. current->flags &= ~PF_FSTRANS; \
  49. } while (0)
  50. /* Restore the PF_FSTRANS state to what was saved in STATEP */
  51. #define PFLAGS_RESTORE_FSTRANS(STATEP) do { \
  52. current->flags = ((current->flags & ~PF_FSTRANS) | \
  53. (*(STATEP) & PF_FSTRANS)); \
  54. } while (0)
  55. #define PFLAGS_DUP(OSTATEP, NSTATEP) do { \
  56. *(NSTATEP) = *(OSTATEP); \
  57. } while (0)
  58. static __inline gfp_t kmem_flags_convert(unsigned int __nocast flags)
  59. {
  60. gfp_t lflags = __GFP_NOWARN; /* we'll report problems, if need be */
  61. #ifdef DEBUG
  62. if (unlikely(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL))) {
  63. printk(KERN_WARNING
  64. "XFS: memory allocation with wrong flags (%x)\n", flags);
  65. BUG();
  66. }
  67. #endif
  68. if (flags & KM_NOSLEEP) {
  69. lflags |= GFP_ATOMIC;
  70. } else {
  71. lflags |= GFP_KERNEL;
  72. /* avoid recusive callbacks to filesystem during transactions */
  73. if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS))
  74. lflags &= ~__GFP_FS;
  75. }
  76. return lflags;
  77. }
  78. static __inline kmem_zone_t *
  79. kmem_zone_init(int size, char *zone_name)
  80. {
  81. return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
  82. }
  83. static __inline void
  84. kmem_zone_free(kmem_zone_t *zone, void *ptr)
  85. {
  86. kmem_cache_free(zone, ptr);
  87. }
  88. static __inline void
  89. kmem_zone_destroy(kmem_zone_t *zone)
  90. {
  91. if (zone && kmem_cache_destroy(zone))
  92. BUG();
  93. }
  94. extern void *kmem_zone_zalloc(kmem_zone_t *, unsigned int __nocast);
  95. extern void *kmem_zone_alloc(kmem_zone_t *, unsigned int __nocast);
  96. extern void *kmem_alloc(size_t, unsigned int __nocast);
  97. extern void *kmem_realloc(void *, size_t, size_t, unsigned int __nocast);
  98. extern void *kmem_zalloc(size_t, unsigned int __nocast);
  99. extern void kmem_free(void *, size_t);
  100. typedef struct shrinker *kmem_shaker_t;
  101. typedef int (*kmem_shake_func_t)(int, gfp_t);
  102. static __inline kmem_shaker_t
  103. kmem_shake_register(kmem_shake_func_t sfunc)
  104. {
  105. return set_shrinker(DEFAULT_SEEKS, sfunc);
  106. }
  107. static __inline void
  108. kmem_shake_deregister(kmem_shaker_t shrinker)
  109. {
  110. remove_shrinker(shrinker);
  111. }
  112. static __inline int
  113. kmem_shake_allow(gfp_t gfp_mask)
  114. {
  115. return (gfp_mask & __GFP_WAIT);
  116. }
  117. #endif /* __XFS_SUPPORT_KMEM_H__ */