Whamcloud - gitweb
Land b_head_libcfs onto HEAD (20080805_1722)
[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 [sun.com URL with a
20  * copy of GPLv2].
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 #else
67         pg->addr = memalign(CFS_PAGE_SIZE, CFS_PAGE_SIZE << order);
68 #endif
69
70         if (!pg->addr) {
71                 free(pg);
72                 return NULL;
73         }
74         return pg;
75 }
76
77 void cfs_free_pages(cfs_page_t *pg, int what)
78 {
79 #if 0 //#ifdef MAP_ANONYMOUS
80         munmap(pg->addr, PAGE_SIZE);
81 #else
82         free(pg->addr);
83 #endif
84         free(pg);
85 }
86
87 cfs_page_t *cfs_alloc_page(unsigned int flags)
88 {
89         cfs_page_t *pg = malloc(sizeof(*pg));
90
91         if (!pg)
92                 return NULL;
93         pg->addr = malloc(CFS_PAGE_SIZE);
94
95         if (!pg->addr) {
96                 free(pg);
97                 return NULL;
98         }
99         return pg;
100 }
101
102 void cfs_free_page(cfs_page_t *pg)
103 {
104         free(pg->addr);
105         free(pg);
106 }
107
108 void *cfs_page_address(cfs_page_t *pg)
109 {
110         return pg->addr;
111 }
112
113 void *cfs_kmap(cfs_page_t *pg)
114 {
115         return pg->addr;
116 }
117
118 void cfs_kunmap(cfs_page_t *pg)
119 {
120 }
121
122 /*
123  * SLAB allocator
124  */
125
126 cfs_mem_cache_t *
127 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
128 {
129         cfs_mem_cache_t *c;
130
131         c = malloc(sizeof(*c));
132         if (!c)
133                 return NULL;
134         c->size = objsize;
135         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
136                name, c, (int)objsize);
137         return c;
138 }
139
140 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
141 {
142         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
143         free(c);
144         return 0;
145 }
146
147 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
148 {
149         return cfs_alloc(c->size, gfp);
150 }
151
152 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
153 {
154         cfs_free(addr);
155 }
156
157