Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 22 | #include "atomic.h" |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 23 | #include "base/logging.h" |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 24 | #include "thread-inl.h" |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 28 | Atomic<uint64_t> TrackedAllocators::bytes_used_[kAllocatorTagCount]; |
| 29 | Atomic<uint64_t> TrackedAllocators::max_bytes_used_[kAllocatorTagCount]; |
| 30 | Atomic<uint64_t> TrackedAllocators::total_bytes_used_[kAllocatorTagCount]; |
| 31 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 32 | class MallocAllocator FINAL : public Allocator { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 33 | public: |
| 34 | explicit MallocAllocator() {} |
| 35 | ~MallocAllocator() {} |
| 36 | |
| 37 | virtual void* Alloc(size_t size) { |
| 38 | return calloc(sizeof(uint8_t), size); |
| 39 | } |
| 40 | |
| 41 | virtual void Free(void* p) { |
| 42 | free(p); |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | DISALLOW_COPY_AND_ASSIGN(MallocAllocator); |
| 47 | }; |
| 48 | |
| 49 | MallocAllocator g_malloc_allocator; |
| 50 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 51 | class NoopAllocator FINAL : public Allocator { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 52 | public: |
| 53 | explicit NoopAllocator() {} |
| 54 | ~NoopAllocator() {} |
| 55 | |
| 56 | virtual void* Alloc(size_t size) { |
| 57 | LOG(FATAL) << "NoopAllocator::Alloc should not be called"; |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | virtual void Free(void* p) { |
| 62 | // Noop. |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | DISALLOW_COPY_AND_ASSIGN(NoopAllocator); |
| 67 | }; |
| 68 | |
| 69 | NoopAllocator g_noop_allocator; |
| 70 | |
| 71 | Allocator* Allocator::GetMallocAllocator() { |
| 72 | return &g_malloc_allocator; |
| 73 | } |
| 74 | |
| 75 | Allocator* Allocator::GetNoopAllocator() { |
| 76 | return &g_noop_allocator; |
| 77 | } |
| 78 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 79 | void TrackedAllocators::Dump(std::ostream& os) { |
| 80 | if (kEnableTrackingAllocator) { |
| 81 | os << "Dumping native memory usage\n"; |
| 82 | for (size_t i = 0; i < kAllocatorTagCount; ++i) { |
| 83 | uint64_t bytes_used = bytes_used_[i].LoadRelaxed(); |
| 84 | uint64_t max_bytes_used = max_bytes_used_[i].LoadRelaxed(); |
| 85 | uint64_t total_bytes_used = total_bytes_used_[i].LoadRelaxed(); |
| 86 | if (total_bytes_used != 0) { |
| 87 | os << static_cast<AllocatorTag>(i) << " active=" << bytes_used << " max=" |
| 88 | << max_bytes_used << " total=" << total_bytes_used << "\n"; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 93 | |
| 94 | } // namespace art |