Commit 90b8f8bb authored by Swann Perarnau's avatar Swann Perarnau

[refactor] convert vector to new errors, create

Implement #31 for vector.
Improve error management.
Checkpatch vector.
parent 8797fca6
Pipeline #6738 passed with stages
in 4 minutes and 42 seconds
......@@ -22,6 +22,7 @@ include/aml/tiling/1d.h
include/aml/tiling/2d.h
include/aml/utils/bitmap.h
include/aml/utils/error.h
include/aml/utils/vector.h
src/aml.c
src/area/area.c
src/area/linux.c
......@@ -35,3 +36,4 @@ src/tiling/tiling.c
src/tiling/tiling_1d.c
src/tiling/tiling_2d.c
src/utils/error.c
src/utils/vector.c
......@@ -17,11 +17,11 @@
******************************************************************************/
/* Pointer to the key within element "e" of a vector "v". */
#define AML_VECTOR_ELTKEY_P(v,e) ((int *)(((intptr_t) e) + v->off))
#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->sz + 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) ((void *)(((intptr_t) v->ptr) + i*v->sz))
struct aml_vector {
int na;
......@@ -81,25 +81,43 @@ void *aml_vector_add(struct aml_vector *vector);
*/
void aml_vector_remove(struct aml_vector *vector, void *elem);
/**
* Allocate and Initialize a vector. Allocates elements and sets their keys to
* the "na" value.
*
* @param vector the address of a pointer to a struct aml_vector used as a
* return value.
* @param num the number of elements to allocate.
* @param size the size of each individual element.
* @param key the offset within each element where the key (of type int)
* is stored.
* @param na a "null" key value used to indicate an unused element.
* @return 0 if successful; an error code otherwise.
**/
int aml_vector_create(struct aml_vector **vector, size_t num, size_t size,
size_t key, int na);
/*
* Initializes a vector. Allocates elements and sets their keys to the "na"
* value.
* "vector": an allocated vector structure.
* "num": the number of elements to allocate.
* "size": the size of each individual element.
* "key": the offset within each element where the key (of type int) is stored.
* "na": a "null" key value used to indicate an unused element.
* Returns 0 if successful; an error code otherwise.
*/
* Initializes a vector. Similar to create.
*
* @param vector a vector declared with AML_VECTOR_DECL
**/
int aml_vector_init(struct aml_vector *vector, size_t num, size_t size,
size_t key, int na);
/**
* Finalize a vector. Frees all inner resources.
*
* @param vector a vector initialized by aml_vector_init.
**/
void aml_vector_fini(struct aml_vector *vector);
/*
* Tears down an initialized vector. Releases the memory buffer holding the
* elements.
* "vector": an initialized vector structure.
* Returns 0 if successful; an error code otherwise.
*/
int aml_vector_destroy(struct aml_vector *vector);
* Finalize and free a struct aml_vector.
*
* @param vector a vector created by aml_vector_create. NULL after return.
**/
void aml_vector_destroy(struct aml_vector **vector);
#endif //AML_VECTOR_H
......@@ -19,6 +19,6 @@
extern const int aml_version_major;
extern const int aml_version_minor;
extern const int aml_version_patch;
extern const char* aml_version_string;
extern const char *aml_version_string;
#endif /* AML_VERSION_H */
......@@ -268,7 +268,7 @@ void aml_dma_linux_par_fini(struct aml_dma *d)
free(req->thread_data);
}
aml_vector_destroy(&dma->data.requests);
aml_vector_fini(&dma->data.requests);
pthread_mutex_destroy(&dma->data.lock);
}
......
......@@ -210,7 +210,7 @@ void aml_dma_linux_seq_fini(struct aml_dma *d)
return;
struct aml_dma_linux_seq *dma = (struct aml_dma_linux_seq *)d->data;
aml_vector_destroy(&dma->data.requests);
aml_vector_fini(&dma->data.requests);
pthread_mutex_destroy(&dma->data.lock);
}
......
......@@ -319,8 +319,8 @@ void aml_scratch_par_fini(struct aml_scratch *d)
if (d == NULL)
return;
scratch = (struct aml_scratch_par *)d->data;
aml_vector_destroy(&scratch->data.requests);
aml_vector_destroy(&scratch->data.tilemap);
aml_vector_fini(&scratch->data.requests);
aml_vector_fini(&scratch->data.tilemap);
aml_area_munmap(scratch->data.sch_area,
scratch->data.sch_ptr,
scratch->data.scratch_size);
......
......@@ -320,8 +320,8 @@ void aml_scratch_seq_fini(struct aml_scratch *d)
if (d == NULL)
return;
scratch = (struct aml_scratch_seq *)d->data;
aml_vector_destroy(&scratch->data.requests);
aml_vector_destroy(&scratch->data.tilemap);
aml_vector_fini(&scratch->data.requests);
aml_vector_fini(&scratch->data.tilemap);
aml_area_munmap(scratch->data.sch_area,
scratch->data.sch_ptr,
scratch->data.scratch_size);
......
......@@ -24,13 +24,12 @@ int aml_vector_resize(struct aml_vector *vec, size_t newsize)
{
assert(vec != NULL);
/* we don't shrink */
if(vec->nbelems > newsize)
if (vec->nbelems > newsize)
return 0;
vec->ptr = realloc(vec->ptr, newsize * vec->sz);
assert(vec->ptr != NULL);
for(int i = vec->nbelems; i < newsize; i++)
{
for (int i = vec->nbelems; i < newsize; i++) {
int *k = AML_VECTOR_KEY_P(vec, i);
*k = vec->na;
}
......@@ -48,7 +47,7 @@ size_t aml_vector_size(const struct aml_vector *vec)
void *aml_vector_get(struct aml_vector *vec, int id)
{
assert(vec != NULL);
if(id != vec->na && id < vec->nbelems)
if (id != vec->na && id < vec->nbelems)
return AML_VECTOR_ELT_P(vec, id);
else
return NULL;
......@@ -58,10 +57,10 @@ void *aml_vector_get(struct aml_vector *vec, int id)
int aml_vector_find(const struct aml_vector *vec, int key)
{
assert(vec != NULL);
for(int i = 0; i < vec->nbelems; i++)
{
for (int i = 0; i < vec->nbelems; i++) {
int *k = AML_VECTOR_KEY_P(vec, i);
if(*k == key)
if (*k == key)
return i;
}
return vec->na;
......@@ -70,13 +69,12 @@ int aml_vector_find(const struct aml_vector *vec, int key)
void *aml_vector_add(struct aml_vector *vec)
{
assert(vec != NULL);
int idx = aml_vector_find(vec, vec->na);
if(idx == vec->na)
{
if (idx == vec->na) {
/* exponential growth, good to amortize cost */
idx = vec->nbelems;
aml_vector_resize(vec, vec->nbelems *2);
aml_vector_resize(vec, vec->nbelems * 2);
}
return AML_VECTOR_ELT_P(vec, idx);
}
......@@ -95,27 +93,67 @@ void aml_vector_remove(struct aml_vector *vec, void *elem)
* Init/destroy:
******************************************************************************/
int aml_vector_create(struct aml_vector **vec, size_t reserve, size_t size,
size_t key, int na)
{
struct aml_vector *ret = NULL;
int err;
if (vec == NULL)
return -AML_EINVAL;
ret = malloc(sizeof(struct aml_vector));
if (ret == NULL) {
*vec = NULL;
return -AML_ENOMEM;
}
err = aml_vector_init(ret, reserve, size, key, na);
if (err) {
*vec = NULL;
free(ret);
return err;
}
*vec = ret;
return 0;
}
int aml_vector_init(struct aml_vector *vec, size_t reserve, size_t size,
size_t key, int na)
{
assert(vec != NULL);
void *ptr;
if (vec == NULL)
return -AML_EINVAL;
ptr = calloc(reserve, size);
if (ptr == NULL)
return -AML_ENOMEM;
vec->sz = size;
vec->off = key;
vec->na = na;
vec->nbelems = reserve;
vec->ptr = calloc(reserve, size);
assert(vec->ptr != NULL);
for(int i = 0; i < vec->nbelems; i++)
{
vec->ptr = ptr;
for (int i = 0; i < vec->nbelems; i++) {
int *k = AML_VECTOR_KEY_P(vec, i);
*k = na;
}
return 0;
}
int aml_vector_destroy(struct aml_vector *vec)
void aml_vector_fini(struct aml_vector *vec)
{
assert(vec != NULL);
if (vec == NULL)
return;
free(vec->ptr);
return 0;
}
void aml_vector_destroy(struct aml_vector **vec)
{
if (vec == NULL)
return;
aml_vector_fini(*vec);
free(*vec);
*vec = NULL;
}
......@@ -44,6 +44,6 @@ int main(int argc, char *argv[])
assert(aml_vector_find(&v, -1) == 1);
assert(aml_vector_size(&v) == 2);
aml_vector_destroy(&v);
aml_vector_fini(&v);
return 0;
}
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