blob: 64cdbbf2d79f2fff7f59e2834790fd4a74d306bc [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
Mathieu Chartierbad02672014-08-25 13:08:22 -070028Atomic<uint64_t> TrackedAllocators::bytes_used_[kAllocatorTagCount];
29Atomic<uint64_t> TrackedAllocators::max_bytes_used_[kAllocatorTagCount];
30Atomic<uint64_t> TrackedAllocators::total_bytes_used_[kAllocatorTagCount];
31
Ian Rogerse77493c2014-08-20 15:08:45 -070032class MallocAllocator FINAL : public Allocator {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070033 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
49MallocAllocator g_malloc_allocator;
50
Ian Rogerse77493c2014-08-20 15:08:45 -070051class NoopAllocator FINAL : public Allocator {
Brian Carlstrom413e89f2013-10-21 23:53:49 -070052 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
69NoopAllocator g_noop_allocator;
70
71Allocator* Allocator::GetMallocAllocator() {
72 return &g_malloc_allocator;
73}
74
75Allocator* Allocator::GetNoopAllocator() {
76 return &g_noop_allocator;
77}
78
Mathieu Chartierbad02672014-08-25 13:08:22 -070079void 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 Carlstrom413e89f2013-10-21 23:53:49 -070093
94} // namespace art