Whamcloud - gitweb
LU-3963 libcfs: cleanup list operations
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / winnt-mem.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/winnt/winnt-mem.h
37  *
38  * Basic library routines of memory manipulation routines.
39  */
40
41 #ifndef __LIBCFS_WINNT_CFS_MEM_H__
42 #define __LIBCFS_WINNT_CFS_MEM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 #include <libcfs/winnt/portals_utils.h>
49
50 #ifdef __KERNEL__
51
52 /*
53  * page definitions
54  */
55
56 #define PAGE_CACHE_SIZE                   PAGE_SIZE
57 #define PAGE_CACHE_SHIFT                  PAGE_SHIFT
58 #define CFS_PAGE_MASK                   (~(PAGE_SIZE - 1))
59
60 #define memory_pressure_get() (0)
61 #define memory_pressure_set() do {} while (0)
62 #define memory_pressure_clr() do {} while (0)
63
64 struct page {
65     void *          addr;
66     atomic_t    count;
67     void *          private;
68     void *          mapping;
69     __u32           index;
70     __u32           flags;
71 };
72
73 #define page cfs_page
74
75 #ifndef page_private
76 #define page_private(page) ((page)->private)
77 #define set_page_private(page, v) ((page)->private = (v))
78 #endif
79
80 #define page_count(page) (0)
81
82 #define PG_locked                0      /* Page is locked. Don't touch. */
83 #define PG_error                 1
84 #define PG_referenced            2
85 #define PG_uptodate              3
86
87 #define PG_dirty                 4
88 #define PG_lru                   5
89 #define PG_active                6
90 #define PG_slab                  7      /* slab debug (Suparna wants this) */
91
92 #define PG_owner_priv_1          8      /* Owner use. If pagecache, fs may use*/
93 #define PG_arch_1                9
94 #define PG_reserved             10
95 #define PG_private              11      /* If pagecache, has fs-private data */
96
97 #define PG_writeback            12      /* Page is under writeback */
98 #define PG_compound             14      /* Part of a compound page */
99 #define PG_swapcache            15      /* Swap page: swp_entry_t in private */
100
101 #define PG_mappedtodisk         16      /* Has blocks allocated on-disk */
102 #define PG_reclaim              17      /* To be reclaimed asap */
103 #define PG_buddy                19      /* Page is free, on buddy lists */
104
105 #define PG_virt         31  /* addr is not */
106
107 #ifndef arch_set_page_uptodate
108 #define arch_set_page_uptodate(page)
109 #endif
110
111 /* Make it prettier to test the above... */
112 #define UnlockPage(page)        unlock_page(page)
113 #define Page_Uptodate(page)     test_bit(PG_uptodate, &(page)->flags)
114 #define SetPageUptodate(page)                                           \
115         do {                                                            \
116                 arch_set_page_uptodate(page);                           \
117                 set_bit(PG_uptodate, &(page)->flags);                   \
118         } while (0)
119 #define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)
120 #define PageDirty(page) test_bit(PG_dirty, &(page)->flags)
121 #define SetPageDirty(page)      set_bit(PG_dirty, &(page)->flags)
122 #define ClearPageDirty(page)    clear_bit(PG_dirty, &(page)->flags)
123 #define PageLocked(page)        test_bit(PG_locked, &(page)->flags)
124 #define LockPage(page)          set_bit(PG_locked, &(page)->flags)
125 #define TryLockPage(page)       test_and_set_bit(PG_locked, &(page)->flags)
126 #define PageChecked(page)       test_bit(PG_checked, &(page)->flags)
127 #define SetPageChecked(page)    set_bit(PG_checked, &(page)->flags)
128 #define ClearPageChecked(page)  clear_bit(PG_checked, &(page)->flags)
129 #define PageLaunder(page)       test_bit(PG_launder, &(page)->flags)
130 #define SetPageLaunder(page)    set_bit(PG_launder, &(page)->flags)
131 #define ClearPageLaunder(page)  clear_bit(PG_launder, &(page)->flags)
132 #define ClearPageArch1(page)    clear_bit(PG_arch_1, &(page)->flags)
133
134 #define PageError(page) test_bit(PG_error, &(page)->flags)
135 #define SetPageError(page)      set_bit(PG_error, &(page)->flags)
136 #define ClearPageError(page)    clear_bit(PG_error, &(page)->flags)
137 #define PageReferenced(page)    test_bit(PG_referenced, &(page)->flags)
138 #define SetPageReferenced(page) set_bit(PG_referenced, &(page)->flags)
139 #define ClearPageReferenced(page) clear_bit(PG_referenced, &(page)->flags)
140
141 #define PageActive(page)        test_bit(PG_active, &(page)->flags)
142 #define SetPageActive(page)     set_bit(PG_active, &(page)->flags)
143 #define ClearPageActive(page)   clear_bit(PG_active, &(page)->flags)
144
145 #define PageWriteback(page)     test_bit(PG_writeback, &(page)->flags)
146 #define TestSetPageWriteback(page) test_and_set_bit(PG_writeback,       \
147                                                         &(page)->flags)
148 #define TestClearPageWriteback(page) test_and_clear_bit(PG_writeback,   \
149                                                         &(page)->flags)
150
151 /*
152  * Universal memory allocator API
153  */
154 enum cfs_alloc_flags {
155         /* allocation is not allowed to block */
156         GFP_ATOMIC = 0x1,
157         /* allocation is allowed to block */
158         __GFP_WAIT   = 0x2,
159         /* allocation should return zeroed memory */
160         __GFP_ZERO   = 0x4,
161         /* allocation is allowed to call file-system code to free/clean
162          * memory */
163         __GFP_FS     = 0x8,
164         /* allocation is allowed to do io to free/clean memory */
165         __GFP_IO     = 0x10,
166         /* don't report allocation failure to the console */
167         __GFP_NOWARN = 0x20,
168         /* standard allocator flag combination */
169         GFP_IOFS    = __GFP_FS | __GFP_IO,
170         GFP_USER   = __GFP_WAIT | __GFP_FS | __GFP_IO,
171         GFP_NOFS   = __GFP_WAIT | __GFP_IO,
172         GFP_KERNEL = __GFP_WAIT | __GFP_IO | __GFP_FS,
173 };
174
175 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
176 enum cfs_alloc_page_flags {
177         /* allow to return page beyond KVM. It has to be mapped into KVM by
178          * kmap() and unmapped with kunmap(). */
179         __GFP_HIGHMEM  = 0x40,
180         GFP_HIGHUSER = __GFP_WAIT | __GFP_FS | __GFP_IO |
181                              __GFP_HIGHMEM,
182 };
183
184 struct page *alloc_page(int flags);
185 void __free_page(struct page *pg);
186 void cfs_release_page(struct page *pg);
187 struct page *virt_to_page(void *addr);
188
189 #define page_cache_get(a) do {} while (0)
190 #define page_cache_release(a) do {} while (0)
191
192 static inline void *page_address(struct page *page)
193 {
194     return page->addr;
195 }
196
197 static inline void *kmap(struct page *page)
198 {
199     return page->addr;
200 }
201
202 static inline void kunmap(struct page *page)
203 {
204     return;
205 }
206
207 static inline void get_page(struct page *page)
208 {
209     atomic_inc(&page->count);
210 }
211
212 static inline void cfs_put_page(struct page *page)
213 {
214     atomic_dec(&page->count);
215 }
216
217 static inline int page_count(struct page *page)
218 {
219     return atomic_read(&page->count);
220 }
221
222 #define page_index(p)       ((p)->index)
223
224 /*
225  * Memory allocator
226  */
227
228 #define ALLOC_ATOMIC_TRY        (0)
229 extern void *kmalloc(size_t nr_bytes, u_int32_t flags);
230 extern void  kfree(void *addr);
231 extern void *vmalloc(size_t nr_bytes);
232 extern void  vfree(void *addr);
233
234 /*
235  * SLAB allocator
236  */
237
238 #define SLAB_HWCACHE_ALIGN              0
239
240 /* The cache name is limited to 20 chars */
241
242 struct kmem_cache {
243     char                    name[20];
244     ulong_ptr_t             flags;
245     NPAGED_LOOKASIDE_LIST   npll;
246 };
247
248
249 extern struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
250                                             unsigned long, void *);
251 extern kmem_cache_destroy(struct kmem_cache *);
252 extern void *kmem_cache_alloc(struct kmem_cache *, int);
253 extern void kmem_cache_free(struct kmem_cache *, void *);
254
255 /*
256  * shrinker
257  */
258 typedef int (*shrink_callback)(int nr_to_scan, gfp_t gfp_mask);
259 struct shrinker {
260         shrink_callback cb;
261         int seeks;      /* seeks to recreate an obj */
262
263         /* These are for internal use */
264         struct list_head list;
265         long nr;        /* objs pending delete */
266 };
267
268 struct shrinker *set_shrinker(int seeks, shrink_callback cb);
269 void remove_shrinker(struct shrinker *s);
270
271 int start_shrinker_timer();
272 void stop_shrinker_timer();
273
274 /*
275  * Page allocator slabs
276  */
277
278 extern struct kmem_cache *cfs_page_t_slab;
279 extern struct kmem_cache *cfs_page_p_slab;
280
281
282 #define DECL_MMSPACE
283 #define MMSPACE_OPEN    do {} while (0)
284 #define MMSPACE_CLOSE   do {} while (0)
285
286
287 #define smp_mb()     do {} while(0)
288 #define rmb()        smp_mb()
289 #define wmb()        smp_mb()
290
291 /*
292  * MM defintions from (linux/mm.h)
293  */
294
295 #define DEFAULT_SEEKS 2 /* shrink seek */
296
297 #else  /* !__KERNEL__ */
298
299 #include "../user-mem.h"
300
301 /* page alignmed buffer allocation */
302 void* pgalloc(size_t factor);
303 void  pgfree(void * page);
304
305 #endif /* __KERNEL__ */
306
307 #endif /* __WINNT_CFS_MEM_H__ */