Fixed potential memory leak in s5j_i2s_initialize function.
authorShivam Garg <garg.shivam@samsung.com>
Tue, 5 Sep 2017 07:18:19 +0000 (16:18 +0900)
committerShivam Garg <garg.shivam@samsung.com>
Mon, 18 Sep 2017 14:03:31 +0000 (23:03 +0900)
Everytime a new s5j_i2s_s object was created when s5j_i2s_initialize function was called. This lead to memory leak when device on same port is initialized by multiple modules. Now , if the device has been initialised , then the dev object from the initialised s5j_i2s_s object is returned.

os/arch/arm/include/s5j/dma.h
os/arch/arm/src/artik053/src/artik053_alc5658char.c
os/arch/arm/src/artik053/src/artik053_i2schar.c
os/arch/arm/src/s5j/s5j_i2s.c
os/arch/arm/src/s5j/s5j_i2s.h

index 481d610..7ba94a8 100644 (file)
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
-#define s5j_dmasetup(a, b) if((b)->setup) (b)->setup(a, b);
+#define s5j_dmasetup(a, b)             \
+do {                                   \
+       if ((b)->setup) {               \
+               (b)->setup(a, b);       \
+       }                               \
+} while (0)
 
 /************************************************************************************
  * Public Types
index 5076d47..22507be 100644 (file)
@@ -129,7 +129,7 @@ int s5j_alc5658char_initialize(int minor)
                }
 
                /* Call s5j_i2s_initialize() to get an instance of the I2S interface */
-               i2s = s5j_i2s_initialize();
+               i2s = s5j_i2s_initialize(0);
                if (!i2s) {
                        auddbg("ERROR: Failed to get the S5J I2S driver\n");
                        return -ENODEV;
index b07a73f..b5c0f6a 100644 (file)
@@ -97,7 +97,7 @@ int i2schar_devinit(void)
        if (!initialized) {
                /* Call s5j_i2s_initialize() to get an instance of the I2S interface */
 
-               i2s = s5j_i2s_initialize();
+               i2s = s5j_i2s_initialize(0);
                if (!i2s) {
                        auddbg("ERROR: Failed to get the S5J I2S driver\n");
                        return -ENODEV;
index fa7f7e2..8e563fc 100644 (file)
 #error CONFIG_AUDIO required by this driver
 #endif
 
+#define S5J_I2S_MAXPORTS 1
+
 #ifndef CONFIG_S5J_I2S_MAXINFLIGHT
 #define CONFIG_S5J_I2S_MAXINFLIGHT 16
 #endif
@@ -322,6 +324,8 @@ static const struct i2s_ops_s g_i2sops = {
        .i2s_send = i2s_send,
 };
 
+static struct s5j_i2s_s *g_i2sdevice[S5J_I2S_MAXPORTS];
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -2011,8 +2015,16 @@ static int i2s_irq_handler(int irq, FAR void *context, FAR void *arg)
  *
  ****************************************************************************/
 
-struct i2s_dev_s *s5j_i2s_initialize(void)
+struct i2s_dev_s *s5j_i2s_initialize(uint16_t port)
 {
+       if (port >= S5J_I2S_MAXPORTS) {
+               lldbg("ERROR: Port number outside the allowed port number range\n");
+               return NULL;
+       }
+       if (g_i2sdevice[port] != NULL) {
+               return &g_i2sdevice[port]->dev;
+       }
+
        struct s5j_i2s_s *priv;
        int ret;
 
@@ -2076,6 +2088,8 @@ struct i2s_dev_s *s5j_i2s_initialize(void)
        irq_attach(priv->isr_num, priv->isr_handler, priv);
        up_enable_irq(priv->isr_num);
 
+       g_i2sdevice[port] = priv;
+
        /* Success exit */
        return &priv->dev;
 
index 0f8a27c..148e065 100644 (file)
@@ -111,7 +111,7 @@ extern "C" {
  *
  ****************************************************************************/
 
-FAR struct i2s_dev_s *s5j_i2s_initialize(void);
+FAR struct i2s_dev_s *s5j_i2s_initialize(uint16_t port);
 
 #undef EXTERN
 #if defined(__cplusplus)