Changeset 1bd82e0 for memlog.cpp


Ignore:
Timestamp:
05/29/15 07:53:00 (9 years ago)
Author:
Hal Finkel <hfinkel@…>
Branches:
master
Children:
e1b15ec
Parents:
966f5de
git-author:
Hal Finkel <hfinkel@…> (05/29/15 07:53:00)
git-committer:
Hal Finkel <hfinkel@…> (05/29/15 07:53:00)
Message:

adjust for c++, use caching of dladdr, fixup for short names when making dot

File:
1 moved

Legend:

Unmodified
Added
Removed
  • memlog.cpp

    r966f5de r1bd82e0  
    33#endif 
    44 
    5 #include <stdlib.h> 
    6 #include <stdio.h> 
     5#include <cstdlib> 
     6#include <cstdio> 
     7#include <cstring> 
     8 
     9#include <unordered_map> 
     10#include <utility> 
     11 
    712#include <limits.h> 
    8 #include <string.h> 
    9  
    1013#include <malloc.h> 
    1114#include <execinfo.h> 
     
    2225#include <dlfcn.h> 
    2326 
     27using namespace std; 
     28 
    2429// NOTE: When static linking, this depends on linker wrapping. 
    2530// Add to your LDFLAGS: 
     
    6873// dladdr is, relatively, quit slow. For this to work on a large application, 
    6974// we need to cache the lookup results. 
    70 static int dladdr_cached(void *addr, Dl_info *info) { 
    71   return dladdr(addr, info); 
     75static int dladdr_cached(void * addr, Dl_info *info) { 
     76  static unordered_map<void *, Dl_info> dladdr_cache; 
     77 
     78  auto I = dladdr_cache.find(addr); 
     79  if (I == dladdr_cache.end()) { 
     80    int r; 
     81    if (!(r = dladdr(addr, info))) 
     82      memset(info, 0, sizeof(Dl_info)); 
     83 
     84    dladdr_cache.insert(make_pair(addr, *info)); 
     85    return r; 
     86  } 
     87 
     88  memcpy(info, &I->second, sizeof(Dl_info)); 
     89  return 1; 
    7290} 
    7391 
     
    176194// glibc exports its underlying malloc implementation under the name 
    177195// __libc_malloc so that hooks like this can use it. 
     196extern "C" { 
    178197extern void *__libc_malloc(size_t size); 
    179198extern void *__libc_realloc(void *ptr, size_t size); 
     
    277296} 
    278297 
     298} // extern "C" 
     299 
Note: See TracChangeset for help on using the changeset viewer.