Skip to content

Commit

Permalink
No need to precheck for ptr!=0 for free (#2890)
Browse files Browse the repository at this point in the history
From https://linux.die.net/man/3/free:

  If ptr is NULL, no operation is performed.
  • Loading branch information
alkino authored Jun 4, 2024
1 parent 22f9c4c commit 8f16a43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
5 changes: 0 additions & 5 deletions src/nrnpython/grids.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ and Flux_pair structs and their respective functions
#include "nrn_pyhocobject.h"
#include "nrnwrap_Python.h"

#define SAFE_FREE(ptr) \
{ \
if ((ptr) != NULL) \
free(ptr); \
}
#define IDX(x, y, z) ((z) + (y) *g->size_z + (x) *g->size_z * g->size_y)
#define INDEX(x, y, z) ((z) + (y) *grid->size_z + (x) *grid->size_z * grid->size_y)
#define ALPHA(x, y, z) (g->get_alpha(g->alpha, IDX(x, y, z)))
Expand Down
4 changes: 2 additions & 2 deletions src/nrnpython/rxd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,10 +988,10 @@ extern "C" void clear_rates() {
}

free(react->state_idx);
SAFE_FREE(react->ecs_state);
free(react->ecs_state);
prev = react;
react = react->next;
SAFE_FREE(prev);
free(prev);
}
_reactions = NULL;
/*clear extracellular reactions*/
Expand Down
34 changes: 17 additions & 17 deletions src/nrnpython/rxd_extracellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ static void ecs_refresh_reactions(const int n) {
int k;
if (threaded_reactions_tasks != NULL) {
for (k = 0; k < NUM_THREADS; k++) {
SAFE_FREE(threaded_reactions_tasks[k].onset);
SAFE_FREE(threaded_reactions_tasks[k].offset);
free(threaded_reactions_tasks[k].onset);
free(threaded_reactions_tasks[k].offset);
}
SAFE_FREE(threaded_reactions_tasks);
free(threaded_reactions_tasks);
}
threaded_reactions_tasks = create_threaded_reactions(n);
}
Expand All @@ -61,13 +61,13 @@ void clear_rates_ecs(void) {
ECS_Grid_node* g;

for (r = ecs_reactions; r != NULL; r = tmp) {
SAFE_FREE(r->species_states);
free(r->species_states);
if (r->subregion) {
SAFE_FREE(r->subregion);
free(r->subregion);
}

tmp = r->next;
SAFE_FREE(r);
free(r);
}
ecs_reactions = NULL;

Expand Down Expand Up @@ -416,12 +416,12 @@ void* ecs_do_reactions(void* dataptr) {
}
}

SAFE_FREE(states_cache);
SAFE_FREE(states_cache_dx);
SAFE_FREE(params_cache);
SAFE_FREE(results_array);
SAFE_FREE(results_array_dx);
SAFE_FREE(mc_mults_array);
free(states_cache);
free(states_cache_dx);
free(params_cache);
free(results_array);
free(results_array_dx);
free(mc_mults_array);

if (stop)
return NULL;
Expand Down Expand Up @@ -537,11 +537,11 @@ void* ecs_do_reactions(void* dataptr) {
}
}

SAFE_FREE(states_cache);
SAFE_FREE(states_cache_dx);
SAFE_FREE(params_cache);
SAFE_FREE(results_array);
SAFE_FREE(results_array_dx);
free(states_cache);
free(states_cache_dx);
free(params_cache);
free(results_array);
free(results_array_dx);

if (stop)
return NULL;
Expand Down

0 comments on commit 8f16a43

Please sign in to comment.