kmem.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #ifndef __XFS_SUPPORT_KMEM_H__
  33. #define __XFS_SUPPORT_KMEM_H__
  34. #include <linux/slab.h>
  35. #include <linux/sched.h>
  36. #include <linux/mm.h>
  37. /*
  38. * memory management routines
  39. */
  40. #define KM_SLEEP 0x0001
  41. #define KM_NOSLEEP 0x0002
  42. #define KM_NOFS 0x0004
  43. #define KM_MAYFAIL 0x0008
  44. #define kmem_zone kmem_cache_s
  45. #define kmem_zone_t kmem_cache_t
  46. typedef unsigned long xfs_pflags_t;
  47. #define PFLAGS_TEST_NOIO() (current->flags & PF_NOIO)
  48. #define PFLAGS_TEST_FSTRANS() (current->flags & PF_FSTRANS)
  49. #define PFLAGS_SET_NOIO() do { \
  50. current->flags |= PF_NOIO; \
  51. } while (0)
  52. #define PFLAGS_CLEAR_NOIO() do { \
  53. current->flags &= ~PF_NOIO; \
  54. } while (0)
  55. /* these could be nested, so we save state */
  56. #define PFLAGS_SET_FSTRANS(STATEP) do { \
  57. *(STATEP) = current->flags; \
  58. current->flags |= PF_FSTRANS; \
  59. } while (0)
  60. #define PFLAGS_CLEAR_FSTRANS(STATEP) do { \
  61. *(STATEP) = current->flags; \
  62. current->flags &= ~PF_FSTRANS; \
  63. } while (0)
  64. /* Restore the PF_FSTRANS state to what was saved in STATEP */
  65. #define PFLAGS_RESTORE_FSTRANS(STATEP) do { \
  66. current->flags = ((current->flags & ~PF_FSTRANS) | \
  67. (*(STATEP) & PF_FSTRANS)); \
  68. } while (0)
  69. #define PFLAGS_DUP(OSTATEP, NSTATEP) do { \
  70. *(NSTATEP) = *(OSTATEP); \
  71. } while (0)
  72. static __inline unsigned int kmem_flags_convert(int flags)
  73. {
  74. int lflags = __GFP_NOWARN; /* we'll report problems, if need be */
  75. #ifdef DEBUG
  76. if (unlikely(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL))) {
  77. printk(KERN_WARNING
  78. "XFS: memory allocation with wrong flags (%x)\n", flags);
  79. BUG();
  80. }
  81. #endif
  82. if (flags & KM_NOSLEEP) {
  83. lflags |= GFP_ATOMIC;
  84. } else {
  85. lflags |= GFP_KERNEL;
  86. /* avoid recusive callbacks to filesystem during transactions */
  87. if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS))
  88. lflags &= ~__GFP_FS;
  89. }
  90. return lflags;
  91. }
  92. static __inline kmem_zone_t *
  93. kmem_zone_init(int size, char *zone_name)
  94. {
  95. return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
  96. }
  97. static __inline void
  98. kmem_zone_free(kmem_zone_t *zone, void *ptr)
  99. {
  100. kmem_cache_free(zone, ptr);
  101. }
  102. static __inline void
  103. kmem_zone_destroy(kmem_zone_t *zone)
  104. {
  105. if (zone && kmem_cache_destroy(zone))
  106. BUG();
  107. }
  108. extern void *kmem_zone_zalloc(kmem_zone_t *, int);
  109. extern void *kmem_zone_alloc(kmem_zone_t *, int);
  110. extern void *kmem_alloc(size_t, int);
  111. extern void *kmem_realloc(void *, size_t, size_t, int);
  112. extern void *kmem_zalloc(size_t, int);
  113. extern void kmem_free(void *, size_t);
  114. typedef struct shrinker *kmem_shaker_t;
  115. typedef int (*kmem_shake_func_t)(int, unsigned int);
  116. static __inline kmem_shaker_t
  117. kmem_shake_register(kmem_shake_func_t sfunc)
  118. {
  119. return set_shrinker(DEFAULT_SEEKS, sfunc);
  120. }
  121. static __inline void
  122. kmem_shake_deregister(kmem_shaker_t shrinker)
  123. {
  124. remove_shrinker(shrinker);
  125. }
  126. static __inline int
  127. kmem_shake_allow(unsigned int gfp_mask)
  128. {
  129. return (gfp_mask & __GFP_WAIT);
  130. }
  131. #endif /* __XFS_SUPPORT_KMEM_H__ */