Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C backend: workaround bulk freeing of constants #718

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/scalc/from_lcalc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ let translate_program ~(config : translation_config) (p : 'm L.program) :
in
let func_id = A.FuncName.fresh (func_name, pos) in
(* The list is being built in reverse order *)
(* FIXME: find a better way than a function with no parameters... *)
(* Note: this pattern is matched in the C backend to make
allocations permanent. *)
( A.SVar
{
var = var_id;
Expand Down
10 changes: 8 additions & 2 deletions compiler/scalc/to_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,14 @@ let format_program
Format.pp_print_space fmt ();
VarName.format fmt var))
typ;
Format.fprintf ppc "@[<hov 2>return (%a ? %a : (%a = %a));@]"
VarName.format var VarName.format var VarName.format var
Format.fprintf ppc "@[<hov 2>return CATALA_GET_LAZY(%a, %a);@]"
(* This does (foo ? foo : foo = foo_init()), but enabling persistent
allocation around the init *)
(* FIXME: the proper solution would be to do a deep copy of the
allocated object from the Catala heap to the persistent heap
instead of switching allocation mode (which could persist
intermediate values) *)
VarName.format var
(format_expression ctx env)
expr;
Format.fprintf ppc "@;<1 -2>}@]@,";
Expand Down
19 changes: 17 additions & 2 deletions runtimes/c/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <dates_calc.h>
#include "runtime.h"

int catala_persistent_malloc_mode_on = 0;

/* --- Error handling --- */

const catala_code_position catala_empty_position =
Expand All @@ -41,6 +43,7 @@ void catala_error(catala_error_code code,
{
catala_error_raised.code = code;
catala_error_raised.position = *pos;
catala_persistent_malloc_mode_on = 0;
longjmp(catala_error_jump_buffer, 1);
}

Expand Down Expand Up @@ -71,7 +74,9 @@ void* catala_malloc (size_t sz)
{
void* ptr = catala_heap.curptr;
void* nextptr = ptr + sz;
if (nextptr < catala_heap.end) {
if (catala_persistent_malloc_mode_on) {
return malloc(sz);
} else if (nextptr < catala_heap.end) {
catala_heap.curptr = nextptr;
return ptr;
} else {
Expand Down Expand Up @@ -100,7 +105,9 @@ void catala_free_all()

void* catala_realloc(void* oldptr, size_t oldsize, size_t newsize)
{
if (newsize <= oldsize) {
if (catala_persistent_malloc_mode_on) {
return realloc(oldptr, newsize);
} else if (newsize <= oldsize) {
memset(oldptr + newsize, 0, oldsize - newsize);
return oldptr;
} else {
Expand All @@ -117,6 +124,14 @@ void catala_free(void* ptr, size_t sz)
}
#pragma GCC diagnostic pop

void catala_set_persistent_malloc() {
catala_persistent_malloc_mode_on++;
}
void catala_unset_persistent_malloc() {
assert (catala_persistent_malloc_mode_on > 0);
catala_persistent_malloc_mode_on--;
}

/* --- Base types --- */

const int catala_true_value = 1;
Expand Down
8 changes: 8 additions & 0 deletions runtimes/c/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ void* catala_malloc (size_t sz);

void catala_free_all();

void catala_set_persistent_malloc();
void catala_unset_persistent_malloc();
/* These two functions can be used for switching an init section to persistent
malloc, then switching back to catala built-in malloc. In other words, any
calls to `catala_malloc` done between the two will not be affected by
`catala_free_all()`. Calls can be nested, but errors reset the context. */
#define CATALA_GET_LAZY(X, X_INIT) (X ? X : (catala_set_persistent_malloc(), X = X_INIT, catala_unset_persistent_malloc(), X))

/* --- Base types --- */

#define CATALA_BOOL const int*
Expand Down