|
| 1 | +#include <stdint.h> |
| 2 | +#include <string.h> |
| 3 | +#include <errno.h> |
| 4 | + |
| 5 | +#define CAML_NAME_SPACE |
| 6 | +#include <caml/alloc.h> |
| 7 | +#include <caml/memory.h> |
| 8 | +#include <caml/threads.h> |
| 9 | +#include <caml/fail.h> |
| 10 | +#include <caml/custom.h> |
| 11 | + |
| 12 | +typedef struct array { |
| 13 | + uint64_t length; |
| 14 | + int64_t* a; |
| 15 | +} array; |
| 16 | + |
| 17 | +static inline array * |
| 18 | +qcow_mapping_arr_of_val (value v) |
| 19 | +{ |
| 20 | + array *arr = *(array **) Data_custom_val(v); |
| 21 | + |
| 22 | + return arr; |
| 23 | +} |
| 24 | + |
| 25 | +static void |
| 26 | +qcow_mapping_finalize (value v) |
| 27 | +{ |
| 28 | + array *arr = qcow_mapping_arr_of_val (v); |
| 29 | + free(arr->a); |
| 30 | + free(arr); |
| 31 | +} |
| 32 | + |
| 33 | +static struct custom_operations qcow_mapping_ops = { |
| 34 | + .identifier = "qcow_mapping_array", |
| 35 | + .finalize = qcow_mapping_finalize, |
| 36 | + .compare = custom_compare_default, /* Can't compare */ |
| 37 | + .hash = custom_hash_default, /* Can't hash */ |
| 38 | + .serialize = custom_serialize_default, /* Can't serialize */ |
| 39 | + .deserialize = custom_deserialize_default, /* Can't deserialize */ |
| 40 | + .compare_ext = custom_compare_ext_default, /* Can't compare */ |
| 41 | +}; |
| 42 | + |
| 43 | +#define Arr_val(v) (*((array **) Data_custom_val(v))) |
| 44 | + |
| 45 | +CAMLprim value |
| 46 | +stub_qcow_mapping_create (value length_val) |
| 47 | +{ |
| 48 | + CAMLparam1(length_val); |
| 49 | + CAMLlocal1(result); |
| 50 | + array *arr = malloc(sizeof *arr); |
| 51 | + |
| 52 | + arr->length = Int64_val(length_val); |
| 53 | + result = caml_alloc_custom(&qcow_mapping_ops, sizeof(array *), 0, 1); |
| 54 | + |
| 55 | + caml_release_runtime_system(); |
| 56 | + arr->a = malloc(sizeof(uint64_t) * arr->length); |
| 57 | + caml_acquire_runtime_system(); |
| 58 | + |
| 59 | + if (!arr->a) |
| 60 | + caml_failwith(strerror(errno)); |
| 61 | + |
| 62 | + caml_release_runtime_system(); |
| 63 | + // Initialize to -1, otherwise there's no way to distinguish data clusters |
| 64 | + // from table clusters and empty clusters |
| 65 | + for (size_t i = 0; i < arr->length; i++) { |
| 66 | + arr->a[i] = -1; |
| 67 | + } |
| 68 | + caml_acquire_runtime_system(); |
| 69 | + |
| 70 | + Arr_val(result) = arr; |
| 71 | + |
| 72 | + CAMLreturn(result); |
| 73 | +} |
| 74 | + |
| 75 | +CAMLprim value |
| 76 | +stub_qcow_mapping_extend (value t_val, value length_val) |
| 77 | +{ |
| 78 | + CAMLparam2(t_val, length_val); |
| 79 | + CAMLlocal1(result); |
| 80 | + array* arr = qcow_mapping_arr_of_val(t_val); |
| 81 | + |
| 82 | + uint64_t new_length = Int64_val(length_val); |
| 83 | + |
| 84 | + caml_release_runtime_system(); |
| 85 | + arr->a = realloc(arr->a, sizeof(uint64_t) * new_length); |
| 86 | + caml_acquire_runtime_system(); |
| 87 | + |
| 88 | + if (!arr->a) |
| 89 | + caml_failwith(strerror(errno)); |
| 90 | + |
| 91 | + caml_release_runtime_system(); |
| 92 | + // Initialize the newly allocated cells to -1, otherwise there's no way |
| 93 | + // to distinguish data clusters from table clusters and empty clusters |
| 94 | + for (size_t i = arr->length; i < new_length; i++) { |
| 95 | + arr->a[i] = -1; |
| 96 | + } |
| 97 | + arr->length = new_length; |
| 98 | + caml_acquire_runtime_system(); |
| 99 | + |
| 100 | + CAMLreturn(Val_unit); |
| 101 | +} |
| 102 | + |
| 103 | +CAMLprim value |
| 104 | +stub_qcow_mapping_get (value t_val, value index_val) |
| 105 | +{ |
| 106 | + CAMLparam2(t_val, index_val); |
| 107 | + CAMLlocal1(result); |
| 108 | + array* arr = qcow_mapping_arr_of_val(t_val); |
| 109 | + |
| 110 | + uint64_t index = Int64_val(index_val); |
| 111 | + result = caml_copy_int64(arr->a[index]); |
| 112 | + |
| 113 | + CAMLreturn(result); |
| 114 | +} |
| 115 | + |
| 116 | +CAMLprim value |
| 117 | +stub_qcow_mapping_set (value t_val, value index_val, value new_val) |
| 118 | +{ |
| 119 | + CAMLparam3(t_val, index_val, new_val); |
| 120 | + array* arr = qcow_mapping_arr_of_val(t_val); |
| 121 | + |
| 122 | + uint64_t index = Int64_val(index_val); |
| 123 | + int64_t new = Int64_val(new_val); |
| 124 | + arr->a[index] = new; |
| 125 | + |
| 126 | + CAMLreturn(Val_unit); |
| 127 | +} |
| 128 | + |
| 129 | +CAMLprim value |
| 130 | +stub_qcow_mapping_length (value t_val) |
| 131 | +{ |
| 132 | + CAMLparam1(t_val); |
| 133 | + CAMLlocal1(result); |
| 134 | + |
| 135 | + array *arr = qcow_mapping_arr_of_val(t_val); |
| 136 | + result = caml_copy_int64(arr->length); |
| 137 | + |
| 138 | + CAMLreturn(result); |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +CAMLprim value |
| 143 | +stub_qcow_mapping_get_sparse_interval (value t_val, value index_val, value cluster_bits_val) |
| 144 | +{ |
| 145 | + CAMLparam3(t_val, index_val, cluster_bits_val); |
| 146 | + CAMLlocal1(result); |
| 147 | + result = caml_alloc_tuple(3); |
| 148 | + |
| 149 | + array* arr = qcow_mapping_arr_of_val(t_val); |
| 150 | + |
| 151 | + uint64_t cluster_bits = Int64_val(cluster_bits_val); |
| 152 | + uint64_t diff_bw_next_clusters = 1 << cluster_bits; |
| 153 | + uint64_t left_index = Int64_val(index_val); |
| 154 | + uint64_t right_index = left_index; |
| 155 | + |
| 156 | + // Find the longest interval of subsequent allocated data clusters |
| 157 | + // (data clusters are subsequent if they're located right next to each |
| 158 | + // other on the virtual disk), return the index to be used in the next |
| 159 | + // search iteration and the [interval_start, interval_end] pair of virtual |
| 160 | + // clusters (both inclusive) |
| 161 | + for (size_t i = left_index+1; i < arr->length; i++, right_index++) { |
| 162 | + if (arr->a[i-1] == -1) { |
| 163 | + if (arr->a[i] == -1 && i == (arr->length-1)) { |
| 164 | + CAMLreturn(Val_none); |
| 165 | + } |
| 166 | + left_index++; |
| 167 | + } else if (arr->a[i-1] + diff_bw_next_clusters != arr->a[i]) { |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + Store_field(result, 0, caml_copy_int64(right_index)); |
| 173 | + Store_field(result, 1, caml_copy_int64((arr->a[left_index]) >> cluster_bits)); |
| 174 | + Store_field(result, 2, caml_copy_int64((arr->a[right_index]) >> cluster_bits)); |
| 175 | + |
| 176 | + CAMLreturn(caml_alloc_some(result)); |
| 177 | +} |
0 commit comments