libs/libhutils: add alignment functions

Add functions to align a given value to a given alignment and to
calculate the necessary padding to align the value.
parent f09be5d1
...@@ -87,6 +87,13 @@ char *hutils_concat_strings_no_sep (const char *str1, const char* str2); ...@@ -87,6 +87,13 @@ char *hutils_concat_strings_no_sep (const char *str1, const char* str2);
char *hutils_concat_strings3 (const char *str1, const char* str2, char *hutils_concat_strings3 (const char *str1, const char* str2,
const char* str3, char sep); const char* str3, char sep);
/* Calculates necessary padding so that a given value is a multiple of a given
* alignment */
uint32_t hutils_calculate_padding(uint32_t value, uint32_t alignment);
/* Aligns a given value to a given alignment */
uint32_t hutils_align_value(uint32_t value, uint32_t alignment);
/* Spawns (fork and exec) a new process. Returns, for the parent process, -1 /* Spawns (fork and exec) a new process. Returns, for the parent process, -1
* in case of error and child's PID (> 0) if success. For the child process, * in case of error and child's PID (> 0) if success. For the child process,
* returns -1 in case of error and 0 in case of success */ * returns -1 in case of error and 0 in case of success */
......
...@@ -177,6 +177,24 @@ char *hutils_concat_strings3 (const char *str1, const char* str2, ...@@ -177,6 +177,24 @@ char *hutils_concat_strings3 (const char *str1, const char* str2,
return _hutils_concat_strings_raw (str1, str2, str3, true, sep); return _hutils_concat_strings_raw (str1, str2, str3, true, sep);
} }
/*******************************************************************/
/***************** Byte manipulation functions ********************/
/*******************************************************************/
uint32_t hutils_calculate_padding(uint32_t value, uint32_t alignment)
{
uint32_t exceeded = value % alignment;
uint32_t remaining = alignment - exceeded;
uint32_t padding = remaining % alignment;
return padding;
}
uint32_t hutils_align_value(uint32_t value, uint32_t alignment)
{
return value + hutils_calculate_padding(value, alignment);
}
/*******************************************************************/ /*******************************************************************/
/***************** System Fork/Exec functions *********************/ /***************** System Fork/Exec functions *********************/
/*******************************************************************/ /*******************************************************************/
...@@ -502,4 +520,3 @@ err_hash_item_alloc: ...@@ -502,4 +520,3 @@ err_hash_item_alloc:
err_cfg_exit: err_cfg_exit:
return err; return err;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment