Skip to content

Commit

Permalink
Further static [] allocation -> malloc / free. Brings compilation on …
Browse files Browse the repository at this point in the history
…Windows with cl.exe
  • Loading branch information
willend committed Feb 23, 2024
1 parent 34e99d0 commit 191e895
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
4 changes: 3 additions & 1 deletion mcstas-comps/share/Geometry_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,8 @@ int sample_mesh_intersect(double *t,int *num_solutions,double *r,double *v,struc
Coords edge1,edge2,h,s,q,tmp,intersect_pos;
double UNION_EPSILON = 0.0000001;
double this_facet_t;
double a,f,u,V,t_intersect[n_facets];
double a,f,u,V;
double *t_intersect=malloc(n_facets*sizeof(double));
*num_solutions = 0;
for (iter = 0 ; iter < n_facets ; iter++){
/*////printf("\n\n facet v1 = [%f,%f,%f]",v1_x[iter],v1_y[iter],v1_z[iter]);
Expand Down Expand Up @@ -1304,6 +1305,7 @@ int sample_mesh_intersect(double *t,int *num_solutions,double *r,double *v,struc
}
//}
}
free(t_intersect);

// find two smallest non-zero intersections:
/*
Expand Down
44 changes: 28 additions & 16 deletions mcstas-comps/share/Union_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,8 @@ void add_element_to_double_list(struct pointer_to_1d_double_list *list,double va
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(double));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
list->elements[list->num_elements-1] = value;
free(temp);
list->elements[list->num_elements-1] = value;
}
};

Expand All @@ -756,8 +756,8 @@ void add_element_to_int_list(struct pointer_to_1d_int_list *list,int value) {
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(int));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
list->elements[list->num_elements-1] = value;
free(temp);
list->elements[list->num_elements-1] = value;
}
};

Expand All @@ -768,13 +768,14 @@ void add_element_to_focus_data_array(struct focus_data_array_struct *focus_data_
focus_data_array-> elements = malloc(focus_data_array->num_elements*sizeof(struct focus_data_struct));
focus_data_array-> elements[0] = focus_data;
} else {
struct focus_data_struct temp[focus_data_array->num_elements];
struct focus_data_struct *temp=malloc(focus_data_array->num_elements*sizeof(struct focus_data_struct));
int iterate;
for (iterate=0;iterate<focus_data_array->num_elements;iterate++) temp[iterate] = focus_data_array->elements[iterate];
free(focus_data_array->elements);
focus_data_array->num_elements++;
focus_data_array-> elements = malloc(focus_data_array->num_elements*sizeof(struct focus_data_struct));
for (iterate=0;iterate<focus_data_array->num_elements-1;iterate++) focus_data_array->elements[iterate] = temp[iterate];
free(temp);
focus_data_array->elements[focus_data_array->num_elements-1] = focus_data;
}
};
Expand Down Expand Up @@ -1002,13 +1003,14 @@ void add_element_to_process_list(struct pointer_to_global_process_list *list,str
list-> elements[0] = new_element;
}
else {
struct global_process_element_struct temp[list->num_elements];
struct global_process_element_struct *temp=malloc(list->num_elements*sizeof(struct global_process_element_struct));
int iterate;
for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
free(list->elements);
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(struct global_process_element_struct));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
free(temp);
list->elements[list->num_elements-1] = new_element;
}
};
Expand All @@ -1020,13 +1022,14 @@ void add_element_to_material_list(struct pointer_to_global_material_list *list,s
list-> elements[0] = new_element;
}
else {
struct global_material_element_struct temp[list->num_elements];
struct global_material_element_struct *temp=malloc(list->num_elements*sizeof(struct global_material_element_struct));
int iterate;
for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
free(list->elements);
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(struct global_material_element_struct));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
free(temp);
list->elements[list->num_elements-1] = new_element;
}
};
Expand All @@ -1038,13 +1041,14 @@ void add_element_to_geometry_list(struct pointer_to_global_geometry_list *list,s
list-> elements[0] = new_element;
}
else {
struct global_geometry_element_struct temp[list->num_elements];
struct global_geometry_element_struct *temp=malloc(list->num_elements*sizeof(struct global_geometry_element_struct));
int iterate;
for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
free(list->elements);
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(struct global_geometry_element_struct));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
free(temp);
list->elements[list->num_elements-1] = new_element;
}
};
Expand All @@ -1056,13 +1060,14 @@ void add_element_to_logger_list(struct pointer_to_global_logger_list *list,struc
list-> elements[0] = new_element;
}
else {
struct global_logger_element_struct temp[list->num_elements];
struct global_logger_element_struct *temp=malloc(list->num_elements*sizeof(struct global_logger_element_struct));
int iterate;
for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
free(list->elements);
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(struct global_logger_element_struct));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
free(temp);
list->elements[list->num_elements-1] = new_element;
}
};
Expand All @@ -1074,13 +1079,14 @@ void add_element_to_abs_logger_list(struct pointer_to_global_abs_logger_list *li
list->elements[0] = new_element;
}
else {
struct global_abs_logger_element_struct temp[list->num_elements];
struct global_abs_logger_element_struct *temp=malloc(list->num_elements*sizeof(struct global_abs_logger_element_struct));
int iterate;
for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
free(list->elements);
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(struct global_abs_logger_element_struct));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
free(temp);
list->elements[list->num_elements-1] = new_element;
}
};
Expand All @@ -1092,13 +1098,14 @@ void add_element_to_tagging_conditional_list(struct global_tagging_conditional_l
list->elements[0] = new_element;
}
else {
struct global_tagging_conditional_element_struct temp[list->num_elements];
struct global_tagging_conditional_element_struct *temp=malloc(list->num_elements*sizeof(struct global_tagging_conditional_element_struct));
int iterate;
for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
free(list->elements);
list->num_elements++;
list->elements = malloc(list->num_elements*sizeof(struct global_tagging_conditional_element_struct));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
free(temp);
list->elements[list->num_elements-1] = new_element;
}
};
Expand All @@ -1110,13 +1117,14 @@ void add_element_to_master_list(struct pointer_to_global_master_list *list,struc
list->elements[0] = new_element;
}
else {
struct global_master_element_struct temp[list->num_elements];
struct global_master_element_struct *temp=malloc(list->num_elements*sizeof(struct global_master_element_struct));
int iterate;
for (iterate=0;iterate<list->num_elements;iterate++) temp[iterate] = list->elements[iterate];
free(list->elements);
list->num_elements++;
list-> elements = malloc(list->num_elements*sizeof(struct global_master_element_struct));
for (iterate=0;iterate<list->num_elements-1;iterate++) list->elements[iterate] = temp[iterate];
free(temp);
list->elements[list->num_elements-1] = new_element;
}
};
Expand All @@ -1132,13 +1140,14 @@ void add_initialized_logger_in_volume(struct loggers_struct *loggers,int number_
loggers->p_logger_volume[0].p_logger_process[iterate] = NULL;
} else {
// Already some elements, store them in temp, free main, transfer back and add newest.
struct logger_for_each_process_list temp[loggers->num_elements];
struct logger_for_each_process_list *temp=malloc(loggers->num_elements*sizeof(struct logger_for_each_process_list));

for (iterate=0;iterate<loggers->num_elements;iterate++) temp[iterate] = loggers->p_logger_volume[iterate];
free(loggers->p_logger_volume);
loggers->num_elements++;
loggers->p_logger_volume = malloc(loggers->num_elements*sizeof(struct logger_for_each_process_list));
for (iterate=0;iterate<loggers->num_elements-1;iterate++) loggers->p_logger_volume[iterate] = temp[iterate];
free(temp);
loggers->p_logger_volume[loggers->num_elements-1].num_elements = number_of_processes;
loggers->p_logger_volume[loggers->num_elements-1].p_logger_process = malloc(number_of_processes * sizeof(struct logger_struct**));
for (iterate=0;iterate<number_of_processes;iterate++) {
Expand Down Expand Up @@ -1182,16 +1191,16 @@ void add_initialized_abs_logger_in_volume(struct abs_loggers_struct *abs_loggers
abs_loggers->p_abs_logger = malloc(abs_loggers->num_elements * sizeof(struct abs_logger_struct*));
} else {
// Already some elements, store them in temp, free main, transfer back and add newest.
struct abs_logger_struct *temp[abs_loggers->num_elements];
struct abs_logger_struct **temp=malloc(abs_loggers->num_elements*sizeof(struct abs_logger_struct *));

for (iterate=0;iterate<abs_loggers->num_elements;iterate++) temp[iterate] = abs_loggers->p_abs_logger[iterate];
free(abs_loggers->p_abs_logger);

abs_loggers->num_elements++;
abs_loggers->p_abs_logger = malloc(abs_loggers->num_elements*sizeof(struct abs_logger_struct*));
for (iterate=0;iterate<abs_loggers->num_elements-1;iterate++) abs_loggers->p_abs_logger[iterate] = temp[iterate];
free(temp);
abs_loggers->p_abs_logger[abs_loggers->num_elements-1] = NULL;

}
};

Expand Down Expand Up @@ -1935,7 +1944,7 @@ struct lines_to_draw draw_line_with_highest_priority(Coords position1,Coords pos
// for (iterate = 0;iterate < intersection_list.num_elements;iterate++) printf("%f ",intersection_list.elements[iterate]);
// printf("\n");

Coords points[intersection_list.num_elements+2];
Coords *points=malloc((intersection_list.num_elements+2)*sizeof(Coords));
points[0] = coords_set(r1[0],r1[1],r1[2]);
points[intersection_list.num_elements+1] = coords_set(r2[0],r2[1],r2[2]);
for (iterate = 1;iterate < intersection_list.num_elements+1;iterate++) {
Expand All @@ -1950,8 +1959,8 @@ struct lines_to_draw draw_line_with_highest_priority(Coords position1,Coords pos
// }
// printf("\n");

struct line_segment lines[intersection_list.num_elements+1];
int draw_logic[intersection_list.num_elements+1];
struct line_segment *lines=malloc((intersection_list.num_elements+1)*sizeof(struct line_segment));
int *draw_logic=malloc((intersection_list.num_elements+1)*sizeof(int));
//struct lines_to_draw draw_order;
Coords midpoint;
struct lines_to_draw draw_order;
Expand Down Expand Up @@ -1988,6 +1997,9 @@ struct lines_to_draw draw_line_with_highest_priority(Coords position1,Coords pos
}

// printf("function done \n");
free(points);
free(lines);
free(draw_logic);
return draw_order;
}

Expand Down

0 comments on commit 191e895

Please sign in to comment.