One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example: struct stripe_c { ... struct stripe stripe[0]; }; In this case alloc_context() and dm_array_too_big() are removed and replaced by the direct use of the struct_size() helper in kmalloc(). Notice that open-coded form is prone to type mistakes. This code was detected with the help of Coccinelle.
Showing the changes to include/linux (1 of 2 files). Show all
Unified · Split
| @@ -594,9 +594,6 @@ void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size); · 593 unmodified lines | ||
| 594 | 594 | */ |
| 595 | 595 | #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz)) |
| 596 | 596 | |
| 597 | −#define dm_array_too_big(fixed, obj, num) \ | |
| 598 | − ((num) > (UINT_MAX - (fixed)) / (obj)) | |
| 599 | − | |
| 600 | 597 | /* |
| 601 | 598 | * Sector offset taken relative to the start of the target instead of |
| 602 | 599 | * relative to the start of the device. |