[RFC,3/4] devtools/cocci: add script to find where rte_calloc should be used

Message ID 20240425182738.4771-4-stephen@networkplumber.org (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series malloc type argument cleanup (part 1) |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Stephen Hemminger April 25, 2024, 6:24 p.m. UTC
  Better to use ret_calloc() than directly multiply up the
sizes of objects.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 devtools/cocci/prefer-calloc.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 devtools/cocci/prefer-calloc.cocci
  

Patch

diff --git a/devtools/cocci/prefer-calloc.cocci b/devtools/cocci/prefer-calloc.cocci
new file mode 100644
index 0000000000..77defed9f6
--- /dev/null
+++ b/devtools/cocci/prefer-calloc.cocci
@@ -0,0 +1,19 @@ 
+//
+// Prefer use of calloc over zmalloc when allocating multiple objects
+//
+@@
+expression name, T, num, socket, align;
+@@
+(
+- rte_zmalloc(name, num * sizeof(T), align)
++ rte_calloc(name, num, sizeof(T), align)
+|
+- rte_zmalloc(name, sizeof(T) * num, align)
++ rte_calloc(name, num, sizeof(T), align)
+|
+- rte_zmalloc_socket(name, num * sizeof(T), align, socket)
++ rte_calloc_socket(name, num, sizeof(T), align, socket)
+|
+- rte_zmalloc_socket(name, sizeof(T) * num, align, socket)
++ rte_calloc_socket(name, num, sizeof(T), align, socket)
+)