blob: 2a2790c7d9f9820d4ccf15c19d9de8688c6eae7d [file] [log] [blame]
Brian Carlstrom413e89f2013-10-21 23:53:49 -07001/*
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 Chartierbad02672014-08-25 13:08:22 -070022#include "atomic.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070023#include "base/logging.h"
24
25namespace art {
26
Ian Rogerse77493c2014-08-20 15:08:45 -070027class MallocAllocator FINAL : public Allocator {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070028 public:
29 explicit MallocAllocator() {}
30 ~MallocAllocator() {}
31
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070032 void* Alloc(size_t size) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070033 return calloc(sizeof(uint8_t), size);
34 }
35
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070036 void Free(void* p) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070037 free(p);
38 }
39
40 private:
41 DISALLOW_COPY_AND_ASSIGN(MallocAllocator);
42};
43
44MallocAllocator g_malloc_allocator;
45
Ian Rogerse77493c2014-08-20 15:08:45 -070046class NoopAllocator FINAL : public Allocator {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070047 public:
48 explicit NoopAllocator() {}
49 ~NoopAllocator() {}
50
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010051 void* Alloc(size_t size ATTRIBUTE_UNUSED) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070052 LOG(FATAL) << "NoopAllocator::Alloc should not be called";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070053 UNREACHABLE();
Brian Carlstrom413e89f2013-10-21 23:53:49 -070054 }
55
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010056 void Free(void* p ATTRIBUTE_UNUSED) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070057 // Noop.
58 }
59
60 private:
61 DISALLOW_COPY_AND_ASSIGN(NoopAllocator);
62};
63
64NoopAllocator g_noop_allocator;
65
66Allocator* Allocator::GetMallocAllocator() {
67 return &g_malloc_allocator;
68}
69
70Allocator* Allocator::GetNoopAllocator() {
71 return &g_noop_allocator;
72}
73
Ian Rogers7e70b002014-10-08 11:47:24 -070074namespace TrackedAllocators {
75
Mathieu Chartier6e88ef62014-10-14 15:01:24 -070076// These globals are safe since they don't have any non-trivial destructors.
Ian Rogers7e70b002014-10-08 11:47:24 -070077Atomic<size_t> g_bytes_used[kAllocatorTagCount];
78volatile size_t g_max_bytes_used[kAllocatorTagCount];
79Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount];
80
81void Dump(std::ostream& os) {
Mathieu Chartierbad02672014-08-25 13:08:22 -070082 if (kEnableTrackingAllocator) {
83 os << "Dumping native memory usage\n";
84 for (size_t i = 0; i < kAllocatorTagCount; ++i) {
Ian Rogers7e70b002014-10-08 11:47:24 -070085 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 Chartierbad02672014-08-25 13:08:22 -070088 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 Carlstrom413e89f2013-10-21 23:53:49 -070095
Ian Rogers7e70b002014-10-08 11:47:24 -070096} // namespace TrackedAllocators
97
Brian Carlstrom413e89f2013-10-21 23:53:49 -070098} // namespace art