We need to have initialization separate, so split up the rate_alloc().
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/rate.c | 16 +++++++++++++---
src/rate.h | 1 +
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/src/rate.c b/src/rate.c
index c60c70a..98c4cf2 100644
--- a/src/rate.c
+++ b/src/rate.c
@@ -4,15 +4,25 @@
#include "iptraf-ng-compat.h"
#include "rate.h"
-void rate_alloc(struct rate *rate, unsigned int n)
+void rate_init(struct rate *rate)
{
if (!rate)
return;
- rate->n = n;
rate->index = 0;
rate->sma = 0;
- rate->rates = xmallocz(n * sizeof(*rate->rates));
+ memset(rate->rates, 0, rate->n * sizeof(rate->rates[0]));
+}
+
+void rate_alloc(struct rate *rate, unsigned int n)
+{
+ if (!rate)
+ return;
+
+ rate->n = n;
+ rate->rates = xmalloc(n * sizeof(rate->rates[0]));
+
+ rate_init(rate);
}
void rate_destroy(struct rate *rate)
diff --git a/src/rate.h b/src/rate.h
index 87f476d..266dd28 100644
--- a/src/rate.h
+++ b/src/rate.h
@@ -23,6 +23,7 @@ struct rate {
unsigned long sma; /* simple moving average */
};
+void rate_init(struct rate *rate);
void rate_alloc(struct rate *rate, unsigned int n);
void rate_destroy(struct rate *rate);
void rate_add_rate(struct rate *rate, unsigned long bytes,
--
1.7.8.4