liberasurecode 1.8.0
Erasure Code API library
Loading...
Searching...
No Matches
liberasurecode_rs_vand.c
Go to the documentation of this file.
1/*
2 * Copyright 2015 Kevin M Greenan
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY
13 * THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
21 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * vi: set noai tw=79 ts=4 sw=4:
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29
30#include "erasurecode.h"
31#include "erasurecode_backend.h"
32#include "erasurecode_helpers.h"
33#include "erasurecode_helpers_ext.h"
34
35#define LIBERASURECODE_RS_VAND_LIB_MAJOR 1
36#define LIBERASURECODE_RS_VAND_LIB_MINOR 0
37#define LIBERASURECODE_RS_VAND_LIB_REV 0
38#define LIBERASURECODE_RS_VAND_LIB_VER_STR "1.0"
39#define LIBERASURECODE_RS_VAND_LIB_NAME "liberasurecode_rs_vand"
40#if defined(__MACOS__) || defined(__MACOSX__) || defined(__OSX__) || defined(__APPLE__)
41#define LIBERASURECODE_RS_VAND_SO_NAME "liberasurecode_rs_vand" LIBERASURECODE_SO_SUFFIX ".dylib"
42#else
43#define LIBERASURECODE_RS_VAND_SO_NAME "liberasurecode_rs_vand" LIBERASURECODE_SO_SUFFIX ".so.1"
44#endif
45
46/* Forward declarations */
47struct ec_backend_common backend_liberasurecode_rs_vand;
48
49typedef int (*liberasurecode_rs_vand_encode_func)(int *, char **, char **, int, int, int);
50typedef int (*liberasurecode_rs_vand_decode_func)(int *, char **, char **, int, int, int *, int, int);
51typedef int (*liberasurecode_rs_vand_reconstruct_func)(int *, char **, char **, int, int, int *, int, int);
52typedef void (*init_liberasurecode_rs_vand_func)(int, int);
54typedef void (*free_systematic_matrix_func)(int *);
55typedef int* (*make_systematic_matrix_func)(int, int);
56
57
59 /* calls required for init */
64
65 /* calls required for encode */
67
68 /* calls required for decode */
70
71 /* calls required for reconstruct */
73
74 /* fields needed to hold state */
75 int *matrix;
76 int k;
77 int m;
78 int w;
79};
80
81static int liberasurecode_rs_vand_encode(void *desc, char **data, char **parity,
82 int blocksize)
83{
84 struct liberasurecode_rs_vand_descriptor *rs_vand_desc =
86
87 /* FIXME: Should this return something? */
88 rs_vand_desc->liberasurecode_rs_vand_encode(rs_vand_desc->matrix, data, parity,
89 rs_vand_desc->k, rs_vand_desc->m, blocksize);
90 return 0;
91}
92
93static int liberasurecode_rs_vand_decode(void *desc, char **data, char **parity,
94 int *missing_idxs, int blocksize)
95{
96 struct liberasurecode_rs_vand_descriptor *rs_vand_desc =
98
99 /* FIXME: Should this return something? */
100 rs_vand_desc->liberasurecode_rs_vand_decode(rs_vand_desc->matrix, data, parity,
101 rs_vand_desc->k, rs_vand_desc->m, missing_idxs, blocksize, 1);
102
103 return 0;
104}
105
106static int liberasurecode_rs_vand_reconstruct(void *desc, char **data, char **parity,
107 int *missing_idxs, int destination_idx, int blocksize)
108{
109 struct liberasurecode_rs_vand_descriptor *rs_vand_desc =
111
112 /* FIXME: Should this return something? */
113 rs_vand_desc->liberasurecode_rs_vand_reconstruct(rs_vand_desc->matrix, data, parity,
114 rs_vand_desc->k, rs_vand_desc->m, missing_idxs, destination_idx, blocksize);
115
116 return 0;
117}
118
119static int liberasurecode_rs_vand_min_fragments(void *desc, int *missing_idxs,
120 int *fragments_to_exclude, int *fragments_needed)
121{
122 struct liberasurecode_rs_vand_descriptor *rs_vand_desc =
124
125 struct ec_bm missing_bm = NEW_BM;
126 convert_list_to_bitmap(fragments_to_exclude, &missing_bm);
127 convert_list_to_bitmap(missing_idxs, &missing_bm);
128 int i;
129 int j = 0;
130 int ret = -1;
131
132 for (i = 0; i < (rs_vand_desc->k + rs_vand_desc->m); i++) {
133 if (!bm_get_value(&missing_bm, i)) {
134 fragments_needed[j] = i;
135 j++;
136 }
137 if (j == rs_vand_desc->k) {
138 ret = 0;
139 fragments_needed[j] = -1;
140 break;
141 }
142 }
143
144 return ret;
145}
146
147static void * liberasurecode_rs_vand_init(struct ec_backend_args *args,
148 void *backend_sohandle)
149{
150 struct liberasurecode_rs_vand_descriptor *desc = NULL;
151
152 desc = (struct liberasurecode_rs_vand_descriptor *)
153 malloc(sizeof(struct liberasurecode_rs_vand_descriptor));
154 if (NULL == desc) {
155 return NULL;
156 }
157
158 desc->k = args->uargs.k;
159 desc->m = args->uargs.m;
160
161 /* store w back in args so upper layer can get to it */
162 args->uargs.w = desc->w = 16; // w is currently hard-coded at 16
163
164 // This check should not matter, since 64K is way higher
165 // than anyone should ever use
166 if ((desc->k + desc->m) > 65536) {
167 goto error;
168 }
169
170 /*
171 * ISO C forbids casting a void* to a function pointer.
172 * Since dlsym return returns a void*, we use this union to
173 * "transform" the void* to a function pointer.
174 */
175 union {
178 free_systematic_matrix_func freematrixp;
179 make_systematic_matrix_func makematrixp;
183 void *vptr;
184 } func_handle = {.vptr = NULL};
185
186
187 /* fill in function addresses */
188 func_handle.vptr = NULL;
189 func_handle.vptr = dlsym(backend_sohandle, "init_liberasurecode_rs_vand");
190 desc->init_liberasurecode_rs_vand = func_handle.initp;
191 if (NULL == desc->init_liberasurecode_rs_vand) {
192 goto error;
193 }
194
195 func_handle.vptr = NULL;
196 func_handle.vptr = dlsym(backend_sohandle, "deinit_liberasurecode_rs_vand");
197 desc->deinit_liberasurecode_rs_vand = func_handle.deinitp;
198 if (NULL == desc->deinit_liberasurecode_rs_vand) {
199 goto error;
200 }
201
202 func_handle.vptr = NULL;
203 func_handle.vptr = dlsym(backend_sohandle, "make_systematic_matrix");
204 desc->make_systematic_matrix = func_handle.makematrixp;
205 if (NULL == desc->make_systematic_matrix) {
206 goto error;
207 }
208
209 func_handle.vptr = NULL;
210 func_handle.vptr = dlsym(backend_sohandle, "free_systematic_matrix");
211 desc->free_systematic_matrix = func_handle.freematrixp;
212 if (NULL == desc->free_systematic_matrix) {
213 goto error;
214 }
215
216 func_handle.vptr = NULL;
217 func_handle.vptr = dlsym(backend_sohandle, "liberasurecode_rs_vand_encode");
218 desc->liberasurecode_rs_vand_encode = func_handle.encodep;
219 if (NULL == desc->liberasurecode_rs_vand_encode) {
220 goto error;
221 }
222
223 func_handle.vptr = NULL;
224 func_handle.vptr = dlsym(backend_sohandle, "liberasurecode_rs_vand_decode");
225 desc->liberasurecode_rs_vand_decode = func_handle.decodep;
226 if (NULL == desc->liberasurecode_rs_vand_decode) {
227 goto error;
228 }
229
230 func_handle.vptr = NULL;
231 func_handle.vptr = dlsym(backend_sohandle, "liberasurecode_rs_vand_reconstruct");
232 desc->liberasurecode_rs_vand_reconstruct = func_handle.reconstructp;
233 if (NULL == desc->liberasurecode_rs_vand_reconstruct) {
234 goto error;
235 }
236
237 desc->init_liberasurecode_rs_vand(desc->k, desc->m);
238
239 desc->matrix = desc->make_systematic_matrix(desc->k, desc->m);
240
241 if (NULL == desc->matrix) {
242 goto error;
243 }
244
245 return desc;
246
247error:
248 free(desc);
249
250 return NULL;
251}
252
260static int
262{
263 struct liberasurecode_rs_vand_descriptor *rs_vand_desc = NULL;
264
265 rs_vand_desc = (struct liberasurecode_rs_vand_descriptor*) desc;
266
267 return rs_vand_desc->w;
268}
269
270static int liberasurecode_rs_vand_exit(void *desc)
271{
272 struct liberasurecode_rs_vand_descriptor *rs_vand_desc = NULL;
273
274 rs_vand_desc = (struct liberasurecode_rs_vand_descriptor*) desc;
275
276 rs_vand_desc->free_systematic_matrix(rs_vand_desc->matrix);
277 rs_vand_desc->deinit_liberasurecode_rs_vand();
278 free(rs_vand_desc);
279
280 return 0;
281}
282
283/*
284 * For the time being, we only claim compatibility with versions that
285 * match exactly
286 */
287static bool liberasurecode_rs_vand_is_compatible_with(uint32_t version) {
288 return version == backend_liberasurecode_rs_vand.ec_backend_version;
289}
290
291static struct ec_backend_op_stubs liberasurecode_rs_vand_op_stubs = {
294 .ISSYSTEMATIC = 1,
301 .GETMETADATASIZE = get_backend_metadata_size_zero,
302 .GETENCODEOFFSET = get_encode_offset_zero,
303};
304
305__attribute__ ((visibility ("internal")))
306struct ec_backend_common backend_liberasurecode_rs_vand = {
307 .id = EC_BACKEND_LIBERASURECODE_RS_VAND,
312 .ec_backend_version = _VERSION(LIBERASURECODE_RS_VAND_LIB_MAJOR,
315};
int(* liberasurecode_rs_vand_encode_func)(int *, char **, char **, int, int, int)
#define LIBERASURECODE_RS_VAND_LIB_NAME
static int liberasurecode_rs_vand_min_fragments(void *desc, int *missing_idxs, int *fragments_to_exclude, int *fragments_needed)
struct ec_backend_common backend_liberasurecode_rs_vand
#define LIBERASURECODE_RS_VAND_LIB_MINOR
static bool liberasurecode_rs_vand_is_compatible_with(uint32_t version)
static int liberasurecode_rs_vand_encode(void *desc, char **data, char **parity, int blocksize)
int(* liberasurecode_rs_vand_decode_func)(int *, char **, char **, int, int, int *, int, int)
#define LIBERASURECODE_RS_VAND_LIB_MAJOR
int *(* make_systematic_matrix_func)(int, int)
static int liberasurecode_rs_vand_decode(void *desc, char **data, char **parity, int *missing_idxs, int blocksize)
#define LIBERASURECODE_RS_VAND_LIB_REV
void(* free_systematic_matrix_func)(int *)
int(* liberasurecode_rs_vand_reconstruct_func)(int *, char **, char **, int, int, int *, int, int)
__attribute__((visibility("internal")))
void(* init_liberasurecode_rs_vand_func)(int, int)
static int liberasurecode_rs_vand_reconstruct(void *desc, char **data, char **parity, int *missing_idxs, int destination_idx, int blocksize)
#define LIBERASURECODE_RS_VAND_LIB_VER_STR
static int liberasurecode_rs_vand_element_size(void *desc)
Return the element-size, which is the number of bits stored on a given device, per codeword.
void(* deinit_liberasurecode_rs_vand_func)(void)
static int liberasurecode_rs_vand_exit(void *desc)
static struct ec_backend_op_stubs liberasurecode_rs_vand_op_stubs
#define LIBERASURECODE_RS_VAND_SO_NAME
static void * liberasurecode_rs_vand_init(struct ec_backend_args *args, void *backend_sohandle)
liberasurecode_rs_vand_reconstruct_func liberasurecode_rs_vand_reconstruct
make_systematic_matrix_func make_systematic_matrix
liberasurecode_rs_vand_decode_func liberasurecode_rs_vand_decode
init_liberasurecode_rs_vand_func init_liberasurecode_rs_vand
free_systematic_matrix_func free_systematic_matrix
deinit_liberasurecode_rs_vand_func deinit_liberasurecode_rs_vand
liberasurecode_rs_vand_encode_func liberasurecode_rs_vand_encode