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