[FE training-materials-updates] Kernel i2c lab: use devm_kzalloc instead of kzalloc

Michael Opdenacker michael.opdenacker at free-electrons.com
Tue Feb 4 14:53:01 CET 2014

Repository : git://git.free-electrons.com/training-materials.git

On branch  : master
Link       : http://git.free-electrons.com/training-materials/commit/?id=924ff033a166689dec71fdb691fcd5fa65ae781f


commit 924ff033a166689dec71fdb691fcd5fa65ae781f
Author: Michael Opdenacker <michael.opdenacker at free-electrons.com>
Date:   Tue Feb 4 14:51:48 2014 +0100

    Kernel i2c lab: use devm_kzalloc instead of kzalloc
    Using kzalloc in this context was deprecated
    Signed-off-by: Michael Opdenacker <michael.opdenacker at free-electrons.com>


 .../kernel-i2c-input-interface.tex                 |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex b/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex
index b8f1f28..53a41ea 100644
--- a/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex
+++ b/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex
@@ -118,7 +118,7 @@ this structure:
 Then allocate one such instead for each new device:
-        nunchuk = kzalloc(sizeof(struct nunchuk_dev), GFP_KERNEL);
+        nunchuk = devm_kzalloc(sizeof(&client->dev, sizeof(struct nunchuk_dev), GFP_KERNEL);
         if (!nunchuk) {
                 dev_err(&client->dev, "Failed to allocate memory\n");
                 return -ENOMEM;
@@ -131,6 +131,10 @@ yet. We haven't explained the \code{dev_*} logging routines yet either
 associated to).  For the moment, just use the above code. You will get
 the details later.
+Note that with \code{devm_} functions it is not needed to free the
+allocated resources. They are automatically freed when the corresponding
+device in unprobed. 
 You will actually have to modify your code and the above lines to call 
 \code{kfree(nunchuk)} if one of the subsequent registration and
 allocation routines fail.

More information about the training-materials-updates mailing list