What is __builtin_operator_new and how it works?

断了今生、忘了曾经 提交于 2019-12-11 15:26:47

问题


While going through libc++ code, I see ndk allocators __allocate function is calling __builtin_operator_new, but I could not found its definition in libc++ code.

By name it is evident that it is memory allocation function. But who implements it? Is it defined by compiler like clang, gcc? where can I find its definition?


回答1:


It's an intrinsic, defined implicitly by the compiler itself (hence why it's called a builtin). It's documented on the language extension section for Clang:

__builtin_operator_new and __builtin_operator_delete

__builtin_operator_new allocates memory just like a non-placement non-class new-expression. This is exactly like directly calling the normal non-placement ::operator new, except that it allows certain optimizations that the C++ standard does not permit for a direct function call to ::operator new (in particular, removing new / delete pairs and merging allocations).

Likewise, __builtin_operator_delete deallocates memory just like a non-class delete-expression, and is exactly like directly calling the normal ::operator delete, except that it permits optimizations. Only the unsized form of __builtin_operator_delete is currently available.

These builtins are intended for use in the implementation of std::allocator and other similar allocation libraries, and are only available in C++.



来源:https://stackoverflow.com/questions/58390998/what-is-builtin-operator-new-and-how-it-works

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!