Whamcloud - gitweb
b=20339
[fs/lustre-release.git] / libcfs / libcfs / user-mem.c
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/libcfs/user-mem.c
37  *
38  * Userspace memory management.
39  *
40  */
41
42 #ifdef __KERNEL__
43 #error "This is not kernel code."
44 #endif
45
46 #include <libcfs/libcfs.h>
47
48 #ifdef __linux__
49 #include <malloc.h>             /* memalign declared here on linux */
50 #endif
51
52 /*
53  * Allocator
54  */
55
56 cfs_page_t *cfs_alloc_pages(int mask, unsigned long order)
57 {
58         cfs_page_t *pg = malloc(sizeof(*pg));
59
60         if (!pg)
61                 return NULL;
62 #if 0 //#ifdef MAP_ANONYMOUS
63         pg->addr = mmap(0, PAGE_SIZE << order, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
64 #elif defined (__DARWIN__)
65         pg->addr = valloc(CFS_PAGE_SIZE << order);
66 #elif defined (__WINNT__)
67         pg->addr = pgalloc(order);
68 #else
69         pg->addr = memalign(CFS_PAGE_SIZE, CFS_PAGE_SIZE << order);
70 #endif
71
72         if (!pg->addr) {
73                 free(pg);
74                 return NULL;
75         }
76         return pg;
77 }
78
79 void cfs_free_pages(cfs_page_t *pg, int what)
80 {
81 #if 0 //#ifdef MAP_ANONYMOUS
82         munmap(pg->addr, PAGE_SIZE);
83 #elif defined (__WINNT__)
84         pgfree(pg->addr);
85 #else
86         free(pg->addr);
87 #endif
88         free(pg);
89 }
90
91 cfs_page_t *cfs_alloc_page(unsigned int flags)
92 {
93         cfs_page_t *pg = malloc(sizeof(*pg));
94
95         if (!pg)
96                 return NULL;
97         pg->addr = malloc(CFS_PAGE_SIZE);
98
99         if (!pg->addr) {
100                 free(pg);
101                 return NULL;
102         }
103         return pg;
104 }
105
106 void cfs_free_page(cfs_page_t *pg)
107 {
108         free(pg->addr);
109         free(pg);
110 }
111
112 void *cfs_page_address(cfs_page_t *pg)
113 {
114         return pg->addr;
115 }
116
117 void *cfs_kmap(cfs_page_t *pg)
118 {
119         return pg->addr;
120 }
121
122 void cfs_kunmap(cfs_page_t *pg)
123 {
124 }
125
126 /*
127  * SLAB allocator
128  */
129
130 cfs_mem_cache_t *
131 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
132 {
133         cfs_mem_cache_t *c;
134
135         c = malloc(sizeof(*c));
136         if (!c)
137                 return NULL;
138         c->size = objsize;
139         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
140                name, c, (int)objsize);
141         return c;
142 }
143
144 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
145 {
146         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
147         free(c);
148         return 0;
149 }
150
151 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
152 {
153         return cfs_alloc(c->size, gfp);
154 }
155
156 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
157 {
158         cfs_free(addr);
159 }
160
161 /**
162  * Returns true if \a addr is an address of an allocated object in a slab \a
163  * kmem. Used in assertions. This check is optimistically imprecise, i.e., it
164  * occasionally returns true for the incorrect addresses, but if it returns
165  * false, then the addresses is guaranteed to be incorrect.
166  */
167 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)
168 {
169         return 1;
170 }