Whamcloud - gitweb
LU-2675 build: remove Darwin "support"
[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 struct page *alloc_page(unsigned int flags)
51 {
52         struct page *pg = malloc(sizeof(*pg));
53         int rc = 0;
54
55         if (!pg)
56                 return NULL;
57         pg->addr = NULL;
58
59         rc = posix_memalign(&pg->addr, PAGE_CACHE_SIZE, PAGE_CACHE_SIZE);
60         if (rc != 0 || pg->addr == NULL) {
61                 free(pg);
62                 return NULL;
63         }
64         return pg;
65 }
66
67 void __free_page(struct page *pg)
68 {
69         free(pg->addr);
70
71         free(pg);
72 }
73
74 void *page_address(struct page *pg)
75 {
76         return pg->addr;
77 }
78
79 void *kmap(struct page *pg)
80 {
81         return pg->addr;
82 }
83
84 void kunmap(struct page *pg)
85 {
86 }
87
88 /*
89  * SLAB allocator
90  */
91
92 struct kmem_cache *
93 kmem_cache_create(const char *name, size_t objsize, size_t off,
94                   unsigned long flags, void *ctor)
95 {
96         struct kmem_cache *c;
97
98         c = malloc(sizeof(*c));
99         if (!c)
100                 return NULL;
101         c->size = objsize;
102         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
103                name, c, (int)objsize);
104         return c;
105 }
106
107 void kmem_cache_destroy(struct kmem_cache *c)
108 {
109         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
110         free(c);
111 }
112
113 void *kmem_cache_alloc(struct kmem_cache *c, int gfp)
114 {
115         return kmalloc(c->size, gfp);
116 }
117
118 void kmem_cache_free(struct kmem_cache *c, void *addr)
119 {
120         kfree(addr);
121 }
122
123 /**
124  * Returns true if \a addr is an address of an allocated object in a slab \a
125  * kmem. Used in assertions. This check is optimistically imprecise, i.e., it
126  * occasionally returns true for the incorrect addresses, but if it returns
127  * false, then the addresses is guaranteed to be incorrect.
128  */
129 int kmem_is_in_cache(const void *addr, const struct kmem_cache *kmem)
130 {
131         return 1;
132 }