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, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2012, Intel Corporation.
28 * This file is part of Lustre, http://www.lustre.org/
29 * Lustre is a trademark of Sun Microsystems, Inc.
31 * Author: liang@whamcloud.com
34 #define DEBUG_SUBSYSTEM S_LNET
36 #include <libcfs/libcfs.h>
38 struct cfs_var_array {
39 unsigned int va_count; /* # of buffers */
40 unsigned int va_size; /* size of each var */
41 struct cfs_cpt_table *va_cptab; /* cpu partition table */
42 void *va_ptrs[0]; /* buffer addresses */
46 * free per-cpu data, see more detail in cfs_percpt_free
49 cfs_percpt_free(void *vars)
51 struct cfs_var_array *arr;
54 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
56 for (i = 0; i < arr->va_count; i++) {
57 if (arr->va_ptrs[i] != NULL)
58 LIBCFS_FREE(arr->va_ptrs[i], arr->va_size);
61 LIBCFS_FREE(arr, offsetof(struct cfs_var_array,
62 va_ptrs[arr->va_count]));
64 EXPORT_SYMBOL(cfs_percpt_free);
67 * allocate per cpu-partition variables, returned value is an array of pointers,
68 * variable can be indexed by CPU partition ID, i.e:
70 * arr = cfs_percpt_alloc(cfs_cpu_pt, size);
71 * then caller can access memory block for CPU 0 by arr[0],
72 * memory block for CPU 1 by arr[1]...
73 * memory block for CPU N by arr[N]...
78 cfs_percpt_alloc(struct cfs_cpt_table *cptab, unsigned int size)
80 struct cfs_var_array *arr;
84 count = cfs_cpt_number(cptab);
86 LIBCFS_ALLOC(arr, offsetof(struct cfs_var_array, va_ptrs[count]));
90 arr->va_size = size = L1_CACHE_ALIGN(size);
91 arr->va_count = count;
92 arr->va_cptab = cptab;
94 for (i = 0; i < count; i++) {
95 LIBCFS_CPT_ALLOC(arr->va_ptrs[i], cptab, i, size);
96 if (arr->va_ptrs[i] == NULL) {
97 cfs_percpt_free((void *)&arr->va_ptrs[0]);
102 return (void *)&arr->va_ptrs[0];
104 EXPORT_SYMBOL(cfs_percpt_alloc);
107 * return number of CPUs (or number of elements in per-cpu data)
108 * according to cptab of @vars
111 cfs_percpt_number(void *vars)
113 struct cfs_var_array *arr;
115 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
117 return arr->va_count;
119 EXPORT_SYMBOL(cfs_percpt_number);
122 * free variable array, see more detail in cfs_array_alloc
125 cfs_array_free(void *vars)
127 struct cfs_var_array *arr;
130 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
132 for (i = 0; i < arr->va_count; i++) {
133 if (arr->va_ptrs[i] == NULL)
136 LIBCFS_FREE(arr->va_ptrs[i], arr->va_size);
138 LIBCFS_FREE(arr, offsetof(struct cfs_var_array,
139 va_ptrs[arr->va_count]));
141 EXPORT_SYMBOL(cfs_array_free);
144 * allocate a variable array, returned value is an array of pointers.
145 * Caller can specify length of array by @count, @size is size of each
146 * memory block in array.
149 cfs_array_alloc(int count, unsigned int size)
151 struct cfs_var_array *arr;
154 LIBCFS_ALLOC(arr, offsetof(struct cfs_var_array, va_ptrs[count]));
158 arr->va_count = count;
161 for (i = 0; i < count; i++) {
162 LIBCFS_ALLOC(arr->va_ptrs[i], size);
164 if (arr->va_ptrs[i] == NULL) {
165 cfs_array_free((void *)&arr->va_ptrs[0]);
170 return (void *)&arr->va_ptrs[0];
172 EXPORT_SYMBOL(cfs_array_alloc);