treewide: use designated initializers for user_data
[collectd.git] / src / java.c
index 6b92f54..67740ac 100644 (file)
  **/
 
 #include "collectd.h"
+
 #include "plugin.h"
 #include "common.h"
 #include "filter_chain.h"
 
-#include <pthread.h>
 #include <jni.h>
 
 #if !defined(JNI_VERSION_1_2)
@@ -507,7 +507,6 @@ static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
   jmethodID m_addchild;
   jobject o_key;
   jobject o_ocitem;
-  int i;
 
   c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
   if (c_ocitem == NULL)
@@ -572,7 +571,7 @@ static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
   (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
 
   /* Call OConfigItem.addValue for each value */
-  for (i = 0; i < ci->values_num; i++) /* {{{ */
+  for (int i = 0; i < ci->values_num; i++) /* {{{ */
   {
     jobject o_value;
 
@@ -590,7 +589,7 @@ static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
   } /* }}} for (i = 0; i < ci->values_num; i++) */
 
   /* Call OConfigItem.addChild for each child */
-  for (i = 0; i < ci->children_num; i++) /* {{{ */
+  for (int i = 0; i < ci->children_num; i++) /* {{{ */
   {
     jobject o_child;
 
@@ -618,7 +617,6 @@ static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
   jmethodID m_add;
   jobject o_type;
   jobject o_dataset;
-  size_t i;
 
   /* Look up the org/collectd/api/DataSet class */
   c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
@@ -668,7 +666,7 @@ static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
   /* Decrease reference counter on the java.lang.String object. */
   (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
 
-  for (i = 0; i < ds->ds_num; i++)
+  for (size_t i = 0; i < ds->ds_num; i++)
   {
     jobject o_datasource;
 
@@ -763,7 +761,6 @@ static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
   jmethodID m_valuelist_constructor;
   jobject o_valuelist;
   int status;
-  size_t i;
 
   /* First, create a new ValueList instance..
    * Look up the class.. */
@@ -846,7 +843,7 @@ static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
     return (NULL);
   }
 
-  for (i = 0; i < vl->values_len; i++)
+  for (size_t i = 0; i < vl->values_len; i++)
   {
     status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
         c_valuelist, o_valuelist);
@@ -1117,7 +1114,6 @@ static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
 
   value_t *values;
   int values_num;
-  int i;
 
   values_num = ds->ds_num;
 
@@ -1177,7 +1173,7 @@ static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
     BAIL_OUT (-1);
   }
 
-  for (i = 0; i < values_num; i++)
+  for (int i = 0; i < values_num; i++)
   {
     jobject o_number;
     int status;
@@ -1364,12 +1360,9 @@ static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_notification)
 {
-  notification_t n;
+  notification_t n = { 0 };
   int status;
 
-  memset (&n, 0, sizeof (n));
-  n.meta = NULL;
-
   status = jtoc_notification (jvm_env, &n, o_notification);
   if (status != 0)
   {
@@ -1424,7 +1417,6 @@ static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_name, jobject o_read)
 {
-  user_data_t ud;
   cjni_callback_info_t *cbi;
 
   cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
@@ -1433,9 +1425,10 @@ static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
 
   DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
 
-  memset (&ud, 0, sizeof (ud));
-  ud.data = (void *) cbi;
-  ud.free_func = cjni_callback_info_destroy;
+  user_data_t ud = {
+    .data = cbi,
+    .free_func = cjni_callback_info_destroy
+  };
 
   plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
       /* interval = */ 0, &ud);
@@ -1448,7 +1441,6 @@ static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_name, jobject o_write)
 {
-  user_data_t ud;
   cjni_callback_info_t *cbi;
 
   cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
@@ -1457,9 +1449,10 @@ static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
 
   DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
 
-  memset (&ud, 0, sizeof (ud));
-  ud.data = (void *) cbi;
-  ud.free_func = cjni_callback_info_destroy;
+  user_data_t ud = {
+    .data = cbi,
+    .free_func = cjni_callback_info_destroy
+  };
 
   plugin_register_write (cbi->name, cjni_write, &ud);
 
@@ -1471,7 +1464,6 @@ static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_name, jobject o_flush)
 {
-  user_data_t ud;
   cjni_callback_info_t *cbi;
 
   cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
@@ -1480,9 +1472,10 @@ static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
 
   DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
 
-  memset (&ud, 0, sizeof (ud));
-  ud.data = (void *) cbi;
-  ud.free_func = cjni_callback_info_destroy;
+  user_data_t ud = {
+    .data = cbi,
+    .free_func = cjni_callback_info_destroy
+  };
 
   plugin_register_flush (cbi->name, cjni_flush, &ud);
 
@@ -1501,7 +1494,6 @@ static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_name, jobject o_log)
 {
-  user_data_t ud;
   cjni_callback_info_t *cbi;
 
   cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
@@ -1510,9 +1502,10 @@ static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
 
   DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
 
-  memset (&ud, 0, sizeof (ud));
-  ud.data = (void *) cbi;
-  ud.free_func = cjni_callback_info_destroy;
+  user_data_t ud = {
+    .data = cbi,
+    .free_func = cjni_callback_info_destroy
+  };
 
   plugin_register_log (cbi->name, cjni_log, &ud);
 
@@ -1524,7 +1517,6 @@ static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_name, jobject o_notification)
 {
-  user_data_t ud;
   cjni_callback_info_t *cbi;
 
   cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
@@ -1534,9 +1526,10 @@ static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
 
   DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
 
-  memset (&ud, 0, sizeof (ud));
-  ud.data = (void *) cbi;
-  ud.free_func = cjni_callback_info_destroy;
+  user_data_t ud = {
+    .data = cbi,
+    .free_func = cjni_callback_info_destroy
+  };
 
   plugin_register_notification (cbi->name, cjni_notification, &ud);
 
@@ -1568,9 +1561,8 @@ static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
 
   if (type == CB_TYPE_MATCH)
   {
-    match_proc_t m_proc;
+    match_proc_t m_proc = { 0 };
 
-    memset (&m_proc, 0, sizeof (m_proc));
     m_proc.create  = cjni_match_target_create;
     m_proc.destroy = cjni_match_target_destroy;
     m_proc.match   = (void *) cjni_match_target_invoke;
@@ -1579,9 +1571,8 @@ static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
   }
   else if (type == CB_TYPE_TARGET)
   {
-    target_proc_t t_proc;
+    target_proc_t t_proc = { 0 };
 
-    memset (&t_proc, 0, sizeof (t_proc));
     t_proc.create  = cjni_match_target_create;
     t_proc.destroy = cjni_match_target_destroy;
     t_proc.invoke  = cjni_match_target_invoke;
@@ -1801,14 +1792,13 @@ static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{
     return (NULL);
   }
 
-  cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
+  cbi = calloc (1, sizeof (*cbi));
   if (cbi == NULL)
   {
-    ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
+    ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
     (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
     return (NULL);
   }
-  memset (cbi, 0, sizeof (*cbi));
   cbi->type = type;
 
   cbi->name = strdup (c_name);
@@ -1906,7 +1896,7 @@ static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
 
   pthread_mutex_lock (&java_callbacks_lock);
 
-  tmp = (cjni_callback_info_t *) realloc (java_callbacks,
+  tmp = realloc (java_callbacks,
       (java_callbacks_num + 1) * sizeof (*java_callbacks));
   if (tmp == NULL)
   {
@@ -1987,11 +1977,10 @@ static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
 static int cjni_create_jvm (void) /* {{{ */
 {
   JNIEnv *jvm_env;
-  JavaVMInitArgs vm_args;
+  JavaVMInitArgs vm_args = { 0 };
   JavaVMOption vm_options[jvm_argc];
 
   int status;
-  size_t i;
 
   if (jvm != NULL)
     return (0);
@@ -2006,12 +1995,11 @@ static int cjni_create_jvm (void) /* {{{ */
 
   jvm_env = NULL;
 
-  memset (&vm_args, 0, sizeof (vm_args));
   vm_args.version = JNI_VERSION_1_2;
   vm_args.options = vm_options;
   vm_args.nOptions = (jint) jvm_argc;
 
-  for (i = 0; i < jvm_argc; i++)
+  for (size_t i = 0; i < jvm_argc; i++)
   {
     DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
         i, jvm_argv[i]);
@@ -2067,13 +2055,12 @@ static JNIEnv *cjni_thread_attach (void) /* {{{ */
   if (cjni_env == NULL)
   {
     /* This pointer is free'd in `cjni_jvm_env_destroy'. */
-    cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
+    cjni_env = calloc (1, sizeof (*cjni_env));
     if (cjni_env == NULL)
     {
-      ERROR ("java plugin: cjni_thread_attach: malloc failed.");
+      ERROR ("java plugin: cjni_thread_attach: calloc failed.");
       return (NULL);
     }
-    memset (cjni_env, 0, sizeof (*cjni_env));
     cjni_env->reference_counter = 0;
     cjni_env->jvm_env = NULL;
 
@@ -2088,11 +2075,10 @@ static JNIEnv *cjni_thread_attach (void) /* {{{ */
   else
   {
     int status;
-    JavaVMAttachArgs args;
+    JavaVMAttachArgs args = { 0 };
 
     assert (cjni_env->jvm_env == NULL);
 
-    memset (&args, 0, sizeof (args));
     args.version = JNI_VERSION_1_2;
 
     status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
@@ -2169,7 +2155,7 @@ static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
     return (-1);
   }
 
-  tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
+  tmp = realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
   if (tmp == NULL)
   {
     ERROR ("java plugin: realloc failed.");
@@ -2205,7 +2191,7 @@ static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
   if (jvm_env == NULL)
     return (-1);
 
-  class = (java_plugin_class_t *) realloc (java_classes_list,
+  class = realloc (java_classes_list,
       (java_classes_list_len + 1) * sizeof (*java_classes_list));
   if (class == NULL)
   {
@@ -2230,8 +2216,7 @@ static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
   { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
        thorough the Java community, but (Sun's) `FindClass' and friends need
        slashes. */
-    size_t i;
-    for (i = 0; class->name[i] != 0; i++)
+    for (size_t i = 0; class->name[i] != 0; i++)
       if (class->name[i] == '.')
         class->name[i] = '/';
   }
@@ -2289,7 +2274,6 @@ static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
   cjni_callback_info_t *cbi;
   jobject o_ocitem;
   const char *name;
-  size_t i;
 
   jclass class;
   jmethodID method;
@@ -2304,7 +2288,7 @@ static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
   name = ci->values[0].value.string;
 
   cbi = NULL;
-  for (i = 0; i < java_callbacks_num; i++)
+  for (size_t i = 0; i < java_callbacks_num; i++)
   {
     if (java_callbacks[i].type != CB_TYPE_CONFIG)
       continue;
@@ -2356,12 +2340,11 @@ static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
   int success;
   int errors;
   int status;
-  int i;
 
   success = 0;
   errors = 0;
 
-  for (i = 0; i < ci->children_num; i++)
+  for (int i = 0; i < ci->children_num; i++)
   {
     oconfig_item_t *child = ci->children + i;
 
@@ -2724,7 +2707,6 @@ static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
   jobject o_ci;
   jobject o_tmp;
   int type;
-  size_t i;
 
   cbi_ret = NULL;
   o_ci = NULL;
@@ -2769,7 +2751,7 @@ static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
 
   /* Lets see if we have a matching factory here.. */
   cbi_factory = NULL;
-  for (i = 0; i < java_callbacks_num; i++)
+  for (size_t i = 0; i < java_callbacks_num; i++)
   {
     if (java_callbacks[i].type != type)
       continue;
@@ -2801,13 +2783,13 @@ static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
 
   /* Allocate a new callback info structure. This is going to be our user_data
    * pointer. */
-  cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
+  cbi_ret = calloc (1, sizeof (*cbi_ret));
   if (cbi_ret == NULL)
   {
-    ERROR ("java plugin: cjni_match_target_create: malloc failed.");
+    ERROR ("java plugin: cjni_match_target_create: calloc failed.");
     BAIL_OUT (-1);
   }
-  memset (cbi_ret, 0, sizeof (*cbi_ret));
+
   cbi_ret->object = NULL;
   cbi_ret->type = type;
 
@@ -2923,9 +2905,8 @@ static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
    * `value_list_t'. */
   if (cbi->type == CB_TYPE_TARGET)
   {
-    value_list_t new_vl;
+    value_list_t new_vl = { 0 };
 
-    memset (&new_vl, 0, sizeof (new_vl));
     status = jtoc_value_list (jvm_env, &new_vl, o_vl);
     if (status != 0)
     {
@@ -2952,9 +2933,8 @@ static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
 {
   int status;
-  size_t i;
 
-  for (i = 0; i < java_callbacks_num; i++)
+  for (size_t i = 0; i < java_callbacks_num; i++)
   {
     if (java_callbacks[i].type != CB_TYPE_INIT)
       continue;
@@ -2979,9 +2959,8 @@ static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
 {
   int status;
-  size_t i;
 
-  for (i = 0; i < java_callbacks_num; i++)
+  for (size_t i = 0; i < java_callbacks_num; i++)
   {
     if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
       continue;
@@ -3004,15 +2983,13 @@ static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
 static int cjni_shutdown (void) /* {{{ */
 {
   JNIEnv *jvm_env;
-  JavaVMAttachArgs args;
+  JavaVMAttachArgs args = { 0 };
   int status;
-  size_t i;
 
   if (jvm == NULL)
     return (0);
 
   jvm_env = NULL;
-  memset (&args, 0, sizeof (args));
   args.version = JNI_VERSION_1_2;
 
   status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
@@ -3027,7 +3004,7 @@ static int cjni_shutdown (void) /* {{{ */
   cjni_shutdown_plugins (jvm_env);
 
   /* Release all the global references to callback functions */
-  for (i = 0; i < java_callbacks_num; i++)
+  for (size_t i = 0; i < java_callbacks_num; i++)
   {
     if (java_callbacks[i].object != NULL)
     {
@@ -3040,7 +3017,7 @@ static int cjni_shutdown (void) /* {{{ */
   sfree (java_callbacks);
 
   /* Release all the global references to directly loaded classes. */
-  for (i = 0; i < java_classes_list_len; i++)
+  for (size_t i = 0; i < java_classes_list_len; i++)
   {
     if (java_classes_list[i].object != NULL)
     {
@@ -3061,7 +3038,7 @@ static int cjni_shutdown (void) /* {{{ */
   pthread_key_delete (jvm_env_key);
 
   /* Free the JVM argument list */
-  for (i = 0; i < jvm_argc; i++)
+  for (size_t i = 0; i < jvm_argc; i++)
     sfree (jvm_argv[i]);
   jvm_argc = 0;
   sfree (jvm_argv);