blob: 4f2fc074fbab9f094efb90c1e4976a7f450a5f0b [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"
Mathieu Chartierbad02672014-08-25 13:08:22 -070024#include "thread-inl.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070025
26namespace art {
27
Ian Rogerse77493c2014-08-20 15:08:45 -070028class MallocAllocator FINAL : public Allocator {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070029 public:
30 explicit MallocAllocator() {}
31 ~MallocAllocator() {}
32
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070033 void* Alloc(size_t size) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070034 return calloc(sizeof(uint8_t), size);
35 }
36
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070037 void Free(void* p) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070038 free(p);
39 }
40
41 private:
42 DISALLOW_COPY_AND_ASSIGN(MallocAllocator);
43};
44
45MallocAllocator g_malloc_allocator;
46
Ian Rogerse77493c2014-08-20 15:08:45 -070047class NoopAllocator FINAL : public Allocator {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070048 public:
49 explicit NoopAllocator() {}
50 ~NoopAllocator() {}
51
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070052 void* Alloc(size_t size) {
53 UNUSED(size);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070054 LOG(FATAL) << "NoopAllocator::Alloc should not be called";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070055 UNREACHABLE();
Brian Carlstrom413e89f2013-10-21 23:53:49 -070056 }
57
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070058 void Free(void* p) {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070059 // Noop.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070060 UNUSED(p);
Brian Carlstrom413e89f2013-10-21 23:53:49 -070061 }
62
63 private:
64 DISALLOW_COPY_AND_ASSIGN(NoopAllocator);
65};
66
67NoopAllocator g_noop_allocator;
68
69Allocator* Allocator::GetMallocAllocator() {
70 return &g_malloc_allocator;
71}
72
73Allocator* Allocator::GetNoopAllocator() {
74 return &g_noop_allocator;
75}
76
Ian Rogers7e70b002014-10-08 11:47:24 -070077namespace TrackedAllocators {
78
Mathieu Chartier6e88ef62014-10-14 15:01:24 -070079// These globals are safe since they don't have any non-trivial destructors.
Ian Rogers7e70b002014-10-08 11:47:24 -070080Atomic<size_t> g_bytes_used[kAllocatorTagCount];
81volatile size_t g_max_bytes_used[kAllocatorTagCount];
82Atomic<uint64_t> g_total_bytes_used[kAllocatorTagCount];
83
84void Dump(std::ostream& os) {
Mathieu Chartierbad02672014-08-25 13:08:22 -070085 if (kEnableTrackingAllocator) {
86 os << "Dumping native memory usage\n";
87 for (size_t i = 0; i < kAllocatorTagCount; ++i) {
Ian Rogers7e70b002014-10-08 11:47:24 -070088 uint64_t bytes_used = g_bytes_used[i].LoadRelaxed();
89 uint64_t max_bytes_used = g_max_bytes_used[i];
90 uint64_t total_bytes_used = g_total_bytes_used[i].LoadRelaxed();
Mathieu Chartierbad02672014-08-25 13:08:22 -070091 if (total_bytes_used != 0) {
92 os << static_cast<AllocatorTag>(i) << " active=" << bytes_used << " max="
93 << max_bytes_used << " total=" << total_bytes_used << "\n";
94 }
95 }
96 }
97}
Brian Carlstrom413e89f2013-10-21 23:53:49 -070098
Ian Rogers7e70b002014-10-08 11:47:24 -070099} // namespace TrackedAllocators
100
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700101} // namespace art