kmem.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * General memory allocation interfaces
  25. */
  26. #define KM_SLEEP 0x0001u
  27. #define KM_NOSLEEP 0x0002u
  28. #define KM_NOFS 0x0004u
  29. #define KM_MAYFAIL 0x0008u
  30. #define KM_LARGE 0x0010u
  31. /*
  32. * We use a special process flag to avoid recursive callbacks into
  33. * the filesystem during transactions. We will also issue our own
  34. * warnings, so we explicitly skip any generic ones (silly of us).
  35. */
  36. static inline gfp_t
  37. kmem_flags_convert(unsigned int __nocast flags)
  38. {
  39. gfp_t lflags;
  40. BUG_ON(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL|KM_LARGE));
  41. if (flags & KM_NOSLEEP) {
  42. lflags = GFP_ATOMIC | __GFP_NOWARN;
  43. } else {
  44. lflags = GFP_KERNEL | __GFP_NOWARN;
  45. if ((current->flags & PF_FSTRANS) || (flags & KM_NOFS))
  46. lflags &= ~__GFP_FS;
  47. }
  48. return lflags;
  49. }
  50. extern void *kmem_alloc(size_t, unsigned int __nocast);
  51. extern void *kmem_zalloc(size_t, unsigned int __nocast);
  52. extern void *kmem_zalloc_greedy(size_t *, size_t, size_t, unsigned int __nocast);
  53. extern void *kmem_realloc(void *, size_t, size_t, unsigned int __nocast);
  54. extern void kmem_free(void *, size_t);
  55. /*
  56. * Zone interfaces
  57. */
  58. #define KM_ZONE_HWALIGN SLAB_HWCACHE_ALIGN
  59. #define KM_ZONE_RECLAIM SLAB_RECLAIM_ACCOUNT
  60. #define KM_ZONE_SPREAD SLAB_MEM_SPREAD
  61. #define kmem_zone kmem_cache
  62. #define kmem_zone_t struct kmem_cache
  63. static inline kmem_zone_t *
  64. kmem_zone_init(int size, char *zone_name)
  65. {
  66. return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
  67. }
  68. static inline kmem_zone_t *
  69. kmem_zone_init_flags(int size, char *zone_name, unsigned long flags,
  70. void (*construct)(void *, kmem_zone_t *, unsigned long))
  71. {
  72. return kmem_cache_create(zone_name, size, 0, flags, construct, NULL);
  73. }
  74. static inline void
  75. kmem_zone_free(kmem_zone_t *zone, void *ptr)
  76. {
  77. kmem_cache_free(zone, ptr);
  78. }
  79. static inline void
  80. kmem_zone_destroy(kmem_zone_t *zone)
  81. {
  82. if (zone)
  83. kmem_cache_destroy(zone);
  84. }
  85. extern void *kmem_zone_alloc(kmem_zone_t *, unsigned int __nocast);
  86. extern void *kmem_zone_zalloc(kmem_zone_t *, unsigned int __nocast);
  87. /*
  88. * Low memory cache shrinkers
  89. */
  90. typedef struct shrinker *kmem_shaker_t;
  91. typedef int (*kmem_shake_func_t)(int, gfp_t);
  92. static inline kmem_shaker_t
  93. kmem_shake_register(kmem_shake_func_t sfunc)
  94. {
  95. return set_shrinker(DEFAULT_SEEKS, sfunc);
  96. }
  97. static inline void
  98. kmem_shake_deregister(kmem_shaker_t shrinker)
  99. {
  100. remove_shrinker(shrinker);
  101. }
  102. static inline int
  103. kmem_shake_allow(gfp_t gfp_mask)
  104. {
  105. return (gfp_mask & __GFP_WAIT);
  106. }
  107. #endif /* __XFS_SUPPORT_KMEM_H__ */