bytearray_stubs.c (1488B)
1 /* Unison file synchronizer: src/bytearray_stubs.c */ 2 /* Copyright 1999-2020 (see COPYING for details) */ 3 4 #include <string.h> 5 6 #include <caml/intext.h> 7 #include <caml/bigarray.h> 8 #include <caml/memory.h> 9 10 CAMLprim value ml_marshal_to_bigarray(value v, value flags) 11 { 12 CAMLparam2(v, flags); 13 char *buf; 14 intnat len; 15 caml_output_value_to_malloc(v, flags, &buf, &len); 16 CAMLreturn( 17 caml_ba_alloc_dims(CAML_BA_UINT8 | CAML_BA_C_LAYOUT | CAML_BA_MANAGED, 18 1, buf, len)); 19 } 20 21 22 #define Array_data(a, i) (((char *) Caml_ba_data_val(a)) + Long_val(i)) 23 24 CAMLprim value ml_unmarshal_from_bigarray(value b, value ofs) 25 { 26 CAMLparam2(b, ofs); 27 CAMLlocal1(result); 28 result = caml_input_value_from_block(Array_data(b, ofs), 29 Caml_ba_array_val(b)->dim[0] - Long_val(ofs)); 30 CAMLreturn(result); 31 } 32 33 CAMLprim value ml_blit_bytes_to_bigarray 34 (value s, value i, value a, value j, value l) 35 { 36 CAMLparam5(s, i, a, j, l); 37 unsigned char *src = Bytes_val(s) + Long_val(i); 38 char *dest = Array_data(a, j); 39 memcpy(dest, src, Long_val(l)); 40 CAMLreturn(Val_unit); 41 } 42 43 CAMLprim value ml_blit_string_to_bigarray 44 (value s, value i, value a, value j, value l) 45 { 46 return ml_blit_bytes_to_bigarray(s, i, a, j, l); 47 } 48 49 CAMLprim value ml_blit_bigarray_to_bytes 50 (value a, value i, value s, value j, value l) 51 { 52 CAMLparam5(a, i, s, j, l); 53 char *src = Array_data(a, i); 54 unsigned char *dest = Bytes_val(s) + Long_val(j); 55 memcpy(dest, src, Long_val(l)); 56 CAMLreturn(Val_unit); 57 }