Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'docs-next' of git://git.lwn.net/linux-2.6

* 'docs-next' of git://git.lwn.net/linux-2.6:
Update flex_arrays.txt

+31 -10
+31 -10
Documentation/flexible-arrays.txt
··· 1 1 Using flexible arrays in the kernel 2 - Last updated for 2.6.31 2 + Last updated for 2.6.32 3 3 Jonathan Corbet <corbet@lwn.net> 4 4 5 5 Large contiguous memory allocations can be unreliable in the Linux kernel. ··· 40 40 the current code, using flags to ask for high memory is likely to lead to 41 41 notably unpleasant side effects. 42 42 43 + It is also possible to define flexible arrays at compile time with: 44 + 45 + DEFINE_FLEX_ARRAY(name, element_size, total); 46 + 47 + This macro will result in a definition of an array with the given name; the 48 + element size and total will be checked for validity at compile time. 49 + 43 50 Storing data into a flexible array is accomplished with a call to: 44 51 45 52 int flex_array_put(struct flex_array *array, unsigned int element_nr, ··· 83 76 Note that it is possible to get back a valid pointer for an element which 84 77 has never been stored in the array. Memory for array elements is allocated 85 78 one page at a time; a single allocation could provide memory for several 86 - adjacent elements. The flexible array code does not know if a specific 87 - element has been written; it only knows if the associated memory is 88 - present. So a flex_array_get() call on an element which was never stored 89 - in the array has the potential to return a pointer to random data. If the 90 - caller does not have a separate way to know which elements were actually 91 - stored, it might be wise, at least, to add GFP_ZERO to the flags argument 92 - to ensure that all elements are zeroed. 79 + adjacent elements. Flexible array elements are normally initialized to the 80 + value FLEX_ARRAY_FREE (defined as 0x6c in <linux/poison.h>), so errors 81 + involving that number probably result from use of unstored array entries. 82 + Note that, if array elements are allocated with __GFP_ZERO, they will be 83 + initialized to zero and this poisoning will not happen. 93 84 94 - There is no way to remove a single element from the array. It is possible, 95 - though, to remove all elements with a call to: 85 + Individual elements in the array can be cleared with: 86 + 87 + int flex_array_clear(struct flex_array *array, unsigned int element_nr); 88 + 89 + This function will set the given element to FLEX_ARRAY_FREE and return 90 + zero. If storage for the indicated element is not allocated for the array, 91 + flex_array_clear() will return -EINVAL instead. Note that clearing an 92 + element does not release the storage associated with it; to reduce the 93 + allocated size of an array, call: 94 + 95 + int flex_array_shrink(struct flex_array *array); 96 + 97 + The return value will be the number of pages of memory actually freed. 98 + This function works by scanning the array for pages containing nothing but 99 + FLEX_ARRAY_FREE bytes, so (1) it can be expensive, and (2) it will not work 100 + if the array's pages are allocated with __GFP_ZERO. 101 + 102 + It is possible to remove all elements of an array with a call to: 96 103 97 104 void flex_array_free_parts(struct flex_array *array); 98 105