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