Commit cad06907 authored by Nicolas Denoyelle's avatar Nicolas Denoyelle

add checking in next and peek for nilpotence when rising STOPIT

parent f36f2f4d
...@@ -151,12 +151,15 @@ static int cons_it_peek(const excit_t data, ssize_t *indexes) ...@@ -151,12 +151,15 @@ static int cons_it_peek(const excit_t data, ssize_t *indexes)
int n = it->n; int n = it->n;
if (indexes) { if (indexes) {
circular_fifo_dump(&it->fifo, indexes);
err = excit_peek(it->it, indexes + dim * (n - 1)); err = excit_peek(it->it, indexes + dim * (n - 1));
if (err)
return err;
circular_fifo_dump(&it->fifo, indexes);
} else } else
err = excit_peek(it->it, NULL); err = excit_peek(it->it, NULL);
if (err) if (err)
return err; return err;
return EXCIT_SUCCESS; return EXCIT_SUCCESS;
} }
...@@ -167,9 +170,11 @@ static int cons_it_next(excit_t data, ssize_t *indexes) ...@@ -167,9 +170,11 @@ static int cons_it_next(excit_t data, ssize_t *indexes)
int dim = it->it->dimension; int dim = it->it->dimension;
int n = it->n; int n = it->n;
if (indexes) { if (indexes) {
circular_fifo_dump(&it->fifo, indexes);
err = excit_next(it->it, indexes + dim * (n - 1)); err = excit_next(it->it, indexes + dim * (n - 1));
if (err)
return err;
circular_fifo_dump(&it->fifo, indexes);
} else } else
err = excit_next(it->it, NULL); err = excit_next(it->it, NULL);
if (err) if (err)
......
#include <string.h>
#include "dev/excit.h" #include "dev/excit.h"
#include "prod.h" #include "prod.h"
...@@ -7,6 +8,7 @@ static int prod_it_alloc(excit_t data) ...@@ -7,6 +8,7 @@ static int prod_it_alloc(excit_t data)
it->count = 0; it->count = 0;
it->its = NULL; it->its = NULL;
it->buff = NULL;
return EXCIT_SUCCESS; return EXCIT_SUCCESS;
} }
...@@ -18,6 +20,7 @@ static void prod_it_free(excit_t data) ...@@ -18,6 +20,7 @@ static void prod_it_free(excit_t data)
for (ssize_t i = 0; i < it->count; i++) for (ssize_t i = 0; i < it->count; i++)
excit_free(it->its[i]); excit_free(it->its[i]);
free(it->its); free(it->its);
free(it->buff);
} }
} }
...@@ -29,6 +32,12 @@ static int prod_it_copy(excit_t dst, const excit_t src) ...@@ -29,6 +32,12 @@ static int prod_it_copy(excit_t dst, const excit_t src)
result->its = (excit_t *) malloc(it->count * sizeof(excit_t)); result->its = (excit_t *) malloc(it->count * sizeof(excit_t));
if (!result->its) if (!result->its)
return -EXCIT_ENOMEM; return -EXCIT_ENOMEM;
result->buff = (ssize_t *) malloc(src->dimension * sizeof(ssize_t));
if (!result->buff){
free(result->its);
return -EXCIT_ENOMEM;
}
ssize_t i; ssize_t i;
for (i = 0; i < it->count; i++) { for (i = 0; i < it->count; i++) {
...@@ -184,11 +193,8 @@ static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes, ...@@ -184,11 +193,8 @@ static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes,
return -EXCIT_EINVAL; return -EXCIT_EINVAL;
looped = next; looped = next;
for (i = it->count - 1; i > 0; i--) { for (i = it->count - 1; i > 0; i--) {
if (indexes) { offset -= it->its[i]->dimension;
offset -= it->its[i]->dimension; next_indexes = it->buff + offset;
next_indexes = indexes + offset;
} else
next_indexes = NULL;
if (looped) if (looped)
err = excit_cyclic_next(it->its[i], next_indexes, err = excit_cyclic_next(it->its[i], next_indexes,
&looped); &looped);
...@@ -197,17 +203,17 @@ static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes, ...@@ -197,17 +203,17 @@ static inline int prod_it_peeknext_helper(excit_t data, ssize_t *indexes,
if (err) if (err)
return err; return err;
} }
if (indexes) { offset -= it->its[i]->dimension;
offset -= it->its[i]->dimension; next_indexes = it->buff + offset;
next_indexes = indexes + offset;
} else
next_indexes = NULL;
if (looped) if (looped)
err = excit_next(it->its[0], next_indexes); err = excit_next(it->its[0], next_indexes);
else else
err = excit_peek(it->its[0], next_indexes); err = excit_peek(it->its[0], next_indexes);
if (err) if (err)
return err; return err;
if(indexes)
memcpy(indexes, it->buff, data->dimension * sizeof(ssize_t));
return EXCIT_SUCCESS; return EXCIT_SUCCESS;
} }
...@@ -288,6 +294,8 @@ int excit_product_add_copy(excit_t it, excit_t added_it) ...@@ -288,6 +294,8 @@ int excit_product_add_copy(excit_t it, excit_t added_it)
int excit_product_add(excit_t it, excit_t added_it) int excit_product_add(excit_t it, excit_t added_it)
{ {
int err = EXCIT_SUCCESS;
if (!it || it->type != EXCIT_PRODUCT || !it->data || !added_it) if (!it || it->type != EXCIT_PRODUCT || !it->data || !added_it)
return -EXCIT_EINVAL; return -EXCIT_EINVAL;
...@@ -295,14 +303,30 @@ int excit_product_add(excit_t it, excit_t added_it) ...@@ -295,14 +303,30 @@ int excit_product_add(excit_t it, excit_t added_it)
ssize_t mew_count = prod_it->count + 1; ssize_t mew_count = prod_it->count + 1;
excit_t *new_its = excit_t *new_its =
(excit_t *) realloc(prod_it->its, mew_count * sizeof(excit_t)); (excit_t *) realloc(prod_it->its, mew_count * sizeof(excit_t));
if (!new_its) if (!new_its)
return -EXCIT_ENOMEM; return -EXCIT_ENOMEM;
ssize_t *new_buff =
realloc(prod_it->buff,
(added_it->dimension + it->dimension) * sizeof(ssize_t));
if (!new_buff){
err = -EXCIT_ENOMEM;
goto exit_with_new_its;
}
prod_it->its = new_its; prod_it->its = new_its;
prod_it->buff = new_buff;
prod_it->its[prod_it->count] = added_it; prod_it->its[prod_it->count] = added_it;
prod_it->count = mew_count; prod_it->count = mew_count;
it->dimension += added_it->dimension; it->dimension += added_it->dimension;
return EXCIT_SUCCESS; return EXCIT_SUCCESS;
exit_with_new_its:
free(new_its);
return err;
} }
struct excit_func_table_s excit_prod_func_table = { struct excit_func_table_s excit_prod_func_table = {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
struct prod_it_s { struct prod_it_s {
ssize_t count; ssize_t count;
ssize_t* buff;
excit_t *its; excit_t *its;
}; };
......
...@@ -107,11 +107,12 @@ void test_peek(excit_t it1) ...@@ -107,11 +107,12 @@ void test_peek(excit_t it1)
excit_dimension_test(it1, &dim1); excit_dimension_test(it1, &dim1);
ssize_t *indexes1, *indexes2; ssize_t *indexes1, *indexes2, *indexes3;
ssize_t buff_dim = dim1 * sizeof(ssize_t); ssize_t buff_dim = dim1 * sizeof(ssize_t);
indexes1 = (ssize_t *) malloc(buff_dim); indexes1 = (ssize_t *) malloc(buff_dim);
indexes2 = (ssize_t *) malloc(buff_dim); indexes2 = (ssize_t *) malloc(buff_dim);
indexes3 = (ssize_t *) malloc(buff_dim);
while (excit_next(it1, indexes1) == ES) { while (excit_next(it1, indexes1) == ES) {
assert(excit_peek(it2, indexes2) == ES); assert(excit_peek(it2, indexes2) == ES);
...@@ -119,11 +120,17 @@ void test_peek(excit_t it1) ...@@ -119,11 +120,17 @@ void test_peek(excit_t it1)
assert(excit_next(it2, indexes2) == ES); assert(excit_next(it2, indexes2) == ES);
assert(memcmp(indexes1, indexes2, buff_dim) == 0); assert(memcmp(indexes1, indexes2, buff_dim) == 0);
} }
memset(indexes2, 1, buff_dim);
memset(indexes3, 1, buff_dim);
assert(excit_peek(it2, indexes2) == EXCIT_STOPIT); assert(excit_peek(it2, indexes2) == EXCIT_STOPIT);
assert(! memcmp(indexes2, indexes3, buff_dim));
assert(excit_next(it2, indexes2) == EXCIT_STOPIT); assert(excit_next(it2, indexes2) == EXCIT_STOPIT);
assert(! memcmp(indexes2, indexes3, buff_dim));
free(indexes1); free(indexes1);
free(indexes2); free(indexes2);
free(indexes3);
excit_free(it2); excit_free(it2);
} }
......
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