1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/user-mem.c
38 * Userspace memory management.
43 #error "This is not kernel code."
46 #include <libcfs/libcfs.h>
49 #include <malloc.h> /* memalign declared here on linux */
56 cfs_page_t *cfs_alloc_pages(int mask, unsigned long order)
58 cfs_page_t *pg = malloc(sizeof(*pg));
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 #elif defined (__WINNT__)
67 pg->addr = pgalloc(order);
69 pg->addr = memalign(CFS_PAGE_SIZE, CFS_PAGE_SIZE << order);
79 void cfs_free_pages(cfs_page_t *pg, int what)
81 #if 0 //#ifdef MAP_ANONYMOUS
82 munmap(pg->addr, PAGE_SIZE);
83 #elif defined (__WINNT__)
91 cfs_page_t *cfs_alloc_page(unsigned int flags)
93 cfs_page_t *pg = malloc(sizeof(*pg));
97 pg->addr = malloc(CFS_PAGE_SIZE);
106 void cfs_free_page(cfs_page_t *pg)
112 void *cfs_page_address(cfs_page_t *pg)
117 void *cfs_kmap(cfs_page_t *pg)
122 void cfs_kunmap(cfs_page_t *pg)
131 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
135 c = malloc(sizeof(*c));
139 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
140 name, c, (int)objsize);
144 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
146 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
151 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
153 return cfs_alloc(c->size, gfp);
156 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
162 * Returns true if \a addr is an address of an allocated object in a slab \a
163 * kmem. Used in assertions. This check is optimistically imprecise, i.e., it
164 * occasionally returns true for the incorrect addresses, but if it returns
165 * false, then the addresses is guaranteed to be incorrect.
167 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)