Commit 7c8992ec authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Added functions to clean-up the allocated memory

parent 8a7a517d
......@@ -611,6 +611,10 @@ int main(int argc, char *argv[]) {
error("call to MPI_Finalize failed with error %i.", res);
#endif
/* Clean everything */
engine_clean(&e);
free(params);
/* Say goodbye. */
if (myrank == 0) message("done. Bye.");
......
......@@ -707,3 +707,8 @@ void cell_clean_links(struct cell *c, void *data) {
c->force = NULL;
c->nr_force = 0;
}
/**
* @brief Frees up the memory allocated for this #cell
*/
void cell_clean(struct cell *c) { free(c->sort); }
......@@ -197,5 +197,6 @@ void cell_init_parts(struct cell *c, void *data);
void cell_init_gparts(struct cell *c, void *data);
void cell_convert_hydro(struct cell *c, void *data);
void cell_clean_links(struct cell *c, void *data);
void cell_clean(struct cell *c);
#endif /* SWIFT_CELL_H */
......@@ -2864,20 +2864,22 @@ void engine_init(struct engine *e, struct space *s,
if (verbose && with_aff) message("Affinity at entry: %s", buf);
int *cpuid = malloc(nr_affinity_cores * sizeof(int));
int *cpuid = NULL;
cpu_set_t cpuset;
int skip = 0;
for (int k = 0; k < nr_affinity_cores; k++) {
int c;
for (c = skip; c < CPU_SETSIZE && !CPU_ISSET(c, entry_affinity); ++c)
;
cpuid[k] = c;
skip = c + 1;
}
if (with_aff) {
cpuid = malloc(nr_affinity_cores * sizeof(int));
int skip = 0;
for (int k = 0; k < nr_affinity_cores; k++) {
int c;
for (c = skip; c < CPU_SETSIZE && !CPU_ISSET(c, entry_affinity); ++c)
;
cpuid[k] = c;
skip = c + 1;
}
#if defined(HAVE_LIBNUMA) && defined(_GNU_SOURCE)
if ((policy & engine_policy_cputight) != engine_policy_cputight) {
......@@ -3241,3 +3243,14 @@ void engine_compute_next_snapshot_time(struct engine *e) {
message("Next output time set to t=%e.", next_snapshot_time);
}
}
/**
* @brief Frees up the memory allocated for this #engine
*/
void engine_clean(struct engine *e) {
free(e->snapshotUnits);
free(e->links);
scheduler_clean(&e->sched);
space_clean(e->s);
}
......@@ -239,5 +239,6 @@ void engine_print_policy(struct engine *e);
int engine_is_done(struct engine *e);
void engine_pin();
void engine_unpin();
void engine_clean(struct engine *e);
#endif /* SWIFT_ENGINE_H */
......@@ -296,3 +296,9 @@ struct task *queue_gettask(struct queue *q, const struct task *prev,
/* Take the money and run. */
return res;
}
void queue_clean(struct queue *q) {
free(q->tid);
free(q->tid_incoming);
}
......@@ -64,5 +64,6 @@ struct task *queue_gettask(struct queue *q, const struct task *prev,
int blocking);
void queue_init(struct queue *q, struct task *tasks);
void queue_insert(struct queue *q, struct task *t);
void queue_clean(struct queue *q);
#endif /* SWIFT_QUEUE_H */
......@@ -1298,3 +1298,16 @@ void scheduler_do_rewait(struct task *t_begin, struct task *t_end,
}
}
}
/**
* @brief Frees up the memory allocated for this #scheduler
*/
void scheduler_clean(struct scheduler *s) {
free(s->tasks);
free(s->tasks_ind);
free(s->unlocks);
free(s->unlock_ind);
for (int i = 0; i < s->nr_queues; ++i) queue_clean(&s->queues[i]);
free(s->queues);
}
......@@ -124,5 +124,6 @@ void scheduler_dump_queue(struct scheduler *s);
void scheduler_print_tasks(const struct scheduler *s, const char *fileName);
void scheduler_do_rewait(struct task *t_begin, struct task *t_end,
unsigned int mask, unsigned int submask);
void scheduler_clean(struct scheduler *s);
#endif /* SWIFT_SCHEDULER_H */
......@@ -1558,3 +1558,12 @@ void space_link_cleanup(struct space *s) {
/* Recursively apply the cell link cleaning routine */
space_map_cells_pre(s, 1, cell_clean_links, NULL);
}
/**
* @brief Frees up the memory allocated for this #space
*/
void space_clean(struct space *s) {
for (int i = 0; i < s->nr_cells; ++i) cell_clean(&s->cells[i]);
free(s->cells);
}
......@@ -160,5 +160,6 @@ void space_do_split(struct space *s, struct cell *c);
void space_do_parts_sort();
void space_do_gparts_sort();
void space_link_cleanup(struct space *s);
void space_clean(struct space *s);
#endif /* SWIFT_SPACE_H */
Supports Markdown
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