summaryrefslogtreecommitdiffstats
path: root/runtime/gc/accounting/gc_allocator.h
blob: d4142f824cca2c566d94116a59f53a65cb39cc8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_
#define ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_

#include "utils.h"

#include <cstdlib>
#include <limits>
#include <memory>

namespace art {
namespace gc {
namespace accounting {

void* RegisterGcAllocation(size_t bytes);
void RegisterGcDeallocation(void* p, size_t bytes);

static constexpr bool kMeasureGcMemoryOverhead = false;

template <typename T>
class GcAllocatorImpl : public std::allocator<T> {
 public:
  typedef typename std::allocator<T>::value_type value_type;
  typedef typename std::allocator<T>::size_type size_type;
  typedef typename std::allocator<T>::difference_type difference_type;
  typedef typename std::allocator<T>::pointer pointer;
  typedef typename std::allocator<T>::const_pointer const_pointer;
  typedef typename std::allocator<T>::reference reference;
  typedef typename std::allocator<T>::const_reference const_reference;

  // Used internally by STL data structures.
  template <class U>
  GcAllocatorImpl(const GcAllocatorImpl<U>& alloc) throw() {
  }

  // Used internally by STL data structures.
  GcAllocatorImpl() throw() {
  }

  // Enables an allocator for objects of one type to allocate storage for objects of another type.
  // Used internally by STL data structures.
  template <class U>
  struct rebind {
    typedef GcAllocatorImpl<U> other;
  };

  pointer allocate(size_type n, const_pointer hint = 0) {
    return reinterpret_cast<pointer>(RegisterGcAllocation(n * sizeof(T)));
  }

  template <typename PT>
  void deallocate(PT p, size_type n) {
    RegisterGcDeallocation(p, n * sizeof(T));
  }
};

// C++ doesn't allow template typedefs. This is a workaround template typedef which is
// GCAllocatorImpl<T> if kMeasureGCMemoryOverhead is true, std::allocator<T> otherwise.
template <typename T>
class GcAllocator : public TypeStaticIf<kMeasureGcMemoryOverhead, GcAllocatorImpl<T>,
                                        std::allocator<T>>::type {
};

}  // namespace accounting
}  // namespace gc
}  // namespace art

#endif  // ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_