]> git.proxmox.com Git - mirror_spl-debian.git/blob - include/spl-kmem.h
Minor nit, SOLARIS should be SPL
[mirror_spl-debian.git] / include / spl-kmem.h
1 #ifndef _SPL_KMEM_H
2 #define _SPL_KMEM_H
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #undef DEBUG_KMEM
9 #undef DEBUG_KMEM_UNIMPLEMENTED
10
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/mm.h>
14 #include <linux/spinlock.h>
15 /*
16 * Memory allocation interfaces
17 */
18 #define KM_SLEEP GFP_KERNEL
19 #define KM_NOSLEEP GFP_ATOMIC
20 #undef KM_PANIC /* No linux analog */
21 #define KM_PUSHPAGE (GFP_KERNEL | GFP_HIGH)
22 #define KM_VMFLAGS GFP_LEVEL_MASK
23 #define KM_FLAGS __GFP_BITS_MASK
24
25 #ifdef DEBUG_KMEM
26 /* Shim layer memory accounting */
27 extern atomic_t kmem_alloc_used;
28 extern unsigned int kmem_alloc_max;
29 #endif
30
31 #ifdef DEBUG_KMEM
32 #define __kmem_alloc(size, flags, allocator) \
33 ({ void *_ptr_; \
34 \
35 /* Marked unlikely because we should never be doing this */ \
36 if (unlikely((size) > (PAGE_SIZE * 2))) \
37 printk("Warning: kmem_alloc(%d, 0x%x) large alloc at %s:%d " \
38 "(%d/%d)\n", (int)(size), (int)(flags), \
39 __FILE__, __LINE__, \
40 atomic_read(&kmem_alloc_used), kmem_alloc_max); \
41 \
42 _ptr_ = (void *)allocator((size), (flags)); \
43 if (_ptr_ == NULL) { \
44 printk("Warning: kmem_alloc(%d, 0x%x) failed at %s:%d " \
45 "(%d/%d)\n", (int)(size), (int)(flags), \
46 __FILE__, __LINE__, \
47 atomic_read(&kmem_alloc_used), kmem_alloc_max); \
48 atomic_add((size), &kmem_alloc_used); \
49 if (unlikely(atomic_read(&kmem_alloc_used) > kmem_alloc_max)) \
50 kmem_alloc_max = atomic_read(&kmem_alloc_used); \
51 } \
52 \
53 _ptr_; \
54 })
55
56 #define kmem_alloc(size, flags) __kmem_alloc(size, flags, kmalloc)
57 #define kmem_zalloc(size, flags) __kmem_alloc(size, flags, kzalloc)
58
59 #define kmem_free(ptr, size) \
60 ({ \
61 BUG_ON(!ptr); \
62 atomic_sub((size), &kmem_alloc_used); \
63 memset(ptr, 0x5a, (size)); /* Poison */ \
64 kfree(ptr); \
65 (ptr) = (void *)0xdeadbeef; \
66 })
67
68
69 #else
70
71 #define kmem_alloc(size, flags) kmalloc(size, flags)
72 #define kmem_zalloc(size, flags) kzalloc(size, flags)
73 #define kmem_free(ptr, size) kfree(ptr)
74
75 #endif /* DEBUG_KMEM */
76
77
78 #ifdef DEBUG_KMEM_UNIMPLEMENTED
79 static __inline__ void *
80 kmem_alloc_tryhard(size_t size, size_t *alloc_size, int kmflags)
81 {
82 #error "kmem_alloc_tryhard() not implemented"
83 }
84 #endif /* DEBUG_KMEM_UNIMPLEMENTED */
85
86 /*
87 * Slab allocation interfaces
88 */
89 #undef KMC_NOTOUCH /* No linux analog */
90 #define KMC_NODEBUG 0x00000000 /* Default beahvior */
91 #define KMC_NOMAGAZINE /* No linux analog */
92 #define KMC_NOHASH /* No linux analog */
93 #define KMC_QCACHE /* No linux analog */
94
95 #define KMC_REAP_CHUNK 256
96 #define KMC_DEFAULT_SEEKS DEFAULT_SEEKS
97
98 /* Defined by linux slab.h
99 * typedef struct kmem_cache_s kmem_cache_t;
100 */
101
102 /* No linux analog
103 * extern int kmem_ready;
104 * extern pgcnt_t kmem_reapahead;
105 */
106
107 #ifdef DEBUG_KMEM_UNIMPLEMENTED
108 static __inline__ void kmem_init(void) {
109 #error "kmem_init() not implemented"
110 }
111
112 static __inline__ void kmem_thread_init(void) {
113 #error "kmem_thread_init() not implemented"
114 }
115
116 static __inline__ void kmem_mp_init(void) {
117 #error "kmem_mp_init() not implemented"
118 }
119
120 static __inline__ void kmem_reap_idspace(void) {
121 #error "kmem_reap_idspace() not implemented"
122 }
123
124 static __inline__ size_t kmem_avail(void) {
125 #error "kmem_avail() not implemented"
126 }
127
128 static __inline__ size_t kmem_maxavail(void) {
129 #error "kmem_maxavail() not implemented"
130 }
131
132 static __inline__ uint64_t kmem_cache_stat(kmem_cache_t *cache) {
133 #error "kmem_cache_stat() not implemented"
134 }
135 #endif /* DEBUG_KMEM_UNIMPLEMENTED */
136
137 /* XXX - Used by arc.c to adjust its memory footprint. We may want
138 * to use this hook in the future to adjust behavior based on
139 * debug levels. For now it's safe to always return 0.
140 */
141 static __inline__ int
142 kmem_debugging(void)
143 {
144 return 0;
145 }
146
147 typedef int (*kmem_constructor_t)(void *, void *, int);
148 typedef void (*kmem_destructor_t)(void *, void *);
149 typedef void (*kmem_reclaim_t)(void *);
150
151 extern kmem_cache_t *
152 __kmem_cache_create(char *name, size_t size, size_t align,
153 kmem_constructor_t constructor,
154 kmem_destructor_t destructor,
155 kmem_reclaim_t reclaim,
156 void *priv, void *vmp, int flags);
157
158 void
159 extern __kmem_cache_destroy(kmem_cache_t *cache);
160
161 void
162 extern __kmem_reap(void);
163
164 #define kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags) \
165 __kmem_cache_create(name,size,align,ctor,dtor,rclm,priv,vmp,flags)
166 #define kmem_cache_destroy(cache) __kmem_cache_destroy(cache)
167 #define kmem_cache_alloc(cache, flags) kmem_cache_alloc(cache, flags)
168 #define kmem_cache_free(cache, ptr) kmem_cache_free(cache, ptr)
169 #define kmem_cache_reap_now(cache) kmem_cache_shrink(cache)
170 #define kmem_reap() __kmem_reap()
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif /* _SPL_KMEM_H */