From 4e3240b6da9db26041f9b07e3c31349e42a6a930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Tue, 19 Jan 2021 17:49:09 +0100 Subject: [PATCH] - Added missing pieces for counters implementation - Added comment - Added function prototypes - Change invocation of non-existed function to correct invocation - Added assertions, when needed - Remove bug caused returning integer instead of pointer. Additionally, previously function returns given parameter instead of pointer to struct this parameter is related to. - Correct documentation and code formating - Assertions added diff --git a/common/counters.c b/common/counters.c index df3f4fbaa4..6a5e2a5159 100644 --- a/common/counters.c +++ b/common/counters.c @@ -15,6 +15,8 @@ #include #endif +#include "fcintl.h" + #include "counters.h" @@ -51,3 +53,78 @@ void counters_init(void) void counters_free(void) { } + +/************************************************************************//** + Return counter by given id +****************************************************************************/ +struct counter *counter_by_id(int id) +{ + int i; + + fc_assert_ret_val(id >= MAX_COUNTERS, NULL); + + for (i = 0; i < MAX_COUNTERS; i++) { + + if (counters[i].id == id) { + + return &counters[i]; + } + } + + return NULL; +} + +/************************************************************************//** + Return id of a given counter +****************************************************************************/ +int counter_id(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, -1); + return pcount->id; +} + +/************************************************************************//** + Search for counter by rule name + (return matched counter if found or NULL) +****************************************************************************/ +struct counter *counter_by_rule_name(const char *name) +{ + int i; + fc_assert_ret_val(NULL == name, NULL); + fc_assert_ret_val('\0' == name[0], NULL); + + for (i = 0; i < MAX_COUNTERS; i++) { + + if (0 == fc_strcasecmp(name, counters[i].rule_name)) { + + return &counters[i]; + } + } + + return NULL; +} + +/************************************************************************//** + Return rule name of a given counter +****************************************************************************/ +const char *counter_rule_name(struct counter *pcount) +{ + return pcount->rule_name; +} + +/************************************************************************//** + Return index in global counter's array +****************************************************************************/ +int counter_index(struct counter *pcount) +{ + fc_assert_ret_val(NULL != pcount, -1); + return pcount - counters; +} + +/************************************************************************//** + Return counter by given index +****************************************************************************/ +struct counter *counter_by_index(int index) +{ + return &counters[index]; +} diff --git a/common/counters.h b/common/counters.h index e4a160751a..9b54bfa13c 100644 --- a/common/counters.h +++ b/common/counters.h @@ -36,6 +36,15 @@ struct counter void counters_init(void); void counters_free(void); +struct counter *counter_by_id(int id); +int counter_id(struct counter *pcount); + +struct counter *counter_by_rule_name(const char *name); +const char *counter_rule_name(struct counter *pcount); + +int counter_index(struct counter *pcount); +struct counter *counter_by_index(int index); + #ifdef __cplusplus } #endif /* __cplusplus */ -- 2.30.0