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