Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 20 | #include "utils.h" |
| 21 | |
| 22 | #include <cstdlib> |
| 23 | #include <limits> |
| 24 | #include <memory> |
| 25 | |
| 26 | namespace art { |
| 27 | namespace gc { |
| 28 | namespace accounting { |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 29 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 30 | void* RegisterGcAllocation(size_t bytes); |
| 31 | void RegisterGcDeallocation(void* p, size_t bytes); |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 32 | |
Mathieu Chartier | 2e290fb | 2014-08-21 12:21:48 -0700 | [diff] [blame] | 33 | static constexpr bool kMeasureGcMemoryOverhead = false; |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 34 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 35 | template <typename T> |
| 36 | class 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 Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 45 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 46 | // Used internally by STL data structures. |
| 47 | template <class U> |
| 48 | GcAllocatorImpl(const GcAllocatorImpl<U>& alloc) throw() { |
| 49 | } |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 50 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 51 | // Used internally by STL data structures. |
| 52 | GcAllocatorImpl() throw() { |
| 53 | } |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 54 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 55 | // 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 Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 62 | 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. |
| 74 | template <typename T> |
| 75 | class GcAllocator : public TypeStaticIf<kMeasureGcMemoryOverhead, GcAllocatorImpl<T>, |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 76 | std::allocator<T>>::type { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 79 | } // namespace accounting |
| 80 | } // namespace gc |
| 81 | } // namespace art |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 82 | |
| 83 | #endif // ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_ |