>From 73a9b9fcf0a7f846c42bcef6368d5681aada472c Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Thu, 17 Sep 2009 16:04:45 +0200
Subject: [PATCH] ELAPI: Ticket 161: Initialize structures with calloc instead of enumerating members

---
 common/elapi/elapi_internal.c               |    9 +--------
 common/elapi/elapi_log.c                    |    5 +----
 common/elapi/elapi_sink.c                   |   18 +-----------------
 common/elapi/providers/file/file_fmt_csv.c  |    2 +-
 common/elapi/providers/file/file_provider.c |    8 ++------
 5 files changed, 6 insertions(+), 36 deletions(-)

diff --git a/common/elapi/elapi_internal.c b/common/elapi/elapi_internal.c
index 7eec6fa..d0fd48d 100644
--- a/common/elapi/elapi_internal.c
+++ b/common/elapi/elapi_internal.c
@@ -242,19 +242,12 @@ int elapi_tgt_create(struct elapi_tgt_ctx **context,
     }
 
     /* Allocate context */
-    target_context = (struct elapi_tgt_ctx *)malloc(sizeof(struct elapi_tgt_ctx));
+    target_context = (struct elapi_tgt_ctx *)calloc(1, sizeof(struct elapi_tgt_ctx));
     if (target_context == NULL) {
         TRACE_ERROR_NUMBER("Memory allocation failed. Error", ENOMEM);
         return ENOMEM;
     }
 
-    /* Initialize the allocatable items so that we can call destroy function
-     * in case of error.
-     * FIXME - add initialization here for other elements as they are added.
-     */
-
-    target_context->sink_ref_list = NULL;
-
     /* Assign target's value */
     error = get_config_item(target,
                             ELAPI_TARGET_VALUE,
diff --git a/common/elapi/elapi_log.c b/common/elapi/elapi_log.c
index 1d1bfa0..926d858 100644
--- a/common/elapi/elapi_log.c
+++ b/common/elapi/elapi_log.c
@@ -165,15 +165,12 @@ int elapi_create_dispatcher_adv(struct elapi_dispatcher **dispatcher,
     TRACE_INFO_STRING("DIR:", config_dir);
 
     /* Allocate memory */
-    handle = (struct elapi_dispatcher *) malloc(sizeof(struct elapi_dispatcher));
+    handle = (struct elapi_dispatcher *) calloc(1, sizeof(struct elapi_dispatcher));
     if (handle == NULL) {
         TRACE_ERROR_NUMBER("Memory allocation failed. Error", ENOMEM);
         return ENOMEM;
     }
 
-    /* Clean memory - we need it to be able to destroy the dispatcher at any moment */
-    memset(handle, 0, sizeof(struct elapi_dispatcher));
-
     /* Save application name in the handle */
     if (appname != NULL) handle->appname = strdup(appname);
     else handle->appname = strdup(ELAPI_DEFAULT_APP_NAME);
diff --git a/common/elapi/elapi_sink.c b/common/elapi/elapi_sink.c
index 46ddadd..0e2712f 100644
--- a/common/elapi/elapi_sink.c
+++ b/common/elapi/elapi_sink.c
@@ -432,28 +432,12 @@ int elapi_sink_create(struct elapi_sink_ctx **sink_ctx,
     TRACE_FLOW_STRING("elapi_sink_create", "Entry point");
 
     /* Allocate context */
-    sink_context = (struct elapi_sink_ctx *)malloc(sizeof(struct elapi_sink_ctx));
+    sink_context = (struct elapi_sink_ctx *)calloc(1, sizeof(struct elapi_sink_ctx));
     if (sink_context == NULL) {
         TRACE_ERROR_NUMBER("Memory allocation failed. Error", ENOMEM);
         return ENOMEM;
     }
 
-    /* Initialize the allocatable items so that we can call destroy function
-     * in case of error.
-     * FIXME - add initialization here for other elements as they are added.
-     */
-
-    sink_context->async_mode = 0;
-    sink_context->in_queue = NULL;
-    sink_context->pending = NULL;
-    sink_context->sink_cfg.provider = NULL;
-    sink_context->sink_cfg.priv_ctx = NULL;
-    sink_context->sink_cfg.libhandle = NULL;
-    sink_context->sink_cfg.ability = NULL;
-    sink_context->sink_cfg.cpb_cb.init_cb = NULL;
-    sink_context->sink_cfg.cpb_cb.submit_cb = NULL;
-    sink_context->sink_cfg.cpb_cb.close_cb = NULL;
-
     /* Read common fields */
     error = elapi_read_sink_cfg(&(sink_context->sink_cfg),
                                 name, ini_config);
diff --git a/common/elapi/providers/file/file_fmt_csv.c b/common/elapi/providers/file/file_fmt_csv.c
index a811113..e55d0b1 100644
--- a/common/elapi/providers/file/file_fmt_csv.c
+++ b/common/elapi/providers/file/file_fmt_csv.c
@@ -275,7 +275,7 @@ int file_get_csv_cfg(void **storage,
     TRACE_FLOW_STRING("file_get_csv_cfg", "Entry");
 
     /* Allocate memory for configuration */
-    cfg = (struct file_csv_cfg *)malloc(sizeof(struct file_csv_cfg));
+    cfg = (struct file_csv_cfg *) calloc(1, sizeof(struct file_csv_cfg));
     if (cfg == NULL) {
         TRACE_ERROR_NUMBER("Failed to allocate storage for CSV configuration", ENOMEM);
         return ENOMEM;
diff --git a/common/elapi/providers/file/file_provider.c b/common/elapi/providers/file/file_provider.c
index 09d6261..652e646 100644
--- a/common/elapi/providers/file/file_provider.c
+++ b/common/elapi/providers/file/file_provider.c
@@ -550,16 +550,12 @@ static int file_create_ctx(struct file_prvdr_ctx **file_ctx,
 
     TRACE_FLOW_STRING("file_create_ctx", "Entry point");
 
-    ctx = (struct file_prvdr_ctx *)malloc(sizeof(struct file_prvdr_ctx));
+    ctx = (struct file_prvdr_ctx *)calloc(1, sizeof(struct file_prvdr_ctx));
     if (ctx == NULL) {
         TRACE_ERROR_NUMBER("Failed to allocate context", ENOMEM);
         return ENOMEM;
     }
-
-    /* Init allocatable items */
-    ctx->config.filename = NULL;
-    ctx->config.main_fmt_cfg = NULL;
-    ctx->config.lo_fmt_cfg = NULL;
+    /* Init items */
     ctx->outfile = -1;
 
     /* Read configuration data */
-- 
1.6.2.5

