How to control memory allocation strategy in third party library code?

前端 未结 3 1889
无人共我
无人共我 2020-12-09 01:48

Previous header: \"Must I replace global operators new and delete to change memory allocation strategy in third party code?\"

Short story: We need t

3条回答
  •  暖寄归人
    2020-12-09 01:59

    Without being able to modify the library's source code - or, better, being able to influence the author of the library to modify it - I'd say you're out of luck.

    There are some things the library potentially can do (even unintentionally) to make it immune to any strategy you might employ - or, in worst cases, have the result that your usage would make the library unstable or it might make your program unstable. Such as using its own custom allocators, providing its own versions of global operator new() and operator delete(), overriding those operators in individual classes, etc.

    A strategy which would probably work is to work with the library vendor and make some modifications. The modifications (from your end) would amount to being able to initialise the library by specifying allocators it uses. For the library the effort is potentially significant (having to touch all functions that dynamically allocate memory, that use standard containers, etc) but not intractable - use the supplied allocators (or sensible defaults) throughout their code.

    Unfortunately, that is at odds with your requirement to not modify the library - I am skeptical of the chances of satisfying that, particularly within constraints you have outlined (memory-thirsty, hosted on windows/linux, etc).

提交回复
热议问题