1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/flex_array.h>
24#include <linux/slab.h>
25#include <linux/stddef.h>
26#include <linux/export.h>
27#include <linux/reciprocal_div.h>
28
29struct flex_array_part {
30 char elements[FLEX_ARRAY_PART_SIZE];
31};
32
33
34
35
36
37
38
39static inline int elements_fit_in_base(struct flex_array *fa)
40{
41 int data_size = fa->element_size * fa->total_nr_elements;
42 if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT)
43 return 1;
44 return 0;
45}
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88struct flex_array *flex_array_alloc(int element_size, unsigned int total,
89 gfp_t flags)
90{
91 struct flex_array *ret;
92 int elems_per_part = 0;
93 int reciprocal_elems = 0;
94 int max_size = 0;
95
96 if (element_size) {
97 elems_per_part = FLEX_ARRAY_ELEMENTS_PER_PART(element_size);
98 reciprocal_elems = reciprocal_value(elems_per_part);
99 max_size = FLEX_ARRAY_NR_BASE_PTRS * elems_per_part;
100 }
101
102
103 if (total > max_size)
104 return NULL;
105 ret = kzalloc(sizeof(struct flex_array), flags);
106 if (!ret)
107 return NULL;
108 ret->element_size = element_size;
109 ret->total_nr_elements = total;
110 ret->elems_per_part = elems_per_part;
111 ret->reciprocal_elems = reciprocal_elems;
112 if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO))
113 memset(&ret->parts[0], FLEX_ARRAY_FREE,
114 FLEX_ARRAY_BASE_BYTES_LEFT);
115 return ret;
116}
117EXPORT_SYMBOL(flex_array_alloc);
118
119static int fa_element_to_part_nr(struct flex_array *fa,
120 unsigned int element_nr)
121{
122 return reciprocal_divide(element_nr, fa->reciprocal_elems);
123}
124
125
126
127
128
129
130
131
132void flex_array_free_parts(struct flex_array *fa)
133{
134 int part_nr;
135
136 if (elements_fit_in_base(fa))
137 return;
138 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++)
139 kfree(fa->parts[part_nr]);
140}
141EXPORT_SYMBOL(flex_array_free_parts);
142
143void flex_array_free(struct flex_array *fa)
144{
145 flex_array_free_parts(fa);
146 kfree(fa);
147}
148EXPORT_SYMBOL(flex_array_free);
149
150static unsigned int index_inside_part(struct flex_array *fa,
151 unsigned int element_nr,
152 unsigned int part_nr)
153{
154 unsigned int part_offset;
155
156 part_offset = element_nr - part_nr * fa->elems_per_part;
157 return part_offset * fa->element_size;
158}
159
160static struct flex_array_part *
161__fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags)
162{
163 struct flex_array_part *part = fa->parts[part_nr];
164 if (!part) {
165 part = kmalloc(sizeof(struct flex_array_part), flags);
166 if (!part)
167 return NULL;
168 if (!(flags & __GFP_ZERO))
169 memset(part, FLEX_ARRAY_FREE,
170 sizeof(struct flex_array_part));
171 fa->parts[part_nr] = part;
172 }
173 return part;
174}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src,
194 gfp_t flags)
195{
196 int part_nr = 0;
197 struct flex_array_part *part;
198 void *dst;
199
200 if (element_nr >= fa->total_nr_elements)
201 return -ENOSPC;
202 if (!fa->element_size)
203 return 0;
204 if (elements_fit_in_base(fa))
205 part = (struct flex_array_part *)&fa->parts[0];
206 else {
207 part_nr = fa_element_to_part_nr(fa, element_nr);
208 part = __fa_get_part(fa, part_nr, flags);
209 if (!part)
210 return -ENOMEM;
211 }
212 dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
213 memcpy(dst, src, fa->element_size);
214 return 0;
215}
216EXPORT_SYMBOL(flex_array_put);
217
218
219
220
221
222
223
224
225int flex_array_clear(struct flex_array *fa, unsigned int element_nr)
226{
227 int part_nr = 0;
228 struct flex_array_part *part;
229 void *dst;
230
231 if (element_nr >= fa->total_nr_elements)
232 return -ENOSPC;
233 if (!fa->element_size)
234 return 0;
235 if (elements_fit_in_base(fa))
236 part = (struct flex_array_part *)&fa->parts[0];
237 else {
238 part_nr = fa_element_to_part_nr(fa, element_nr);
239 part = fa->parts[part_nr];
240 if (!part)
241 return -EINVAL;
242 }
243 dst = &part->elements[index_inside_part(fa, element_nr, part_nr)];
244 memset(dst, FLEX_ARRAY_FREE, fa->element_size);
245 return 0;
246}
247EXPORT_SYMBOL(flex_array_clear);
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263int flex_array_prealloc(struct flex_array *fa, unsigned int start,
264 unsigned int nr_elements, gfp_t flags)
265{
266 int start_part;
267 int end_part;
268 int part_nr;
269 unsigned int end;
270 struct flex_array_part *part;
271
272 if (!start && !nr_elements)
273 return 0;
274 if (start >= fa->total_nr_elements)
275 return -ENOSPC;
276 if (!nr_elements)
277 return 0;
278
279 end = start + nr_elements - 1;
280
281 if (end >= fa->total_nr_elements)
282 return -ENOSPC;
283 if (!fa->element_size)
284 return 0;
285 if (elements_fit_in_base(fa))
286 return 0;
287 start_part = fa_element_to_part_nr(fa, start);
288 end_part = fa_element_to_part_nr(fa, end);
289 for (part_nr = start_part; part_nr <= end_part; part_nr++) {
290 part = __fa_get_part(fa, part_nr, flags);
291 if (!part)
292 return -ENOMEM;
293 }
294 return 0;
295}
296EXPORT_SYMBOL(flex_array_prealloc);
297
298
299
300
301
302
303
304
305
306
307
308
309
310void *flex_array_get(struct flex_array *fa, unsigned int element_nr)
311{
312 int part_nr = 0;
313 struct flex_array_part *part;
314
315 if (!fa->element_size)
316 return NULL;
317 if (element_nr >= fa->total_nr_elements)
318 return NULL;
319 if (elements_fit_in_base(fa))
320 part = (struct flex_array_part *)&fa->parts[0];
321 else {
322 part_nr = fa_element_to_part_nr(fa, element_nr);
323 part = fa->parts[part_nr];
324 if (!part)
325 return NULL;
326 }
327 return &part->elements[index_inside_part(fa, element_nr, part_nr)];
328}
329EXPORT_SYMBOL(flex_array_get);
330
331
332
333
334
335
336
337
338
339
340void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr)
341{
342 void **tmp;
343
344 tmp = flex_array_get(fa, element_nr);
345 if (!tmp)
346 return NULL;
347
348 return *tmp;
349}
350EXPORT_SYMBOL(flex_array_get_ptr);
351
352static int part_is_free(struct flex_array_part *part)
353{
354 int i;
355
356 for (i = 0; i < sizeof(struct flex_array_part); i++)
357 if (part->elements[i] != FLEX_ARRAY_FREE)
358 return 0;
359 return 1;
360}
361
362
363
364
365
366
367
368
369
370
371int flex_array_shrink(struct flex_array *fa)
372{
373 struct flex_array_part *part;
374 int part_nr;
375 int ret = 0;
376
377 if (!fa->total_nr_elements || !fa->element_size)
378 return 0;
379 if (elements_fit_in_base(fa))
380 return ret;
381 for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) {
382 part = fa->parts[part_nr];
383 if (!part)
384 continue;
385 if (part_is_free(part)) {
386 fa->parts[part_nr] = NULL;
387 kfree(part);
388 ret++;
389 }
390 }
391 return ret;
392}
393EXPORT_SYMBOL(flex_array_shrink);
394