问题
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