Commit 17dca050 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by Jonathan Corbet

docs: deprecated.rst: Update zero-length/one-element arrays section

Update information in the zero-length and one-element arrays section
and illustrate how to make use of the new flex_array_size() helper,
together with struct_size() and a flexible-array member.
Signed-off-by: default avatarGustavo A. R. Silva <gustavoars@kernel.org>
Acked-by: default avatarKees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20200901010949.GA21398@embeddedorSigned-off-by: default avatarJonathan Corbet <corbet@lwn.net>
parent 9334e34f
...@@ -304,7 +304,8 @@ to allocate for a structure containing an array of this kind as a member:: ...@@ -304,7 +304,8 @@ to allocate for a structure containing an array of this kind as a member::
In the example above, we had to remember to calculate ``count - 1`` when using In the example above, we had to remember to calculate ``count - 1`` when using
the struct_size() helper, otherwise we would have --unintentionally-- allocated the struct_size() helper, otherwise we would have --unintentionally-- allocated
memory for one too many ``items`` objects. The cleanest and least error-prone way memory for one too many ``items`` objects. The cleanest and least error-prone way
to implement this is through the use of a `flexible array member`:: to implement this is through the use of a `flexible array member`, together with
struct_size() and flex_array_size() helpers::
struct something { struct something {
size_t count; size_t count;
...@@ -316,5 +317,4 @@ to implement this is through the use of a `flexible array member`:: ...@@ -316,5 +317,4 @@ to implement this is through the use of a `flexible array member`::
instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL); instance = kmalloc(struct_size(instance, items, count), GFP_KERNEL);
instance->count = count; instance->count = count;
size = sizeof(instance->items[0]) * instance->count; memcpy(instance->items, source, flex_array_size(instance, items, instance->count));
memcpy(instance->items, source, size);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment