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