Chromium Code Reviews| Index: third_party/tcmalloc/chromium/src/allocated_type_map.cc |
| diff --git a/third_party/tcmalloc/chromium/src/allocated_type_map.cc b/third_party/tcmalloc/chromium/src/allocated_type_map.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9b8b4107b024bb6735c4b00e98b59d2003e47b67 |
| --- /dev/null |
| +++ b/third_party/tcmalloc/chromium/src/allocated_type_map.cc |
| @@ -0,0 +1,106 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <config.h> |
| + |
| +#include <new> |
| +#include <typeinfo> |
| + |
| +#include <gperftools/allocated_type_map.h> |
| + |
| +#include "addressmap-inl.h" |
| +#include "tcmalloc_guard.h" |
| +#include "base/spinlock.h" |
|
Alexander Potapenko
2012/08/03 09:57:07
Mind the order of includes, please.
Dai Mikurube (NOT FULLTIME)
2012/08/07 10:39:18
Done.
|
| +#include "base/low_level_alloc.h" |
| + |
| + |
| +//---------------------------------------------------------------------- |
| +// Locking |
| +//---------------------------------------------------------------------- |
| + |
| +static SpinLock allocated_type_lock(SpinLock::LINKER_INITIALIZED); |
| + |
| +//---------------------------------------------------------------------- |
| +// Simple allocator for allocated type map's internal memory |
| +//---------------------------------------------------------------------- |
| + |
| +static LowLevelAlloc::Arena* allocated_type_map_memory = NULL; |
| + |
| +static void* AllocatedTypeMalloc(size_t bytes) { |
| + return LowLevelAlloc::AllocWithArena(bytes, allocated_type_map_memory); |
| +} |
| +static void AllocatedTypeFree(void* p) { |
| + LowLevelAlloc::Free(p); |
| +} |
| + |
| +//---------------------------------------------------------------------- |
| +// Profiling control/state data |
| +//---------------------------------------------------------------------- |
| + |
| +static AddressMap<AllocatedObject>* allocated_type_map = NULL; |
|
Alexander Potapenko
2012/08/02 14:57:12
How about keeping a per-thread allocated_type_map.
Dai Mikurube (NOT FULLTIME)
2012/08/02 16:14:35
I thought about that. But in my thought, the prob
jar (doing other things)
2012/08/02 23:38:30
Allocation on one thread, and deletion on another
Dai Mikurube (NOT FULLTIME)
2012/08/03 10:01:50
Thanks. We cannot use per-thread storage for this
|
| + |
| +//---------------------------------------------------------------------- |
| +// Manage allocated type map |
| +//---------------------------------------------------------------------- |
| + |
| +static void InitializeAllocatedTypeMemory() { |
| + if (allocated_type_map_memory == NULL) { |
| + allocated_type_map_memory = |
| + LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena()); |
| + } |
| + |
| + if (allocated_type_map == NULL) { |
| + allocated_type_map = |
| + new(AllocatedTypeMalloc(sizeof(AddressMap<AllocatedObject>))) |
| + AddressMap<AllocatedObject>(AllocatedTypeMalloc, AllocatedTypeFree); |
| + } |
| +} |
| + |
| +extern "C" void InsertAllocatedType( |
| + void* address, unsigned int size, const std::type_info& type) { |
|
jar (doing other things)
2012/08/02 23:38:30
nit: align parameters, one per line, indented to m
Dai Mikurube (NOT FULLTIME)
2012/08/03 10:01:50
Done.
|
| + if (allocated_type_lock.IsHeld()) { |
|
jar (doing other things)
2012/08/02 23:38:30
I suspect you're trying to use this as recursion p
Dai Mikurube (NOT FULLTIME)
2012/08/03 10:01:50
They were just-in-case code when I was doing heavy
|
| + return; |
| + } |
| + SpinLockHolder l(&allocated_type_lock); |
| + InitializeAllocatedTypeMemory(); |
| + |
| + allocated_type_map->Insert(address, AllocatedObject(size, &type)); |
| +} |
| + |
| +extern "C" void EraseAllocatedType(void* address) { |
| + if (allocated_type_lock.IsHeld()) { |
| + return; |
| + } |
| + SpinLockHolder l(&allocated_type_lock); |
| + InitializeAllocatedTypeMemory(); |
| + |
| + AllocatedObject obj; |
| + allocated_type_map->FindAndRemove(address, &obj); |
| +} |
| + |
| +extern "C" const std::type_info* LookupAllocatedType(const void* address) { |
| + if (allocated_type_lock.IsHeld()) { |
| + return NULL; |
| + } |
| + SpinLockHolder l(&allocated_type_lock); |
| + InitializeAllocatedTypeMemory(); |
| + |
| + const AllocatedObject* found = allocated_type_map->Find(address); |
| + if (found == NULL) |
| + return NULL; |
| + return found->type; |
| +} |
| + |
| +extern "C" void IterateAllocatedType( |
| + void (*callback)(const void*, AllocatedObject*, void*), void* arg) { |
|
jar (doing other things)
2012/08/02 23:38:30
This class of API is always frightening to me. Yo
Dai Mikurube (NOT FULLTIME)
2012/08/03 10:01:50
In fact, I was a little worried about that. The u
|
| + if (allocated_type_lock.IsHeld()) { |
| + return; |
| + } |
| + SpinLockHolder l(&allocated_type_lock); |
| + InitializeAllocatedTypeMemory(); |
| + |
| + allocated_type_map->Iterate(callback, arg); |
| +} |
| + |
| +static const TCMallocGuard tcmalloc_initializer; |