Home | History | Annotate | Download | only in accounting
      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 
     20 #include "utils.h"
     21 
     22 #include <cstdlib>
     23 #include <limits>
     24 #include <memory>
     25 
     26 namespace art {
     27 namespace gc {
     28 namespace accounting {
     29   void* RegisterGCAllocation(size_t bytes);
     30   void RegisterGCDeAllocation(void* p, size_t bytes);
     31 
     32   static const bool kMeasureGCMemoryOverhead = false;
     33 
     34   template <typename T>
     35   class GCAllocatorImpl : public std::allocator<T> {
     36   public:
     37     typedef typename std::allocator<T>::value_type value_type;
     38     typedef typename std::allocator<T>::size_type size_type;
     39     typedef typename std::allocator<T>::difference_type difference_type;
     40     typedef typename std::allocator<T>::pointer pointer;
     41     typedef typename std::allocator<T>::const_pointer const_pointer;
     42     typedef typename std::allocator<T>::reference reference;
     43     typedef typename std::allocator<T>::const_reference const_reference;
     44 
     45     // Used internally by STL data structures.
     46     template <class U>
     47     GCAllocatorImpl(const GCAllocatorImpl<U>& alloc) throw() {
     48     }
     49 
     50     // Used internally by STL data structures.
     51     GCAllocatorImpl() throw() {
     52     }
     53 
     54     // Enables an allocator for objects of one type to allocate storage for objects of another type.
     55     // Used internally by STL data structures.
     56     template <class U>
     57     struct rebind {
     58         typedef GCAllocatorImpl<U> other;
     59     };
     60 
     61     pointer allocate(size_type n, const_pointer hint = 0) {
     62       return reinterpret_cast<pointer>(RegisterGCAllocation(n * sizeof(T)));
     63     }
     64 
     65     template <typename PT>
     66     void deallocate(PT p, size_type n) {
     67       RegisterGCDeAllocation(p, n * sizeof(T));
     68     }
     69   };
     70 
     71   // C++ doesn't allow template typedefs. This is a workaround template typedef which is
     72   // GCAllocatorImpl<T> if kMeasureGCMemoryOverhead is true, std::allocator<T> otherwise.
     73   template <typename T>
     74   class GCAllocator : public TypeStaticIf<kMeasureGCMemoryOverhead,
     75                                           GCAllocatorImpl<T>,
     76                                           std::allocator<T> >::value {
     77   };
     78 }  // namespace accounting
     79 }  // namespace gc
     80 }  // namespace art
     81 
     82 #endif  // ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_
     83