Skip to content

Commit

Permalink
feat(error-utils): add functions for utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonidotpy committed Oct 20, 2023
1 parent ed28dd0 commit 574daae
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
82 changes: 82 additions & 0 deletions error-utils/error_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
33 changes: 33 additions & 0 deletions error-utils/error_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#ifndef ERROR_UTILS_H
#define ERROR_UTILS_H

#include <cstdint>
#include <inttypes.h>
#include <stddef.h>
#include <stdbool.h>
Expand Down Expand Up @@ -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

0 comments on commit 574daae

Please sign in to comment.