/* * 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 #include #include namespace art { namespace gc { namespace accounting { void* RegisterGcAllocation(size_t bytes); void RegisterGcDeallocation(void* p, size_t bytes); static const bool kMeasureGcMemoryOverhead = false; template class GcAllocatorImpl : public std::allocator { public: typedef typename std::allocator::value_type value_type; typedef typename std::allocator::size_type size_type; typedef typename std::allocator::difference_type difference_type; typedef typename std::allocator::pointer pointer; typedef typename std::allocator::const_pointer const_pointer; typedef typename std::allocator::reference reference; typedef typename std::allocator::const_reference const_reference; // Used internally by STL data structures. template GcAllocatorImpl(const GcAllocatorImpl& 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 struct rebind { typedef GcAllocatorImpl other; }; pointer allocate(size_type n, const_pointer hint = 0) { return reinterpret_cast(RegisterGcAllocation(n * sizeof(T))); } template 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 if kMeasureGCMemoryOverhead is true, std::allocator otherwise. template class GcAllocator : public TypeStaticIf, std::allocator >::value { }; } // namespace accounting } // namespace gc } // namespace art #endif // ART_RUNTIME_GC_ACCOUNTING_GC_ALLOCATOR_H_