Commit 1b30a37c9f75df371cf4deaedfde6b843933b4f0

Authored by Loic Dachary
1 parent b8221e7d
Exists in master

add galois_init_default_field error code

galois_init_default_field returns an errno(3) code in case of error
instead of exiting. This is handy when the caller needs to perform
cleanup or error reporting when an error occurs instead of exit(2).

The exit(2) based error handling is preserved in the static
galois_init() function which is used in galois.c instead and is based on
galois_init_default_field to avoid code duplication.

Signed-off-by: Loic Dachary <loic@dachary.org>
Showing 2 changed files with 30 additions and 19 deletions   Show diff stats
include/galois.h
... ... @@ -46,7 +46,7 @@
46 46 extern "C" {
47 47 #endif
48 48  
49   -extern void galois_init_default_field(int w);
  49 +extern int galois_init_default_field(int w);
50 50 extern void galois_change_technique(gf_t *gf, int w);
51 51  
52 52 extern int galois_single_multiply(int a, int b, int w);
... ...
src/galois.c
... ... @@ -47,6 +47,7 @@
47 47 #include <stdio.h>
48 48 #include <stdlib.h>
49 49 #include <string.h>
  50 +#include <errno.h>
50 51  
51 52 #include "galois.h"
52 53  
... ... @@ -168,24 +169,34 @@ gf_t* galois_init_composite_field(int w,
168 169 return gfp;
169 170 }
170 171  
171   -void galois_init_default_field(int w)
  172 +int galois_init_default_field(int w)
  173 +{
  174 + if (gfp_array[w] == NULL) {
  175 + gfp_array[w] = (gf_t*)malloc(sizeof(gf_t));
  176 + if(gfp_array[w] == NULL)
  177 + return ENOMEM;
  178 + if (!gf_init_easy(gfp_array[w], w))
  179 + return EINVAL;
  180 + }
  181 + return 0;
  182 +}
  183 +
  184 +static void galois_init(int w)
172 185 {
173 186 if (w <= 0 || w > 32) {
174 187 fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
175 188 exit(1);
176 189 }
177 190  
178   - if (gfp_array[w] == NULL) {
179   - gfp_array[w] = (gf_t*)malloc(sizeof(gf_t));
180   - if (gfp_array[w] == NULL) {
181   - fprintf(stderr, "ERROR -- cannot allocate memory for Galois field w=%d\n", w);
182   - exit(1);
183   - }
184   - }
185   -
186   - if (!gf_init_easy(gfp_array[w], w)) {
  191 + switch (galois_init_default_field(w)) {
  192 + case ENOMEM:
  193 + fprintf(stderr, "ERROR -- cannot allocate memory for Galois field w=%d\n", w);
  194 + exit(1);
  195 + break;
  196 + case EINVAL:
187 197 fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
188 198 exit(1);
  199 + break;
189 200 }
190 201 }
191 202  
... ... @@ -243,7 +254,7 @@ int galois_single_multiply(int x, int y, int w)
243 254 if (x == 0 || y == 0) return 0;
244 255  
245 256 if (gfp_array[w] == NULL) {
246   - galois_init_default_field(w);
  257 + galois_init(w);
247 258 }
248 259  
249 260 if (w <= 32) {
... ... @@ -260,7 +271,7 @@ int galois_single_divide(int x, int y, int w)
260 271 if (y == 0) return -1;
261 272  
262 273 if (gfp_array[w] == NULL) {
263   - galois_init_default_field(w);
  274 + galois_init(w);
264 275 }
265 276  
266 277 if (w <= 32) {
... ... @@ -278,7 +289,7 @@ void galois_w08_region_multiply(char *region, /* Region to multiply */
278 289 int add)
279 290 {
280 291 if (gfp_array[8] == NULL) {
281   - galois_init_default_field(8);
  292 + galois_init(8);
282 293 }
283 294 gfp_array[8]->multiply_region.w32(gfp_array[8], region, r2, multby, nbytes, add);
284 295 }
... ... @@ -290,7 +301,7 @@ void galois_w16_region_multiply(char *region, /* Region to multiply */
290 301 int add)
291 302 {
292 303 if (gfp_array[16] == NULL) {
293   - galois_init_default_field(16);
  304 + galois_init(16);
294 305 }
295 306 gfp_array[16]->multiply_region.w32(gfp_array[16], region, r2, multby, nbytes, add);
296 307 }
... ... @@ -303,7 +314,7 @@ void galois_w32_region_multiply(char *region, /* Region to multiply */
303 314 int add)
304 315 {
305 316 if (gfp_array[32] == NULL) {
306   - galois_init_default_field(32);
  317 + galois_init(32);
307 318 }
308 319 gfp_array[32]->multiply_region.w32(gfp_array[32], region, r2, multby, nbytes, add);
309 320 }
... ... @@ -311,7 +322,7 @@ void galois_w32_region_multiply(char *region, /* Region to multiply */
311 322 void galois_w8_region_xor(void *src, void *dest, int nbytes)
312 323 {
313 324 if (gfp_array[8] == NULL) {
314   - galois_init_default_field(8);
  325 + galois_init(8);
315 326 }
316 327 gfp_array[8]->multiply_region.w32(gfp_array[32], src, dest, 1, nbytes, 1);
317 328 }
... ... @@ -319,7 +330,7 @@ void galois_w8_region_xor(void *src, void *dest, int nbytes)
319 330 void galois_w16_region_xor(void *src, void *dest, int nbytes)
320 331 {
321 332 if (gfp_array[16] == NULL) {
322   - galois_init_default_field(16);
  333 + galois_init(16);
323 334 }
324 335 gfp_array[16]->multiply_region.w32(gfp_array[16], src, dest, 1, nbytes, 1);
325 336 }
... ... @@ -327,7 +338,7 @@ void galois_w16_region_xor(void *src, void *dest, int nbytes)
327 338 void galois_w32_region_xor(void *src, void *dest, int nbytes)
328 339 {
329 340 if (gfp_array[32] == NULL) {
330   - galois_init_default_field(32);
  341 + galois_init(32);
331 342 }
332 343 gfp_array[32]->multiply_region.w32(gfp_array[32], src, dest, 1, nbytes, 1);
333 344 }
... ...