summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfumingwei <[email protected]>2023-03-23 15:26:50 +0800
committerfumingwei <[email protected]>2023-03-23 15:31:48 +0800
commit0999ff92c5ac2214365416054546de4b1776f206 (patch)
treeb1f7ed50a90ba83ca3e2bf019b7d9edbe51ab913
parent08a8907cdb1f7537d95ee144f947a743e2ae697d (diff)
feature:修改calloc调用方式
-rw-r--r--src/fieldstat.cpp16
-rw-r--r--src/fieldstat_dynamic.cpp12
-rw-r--r--src/file_output.cpp29
-rw-r--r--src/prometheus_output.cpp6
4 files changed, 41 insertions, 22 deletions
diff --git a/src/fieldstat.cpp b/src/fieldstat.cpp
index cdb7de9..ddbe054 100644
--- a/src/fieldstat.cpp
+++ b/src/fieldstat.cpp
@@ -28,7 +28,7 @@ static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
char* __str_dup(const char* str)
{
char* dup=NULL;
- dup=(char*)calloc(sizeof(char),strlen(str)+1);
+ dup=(char*)calloc(strlen(str)+1, sizeof(char));
memcpy(dup, str, strlen(str));
return dup;
}
@@ -155,7 +155,7 @@ void add_tags_to_metric(const struct fieldstat_tag tags[], size_t n_tag, char *t
struct metric * metric_new(enum field_type type, const char *field_name, const struct fieldstat_tag tags[], size_t n_tag)
{
- struct metric *metric = (struct metric*)calloc(sizeof(struct metric),1);
+ struct metric *metric = (struct metric*)calloc(1, sizeof(struct metric));
metric->field_name = __str_dup(field_name);
metric->field_type = type;
metric->is_ratio = 0;
@@ -209,7 +209,7 @@ struct metric ** read_metric_slot(struct fieldstat_instance *instance, int metri
if(in_block_index == 0)
{
assert(instance->metric_block_list[block_index] == NULL);
- instance->metric_block_list[block_index] = (struct metric **)calloc(sizeof(struct metric *), NUM_INIT_METRICS);
+ instance->metric_block_list[block_index] = (struct metric **)calloc(NUM_INIT_METRICS, sizeof(struct metric *));
}
else
{
@@ -544,7 +544,7 @@ struct fieldstat_instance * fieldstat_instance_new(const char *name)
return NULL;
}
- instance = (struct fieldstat_instance *)calloc(sizeof(struct fieldstat_instance),1);
+ instance = (struct fieldstat_instance *)calloc(1, sizeof(struct fieldstat_instance));
strcpy(instance->name, name);
instance->running = 0;
instance->output_interval_ms = 2000;
@@ -615,7 +615,7 @@ void fieldstat_instance_free(struct fieldstat_instance *instance)
struct table_metric* table_metric_new(const char *name, const char *column_name[], enum field_type column_type[], size_t n_column)
{
int i = 0;
- struct table_metric *table_metric = (struct table_metric *)calloc(sizeof(struct table_metric), 1);
+ struct table_metric *table_metric = (struct table_metric *)calloc(1, sizeof(struct table_metric));
table_metric->column_cnt = (int)n_column;
table_metric->name = __str_dup(name);
@@ -720,7 +720,7 @@ struct table_line ** read_table_line_slot(struct table_metric *table, int line_i
if(in_block_index == 0)
{
assert(table->line_block[block_index] == NULL);
- table->line_block[block_index] = (struct table_line **)calloc(sizeof(struct table_line *), NUM_INIT_METRICS);
+ table->line_block[block_index] = (struct table_line **)calloc(NUM_INIT_METRICS, sizeof(struct table_line *));
}
else
{
@@ -733,7 +733,7 @@ struct table_line ** read_table_line_slot(struct table_metric *table, int line_i
struct table_line *table_line_new(const char *name, const struct fieldstat_tag tags[],size_t n_tag)
{
- struct table_line *table_line = (struct table_line *)calloc(sizeof(struct table_line), 1);
+ struct table_line *table_line = (struct table_line *)calloc(1, sizeof(struct table_line));
table_line->name = __str_dup(name);
table_line->n_tag = n_tag;
@@ -911,7 +911,7 @@ static int parse_histogram_bin_format(const char* format, double **output_bins,
comma_num++;
}
}
- bins=(double*)calloc(sizeof(double),comma_num+1);
+ bins=(double*)calloc(comma_num+1, sizeof(double));
for (token = dup_format,i=0; ; token= NULL, i++)
{
sub_token= strtok_r(token,",", &saveptr);
diff --git a/src/fieldstat_dynamic.cpp b/src/fieldstat_dynamic.cpp
index 72f3155..8495a38 100644
--- a/src/fieldstat_dynamic.cpp
+++ b/src/fieldstat_dynamic.cpp
@@ -14,7 +14,7 @@ struct fieldstat_dynamic_instance * fieldstat_dynamic_instance_new(const char *n
return NULL;
}
- instance = (struct fieldstat_dynamic_instance *)calloc(sizeof(struct fieldstat_dynamic_instance), 1);
+ instance = (struct fieldstat_dynamic_instance *)calloc(1, sizeof(struct fieldstat_dynamic_instance));
strcpy(instance->name, name);
instance->running = 0;
@@ -22,7 +22,7 @@ struct fieldstat_dynamic_instance * fieldstat_dynamic_instance_new(const char *n
instance->background_thread_disable = 0;
instance->n_thread = n_thread;
- instance->n_thread_dynamic_metric = (struct dynamic_metric **)calloc(sizeof(struct dynamic_metric *), instance->n_thread);
+ instance->n_thread_dynamic_metric = (struct dynamic_metric **)calloc(instance->n_thread, sizeof(struct dynamic_metric *));
return instance;
}
@@ -296,12 +296,12 @@ static struct metric * create_dynamic_table_metric(struct fieldstat_dynamic_inst
head = &instance->n_thread_dynamic_metric[thread_id];
- value = (struct dynamic_metric *)calloc(sizeof(struct dynamic_metric), 1);
+ value = (struct dynamic_metric *)calloc(1, sizeof(struct dynamic_metric));
build_dynamic_metric_key(table_id, row_name, tags, n_tags, sizeof(value->metric_key), value->metric_key);
table = instance->table_metrics[table_id];
- value->metrics = (struct metric **)calloc(sizeof(struct metric *), table->column_cnt);
+ value->metrics = (struct metric **)calloc(table->column_cnt, sizeof(struct metric *));
for(i = 0; i < table->column_cnt; i ++)
{
metric = metric_new(table->column_type[i], row_name, tags, n_tags);
@@ -344,10 +344,10 @@ static struct metric * create_dynamic_metric(struct fieldstat_dynamic_instance *
head = &instance->n_thread_dynamic_metric[thread_id];
- insert = (struct dynamic_metric *)calloc(sizeof(struct dynamic_metric), 1);
+ insert = (struct dynamic_metric *)calloc(1, sizeof(struct dynamic_metric));
build_dynamic_metric_key(-1, field_name, tags, n_tags, sizeof(insert->metric_key), insert->metric_key);
- insert->metrics = (struct metric **)calloc(sizeof(struct metric *), 1);
+ insert->metrics = (struct metric **)calloc(1, sizeof(struct metric *));
metric = metric_new(type, field_name, tags, n_tags);
switch(metric->field_type)
diff --git a/src/file_output.cpp b/src/file_output.cpp
index 7289f28..27187f3 100644
--- a/src/file_output.cpp
+++ b/src/file_output.cpp
@@ -403,8 +403,13 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta
char *pos = print_buf;
//display_manifest_t* p=NULL;
struct metric *metric = NULL;
- struct metric *metric_array[current_metric_cnt];
- int metric_is_print[current_metric_cnt];
+ struct metric **metric_array = NULL;
+ int *printed_metric_id = NULL;
+
+ if(current_metric_cnt < 1)
+ {
+ return 0;
+ }
if(instance->histogram_cnt == 0
&& instance->summary_cnt == 0)
@@ -412,6 +417,9 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta
return 0;
}
+ metric_array = (struct metric **)calloc(current_metric_cnt, sizeof(struct metric *));
+ printed_metric_id = (int *)calloc(current_metric_cnt, sizeof(int));
+
for(i = 0; i < current_metric_cnt; i ++)
{
metric = get_metric(instance, i);
@@ -429,7 +437,7 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta
for(i = 0; i < metric_num; i++)
{
- if(metric_is_print[i] == 1)
+ if(printed_metric_id[i] == 1)
{
continue;
}
@@ -449,7 +457,7 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta
continue;
}
pos += output_file_print_hdr_unit(metric_array[j], pos, size-(pos-print_buf));
- metric_is_print[j] = 1;
+ printed_metric_id[j] = 1;
}
if(pos-print_buf>0)
{
@@ -460,6 +468,17 @@ static int output_file_format_default_type_histogram_and_summary(struct fieldsta
pos+=snprintf(pos,size-(pos-print_buf),"\n%s\n", draw_line);
}
}
+ if(metric_array)
+ {
+ free(metric_array);
+ metric_array = NULL;
+ }
+ if(printed_metric_id)
+ {
+ free(printed_metric_id);
+ printed_metric_id = NULL;
+ }
+
return pos-print_buf;
}
@@ -491,7 +510,7 @@ int fieldstat_output_file(struct fieldstat_instance *instance,long long interval
{
time(&current);
ctime_r(&current, ctime_buff);
- print_buf = (char*)calloc(sizeof(char), print_buf_sz);
+ print_buf = (char*)calloc(print_buf_sz, sizeof(char));
append_pos = print_buf;
append_pos += snprintf(append_pos, print_buf_sz - (append_pos - print_buf), "%s%s", draw_boundary, ctime_buff);
append_pos --;//jump '\n' generate by ctime()
diff --git a/src/prometheus_output.cpp b/src/prometheus_output.cpp
index 96016c1..13a0355 100644
--- a/src/prometheus_output.cpp
+++ b/src/prometheus_output.cpp
@@ -103,7 +103,7 @@ static void prometheus_output_uri_list(struct prometheus_endpoint_instance *prom
{
payload_len = prometheus_output->fs_instance_cnt * 128; //TODO using marco, len?
printf("payload_len =%d\n, n_instance =%d\n", payload_len, prometheus_output->fs_instance_cnt);
- payload = (char *)calloc(1,payload_len);
+ payload = (char *)calloc(payload_len, sizeof(char));
payload_append_position = payload;
payload_append_position += snprintf(payload_append_position, payload_len - (payload_append_position - payload),"url_path:\n\t%s\n", prometheus_output->url_path);
@@ -114,7 +114,7 @@ static void prometheus_output_uri_list(struct prometheus_endpoint_instance *prom
}
else
{
- payload = (char *)calloc(1, 128);
+ payload = (char *)calloc(128, sizeof(char));
strncpy(payload, "Not Invaild instance\n", 128);
}
response = http_response_init();
@@ -424,7 +424,7 @@ int fieldstat_global_enable_prometheus_endpoint(unsigned short listen_port, cons
g_prometheus_endpoint_instance.url_path = url_path == NULL ? strdup(PROMETHEUS_ENDPOINT_DEFAULT_URL):strdup(url_path);
g_prometheus_endpoint_instance.port = listen_port;
g_prometheus_endpoint_instance.running = 1;
- g_prometheus_endpoint_instance.fs_instance = (struct fieldstat_instance **)calloc( sizeof(struct fieldstat_instance *), g_prometheus_endpoint_instance.fs_instance_size);
+ g_prometheus_endpoint_instance.fs_instance = (struct fieldstat_instance **)calloc(g_prometheus_endpoint_instance.fs_instance_size, sizeof(struct fieldstat_instance *));
pthread_create(&g_prometheus_endpoint_instance.tid, NULL, prometheus_endpoint_listen_thread_entry, (void *)&g_prometheus_endpoint_instance);
g_prometheus_endpoint_instance.thread_created = 1;
return 0;