From 574daaec127415ba0b48446724b6f59914144db7 Mon Sep 17 00:00:00 2001 From: Antonio Gelain Date: Fri, 20 Oct 2023 19:32:07 +0200 Subject: [PATCH] feat(error-utils): add functions for utility --- error-utils/error_utils.c | 82 +++++++++++++++++++++++++++++++++++++++ error-utils/error_utils.h | 33 ++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/error-utils/error_utils.c b/error-utils/error_utils.c index edcfcea..d3d8256 100644 --- a/error-utils/error_utils.c +++ b/error-utils/error_utils.c @@ -555,3 +555,85 @@ size_t error_utils_expired_count(ErrorUtilsHandler * handler) { return 0; return handler->expired; } +bool error_utils_is_running( + ErrorUtilsHandler * handler, + uint32_t error, + ErrorUtilsInstance instance, + bool is_string) +{ + if (handler == NULL) + return false; + + // Get error instance index + uint32_t index = _error_utils_hash(error, instance, is_string, handler->buffer_size); + ErrorUtilsRunningInstance * err = (handler->errors + index); + + // Iterate until the error is found or the buffer is full and the error is not present + size_t off = 0; + for (; off < handler->buffer_size; ++off) { + // Check if the error is initialized + if (_error_utils_hash_is_free(err)) { + // Exit from the critical section + CS_EXIT(); + return false; + } + + // Check if the error is already set + if (_error_utils_is_equal(err, error, instance, is_string)) { + bool is_running = err->is_running; + + // Exit from the critical section + CS_EXIT(); + return is_running; + } + + // Update index + index = _error_utils_hash_probe(index, off, handler->buffer_size); + err = (handler->errors + index); + } + + // Exit from the critical section + CS_EXIT(); + return false; +} +bool error_utils_is_expired( + ErrorUtilsHandler * handler, + uint32_t error, + ErrorUtilsInstance instance, + bool is_string) +{ + if (handler == NULL) + return false; + + // Get error instance index + uint32_t index = _error_utils_hash(error, instance, is_string, handler->buffer_size); + ErrorUtilsRunningInstance * err = (handler->errors + index); + + // Iterate until the error is found or the buffer is full and the error is not present + size_t off = 0; + for (; off < handler->buffer_size; ++off) { + // Check if the error is initialized + if (_error_utils_hash_is_free(err)) { + // Exit from the critical section + CS_EXIT(); + return false; + } + + // Check if the error is already set + if (_error_utils_is_equal(err, error, instance, is_string)) { + bool is_expired = err->is_expired; + + // Exit from the critical section + CS_EXIT(); + return is_expired; + } + + // Update index + index = _error_utils_hash_probe(index, off, handler->buffer_size); + err = (handler->errors + index); + } + + // Exit from the critical section + CS_EXIT(); + return false; +} diff --git a/error-utils/error_utils.h b/error-utils/error_utils.h index 85bc695..8142cca 100644 --- a/error-utils/error_utils.h +++ b/error-utils/error_utils.h @@ -43,6 +43,7 @@ #ifndef ERROR_UTILS_H #define ERROR_UTILS_H +#include #include #include #include @@ -281,5 +282,37 @@ size_t error_utils_running_count(ErrorUtilsHandler * handler); * @return size_t The number of expired errors */ size_t error_utils_expired_count(ErrorUtilsHandler * handler); +/** + * @brief Check if an error is running + * + * @param handler The error handler structure + * @param error The error type + * @param instance The error instance + * @param is_string True if the instance is a string, otherwise it is an integer + * @return True if the error is running + * @return False otherwise + */ +bool error_utils_is_running( + ErrorUtilsHandler * handler, + uint32_t error, + ErrorUtilsInstance instance, + bool is_string +); +/** + * @brief Check if an error is expired + * + * @param handler The error handler structure + * @param error The error type + * @param instance The error instance + * @param is_string True if the instance is a string, otherwise it is an integer + * @return True if the error is expired + * @return False otherwise + */ +bool error_utils_is_expired( + ErrorUtilsHandler * handler, + uint32_t error, + ErrorUtilsInstance instance, + bool is_string +); #endif // ERROR_UTILS_H