25 #if !defined(ION_ALLOC_TRACKER_DEFINED)
26 # error alloctracker.cc requires the --track-allocations build option.
40 static ion::base::AllocTracker* GetTracker() {
41 return ion::base::AllocTracker::GetMutableInstance();
48 static T* CreateWithPlacementNew() {
49 void* ptr = malloc(
sizeof(
T));
66 class AllocTracker::Helper {
69 : internal_allocator_(CreateWithPlacementNew<InternalAllocator>()),
70 size_map_(internal_allocator_) {}
75 void AddSize(
void* ptr,
size_t size) {
76 size_map_[ptr] = size;
78 size_t FindAndRemoveSize(
void* ptr) {
80 SizeMap::iterator it = size_map_.find(ptr);
81 if (it != size_map_.end()) {
92 virtual void* Allocate(
size_t size) {
97 return tracker->New(size, kInternalAlloc);
102 virtual void Deallocate(
void* ptr) {
107 virtual ~InternalAllocator() {}
113 InternalAllocatorPtr internal_allocator_;
128 AllocTracker* AllocTracker::GetMutableInstance() {
131 static AllocTracker* tracker = CreateWithPlacementNew<AllocTracker>();
135 const AllocTracker& AllocTracker::GetInstance() {
136 return *GetMutableInstance();
139 AllocTracker::AllocTracker() {
143 helper_.reset(CreateWithPlacementNew<Helper>());
146 AllocTracker::~AllocTracker() {
149 void AllocTracker::SetBaseline() {
150 baseline_counts_ = all_counts_;
153 void* AllocTracker::New(std::size_t size, AllocType
type) {
154 void* ptr = malloc(size);
157 TypeCounts &all_c = all_counts_.counts[
type];
158 TypeCounts &open_c = open_counts_.counts[
type];
162 open_c.bytes += size;
166 if (type != kInternalAlloc)
167 helper_->AddSize(ptr, size);
172 void AllocTracker::Delete(
void* ptr, AllocType type) {
173 size_t bytes = type == kInternalAlloc ? 0 : helper_->FindAndRemoveSize(ptr);
176 TypeCounts &open_c = open_counts_.counts[
type];
179 if (open_c.bytes >= bytes)
180 open_c.bytes -= bytes;
196 using ion::base::AllocTracker;
198 void*
operator new(std::size_t size) {
199 return GetTracker()->New(size, AllocTracker::kNonArrayAlloc);
202 void*
operator new[](std::size_t size) {
203 return GetTracker()->New(size, AllocTracker::kArrayAlloc);
206 void operator delete(
void* ptr) {
207 GetTracker()->Delete(ptr, AllocTracker::kNonArrayAlloc);
210 void operator delete[](
void* ptr) {
211 GetTracker()->Delete(ptr, AllocTracker::kArrayAlloc);
const AllocationTrackerPtr & GetTracker() const
Allocator is an abstract base class for a memory allocator used for Ion objects derived from Allocata...