liberasurecode  1.2.0
Erasure Code API library
 All Data Structures Files Functions Variables Typedefs Macros
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 *);
53 
55  /* calls required for init */
57 
58  /* calls required for encode */
60 
61  /* calls required for decode */
63 
64  /* calls required for reconstruct */
66 
67  /* set of fragments needed to reconstruct at a minimum */
69 
70  /* fields needed to hold state */
71  int *matrix;
72  int k;
73  int m;
74  int w;
75  int arg1;
76 };
77 
78 #define DEFAULT_W 32
79 
80 static int null_encode(void *desc, char **data, char **parity, int blocksize)
81 {
82  return 0;
83 }
84 
85 static int null_decode(void *desc, char **data, char **parity,
86  int *missing_idxs, int blocksize)
87 {
88  return 0;
89 }
90 
91 static int null_reconstruct(void *desc, char **data, char **parity,
92  int *missing_idxs, int destination_idx, int blocksize)
93 {
94  return 0;
95 }
96 
97 static int null_min_fragments(void *desc, int *missing_idxs,
98  int *fragments_to_exclude, int *fragments_needed)
99 {
100  return 0;
101 }
102 
107 static int
108 null_element_size(void* desc)
109 {
110  return DEFAULT_W;
111 }
112 
113 static void * null_init(struct ec_backend_args *args, void *backend_sohandle)
114 {
115  struct null_descriptor *xdesc = NULL;
116 
117  /* allocate and fill in null_descriptor */
118  xdesc = (struct null_descriptor *) malloc(sizeof(struct null_descriptor));
119  if (NULL == xdesc) {
120  return NULL;
121  }
122  memset(xdesc, 0, sizeof(struct null_descriptor));
123 
124  xdesc->k = args->uargs.k;
125  xdesc->m = args->uargs.m;
126  xdesc->w = args->uargs.w;
127 
128  if (xdesc->w <= 0)
129  xdesc->w = DEFAULT_W;
130 
131  /* Sample on how to pass extra args to the backend */
132  xdesc->arg1 = args->uargs.priv_args1.null_args.arg1;
133 
134  /* store w back in args so upper layer can get to it */
135  args->uargs.w = DEFAULT_W;
136 
137  /* validate EC arguments */
138  {
139  long long max_symbols;
140  if (xdesc->w != 8 && xdesc->w != 16 && xdesc->w != 32) {
141  goto error;
142  }
143  max_symbols = 1LL << xdesc->w;
144  if ((xdesc->k + xdesc->m) > max_symbols) {
145  goto error;
146  }
147  }
148 
149  /*
150  * ISO C forbids casting a void* to a function pointer.
151  * Since dlsym return returns a void*, we use this union to
152  * "transform" the void* to a function pointer.
153  */
154  union {
155  init_null_code_func initp;
156  null_code_encode_func encodep;
157  null_code_decode_func decodep;
158  null_reconstruct_func reconp;
159  null_code_fragments_needed_func fragsneededp;
160  void *vptr;
161  } func_handle = {.vptr = NULL};
162 
163  /* fill in function addresses */
164  func_handle.vptr = NULL;
165  func_handle.vptr = dlsym(backend_sohandle, "null_code_init");
166  xdesc->init_null_code = func_handle.initp;
167  if (NULL == xdesc->init_null_code) {
168  goto error;
169  }
170 
171  func_handle.vptr = NULL;
172  func_handle.vptr = dlsym(backend_sohandle, "null_code_encode");
173  xdesc->null_code_encode = func_handle.encodep;
174  if (NULL == xdesc->null_code_encode) {
175  goto error;
176  }
177 
178  func_handle.vptr = NULL;
179  func_handle.vptr = dlsym(backend_sohandle, "null_code_decode");
180  xdesc->null_code_decode = func_handle.decodep;
181  if (NULL == xdesc->null_code_decode) {
182  goto error;
183  }
184 
185  func_handle.vptr = NULL;
186  func_handle.vptr = dlsym(backend_sohandle, "null_reconstruct");
187  xdesc->null_reconstruct = func_handle.reconp;
188  if (NULL == xdesc->null_reconstruct) {
189  goto error;
190  }
191 
192  func_handle.vptr = NULL;
193  func_handle.vptr = dlsym(backend_sohandle, "null_code_fragments_needed");
194  xdesc->null_code_fragments_needed = func_handle.fragsneededp;
195  if (NULL == xdesc->null_code_fragments_needed) {
196  goto error;
197  }
198 
199  return (void *) xdesc;
200 
201 error:
202  free (xdesc);
203 
204  return NULL;
205 }
206 
207 static int null_exit(void *desc)
208 {
209  struct null_descriptor *xdesc = (struct null_descriptor *) desc;
210 
211  free (xdesc);
212  return 0;
213 }
214 
215 static bool null_is_compatible_with(uint32_t version) {
216  return true;
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 };
228 
229 struct ec_backend_common backend_null = {
230  .id = EC_BACKEND_NULL,
231  .name = NULL_LIB_NAME,
232  .soname = NULL_SO_NAME,
233  .soversion = NULL_LIB_VER_STR,
234  .ops = &null_op_stubs,
235  .backend_metadata_size = 0,
236  .ec_backend_version = _VERSION(NULL_LIB_MAJOR, NULL_LIB_MINOR,
237  NULL_LIB_REV),
238 };
239 
null_code_fragments_needed_func null_code_fragments_needed
Definition: null.c:68
int * matrix
Definition: null.c:71
#define DEFAULT_W
Definition: null.c:78
#define NULL_LIB_VER_STR
Definition: null.c:37
int arg1
Definition: null.c:75
int(* null_reconstruct_func)(char **, int, uint64_t, int, char *)
Definition: null.c:51
#define NULL_LIB_REV
Definition: null.c:36
struct ec_backend_common backend_null
Definition: null.c:229
static int null_exit(void *desc)
Definition: null.c:207
static int null_min_fragments(void *desc, int *missing_idxs, int *fragments_to_exclude, int *fragments_needed)
Definition: null.c:97
null_reconstruct_func null_reconstruct
Definition: null.c:65
void *(* init_null_code_func)(int, int, int)
Definition: null.c:48
int(* null_code_decode_func)(void *, char **, char **, int *, int, int)
Definition: null.c:50
static bool null_is_compatible_with(uint32_t version)
Definition: null.c:215
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:108
null_code_encode_func null_code_encode
Definition: null.c:59
#define NULL_LIB_MAJOR
Definition: null.c:34
static int null_decode(void *desc, char **data, char **parity, int *missing_idxs, int blocksize)
Definition: null.c:85
int(* null_code_encode_func)(void *, char **, char **, int)
Definition: null.c:49
null_code_decode_func null_code_decode
Definition: null.c:62
#define NULL_SO_NAME
Definition: null.c:42
static int null_reconstruct(void *desc, char **data, char **parity, int *missing_idxs, int destination_idx, int blocksize)
Definition: null.c:91
#define NULL_LIB_NAME
Definition: null.c:38
static void * null_init(struct ec_backend_args *args, void *backend_sohandle)
Definition: null.c:113
struct ec_backend_op_stubs null_op_stubs
Definition: null.c:218
struct ec_backend null
Definition: null.c:45
static int null_encode(void *desc, char **data, char **parity, int blocksize)
Definition: null.c:80
struct ec_backend_op_stubs null_ops
Definition: null.c:46
int(* null_code_fragments_needed_func)(void *, int *, int *, int *)
Definition: null.c:52
init_null_code_func init_null_code
Definition: null.c:56
#define NULL_LIB_MINOR
Definition: null.c:35