将GMP库与Boehm的垃圾收集器一起使用

时间:2019-12-26 13:32:01

标签: c garbage-collection malloc gmp boehm-gc

所以...

我已经设置了Boehm的GC,并希望让GMP库使用它。

这就是我现在正在做的事情:

    //===================
    // 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
        // ...
    }

我做对了吗?

有什么要考虑的吗?

0 个答案:

没有答案