blob: d4142f824cca2c566d94116a59f53a65cb39cc8f [file] [log] [blame]
Mathieu Chartier0a9dc052013-07-25 11:01:28 -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#ifndef ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_
18#define ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_
19
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070020#include "utils.h"
21
22#include <cstdlib>
23#include <limits>
24#include <memory>
25
26namespace art {
27namespace gc {
28namespace accounting {
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070029
Ian Rogers8d31bbd2013-10-13 10:44:14 -070030void* RegisterGcAllocation(size_t bytes);
31void RegisterGcDeallocation(void* p, size_t bytes);
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070032
Mathieu Chartier2e290fb2014-08-21 12:21:48 -070033static constexpr bool kMeasureGcMemoryOverhead = false;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070034
Ian Rogers8d31bbd2013-10-13 10:44:14 -070035template <typename T>
36class GcAllocatorImpl : public std::allocator<T> {
37 public:
38 typedef typename std::allocator<T>::value_type value_type;
39 typedef typename std::allocator<T>::size_type size_type;
40 typedef typename std::allocator<T>::difference_type difference_type;
41 typedef typename std::allocator<T>::pointer pointer;
42 typedef typename std::allocator<T>::const_pointer const_pointer;
43 typedef typename std::allocator<T>::reference reference;
44 typedef typename std::allocator<T>::const_reference const_reference;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070045
Ian Rogers8d31bbd2013-10-13 10:44:14 -070046 // Used internally by STL data structures.
47 template <class U>
48 GcAllocatorImpl(const GcAllocatorImpl<U>& alloc) throw() {
49 }
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070050
Ian Rogers8d31bbd2013-10-13 10:44:14 -070051 // Used internally by STL data structures.
52 GcAllocatorImpl() throw() {
53 }
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070054
Ian Rogers8d31bbd2013-10-13 10:44:14 -070055 // Enables an allocator for objects of one type to allocate storage for objects of another type.
56 // Used internally by STL data structures.
57 template <class U>
58 struct rebind {
59 typedef GcAllocatorImpl<U> other;
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070060 };
61
Ian Rogers8d31bbd2013-10-13 10:44:14 -070062 pointer allocate(size_type n, const_pointer hint = 0) {
63 return reinterpret_cast<pointer>(RegisterGcAllocation(n * sizeof(T)));
64 }
65
66 template <typename PT>
67 void deallocate(PT p, size_type n) {
68 RegisterGcDeallocation(p, n * sizeof(T));
69 }
70};
71
72// C++ doesn't allow template typedefs. This is a workaround template typedef which is
73// GCAllocatorImpl<T> if kMeasureGCMemoryOverhead is true, std::allocator<T> otherwise.
74template <typename T>
75class GcAllocator : public TypeStaticIf<kMeasureGcMemoryOverhead, GcAllocatorImpl<T>,
Ian Rogers700a4022014-05-19 16:49:03 -070076 std::allocator<T>>::type {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070077};
78
Mathieu Chartier02e25112013-08-14 16:14:24 -070079} // namespace accounting
80} // namespace gc
81} // namespace art
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070082
83#endif // ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_