···14141515### Runtime system:
16161717+* #11449, #13497: Add caml_stat_char_array_{to,of}_os functions allowing
1818+ conversion of string data which may contain NUL characters. Correct
1919+ implementation of caml_stat_strdup_to_utf16 to raise Out_of_memory instead of
2020+ returning of NULL (the behaviour of caml_stat_strdup_to_os was inconsistent
2121+ between Unix/Windows).
2222+ (David Allsopp, review by Nick Barnes, Antonin Décimo and Miod Vallat)
2323+1724- #13352: Concurrency refactors and cleanups.
1825 (Antonin Décimo, review by Gabriel Scherer, David Allsopp, and Miod Vallat)
1926
+10
runtime/caml/memory.h
···166166CAMLextern wchar_t* caml_stat_wcsdup_noexc(const wchar_t *s);
167167#endif
168168169169+/* [caml_stat_memdup(s, size, &out_size)] returns a copy of the first [size]
170170+ bytes of [s] (which may include NUL characters). If [out_size] is not [NULL],
171171+ then [size] is stored in [*out_size]. This function is the "dummy" Unix
172172+ implementation of the Windows-only functions
173173+ caml_stat_char_array_{to,from}_utf16.
174174+*/
175175+CAMLmalloc(caml_stat_free, 1, 2) CAMLreturns_nonnull()
176176+CAMLextern caml_stat_string caml_stat_memdup(const char *s, asize_t size,
177177+ asize_t *out_size);
178178+169179/* [caml_stat_strconcat(nargs, strings)] concatenates null-terminated [strings]
170180 (an array of [char*] of size [nargs]) into a new string, dropping all NULs,
171181 except for the very last one. It throws an OCaml exception in case the
···164164extern unsigned short caml_win32_build;
165165extern unsigned short caml_win32_revision;
166166167167-/* [caml_stat_strdup_to_utf16(s)] returns a null-terminated copy of [s],
167167+/* [caml_stat_strdup_noexc_to_utf16(s)] returns a NUL-terminated copy of [s],
168168 re-encoded in UTF-16. The encoding of [s] is assumed to be UTF-8 if
169169 [caml_windows_unicode_runtime_enabled] is non-zero **and** [s] is valid
170170 UTF-8, or the current Windows code page otherwise.
171171172172- The returned string is allocated with [caml_stat_alloc], so it should be free
173173- using [caml_stat_free].
172172+ The returned string is allocated with [caml_stat_alloc_noexc], so it should
173173+ be freed using [caml_stat_free].
174174+175175+ If allocation fails, this returns NULL. This function never raises any
176176+ exceptions when [s] is valid UTF-8 but may raise [Sys_error] if it is not.
174177*/
178178+CAMLalloc(caml_stat_free, 1)
179179+CAMLextern wchar_t* caml_stat_strdup_noexc_to_utf16(const char *s);
180180+181181+/* [caml_stat_strdup_to_utf16(s)] returns a NUL-terminated copy of [s],
182182+ re-encoded in UTF-16. The encoding of [s] is assumed to be UTF-8 if
183183+ [caml_windows_unicode_runtime_enabled] is non-zero **and** [s] is valid
184184+ UTF-8, or the current Windows code page otherwise.
185185+186186+ The returned string is allocated with [caml_stat_alloc], so it should be
187187+ freed using [caml_stat_free].
188188+189189+ If allocation fails, this raises Out_of_memory. This function may also raise
190190+ [Sys_error] if [s] is not valid UTF-8.
191191+*/
192192+CAMLalloc(caml_stat_free, 1) CAMLreturns_nonnull()
175193CAMLextern wchar_t* caml_stat_strdup_to_utf16(const char *s);
176194177177-/* [caml_stat_strdup_noexc_of_utf16(s)] returns a null-terminated copy of [s],
195195+/* [caml_stat_strdup_noexc_of_utf16(s)] returns a NUL-terminated copy of [s],
178196 re-encoded in UTF-8 if [caml_windows_unicode_runtime_enabled] is non-zero or
179197 the current Windows code page otherwise.
180198181181- The returned string is allocated with [caml_stat_alloc_noexc], so
182182- it should be freed using [caml_stat_free].
199199+ The returned string is allocated with [caml_stat_alloc_noexc], so it should
200200+ be freed using [caml_stat_free].
183201184184- If allocation fails, this returns NULL.
202202+ If allocation fails, this returns NULL. This function never raises any
203203+ exceptions when [s] is valid UTF-16 but may raise [Sys_error] if it is not.
185204*/
205205+CAMLalloc(caml_stat_free, 1)
186206CAMLextern char* caml_stat_strdup_noexc_of_utf16(const wchar_t *s);
187207188188-/* [caml_stat_strdup_of_utf16(s)] returns a null-terminated copy of [s],
208208+/* [caml_stat_strdup_of_utf16(s)] returns a NUL-terminated copy of [s],
189209 re-encoded in UTF-8 if [caml_windows_unicode_runtime_enabled] is non-zero or
190210 the current Windows code page otherwise.
191211192192- The returned string is allocated with [caml_stat_alloc_noexc], so
193193- it should be freed using [caml_stat_free].
212212+ The returned string is allocated with [caml_stat_alloc], so it should be
213213+ freed using [caml_stat_free].
194214195195- If allocation fails, this raises Out_of_memory.
215215+ If allocation fails, this raises Out_of_memory. This function may also raise
216216+ [Sys_error] if [s] is not valid UTF-16.
196217*/
218218+CAMLalloc(caml_stat_free, 1) CAMLreturns_nonnull()
197219CAMLextern char* caml_stat_strdup_of_utf16(const wchar_t *s);
220220+221221+/* [caml_stat_char_array_to_utf16(s, size, &out_size)] returns a copy of the
222222+ first [size] bytes of [s] re-encoded in UTF-16. [s] does not have to be NUL-
223223+ terminated and may contain embedded NUL characters. The encoding of [s] is
224224+ assumed to be UTF-8 if [caml_windows_unicode_runtime_enabled] is non-zero
225225+ **and** [s] is valid UTF-8, or the current Windows code page otherwise. If
226226+ [out_size] is not [NULL], then the number of UTF-16 code units in the result
227227+ is recorded in [*out_size].
228228+229229+ [size] must be greater than zero.
230230+231231+ The returned buffer is allocated with [caml_stat_alloc], so it should be
232232+ freed using [caml_stat_free].
233233+234234+ If allocation fails, this raises Out_of_memory. This function may also raise
235235+ [Sys_error] if [s] is not valid UTF-8.
236236+*/
237237+CAMLmalloc(caml_stat_free, 1, 2) CAMLreturns_nonnull()
238238+CAMLextern wchar_t *caml_stat_char_array_to_utf16(const char *s, size_t size,
239239+ size_t *out_size);
240240+241241+/* [caml_stat_char_array_of_utf16(s, size, &out_size)] returns a copy of the
242242+ first [size] UTF-16 code units of [s] re-encoded in UTF-8 if
243243+ [caml_windows_unicode_runtime_enabled] is non-zero or the current Windows
244244+ code page otherwise. [s] does not have to be NUL-terminated and may contain
245245+ embedded NUL characters. If [out_size] is not [NULL], then the size of the
246246+ result in bytes recorded in [*out_size].
247247+248248+ [size] must be greater than zero.
249249+250250+ The returned buffer is allocated with [caml_stat_alloc], so it should be
251251+ freed using [caml_stat_free].
252252+253253+ If allocation fails, this raises Out_of_memory. This function may also raise
254254+ [Sys_error] if [s] is not valid UTF-16.
255255+*/
256256+CAMLalloc(caml_stat_free, 1) CAMLreturns_nonnull()
257257+CAMLextern char *caml_stat_char_array_of_utf16(const wchar_t *s, size_t size,
258258+ size_t *out_size);
198259199260/* [caml_copy_string_of_utf16(s)] returns an OCaml string containing a copy of
200261 [s] re-encoded in UTF-8 if [caml_windows_unicode_runtime_enabled] is non-zero