1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "allocator.h" 18 19 #include <inttypes.h> 20 #include <stdlib.h> 21 22 #include <android-base/logging.h> 23 24 #include "base/atomic.h" 25 26 namespace art { 27 28 class MallocAllocator FINAL : public Allocator { 29 public: MallocAllocator()30 MallocAllocator() {} ~MallocAllocator()31 ~MallocAllocator() {} 32 Alloc(size_t size)33 void* Alloc(size_t size) { 34 return calloc(sizeof(uint8_t), size); 35 } 36 Free(void * p)37 void Free(void* p) { 38 free(p); 39 } 40 41 private: 42 DISALLOW_COPY_AND_ASSIGN(MallocAllocator); 43 }; 44 45 MallocAllocator g_malloc_allocator; 46 47 class NoopAllocator FINAL : public Allocator { 48 public: NoopAllocator()49 NoopAllocator() {} ~NoopAllocator()50 ~NoopAllocator() {} 51 Alloc(size_t size ATTRIBUTE_UNUSED)52 void* Alloc(size_t size ATTRIBUTE_UNUSED) { 53 LOG(FATAL) << "NoopAllocator::Alloc should not be called"; 54 UNREACHABLE(); 55 } 56 Free(void * p ATTRIBUTE_UNUSED)57 void Free(void* p ATTRIBUTE_UNUSED) { 58 // Noop. 59 } 60 61 private: 62 DISALLOW_COPY_AND_ASSIGN(NoopAllocator); 63 }; 64 65 NoopAllocator g_noop_allocator; 66 GetMallocAllocator()67Allocator* Allocator::GetMallocAllocator() { 68 return &g_malloc_allocator; 69 } 70 GetNoopAllocator()71Allocator* Allocator::GetNoopAllocator() { 72 return &g_noop_allocator; 73 } 74 75 namespace TrackedAllocators { 76 77 // These globals are safe since they don't have any non-trivial destructors. 78 Atomic<size_t> g_bytes_used[kAllocatorTagCount]; 79 volatile size_t g_max_bytes_used[kAllocatorTagCount]; 80 Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount]; 81 Dump(std::ostream & os)82void Dump(std::ostream& os) { 83 if (kEnableTrackingAllocator) { 84 os << "Dumping native memory usage\n"; 85 for (size_t i = 0; i < kAllocatorTagCount; ++i) { 86 uint64_t bytes_used = g_bytes_used[i].LoadRelaxed(); 87 uint64_t max_bytes_used = g_max_bytes_used[i]; 88 uint64_t total_bytes_used = g_total_bytes_used[i].LoadRelaxed(); 89 if (total_bytes_used != 0) { 90 os << static_cast<AllocatorTag>(i) << " active=" << bytes_used << " max=" 91 << max_bytes_used << " total=" << total_bytes_used << "\n"; 92 } 93 } 94 } 95 } 96 97 } // namespace TrackedAllocators 98 99 } // namespace art 100