0

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?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Pendyala
  • 585
  • 1
  • 6
  • 17
  • Well clang is open source, so you can search its code to see if the symbol is there. Built-ins are by definition things _built in to_ the compiler, so it seems like a good place to look. – Useless Oct 15 '19 at 09:07
  • 1
    note that this is implementation details that are specific to the particular compiler you are using. If you want to write portable C++ you should pretend that you know nothing about them – 463035818_is_not_an_ai Oct 15 '19 at 09:56

1 Answers1

8

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++.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Most peculiar... Would you happen to know _why_ "removing new / delete pairs and merging allocations" is forbidden by the standard? – nada Oct 15 '19 at 10:09
  • @nada - I'll have to dig into it to find out. But if you are curious it seems to me like it's worth posting a new question about it. – StoryTeller - Unslander Monica Oct 15 '19 at 10:11
  • Thanks. I made a [short question](https://stackoverflow.com/questions/58392330/why-does-the-c-standard-forbid-removing-new-delete-pairs-and-merging-allocat) about it. – nada Oct 15 '19 at 10:17