1 /* 2 * Copyright (C) 2016 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_ALLOCATION_LISTENER_H_ 18 #define ART_RUNTIME_GC_ALLOCATION_LISTENER_H_ 19 20 #include <list> 21 #include <memory> 22 23 #include "base/locks.h" 24 #include "base/macros.h" 25 #include "gc_root.h" 26 #include "handle.h" 27 #include "obj_ptr.h" 28 29 namespace art { 30 31 namespace mirror { 32 class Class; 33 class Object; 34 } // namespace mirror 35 36 class Thread; 37 38 namespace gc { 39 40 class AllocationListener { 41 public: ~AllocationListener()42 virtual ~AllocationListener() {} 43 44 // An event to allow a listener to intercept and modify an allocation before it takes place. 45 // The listener can change the byte_count and type as they see fit. Extreme caution should be used 46 // when doing so. This can also be used to control allocation occurring on another thread. 47 // 48 // Concurrency guarantees: This might be called multiple times for each single allocation. It's 49 // guaranteed that, between the final call to the callback and the object being visible to 50 // heap-walks there are no suspensions. If a suspension was allowed between these events the 51 // callback will be invoked again after passing the suspend point. 52 // 53 // If the alloc succeeds it is guaranteed there are no suspend-points between the last return of 54 // PreObjectAlloc and the newly allocated object being visible to heap-walks. 55 // 56 // This can also be used to make any last-minute changes to the type or size of the allocation. PreObjectAllocated(Thread * self ATTRIBUTE_UNUSED,MutableHandle<mirror::Class> type ATTRIBUTE_UNUSED,size_t * byte_count ATTRIBUTE_UNUSED)57 virtual void PreObjectAllocated(Thread* self ATTRIBUTE_UNUSED, 58 MutableHandle<mirror::Class> type ATTRIBUTE_UNUSED, 59 size_t* byte_count ATTRIBUTE_UNUSED) 60 REQUIRES(!Roles::uninterruptible_) REQUIRES_SHARED(Locks::mutator_lock_) {} 61 // Fast check if we want to get the PreObjectAllocated callback, to avoid the expense of creating 62 // handles. Defaults to false. HasPreAlloc()63 virtual bool HasPreAlloc() const { return false; } 64 virtual void ObjectAllocated(Thread* self, ObjPtr<mirror::Object>* obj, size_t byte_count) 65 REQUIRES_SHARED(Locks::mutator_lock_) = 0; 66 }; 67 68 } // namespace gc 69 } // namespace art 70 71 #endif // ART_RUNTIME_GC_ALLOCATION_LISTENER_H_ 72