1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=4:tabstop=4:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define DEBUG_SUBSYSTEM S_LNET
24 #include <libcfs/libcfs.h>
27 cfs_mem_cache_t *cfs_page_t_slab = NULL;
28 cfs_mem_cache_t *cfs_page_p_slab = NULL;
32 * To allocate the cfs_page_t and also 1 page of memory
35 * flags: the allocation options
38 * pointer to the cfs_page_t strcture in success or
39 * NULL in failure case
45 cfs_page_t * cfs_alloc_page(int flags)
48 pg = cfs_mem_cache_alloc(cfs_page_t_slab, 0);
55 memset(pg, 0, sizeof(cfs_page_t));
56 pg->addr = cfs_mem_cache_alloc(cfs_page_p_slab, 0);
57 atomic_set(&pg->count, 1);
60 if (cfs_is_flag_set(flags, CFS_ALLOC_ZERO)) {
61 memset(pg->addr, 0, CFS_PAGE_SIZE);
65 cfs_mem_cache_free(cfs_page_t_slab, pg);
74 * To free the cfs_page_t including the page
77 * pg: pointer to the cfs_page_t strcture
85 void cfs_free_page(cfs_page_t *pg)
88 ASSERT(pg->addr != NULL);
89 ASSERT(atomic_read(&pg->count) <= 1);
91 cfs_mem_cache_free(cfs_page_p_slab, pg->addr);
92 cfs_mem_cache_free(cfs_page_t_slab, pg);
98 * To allocate memory from system pool
101 * nr_bytes: length in bytes of the requested buffer
102 * flags: flags indiction
105 * NULL: if there's no enough memory space in system
106 * the address of the allocated memory in success.
109 * This operation can be treated as atomic.
113 cfs_alloc(size_t nr_bytes, u_int32_t flags)
117 /* Ignore the flags: always allcoate from NonPagedPool */
119 ptr = ExAllocatePoolWithTag(NonPagedPool, nr_bytes, 'Lufs');
121 if (ptr != NULL && (flags & CFS_ALLOC_ZERO)) {
122 memset(ptr, 0, nr_bytes);
126 cfs_enter_debugger();
134 * To free the sepcified memory to system pool
137 * addr: pointer to the buffer to be freed
143 * This operation can be treated as atomic.
154 * To allocate large block of memory from system pool
157 * nr_bytes: length in bytes of the requested buffer
160 * NULL: if there's no enough memory space in system
161 * the address of the allocated memory in success.
168 cfs_alloc_large(size_t nr_bytes)
170 return cfs_alloc(nr_bytes, 0);
175 * To free the sepcified memory to system pool
178 * addr: pointer to the buffer to be freed
188 cfs_free_large(void *addr)
195 * cfs_mem_cache_create
196 * To create a SLAB cache
199 * name: name string of the SLAB cache to be created
200 * size: size in bytes of SLAB entry buffer
201 * offset: offset in the page
202 * flags: SLAB creation flags
205 * The poitner of cfs_memory_cache structure in success.
206 * NULL pointer in failure case.
209 * 1, offset won't be used here.
210 * 2, it could be better to induce a lock to protect the access of the
211 * SLAB structure on SMP if there's not outside lock protection.
212 * 3, parameters C/D are removed.
216 cfs_mem_cache_create(
223 cfs_mem_cache_t * kmc = NULL;
225 /* The name of the SLAB could not exceed 20 chars */
227 if (name && strlen(name) >= 20) {
231 /* Allocate and initialize the SLAB strcture */
233 kmc = cfs_alloc (sizeof(cfs_mem_cache_t), 0);
239 memset(kmc, 0, sizeof(cfs_mem_cache_t));
244 strcpy(&kmc->name[0], name);
247 /* Initialize the corresponding LookAside list */
249 ExInitializeNPagedLookasideList(
264 * cfs_mem_cache_destroy
265 * To destroy the unused SLAB cache
268 * kmc: the SLAB cache to be destroied.
271 * 0: in success case.
272 * 1: in failure case.
278 int cfs_mem_cache_destroy (cfs_mem_cache_t * kmc)
282 ExDeleteNPagedLookasideList(&(kmc->npll));
290 * cfs_mem_cache_alloc
291 * To allocate an object (LookAside entry) from the SLAB
294 * kmc: the SLAB cache to be allocated from.
295 * flags: flags for allocation options
298 * object buffer address: in success case.
299 * NULL: in failure case.
305 void *cfs_mem_cache_alloc(cfs_mem_cache_t * kmc, int flags)
309 buf = ExAllocateFromNPagedLookasideList(&(kmc->npll));
316 * To free an object (LookAside entry) to the SLAB cache
319 * kmc: the SLAB cache to be freed to.
320 * buf: the pointer to the object to be freed.
329 void cfs_mem_cache_free(cfs_mem_cache_t * kmc, void * buf)
331 ExFreeToNPagedLookasideList(&(kmc->npll), buf);