27cf86106363ed7eb6cf853ac23e2ad90a3709c8
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 "../../include/linux/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
]) {
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 * return memory block shadowed from current CPU
125 cfs_percpt_current(void *vars
)
127 struct cfs_var_array
*arr
;
130 arr
= container_of(vars
, struct cfs_var_array
, va_ptrs
[0]);
131 cpt
= cfs_cpt_current(arr
->va_cptab
, 0);
135 return arr
->va_ptrs
[cpt
];
139 cfs_percpt_index(void *vars
, int idx
)
141 struct cfs_var_array
*arr
;
143 arr
= container_of(vars
, struct cfs_var_array
, va_ptrs
[0]);
145 LASSERT(idx
>= 0 && idx
< arr
->va_count
);
146 return arr
->va_ptrs
[idx
];
150 * free variable array, see more detail in cfs_array_alloc
153 cfs_array_free(void *vars
)
155 struct cfs_var_array
*arr
;
158 arr
= container_of(vars
, struct cfs_var_array
, va_ptrs
[0]);
160 for (i
= 0; i
< arr
->va_count
; i
++) {
161 if (!arr
->va_ptrs
[i
])
164 LIBCFS_FREE(arr
->va_ptrs
[i
], arr
->va_size
);
166 LIBCFS_FREE(arr
, offsetof(struct cfs_var_array
,
167 va_ptrs
[arr
->va_count
]));
169 EXPORT_SYMBOL(cfs_array_free
);
172 * allocate a variable array, returned value is an array of pointers.
173 * Caller can specify length of array by @count, @size is size of each
174 * memory block in array.
177 cfs_array_alloc(int count
, unsigned int size
)
179 struct cfs_var_array
*arr
;
182 LIBCFS_ALLOC(arr
, offsetof(struct cfs_var_array
, va_ptrs
[count
]));
186 arr
->va_count
= count
;
189 for (i
= 0; i
< count
; i
++) {
190 LIBCFS_ALLOC(arr
->va_ptrs
[i
], size
);
192 if (!arr
->va_ptrs
[i
]) {
193 cfs_array_free((void *)&arr
->va_ptrs
[0]);
198 return (void *)&arr
->va_ptrs
[0];
200 EXPORT_SYMBOL(cfs_array_alloc
);
This page took 0.035207 seconds and 4 git commands to generate.