Whamcloud - gitweb
211ffef9943fb0c621a17dc9e7d42b907dbe07fa
[fs/lustre-release.git] / libcfs / include / libcfs / winnt / winnt-mem.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 typedef struct cfs_mem_cache cfs_mem_cache_t;
53
54 /*
55  * page definitions
56  */
57
58 #define CFS_PAGE_SIZE                   PAGE_SIZE
59 #define CFS_PAGE_SHIFT                  PAGE_SHIFT
60 #define CFS_PAGE_MASK                   (~(PAGE_SIZE - 1))
61
62 typedef struct cfs_page {
63     void *      addr;
64     atomic_t    count;
65     void *      private;
66     void *      mapping;
67     __u32       index;
68     __u32       flags;
69 } cfs_page_t;
70
71 #define page cfs_page
72
73 #ifndef page_private
74 #define page_private(page) ((page)->private)
75 #define set_page_private(page, v) ((page)->private = (v))
76 #endif
77
78 #define page_count(page) (0)
79
80 #define PG_locked                0      /* Page is locked. Don't touch. */
81 #define PG_error                 1
82 #define PG_referenced            2
83 #define PG_uptodate              3
84
85 #define PG_dirty                 4
86 #define PG_lru                   5
87 #define PG_active                6
88 #define PG_slab                  7      /* slab debug (Suparna wants this) */
89
90 #define PG_owner_priv_1          8      /* Owner use. If pagecache, fs may use*/
91 #define PG_arch_1                9
92 #define PG_reserved             10
93 #define PG_private              11      /* If pagecache, has fs-private data */
94
95 #define PG_writeback            12      /* Page is under writeback */
96 #define PG_compound             14      /* Part of a compound page */
97 #define PG_swapcache            15      /* Swap page: swp_entry_t in private */
98
99 #define PG_mappedtodisk         16      /* Has blocks allocated on-disk */
100 #define PG_reclaim              17      /* To be reclaimed asap */
101 #define PG_buddy                19      /* Page is free, on buddy lists */
102
103 #define PG_virt         31  /* addr is not */
104
105 #ifndef arch_set_page_uptodate
106 #define arch_set_page_uptodate(page)
107 #endif
108
109 /* Make it prettier to test the above... */
110 #define UnlockPage(page)        unlock_page(page)
111 #define Page_Uptodate(page)     test_bit(PG_uptodate, &(page)->flags)
112 #define SetPageUptodate(page) \
113         do {                                                            \
114                 arch_set_page_uptodate(page);                           \
115                 set_bit(PG_uptodate, &(page)->flags);                   \
116         } while (0)
117 #define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)
118 #define PageDirty(page)         test_bit(PG_dirty, &(page)->flags)
119 #define SetPageDirty(page)      set_bit(PG_dirty, &(page)->flags)
120 #define ClearPageDirty(page)    clear_bit(PG_dirty, &(page)->flags)
121 #define PageLocked(page)        test_bit(PG_locked, &(page)->flags)
122 #define LockPage(page)          set_bit(PG_locked, &(page)->flags)
123 #define TryLockPage(page)       test_and_set_bit(PG_locked, &(page)->flags)
124 #define PageChecked(page)       test_bit(PG_checked, &(page)->flags)
125 #define SetPageChecked(page)    set_bit(PG_checked, &(page)->flags)
126 #define ClearPageChecked(page)  clear_bit(PG_checked, &(page)->flags)
127 #define PageLaunder(page)       test_bit(PG_launder, &(page)->flags)
128 #define SetPageLaunder(page)    set_bit(PG_launder, &(page)->flags)
129 #define ClearPageLaunder(page)  clear_bit(PG_launder, &(page)->flags)
130 #define ClearPageArch1(page)    clear_bit(PG_arch_1, &(page)->flags)
131
132 #define PageError(page)         test_bit(PG_error, &(page)->flags)
133 #define SetPageError(page)      set_bit(PG_error, &(page)->flags)
134 #define ClearPageError(page)    clear_bit(PG_error, &(page)->flags)
135 #define PageReferenced(page)    test_bit(PG_referenced, &(page)->flags)
136 #define SetPageReferenced(page) set_bit(PG_referenced, &(page)->flags)
137 #define ClearPageReferenced(page)       clear_bit(PG_referenced, &(page)->flags)
138
139 #define PageActive(page)        test_bit(PG_active, &(page)->flags)
140 #define SetPageActive(page)     set_bit(PG_active, &(page)->flags)
141 #define ClearPageActive(page)   clear_bit(PG_active, &(page)->flags)
142
143 #define PageWriteback(page)     test_bit(PG_writeback, &(page)->flags)
144 #define TestSetPageWriteback(page) test_and_set_bit(PG_writeback,       \
145                                                         &(page)->flags)
146 #define TestClearPageWriteback(page) test_and_clear_bit(PG_writeback,   \
147                                                         &(page)->flags)
148
149 #define __GFP_FS    (1)
150 #define GFP_KERNEL  (2)
151 #define GFP_ATOMIC  (4)
152
153 cfs_page_t *cfs_alloc_page(int flags);
154 void cfs_free_page(cfs_page_t *pg);
155 void cfs_release_page(cfs_page_t *pg);
156 cfs_page_t * virt_to_page(void * addr);
157 cfs_page_t *cfs_alloc_pages(unsigned int flags, unsigned int order);
158 void __cfs_free_pages(cfs_page_t *page, unsigned int order);
159 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem);
160
161 #define page_cache_get(a) do {} while (0)
162 #define page_cache_release(a) do {} while (0)
163
164 static inline void *cfs_page_address(cfs_page_t *page)
165 {
166     return page->addr;
167 }
168
169 static inline void *cfs_kmap(cfs_page_t *page)
170 {
171     return page->addr;
172 }
173
174 static inline void cfs_kunmap(cfs_page_t *page)
175 {
176     return;
177 }
178
179 static inline void cfs_get_page(cfs_page_t *page)
180 {
181     atomic_inc(&page->count);
182 }
183
184 static inline void cfs_put_page(cfs_page_t *page)
185 {
186     atomic_dec(&page->count);
187 }
188
189 static inline int cfs_page_count(cfs_page_t *page)
190 {
191     return atomic_read(&page->count);
192 }
193
194 #define cfs_page_index(p)       ((p)->index)
195
196 /*
197  * Memory allocator
198  */
199
200 #define CFS_ALLOC_ATOMIC_TRY    (0)
201 extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
202 extern void  cfs_free(void *addr);
203
204 #define kmalloc cfs_alloc
205
206 extern void *cfs_alloc_large(size_t nr_bytes);
207 extern void  cfs_free_large(void *addr);
208
209 /*
210  * SLAB allocator
211  */
212
213 #define SLAB_HWCACHE_ALIGN              0
214
215 /* The cache name is limited to 20 chars */
216
217 struct cfs_mem_cache {
218     char                    name[20];
219     ulong_ptr_t             flags;
220     NPAGED_LOOKASIDE_LIST   npll;
221 };
222
223
224 extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
225 extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
226 extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
227 extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
228
229 /*
230  * shrinker 
231  */
232 typedef int (*shrink_callback)(int nr_to_scan, gfp_t gfp_mask);
233 struct shrinker {
234     shrink_callback cb;
235         int seeks;      /* seeks to recreate an obj */
236
237         /* These are for internal use */
238         struct list_head list;
239         long nr;        /* objs pending delete */
240 };
241
242 struct shrinker * set_shrinker(int seeks, shrink_callback cb);
243 void remove_shrinker(struct shrinker *s);
244
245 int start_shrinker_timer();
246 void stop_shrinker_timer();
247
248 /*
249  * Page allocator slabs 
250  */
251
252 extern cfs_mem_cache_t *cfs_page_t_slab;
253 extern cfs_mem_cache_t *cfs_page_p_slab;
254
255
256 #define CFS_DECL_MMSPACE
257 #define CFS_MMSPACE_OPEN    do {} while(0)
258 #define CFS_MMSPACE_CLOSE   do {} while(0)
259
260
261 #define mb()    do {} while(0)
262 #define rmb()   mb()
263 #define wmb()   mb()
264
265 /*
266  * MM defintions from (linux/mm.h)
267  */
268
269 #define DEFAULT_SEEKS 2 /* shrink seek */
270
271 #else  /* !__KERNEL__ */
272
273 #include "../user-mem.h"
274
275 /* page alignmed buffer allocation */
276 void* pgalloc(size_t factor);
277 void  pgfree(void * page);
278
279 #endif /* __KERNEL__ */
280
281 #endif /* __WINNT_CFS_MEM_H__ */