Using the GMP library with Boehm's Garbage Collector

152 Views Asked by At

So...

I've set up Boehm's GC and want to make the GMP library use it.

This is what I'm doing right now:

    //===================
    // Definitions
    //===================

    #include <gc.h>

    #define MALLOC(x)       GC_malloc(x)
    #define XALLOC(x)       GC_malloc_atomic(x)
    #define CALLOC(n,x)     GC_malloc((n)*(x))
    #define REALLOC(p,x)    GC_realloc((p),(x))
    #define FREE(x)         (x) = NULL
    #define FREENOW(x)      GC_free(x)

    //===================
    // Helpers
    //===================

    void* allocate_function (size_t alloc_size) {
        return MALLOC(alloc_size);
    }

    void* reallocate_function (void *ptr, size_t old_size, size_t new_size) {
        return REALLOC(ptr, new_size);
    }

    void deallocate_function (void *ptr, size_t size) {
        FREE(ptr);
    }

    //===================
    // Main code
    //===================

    int main(int argc, char** argv) {

        mp_set_memory_functions(&allocate_function, 
                                &reallocate_function, 
                                &deallocate_function);

        // ...
        // rest of the code
        // ...
    }

Am I doing it right?

Is there anything to take into account?

0

There are 0 best solutions below