diff --git a/gc.c b/gc.c index 892f0451..571c653e 100644 --- a/gc.c +++ b/gc.c @@ -587,6 +587,25 @@ void *gc_try_alloc(gc_heap * h, int heap_type, size_t size, char *obj, return NULL; } +// A convenience function for allocating bignums +void *gc_alloc_bignum(gc_thread_data *data) +{ + int heap_grown, result; + bignum_type *bn; + bignum_type tmp; + tmp.hdr.mark = gc_color_red; + tmp.hdr.grayed = 0; + tmp.tag = bignum_tag; + bn = gc_alloc(((gc_thread_data *)data)->heap, sizeof(bignum_type), (char *)(&tmp), (gc_thread_data *)data, &heap_grown); + + if ((result = mp_init(&bignum_value(bn))) != MP_OKAY) { + fprintf(stderr, "Error initializing number %s", + mp_error_to_string(result)); + exit(1); + } + return bn; +} + void *gc_alloc(gc_heap_root * hrt, size_t size, char *obj, gc_thread_data * thd, int *heap_grown) { diff --git a/include/cyclone/types.h b/include/cyclone/types.h index d5424b47..68153ba3 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -367,6 +367,9 @@ typedef struct { mp_int bn; } bignum_type; +#define alloc_bignum(data, p) \ + bignum_type *p = gc_alloc_bignum((gc_thread_data *)data); + #define make_empty_bignum(n) \ bignum_type n; \ n.hdr.mark = gc_color_red; \ @@ -375,12 +378,6 @@ typedef struct { mp_init(&(n.bn)); /* TODO: check return value of mp_init */ -#define init_empty_bignum(n) \ - n.hdr.mark = gc_color_red; \ - n.hdr.grayed = 0; \ - n.tag = bignum_tag; \ - mp_init(&(n.bn)); -/* TODO: check return value of mp_init */ #define assign_empty_bignum(pobj) \ ((bignum_type *)pobj)->hdr.mark = gc_color_red; \ ((bignum_type *)pobj)->hdr.grayed = 0; \ @@ -710,6 +707,7 @@ void *gc_try_alloc(gc_heap * h, int heap_type, size_t size, char *obj, gc_thread_data * thd); void *gc_alloc(gc_heap_root * h, size_t size, char *obj, gc_thread_data * thd, int *heap_grown); +void *gc_alloc_bignum(gc_thread_data *data); size_t gc_allocated_bytes(object obj, gc_free_list * q, gc_free_list * r); gc_heap *gc_heap_last(gc_heap * h); size_t gc_heap_total_size(gc_heap * h);