diff --git a/library/string.m b/library/string.m index 4c7e3df1b..9bd36a332 100644 --- a/library/string.m +++ b/library/string.m @@ -1269,7 +1269,9 @@ make_format(Flags, MaybeWidth, MaybePrec, LengthMod, Spec) = String :- :- func format_float(string, float) = string. :- pragma c_code(format_float(FormatStr::in, Val::in) = (Str::out), [will_not_call_mercury, thread_safe], "{ + save_transient_hp(); Str = MR_make_string(MR_PROC_LABEL, FormatStr, (long double) Val); + restore_transient_hp(); }"). % Create a string from a int using the format string. @@ -1278,7 +1280,9 @@ make_format(Flags, MaybeWidth, MaybePrec, LengthMod, Spec) = String :- :- func format_int(string, int) = string. :- pragma c_code(format_int(FormatStr::in, Val::in) = (Str::out), [will_not_call_mercury, thread_safe], "{ + save_transient_hp(); Str = MR_make_string(MR_PROC_LABEL, FormatStr, Val); + restore_transient_hp(); }"). % Create a string from a string using the format string. @@ -1296,7 +1300,9 @@ make_format(Flags, MaybeWidth, MaybePrec, LengthMod, Spec) = String :- :- func format_char(string, char) = string. :- pragma c_code(format_char(FormatStr::in, Val::in) = (Str::out), [will_not_call_mercury, thread_safe], "{ + save_transient_hp(); Str = MR_make_string(MR_PROC_LABEL, FormatStr, Val); + restore_transient_hp(); }"). %-----------------------------------------------------------------------------% diff --git a/runtime/mercury_string.c b/runtime/mercury_string.c index 529bf7295..30943816b 100644 --- a/runtime/mercury_string.c +++ b/runtime/mercury_string.c @@ -74,9 +74,10 @@ MR_make_string(MR_Code *proclabel, const char *fmt, ...) { p = fixed; #endif - + restore_transient_hp(); MR_allocate_aligned_string_msg(result, strlen(p), proclabel); + save_transient_hp(); strcpy(result, p); #ifdef HAVE_VSNPRINTF diff --git a/runtime/mercury_string.h b/runtime/mercury_string.h index fafba31ec..a5159b0b7 100644 --- a/runtime/mercury_string.h +++ b/runtime/mercury_string.h @@ -174,10 +174,10 @@ int MR_hash_string(MR_Word); ** allocation as coming from proclabel. The MR_String returned has been ** allocated on the mercury heap using MR_allocate_aligned_string_msg. ** -** BEWARE: this may modify `hp', so it must only be called from -** places where `hp' is valid. If calling it from inside a C function, -** rather than inside Mercury code, you may need to call -** save/restore_transient_hp(). +** BEWARE: this may modify the saved copy of `hp', so it must only be +** called from places where the saved copy of `hp' is valid. +** You will generally need to call save/restore_transient_hp() +** before/after calling this function. */ MR_String MR_make_string(MR_Code *proclabel, const char *fmt, ...);