Is there a way to redefine malloc at link time on Windows?

前端 未结 4 969
我在风中等你
我在风中等你 2020-12-23 23:34

I would like to replace the default malloc at link time to use a custom malloc. But when I try to redefine malloc in my program, I get this error:

MSVCRT.lib         


        
相关标签:
4条回答
  • 2020-12-24 00:06

    What about defining malloc=_custom_malloc in the project makefile. Than adding a file such as:

    my_memory.c
    #undef malloc
    #undef calloc
    ...
    void *_custom_malloc(int size) { return jmalloc(size); }
    void *_custom_calloc(int size) { return jcalloc(size); }
    ...
    
    0 讨论(0)
  • 2020-12-24 00:12

    I think it depends in which order you link the files. I think you need to link your custom function first, then the import library.

    0 讨论(0)
  • 2020-12-24 00:13

    From version 3.0 Firefox uses a custom allocator (AFAIR jmalloc) -- you could check how they did it. I read that they had some problems with it. You may check this blog post.

    0 讨论(0)
  • 2020-12-24 00:20

    There is really good discussion of how hard this is here:

    http://benjamin.smedbergs.us/blog/2008-01-10/patching-the-windows-crt/

    Apparently, you need to patch the CRT

    Edit: actually, a MS employee gave the technique in the discussion. You need to move your malloc to a lib, and then link it before the CRT

    "he also mentions that if you link your malloc as a lib before the CRT (i.e. make sure to turn on ‘ignore default libs’ and explictly include the CRT), you’ll get what you want, and can redistribute this lib without problems."

    0 讨论(0)
提交回复
热议问题