2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32 #ifndef __XFS_SUPPORT_KMEM_H__
33 #define __XFS_SUPPORT_KMEM_H__
36 #include <linux/highmem.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
41 * Cutoff point to use vmalloc instead of kmalloc.
43 #define MAX_SLAB_SIZE 0x10000
46 * XFS uses slightly different names for these due to the
49 #define kmem_zone kmem_cache_s
50 #define kmem_zone_t kmem_cache_t
52 #define KM_SLEEP 0x0001
53 #define KM_NOSLEEP 0x0002
54 #define KM_NOFS 0x0004
56 typedef unsigned long xfs_pflags_t;
58 #define PFLAGS_TEST_FSTRANS() (current->flags & PF_FSTRANS)
60 /* these could be nested, so we save state */
61 #define PFLAGS_SET_FSTRANS(STATEP) do { \
62 *(STATEP) = current->flags; \
63 current->flags |= PF_FSTRANS; \
66 #define PFLAGS_CLEAR_FSTRANS(STATEP) do { \
67 *(STATEP) = current->flags; \
68 current->flags &= ~PF_FSTRANS; \
71 /* Restore the PF_FSTRANS state to what was saved in STATEP */
72 #define PFLAGS_RESTORE_FSTRANS(STATEP) do { \
73 current->flags = ((current->flags & ~PF_FSTRANS) | \
74 (*(STATEP) & PF_FSTRANS)); \
77 #define PFLAGS_DUP(OSTATEP, NSTATEP) do { \
78 *(NSTATEP) = *(OSTATEP); \
82 * XXX get rid of the unconditional __GFP_NOFAIL by adding
83 * a KM_FAIL flag and using it where we're allowed to fail.
85 static __inline unsigned int
86 kmem_flags_convert(int flags)
91 if (unlikely(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS))) {
93 "XFS: memory allocation with wrong flags (%x)\n", flags);
98 lflags = (flags & KM_NOSLEEP) ? GFP_ATOMIC : (GFP_KERNEL|__GFP_NOFAIL);
100 /* avoid recusive callbacks to filesystem during transactions */
101 if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS))
107 static __inline void *
108 kmem_alloc(size_t size, int flags)
110 if (unlikely(MAX_SLAB_SIZE < size))
111 /* Avoid doing filesystem sensitive stuff to get this */
112 return __vmalloc(size, kmem_flags_convert(flags), PAGE_KERNEL);
113 return kmalloc(size, kmem_flags_convert(flags));
116 static __inline void *
117 kmem_zalloc(size_t size, int flags)
119 void *ptr = kmem_alloc(size, flags);
120 if (likely(ptr != NULL))
121 memset(ptr, 0, size);
126 kmem_free(void *ptr, size_t size)
128 if (unlikely((unsigned long)ptr < VMALLOC_START ||
129 (unsigned long)ptr >= VMALLOC_END))
135 static __inline void *
136 kmem_realloc(void *ptr, size_t newsize, size_t oldsize, int flags)
138 void *new = kmem_alloc(newsize, flags);
140 if (likely(ptr != NULL)) {
141 if (likely(new != NULL))
142 memcpy(new, ptr, min(oldsize, newsize));
143 kmem_free(ptr, oldsize);
149 static __inline kmem_zone_t *
150 kmem_zone_init(int size, char *zone_name)
152 return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
155 static __inline void *
156 kmem_zone_alloc(kmem_zone_t *zone, int flags)
158 return kmem_cache_alloc(zone, kmem_flags_convert(flags));
161 static __inline void *
162 kmem_zone_zalloc(kmem_zone_t *zone, int flags)
164 void *ptr = kmem_zone_alloc(zone, flags);
165 if (likely(ptr != NULL))
166 memset(ptr, 0, kmem_cache_size(zone));
171 kmem_zone_free(kmem_zone_t *zone, void *ptr)
173 kmem_cache_free(zone, ptr);
176 typedef struct shrinker *kmem_shaker_t;
177 typedef int (*kmem_shake_func_t)(int, unsigned int);
179 static __inline kmem_shaker_t
180 kmem_shake_register(kmem_shake_func_t sfunc)
182 return set_shrinker(DEFAULT_SEEKS, sfunc);
186 kmem_shake_deregister(kmem_shaker_t shrinker)
188 remove_shrinker(shrinker);
192 kmem_shake_allow(unsigned int gfp_mask)
194 return (gfp_mask & __GFP_WAIT);
197 #endif /* __XFS_SUPPORT_KMEM_H__ */