<div style="font-family:arial,helvetica,sans-serif;font-size:10pt"><div dir="ltr">Hi cfe-dev,<br clear="all"><div><br></div><div style>I'd like to propose a way of Type-Aware Memory Profiling in Clang.  It was one of the lightning talks at the DevMtg by Nico Weber on behalf of me.  Slides and Video were recently published.</div>

<div>* <a href="http://llvm.org/devmtg/2012-11/Weber_TypeAwareMemoryProfiling.pdf">http://llvm.org/devmtg/2012-11/Weber_TypeAwareMemoryProfiling.pdf</a></div><div style>* <a href="http://llvm.org/devmtg/2012-11/videos/Weber_TypeAwareMemoryProfiling.mp4">http://llvm.org/devmtg/2012-11/videos/Weber_TypeAwareMemoryProfiling.mp4</a></div>

<div style><br></div><div style>Object's type information is useful to profile memory usage in a process.  Type info is, however, hard to use in C++ programs.  Our proposal is to make it easy by compiler's help.  We implemented an early prototype, and the prototype has been working in Chromium project for a couple of months.</div>

<div><br></div><div><br></div><div style>What do you think?  I wonder if I could have your opinions about it.  The details are discussed in this document.  <a href="https://docs.google.com/document/d/1zyJ3FJhheJ5iSxYaMXARkZcR61tx6r44euMJbWw8jFY/edit">https://docs.google.com/document/d/1zyJ3FJhheJ5iSxYaMXARkZcR61tx6r44euMJbWw8jFY/edit</a></div>

<div style><br></div><div style>tl;dr, we did it by user-defined intercepting functions for operator new and delete.  If a developer defines a function __op_new_intercept__ (and if a compiler option is given!), the function intercepts operator new calls.</div>

<div style><br></div><div style><br></div><div style>An example follows :</div><div style><br></div><div style>$ cat typeaware.cc</div><div style><div>#include <stdio.h></div><div>#include <typeinfo></div><div>

<br></div><div>struct Klass {</div><div>  int x, y, z;</div><div>};</div><div><br></div><div>void* __op_new_intercept__(</div><div>    void* address, std::size_t size, const std::type_info& type) {</div><div>  printf("Allocated %lu bytes for %s at %016p.\n",</div>

<div>         size, <a href="http://type.name">type.name</a>(), address);</div><div>  return address;</div><div>}</div><div><br></div><div>void* __op_delete_intercept__(</div><div>    void* address, std::size_t size, const std::type_info& type) {</div>

<div>  printf("Deallocated %lu bytes for %s at %016p.\n",</div><div>         size, <a href="http://type.name">type.name</a>(), address);</div><div>  return address;</div><div>}</div><div><br></div><div>int main() {</div>

<div>  int *i;</div><div>  Klass *k;</div><div>  i = new int(3);</div><div>  k = new Klass();</div><div>  delete k;</div><div>  delete i;</div><div>  return 0;</div><div>}</div><div><br></div><div>$ clang++ -fintercept-allocation-functions typeaware.cc</div>

<div><br></div><div style>$ ./a.out</div><div style><div>Allocated 4 bytes for i at 0x000000022df010.</div><div>Allocated 12 bytes for 5Klass at 0x000000022df030.</div><div>Deallocated 12 bytes for 5Klass at 0x000000022df030.</div>

<div>Deallocated 4 bytes for i at 0x000000022df010.</div></div><div><br></div></div><div style>-- <br></div><div>Dai MIKURUBE</div><div>   <a href="mailto:dmikurube@chromium.org" target="_blank" class="cremed">dmikurube@chromium.org</a></div>

<br>



</div></div>