Skip to content

Commit

Permalink
Improving memory handling in generated c++ code
Browse files Browse the repository at this point in the history
  • Loading branch information
clararod9 committed Oct 4, 2022
1 parent 17ccb3f commit a714807
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions code_producers/src/c_elements/c_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,30 @@ pub fn generate_message_list_def(_producer: &CProducer, message_list: &MessageLi
instructions
}

pub fn generate_function_release_memory_component() -> Vec<String>{
let mut instructions = vec![];
instructions.push("void release_memory_component(Circom_CalcWit* ctx, uint pos) {{\n".to_string());
instructions.push("delete ctx->componentMemory[pos].subcomponents;\n".to_string());
instructions.push("delete ctx->componentMemory[pos].subcomponentsParallel;\n".to_string());
instructions.push("delete ctx->componentMemory[pos].outputIsSet;\n".to_string());
instructions.push("delete ctx->componentMemory[pos].mutexes;\n".to_string());
instructions.push("delete ctx->componentMemory[pos].cvs;\n".to_string());
instructions.push("delete ctx->componentMemory[pos].sbct;\n".to_string());
instructions.push("}}\n\n".to_string());
instructions
}

pub fn generate_function_release_memory_circuit() -> Vec<String>{
// deleting each one of the components
let mut instructions = vec![];
instructions.push("void release_memory(Circom_CalcWit* ctx) {{\n".to_string());
instructions.push("for (int i = 0; i < get_number_of_components(); i++) {{\n".to_string());
instructions.push("release_memory_component(ctx, i);\n".to_string());
instructions.push("}}\n".to_string());
instructions.push("}}\n".to_string());
instructions
}

pub fn generate_main_cpp_file(c_folder: &PathBuf) -> std::io::Result<()> {
use std::io::BufWriter;
let mut file_path = c_folder.clone();
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/circuit_design/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ impl WriteC for Circuit {
));
//code.append(&mut generate_message_list_def(producer, producer.get_message_list()));

// Function to release the memory of a component
let mut release_component_code = generate_function_release_memory_component();
code.append(&mut release_component_code);

// Actual code of the circuit
code.push("// function declarations".to_string());
for f in &self.functions {
Expand Down
15 changes: 15 additions & 0 deletions compiler/src/circuit_design/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,21 @@ impl TemplateCodeInfo {
run_body.push(format!("ctx->numThreadMutex.unlock();"));
run_body.push(format!("ctx->ntcvs.notify_one();"));
}

// to release the memory of its subcomponents
run_body.push(format!("for (uint i = 0; i < {}; i++){{", &self.number_of_components.to_string()));
run_body.push(format!(
"uint index_subc = {}->componentMemory[{}].subcomponents[i];",
CIRCOM_CALC_WIT,
ctx_index(),
));
run_body.push(format!("{};",
build_call(
"release_memory_component".to_string(),
vec![CIRCOM_CALC_WIT.to_string(), "index_subc".to_string()]
)));
run_body.push(format!("}}"));

let run_fun = build_callable(run_header, run_params, run_body);
vec![create_fun, run_fun]
}
Expand Down

0 comments on commit a714807

Please sign in to comment.