| 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" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 27 | class MallocAllocator FINAL : public Allocator { |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 28 | public: |
| 29 | explicit MallocAllocator() {} |
| 30 | ~MallocAllocator() {} |
| 31 | |
| Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 32 | void* Alloc(size_t size) { |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 33 | return calloc(sizeof(uint8_t), size); |
| 34 | } |
| 35 | |
| Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 36 | void Free(void* p) { |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 37 | free(p); |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | DISALLOW_COPY_AND_ASSIGN(MallocAllocator); |
| 42 | }; |
| 43 | |
| 44 | MallocAllocator g_malloc_allocator; |
| 45 | |
| Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 46 | class NoopAllocator FINAL : public Allocator { |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 47 | public: |
| 48 | explicit NoopAllocator() {} |
| 49 | ~NoopAllocator() {} |
| 50 | |
| Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 51 | void* Alloc(size_t size ATTRIBUTE_UNUSED) { |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 52 | LOG(FATAL) << "NoopAllocator::Alloc should not be called"; |
| Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 53 | UNREACHABLE(); |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 56 | void Free(void* p ATTRIBUTE_UNUSED) { |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 57 | // Noop. |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | DISALLOW_COPY_AND_ASSIGN(NoopAllocator); |
| 62 | }; |
| 63 | |
| 64 | NoopAllocator g_noop_allocator; |
| 65 | |
| 66 | Allocator* Allocator::GetMallocAllocator() { |
| 67 | return &g_malloc_allocator; |
| 68 | } |
| 69 | |
| 70 | Allocator* Allocator::GetNoopAllocator() { |
| 71 | return &g_noop_allocator; |
| 72 | } |
| 73 | |
| Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 74 | namespace TrackedAllocators { |
| 75 | |
| Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 76 | // These globals are safe since they don't have any non-trivial destructors. |
| Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 77 | Atomic<size_t> g_bytes_used[kAllocatorTagCount]; |
| 78 | volatile size_t g_max_bytes_used[kAllocatorTagCount]; |
| 79 | Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount]; |
| 80 | |
| 81 | void Dump(std::ostream& os) { |
| Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 82 | if (kEnableTrackingAllocator) { |
| 83 | os << "Dumping native memory usage\n"; |
| 84 | for (size_t i = 0; i < kAllocatorTagCount; ++i) { |
| Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 85 | uint64_t bytes_used = g_bytes_used[i].LoadRelaxed(); |
| 86 | uint64_t max_bytes_used = g_max_bytes_used[i]; |
| 87 | uint64_t total_bytes_used = g_total_bytes_used[i].LoadRelaxed(); |
| Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 88 | if (total_bytes_used != 0) { |
| 89 | os << static_cast<AllocatorTag>(i) << " active=" << bytes_used << " max=" |
| 90 | << max_bytes_used << " total=" << total_bytes_used << "\n"; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 95 | |
| Ian Rogers | 7e70b00 | 2014-10-08 11:47:24 -0700 | [diff] [blame] | 96 | } // namespace TrackedAllocators |
| 97 | |
| Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 98 | } // namespace art |