Skip to content
Snippets Groups Projects
Commit efab2914 authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Safer addlink from Pedro's branch

parent e4766c19
No related branches found
No related tags found
2 merge requests!136Master,!79First version of the multiple time-stepping
...@@ -69,7 +69,11 @@ int engine_rank; ...@@ -69,7 +69,11 @@ int engine_rank;
struct link *engine_addlink(struct engine *e, struct link *l, struct task *t) { struct link *engine_addlink(struct engine *e, struct link *l, struct task *t) {
struct link *res = &e->links[atomic_inc(&e->nr_links)]; const int ind = atomic_inc(&e->nr_links);
if (ind >= e->size_links) {
error("Link table overflow.");
}
struct link *res = &e->links[ind];
res->next = l; res->next = l;
res->t = t; res->t = t;
return res; return res;
...@@ -1058,8 +1062,9 @@ void engine_maketasks(struct engine *e) { ...@@ -1058,8 +1062,9 @@ void engine_maketasks(struct engine *e) {
/* Allocate the list of cell-task links. The maximum number of links /* Allocate the list of cell-task links. The maximum number of links
is the number of cells (s->tot_cells) times the number of neighbours (27) is the number of cells (s->tot_cells) times the number of neighbours (27)
times the number of interaction types (2, density and force). */ times the number of interaction types (2, density and force). */
if (e->links != NULL) free(e->links); free(e->links);
if ((e->links = malloc(sizeof(struct link) * s->tot_cells * 27 * 2)) == NULL) e->size_links = s->tot_cells * 27 * 2;
if ((e->links = malloc(sizeof(struct link) * e->size_links)) == NULL)
error("Failed to allocate cell-task links."); error("Failed to allocate cell-task links.");
e->nr_links = 0; e->nr_links = 0;
......
...@@ -128,7 +128,7 @@ struct engine { ...@@ -128,7 +128,7 @@ struct engine {
/* Linked list for cell-task association. */ /* Linked list for cell-task association. */
struct link *links; struct link *links;
int nr_links; int nr_links, size_links;
}; };
/* Function prototypes. */ /* Function prototypes. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment