serial2002: Reduce stack usage in serial_2002_open() by allocating
authorIan Abbott <abbotti@mev.co.uk>
Wed, 24 Mar 2010 12:19:30 +0000 (12:19 +0000)
committerIan Abbott <abbotti@mev.co.uk>
Wed, 24 Mar 2010 12:19:30 +0000 (12:19 +0000)
dig_in_config, dig_out_config, chan_in_config and chan_out_config arrays
using kcalloc().

comedi/drivers/serial2002.c

index 974f0a1c42bd7776f10c9126178ee5f31c15a810..3bb3c457cd56a9b89c0408a75bd7a8dd2141749f 100644 (file)
@@ -407,29 +407,21 @@ static int serial_2002_open(comedi_device * dev)
                        int min;
                        int max;
                } config_t;
-               config_t dig_in_config[32];
-               config_t dig_out_config[32];
-               config_t chan_in_config[32];
-               config_t chan_out_config[32];
+               config_t *dig_in_config;
+               config_t *dig_out_config;
+               config_t *chan_in_config;
+               config_t *chan_out_config;
                int i;
 
-               for (i = 0; i < 32; i++) {
-                       dig_in_config[i].kind = 0;
-                       dig_in_config[i].bits = 0;
-                       dig_in_config[i].min = 0;
-                       dig_in_config[i].max = 0;
-                       dig_out_config[i].kind = 0;
-                       dig_out_config[i].bits = 0;
-                       dig_out_config[i].min = 0;
-                       dig_out_config[i].max = 0;
-                       chan_in_config[i].kind = 0;
-                       chan_in_config[i].bits = 0;
-                       chan_in_config[i].min = 0;
-                       chan_in_config[i].max = 0;
-                       chan_out_config[i].kind = 0;
-                       chan_out_config[i].bits = 0;
-                       chan_out_config[i].min = 0;
-                       chan_out_config[i].max = 0;
+               result = 0;
+               dig_in_config = kcalloc(32, sizeof(config_t), GFP_KERNEL);
+               dig_out_config = kcalloc(32, sizeof(config_t), GFP_KERNEL);
+               chan_in_config = kcalloc(32, sizeof(config_t), GFP_KERNEL);
+               chan_out_config = kcalloc(32, sizeof(config_t), GFP_KERNEL);
+               if (!dig_in_config || !dig_out_config
+                               || !chan_in_config || !chan_out_config) {
+                       result = -ENOMEM;
+                       goto err_alloc_configs;
                }
 
                tty_setspeed(devpriv->tty, devpriv->speed);
@@ -652,7 +644,17 @@ static int serial_2002_open(comedi_device * dev)
                                }
                        }
                }
-               result = 0;
+err_alloc_configs:
+               kfree(dig_in_config);
+               kfree(dig_out_config);
+               kfree(chan_in_config);
+               kfree(chan_out_config);
+               if (result) {
+                       if (devpriv->tty) {
+                               filp_close(devpriv->tty, 0);
+                               devpriv->tty = NULL;
+                       }
+               }
        }
        return result;
 }
@@ -661,6 +663,7 @@ static void serial_2002_close(comedi_device * dev)
 {
        if (!IS_ERR(devpriv->tty) && (devpriv->tty != 0)) {
                filp_close(devpriv->tty, 0);
+               devpriv->tty = NULL;
        }
 }