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).
19 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
20 * Copyright (c) 2012, Intel Corporation.
23 * This file is part of Lustre, http://www.lustre.org/
24 * Lustre is a trademark of Sun Microsystems, Inc.
26 * Author: liang@whamcloud.com
29 #define DEBUG_SUBSYSTEM S_LNET
31 #include <linux/libcfs/libcfs.h>
33 struct cfs_var_array {
34 unsigned int va_count; /* # of buffers */
35 unsigned int va_size; /* size of each var */
36 struct cfs_cpt_table *va_cptab; /* cpu partition table */
37 void *va_ptrs[0]; /* buffer addresses */
41 * free per-cpu data, see more detail in cfs_percpt_free
44 cfs_percpt_free(void *vars)
46 struct cfs_var_array *arr;
49 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
51 for (i = 0; i < arr->va_count; i++) {
53 LIBCFS_FREE(arr->va_ptrs[i], arr->va_size);
56 LIBCFS_FREE(arr, offsetof(struct cfs_var_array,
57 va_ptrs[arr->va_count]));
59 EXPORT_SYMBOL(cfs_percpt_free);
62 * allocate per cpu-partition variables, returned value is an array of pointers,
63 * variable can be indexed by CPU partition ID, i.e:
65 * arr = cfs_percpt_alloc(cfs_cpu_pt, size);
66 * then caller can access memory block for CPU 0 by arr[0],
67 * memory block for CPU 1 by arr[1]...
68 * memory block for CPU N by arr[N]...
73 cfs_percpt_alloc(struct cfs_cpt_table *cptab, unsigned int size)
75 struct cfs_var_array *arr;
79 count = cfs_cpt_number(cptab);
81 LIBCFS_ALLOC(arr, offsetof(struct cfs_var_array, va_ptrs[count]));
85 size = L1_CACHE_ALIGN(size);
87 arr->va_count = count;
88 arr->va_cptab = cptab;
90 for (i = 0; i < count; i++) {
91 LIBCFS_CPT_ALLOC(arr->va_ptrs[i], cptab, i, size);
92 if (!arr->va_ptrs[i]) {
93 cfs_percpt_free((void *)&arr->va_ptrs[0]);
98 return (void *)&arr->va_ptrs[0];
100 EXPORT_SYMBOL(cfs_percpt_alloc);
103 * return number of CPUs (or number of elements in per-cpu data)
104 * according to cptab of @vars
107 cfs_percpt_number(void *vars)
109 struct cfs_var_array *arr;
111 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
113 return arr->va_count;
115 EXPORT_SYMBOL(cfs_percpt_number);
118 * free variable array, see more detail in cfs_array_alloc
121 cfs_array_free(void *vars)
123 struct cfs_var_array *arr;
126 arr = container_of(vars, struct cfs_var_array, va_ptrs[0]);
128 for (i = 0; i < arr->va_count; i++) {
129 if (!arr->va_ptrs[i])
132 LIBCFS_FREE(arr->va_ptrs[i], arr->va_size);
134 LIBCFS_FREE(arr, offsetof(struct cfs_var_array,
135 va_ptrs[arr->va_count]));
137 EXPORT_SYMBOL(cfs_array_free);
140 * allocate a variable array, returned value is an array of pointers.
141 * Caller can specify length of array by @count, @size is size of each
142 * memory block in array.
145 cfs_array_alloc(int count, unsigned int size)
147 struct cfs_var_array *arr;
150 LIBCFS_ALLOC(arr, offsetof(struct cfs_var_array, va_ptrs[count]));
154 arr->va_count = count;
157 for (i = 0; i < count; i++) {
158 LIBCFS_ALLOC(arr->va_ptrs[i], size);
160 if (!arr->va_ptrs[i]) {
161 cfs_array_free((void *)&arr->va_ptrs[0]);
166 return (void *)&arr->va_ptrs[0];
168 EXPORT_SYMBOL(cfs_array_alloc);