diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cm/p1363.c | 4 | ||||
| -rw-r--r-- | src/exhaustive/anomalous.c | 4 | ||||
| -rw-r--r-- | src/exhaustive/arg.c | 4 | ||||
| -rw-r--r-- | src/gen/curve.c | 2 | ||||
| -rw-r--r-- | src/gen/point.c | 4 | ||||
| -rw-r--r-- | src/gen/seed.c | 2 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 4 | ||||
| -rw-r--r-- | src/io/output.c | 14 | ||||
| -rw-r--r-- | src/util/memory.c | 30 | ||||
| -rw-r--r-- | src/util/memory.h | 17 |
10 files changed, 63 insertions, 22 deletions
diff --git a/src/cm/p1363.c b/src/cm/p1363.c index d9fe943..6e428cb 100644 --- a/src/cm/p1363.c +++ b/src/cm/p1363.c @@ -70,11 +70,11 @@ void p1363_free(form_t ***forms, size_t nforms) { if (*forms) { for (size_t i = 0; i < nforms; ++i) { if ((*forms)[i]) { - pari_free((*forms)[i]); + try_free((*forms)[i]); (*forms)[i] = NULL; } } - pari_free(*forms); + try_free(*forms); *forms = NULL; } } diff --git a/src/exhaustive/anomalous.c b/src/exhaustive/anomalous.c index 507d8ca..7edab5e 100644 --- a/src/exhaustive/anomalous.c +++ b/src/exhaustive/anomalous.c @@ -138,9 +138,9 @@ void anomalous_quit() { if (disc_table) { for (int i = 0; i < 5; ++i) { if (disc_table[i]) { - pari_free(disc_table[i]); + try_free(disc_table[i]); } } - pari_free(disc_table); + try_free(disc_table); } } diff --git a/src/exhaustive/arg.c b/src/exhaustive/arg.c index 6b11777..ba96357 100644 --- a/src/exhaustive/arg.c +++ b/src/exhaustive/arg.c @@ -10,10 +10,10 @@ arg_t *arg_new(void) { return try_calloc(sizeof(arg_t)); } void arg_free(arg_t **arg) { if (*arg) { if ((*arg)->allocd) { - pari_free((*arg)->allocd); + try_free((*arg)->allocd); (*arg)->allocd = NULL; } - pari_free(*arg); + try_free(*arg); *arg = NULL; } } diff --git a/src/gen/curve.c b/src/gen/curve.c index bc382cd..ea5ed95 100644 --- a/src/gen/curve.c +++ b/src/gen/curve.c @@ -82,7 +82,7 @@ void curve_free(curve_t **curve) { gunclone((*curve)->order); } - pari_free(*curve); + try_free(*curve); *curve = NULL; } } diff --git a/src/gen/point.c b/src/gen/point.c index 42f20db..2a5a464 100644 --- a/src/gen/point.c +++ b/src/gen/point.c @@ -44,7 +44,7 @@ void point_free(point_t **point) { if ((*point)->cofactor && isclone((*point)->cofactor)) { gunclone((*point)->cofactor); } - pari_free(*point); + try_free(*point); *point = NULL; } } @@ -77,7 +77,7 @@ point_t **points_new_clone(point_t **const src, size_t num) { void points_free(point_t ***points) { if (*points) { - pari_free(*points); + try_free(*points); *points = NULL; } } diff --git a/src/gen/seed.c b/src/gen/seed.c index f26a9a2..e131879 100644 --- a/src/gen/seed.c +++ b/src/gen/seed.c @@ -33,7 +33,7 @@ void seed_free(seed_t **seed) { if ((*seed)->seed && isclone((*seed)->seed)) { gunclone((*seed)->seed); } - pari_free(*seed); + try_free(*seed); *seed = NULL; } } diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 1d51a15..cd00b43 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -326,8 +326,8 @@ int invalid_do(config_t *cfg) { for (size_t i = 0; i < ncurves; ++i) { curve_free(&curves[i]); } - pari_free(curves); - pari_free(primes); + try_free(curves); + try_free(primes); curve_free(&curve); debug_log_end("Finished Invalid curve method"); diff --git a/src/io/output.c b/src/io/output.c index 272faa2..f3c50e0 100644 --- a/src/io/output.c +++ b/src/io/output.c @@ -59,7 +59,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) { len += strlen(gens[i]); } size_t lenn = sizeof(char) * (len + curve->ngens); - params[OFFSET_GENERATORS] = try_calloc(lenn); + params[OFFSET_GENERATORS] = pari_calloc(lenn); for (size_t i = 0; i < curve->ngens; ++i) { if (i > 0) strncat(params[OFFSET_GENERATORS], ",", lenn - 1); strncat(params[OFFSET_GENERATORS], gens[i], lenn - 1); @@ -83,7 +83,7 @@ char *output_scsv(curve_t *curve, const config_t *cfg) { len += strlen(points[i]); } size_t lenn = sizeof(char) * (len + curve->npoints); - params[OFFSET_POINTS] = try_calloc(lenn); + params[OFFSET_POINTS] = pari_calloc(lenn); for (size_t i = 0; i < curve->npoints; ++i) { if (i > 0) strncat(params[OFFSET_POINTS], ",", lenn - 1); strncat(params[OFFSET_POINTS], points[i], lenn - 1); @@ -254,7 +254,7 @@ void output_f(FILE *out, curve_t *curve, const config_t *cfg) { char *s = output_s(curve, cfg); if (s) { fprintf(out, "%s", s); - pari_free(s); + try_free(s); } } @@ -266,7 +266,7 @@ void output_f_separator(FILE *out, const config_t *cfg) { char *s = output_s_separator(cfg); if (s) { fprintf(out, "%s", s); - pari_free(s); + try_free(s); } } @@ -276,7 +276,7 @@ void output_f_begin(FILE *out, const config_t *cfg) { char *s = output_s_begin(cfg); if (s) { fprintf(out, "%s", s); - pari_free(s); + try_free(s); } } @@ -286,14 +286,14 @@ void output_f_end(FILE *out, const config_t *cfg) { char *s = output_s_end(cfg); if (s) { fprintf(out, "%s", s); - pari_free(s); + try_free(s); } } void output_o_end(const config_t *cfg) { output_f_end(out, cfg); } bool output_init(const config_t *cfg) { - json_set_allocation_functions(try_malloc, pari_free); + json_set_allocation_functions(try_malloc, try_free); if (cfg->output) { out = fopen(cfg->output, cfg->append ? "a" : "w"); diff --git a/src/util/memory.c b/src/util/memory.c index d7d4b48..53f68c9 100644 --- a/src/util/memory.c +++ b/src/util/memory.c @@ -5,6 +5,15 @@ #include "memory.h" #include <pari/pari.h> + +static void *(*malloc_func)(size_t) = pari_malloc; + +static void *(*calloc_func)(size_t) = pari_calloc; + +static void *(*realloc_func)(void *, size_t) = pari_realloc; + +static void (*free_func)(void *) = pari_free; + void *alloc(void *(*fun)(size_t), size_t size) { void *result = fun(size); if (!result) { @@ -14,15 +23,30 @@ void *alloc(void *(*fun)(size_t), size_t size) { return result; } -void *try_malloc(size_t size) { return alloc(pari_malloc, size); } +void *try_malloc(size_t size) { return alloc(malloc_func, size); } -void *try_calloc(size_t size) { return alloc(pari_calloc, size); } +void *try_calloc(size_t size) { + return alloc(calloc_func, size); +} void *try_realloc(void *ptr, size_t size) { - void *result = pari_realloc(ptr, size); + void *result = realloc_func(ptr, size); if (!result) { perror("Couldn't alloc."); exit(EXIT_FAILURE); } return result; } + +void try_free(void *ptr) { + free_func(ptr); +} + +void set_mem_funcs(void *(*malloc_fun)(size_t), void *(*calloc_fun)(size_t), + void *(*realloc_fun)(void *, size_t), + void(*free_fun)(void *)) { + malloc_func = malloc_fun; + calloc_func = calloc_fun; + realloc_func = realloc_fun; + free_func = free_fun; +}
\ No newline at end of file diff --git a/src/util/memory.h b/src/util/memory.h index 55e8eac..7070430 100644 --- a/src/util/memory.h +++ b/src/util/memory.h @@ -32,4 +32,21 @@ void *try_calloc(size_t size); */ void *try_realloc(void *ptr, size_t size); +/** + * @brief + * @param ptr + */ +void try_free(void *ptr); + +/** + * @brief + * @param malloc_fun + * @param calloc_fun + * @param realloc_fun + * @param free_fun + */ +void set_mem_funcs(void *(*malloc_fun)(size_t), void *(*calloc_fun)(size_t), + void *(*realloc_fun)(void *, size_t), + void(*free_fun)(void *)); + #endif // ECGEN_MEMORY_H |
