Commit 5c9af3d3 authored by Chad Versace's avatar Chad Versace

core: Add func wcore_attrib_list_from_int32

This function converts an int32_t attribute list to an intptr_t
attribute list. It will be needed in the future when updating
waffle_config_choose() to accept an intptr_t attribute list.
Signed-off-by: default avatarChad Versace <chad.versace@intel.com>
Tested-by: Emil Velikov <emil.l.velikov@gmail.com> (msvc/wgl)
Reviewed-by: Emil Velikov's avatarEmil Velikov <emil.l.velikov@gmail.com>
parent 463bddf5
......@@ -29,6 +29,9 @@
#include <stdint.h>
#include <stddef.h>
#include "wcore_error.h"
#include "wcore_util.h"
#define WCORE_ATTRIB_LIST_COMMON_FUNCS(T, \
length_func, \
get_func, \
......@@ -119,3 +122,48 @@ WCORE_ATTRIB_LIST_COMMON_FUNCS(intptr_t,
wcore_attrib_list_get,
wcore_attrib_list_get_with_default,
wcore_attrib_list_update)
/// Given length of attribute list, calculate its size in bytes. Return false
/// on arithemtic overflow.
static bool
wcore_attrib_list_get_size(size_t *size, size_t len) {
bool ok;
ok = wcore_mul_size(size, 2, len);
ok &= wcore_iadd_size(size, 1);
ok &= wcore_imul_size(size, sizeof(intptr_t));
return ok;
}
intptr_t*
wcore_attrib_list_from_int32(const int32_t attrib_list32[])
{
size_t len = 0;
size_t size = 0;
intptr_t *attrib_list = NULL;
len = wcore_attrib_list32_length(attrib_list32);
if (!wcore_attrib_list_get_size(&size, len)) {
// Arithmetic overflow occurred, therefore we can't allocate the
// memory.
wcore_error(WAFFLE_ERROR_BAD_ALLOC);
return NULL;
}
attrib_list = wcore_malloc(size);
if (!attrib_list) {
return NULL;
}
// Copy all key/value pairs.
for (size_t i = 0; i < 2 * len; ++i) {
attrib_list[i] = attrib_list32[i];
}
// Add terminal null.
attrib_list[2 * len] = 0;
return attrib_list;
}
......@@ -30,6 +30,10 @@
#include <stdint.h>
#include <stdlib.h>
/// @brief Convert int32_t attribute list to an intptr_t attribute list.
intptr_t*
wcore_attrib_list_from_int32(const int32_t attrib_list[]);
size_t
wcore_attrib_list_length(const intptr_t attrib_list[]);
......
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