Commit e964e69e authored by Swann Perarnau's avatar Swann Perarnau

[refactor] change vector to alloc data out of it

We need the vector type used for all our "command queues" to be able to
maintain pointers to the inner data structure across reallocs of the
vector itself. The resulting change just uses independent callocs for
each pointer to a data element.
parent d2efb479
......@@ -16,16 +16,17 @@
* @brief AML Vector API
*
* Generic vector type:
* Vector of nbelems, each of size sz, with a comparison key at offset off
* Vector of nbelems, each of size sz, with a comparison key at offset off.
* Elements are not stored directly in the vector, but are allocated one by one.
* @{
**/
/** Pointer to the key within element "e" of a vector "v". **/
#define AML_VECTOR_ELTKEY_P(v, e) ((int *)(((intptr_t) e) + v->off))
/** Pointer to the key within element index "i" of a vector "v". **/
#define AML_VECTOR_KEY_P(v, i) ((int *)(((intptr_t) v->ptr) + i*v->sz + v->off))
#define AML_VECTOR_KEY_P(v, i) ((int *)(((intptr_t) v->ptr[i]) + v->off))
/** Pointer to the element index "i" of a vector "v". **/
#define AML_VECTOR_ELT_P(v, i) ((void *)(((intptr_t) v->ptr) + i*v->sz))
#define AML_VECTOR_ELT_P(v, i) (v->ptr[i])
/** AML vector structure **/
struct aml_vector {
......@@ -40,8 +41,8 @@ struct aml_vector {
* key (of type int) is stored.
**/
size_t off;
/** Pointer to elements in vector **/
void *ptr;
/** array of element pointers in the vector **/
void **ptr;
};
/**
......@@ -59,6 +60,13 @@ size_t aml_vector_size(const struct aml_vector *vector);
* @return a pointer to the requested element.
**/
void *aml_vector_get(struct aml_vector *vector, int index);
/**
* Provides the index of element "elem" within the vector.
* @param vector: an initialized vector structure.
* @param elem: a valid element within "vector".
* @return the index to the element or "na" if not found.
**/
int aml_vector_getid(struct aml_vector *vector, void *elem);
/**
* Find the first element with a particular key.
* @param vector: an initialized vector structure.
......
......@@ -30,6 +30,8 @@ int aml_vector_resize(struct aml_vector *vec, size_t newsize)
vec->ptr = realloc(vec->ptr, newsize * vec->sz);
assert(vec->ptr != NULL);
for (size_t i = vec->nbelems; i < newsize; i++) {
vec->ptr[i] = calloc(1, vec->sz);
assert(vec->ptr[i] != NULL);
int *k = AML_VECTOR_KEY_P(vec, i);
*k = vec->na;
}
......@@ -58,6 +60,17 @@ void *aml_vector_get(struct aml_vector *vec, int id)
return NULL;
}
int aml_vector_getid(struct aml_vector *vec, void *elem)
{
assert(vec != NULL);
assert(elem != NULL);
for (size_t i = 0; i < vec->nbelems; i++)
if (vec->ptr[i] == elem)
return i;
return vec->na;
}
/* return index of first element with key */
int aml_vector_find(const struct aml_vector *vec, int key)
{
......@@ -88,7 +101,6 @@ void aml_vector_remove(struct aml_vector *vec, void *elem)
{
assert(vec != NULL);
assert(elem != NULL);
assert(elem >= vec->ptr && elem < AML_VECTOR_ELT_P(vec, vec->nbelems));
int *k = AML_VECTOR_ELTKEY_P(vec, elem);
*k = vec->na;
......@@ -102,7 +114,7 @@ int aml_vector_create(struct aml_vector **vec, size_t reserve, size_t size,
size_t key, int na)
{
struct aml_vector *ret = NULL;
void *ptr;
void **ptr;
if (vec == NULL)
return -AML_EINVAL;
......@@ -113,13 +125,25 @@ int aml_vector_create(struct aml_vector **vec, size_t reserve, size_t size,
return -AML_ENOMEM;
}
ptr = calloc(reserve, size);
ptr = calloc(reserve, sizeof(void *));
if (ptr == NULL) {
free(ret);
*vec = NULL;
return -AML_ENOMEM;
}
for (size_t i = 0; i < reserve; i++) {
ptr[i] = calloc(1, size);
if (ptr[i] == NULL) {
/* avoid issues with size_t and negative values */
for (size_t j = 0; j + 1 <= i; j++)
free(ptr[i]);
free(ret);
*vec = NULL;
return -AML_ENOMEM;
}
}
ret->sz = size;
ret->off = key;
ret->na = na;
......@@ -145,6 +169,9 @@ void aml_vector_destroy(struct aml_vector **vec)
if (v == NULL)
return;
for (size_t i = 0; i < v->nbelems; i++)
free(v->ptr[i]);
free(v->ptr);
free(v);
*vec = NULL;
......
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