liberasurecode  1.6.2
Erasure Code API library
null.c
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Tushar Gohad
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  * liberasurecode null backend
25  *
26  * vi: set noai tw=79 ts=4 sw=4:
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #include "erasurecode.h"
33 #include "erasurecode_backend.h"
34 #define NULL_LIB_MAJOR 1
35 #define NULL_LIB_MINOR 0
36 #define NULL_LIB_REV 0
37 #define NULL_LIB_VER_STR "1.0"
38 #define NULL_LIB_NAME "null"
39 #if defined(__MACOS__) || defined(__MACOSX__) || defined(__OSX__) || defined(__APPLE__)
40 #define NULL_SO_NAME "libnullcode.dylib"
41 #else
42 #define NULL_SO_NAME "libnullcode.so.1"
43 #endif
44 /* Forward declarations */
45 struct ec_backend null;
46 struct ec_backend_op_stubs null_ops;
47 
48 typedef void* (*init_null_code_func)(int, int, int);
49 typedef int (*null_code_encode_func)(void *, char **, char **, int);
50 typedef int (*null_code_decode_func)(void *, char **, char **, int *, int, int);
51 typedef int (*null_reconstruct_func)(char **, int, uint64_t, int, char *);
52 typedef int (*null_code_fragments_needed_func)(void *, int *, int *, int *);
54  /* calls required for init */
56 
57  /* calls required for encode */
59 
60  /* calls required for decode */
62 
63  /* calls required for reconstruct */
65 
66  /* set of fragments needed to reconstruct at a minimum */
68 
69  /* fields needed to hold state */
70  int *matrix;
71  int k;
72  int m;
73  int w;
74  int arg1;
75 };
76 
77 #define DEFAULT_W 32
78 
79 static int null_encode(void *desc, char **data, char **parity, int blocksize)
80 {
81  return 0;
82 }
83 
84 static int null_decode(void *desc, char **data, char **parity,
85  int *missing_idxs, int blocksize)
86 {
87  return 0;
88 }
89 
90 static int null_reconstruct(void *desc, char **data, char **parity,
91  int *missing_idxs, int destination_idx, int blocksize)
92 {
93  return 0;
94 }
95 
96 static int null_min_fragments(void *desc, int *missing_idxs,
97  int *fragments_to_exclude, int *fragments_needed)
98 {
99  return 0;
100 }
101 
106 static int
107 null_element_size(void* desc)
108 {
109  return DEFAULT_W;
110 }
111 
112 static void * null_init(struct ec_backend_args *args, void *backend_sohandle)
113 {
114  struct null_descriptor *xdesc = NULL;
115 
116  /* allocate and fill in null_descriptor */
117  xdesc = (struct null_descriptor *) malloc(sizeof(struct null_descriptor));
118  if (NULL == xdesc) {
119  return NULL;
120  }
121  memset(xdesc, 0, sizeof(struct null_descriptor));
122 
123  xdesc->k = args->uargs.k;
124  xdesc->m = args->uargs.m;
125  xdesc->w = args->uargs.w;
126 
127  if (xdesc->w <= 0)
128  xdesc->w = DEFAULT_W;
129 
130  /* Sample on how to pass extra args to the backend */
131  xdesc->arg1 = args->uargs.priv_args1.null_args.arg1;
132 
133  /* store w back in args so upper layer can get to it */
134  args->uargs.w = DEFAULT_W;
135 
136  /* validate EC arguments */
137  {
138  long long max_symbols;
139  if (xdesc->w != 8 && xdesc->w != 16 && xdesc->w != 32) {
140  goto error;
141  }
142  max_symbols = 1LL << xdesc->w;
143  if ((xdesc->k + xdesc->m) > max_symbols) {
144  goto error;
145  }
146  }
147 
148  /*
149  * ISO C forbids casting a void* to a function pointer.
150  * Since dlsym return returns a void*, we use this union to
151  * "transform" the void* to a function pointer.
152  */
153  union {
154  init_null_code_func initp;
155  null_code_encode_func encodep;
156  null_code_decode_func decodep;
157  null_reconstruct_func reconp;
158  null_code_fragments_needed_func fragsneededp;
159  void *vptr;
160  } func_handle = {.vptr = NULL};
161 
162  /* fill in function addresses */
163  func_handle.vptr = NULL;
164  func_handle.vptr = dlsym(backend_sohandle, "null_code_init");
165  xdesc->init_null_code = func_handle.initp;
166  if (NULL == xdesc->init_null_code) {
167  goto error;
168  }
169 
170  func_handle.vptr = NULL;
171  func_handle.vptr = dlsym(backend_sohandle, "null_code_encode");
172  xdesc->null_code_encode = func_handle.encodep;
173  if (NULL == xdesc->null_code_encode) {
174  goto error;
175  }
176 
177  func_handle.vptr = NULL;
178  func_handle.vptr = dlsym(backend_sohandle, "null_code_decode");
179  xdesc->null_code_decode = func_handle.decodep;
180  if (NULL == xdesc->null_code_decode) {
181  goto error;
182  }
183 
184  func_handle.vptr = NULL;
185  func_handle.vptr = dlsym(backend_sohandle, "null_reconstruct");
186  xdesc->null_reconstruct = func_handle.reconp;
187  if (NULL == xdesc->null_reconstruct) {
188  goto error;
189  }
190 
191  func_handle.vptr = NULL;
192  func_handle.vptr = dlsym(backend_sohandle, "null_code_fragments_needed");
193  xdesc->null_code_fragments_needed = func_handle.fragsneededp;
194  if (NULL == xdesc->null_code_fragments_needed) {
195  goto error;
196  }
197 
198  return (void *) xdesc;
199 
200 error:
201  free (xdesc);
202 
203  return NULL;
204 }
205 
206 static int null_exit(void *desc)
207 {
208  struct null_descriptor *xdesc = (struct null_descriptor *) desc;
209 
210  free (xdesc);
211  return 0;
212 }
213 
214 static bool null_is_compatible_with(uint32_t version) {
215  return true;
216 }
217 
218 struct ec_backend_op_stubs null_op_stubs = {
219  .INIT = null_init,
220  .EXIT = null_exit,
221  .ENCODE = null_encode,
222  .DECODE = null_decode,
223  .FRAGSNEEDED = null_min_fragments,
224  .RECONSTRUCT = null_reconstruct,
225  .ELEMENTSIZE = null_element_size,
226  .ISCOMPATIBLEWITH = null_is_compatible_with,
227  .GETMETADATASIZE = get_backend_metadata_size_zero,
228  .GETENCODEOFFSET = get_encode_offset_zero,
229 };
230 
231 struct ec_backend_common backend_null = {
232  .id = EC_BACKEND_NULL,
233  .name = NULL_LIB_NAME,
234  .soname = NULL_SO_NAME,
235  .soversion = NULL_LIB_VER_STR,
236  .ops = &null_op_stubs,
237  .ec_backend_version = _VERSION(NULL_LIB_MAJOR, NULL_LIB_MINOR,
238  NULL_LIB_REV),
239 };
240 
NULL_LIB_REV
#define NULL_LIB_REV
Definition: null.c:36
null_descriptor::init_null_code
init_null_code_func init_null_code
Definition: null.c:55
null_reconstruct
static int null_reconstruct(void *desc, char **data, char **parity, int *missing_idxs, int destination_idx, int blocksize)
Definition: null.c:90
null_ops
struct ec_backend_op_stubs null_ops
Definition: null.c:46
null_element_size
static int null_element_size(void *desc)
Return the element-size, which is the number of bits stored on a given device, per codeword.
Definition: null.c:107
NULL_LIB_NAME
#define NULL_LIB_NAME
Definition: null.c:38
null_reconstruct_func
int(* null_reconstruct_func)(char **, int, uint64_t, int, char *)
Definition: null.c:51
null_descriptor::m
int m
Definition: null.c:72
null_op_stubs
struct ec_backend_op_stubs null_op_stubs
Definition: null.c:218
init_null_code_func
void *(* init_null_code_func)(int, int, int)
Definition: null.c:48
null_descriptor::matrix
int * matrix
Definition: null.c:70
null_descriptor::null_code_encode
null_code_encode_func null_code_encode
Definition: null.c:58
null_descriptor::arg1
int arg1
Definition: null.c:74
NULL_LIB_VER_STR
#define NULL_LIB_VER_STR
Definition: null.c:37
DEFAULT_W
#define DEFAULT_W
Definition: null.c:77
null_encode
static int null_encode(void *desc, char **data, char **parity, int blocksize)
Definition: null.c:79
NULL_SO_NAME
#define NULL_SO_NAME
Definition: null.c:42
null_decode
static int null_decode(void *desc, char **data, char **parity, int *missing_idxs, int blocksize)
Definition: null.c:84
null_init
static void * null_init(struct ec_backend_args *args, void *backend_sohandle)
Definition: null.c:112
backend_null
struct ec_backend_common backend_null
Definition: null.c:231
null_code_encode_func
int(* null_code_encode_func)(void *, char **, char **, int)
Definition: null.c:49
null_descriptor::null_code_decode
null_code_decode_func null_code_decode
Definition: null.c:61
null_code_fragments_needed_func
int(* null_code_fragments_needed_func)(void *, int *, int *, int *)
Definition: null.c:52
null_descriptor::k
int k
Definition: null.c:71
null_descriptor::null_reconstruct
null_reconstruct_func null_reconstruct
Definition: null.c:64
null_descriptor::w
int w
Definition: null.c:73
null_min_fragments
static int null_min_fragments(void *desc, int *missing_idxs, int *fragments_to_exclude, int *fragments_needed)
Definition: null.c:96
null_descriptor::null_code_fragments_needed
null_code_fragments_needed_func null_code_fragments_needed
Definition: null.c:67
NULL_LIB_MINOR
#define NULL_LIB_MINOR
Definition: null.c:35
null_is_compatible_with
static bool null_is_compatible_with(uint32_t version)
Definition: null.c:214
NULL_LIB_MAJOR
#define NULL_LIB_MAJOR
Definition: null.c:34
null_descriptor
Definition: null.c:53
null_exit
static int null_exit(void *desc)
Definition: null.c:206
null
struct ec_backend null
Definition: null.c:45
null_code_decode_func
int(* null_code_decode_func)(void *, char **, char **, int *, int, int)
Definition: null.c:50