4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/libcfs/user-mem.c
36 * Userspace memory management.
41 #error "This is not kernel code."
44 #include <libcfs/libcfs.h>
50 cfs_page_t *cfs_alloc_page(unsigned int flags)
52 cfs_page_t *pg = malloc(sizeof(*pg));
59 #if defined (__DARWIN__)
60 pg->addr = valloc(CFS_PAGE_SIZE);
61 #elif defined (__WINNT__)
62 pg->addr = pgalloc(0);
64 rc = posix_memalign(&pg->addr, CFS_PAGE_SIZE, CFS_PAGE_SIZE);
66 if (rc != 0 || pg->addr == NULL) {
73 void cfs_free_page(cfs_page_t *pg)
75 #if defined (__WINNT__)
84 void *cfs_page_address(cfs_page_t *pg)
89 void *cfs_kmap(cfs_page_t *pg)
94 void cfs_kunmap(cfs_page_t *pg)
103 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
107 c = malloc(sizeof(*c));
111 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
112 name, c, (int)objsize);
116 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
118 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
123 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
125 return cfs_alloc(c->size, gfp);
128 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
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.
139 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)