| 1 | #include "gsl__config.h" |
| 2 | #include "gsl_math.h" |
| 3 | #include "gsl_errno.h" |
| 4 | #include "gsl_sum.h" |
| 5 | |
| 6 | gsl_sum_levin_utrunc_workspace * |
| 7 | gsl_sum_levin_utrunc_alloc (size_t n) |
| 8 | { |
| 9 | gsl_sum_levin_utrunc_workspace * w; |
| 10 | |
| 11 | if (n == 0) |
| 12 | { |
| 13 | GSL_ERROR_VAL ("length n must be positive integer", GSL_EDOM, 0); |
| 14 | } |
| 15 | |
| 16 | w = (gsl_sum_levin_utrunc_workspace *) malloc(sizeof(gsl_sum_levin_utrunc_workspace)); |
| 17 | |
| 18 | if (w == NULL) |
| 19 | { |
| 20 | GSL_ERROR_VAL ("failed to allocate struct", GSL_ENOMEM, 0); |
| 21 | } |
| 22 | |
| 23 | w->q_num = (double *) malloc (n * sizeof (double)); |
| 24 | |
| 25 | if (w->q_num == NULL) |
| 26 | { |
| 27 | free(w) ; /* error in constructor, prevent memory leak */ |
| 28 | |
| 29 | GSL_ERROR_VAL ("failed to allocate space for q_num", GSL_ENOMEM, 0); |
| 30 | } |
| 31 | |
| 32 | w->q_den = (double *) malloc (n * sizeof (double)); |
| 33 | |
| 34 | if (w->q_den == NULL) |
| 35 | { |
| 36 | free (w->q_num); |
| 37 | free (w) ; /* error in constructor, prevent memory leak */ |
| 38 | |
| 39 | GSL_ERROR_VAL ("failed to allocate space for q_den", GSL_ENOMEM, 0); |
| 40 | } |
| 41 | |
| 42 | w->dsum = (double *) malloc (n * sizeof (double)); |
| 43 | |
| 44 | if (w->dsum == NULL) |
| 45 | { |
| 46 | free (w->q_den); |
| 47 | free (w->q_num); |
| 48 | free (w) ; /* error in constructor, prevent memory leak */ |
| 49 | |
| 50 | GSL_ERROR_VAL ("failed to allocate space for dsum", GSL_ENOMEM, 0); |
| 51 | } |
| 52 | |
| 53 | w->size = n; |
| 54 | w->terms_used = 0; |
| 55 | w->sum_plain = 0; |
| 56 | |
| 57 | return w; |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | gsl_sum_levin_utrunc_free (gsl_sum_levin_utrunc_workspace * w) |
| 62 | { |
| 63 | free (w->dsum); |
| 64 | free (w->q_den); |
| 65 | free (w->q_num); |
| 66 | free (w); |
| 67 | } |