Whamcloud - gitweb
LU-506 FC15: ctl_name & strategy removed from ctl_table.
[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 (c) 2008, 2010, Oracle and/or its affiliates. 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 /*
49  * Allocator
50  */
51
52 cfs_page_t *cfs_alloc_page(unsigned int flags)
53 {
54         cfs_page_t *pg = malloc(sizeof(*pg));
55         int rc = 0;
56
57         if (!pg)
58                 return NULL;
59         pg->addr = NULL;
60
61 #if defined (__DARWIN__)
62         pg->addr = valloc(CFS_PAGE_SIZE);
63 #elif defined (__WINNT__)
64         pg->addr = pgalloc(0);
65 #else
66         rc = posix_memalign(&pg->addr, CFS_PAGE_SIZE, CFS_PAGE_SIZE);
67 #endif
68         if (rc != 0 || pg->addr == NULL) {
69                 free(pg);
70                 return NULL;
71         }
72         return pg;
73 }
74
75 void cfs_free_page(cfs_page_t *pg)
76 {
77 #if defined (__WINNT__)
78         pgfree(pg->addr);
79 #else
80         free(pg->addr);
81 #endif
82
83         free(pg);
84 }
85
86 void *cfs_page_address(cfs_page_t *pg)
87 {
88         return pg->addr;
89 }
90
91 void *cfs_kmap(cfs_page_t *pg)
92 {
93         return pg->addr;
94 }
95
96 void cfs_kunmap(cfs_page_t *pg)
97 {
98 }
99
100 /*
101  * SLAB allocator
102  */
103
104 cfs_mem_cache_t *
105 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
106 {
107         cfs_mem_cache_t *c;
108
109         c = malloc(sizeof(*c));
110         if (!c)
111                 return NULL;
112         c->size = objsize;
113         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
114                name, c, (int)objsize);
115         return c;
116 }
117
118 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
119 {
120         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
121         free(c);
122         return 0;
123 }
124
125 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
126 {
127         return cfs_alloc(c->size, gfp);
128 }
129
130 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
131 {
132         cfs_free(addr);
133 }
134
135 /**
136  * Returns true if \a addr is an address of an allocated object in a slab \a
137  * kmem. Used in assertions. This check is optimistically imprecise, i.e., it
138  * occasionally returns true for the incorrect addresses, but if it returns
139  * false, then the addresses is guaranteed to be incorrect.
140  */
141 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)
142 {
143         return 1;
144 }