Whamcloud - gitweb
fa4ba3d3ae72312fd6efc8c74ec9fd276a3bbd7d
[fs/lustre-release.git] / lnet / include / libcfs / linux / linux-mem.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Basic library routines.
22  *
23  */
24
25 #ifndef __LIBCFS_LINUX_CFS_MEM_H__
26 #define __LIBCFS_LINUX_CFS_MEM_H__
27
28 #ifndef __LIBCFS_LIBCFS_H__
29 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
30 #endif
31
32 #ifdef __KERNEL__
33 # include <linux/mm.h>
34 # include <linux/vmalloc.h>
35 # include <linux/pagemap.h>
36 # include <linux/slab.h>
37 # ifdef HAVE_MM_INLINE
38 #  include <linux/mm_inline.h>
39 # endif
40
41 typedef struct page                     cfs_page_t;
42 #define CFS_PAGE_SIZE                   PAGE_CACHE_SIZE
43 #define CFS_PAGE_SHIFT                  PAGE_CACHE_SHIFT
44 #define CFS_PAGE_MASK                   (~((__u64)CFS_PAGE_SIZE-1))
45
46 static inline void *cfs_page_address(cfs_page_t *page)
47 {
48         /*
49          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
50          * from here: this will lead to infinite recursion.
51          */
52         return page_address(page);
53 }
54
55 static inline void *cfs_kmap(cfs_page_t *page)
56 {
57         return kmap(page);
58 }
59
60 static inline void cfs_kunmap(cfs_page_t *page)
61 {
62         kunmap(page);
63 }
64
65 static inline void cfs_get_page(cfs_page_t *page)
66 {
67         get_page(page);
68 }
69
70 static inline int cfs_page_count(cfs_page_t *page)
71 {
72         return page_count(page);
73 }
74
75 #define cfs_page_index(p)       ((p)->index)
76
77 /*
78  * Memory allocator
79  * XXX Liang: move these declare to public file
80  */
81 extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
82 extern void  cfs_free(void *addr);
83
84 extern void *cfs_alloc_large(size_t nr_bytes);
85 extern void  cfs_free_large(void *addr);
86
87 extern cfs_page_t *cfs_alloc_pages(unsigned int flags, unsigned int order);
88 extern void __cfs_free_pages(cfs_page_t *page, unsigned int order);
89
90 #define cfs_alloc_page(flags)  cfs_alloc_pages(flags, 0)
91 #define __cfs_free_page(page)  __cfs_free_pages(page, 0)
92 #define cfs_free_page(p)       __free_pages(p, 0)
93
94 /*
95  * In Linux there is no way to determine whether current execution context is
96  * blockable.
97  */
98 #define CFS_ALLOC_ATOMIC_TRY   CFS_ALLOC_ATOMIC
99
100 /*
101  * SLAB allocator
102  * XXX Liang: move these declare to public file
103  */
104 #ifdef HAVE_KMEM_CACHE
105 typedef struct kmem_cache cfs_mem_cache_t;
106 #else
107 typedef kmem_cache_t cfs_mem_cache_t;
108 #endif
109 extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
110 extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
111 extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
112 extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
113
114 /*
115  */
116 #define CFS_DECL_MMSPACE                mm_segment_t __oldfs
117 #define CFS_MMSPACE_OPEN                do { __oldfs = get_fs(); set_fs(get_ds());} while(0)
118 #define CFS_MMSPACE_CLOSE               set_fs(__oldfs)
119
120 #else   /* !__KERNEL__ */
121 #ifdef HAVE_ASM_PAGE_H
122 #include <asm/page.h>           /* needed for PAGE_SIZE - rread */
123 #endif
124
125 #include <libcfs/user-prim.h>
126 /* __KERNEL__ */
127 #endif
128
129 #endif /* __LINUX_CFS_MEM_H__ */