Whamcloud - gitweb
LU-2088 utils: lctl to generate gdb symbols for lod/osp
[fs/lustre-release.git] / libcfs / libcfs / user-mem.c
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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/libcfs/user-mem.c
35  *
36  * Userspace memory management.
37  *
38  */
39
40 #ifdef __KERNEL__
41 #error "This is not kernel code."
42 #endif
43
44 #include <libcfs/libcfs.h>
45
46 /*
47  * Allocator
48  */
49
50 cfs_page_t *cfs_alloc_page(unsigned int flags)
51 {
52         cfs_page_t *pg = malloc(sizeof(*pg));
53         int rc = 0;
54
55         if (!pg)
56                 return NULL;
57         pg->addr = NULL;
58
59 #if defined (__DARWIN__)
60         pg->addr = valloc(CFS_PAGE_SIZE);
61 #elif defined (__WINNT__)
62         pg->addr = pgalloc(0);
63 #else
64         rc = posix_memalign(&pg->addr, CFS_PAGE_SIZE, CFS_PAGE_SIZE);
65 #endif
66         if (rc != 0 || pg->addr == NULL) {
67                 free(pg);
68                 return NULL;
69         }
70         return pg;
71 }
72
73 void cfs_free_page(cfs_page_t *pg)
74 {
75 #if defined (__WINNT__)
76         pgfree(pg->addr);
77 #else
78         free(pg->addr);
79 #endif
80
81         free(pg);
82 }
83
84 void *cfs_page_address(cfs_page_t *pg)
85 {
86         return pg->addr;
87 }
88
89 void *cfs_kmap(cfs_page_t *pg)
90 {
91         return pg->addr;
92 }
93
94 void cfs_kunmap(cfs_page_t *pg)
95 {
96 }
97
98 /*
99  * SLAB allocator
100  */
101
102 cfs_mem_cache_t *
103 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
104 {
105         cfs_mem_cache_t *c;
106
107         c = malloc(sizeof(*c));
108         if (!c)
109                 return NULL;
110         c->size = objsize;
111         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
112                name, c, (int)objsize);
113         return c;
114 }
115
116 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
117 {
118         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
119         free(c);
120         return 0;
121 }
122
123 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
124 {
125         return cfs_alloc(c->size, gfp);
126 }
127
128 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
129 {
130         cfs_free(addr);
131 }
132
133 /**
134  * Returns true if \a addr is an address of an allocated object in a slab \a
135  * kmem. Used in assertions. This check is optimistically imprecise, i.e., it
136  * occasionally returns true for the incorrect addresses, but if it returns
137  * false, then the addresses is guaranteed to be incorrect.
138  */
139 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)
140 {
141         return 1;
142 }